n/fractal: extract each type into seperate units

This commit is contained in:
Danny Robson 2015-06-01 16:20:50 +10:00
parent 79300cc8c4
commit 2d002c9dce
14 changed files with 657 additions and 507 deletions

View File

@ -145,8 +145,14 @@ UTIL_FILES = \
noise/basis/perlin.hpp \
noise/basis/worley.cpp \
noise/basis/worley.hpp \
noise/fractal.cpp \
noise/fractal.hpp \
noise/fractal/fbm.cpp \
noise/fractal/fbm.hpp \
noise/fractal/hetero.cpp \
noise/fractal/hetero.hpp \
noise/fractal/hmf.cpp \
noise/fractal/hmf.hpp \
noise/fractal/rmf.cpp \
noise/fractal/rmf.hpp \
noise/lerp.cpp \
noise/lerp.hpp \
noise/lut.cpp \

View File

@ -14,23 +14,18 @@
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_PERLIN_HPP
#define __UTIL_PERLIN_HPP
#ifndef __UTIL_NOISE_HPP
#define __UTIL_NOISE_HPP
#include <cstdint>
#include <cstdlib>
#include "noise/basis.hpp"
#include "noise/fractal.hpp"
#include "image.hpp"
namespace util {
namespace noise {
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<float>&);
template <typename T, typename G>
void fill (image::buffer<T>&, const G&);
}
}

35
noise.ipp Normal file
View File

@ -0,0 +1,35 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifdef __UTIL_NOISE_IPP
#error
#endif
#define __UTIL_NOISE_IPP
namespace util { namespace noise {
//-------------------------------------------------------------------------
template <typename T, typename G>
void
fill (image::buffer<T> &pixels, const G& gen)
{
size_t h = pixels.h, s = pixels.s, w = pixels.w;
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 ({T(x), T(y)});
}
} }

View File

