n/fractal: use inline headers for fractals

This commit is contained in:
Danny Robson 2015-06-01 17:21:51 +10:00
parent 00195c54d0
commit f6e2e6a409
15 changed files with 322 additions and 376 deletions

View File

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

View File

@ -22,12 +22,11 @@
#include "image.hpp" #include "image.hpp"
namespace util { namespace util { namespace noise {
namespace noise {
template <typename T, typename G> template <typename T, typename G>
void fill (image::buffer<T>&, const G&); void fill (image::buffer<T>&, const G&);
} } }
}
#include "noise.ipp"
#endif #endif

View File

@ -1,100 +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-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>>;

View File

@ -64,4 +64,6 @@ namespace util { namespace noise { namespace fractal {
}; };
} } } } } }
#include "fbm.ipp"
#endif #endif

77
noise/fractal/fbm.ipp Normal file
View File

@ -0,0 +1,77 @@
/*
* 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>
*/
#ifdef __UTIL_NOISE_FRACTAL_FBM_IPP
#error
#endif
#define __UTIL_NOISE_FRACTAL_FBM_IPP
#include "../../debug.hpp"
namespace util { namespace noise { namespace fractal {
///////////////////////////////////////////////////////////////////////////
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;
}
} } }

View File

@ -1,76 +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-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>>;

View File

@ -48,6 +48,8 @@ namespace util { namespace noise { namespace fractal {
}; };
} } } } } }
#include "hetero.ipp"
#endif #endif

64
noise/fractal/hetero.ipp Normal file
View File

@ -0,0 +1,64 @@
/*
* 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>
*/
#ifdef __UTIL_NOISE_FRACTAL_HETERO_IPP
#error
#endif
#define __UTIL_NOISE_FRACTAL_HETERO_IPP
namespace util { namespace noise { namespace fractal {
//-------------------------------------------------------------------------
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;
}
} } }

View File

@ -1,75 +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-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>>;

View File

@ -48,5 +48,7 @@ namespace util { namespace noise { namespace fractal {
}; };
} } } } } }
#include "hmf.ipp"
#endif #endif

65
noise/fractal/hmf.ipp Normal file
View File

@ -0,0 +1,65 @@
/*
* 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>
*/
#ifdef __UTIL_NOISE_FRACTAL_HMF_IPP
#error
#endif
#define __UTIL_NOISE_FRACTAL_HMF_IPP
namespace util { namespace noise { namespace fractal {
//-------------------------------------------------------------------------
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;
}
} } }

View File

@ -1,113 +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-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>>;

View File

@ -59,4 +59,6 @@ namespace util { namespace noise { namespace fractal {
}; };
} } } } } }
#include "rmf.ipp"
#endif #endif

96
noise/fractal/rmf.ipp Normal file
View File

@ -0,0 +1,96 @@
/*
* 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>
*/
#ifdef __UTIL_NOISE_FRACTAL_RMF_IPP
#error
#endif
#define __UTIL_NOISE_FRACTAL_RMF_IPP
namespace util { namespace noise { namespace fractal {
///////////////////////////////////////////////////////////////////////////
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;
}
} } }

View File

@ -11,19 +11,20 @@
#include "noise/basis/perlin.hpp" #include "noise/basis/perlin.hpp"
#include "noise/basis/worley.hpp" #include "noise/basis/worley.hpp"
#include "extent.hpp" #include "extent.hpp"
int int
main (void) main (void)
{ {
// setup the output buffer // setup the output buffer
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 // setup the noise generator
util::noise::fractal::fbm<float, util::noise::basis::worley<float>> b; //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::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::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::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; //util::noise::fractal::hetero<float, util::noise::basis::perlin<float,util::lerp::quintic>> b;
b.octaves = 8; b.octaves = 8;
@ -50,7 +51,7 @@ main (void)
std::cerr << "range: [" << *range.first << ", " << *range.second << "]\n"; std::cerr << "range: [" << *range.first << ", " << *range.second << "]\n";
std::transform (img.begin (), img.end (), img.begin (), [offset,div] (auto i) { return (i - offset) / div; }); std::transform (img.begin (), img.end (), img.begin (), [offset,div] (auto i) { return (i - offset) / div; });
// write the image to disk // write the image to disk
auto grey = img.clone<uint8_t> (); auto grey = img.clone<uint8_t> ();
util::pgm::write (grey, "noise.ppm"); util::pgm::write (grey, "noise.ppm");