n/basis: extract gradient generators

This commit is contained in:
Danny Robson 2015-10-07 14:34:56 +11:00
parent ed124949a8
commit f0f1522307
7 changed files with 54 additions and 55 deletions

View File

@ -150,8 +150,10 @@ UTIL_FILES = \
noise/basis/constant.cpp \ noise/basis/constant.cpp \
noise/basis/constant.hpp \ noise/basis/constant.hpp \
noise/basis/constant.ipp \ noise/basis/constant.ipp \
noise/basis/expdist.hpp \ noise/basis/gradient/uniform.hpp \
noise/basis/expdist.ipp \ noise/basis/gradient/uniform.ipp \
noise/basis/gradient/exp.hpp \
noise/basis/gradient/exp.ipp \
noise/basis/patch.hpp \ noise/basis/patch.hpp \
noise/basis/patch.ipp \ noise/basis/patch.ipp \
noise/basis/perlin.hpp \ noise/basis/perlin.hpp \

View File

@ -15,30 +15,28 @@
*/ */
#ifndef __UTIL_NOISE_BASIS_EXPGRAD_HPP #ifndef __UTIL_NOISE_BASIS_GRADIENT_EXP_HPP
#define __UTIL_NOISE_BASIS_EXPGRAD_HPP #define __UTIL_NOISE_BASIS_GRADIENT_EXP_HPP
#include "./gradient.hpp" #include "./uniform.hpp"
#include "../fwd.hpp"
#include "../../point.hpp"
#include "../../range.hpp"
#include "../../fwd.hpp"
#include "../../../point.hpp"
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// modifies a standard uniforma gradient generator to follow an exponential // modifies a standard uniforma gradient generator to follow an exponential
// distribution, base^(-t*exp) // distribution, base^(-t*exp)
// //
// //
namespace util { namespace noise { namespace basis { namespace util { namespace noise { namespace basis { namespace gradient {
template < template <
size_t S, // probe point dimensionality size_t S, // probe point dimensionality
typename T // probe point value_type typename T // probe point value_type
> >
struct expgrad : public gradient<S,T> { struct exp : public uniform<S,T> {
explicit expgrad (seed_t seed, explicit exp (seed_t seed,
T base = (T)1.02, T base = (T)1.02,
T exponent = T{256}); T exponent = T{256});
T base (void) const; T base (void) const;
T base (T); T base (T);
@ -52,8 +50,8 @@ namespace util { namespace noise { namespace basis {
T m_base; T m_base;
T m_exponent; T m_exponent;
}; };
} } } } } } }
#include "expgrad.ipp" #include "exp.ipp"
#endif #endif

View File

@ -14,19 +14,20 @@
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net> * Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/ */
#ifdef __UTIL_NOISE_BASIS_EXPGRAD_IPP #ifdef __UTIL_NOISE_BASIS_EXP_IPP
#error #error
#endif #endif
#define __UTIL_NOISE_BASIS_EXPGRAD_IPP #define __UTIL_NOISE_BASIS_EXP_IPP
#include "../rand.hpp" #include "../../rand.hpp"
#include <cmath>
namespace util { namespace noise { namespace basis { namespace util { namespace noise { namespace basis { namespace gradient {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <size_t S, typename T>
expgrad<S,T>::expgrad (seed_t _seed, T _base, T _exponent): exp<S,T>::exp (seed_t _seed, T _base, T _exponent):
gradient<S,T> (_seed), uniform<S,T> (_seed),
m_base (_base), m_base (_base),
m_exponent (_exponent) m_exponent (_exponent)
{ ; } { ; }
@ -35,10 +36,10 @@ namespace util { namespace noise { namespace basis {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <size_t S, typename T>
vector<S,T> vector<S,T>
expgrad<S,T>::generate (pointi<S> p) const exp<S,T>::generate (pointi<S> p) const
{ {
auto t = rand::scalar<float> (this->seed (), p); auto t = rand::scalar<float> (this->seed (), p);
auto factor = std::pow (m_base, -t * m_exponent); auto factor = std::pow (m_base, -t * m_exponent);
return factor * gradient<S,T>::generate (p); return factor * uniform<S,T>::generate (p);
} }
} } } } } } }

View File

@ -15,21 +15,20 @@
*/ */
#ifndef __UTIL_NOISE_BASIS_GRADIENT_HPP #ifndef __UTIL_NOISE_BASIS_GRADIENT_UNIFORM_HPP
#define __UTIL_NOISE_BASIS_GRADIENT_HPP #define __UTIL_NOISE_BASIS_GRADIENT_UNIFORM_HPP
#include "../fwd.hpp" #include "../../fwd.hpp"
#include "../../point.hpp" #include "../../../point.hpp"
#include "../../range.hpp"
namespace util { namespace noise { namespace basis { namespace util { namespace noise { namespace basis { namespace gradient {
/// Perlin: interpolated value across each grid space /// Perlin: interpolated value across each grid space
template < template <
size_t S, // probe point dimensionality size_t S, // probe point dimensionality
typename T // probe point value_type typename T // probe point value_type
> >
struct gradient { struct uniform {
gradient (seed_t); uniform (seed_t);
seed_t seed (void) const; seed_t seed (void) const;
seed_t seed (seed_t); seed_t seed (seed_t);
@ -39,9 +38,9 @@ namespace util { namespace noise { namespace basis {
seed_t m_seed; seed_t m_seed;
}; };
} } } } } } }
#include "gradient.ipp" #include "uniform.ipp"
#endif #endif

View File

@ -14,17 +14,17 @@
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net> * Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/ */
#ifdef __UTIL_NOISE_BASIS_GRADIENT_IPP #ifdef __UTIL_NOISE_BASIS_GRADIENT_UNIFORM_IPP
#error #error
#endif #endif
#define __UTIL_NOISE_BASIS_GRADIENT_IPP #define __UTIL_NOISE_BASIS_GRADIENT_UNIFORM_IPP
#include "../rand.hpp" #include "../../rand.hpp"
namespace util { namespace noise { namespace basis { namespace util { namespace noise { namespace basis { namespace gradient {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <size_t S, typename T>
gradient<S,T>::gradient (seed_t _seed): uniform<S,T>::uniform (seed_t _seed):
m_seed (_seed) m_seed (_seed)
{ ; } { ; }
@ -32,7 +32,7 @@ namespace util { namespace noise { namespace basis {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
seed_t seed_t
gradient<S,T>::seed (void) const uniform<S,T>::seed (void) const
{ {
return m_seed; return m_seed;
} }
@ -41,7 +41,7 @@ namespace util { namespace noise { namespace basis {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
seed_t seed_t
gradient<S,T>::seed (seed_t _seed) uniform<S,T>::seed (seed_t _seed)
{ {
return m_seed = _seed; return m_seed = _seed;
} }
@ -50,9 +50,8 @@ namespace util { namespace noise { namespace basis {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
vector<S,T> vector<S,T>
gradient<S,T>::generate (pointi<S> p) const uniform<S,T>::generate (pointi<S> p) const
{ {
return rand::coord<vector,T> (m_seed, p) * 2 - 1; return rand::coord<vector,T> (m_seed, p) * 2 - 1;
} }
} } } } } } }

View File

@ -18,7 +18,7 @@
#ifndef __UTIL_NOISE_BASIS_PERLIN_HPP #ifndef __UTIL_NOISE_BASIS_PERLIN_HPP
#define __UTIL_NOISE_BASIS_PERLIN_HPP #define __UTIL_NOISE_BASIS_PERLIN_HPP
#include "./gradient.hpp" #include "./gradient/uniform.hpp"
#include "./type/gradient.hpp" #include "./type/gradient.hpp"
#include "../fwd.hpp" #include "../fwd.hpp"
@ -36,7 +36,7 @@ namespace util { namespace noise { namespace basis {
template < // gradient provider class, must provide generate(point_t) template < // gradient provider class, must provide generate(point_t)
size_t, size_t,
typename typename
> class G = gradient > class G = gradient::uniform
> >
struct perlin : public G<S,T>, public type::gradient<S> { struct perlin : public G<S,T>, public type::gradient<S> {
using value_t = T; using value_t = T;

View File

@ -8,7 +8,7 @@
#include "noise/fractal/runtime.hpp" #include "noise/fractal/runtime.hpp"
#include "noise/lerp.hpp" #include "noise/lerp.hpp"
#include "noise/basis/constant.hpp" #include "noise/basis/constant.hpp"
#include "noise/basis/expgrad.hpp" #include "noise/basis/gradient/exp.hpp"
#include "noise/basis/value.hpp" #include "noise/basis/value.hpp"
#include "noise/basis/patch.hpp" #include "noise/basis/patch.hpp"
#include "noise/basis/perlin.hpp" #include "noise/basis/perlin.hpp"
@ -32,13 +32,13 @@ template struct util::noise::fractal::rmf<util::noise::basis::constant<S,float>>
template struct util::noise::fractal::fbm< template struct util::noise::fractal::fbm<
util::noise::basis::perlin< util::noise::basis::perlin<
S,float,util::lerp::cubic,util::noise::basis::gradient S,float,util::lerp::cubic,util::noise::basis::gradient::uniform
> >
>; >;
template struct util::noise::fractal::fbm< template struct util::noise::fractal::fbm<
util::noise::basis::perlin< util::noise::basis::perlin<
S,float,util::lerp::quintic,util::noise::basis::expgrad S,float,util::lerp::quintic,util::noise::basis::gradient::exp
> >
>; >;
@ -292,14 +292,14 @@ main (int argc, char **argv)
switch (lerp) { switch (lerp) {
case LINEAR: b.reset< case LINEAR: b.reset<
basis::perlin< basis::perlin<
S,float,util::lerp::linear,basis::expgrad S,float,util::lerp::linear,basis::gradient::exp
> >
> (seed); break; > (seed); break;
case CUBIC: b.reset<basis::perlin<S,float,util::lerp::cubic,basis::expgrad>> (seed); break; case CUBIC: b.reset<basis::perlin<S,float,util::lerp::cubic,basis::gradient::exp>> (seed); break;
case QUINTIC: b.reset<basis::perlin<S,float,util::lerp::quintic,basis::expgrad>> (seed); break; case QUINTIC: b.reset<basis::perlin<S,float,util::lerp::quintic,basis::gradient::exp>> (seed); break;
case COSINE: b.reset<basis::perlin<S,float,util::lerp::cosine,basis::expgrad>> (seed); break; case COSINE: b.reset<basis::perlin<S,float,util::lerp::cosine,basis::gradient::exp>> (seed); break;
case TRUNC: b.reset<basis::perlin<S,float,util::lerp::truncate,basis::expgrad>> (seed); break; case TRUNC: b.reset<basis::perlin<S,float,util::lerp::truncate,basis::gradient::exp>> (seed); break;
default: default:
unreachable (); unreachable ();