@ -1,332 +0,0 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
*/
#include "fractal.hpp"
#include "basis/constant.hpp"
#include "basis/value.hpp"
#include "basis/perlin.hpp"
#include "basis/worley.hpp"
#include "lerp.hpp"
#include "../debug.hpp"
#include <algorithm>
#include <cmath>
using util::noise::fractal;
using util::noise::fbm;
using util::noise::rmf;
using util::noise::hmf;
using util::noise::hetero;
///////////////////////////////////////////////////////////////////////////////
template <typename T>
fractal<T>::fractal ():
seed (rand ())
{ ; }
//-----------------------------------------------------------------------------
template <typename T>
fractal<T>::fractal (seed_t _seed):
seed (_seed)
{ ; }
//-----------------------------------------------------------------------------
template <typename T>
fractal<T>::~fractal ()
{ ; }
//-----------------------------------------------------------------------------
template <typename T>
T
fractal<T>::operator() (util::point<2,T>) const
{
unreachable ();
}
//-----------------------------------------------------------------------------
namespace util { namespace noise {
template struct fractal<float>;
template struct fractal<double>;
} }
///////////////////////////////////////////////////////////////////////////////
template <typename T, typename B>
fbm<T,B>::fbm (unsigned _octaves,
T _frequency,
T _lacunarity,
T _amplitude,
T _gain,
seed_t _seed):
fractal<T> (_seed),
octaves (_octaves),
frequency (_frequency),
lacunarity (_lacunarity),
amplitude (_amplitude),
gain (_gain),
basis (_seed)
{
CHECK_NEQ (octaves, 0);
CHECK_NEQ (frequency, 0);
CHECK_NEQ (amplitude, 0);
}
//-----------------------------------------------------------------------------
template <typename T, typename B>
fbm<T,B>::fbm ():
fbm<T,B> (DEFAULT_OCTAVES,
DEFAULT_FREQUENCY,
DEFAULT_LACUNARITY,
DEFAULT_AMPLITUDE,
DEFAULT_GAIN,
rand ())
{ ; }
//-----------------------------------------------------------------------------
template <typename T, typename B>
T
fbm<T,B>::operator() (util::point<2,T> p) const {
T total = 0;
T f = frequency;
T a = amplitude;
for (size_t i = 0; i < octaves; ++i) {
total += basis (p * f) * a;
f *= lacunarity;
a *= gain;
}
return total;
}
//-----------------------------------------------------------------------------
template struct util::noise::fbm<float, util::noise::basis::worley<float>>;
template struct util::noise::fbm<float, util::noise::basis::constant<float>>;
template struct util::noise::fbm<float, util::noise::basis::perlin<float,util::lerp::trunc>>;
template struct util::noise::fbm<float, util::noise::basis::perlin<float,util::lerp::linear>>;
template struct util::noise::fbm<float, util::noise::basis::perlin<float,util::lerp::cubic>>;
template struct util::noise::fbm<float, util::noise::basis::perlin<float,util::lerp::quintic>>;
template struct util::noise::fbm<float, util::noise::basis::value<float,util::lerp::trunc>>;
template struct util::noise::fbm<float, util::noise::basis::value<float,util::lerp::linear>>;
template struct util::noise::fbm<float, util::noise::basis::value<float,util::lerp::cosine>>;
template struct util::noise::fbm<float, util::noise::basis::value<float,util::lerp::cubic>>;
template struct util::noise::fbm<float, util::noise::basis::value<float,util::lerp::quintic>>;
template struct util::noise::fbm<double, util::noise::basis::worley<double>>;
template struct util::noise::fbm<double, util::noise::basis::perlin<double,util::lerp::linear>>;
template struct util::noise::fbm<double, util::noise::basis::perlin<double,util::lerp::quintic>>;
template struct util::noise::fbm<double, util::noise::basis::value<double,util::lerp::trunc>>;
template struct util::noise::fbm<double, util::noise::basis::value<double,util::lerp::linear>>;
template struct util::noise::fbm<double, util::noise::basis::value<double,util::lerp::quintic>>;
///////////////////////////////////////////////////////////////////////////////
template <typename T, typename B>
rmf<T,B>::rmf (unsigned _octaves,
T _frequency,
T _lacunarity,
T _amplitude,
T _gain,
seed_t _seed):
fractal<T> (_seed),
octaves (_octaves),
frequency (_frequency),
lacunarity (_lacunarity),
amplitude (_amplitude),
gain (_gain),
basis (_seed)
{ ; }
//-----------------------------------------------------------------------------
template <typename T, typename B>
rmf<T,B>::rmf ():
rmf<T,B> (DEFAULT_OCTAVES,
DEFAULT_FREQUENCY,
DEFAULT_LACUNARITY,
DEFAULT_AMPLITUDE,
DEFAULT_GAIN,
rand ())
{ ; }
//-----------------------------------------------------------------------------
template <typename T, typename B>
T
rmf<T,B>::operator() (util::point<2,T> p) const {
const T offset = 1;
const T H = 1.f;
T exponents[octaves];
for (size_t i = 0; i < octaves; ++i)
exponents[i] = std::pow (std::pow (lacunarity, float (i)), -H);
T signal = 0;
T result = 0;
T weight = 1;
p *= frequency;
for (size_t i = 0; i < octaves; ++i) {
// generates ridged noise
signal = basis (p);
signal = std::fabs (signal);
signal = offset - signal;
// sharpens the ridges
signal *= signal;
// influence by sharpness of previous iteration
signal *= weight;
// contribute to the weight
weight = signal * gain;
weight = limit (weight, 0, 1);
// record and continue
result += signal * exponents[i];
p *= lacunarity;
}
return result;
}
//-----------------------------------------------------------------------------
template struct util::noise::rmf<float, util::noise::basis::worley<float>>;
template struct util::noise::rmf<float, util::noise::basis::perlin<float,util::lerp::linear>>;
template struct util::noise::rmf<float, util::noise::basis::perlin<float,util::lerp::quintic>>;
template struct util::noise::rmf<float, util::noise::basis::value<float,util::lerp::linear>>;
template struct util::noise::rmf<float, util::noise::basis::value<float,util::lerp::quintic>>;
template struct util::noise::rmf<double, util::noise::basis::worley<double>>;
template struct util::noise::rmf<double, util::noise::basis::perlin<double,util::lerp::linear>>;
template struct util::noise::rmf<double, util::noise::basis::perlin<double,util::lerp::quintic>>;
template struct util::noise::rmf<double, util::noise::basis::value<double,util::lerp::linear>>;
template struct util::noise::rmf<double, util::noise::basis::value<double,util::lerp::quintic>>;
//-----------------------------------------------------------------------------
template <typename T, typename B>
hmf<T,B>::hmf ():
H (0.25f),
octaves (6),
frequency (0.1f),
lacunarity (2),
offset (0.7f),
amplitude (1),
gain (1)
{ ; }
//-----------------------------------------------------------------------------
template <typename T, typename B>
T
hmf<T,B>::operator() (util::point<2,T> p) const
{
T exponents[octaves];
for (size_t i = 0; i < octaves; ++i)
exponents[i] = std::pow (std::pow (lacunarity, float (i)), -H);
T result = 0;
T signal = 0;
T weight = 1;
p *= frequency;
for (size_t i = 0; i < octaves; ++i) {
signal = (basis (p) + offset) * exponents[i];
result += weight * signal;
weight *= gain * signal;
if (weight > 1)
weight = 1;
p *= lacunarity;
}
return result;
}
template struct util::noise::hmf<float, util::noise::basis::worley<float>>;
template struct util::noise::hmf<float, util::noise::basis::perlin<float,util::lerp::linear>>;
template struct util::noise::hmf<float, util::noise::basis::perlin<float,util::lerp::quintic>>;
template struct util::noise::hmf<float, util::noise::basis::value<float,util::lerp::linear>>;
template struct util::noise::hmf<float, util::noise::basis::value<float,util::lerp::quintic>>;
//-----------------------------------------------------------------------------
template <typename T, typename B>
hetero<T,B>::hetero():
H (0.75f),
octaves (6),
frequency (0.1f),
lacunarity (2),
offset (0.7f),
amplitude (1),
gain (1)
{ ; }
//-----------------------------------------------------------------------------
template <typename T, typename B>
T
hetero<T,B>::operator() (util::point<2,T> p) const
{
T exponents[octaves];
for (size_t i = 0; i < octaves; ++i)
exponents[i] = std::pow (std::pow (lacunarity, float (i)), -H);
T result = 0;
T increment = 0;
p *= frequency;
result = basis (p) + offset;
p *= lacunarity;
for (size_t i = 0; i < octaves; ++i) {
increment = basis (p) + offset;
increment *= exponents[i];
increment *= result;
result += increment;
p *= lacunarity;
}
return result;
}
template struct util::noise::hetero<float, util::noise::basis::worley<float>>;
template struct util::noise::hetero<float, util::noise::basis::perlin<float,util::lerp::linear>>;
template struct util::noise::hetero<float, util::noise::basis::perlin<float,util::lerp::quintic>>;
template struct util::noise::hetero<float, util::noise::basis::value<float,util::lerp::linear>>;
template struct util::noise::hetero<float, util::noise::basis::value<float,util::lerp::quintic>>;

