style: comments and namespacing
This commit is contained in:
parent
5295f1a205
commit
e525c34134
6
lerp.cpp
6
lerp.cpp
@ -22,16 +22,19 @@
|
||||
#include <cmath>
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
double
|
||||
lerp::sigmoid (double val) {
|
||||
return -1.0 + 2.0 / (1.0 + exp (-2.0 * val));
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
double lerp::trunc (double a, double, double)
|
||||
{ return a; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
double
|
||||
lerp::linear (double a, double b, double weight) {
|
||||
CHECK (weight >= 0.0 && weight <= 1.0);
|
||||
@ -39,6 +42,7 @@ lerp::linear (double a, double b, double weight) {
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
double
|
||||
lerp::cosine (double a, double b, double weight) {
|
||||
CHECK (weight >= 0.0 && weight <= 1.0);
|
||||
@ -48,6 +52,7 @@ lerp::cosine (double a, double b, double weight) {
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
double
|
||||
lerp::cubic (double a, double b, double weight) {
|
||||
CHECK (weight >= 0.0 && weight <= 1.0);
|
||||
@ -56,6 +61,7 @@ lerp::cubic (double a, double b, double weight) {
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
double
|
||||
lerp::quintic (double a, double b, double weight) {
|
||||
CHECK (weight >= 0.0 && weight <= 1.0);
|
||||
|
@ -11,7 +11,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
||||
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "noise/basis.hpp"
|
||||
@ -24,8 +24,11 @@
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
using namespace util::noise;
|
||||
using util::range;
|
||||
using util::noise::basis;
|
||||
using util::noise::value;
|
||||
using util::noise::gradient;
|
||||
using util::noise::cellular;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Generate a type from [-UNIT..UNIT]
|
||||
@ -34,21 +37,23 @@ 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];
|
||||
size_t idx = util::noise::permute (x, y, seed);
|
||||
return util::noise::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);
|
||||
auto u = util::noise::permute (x, y, seed);
|
||||
auto v = util::noise::permute (u ^ seed);
|
||||
|
||||
return util::vector2d (LUT[u], LUT[v]);
|
||||
return util::vector2d (util::noise::LUT[u], util::noise::LUT[v]);
|
||||
}
|
||||
|
||||
|
||||
@ -58,15 +63,18 @@ basis::basis (seed_t _seed):
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
basis::basis ():
|
||||
seed (util::random<seed_t> ())
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
basis::~basis ()
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
double
|
||||
basis::eval (double, double) const
|
||||
{ unreachable (); }
|
||||
@ -74,24 +82,27 @@ basis::eval (double, double) const
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <lerp_function L>
|
||||
template <util::noise::lerp_function L>
|
||||
value<L>::value (seed_t _seed):
|
||||
basis (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
template <lerp_function L>
|
||||
//-----------------------------------------------------------------------------
|
||||
template <util::noise::lerp_function L>
|
||||
value<L>::value ()
|
||||
{ ; }
|
||||
|
||||
|
||||
template <lerp_function L>
|
||||
range<double>
|
||||
//-----------------------------------------------------------------------------
|
||||
template <util::noise::lerp_function L>
|
||||
util::range<double>
|
||||
value<L>::bounds (void) const
|
||||
{ return { -1.0, 1.0 }; }
|
||||
|
||||
|
||||
template <lerp_function L>
|
||||
//-----------------------------------------------------------------------------
|
||||
template <util::noise::lerp_function L>
|
||||
double
|
||||
value<L>::eval (double x, double y) const {
|
||||
intmax_t x_int = static_cast<intmax_t> (x);
|
||||
@ -118,6 +129,7 @@ value<L>::eval (double x, double y) const {
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
namespace util {
|
||||
namespace noise {
|
||||
template struct value<lerp::linear>;
|
||||
@ -128,24 +140,27 @@ namespace util {
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <lerp_function L>
|
||||
template <util::noise::lerp_function L>
|
||||
gradient<L>::gradient (seed_t _seed):
|
||||
basis (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
template <lerp_function L>
|
||||
//-----------------------------------------------------------------------------
|
||||
template <util::noise::lerp_function L>
|
||||
gradient<L>::gradient ()
|
||||
{ ; }
|
||||
|
||||
|
||||
template <lerp_function L>
|
||||
range<double>
|
||||
//-----------------------------------------------------------------------------
|
||||
template <util::noise::lerp_function L>
|
||||
util::range<double>
|
||||
gradient<L>::bounds (void) const
|
||||
{ return { -sqrt(2.0) / 2.0, sqrt (2.0) / 2.0 }; }
|
||||
|
||||
|
||||
template <lerp_function L>
|
||||
//-----------------------------------------------------------------------------
|
||||
template <util::noise::lerp_function L>
|
||||
double
|
||||
gradient<L>::eval (double x, double y) const {
|
||||
intmax_t x_int = static_cast<intmax_t> (x);
|
||||
@ -178,6 +193,7 @@ gradient<L>::eval (double x, double y) const {
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
namespace util {
|
||||
namespace noise {
|
||||
template struct gradient<lerp::linear>;
|
||||
@ -193,15 +209,18 @@ cellular::cellular (seed_t _seed):
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
cellular::cellular ()
|
||||
{ ; }
|
||||
|
||||
|
||||
range<double>
|
||||
//-----------------------------------------------------------------------------
|
||||
util::range<double>
|
||||
cellular::bounds (void) const
|
||||
{ return { 0.0, 1.5 }; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
double
|
||||
cellular::eval (double x, double y) const {
|
||||
using util::point2d;
|
||||
|
@ -11,7 +11,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
||||
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_BASIS_HPP
|
||||
@ -39,7 +39,9 @@ namespace util {
|
||||
};
|
||||
|
||||
|
||||
template <lerp_function L> struct value : public basis {
|
||||
/// Perlin: single value per grid space
|
||||
template <lerp_function L>
|
||||
struct value : public basis {
|
||||
value (seed_t);
|
||||
value ();
|
||||
|
||||
@ -47,7 +49,10 @@ namespace util {
|
||||
virtual double eval (double x, double y) const;
|
||||
};
|
||||
|
||||
template <lerp_function L> struct gradient : public basis {
|
||||
|
||||
/// Perlin: interpolated value across each grid space
|
||||
template <lerp_function L>
|
||||
struct gradient : public basis {
|
||||
gradient (seed_t);
|
||||
gradient ();
|
||||
|
||||
@ -55,6 +60,8 @@ namespace util {
|
||||
virtual double eval (double x, double y) const;
|
||||
};
|
||||
|
||||
|
||||
/// Cellular/Worley/Vornoi of order-1
|
||||
struct cellular : public basis {
|
||||
cellular (seed_t);
|
||||
cellular ();
|
||||
|
@ -31,6 +31,7 @@ util::noise::fractal::fractal (unsigned _octaves,
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
util::noise::fractal::fractal ():
|
||||
octaves (1),
|
||||
frequency (0.1),
|
||||
@ -38,10 +39,12 @@ util::noise::fractal::fractal ():
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
util::noise::fractal::~fractal ()
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
double
|
||||
util::noise::fractal::eval (double, double) const
|
||||
{ unreachable (); }
|
||||
@ -58,11 +61,13 @@ util::noise::fbm<B>::fbm (unsigned _octaves,
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename B>
|
||||
util::noise::fbm<B>::fbm ()
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename B>
|
||||
double
|
||||
util::noise::fbm<B>::eval (double x, double y) const {
|
||||
@ -84,6 +89,7 @@ util::noise::fbm<B>::eval (double x, double y) const {
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template struct util::noise::fbm<util::noise::cellular>;
|
||||
template struct util::noise::fbm<util::noise::gradient<lerp::linear>>;
|
||||
template struct util::noise::fbm<util::noise::gradient<lerp::quintic>>;
|
||||
@ -102,11 +108,13 @@ util::noise::musgrave<B>::musgrave (unsigned _octaves,
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename B>
|
||||
util::noise::musgrave<B>::musgrave ()
|
||||
{ ; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename B>
|
||||
double
|
||||
util::noise::musgrave<B>::eval (double x, double y) const {
|
||||
@ -146,6 +154,8 @@ util::noise::musgrave<B>::eval (double x, double y) const {
|
||||
return total;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template struct util::noise::musgrave<util::noise::cellular>;
|
||||
template struct util::noise::musgrave<util::noise::gradient<lerp::linear>>;
|
||||
template struct util::noise::musgrave<util::noise::gradient<lerp::quintic>>;
|
||||
|
@ -21,6 +21,7 @@
|
||||
|
||||
namespace util {
|
||||
namespace noise {
|
||||
/// Base noise summation
|
||||
struct fractal {
|
||||
fractal (unsigned octaves,
|
||||
double frequency,
|
||||
@ -37,6 +38,7 @@ namespace util {
|
||||
};
|
||||
|
||||
|
||||
/// Fractal Brownian Motion summation.
|
||||
template <typename B>
|
||||
struct fbm : public fractal {
|
||||
fbm (unsigned octaves,
|
||||
@ -49,6 +51,8 @@ namespace util {
|
||||
virtual double eval (double x, double y) const;
|
||||
};
|
||||
|
||||
|
||||
/// Rigid Multifractal noise summation.
|
||||
template <typename B>
|
||||
struct musgrave : public fractal {
|
||||
musgrave (unsigned octaves,
|
||||
|
Loading…
Reference in New Issue
Block a user