libcruft-util/noise/basis.cpp

315 lines
9.2 KiB
C++
Raw Normal View History

/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
2015-05-18 14:34:48 +10:00
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/
#include "noise/basis.hpp"
#include "noise/lut.hpp"
2012-05-23 20:31:59 +10:00
#include "../debug.hpp"
2012-05-23 20:31:59 +10:00
#include "../point.hpp"
#include "../random.hpp"
#include "../vector.hpp"
2012-05-23 20:31:59 +10:00
#include <algorithm>
2015-05-18 14:34:48 +10:00
using util::noise::basis;
using util::noise::value;
using util::noise::gradient;
using util::noise::cellular;
///////////////////////////////////////////////////////////////////////////////
// Generate a type from [-UNIT..UNIT]
template <typename T>
T
generate (intmax_t x, intmax_t y, uint64_t seed)
{
size_t idx = util::noise::permute (x, y, seed);
return T(util::noise::LUT[idx]);
}
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <>
util::vector2d
generate (intmax_t x, intmax_t y, uint64_t seed)
{
auto u = util::noise::permute (x, y, seed);
auto v = util::noise::permute (u ^ seed);
return {
util::noise::LUT[u],
util::noise::LUT[v]
};
}
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <>
util::vector2f
generate (intmax_t x, intmax_t y, uint64_t seed)
{
2015-05-18 14:34:48 +10:00
auto u = util::noise::permute (x, y, seed);
auto v = util::noise::permute (u ^ seed);
return {
float (util::noise::LUT[u]),
float (util::noise::LUT[v])
};
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
basis<T>::basis (seed_t _seed):
seed (_seed)
{ ; }
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <typename T>
basis<T>::basis ():
seed (util::random<seed_t> ())
{ ; }
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <typename T>
basis<T>::~basis ()
{ ; }
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <typename T>
T
basis<T>::operator() (T, T) const
{
unreachable ();
}
//-----------------------------------------------------------------------------
namespace util { namespace noise {
template struct basis<float>;
template struct basis<double>;
} }
///////////////////////////////////////////////////////////////////////////////
template <typename T, util::noise::lerp_t<T> L>
value<T,L>::value (seed_t _seed):
basis<T> (_seed)
{ ; }
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <typename T, util::noise::lerp_t<T> L>
value<T,L>::value ()
{ ; }
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <typename T, util::noise::lerp_t<T> L>
util::range<T>
value<T,L>::bounds (void) const
{
return { -1, 1 };
}
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <typename T, util::noise::lerp_t<T> L>
T
value<T,L>::operator() (T x, T y) const
{
2012-05-23 20:31:59 +10:00
intmax_t x_int = static_cast<intmax_t> (x);
intmax_t y_int = static_cast<intmax_t> (y);
T x_fac = x - x_int;
T y_fac = y - y_int;
// Shift the coordinate system down a little to ensure we get unit weights
// for the lerp. It's better to do this than abs the fractional portion so
// we don't get reflections along the origin.
if (x < 0) { x_fac = 1 + x_fac; x_int -= 1; }
if (y < 0) { y_fac = 1 + y_fac; y_int -= 1; }
// Generate the four corner values
T p0 = generate<T> (x_int, y_int, this->seed);
T p1 = generate<T> (x_int + 1, y_int, this->seed);
T p2 = generate<T> (x_int, y_int + 1, this->seed);
T p3 = generate<T> (x_int + 1, y_int + 1, this->seed);
// Interpolate on one dimension, then the other.
return L (L (p0, p1, x_fac),
L (p2, p3, x_fac),
y_fac);
}
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
namespace util { namespace noise {
template struct value<float, lerp::linear>;
template struct value<float, lerp::cubic>;
template struct value<float, lerp::quintic>;
template struct value<double, lerp::linear>;
template struct value<double, lerp::cubic>;
template struct value<double, lerp::quintic>;
} }
///////////////////////////////////////////////////////////////////////////////
template <typename T, util::noise::lerp_t<T> L>
gradient<T,L>::gradient (seed_t _seed):
basis<T> (_seed)
{ ; }
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <typename T, util::noise::lerp_t<T> L>
gradient<T,L>::gradient ()
{ ; }
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <typename T, util::noise::lerp_t<T> L>
util::range<T>
gradient<T,L>::bounds (void) const
{ return { -std::sqrt(T{2}) / 2, std::sqrt (T{2}) / 2 }; }
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <typename T, util::noise::lerp_t<T> L>
T
gradient<T,L>::operator() (T x, T y) const
{
2012-05-23 20:31:59 +10:00
intmax_t x_int = static_cast<intmax_t> (x);
intmax_t y_int = static_cast<intmax_t> (y);
T x_fac = x - x_int;
T y_fac = y - y_int;
// Shift the coordinate system down a little to ensure we get unit weights
// for the lerp. It's better to do this than abs the fractional portion so
// we don't get reflections along the origin.
if (x < 0) { x_fac = 1 + x_fac; x_int -= 1; }
if (y < 0) { y_fac = 1 + y_fac; y_int -= 1; }
// Generate the four corner values. It's not strictly necessary to
// normalise the values, but we get a more consistent and visually
// appealing range of outputs with normalised values.
auto p0 = generate<vector<2,T>> (x_int, y_int, this->seed).normalise ();
auto p1 = generate<vector<2,T>> (x_int + 1, y_int, this->seed).normalise ();
auto p2 = generate<vector<2,T>> (x_int, y_int + 1, this->seed).normalise ();
auto p3 = generate<vector<2,T>> (x_int + 1, y_int + 1, this->seed).normalise ();
T v0 = p0.x * x_fac + p0.y * y_fac;
T v1 = p1.x * (x_fac - 1) + p1.y * y_fac;
T v2 = p2.x * x_fac + p2.y * (y_fac - 1);
T v3 = p3.x * (x_fac - 1) + p3.y * (y_fac - 1);
return L (L (v0, v1, x_fac),
L (v2, v3, x_fac),
y_fac);
}
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
namespace util {
namespace noise {
template struct gradient<float, lerp::linear>;
template struct gradient<float, lerp::cubic>;
template struct gradient<float, lerp::quintic>;
template struct gradient<double, lerp::linear>;
template struct gradient<double, lerp::cubic>;
template struct gradient<double, lerp::quintic>;
}
}
2012-05-23 20:31:59 +10:00
///////////////////////////////////////////////////////////////////////////////
template <typename T>
cellular<T>::cellular (seed_t _seed):
basis<T> (_seed)
2012-05-23 20:31:59 +10:00
{ ; }
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <typename T>
cellular<T>::cellular ()
2012-05-23 20:31:59 +10:00
{ ; }
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <typename T>
util::range<T>
cellular<T>::bounds (void) const
{ return { 0.0, 1.5 }; }
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
template <typename T>
T
cellular<T>::operator() (T x, T y) const
{
2012-05-23 20:31:59 +10:00
intmax_t x_int = static_cast<intmax_t> (x);
intmax_t y_int = static_cast<intmax_t> (y);
T x_fac = x - x_int;
T y_fac = y - y_int;
2012-05-23 20:31:59 +10:00
// Generate the four corner values. It's not strictly necessary to
// normalise the values, but we get a more consistent and visually
// appealing range of outputs with normalised values.
if (x < 0) { x_fac = 1 + x_fac; x_int -= 1; }
if (y < 0) { y_fac = 1 + y_fac; y_int -= 1; }
2012-05-23 20:31:59 +10:00
// +---+---+---+
// | 0 | 1 | 2 |
// +---+---+---+
// | 3 | 4 | 5 |
// +---+-------+
// | 6 | 7 | 8 |
// +---+---+---+
point<2,T> centre = { x_fac, y_fac };
T distances[9] = { std::numeric_limits<T>::quiet_NaN () };
T *cursor = distances;
2012-05-23 20:31:59 +10:00
for (signed y_off = -1; y_off <= 1 ; ++y_off)
for (signed x_off = -1; x_off <= 1; ++x_off) {
auto pos = point<2,T> (T (x_off), T (y_off));
auto off = generate<vector<2,T>> (x_int + x_off, y_int + y_off, this->seed);
off += T{1};
off /= T{2};
2012-05-23 20:31:59 +10:00
CHECK (off.x >= 0 && off.x <= 1);
CHECK (off.y >= 0 && off.y <= 1);
2012-05-23 20:31:59 +10:00
pos += off;
*cursor++ = pos.distance2 (centre);
}
std::sort (std::begin (distances), std::end (distances));
CHECK_GE (distances[0], 0);
CHECK (bounds ().contains (distances[0]));
2012-05-23 20:31:59 +10:00
return distances[0];
}
//-----------------------------------------------------------------------------
namespace util { namespace noise {
template struct cellular<float>;
template struct cellular<double>;
} }