View File

@ -1,158 +0,0 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_NOISE_FRACTAL_HPP
#define __UTIL_NOISE_FRACTAL_HPP
#include "basis.hpp"
#include "../point.hpp"
namespace util {
namespace noise {
/// Base noise summation
template <typename T>
struct fractal {
fractal (seed_t);
fractal ();
virtual ~fractal ();
virtual T operator() (util::point<2,T>) const = 0;
seed_t seed;
};
/// Fractal Brownian Motion summation.
///
/// Sum progressive layers of a noise basis with scaling frequency
/// and amplitude.
///
/// octaves: count of layers to be summed
/// frequency: point scaling factor for the base octave
/// lacunarity: per octave frequency scaling factor
/// amplitude: maximum absolute value of the noise
/// gain: per octave amplitude scaling factor. typically 1/f.
template <typename T, typename B>
struct fbm : public fractal<T> {
static constexpr unsigned DEFAULT_OCTAVES = 8;
static constexpr T DEFAULT_FREQUENCY = T(0.1);
static constexpr T DEFAULT_LACUNARITY = 2;
static constexpr T DEFAULT_AMPLITUDE = 1;
static constexpr T DEFAULT_GAIN = 1 / T(2);
fbm (unsigned octaves,
T frequency,
T lacunarity,
T amplitude,
T gain,
seed_t seed);
fbm ();
unsigned octaves;
T frequency;
T lacunarity;
T amplitude;
T gain;
B basis;
virtual T operator() (util::point<2,T>) const override;
};
/// Rigid Multifractal summation, based on Musgrave's algorithm
///
/// octaves: count of layers to be summed
/// frequency: point scaling factor for the base octave
/// lacunarity: per octave frequency scaling factor
template <typename T, typename B>
struct rmf : public fractal<T> {
static constexpr unsigned DEFAULT_OCTAVES = 5;
static constexpr T DEFAULT_FREQUENCY = T(1);
static constexpr T DEFAULT_LACUNARITY = 2;
static constexpr T DEFAULT_AMPLITUDE = 1;
static constexpr T DEFAULT_GAIN = 2;
rmf (unsigned octaves,
T frequency,
T lacunarity,
T amplitude,
T gain,
seed_t seed);
rmf ();
unsigned octaves;
T frequency;
T lacunarity;
T amplitude;
T gain;
B basis;
virtual T operator() (util::point<2,T>) const override;
};
///////////////////////////////////////////////////////////////////////
template <typename T, typename B>
struct hmf : public fractal<T> {
hmf ();
T H;
unsigned octaves;
T frequency;
T lacunarity;
T offset;
T amplitude;
T gain;
B basis;
virtual T operator() (util::point<2,T>) const override;
};
///////////////////////////////////////////////////////////////////////
/// Heterogeneous procedural terrain fucntion: stats by altitude method
template <typename T, typename B>
struct hetero : public fractal<T> {
hetero ();
T H;
unsigned octaves;
T frequency;
T lacunarity;
T offset;
T amplitude;
T gain;
B basis;
virtual T operator() (util::point<2,T>) const override;
};
}
}
#endif

