libcruft-util/noise/basis.cpp

251 lines
6.7 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.
*
* Copyright 2012 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>
using namespace util::noise;
using util::range;
///////////////////////////////////////////////////////////////////////////////
// Generate a type from [-UNIT..UNIT]
template <typename T>
T
generate (intmax_t x, intmax_t y, basis::seed_t);
template <>
double
generate (intmax_t x, intmax_t y, basis::seed_t seed) {
size_t idx = permute (x, y, seed);
return LUT[idx];
}
template <>
util::vector2d
generate (intmax_t x, intmax_t y, basis::seed_t seed) {
auto u = permute (x, y, seed);
auto v = permute (u ^ seed);
return util::vector2d (LUT[u], LUT[v]);
}
///////////////////////////////////////////////////////////////////////////////
basis::basis (seed_t _seed):
seed (_seed)
{ ; }
basis::basis ():
seed (util::random<seed_t> ())
{ ; }
basis::~basis ()
{ ; }
double
basis::eval (double, double) const
{ unreachable (); }
///////////////////////////////////////////////////////////////////////////////
template <lerp_function L>
value<L>::value (seed_t _seed):
basis (_seed)
{ ; }
template <lerp_function L>
value<L>::value ()
{ ; }
template <lerp_function L>
range<double>
value<L>::bounds (void) const
{ return { -1.0, 1.0 }; }
template <lerp_function L>
2015-04-13 18:06:08 +10:00
double
value<L>::eval (double x, double 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);
double x_fac = x - x_int;
double 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.0 + x_fac; x_int -= 1; }
if (y < 0) { y_fac = 1.0 + y_fac; y_int -= 1; }
// Generate the four corner values
double p0 = generate<double> (x_int, y_int, this->seed);
double p1 = generate<double> (x_int + 1, y_int, this->seed);
double p2 = generate<double> (x_int, y_int + 1, this->seed);
double p3 = generate<double> (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);
}
namespace util {
namespace noise {
template struct value<lerp::linear>;
template struct value<lerp::cubic>;
template struct value<lerp::quintic>;
}
}
///////////////////////////////////////////////////////////////////////////////
template <lerp_function L>
gradient<L>::gradient (seed_t _seed):
basis (_seed)
{ ; }
template <lerp_function L>
gradient<L>::gradient ()
{ ; }
template <lerp_function L>
range<double>
gradient<L>::bounds (void) const
{ return { -sqrt(2.0) / 2.0, sqrt (2.0) / 2.0 }; }
template <lerp_function L>
2015-04-13 18:06:08 +10:00
double
gradient<L>::eval (double x, double 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);
double x_fac = x - x_int;
double 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.0 + x_fac; x_int -= 1; }
if (y < 0) { y_fac = 1.0 + 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.
vector2d p0 = generate<vector2d> (x_int, y_int, this->seed).normalise ();
vector2d p1 = generate<vector2d> (x_int + 1, y_int, this->seed).normalise ();
vector2d p2 = generate<vector2d> (x_int, y_int + 1, this->seed).normalise ();
vector2d p3 = generate<vector2d> (x_int + 1, y_int + 1, this->seed).normalise ();
double v0 = p0.x * x_fac + p0.y * y_fac;
double v1 = p1.x * (x_fac - 1.0) + p1.y * y_fac;
double v2 = p2.x * x_fac + p2.y * (y_fac - 1.0);
double v3 = p3.x * (x_fac - 1.0) + p3.y * (y_fac - 1.0);
return L (L (v0, v1, x_fac),
L (v2, v3, x_fac),
y_fac);
}
namespace util {
namespace noise {
template struct gradient<lerp::linear>;
template struct gradient<lerp::cubic>;
template struct gradient<lerp::quintic>;
}
}
2012-05-23 20:31:59 +10:00
///////////////////////////////////////////////////////////////////////////////
cellular::cellular (seed_t _seed):
basis (_seed)
{ ; }
cellular::cellular ()
{ ; }
range<double>
cellular::bounds (void) const
{ return { 0.0, 1.5 }; }
2012-05-23 20:31:59 +10:00
double
cellular::eval (double x, double y) const {
using util::point2d;
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);
double x_fac = x - x_int;
double y_fac = y - y_int;
// 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.0 + x_fac; x_int -= 1; }
if (y < 0) { y_fac = 1.0 + y_fac; y_int -= 1; }
2012-05-23 20:31:59 +10:00
// +---+---+---+
// | 0 | 1 | 2 |
// +---+---+---+
// | 3 | 4 | 5 |
// +---+-------+
// | 6 | 7 | 8 |
// +---+---+---+
point2d centre = { x_fac, y_fac };
2012-05-23 20:31:59 +10:00
double distances[9] = { std::numeric_limits<double>::quiet_NaN () };
double *cursor = distances;
for (signed y_off = -1; y_off <= 1 ; ++y_off)
for (signed x_off = -1; x_off <= 1; ++x_off) {
auto pos = point2d (double (x_off), double (y_off));
auto off = generate<vector2d> (x_int + x_off, y_int + y_off, this->seed);
off += 1.;
off /= 2.;
2012-05-23 20:31:59 +10:00
CHECK (off.x >= 0 && off.x <= 1.0);
CHECK (off.y >= 0 && off.y <= 1.0);
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];
}