2012-05-23 17:01:30 +10:00
|
|
|
/*
|
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
|
2012-05-23 17:01:30 +10:00
|
|
|
*
|
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.
|
2012-05-23 17:01:30 +10:00
|
|
|
*
|
2015-05-18 14:34:48 +10:00
|
|
|
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
|
2012-05-23 17:01:30 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "noise/basis.hpp"
|
|
|
|
#include "noise/lut.hpp"
|
2012-05-23 20:31:59 +10:00
|
|
|
|
2012-05-25 15:19:07 +10:00
|
|
|
#include "../debug.hpp"
|
2012-05-23 20:31:59 +10:00
|
|
|
#include "../point.hpp"
|
2012-05-23 17:01:30 +10:00
|
|
|
#include "../random.hpp"
|
2012-05-25 15:19:07 +10:00
|
|
|
#include "../vector.hpp"
|
2015-05-26 16:26:43 +10:00
|
|
|
#include "../hash/murmur/murmur2.hpp"
|
2012-05-23 17:01:30 +10:00
|
|
|
|
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;
|
|
|
|
|
2012-05-23 17:01:30 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Generate a type from [-UNIT..UNIT]
|
|
|
|
template <typename T>
|
|
|
|
T
|
2015-05-18 17:16:04 +10:00
|
|
|
generate (intmax_t x, intmax_t y, uint64_t seed)
|
|
|
|
{
|
2015-05-26 16:26:43 +10:00
|
|
|
using util::hash::murmur2::mix;
|
|
|
|
|
|
|
|
T v = mix (seed, mix (uint64_t (y), uint64_t (x))) & 0xffff;
|
|
|
|
v = v / T{0xffff} * 2 - 1;
|
|
|
|
|
|
|
|
CHECK_GE (v, T{0});
|
|
|
|
CHECK_LE (v, T{1});
|
|
|
|
|
|
|
|
return v;
|
2015-05-18 17:16:04 +10:00
|
|
|
}
|
2012-05-23 17:01:30 +10:00
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-05-23 17:01:30 +10:00
|
|
|
template <>
|
2015-05-18 17:16:04 +10:00
|
|
|
util::vector2d
|
|
|
|
generate (intmax_t x, intmax_t y, uint64_t seed)
|
|
|
|
{
|
2015-05-26 16:26:43 +10:00
|
|
|
using util::hash::murmur2::mix;
|
|
|
|
|
|
|
|
auto u = mix (seed, mix (uint64_t (x), uint64_t (y)));
|
|
|
|
auto v = mix (u, seed);
|
|
|
|
|
|
|
|
auto r = util::vector2d {
|
|
|
|
(u & 0xffff) / double{0xffff},
|
|
|
|
(v & 0xffff) / double{0xffff}
|
|
|
|
} * 2.0 - 1.0;
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-26 16:26:43 +10:00
|
|
|
CHECK_GE (r, double{-1});
|
|
|
|
CHECK_LE (r, double{ 1});
|
|
|
|
|
|
|
|
return r;
|
2015-05-18 17:16:04 +10:00
|
|
|
}
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-05-23 17:01:30 +10:00
|
|
|
template <>
|
2015-05-18 17:16:04 +10:00
|
|
|
util::vector2f
|
|
|
|
generate (intmax_t x, intmax_t y, uint64_t seed)
|
|
|
|
{
|
2015-05-26 16:26:43 +10:00
|
|
|
using util::hash::murmur2::mix;
|
|
|
|
|
|
|
|
auto u = mix (seed, mix (uint64_t (x), uint64_t (y)));
|
|
|
|
auto v = mix (u, seed);
|
|
|
|
|
|
|
|
auto r = util::vector2f {
|
|
|
|
(u & 0xffff) / float{0xffff},
|
|
|
|
(v & 0xffff) / float{0xffff}
|
|
|
|
} * 2.f - 1.f;
|
|
|
|
|
|
|
|
CHECK_GE (r, float (-1));
|
|
|
|
CHECK_LE (r, float ( 1));
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-26 16:26:43 +10:00
|
|
|
return r;
|
2012-05-23 17:01:30 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T>
|
|
|
|
basis<T>::basis (seed_t _seed):
|
2012-05-23 17:01:30 +10:00
|
|
|
seed (_seed)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T>
|
|
|
|
basis<T>::basis ():
|
2012-05-23 17:01:30 +10:00
|
|
|
seed (util::random<seed_t> ())
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T>
|
|
|
|
basis<T>::~basis ()
|
2012-05-23 17:01:30 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T>
|
|
|
|
T
|
2015-05-18 22:05:39 +10:00
|
|
|
basis<T>::operator() (T, T) const
|
|
|
|
{
|
|
|
|
unreachable ();
|
|
|
|
}
|
2012-05-23 17:01:30 +10:00
|
|
|
|
|
|
|
|
2015-05-18 17:16:04 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
namespace util { namespace noise {
|
|
|
|
template struct basis<float>;
|
|
|
|
template struct basis<double>;
|
|
|
|
} }
|
|
|
|
|
2012-05-23 17:01:30 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T, util::noise::lerp_t<T> L>
|
|
|
|
value<T,L>::value (seed_t _seed):
|
|
|
|
basis<T> (_seed)
|
2012-05-23 17:01:30 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T, util::noise::lerp_t<T> L>
|
|
|
|
value<T,L>::value ()
|
2012-05-23 17:01:30 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T, util::noise::lerp_t<T> L>
|
|
|
|
util::range<T>
|
|
|
|
value<T,L>::bounds (void) const
|
2015-05-18 22:05:39 +10:00
|
|
|
{
|
|
|
|
return { -1, 1 };
|
|
|
|
}
|
2012-05-24 15:04:06 +10:00
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T, util::noise::lerp_t<T> L>
|
|
|
|
T
|
2015-05-18 22:05:39 +10:00
|
|
|
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);
|
2015-05-18 17:16:04 +10:00
|
|
|
T x_fac = x - x_int;
|
|
|
|
T y_fac = y - y_int;
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2012-05-24 17:08:11 +10:00
|
|
|
// 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.
|
2015-05-18 17:16:04 +10:00
|
|
|
if (x < 0) { x_fac = 1 + x_fac; x_int -= 1; }
|
|
|
|
if (y < 0) { y_fac = 1 + y_fac; y_int -= 1; }
|
2012-05-24 17:08:11 +10:00
|
|
|
|
2012-05-23 17:01:30 +10:00
|
|
|
// Generate the four corner values
|
2015-05-18 17:16:04 +10:00
|
|
|
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);
|
2012-05-23 17:01:30 +10:00
|
|
|
|
|
|
|
// Interpolate on one dimension, then the other.
|
|
|
|
return L (L (p0, p1, x_fac),
|
|
|
|
L (p2, p3, x_fac),
|
|
|
|
y_fac);
|
|
|
|
}
|
|
|
|
|
2012-06-13 14:41:53 +10:00
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 17:16:04 +10:00
|
|
|
namespace util { namespace noise {
|
2015-05-26 16:27:56 +10:00
|
|
|
template struct value<float, lerp::trunc>;
|
2015-05-18 17:16:04 +10:00
|
|
|
template struct value<float, lerp::linear>;
|
|
|
|
template struct value<float, lerp::cubic>;
|
|
|
|
template struct value<float, lerp::quintic>;
|
|
|
|
|
2015-05-26 16:27:56 +10:00
|
|
|
template struct value<double, lerp::trunc>;
|
2015-05-18 17:16:04 +10:00
|
|
|
template struct value<double, lerp::linear>;
|
|
|
|
template struct value<double, lerp::cubic>;
|
|
|
|
template struct value<double, lerp::quintic>;
|
|
|
|
} }
|
2012-05-23 17:01:30 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T, util::noise::lerp_t<T> L>
|
|
|
|
gradient<T,L>::gradient (seed_t _seed):
|
|
|
|
basis<T> (_seed)
|
2012-05-23 17:01:30 +10:00
|
|
|
{ ; }
|
|
|
|
|
2012-05-24 15:04:06 +10:00
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T, util::noise::lerp_t<T> L>
|
|
|
|
gradient<T,L>::gradient ()
|
2012-05-23 17:01:30 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 17:16:04 +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 }; }
|
2012-05-24 15:04:06 +10:00
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T, util::noise::lerp_t<T> L>
|
|
|
|
T
|
2015-05-18 22:05:39 +10:00
|
|
|
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);
|
2015-05-18 17:16:04 +10:00
|
|
|
T x_fac = x - x_int;
|
|
|
|
T y_fac = y - y_int;
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2012-05-24 17:08:11 +10:00
|
|
|
// 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.
|
2015-05-18 17:16:04 +10:00
|
|
|
if (x < 0) { x_fac = 1 + x_fac; x_int -= 1; }
|
|
|
|
if (y < 0) { y_fac = 1 + y_fac; y_int -= 1; }
|
2012-05-24 17:08:11 +10:00
|
|
|
|
2012-05-24 15:04:06 +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.
|
2015-05-18 17:16:04 +10:00
|
|
|
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 ();
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-18 17:16:04 +10:00
|
|
|
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);
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-26 16:28:34 +10:00
|
|
|
auto L0 = L (v0, v1, x_fac);
|
|
|
|
auto L1 = L (v2, v3, x_fac);
|
|
|
|
auto L_ = L (L0, L1, y_fac);
|
|
|
|
return L_;
|
2012-05-23 17:01:30 +10:00
|
|
|
}
|
|
|
|
|
2012-06-13 14:41:53 +10:00
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-06-13 14:41:53 +10:00
|
|
|
namespace util {
|
|
|
|
namespace noise {
|
2015-05-18 17:16:04 +10:00
|
|
|
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-06-13 14:41:53 +10:00
|
|
|
}
|
|
|
|
}
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2012-05-23 20:31:59 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-05-18 17:16:04 +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
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T>
|
|
|
|
cellular<T>::cellular ()
|
2012-05-23 20:31:59 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T>
|
|
|
|
util::range<T>
|
|
|
|
cellular<T>::bounds (void) const
|
2012-05-24 17:08:47 +10:00
|
|
|
{ return { 0.0, 1.5 }; }
|
2012-05-24 15:04:06 +10:00
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 17:16:04 +10:00
|
|
|
template <typename T>
|
|
|
|
T
|
2015-05-18 22:05:39 +10:00
|
|
|
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);
|
2015-05-18 17:16:04 +10:00
|
|
|
T x_fac = x - x_int;
|
|
|
|
T y_fac = y - y_int;
|
2012-05-23 20:31:59 +10:00
|
|
|
|
2012-05-24 17:08:11 +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.
|
2015-05-18 17:16:04 +10:00
|
|
|
if (x < 0) { x_fac = 1 + x_fac; x_int -= 1; }
|
|
|
|
if (y < 0) { y_fac = 1 + y_fac; y_int -= 1; }
|
2012-05-24 17:08:11 +10:00
|
|
|
|
2012-05-23 20:31:59 +10:00
|
|
|
// +---+---+---+
|
|
|
|
// | 0 | 1 | 2 |
|
|
|
|
// +---+---+---+
|
|
|
|
// | 3 | 4 | 5 |
|
|
|
|
// +---+-------+
|
|
|
|
// | 6 | 7 | 8 |
|
|
|
|
// +---+---+---+
|
|
|
|
|
2015-05-18 17:16:04 +10:00
|
|
|
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) {
|
2015-05-18 17:16:04 +10:00
|
|
|
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
|
|
|
|
2015-05-18 17:16:04 +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));
|
2015-01-28 14:49:34 +11:00
|
|
|
CHECK_GE (distances[0], 0);
|
|
|
|
CHECK (bounds ().contains (distances[0]));
|
2012-05-23 20:31:59 +10:00
|
|
|
return distances[0];
|
|
|
|
}
|
2015-05-18 17:16:04 +10:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
namespace util { namespace noise {
|
|
|
|
template struct cellular<float>;
|
|
|
|
template struct cellular<double>;
|
|
|
|
} }
|