100
noise/fractal/fbm.cpp Normal file
View File

@ -0,0 +1,100 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/
#include "fbm.hpp"
#include "../basis/constant.hpp"
#include "../basis/perlin.hpp"
#include "../basis/value.hpp"
#include "../basis/worley.hpp"
#include "../lerp.hpp"
using util::noise::fractal::fbm;
///////////////////////////////////////////////////////////////////////////////
template <typename T, typename B>
fbm<T,B>::fbm (unsigned _octaves,
T _frequency,
T _lacunarity,
T _amplitude,
T _gain,
seed_t _seed):
seed (_seed),
octaves (_octaves),
frequency (_frequency),
lacunarity (_lacunarity),
amplitude (_amplitude),
gain (_gain),
basis (_seed)
{
CHECK_NEQ (octaves, 0);
CHECK_NEQ (frequency, 0);
CHECK_NEQ (amplitude, 0);
}
//-----------------------------------------------------------------------------
template <typename T, typename B>
fbm<T,B>::fbm ():
fbm<T,B> (DEFAULT_OCTAVES,
DEFAULT_FREQUENCY,
DEFAULT_LACUNARITY,
DEFAULT_AMPLITUDE,
DEFAULT_GAIN,
rand ())
{ ; }
//-----------------------------------------------------------------------------
template <typename T, typename B>
T
fbm<T,B>::operator() (util::point<2,T> p) const {
T total = 0;
T f = frequency;
T a = amplitude;
for (size_t i = 0; i < octaves; ++i) {
total += basis (p * f) * a;
f *= lacunarity;
a *= gain;
}
return total;
}
//-----------------------------------------------------------------------------
template struct util::noise::fractal::fbm<float, util::noise::basis::worley<float>>;
template struct util::noise::fractal::fbm<float, util::noise::basis::constant<float>>;
template struct util::noise::fractal::fbm<float, util::noise::basis::perlin<float,util::lerp::trunc>>;
template struct util::noise::fractal::fbm<float, util::noise::basis::perlin<float,util::lerp::linear>>;
template struct util::noise::fractal::fbm<float, util::noise::basis::perlin<float,util::lerp::cubic>>;
template struct util::noise::fractal::fbm<float, util::noise::basis::perlin<float,util::lerp::quintic>>;
template struct util::noise::fractal::fbm<float, util::noise::basis::value<float,util::lerp::trunc>>;
template struct util::noise::fractal::fbm<float, util::noise::basis::value<float,util::lerp::linear>>;
template struct util::noise::fractal::fbm<float, util::noise::basis::value<float,util::lerp::cosine>>;
template struct util::noise::fractal::fbm<float, util::noise::basis::value<float,util::lerp::cubic>>;
template struct util::noise::fractal::fbm<float, util::noise::basis::value<float,util::lerp::quintic>>;
template struct util::noise::fractal::fbm<double, util::noise::basis::worley<double>>;
template struct util::noise::fractal::fbm<double, util::noise::basis::perlin<double,util::lerp::linear>>;
template struct util::noise::fractal::fbm<double, util::noise::basis::perlin<double,util::lerp::quintic>>;
template struct util::noise::fractal::fbm<double, util::noise::basis::value<double,util::lerp::trunc>>;
template struct util::noise::fractal::fbm<double, util::noise::basis::value<double,util::lerp::linear>>;
template struct util::noise::fractal::fbm<double, util::noise::basis::value<double,util::lerp::quintic>>;

