noise: convert eval method to function operator

This commit is contained in:
Danny Robson 2015-05-18 22:05:39 +10:00
parent 9b0abd8f7a
commit b082fd6a44
5 changed files with 30 additions and 22 deletions

View File

@ -49,7 +49,7 @@ util::noise::image2d (uint8_t *restrict pixels,
const util::noise::fractal<float> &p) { const util::noise::fractal<float> &p) {
for (size_t y = 0; y < height; ++y) for (size_t y = 0; y < height; ++y)
for (size_t x = 0; x < width; ++x) { for (size_t x = 0; x < width; ++x) {
double v = p.eval (x, y); double v = p (x, y);
pixels[x + y * width] = static_cast<uint8_t> (v * std::numeric_limits<uint8_t>::max ()); pixels[x + y * width] = static_cast<uint8_t> (v * std::numeric_limits<uint8_t>::max ());
} }
} }

View File

@ -93,8 +93,10 @@ basis<T>::~basis ()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
T T
basis<T>::eval (T, T) const basis<T>::operator() (T, T) const
{ unreachable (); } {
unreachable ();
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -121,13 +123,16 @@ value<T,L>::value ()
template <typename T, util::noise::lerp_t<T> L> template <typename T, util::noise::lerp_t<T> L>
util::range<T> util::range<T>
value<T,L>::bounds (void) const value<T,L>::bounds (void) const
{ return { -1, 1 }; } {
return { -1, 1 };
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T, util::noise::lerp_t<T> L> template <typename T, util::noise::lerp_t<T> L>
T T
value<T,L>::eval (T x, T y) const { value<T,L>::operator() (T x, T y) const
{
intmax_t x_int = static_cast<intmax_t> (x); intmax_t x_int = static_cast<intmax_t> (x);
intmax_t y_int = static_cast<intmax_t> (y); intmax_t y_int = static_cast<intmax_t> (y);
T x_fac = x - x_int; T x_fac = x - x_int;
@ -187,7 +192,8 @@ gradient<T,L>::bounds (void) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T, util::noise::lerp_t<T> L> template <typename T, util::noise::lerp_t<T> L>
T T
gradient<T,L>::eval (T x, T y) const { gradient<T,L>::operator() (T x, T y) const
{
intmax_t x_int = static_cast<intmax_t> (x); intmax_t x_int = static_cast<intmax_t> (x);
intmax_t y_int = static_cast<intmax_t> (y); intmax_t y_int = static_cast<intmax_t> (y);
T x_fac = x - x_int; T x_fac = x - x_int;
@ -255,7 +261,8 @@ cellular<T>::bounds (void) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
T T
cellular<T>::eval (T x, T y) const { cellular<T>::operator() (T x, T y) const
{
intmax_t x_int = static_cast<intmax_t> (x); intmax_t x_int = static_cast<intmax_t> (x);
intmax_t y_int = static_cast<intmax_t> (y); intmax_t y_int = static_cast<intmax_t> (y);
T x_fac = x - x_int; T x_fac = x - x_int;

View File

@ -36,7 +36,7 @@ namespace util {
seed_t seed; seed_t seed;
virtual range<T> bounds (void) const = 0; virtual range<T> bounds (void) const = 0;
virtual T eval (T x, T y) const = 0; virtual T operator() (T x, T y) const = 0;
}; };
@ -49,7 +49,7 @@ namespace util {
value (); value ();
virtual range<T> bounds (void) const override; virtual range<T> bounds (void) const override;
virtual T eval (T x, T y) const override; virtual T operator() (T x, T y) const override;
}; };
@ -62,7 +62,7 @@ namespace util {
gradient (); gradient ();
virtual range<T> bounds (void) const override; virtual range<T> bounds (void) const override;
virtual T eval (T x, T y) const override; virtual T operator() (T x, T y) const override;
}; };
@ -75,7 +75,7 @@ namespace util {
cellular (); cellular ();
virtual range<T> bounds (void) const override; virtual range<T> bounds (void) const override;
virtual T eval (T x, T y) const override; virtual T operator() (T x, T y) const override;
}; };
} }
} }

View File

@ -55,8 +55,10 @@ fractal<T>::~fractal ()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
T T
fractal<T>::eval (T, T) const fractal<T>::operator() (T, T) const
{ unreachable (); } {
unreachable ();
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -86,14 +88,14 @@ fbm<T,B>::fbm ()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T, typename B> template <typename T, typename B>
T T
fbm<T,B>::eval (T x, T y) const { fbm<T,B>::operator() (T x, T y) const {
T total = 0; T total = 0;
T f = this->frequency; T f = this->frequency;
T a = 1; T a = 1;
T a_sum = 0; T a_sum = 0;
for (size_t i = 0; i < this->octaves; ++i) { for (size_t i = 0; i < this->octaves; ++i) {
total += basis.eval (x * f, y * f) * a; total += basis (x * f, y * f) * a;
f *= 2; f *= 2;
@ -139,7 +141,7 @@ musgrave<T,B>::musgrave ()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T, typename B> template <typename T, typename B>
T T
musgrave<T,B>::eval (T x, T y) const { musgrave<T,B>::operator() (T x, T y) const {
T total = 0; T total = 0;
T f = this->frequency; T f = this->frequency;
T a = 1; T a = 1;
@ -150,7 +152,7 @@ musgrave<T,B>::eval (T x, T y) const {
T signal; T signal;
signal = basis.eval (x * f, y * f); signal = basis (x * f, y * f);
signal = std::fabs (signal); signal = std::fabs (signal);
signal = offset - signal; signal = offset - signal;
signal *= signal; signal *= signal;
@ -163,7 +165,7 @@ musgrave<T,B>::eval (T x, T y) const {
weight = signal * gain; weight = signal * gain;
weight = limit (weight, 0, 1); weight = limit (weight, 0, 1);
signal = basis.eval (x * f, y * f); signal = basis (x * f, y * f);
signal = std::fabs (signal); signal = std::fabs (signal);
signal = offset - signal; signal = offset - signal;
signal *= signal; signal *= signal;

View File

@ -34,8 +34,7 @@ namespace util {
T frequency; T frequency;
T lacunarity; T lacunarity;
T operator() (T x, T y) const { return eval (x, y); }; virtual T operator() (T x, T y) const = 0;
virtual T eval (T x, T y) const = 0;
}; };
@ -51,7 +50,7 @@ namespace util {
fbm (); fbm ();
B basis; B basis;
virtual T eval (T x, T y) const; virtual T operator() (T x, T y) const override;
}; };
@ -67,7 +66,7 @@ namespace util {
musgrave (); musgrave ();
B basis; B basis;
virtual T eval (T x, T y) const; virtual T operator() (T x, T y) const override;
}; };
} }
} }