n/fractal: use a common base class
some working variables need to be precomputed. it makes sense to do this in a base class.
This commit is contained in:
parent
bc1c576297
commit
6278902e3e
@ -145,6 +145,8 @@ UTIL_FILES = \
|
|||||||
noise/basis/perlin.ipp \
|
noise/basis/perlin.ipp \
|
||||||
noise/basis/worley.hpp \
|
noise/basis/worley.hpp \
|
||||||
noise/basis/worley.ipp \
|
noise/basis/worley.ipp \
|
||||||
|
noise/fractal/base.hpp \
|
||||||
|
noise/fractal/base.ipp \
|
||||||
noise/fractal/fbm.hpp \
|
noise/fractal/fbm.hpp \
|
||||||
noise/fractal/fbm.ipp \
|
noise/fractal/fbm.ipp \
|
||||||
noise/fractal/hetero.hpp \
|
noise/fractal/hetero.hpp \
|
||||||
|
84
noise/fractal/base.hpp
Normal file
84
noise/fractal/base.hpp
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* 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 2015 Danny Robson <danny@nerdcruft.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __UTIL_NOISE_FRACTAL_BASE_HPP
|
||||||
|
#define __UTIL_NOISE_FRACTAL_BASE_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 base {
|
||||||
|
using seed_t = uint64_t;
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
base (seed_t,
|
||||||
|
unsigned octaves,
|
||||||
|
T H,
|
||||||
|
T frequency,
|
||||||
|
T lacunarity,
|
||||||
|
T amplitude,
|
||||||
|
T gain);
|
||||||
|
|
||||||
|
// accessors
|
||||||
|
constexpr unsigned octaves (void) const;
|
||||||
|
unsigned octaves (unsigned);
|
||||||
|
|
||||||
|
constexpr T H (void) const;
|
||||||
|
T H (T);
|
||||||
|
|
||||||
|
constexpr T frequency (void) const;
|
||||||
|
T frequency (T);
|
||||||
|
|
||||||
|
constexpr T lacunarity (void) const;
|
||||||
|
T lacunarity (T);
|
||||||
|
|
||||||
|
constexpr seed_t seed (void) const;
|
||||||
|
seed_t seed (seed_t);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
unsigned m_octaves;
|
||||||
|
T m_H;
|
||||||
|
|
||||||
|
T m_frequency;
|
||||||
|
T m_lacunarity;
|
||||||
|
|
||||||
|
T m_amplitude;
|
||||||
|
T m_gain;
|
||||||
|
|
||||||
|
B m_basis;
|
||||||
|
|
||||||
|
T m_invAH;
|
||||||
|
T m_invGH;
|
||||||
|
};
|
||||||
|
} } }
|
||||||
|
|
||||||
|
#include "base.ipp"
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
99
noise/fractal/base.ipp
Normal file
99
noise/fractal/base.ipp
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
/*
|
||||||
|
* 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 2015 Danny Robson <danny@nerdcruft.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef __UTIL_NOISE_FRACTAL_BASE_IPP
|
||||||
|
#error
|
||||||
|
#endif
|
||||||
|
#define __UTIL_NOISE_FRACTAL_BASE_IPP
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace util { namespace noise { namespace fractal {
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T, typename B>
|
||||||
|
base<T,B>::base (seed_t _seed,
|
||||||
|
unsigned _octaves,
|
||||||
|
T _H,
|
||||||
|
T _frequency,
|
||||||
|
T _lacunarity,
|
||||||
|
T _amplitude,
|
||||||
|
T _gain):
|
||||||
|
// literals
|
||||||
|
m_octaves (_octaves),
|
||||||
|
m_H (_H),
|
||||||
|
m_frequency (_frequency),
|
||||||
|
m_lacunarity (_lacunarity),
|
||||||
|
m_amplitude (_amplitude),
|
||||||
|
m_gain (_gain),
|
||||||
|
// compound
|
||||||
|
m_basis (_seed),
|
||||||
|
// calculated
|
||||||
|
m_invAH (std::pow (_amplitude, -_H)),
|
||||||
|
m_invGH (std::pow (_gain, _H))
|
||||||
|
{
|
||||||
|
CHECK_NEQ (m_octaves, 0);
|
||||||
|
CHECK_NEQ (m_frequency, 0);
|
||||||
|
CHECK_NEQ (m_amplitude, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T, typename B>
|
||||||
|
unsigned
|
||||||
|
base<T,B>::octaves (unsigned _octaves)
|
||||||
|
{
|
||||||
|
return m_octaves = _octaves;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
template <typename T, typename B>
|
||||||
|
T
|
||||||
|
base<T,B>::H (T _h)
|
||||||
|
{
|
||||||
|
m_H = _h;
|
||||||
|
m_invAH = std::pow (m_amplitude, -m_H);
|
||||||
|
m_invGH = std::pow (m_gain, m_H);
|
||||||
|
return H;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
template <typename T, typename B>
|
||||||
|
T
|
||||||
|
base<T,B>::frequency (T _frequency)
|
||||||
|
{
|
||||||
|
return m_frequency = _frequency;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
template <typename T, typename B>
|
||||||
|
T
|
||||||
|
base<T,B>::lacunarity (T _lacunarity)
|
||||||
|
{
|
||||||
|
return m_lacunarity = _lacunarity;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
//template <typename T, typename B>
|
||||||
|
//typename base<T,B>::seed_t
|
||||||
|
//base<T,B>::seed (seed_t _seed)
|
||||||
|
//{
|
||||||
|
// return basis.seed (_seed);
|
||||||
|
//}
|
||||||
|
} } }
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "base.hpp"
|
||||||
#include "../../point.hpp"
|
#include "../../point.hpp"
|
||||||
|
|
||||||
namespace util { namespace noise { namespace fractal {
|
namespace util { namespace noise { namespace fractal {
|
||||||
@ -33,8 +34,8 @@ namespace util { namespace noise { namespace fractal {
|
|||||||
/// amplitude: maximum absolute value of the noise
|
/// amplitude: maximum absolute value of the noise
|
||||||
/// gain: per octave amplitude scaling factor. typically 1/f.
|
/// gain: per octave amplitude scaling factor. typically 1/f.
|
||||||
template <typename T, typename B>
|
template <typename T, typename B>
|
||||||
struct fbm {
|
struct fbm : public base<T,B> {
|
||||||
using seed_t = uint64_t;
|
using seed_t = typename base<T,B>::seed_t;
|
||||||
|
|
||||||
static constexpr unsigned DEFAULT_OCTAVES = 8;
|
static constexpr unsigned DEFAULT_OCTAVES = 8;
|
||||||
static constexpr T DEFAULT_H = 1;
|
static constexpr T DEFAULT_H = 1;
|
||||||
@ -43,31 +44,16 @@ namespace util { namespace noise { namespace fractal {
|
|||||||
static constexpr T DEFAULT_AMPLITUDE = 1;
|
static constexpr T DEFAULT_AMPLITUDE = 1;
|
||||||
static constexpr T DEFAULT_GAIN = 1 / DEFAULT_LACUNARITY;
|
static constexpr T DEFAULT_GAIN = 1 / DEFAULT_LACUNARITY;
|
||||||
|
|
||||||
fbm (unsigned octaves,
|
fbm (seed_t seed,
|
||||||
|
unsigned octaves,
|
||||||
T H,
|
T H,
|
||||||
T frequency,
|
T frequency,
|
||||||
T lacunarity,
|
T lacunarity,
|
||||||
T amplitude,
|
T amplitude,
|
||||||
T gain,
|
T gain);
|
||||||
seed_t seed);
|
fbm (seed_t);
|
||||||
fbm ();
|
|
||||||
|
|
||||||
seed_t seed;
|
|
||||||
unsigned octaves;
|
|
||||||
T H;
|
|
||||||
|
|
||||||
T frequency;
|
|
||||||
T lacunarity;
|
|
||||||
|
|
||||||
T amplitude;
|
|
||||||
T gain;
|
|
||||||
|
|
||||||
B basis;
|
|
||||||
constexpr T operator() (util::point<2,T>) const;
|
constexpr T operator() (util::point<2,T>) const;
|
||||||
|
|
||||||
private:
|
|
||||||
T invAH;
|
|
||||||
T invGH;
|
|
||||||
};
|
};
|
||||||
} } }
|
} } }
|
||||||
|
|
||||||
|
@ -24,57 +24,50 @@
|
|||||||
namespace util { namespace noise { namespace fractal {
|
namespace util { namespace noise { namespace fractal {
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
template <typename T, typename B>
|
template <typename T, typename B>
|
||||||
fbm<T,B>::fbm (unsigned _octaves,
|
fbm<T,B>::fbm (seed_t _seed,
|
||||||
|
unsigned _octaves,
|
||||||
T _H,
|
T _H,
|
||||||
T _frequency,
|
T _frequency,
|
||||||
T _lacunarity,
|
T _lacunarity,
|
||||||
T _amplitude,
|
T _amplitude,
|
||||||
T _gain,
|
T _gain):
|
||||||
seed_t _seed):
|
base<T,B> (_seed,
|
||||||
seed (_seed),
|
_octaves,
|
||||||
octaves (_octaves),
|
_H,
|
||||||
H (_H),
|
_frequency,
|
||||||
frequency (_frequency),
|
_lacunarity,
|
||||||
lacunarity (_lacunarity),
|
_amplitude,
|
||||||
amplitude (_amplitude),
|
_gain)
|
||||||
gain (_gain),
|
|
||||||
basis (_seed),
|
|
||||||
invAH (std::pow (amplitude, -H)),
|
|
||||||
invGH (std::pow (gain, H))
|
|
||||||
{
|
|
||||||
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_H,
|
|
||||||
DEFAULT_FREQUENCY,
|
|
||||||
DEFAULT_LACUNARITY,
|
|
||||||
DEFAULT_AMPLITUDE,
|
|
||||||
DEFAULT_GAIN,
|
|
||||||
rand ())
|
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
template <typename T, typename B>
|
template <typename T, typename B>
|
||||||
|
fbm<T,B>::fbm (seed_t _seed):
|
||||||
|
fbm<T,B> (_seed,
|
||||||
|
DEFAULT_OCTAVES,
|
||||||
|
DEFAULT_H,
|
||||||
|
DEFAULT_FREQUENCY,
|
||||||
|
DEFAULT_LACUNARITY,
|
||||||
|
DEFAULT_AMPLITUDE,
|
||||||
|
DEFAULT_GAIN)
|
||||||
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T, typename B>
|
||||||
constexpr T
|
constexpr T
|
||||||
fbm<T,B>::operator() (util::point<2,T> p) const
|
fbm<T,B>::operator() (util::point<2,T> p) const
|
||||||
{
|
{
|
||||||
T total = 0;
|
T total = 0;
|
||||||
T f = frequency;
|
T f = this->m_frequency;
|
||||||
T a = invAH;
|
T a = this->m_invAH;
|
||||||
|
|
||||||
for (size_t i = 0; i < octaves; ++i) {
|
for (size_t i = 0; i < this->m_octaves; ++i) {
|
||||||
total += basis (p * f) * a;
|
total += this->m_basis (p * f) * a;
|
||||||
|
|
||||||
f *= lacunarity;
|
f *= this->m_lacunarity;
|
||||||
a *= invGH;
|
a *= this->m_invGH;
|
||||||
}
|
}
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
|
@ -19,36 +19,39 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "base.hpp"
|
||||||
#include "../../point.hpp"
|
#include "../../point.hpp"
|
||||||
|
|
||||||
namespace util { namespace noise { namespace fractal {
|
namespace util { namespace noise { namespace fractal {
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
/// Heterogeneous procedural terrain fucntion: stats by altitude method
|
/// Heterogeneous procedural terrain fucntion: stats by altitude method
|
||||||
template <typename T, typename B>
|
template <typename T, typename B>
|
||||||
struct hetero {
|
struct hetero : public base<T,B> {
|
||||||
using seed_t = uint64_t;
|
using seed_t = typename base<T,B>::seed_t;
|
||||||
|
|
||||||
hetero ();
|
static constexpr T DEFAULT_H = T(0.75);
|
||||||
|
static constexpr T DEFAULT_OCTAVES = 6;
|
||||||
|
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 / DEFAULT_LACUNARITY;
|
||||||
|
static constexpr T DEFAULT_OFFSET = T(0.7);
|
||||||
|
|
||||||
seed_t seed;
|
hetero (seed_t,
|
||||||
T H;
|
unsigned octaves,
|
||||||
unsigned octaves;
|
T H,
|
||||||
|
T frequency,
|
||||||
|
T lacunarity,
|
||||||
|
T amplitude,
|
||||||
|
T gain,
|
||||||
|
T offset);
|
||||||
|
|
||||||
T frequency;
|
hetero (seed_t);
|
||||||
T lacunarity;
|
|
||||||
|
|
||||||
T offset;
|
|
||||||
|
|
||||||
T amplitude;
|
|
||||||
T gain;
|
|
||||||
|
|
||||||
B basis;
|
|
||||||
|
|
||||||
constexpr T operator() (util::point<2,T>) const;
|
constexpr T operator() (util::point<2,T>) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T invAH;
|
T m_offset;
|
||||||
T invGH;
|
|
||||||
};
|
};
|
||||||
} } }
|
} } }
|
||||||
|
|
||||||
|
@ -20,44 +20,64 @@
|
|||||||
#define __UTIL_NOISE_FRACTAL_HETERO_IPP
|
#define __UTIL_NOISE_FRACTAL_HETERO_IPP
|
||||||
|
|
||||||
namespace util { namespace noise { namespace fractal {
|
namespace util { namespace noise { namespace fractal {
|
||||||
//-------------------------------------------------------------------------
|
///////////////////////////////////////////////////////////////////////////
|
||||||
template <typename T, typename B>
|
template <typename T, typename B>
|
||||||
hetero<T,B>::hetero():
|
hetero<T,B>::hetero(seed_t _seed,
|
||||||
H (0.75f),
|
unsigned _octaves,
|
||||||
octaves (6),
|
T _H,
|
||||||
frequency (0.1f),
|
T _frequency,
|
||||||
lacunarity (2),
|
T _lacunarity,
|
||||||
offset (0.7f),
|
T _amplitude,
|
||||||
amplitude (1),
|
T _gain,
|
||||||
gain (1/lacunarity),
|
T _offset):
|
||||||
invAH (std::pow (amplitude, -H)),
|
base<T,B> (_seed,
|
||||||
invGH (std::pow (gain, H))
|
_octaves,
|
||||||
|
_H,
|
||||||
|
_frequency,
|
||||||
|
_lacunarity,
|
||||||
|
_amplitude,
|
||||||
|
_gain),
|
||||||
|
m_offset (_offset)
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
template <typename T, typename B>
|
template <typename T, typename B>
|
||||||
|
hetero<T,B>::hetero (seed_t _seed):
|
||||||
|
hetero<T,B> (_seed,
|
||||||
|
DEFAULT_OCTAVES,
|
||||||
|
DEFAULT_H,
|
||||||
|
DEFAULT_FREQUENCY,
|
||||||
|
DEFAULT_LACUNARITY,
|
||||||
|
DEFAULT_AMPLITUDE,
|
||||||
|
DEFAULT_GAIN,
|
||||||
|
DEFAULT_OFFSET)
|
||||||
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T, typename B>
|
||||||
constexpr T
|
constexpr T
|
||||||
hetero<T,B>::operator() (util::point<2,T> p) const
|
hetero<T,B>::operator() (util::point<2,T> p) const
|
||||||
{
|
{
|
||||||
T scale = invAH;
|
T scale = this->m_invAH;
|
||||||
|
|
||||||
p *= frequency;
|
p *= this->m_frequency;
|
||||||
T result = (basis (p) + offset) * scale;
|
T result = (this->m_basis (p) + m_offset) * scale;
|
||||||
p *= lacunarity;
|
p *= this->m_lacunarity;
|
||||||
|
|
||||||
T increment = 0;
|
T increment = 0;
|
||||||
|
|
||||||
for (size_t i = 1; i < octaves; ++i) {
|
for (size_t i = 1; i < this->m_octaves; ++i) {
|
||||||
scale *= invGH;
|
scale *= this->m_invGH;
|
||||||
|
|
||||||
increment = basis (p) + offset;
|
increment = this->m_basis (p) + m_offset;
|
||||||
increment *= scale;
|
increment *= scale;
|
||||||
increment *= result;
|
increment *= result;
|
||||||
|
|
||||||
result += increment;
|
result += increment;
|
||||||
|
|
||||||
p *= lacunarity;
|
p *= this->m_lacunarity;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -25,31 +25,33 @@ namespace util { namespace noise { namespace fractal {
|
|||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
/// Musgrave's "Hybrid MultiFractal"
|
/// Musgrave's "Hybrid MultiFractal"
|
||||||
template <typename T, typename B>
|
template <typename T, typename B>
|
||||||
struct hmf {
|
struct hmf : public base<T,B> {
|
||||||
using seed_t = uint64_t;
|
using seed_t = typename base<T,B>::seed_t;
|
||||||
|
|
||||||
hmf ();
|
// H should be fairly low due to the decreasing weight parameter in eval
|
||||||
|
static constexpr unsigned DEFAULT_OCTAVES = 6;
|
||||||
|
static constexpr T DEFAULT_H = T(0.25);
|
||||||
|
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 / DEFAULT_LACUNARITY;
|
||||||
|
static constexpr T DEFAULT_OFFSET = T(0.7);
|
||||||
|
|
||||||
seed_t seed;
|
hmf (seed_t,
|
||||||
|
unsigned octaves,
|
||||||
|
T H,
|
||||||
|
T frequency,
|
||||||
|
T lacunarity,
|
||||||
|
T amplitude,
|
||||||
|
T gain,
|
||||||
|
T offset);
|
||||||
|
|
||||||
T H;
|
hmf (seed_t);
|
||||||
unsigned octaves;
|
|
||||||
|
|
||||||
T frequency;
|
|
||||||
T lacunarity;
|
|
||||||
|
|
||||||
T offset;
|
|
||||||
|
|
||||||
T amplitude;
|
|
||||||
T gain;
|
|
||||||
|
|
||||||
B basis;
|
|
||||||
|
|
||||||
constexpr T operator() (point<2,T>) const;
|
constexpr T operator() (point<2,T>) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T invAH;
|
T m_offset;
|
||||||
T invGH;
|
|
||||||
};
|
};
|
||||||
} } }
|
} } }
|
||||||
|
|
||||||
|
@ -21,44 +21,63 @@
|
|||||||
|
|
||||||
|
|
||||||
namespace util { namespace noise { namespace fractal {
|
namespace util { namespace noise { namespace fractal {
|
||||||
//-------------------------------------------------------------------------
|
///////////////////////////////////////////////////////////////////////////
|
||||||
// H should be fairly low due to the decreasing weight parameter in eval
|
|
||||||
template <typename T, typename B>
|
template <typename T, typename B>
|
||||||
hmf<T,B>::hmf ():
|
hmf<T,B>::hmf (seed_t _seed,
|
||||||
H (0.25f),
|
unsigned _octaves,
|
||||||
octaves (6),
|
T _H,
|
||||||
frequency (0.1f),
|
T _frequency,
|
||||||
lacunarity (2),
|
T _lacunarity,
|
||||||
offset (0.7f),
|
T _amplitude,
|
||||||
amplitude (1),
|
T _gain,
|
||||||
gain (1 / lacunarity),
|
T _offset):
|
||||||
invAH (std::pow (amplitude, -H)),
|
base<T,B> (_seed,
|
||||||
invGH (std::pow (gain, H))
|
_octaves,
|
||||||
|
_H,
|
||||||
|
_frequency,
|
||||||
|
_lacunarity,
|
||||||
|
_amplitude,
|
||||||
|
_gain),
|
||||||
|
m_offset (_offset)
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
template <typename T, typename B>
|
template <typename T, typename B>
|
||||||
|
hmf<T,B>::hmf (seed_t _seed):
|
||||||
|
hmf<T,B> (_seed,
|
||||||
|
DEFAULT_OCTAVES,
|
||||||
|
DEFAULT_H,
|
||||||
|
DEFAULT_FREQUENCY,
|
||||||
|
DEFAULT_LACUNARITY,
|
||||||
|
DEFAULT_AMPLITUDE,
|
||||||
|
DEFAULT_GAIN,
|
||||||
|
DEFAULT_OFFSET)
|
||||||
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T, typename B>
|
||||||
constexpr T
|
constexpr T
|
||||||
hmf<T,B>::operator() (util::point<2,T> p) const
|
hmf<T,B>::operator() (util::point<2,T> p) const
|
||||||
{
|
{
|
||||||
T scale = invAH;
|
T scale = this->m_invAH;
|
||||||
|
|
||||||
T result = 0;
|
T result = 0;
|
||||||
T signal = 0;
|
T signal = 0;
|
||||||
T weight = 1;
|
T weight = 1;
|
||||||
|
|
||||||
p *= frequency;
|
p *= this->m_frequency;
|
||||||
|
|
||||||
for (size_t i = 0; i < octaves; ++i) {
|
for (size_t i = 0; i < this->m_octaves; ++i) {
|
||||||
signal = (basis (p) + offset) * scale;
|
signal = (this->m_basis (p) + m_offset) * scale;
|
||||||
result += signal * weight;
|
result += signal * weight;
|
||||||
|
|
||||||
weight *= signal;
|
weight *= signal;
|
||||||
weight = min (weight, T{1});
|
weight = min (weight, T{1});
|
||||||
|
|
||||||
scale *= invGH;
|
scale *= this->m_invGH;
|
||||||
p *= lacunarity;
|
p *= this->m_lacunarity;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "base.hpp"
|
||||||
#include "../../point.hpp"
|
#include "../../point.hpp"
|
||||||
|
|
||||||
namespace util { namespace noise { namespace fractal {
|
namespace util { namespace noise { namespace fractal {
|
||||||
@ -32,8 +33,8 @@ namespace util { namespace noise { namespace fractal {
|
|||||||
/// amplitude: value scaling factor for the base octave
|
/// amplitude: value scaling factor for the base octave
|
||||||
/// gain: incremental octave value scaling factor
|
/// gain: incremental octave value scaling factor
|
||||||
template <typename T, typename B>
|
template <typename T, typename B>
|
||||||
struct rmf {
|
struct rmf : public base<T,B> {
|
||||||
using seed_t = uint64_t;
|
using seed_t = typename base<T,B>::seed_t;
|
||||||
|
|
||||||
static constexpr unsigned DEFAULT_OCTAVES = 5;
|
static constexpr unsigned DEFAULT_OCTAVES = 5;
|
||||||
static constexpr T DEFAULT_H = 1;
|
static constexpr T DEFAULT_H = 1;
|
||||||
@ -43,35 +44,21 @@ namespace util { namespace noise { namespace fractal {
|
|||||||
static constexpr T DEFAULT_AMPLITUDE = 2;
|
static constexpr T DEFAULT_AMPLITUDE = 2;
|
||||||
static constexpr T DEFAULT_GAIN = 1 / DEFAULT_LACUNARITY;
|
static constexpr T DEFAULT_GAIN = 1 / DEFAULT_LACUNARITY;
|
||||||
|
|
||||||
rmf (unsigned octaves,
|
rmf (seed_t,
|
||||||
|
unsigned octaves,
|
||||||
T H,
|
T H,
|
||||||
T offset,
|
|
||||||
T frequency,
|
T frequency,
|
||||||
T lacunarity,
|
T lacunarity,
|
||||||
T amplitude,
|
T amplitude,
|
||||||
T gain,
|
T gain,
|
||||||
seed_t seed);
|
T offset);
|
||||||
rmf ();
|
|
||||||
|
|
||||||
seed_t seed;
|
rmf (seed_t);
|
||||||
|
|
||||||
unsigned octaves;
|
|
||||||
T H;
|
|
||||||
T offset;
|
|
||||||
|
|
||||||
T frequency;
|
|
||||||
T lacunarity;
|
|
||||||
|
|
||||||
T amplitude;
|
|
||||||
T gain;
|
|
||||||
|
|
||||||
B basis;
|
|
||||||
|
|
||||||
constexpr T operator() (util::point<2,T>) const;
|
constexpr T operator() (util::point<2,T>) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T invAH;
|
T m_offset;
|
||||||
T invGH;
|
|
||||||
};
|
};
|
||||||
} } }
|
} } }
|
||||||
|
|
||||||
|
@ -23,62 +23,59 @@
|
|||||||
namespace util { namespace noise { namespace fractal {
|
namespace util { namespace noise { namespace fractal {
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
template <typename T, typename B>
|
template <typename T, typename B>
|
||||||
rmf<T,B>::rmf (unsigned _octaves,
|
rmf<T,B>::rmf (seed_t _seed,
|
||||||
|
unsigned _octaves,
|
||||||
T _H,
|
T _H,
|
||||||
T _offset,
|
T _offset,
|
||||||
T _frequency,
|
T _frequency,
|
||||||
T _lacunarity,
|
T _lacunarity,
|
||||||
T _amplitude,
|
T _amplitude,
|
||||||
T _gain,
|
T _gain):
|
||||||
seed_t _seed):
|
base<T,B> (_seed,
|
||||||
seed (_seed),
|
_octaves,
|
||||||
octaves (_octaves),
|
_H,
|
||||||
H (_H),
|
_frequency,
|
||||||
offset (_offset),
|
_lacunarity,
|
||||||
frequency (_frequency),
|
_amplitude,
|
||||||
lacunarity (_lacunarity),
|
_gain),
|
||||||
amplitude (_amplitude),
|
m_offset (_offset)
|
||||||
gain (_gain),
|
|
||||||
basis (_seed),
|
|
||||||
invAH (std::pow (amplitude, -H)),
|
|
||||||
invGH (std::pow (gain, H))
|
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
template <typename T, typename B>
|
template <typename T, typename B>
|
||||||
rmf<T,B>::rmf ():
|
rmf<T,B>::rmf (seed_t _seed):
|
||||||
rmf<T,B> (DEFAULT_OCTAVES,
|
rmf<T,B> (_seed,
|
||||||
|
DEFAULT_OCTAVES,
|
||||||
DEFAULT_H,
|
DEFAULT_H,
|
||||||
DEFAULT_OFFSET,
|
|
||||||
DEFAULT_FREQUENCY,
|
DEFAULT_FREQUENCY,
|
||||||
DEFAULT_LACUNARITY,
|
DEFAULT_LACUNARITY,
|
||||||
DEFAULT_AMPLITUDE,
|
DEFAULT_AMPLITUDE,
|
||||||
DEFAULT_GAIN,
|
DEFAULT_GAIN,
|
||||||
rand ())
|
DEFAULT_OFFSET)
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
///////////////////////////////////////////////////////////////////////////
|
||||||
// we use the name 'amplitude' instead of musgrave's 'gain'.
|
// we use the name 'amplitude' instead of musgrave's 'gain'.
|
||||||
// assumes basis distribution [-1,1] and offset ~= 1
|
// assumes basis distribution [-1,1] and offset ~= 1
|
||||||
template <typename T, typename B>
|
template <typename T, typename B>
|
||||||
constexpr T
|
constexpr T
|
||||||
rmf<T,B>::operator() (util::point<2,T> p) const
|
rmf<T,B>::operator() (util::point<2,T> p) const
|
||||||
{
|
{
|
||||||
T scale = invAH;
|
T scale = this->m_invAH;
|
||||||
|
|
||||||
T signal = 0;
|
T signal = 0;
|
||||||
T result = 0;
|
T result = 0;
|
||||||
T weight = 1;
|
T weight = 1;
|
||||||
|
|
||||||
p *= frequency;
|
p *= this->m_frequency;
|
||||||
|
|
||||||
for (size_t i = 0; i < octaves; ++i) {
|
for (size_t i = 0; i < this->m_octaves; ++i) {
|
||||||
// generates ridged noise
|
// generates ridged noise
|
||||||
signal = basis (p);
|
signal = this->m_basis (p);
|
||||||
signal = std::fabs (signal);
|
signal = std::fabs (signal);
|
||||||
signal = offset - signal;
|
signal = m_offset - signal;
|
||||||
|
|
||||||
// sharpens the ridges
|
// sharpens the ridges
|
||||||
signal *= signal;
|
signal *= signal;
|
||||||
@ -87,14 +84,14 @@ namespace util { namespace noise { namespace fractal {
|
|||||||
signal *= weight;
|
signal *= weight;
|
||||||
|
|
||||||
// contribute to the weight
|
// contribute to the weight
|
||||||
weight = signal * amplitude;
|
weight = signal * this->m_amplitude;
|
||||||
weight = limit (weight, 0, 1);
|
weight = limit (weight, 0, 1);
|
||||||
|
|
||||||
// record and continue
|
// record and continue
|
||||||
result += signal * scale;
|
result += signal * scale;
|
||||||
|
|
||||||
scale *= invGH;
|
scale *= this->m_invGH;
|
||||||
p *= lacunarity;
|
p *= this->m_lacunarity;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -25,19 +25,21 @@ main (void)
|
|||||||
util::extent2u size {1920, 1080};
|
util::extent2u size {1920, 1080};
|
||||||
util::image::buffer<float> img (size);
|
util::image::buffer<float> img (size);
|
||||||
|
|
||||||
// setup the noise generator
|
uint64_t seed = time (nullptr);
|
||||||
//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::hmf<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.octaves = 8;
|
// setup the noise generator
|
||||||
b.frequency = 10.f / size.w;
|
util::noise::fractal::fbm<float, util::noise::basis::worley<float>> b (seed);
|
||||||
b.lacunarity = 2.f;
|
//util::noise::fractal::rmf<float, util::noise::basis::worley<float>> b (seed);
|
||||||
//b.H = 0.75f;
|
//util::noise::fractal::fbm<float, util::noise::basis::perlin<float,util::lerp::cubic>> b (seed);
|
||||||
b.basis.seed = time (NULL);
|
//util::noise::fractal::rmf<float, util::noise::basis::perlin<float,util::lerp::cubic>> b (seed);
|
||||||
|
//util::noise::fractal::hmf<float, util::noise::basis::perlin<float,util::lerp::cubic>> b (seed);
|
||||||
|
//util::noise::fractal::hetero<float, util::noise::basis::value<float,util::lerp::quintic>> b (seed);
|
||||||
|
|
||||||
|
b.octaves (8);
|
||||||
|
b.frequency (10.f / size.w);
|
||||||
|
//b.lacunarity = 2.f;
|
||||||
|
//b.H = 1.0f;
|
||||||
|
//b.basis.seed = time (NULL);
|
||||||
|
|
||||||
// generate the values. offset positions slightly to avoid simple axis issues with perlin basis
|
// generate the values. offset positions slightly to avoid simple axis issues with perlin basis
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user