67
noise/fractal/fbm.hpp Normal file
View File

@ -0,0 +1,67 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_NOISE_FRACTAL_FBM_HPP
#define __UTIL_NOISE_FRACTAL_FBM_HPP
#include <cstdint>
#include "../../point.hpp"
namespace util { namespace noise { namespace fractal {
/// Fractal Brownian Motion summation.
///
/// Sum progressive layers of a noise basis with scaling frequency
/// and amplitude.
///
/// octaves: count of layers to be summed
/// frequency: point scaling factor for the base octave
/// lacunarity: per octave frequency scaling factor
/// amplitude: maximum absolute value of the noise
/// gain: per octave amplitude scaling factor. typically 1/f.
template <typename T, typename B>
struct fbm {
using seed_t = uint64_t;
static constexpr unsigned DEFAULT_OCTAVES = 8;
static constexpr T DEFAULT_FREQUENCY = T(0.1);
static constexpr T DEFAULT_LACUNARITY = 2;
static constexpr T DEFAULT_AMPLITUDE = 1;
static constexpr T DEFAULT_GAIN = 1 / T(2);
fbm (unsigned octaves,
T frequency,
T lacunarity,
T amplitude,
T gain,
seed_t seed);
fbm ();
seed_t seed;
unsigned octaves;
T frequency;
T lacunarity;
T amplitude;
T gain;
B basis;
T operator() (util::point<2,T>) const;
};
} } }
#endif

76
noise/fractal/hetero.cpp Normal file
View File

@ -0,0 +1,76 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/
#include "hetero.hpp"
#include "../basis/constant.hpp"
#include "../basis/perlin.hpp"
#include "../basis/value.hpp"
#include "../basis/worley.hpp"
#include "../lerp.hpp"
using util::noise::fractal::hetero;
//-----------------------------------------------------------------------------
template <typename T, typename B>
hetero<T,B>::hetero():
H (0.75f),
octaves (6),
frequency (0.1f),
lacunarity (2),
offset (0.7f),
amplitude (1),
gain (1)
{ ; }
//-----------------------------------------------------------------------------
template <typename T, typename B>
T
hetero<T,B>::operator() (util::point<2,T> p) const
{
T exponents[octaves];
for (size_t i = 0; i < octaves; ++i)
exponents[i] = std::pow (std::pow (lacunarity, float (i)), -H);
T result = 0;
T increment = 0;
p *= frequency;
result = basis (p) + offset;
p *= lacunarity;
for (size_t i = 0; i < octaves; ++i) {
increment = basis (p) + offset;
increment *= exponents[i];
increment *= result;
result += increment;
p *= lacunarity;
}
return result;
}
//-----------------------------------------------------------------------------
template struct util::noise::fractal::hetero<float, util::noise::basis::worley<float>>;
template struct util::noise::fractal::hetero<float, util::noise::basis::perlin<float,util::lerp::linear>>;
template struct util::noise::fractal::hetero<float, util::noise::basis::perlin<float,util::lerp::quintic>>;
template struct util::noise::fractal::hetero<float, util::noise::basis::value<float,util::lerp::linear>>;
template struct util::noise::fractal::hetero<float, util::noise::basis::value<float,util::lerp::quintic>>;

53
noise/fractal/hetero.hpp Normal file
View File

@ -0,0 +1,53 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_NOISE_FRACTAL_HETERO_HPP
#define __UTIL_NOISE_FRACTAL_HETERO_HPP
#include <cstdint>
#include "../../point.hpp"
namespace util { namespace noise { namespace fractal {
///////////////////////////////////////////////////////////////////////
/// Heterogeneous procedural terrain fucntion: stats by altitude method
template <typename T, typename B>
struct hetero {
using seed_t = uint64_t;
hetero ();
seed_t seed;
T H;
unsigned octaves;
T frequency;
T lacunarity;
T offset;
T amplitude;
T gain;
B basis;
T operator() (util::point<2,T>) const;
};
} } }
#endif

