noise: parameterise fractal summation types
This commit is contained in:
parent
c24387bf8f
commit
9b0abd8f7a
14
noise.cpp
14
noise.cpp
@ -24,25 +24,29 @@
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
void
|
||||
util::noise::fill (image::buffer<float> &pixels,
|
||||
const util::noise::fractal &gen)
|
||||
util::noise::fill (image::buffer<T> &pixels,
|
||||
const util::noise::fractal<T> &gen)
|
||||
{
|
||||
size_t h = pixels.h, s = pixels.s, w = pixels.w;
|
||||
float *data = pixels.data ();
|
||||
T *data = pixels.data ();
|
||||
|
||||
for (size_t y = 0; y < h; ++y)
|
||||
for (size_t x = 0; x < w; ++x)
|
||||
data[y * s + x] = gen (double(x), double(y));
|
||||
data[y * s + x] = gen (x, y);
|
||||
}
|
||||
|
||||
template void util::noise::fill (image::buffer<float>&, const util::noise::fractal<float>&);
|
||||
template void util::noise::fill (image::buffer<double>&, const util::noise::fractal<double>&);
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void
|
||||
util::noise::image2d (uint8_t *restrict pixels,
|
||||
size_t width,
|
||||
size_t height,
|
||||
const util::noise::fractal &p) {
|
||||
const util::noise::fractal<float> &p) {
|
||||
for (size_t y = 0; y < height; ++y)
|
||||
for (size_t x = 0; x < width; ++x) {
|
||||
double v = p.eval (x, y);
|
||||
|
@ -27,9 +27,10 @@
|
||||
|
||||
namespace util {
|
||||
namespace noise {
|
||||
void fill (image::buffer<float>&, const util::noise::fractal&);
|
||||
template <typename T>
|
||||
void fill (image::buffer<T>&, const util::noise::fractal<T>&);
|
||||
|
||||
void image2d (uint8_t *restrict pixels, size_t width, size_t height, const util::noise::fractal&);
|
||||
void image2d (uint8_t *restrict pixels, size_t width, size_t height, const util::noise::fractal<float>&);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,10 +21,16 @@
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
using util::noise::fractal;
|
||||
using util::noise::fbm;
|
||||
using util::noise::musgrave;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
util::noise::fractal::fractal (unsigned _octaves,
|
||||
double _frequency,
|
||||
double _lacunarity):
|
||||
template <typename T>
|
||||
fractal<T>::fractal (unsigned _octaves,
|
||||
T _frequency,
|
||||
T _lacunarity):
|
||||
octaves (_octaves),
|
||||
frequency (_frequency),
|
||||
lacunarity (_lacunarity)
|
||||
@ -32,54 +38,64 @@ util::noise::fractal::fractal (unsigned _octaves,
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
util::noise::fractal::fractal ():
|
||||
template <typename T>
|
||||
fractal<T>::fractal ():
|
||||
octaves (1),
|
||||
frequency (0.1),
|
||||
lacunarity (0.6)
|
||||
frequency (T(0.1)),
|
||||
lacunarity (T(0.6))
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
util::noise::fractal::~fractal ()
|
||||
template <typename T>
|
||||
fractal<T>::~fractal ()
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
double
|
||||
util::noise::fractal::eval (double, double) const
|
||||
template <typename T>
|
||||
T
|
||||
fractal<T>::eval (T, T) const
|
||||
{ unreachable (); }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
namespace util { namespace noise {
|
||||
template struct fractal<float>;
|
||||
template struct fractal<double>;
|
||||
} }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <typename B>
|
||||
util::noise::fbm<B>::fbm (unsigned _octaves,
|
||||
double _frequency,
|
||||
double _lacunarity,
|
||||
template <typename T, typename B>
|
||||
fbm<T,B>::fbm (unsigned _octaves,
|
||||
T _frequency,
|
||||
T _lacunarity,
|
||||
seed_t _seed):
|
||||
fractal (_octaves, _frequency, _lacunarity),
|
||||
fractal<T> (_octaves, _frequency, _lacunarity),
|
||||
basis (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename B>
|
||||
util::noise::fbm<B>::fbm ()
|
||||
template <typename T, typename B>
|
||||
fbm<T,B>::fbm ()
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename B>
|
||||
double
|
||||
util::noise::fbm<B>::eval (double x, double y) const {
|
||||
double total = 0.0;
|
||||
double f = this->frequency;
|
||||
double a = 1.0;
|
||||
double a_sum = 0.0;
|
||||
template <typename T, typename B>
|
||||
T
|
||||
fbm<T,B>::eval (T x, T y) const {
|
||||
T total = 0;
|
||||
T f = this->frequency;
|
||||
T a = 1;
|
||||
T a_sum = 0;
|
||||
|
||||
for (size_t i = 0; i < this->octaves; ++i) {
|
||||
total += basis.eval (x * f, y * f) * a;
|
||||
|
||||
f *= 2.0;
|
||||
f *= 2;
|
||||
|
||||
a_sum += a;
|
||||
a *= this->lacunarity;
|
||||
@ -90,65 +106,71 @@ util::noise::fbm<B>::eval (double x, double y) const {
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template struct util::noise::fbm<util::noise::cellular<double>>;
|
||||
template struct util::noise::fbm<util::noise::gradient<double,util::lerp::linear>>;
|
||||
template struct util::noise::fbm<util::noise::gradient<double,util::lerp::quintic>>;
|
||||
template struct util::noise::fbm<util::noise::value<double,util::lerp::linear>>;
|
||||
template struct util::noise::fbm<util::noise::value<double,util::lerp::quintic>>;
|
||||
template struct util::noise::fbm<float, util::noise::cellular<float>>;
|
||||
template struct util::noise::fbm<float, util::noise::gradient<float,util::lerp::linear>>;
|
||||
template struct util::noise::fbm<float, util::noise::gradient<float,util::lerp::quintic>>;
|
||||
template struct util::noise::fbm<float, util::noise::value<float,util::lerp::linear>>;
|
||||
template struct util::noise::fbm<float, util::noise::value<float,util::lerp::quintic>>;
|
||||
|
||||
template struct util::noise::fbm<double, util::noise::cellular<double>>;
|
||||
template struct util::noise::fbm<double, util::noise::gradient<double,util::lerp::linear>>;
|
||||
template struct util::noise::fbm<double, util::noise::gradient<double,util::lerp::quintic>>;
|
||||
template struct util::noise::fbm<double, util::noise::value<double,util::lerp::linear>>;
|
||||
template struct util::noise::fbm<double, util::noise::value<double,util::lerp::quintic>>;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <typename B>
|
||||
util::noise::musgrave<B>::musgrave (unsigned _octaves,
|
||||
double _frequency,
|
||||
double _lacunarity,
|
||||
template <typename T, typename B>
|
||||
musgrave<T,B>::musgrave (unsigned _octaves,
|
||||
T _frequency,
|
||||
T _lacunarity,
|
||||
seed_t _seed):
|
||||
fractal (_octaves, _frequency, _lacunarity),
|
||||
fractal<T> (_octaves, _frequency, _lacunarity),
|
||||
basis (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename B>
|
||||
util::noise::musgrave<B>::musgrave ()
|
||||
template <typename T, typename B>
|
||||
musgrave<T,B>::musgrave ()
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename B>
|
||||
double
|
||||
util::noise::musgrave<B>::eval (double x, double y) const {
|
||||
double total = 0.0;
|
||||
double f = this->frequency;
|
||||
double a = 1.0;
|
||||
template <typename T, typename B>
|
||||
T
|
||||
musgrave<T,B>::eval (T x, T y) const {
|
||||
T total = 0;
|
||||
T f = this->frequency;
|
||||
T a = 1;
|
||||
|
||||
double weight = 1.0;
|
||||
double offset = 1.0;
|
||||
double gain = 2.0;
|
||||
T weight = 1;
|
||||
T offset = 1;
|
||||
T gain = 2;
|
||||
|
||||
double signal;
|
||||
T signal;
|
||||
|
||||
signal = basis.eval (x * f, y * f);
|
||||
signal = fabs (signal);
|
||||
signal = std::fabs (signal);
|
||||
signal = offset - signal;
|
||||
signal *= signal;
|
||||
total = signal;
|
||||
|
||||
for (size_t i = 1; i < this->octaves; ++i) {
|
||||
f *= 2.0;
|
||||
f *= 2;
|
||||
a *= this->lacunarity;
|
||||
|
||||
weight = signal * gain;
|
||||
weight = max (0.0, min (1.0, weight));
|
||||
weight = limit (weight, 0, 1);
|
||||
|
||||
signal = basis.eval (x * f, y * f);
|
||||
signal = fabs (signal);
|
||||
signal = std::fabs (signal);
|
||||
signal = offset - signal;
|
||||
signal *= signal;
|
||||
|
||||
signal *= weight;
|
||||
total += signal * a;
|
||||
total = min (1.0, max (0.0, total));
|
||||
total = limit (total, 0, 1);
|
||||
}
|
||||
|
||||
return total;
|
||||
@ -156,9 +178,15 @@ util::noise::musgrave<B>::eval (double x, double y) const {
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template struct util::noise::musgrave<util::noise::cellular<double>>;
|
||||
template struct util::noise::musgrave<util::noise::gradient<double,util::lerp::linear>>;
|
||||
template struct util::noise::musgrave<util::noise::gradient<double,util::lerp::quintic>>;
|
||||
template struct util::noise::musgrave<util::noise::value<double,util::lerp::linear>>;
|
||||
template struct util::noise::musgrave<util::noise::value<double,util::lerp::quintic>>;
|
||||
template struct util::noise::musgrave<float, util::noise::cellular<float>>;
|
||||
template struct util::noise::musgrave<float, util::noise::gradient<float,util::lerp::linear>>;
|
||||
template struct util::noise::musgrave<float, util::noise::gradient<float,util::lerp::quintic>>;
|
||||
template struct util::noise::musgrave<float, util::noise::value<float,util::lerp::linear>>;
|
||||
template struct util::noise::musgrave<float, util::noise::value<float,util::lerp::quintic>>;
|
||||
|
||||
template struct util::noise::musgrave<double, util::noise::cellular<double>>;
|
||||
template struct util::noise::musgrave<double, util::noise::gradient<double,util::lerp::linear>>;
|
||||
template struct util::noise::musgrave<double, util::noise::gradient<double,util::lerp::quintic>>;
|
||||
template struct util::noise::musgrave<double, util::noise::value<double,util::lerp::linear>>;
|
||||
template struct util::noise::musgrave<double, util::noise::value<double,util::lerp::quintic>>;
|
||||
|
||||
|
@ -22,51 +22,52 @@
|
||||
namespace util {
|
||||
namespace noise {
|
||||
/// Base noise summation
|
||||
template <typename T>
|
||||
struct fractal {
|
||||
fractal (unsigned octaves,
|
||||
double frequency,
|
||||
double lacunarity);
|
||||
T frequency,
|
||||
T lacunarity);
|
||||
fractal ();
|
||||
virtual ~fractal ();
|
||||
|
||||
unsigned octaves;
|
||||
double frequency;
|
||||
double lacunarity;
|
||||
T frequency;
|
||||
T lacunarity;
|
||||
|
||||
virtual double operator() (double x, double y) const { return eval (x, y); };
|
||||
virtual double eval (double x, double y) const = 0;
|
||||
T operator() (T x, T y) const { return eval (x, y); };
|
||||
virtual T eval (T x, T y) const = 0;
|
||||
};
|
||||
|
||||
|
||||
/// Fractal Brownian Motion summation.
|
||||
template <typename B>
|
||||
struct fbm : public fractal {
|
||||
using seed_t = typename basis<double>::seed_t;
|
||||
template <typename T, typename B>
|
||||
struct fbm : public fractal<T> {
|
||||
using seed_t = typename basis<T>::seed_t;
|
||||
|
||||
fbm (unsigned octaves,
|
||||
double frequency,
|
||||
double lacunarity,
|
||||
T frequency,
|
||||
T lacunarity,
|
||||
seed_t seed);
|
||||
fbm ();
|
||||
|
||||
B basis;
|
||||
virtual double eval (double x, double y) const;
|
||||
virtual T eval (T x, T y) const;
|
||||
};
|
||||
|
||||
|
||||
/// Rigid Multifractal noise summation.
|
||||
template <typename B>
|
||||
struct musgrave : public fractal {
|
||||
using seed_t = typename basis<double>::seed_t;
|
||||
template <typename T, typename B>
|
||||
struct musgrave : public fractal<T> {
|
||||
using seed_t = typename basis<T>::seed_t;
|
||||
|
||||
musgrave (unsigned octaves,
|
||||
double frequency,
|
||||
double lacunarity,
|
||||
T frequency,
|
||||
T lacunarity,
|
||||
seed_t seed);
|
||||
musgrave ();
|
||||
|
||||
B basis;
|
||||
virtual double eval (double x, double y) const;
|
||||
virtual T eval (T x, T y) const;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ main (void)
|
||||
//using basis_t = util::noise::gradient<lerp_t>;
|
||||
//using noise_t = util::noise::fbm<basis_t>;
|
||||
|
||||
using noise_t = util::noise::fbm<util::noise::gradient<double,util::lerp::quintic>>;
|
||||
//using noise_t = util::noise::musgrave<util::noise::gradient<double,util::lerp::quintic>>;
|
||||
//using noise_t = util::noise::fbm<util::noise::cellular<double>>;
|
||||
using noise_t = util::noise::fbm<float,util::noise::gradient<float,util::lerp::quintic>>;
|
||||
//using noise_t = util::noise::musgrave<float,util::noise::gradient<float,util::lerp::quintic>>;
|
||||
//using noise_t = util::noise::fbm<float,util::noise::cellular<float>>;
|
||||
|
||||
util::noise::fill (img, noise_t {});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user