style: comments and namespacing

This commit is contained in:
Danny Robson 2015-05-18 14:34:48 +10:00
parent 5295f1a205
commit e525c34134
5 changed files with 68 additions and 22 deletions

View File

@ -22,16 +22,19 @@
#include <cmath> #include <cmath>
//-----------------------------------------------------------------------------
double double
lerp::sigmoid (double val) { lerp::sigmoid (double val) {
return -1.0 + 2.0 / (1.0 + exp (-2.0 * val)); return -1.0 + 2.0 / (1.0 + exp (-2.0 * val));
} }
//-----------------------------------------------------------------------------
double lerp::trunc (double a, double, double) double lerp::trunc (double a, double, double)
{ return a; } { return a; }
//-----------------------------------------------------------------------------
double double
lerp::linear (double a, double b, double weight) { lerp::linear (double a, double b, double weight) {
CHECK (weight >= 0.0 && weight <= 1.0); CHECK (weight >= 0.0 && weight <= 1.0);
@ -39,6 +42,7 @@ lerp::linear (double a, double b, double weight) {
} }
//-----------------------------------------------------------------------------
double double
lerp::cosine (double a, double b, double weight) { lerp::cosine (double a, double b, double weight) {
CHECK (weight >= 0.0 && weight <= 1.0); CHECK (weight >= 0.0 && weight <= 1.0);
@ -48,6 +52,7 @@ lerp::cosine (double a, double b, double weight) {
} }
//-----------------------------------------------------------------------------
double double
lerp::cubic (double a, double b, double weight) { lerp::cubic (double a, double b, double weight) {
CHECK (weight >= 0.0 && weight <= 1.0); CHECK (weight >= 0.0 && weight <= 1.0);
@ -56,6 +61,7 @@ lerp::cubic (double a, double b, double weight) {
} }
//-----------------------------------------------------------------------------
double double
lerp::quintic (double a, double b, double weight) { lerp::quintic (double a, double b, double weight) {
CHECK (weight >= 0.0 && weight <= 1.0); CHECK (weight >= 0.0 && weight <= 1.0);

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2012 Danny Robson <danny@nerdcruft.net> * Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/ */
#include "noise/basis.hpp" #include "noise/basis.hpp"
@ -24,8 +24,11 @@
#include <algorithm> #include <algorithm>
using namespace util::noise; using util::noise::basis;
using util::range; using util::noise::value;
using util::noise::gradient;
using util::noise::cellular;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Generate a type from [-UNIT..UNIT] // Generate a type from [-UNIT..UNIT]
@ -34,21 +37,23 @@ T
generate (intmax_t x, intmax_t y, basis::seed_t); generate (intmax_t x, intmax_t y, basis::seed_t);
//-----------------------------------------------------------------------------
template <> template <>
double double
generate (intmax_t x, intmax_t y, basis::seed_t seed) { generate (intmax_t x, intmax_t y, basis::seed_t seed) {
size_t idx = permute (x, y, seed); size_t idx = util::noise::permute (x, y, seed);
return LUT[idx]; return util::noise::LUT[idx];
} }
//-----------------------------------------------------------------------------
template <> template <>
util::vector2d util::vector2d
generate (intmax_t x, intmax_t y, basis::seed_t seed) { generate (intmax_t x, intmax_t y, basis::seed_t seed) {
auto u = permute (x, y, seed); auto u = util::noise::permute (x, y, seed);
auto v = permute (u ^ 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 (): basis::basis ():
seed (util::random<seed_t> ()) seed (util::random<seed_t> ())
{ ; } { ; }
//-----------------------------------------------------------------------------
basis::~basis () basis::~basis ()
{ ; } { ; }
//-----------------------------------------------------------------------------
double double
basis::eval (double, double) const basis::eval (double, double) const
{ unreachable (); } { 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): value<L>::value (seed_t _seed):
basis (_seed) basis (_seed)
{ ; } { ; }
template <lerp_function L> //-----------------------------------------------------------------------------
template <util::noise::lerp_function L>
value<L>::value () value<L>::value ()
{ ; } { ; }
template <lerp_function L> //-----------------------------------------------------------------------------
range<double> template <util::noise::lerp_function L>
util::range<double>
value<L>::bounds (void) const value<L>::bounds (void) const
{ return { -1.0, 1.0 }; } { return { -1.0, 1.0 }; }
template <lerp_function L> //-----------------------------------------------------------------------------
template <util::noise::lerp_function L>
double double
value<L>::eval (double x, double y) const { value<L>::eval (double x, double y) const {
intmax_t x_int = static_cast<intmax_t> (x); intmax_t x_int = static_cast<intmax_t> (x);
@ -118,6 +129,7 @@ value<L>::eval (double x, double y) const {
} }
//-----------------------------------------------------------------------------
namespace util { namespace util {
namespace noise { namespace noise {
template struct value<lerp::linear>; 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): gradient<L>::gradient (seed_t _seed):
basis (_seed) basis (_seed)
{ ; } { ; }
template <lerp_function L> //-----------------------------------------------------------------------------
template <util::noise::lerp_function L>
gradient<L>::gradient () gradient<L>::gradient ()
{ ; } { ; }
template <lerp_function L> //-----------------------------------------------------------------------------
range<double> template <util::noise::lerp_function L>
util::range<double>
gradient<L>::bounds (void) const gradient<L>::bounds (void) const
{ return { -sqrt(2.0) / 2.0, sqrt (2.0) / 2.0 }; } { return { -sqrt(2.0) / 2.0, sqrt (2.0) / 2.0 }; }
template <lerp_function L> //-----------------------------------------------------------------------------
template <util::noise::lerp_function L>
double double
gradient<L>::eval (double x, double y) const { gradient<L>::eval (double x, double y) const {
intmax_t x_int = static_cast<intmax_t> (x); intmax_t x_int = static_cast<intmax_t> (x);
@ -178,6 +193,7 @@ gradient<L>::eval (double x, double y) const {
} }
//-----------------------------------------------------------------------------
namespace util { namespace util {
namespace noise { namespace noise {
template struct gradient<lerp::linear>; template struct gradient<lerp::linear>;
@ -193,15 +209,18 @@ cellular::cellular (seed_t _seed):
{ ; } { ; }
//-----------------------------------------------------------------------------
cellular::cellular () cellular::cellular ()
{ ; } { ; }
range<double> //-----------------------------------------------------------------------------
util::range<double>
cellular::bounds (void) const cellular::bounds (void) const
{ return { 0.0, 1.5 }; } { return { 0.0, 1.5 }; }
//-----------------------------------------------------------------------------
double double
cellular::eval (double x, double y) const { cellular::eval (double x, double y) const {
using util::point2d; using util::point2d;

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2012 Danny Robson <danny@nerdcruft.net> * Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_NOISE_BASIS_HPP #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 (seed_t);
value (); value ();
@ -47,7 +49,10 @@ namespace util {
virtual double eval (double x, double y) const; 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 (seed_t);
gradient (); gradient ();
@ -55,6 +60,8 @@ namespace util {
virtual double eval (double x, double y) const; virtual double eval (double x, double y) const;
}; };
/// Cellular/Worley/Vornoi of order-1
struct cellular : public basis { struct cellular : public basis {
cellular (seed_t); cellular (seed_t);
cellular (); cellular ();

View File

@ -31,6 +31,7 @@ util::noise::fractal::fractal (unsigned _octaves,
{ ; } { ; }
//-----------------------------------------------------------------------------
util::noise::fractal::fractal (): util::noise::fractal::fractal ():
octaves (1), octaves (1),
frequency (0.1), frequency (0.1),
@ -38,10 +39,12 @@ util::noise::fractal::fractal ():
{ ; } { ; }
//-----------------------------------------------------------------------------
util::noise::fractal::~fractal () util::noise::fractal::~fractal ()
{ ; } { ; }
//-----------------------------------------------------------------------------
double double
util::noise::fractal::eval (double, double) const util::noise::fractal::eval (double, double) const
{ unreachable (); } { unreachable (); }
@ -58,11 +61,13 @@ util::noise::fbm<B>::fbm (unsigned _octaves,
{ ; } { ; }
//-----------------------------------------------------------------------------
template <typename B> template <typename B>
util::noise::fbm<B>::fbm () util::noise::fbm<B>::fbm ()
{ ; } { ; }
//-----------------------------------------------------------------------------
template <typename B> template <typename B>
double double
util::noise::fbm<B>::eval (double x, double y) const { 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::cellular>;
template struct util::noise::fbm<util::noise::gradient<lerp::linear>>; template struct util::noise::fbm<util::noise::gradient<lerp::linear>>;
template struct util::noise::fbm<util::noise::gradient<lerp::quintic>>; template struct util::noise::fbm<util::noise::gradient<lerp::quintic>>;
@ -102,11 +108,13 @@ util::noise::musgrave<B>::musgrave (unsigned _octaves,
{ ; } { ; }
//-----------------------------------------------------------------------------
template <typename B> template <typename B>
util::noise::musgrave<B>::musgrave () util::noise::musgrave<B>::musgrave ()
{ ; } { ; }
//-----------------------------------------------------------------------------
template <typename B> template <typename B>
double double
util::noise::musgrave<B>::eval (double x, double y) const { 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; return total;
} }
//-----------------------------------------------------------------------------
template struct util::noise::musgrave<util::noise::cellular>; 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::linear>>;
template struct util::noise::musgrave<util::noise::gradient<lerp::quintic>>; template struct util::noise::musgrave<util::noise::gradient<lerp::quintic>>;

View File

@ -21,6 +21,7 @@
namespace util { namespace util {
namespace noise { namespace noise {
/// Base noise summation
struct fractal { struct fractal {
fractal (unsigned octaves, fractal (unsigned octaves,
double frequency, double frequency,
@ -37,6 +38,7 @@ namespace util {
}; };
/// Fractal Brownian Motion summation.
template <typename B> template <typename B>
struct fbm : public fractal { struct fbm : public fractal {
fbm (unsigned octaves, fbm (unsigned octaves,
@ -49,6 +51,8 @@ namespace util {
virtual double eval (double x, double y) const; virtual double eval (double x, double y) const;
}; };
/// Rigid Multifractal noise summation.
template <typename B> template <typename B>
struct musgrave : public fractal { struct musgrave : public fractal {
musgrave (unsigned octaves, musgrave (unsigned octaves,