75
noise/fractal/hmf.cpp Normal file
View File

@ -0,0 +1,75 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/
#include "hmf.hpp"
#include "../basis/constant.hpp"
#include "../basis/perlin.hpp"
#include "../basis/value.hpp"
#include "../basis/worley.hpp"
#include "../lerp.hpp"
using util::noise::fractal::hmf;
//-----------------------------------------------------------------------------
template <typename T, typename B>
hmf<T,B>::hmf ():
H (0.25f),
octaves (6),
frequency (0.1f),
lacunarity (2),
offset (0.7f),
amplitude (1),
gain (1)
{ ; }
//-----------------------------------------------------------------------------
template <typename T, typename B>
T
hmf<T,B>::operator() (util::point<2,T> p) const
{
T exponents[octaves];
for (size_t i = 0; i < octaves; ++i)
exponents[i] = std::pow (std::pow (lacunarity, float (i)), -H);
T result = 0;
T signal = 0;
T weight = 1;
p *= frequency;
for (size_t i = 0; i < octaves; ++i) {
signal = (basis (p) + offset) * exponents[i];
result += weight * signal;
weight *= gain * signal;
if (weight > 1)
weight = 1;
p *= lacunarity;
}
return result;
}
template struct util::noise::fractal::hmf<float, util::noise::basis::worley<float>>;
template struct util::noise::fractal::hmf<float, util::noise::basis::perlin<float,util::lerp::linear>>;
template struct util::noise::fractal::hmf<float, util::noise::basis::perlin<float,util::lerp::quintic>>;
template struct util::noise::fractal::hmf<float, util::noise::basis::value<float,util::lerp::linear>>;
template struct util::noise::fractal::hmf<float, util::noise::basis::value<float,util::lerp::quintic>>;

52
noise/fractal/hmf.hpp Normal file
View File

@ -0,0 +1,52 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_NOISE_FRACTAL_HMF_HPP
#define __UTIL_NOISE_FRACTAL_HMF_HPP
#include <cstdint>
#include "../../point.hpp"
namespace util { namespace noise { namespace fractal {
///////////////////////////////////////////////////////////////////////
template <typename T, typename B>
struct hmf {
using seed_t = uint64_t;
hmf ();
seed_t seed;
T H;
unsigned octaves;
T frequency;
T lacunarity;
T offset;
T amplitude;
T gain;
B basis;
T operator() (util::point<2,T>) const;
};
} } }
#endif

113
noise/fractal/rmf.cpp Normal file
View File

@ -0,0 +1,113 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/
#include "rmf.hpp"
#include "../basis/constant.hpp"
#include "../basis/perlin.hpp"
#include "../basis/value.hpp"
#include "../basis/worley.hpp"
#include "../lerp.hpp"
using util::noise::fractal::rmf;
///////////////////////////////////////////////////////////////////////////////
template <typename T, typename B>
rmf<T,B>::rmf (unsigned _octaves,
T _frequency,
T _lacunarity,
T _amplitude,
T _gain,
seed_t _seed):
seed (_seed),
octaves (_octaves),
frequency (_frequency),
lacunarity (_lacunarity),
amplitude (_amplitude),
gain (_gain),
basis (_seed)
{ ; }
//-----------------------------------------------------------------------------
template <typename T, typename B>
rmf<T,B>::rmf ():
rmf<T,B> (DEFAULT_OCTAVES,
DEFAULT_FREQUENCY,
DEFAULT_LACUNARITY,
DEFAULT_AMPLITUDE,
DEFAULT_GAIN,
rand ())
{ ; }
//-----------------------------------------------------------------------------
template <typename T, typename B>
T
rmf<T,B>::operator() (util::point<2,T> p) const {
const T offset = 1;
const T H = 1.f;
T exponents[octaves];
for (size_t i = 0; i < octaves; ++i)
exponents[i] = std::pow (std::pow (lacunarity, float (i)), -H);
T signal = 0;
T result = 0;
T weight = 1;
p *= frequency;
for (size_t i = 0; i < octaves; ++i) {
// generates ridged noise
signal = basis (p);
signal = std::fabs (signal);
signal = offset - signal;
// sharpens the ridges
signal *= signal;
// influence by sharpness of previous iteration
signal *= weight;
// contribute to the weight
weight = signal * gain;
weight = limit (weight, 0, 1);
// record and continue
result += signal * exponents[i];
p *= lacunarity;
}
return result;
}
//-----------------------------------------------------------------------------
template struct util::noise::fractal::rmf<float, util::noise::basis::worley<float>>;
template struct util::noise::fractal::rmf<float, util::noise::basis::perlin<float,util::lerp::linear>>;
template struct util::noise::fractal::rmf<float, util::noise::basis::perlin<float,util::lerp::cubic>>;
template struct util::noise::fractal::rmf<float, util::noise::basis::perlin<float,util::lerp::quintic>>;
template struct util::noise::fractal::rmf<float, util::noise::basis::value<float,util::lerp::linear>>;
template struct util::noise::fractal::rmf<float, util::noise::basis::value<float,util::lerp::quintic>>;
template struct util::noise::fractal::rmf<double, util::noise::basis::worley<double>>;
template struct util::noise::fractal::rmf<double, util::noise::basis::perlin<double,util::lerp::linear>>;
template struct util::noise::fractal::rmf<double, util::noise::basis::perlin<double,util::lerp::quintic>>;
template struct util::noise::fractal::rmf<double, util::noise::basis::value<double,util::lerp::linear>>;
template struct util::noise::fractal::rmf<double, util::noise::basis::value<double,util::lerp::quintic>>;

62
noise/fractal/rmf.hpp Normal file
View File

@ -0,0 +1,62 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_NOISE_FRACTAL_RMF_HPP
#define __UTIL_NOISE_FRACTAL_RMF_HPP
#include <cstdint>
#include "../../point.hpp"
namespace util { namespace noise { namespace fractal {
/// Rigid Multifractal summation, based on Musgrave's algorithm
///
/// octaves: count of layers to be summed
/// frequency: point scaling factor for the base octave
/// lacunarity: per octave frequency scaling factor
template <typename T, typename B>
struct rmf {
using seed_t = uint64_t;
static constexpr unsigned DEFAULT_OCTAVES = 5;
static constexpr T DEFAULT_FREQUENCY = T(1);
static constexpr T DEFAULT_LACUNARITY = 2;
static constexpr T DEFAULT_AMPLITUDE = 1;
static constexpr T DEFAULT_GAIN = 2;
rmf (unsigned octaves,
T frequency,
T lacunarity,
T amplitude,
T gain,
seed_t seed);
rmf ();
seed_t seed;
unsigned octaves;
T frequency;
T lacunarity;
T amplitude;
T gain;
B basis;
T operator() (util::point<2,T>) const;
};
} } }
#endif

View File

@ -1,5 +1,10 @@
#include "image.hpp"
#include "noise.hpp"
#include "noise/fractal/fbm.hpp"
#include "noise/fractal/rmf.hpp"
#include "noise/fractal/hmf.hpp"
#include "noise/fractal/hetero.hpp"
#include "noise/lerp.hpp"
#include "noise/basis/constant.hpp"
#include "noise/basis/value.hpp"
@ -15,11 +20,12 @@ main (void)
util::image::buffer<float> img (size);
// setup the noise generator
//util::noise::fbm<float, util::noise::basis::worley<float>> b;
//util::noise::rmf<float, util::noise::basis::worley<float>> b;
//util::noise::hmf<float, util::noise::basis::perlin<float,util::lerp::quintic>> b;
util::noise::hetero<float, util::noise::basis::perlin<float,util::lerp::quintic>> b;
//b.octaves = 3;
util::noise::fractal::fbm<float, util::noise::basis::worley<float>> b;
//util::noise::fractal::rmf<float, util::noise::basis::worley<float>> b;
//util::noise::fractal::fbm<float, util::noise::basis::perlin<float,util::lerp::cubic>> b;
//util::noise::fractal::rmf<float, util::noise::basis::perlin<float,util::lerp::cubic>> b;
//util::noise::fractal::hetero<float, util::noise::basis::perlin<float,util::lerp::quintic>> b;
b.frequency = 10.f / size.w;
b.lacunarity = 2;
b.basis.seed = time (NULL);
@ -30,7 +36,7 @@ main (void)
for (size_t y = 0; y < size.h; ++y)
for (size_t x = 0; x < size.w; ++x) {
auto v = b (util::point2f {float (x), float (y)} + offset); //{x + offset, y + offset});
auto v = b (util::point2f {float (x), float (y)} + offset);
img.data ()[y * size.w + x] = v;
}
}