From f6e2e6a4094bb2f7082dc111405a4242428ecb6c Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 1 Jun 2015 17:21:51 +1000 Subject: [PATCH] n/fractal: use inline headers for fractals --- Makefile.am | 8 +-- noise.hpp | 7 ++- noise/fractal/fbm.cpp | 100 ---------------------------------- noise/fractal/fbm.hpp | 2 + noise/fractal/fbm.ipp | 77 ++++++++++++++++++++++++++ noise/fractal/hetero.cpp | 76 -------------------------- noise/fractal/hetero.hpp | 2 + noise/fractal/hetero.ipp | 64 ++++++++++++++++++++++ noise/fractal/hmf.cpp | 75 -------------------------- noise/fractal/hmf.hpp | 2 + noise/fractal/hmf.ipp | 65 ++++++++++++++++++++++ noise/fractal/rmf.cpp | 113 --------------------------------------- noise/fractal/rmf.hpp | 2 + noise/fractal/rmf.ipp | 96 +++++++++++++++++++++++++++++++++ tools/noise.cpp | 9 ++-- 15 files changed, 322 insertions(+), 376 deletions(-) delete mode 100644 noise/fractal/fbm.cpp create mode 100644 noise/fractal/fbm.ipp delete mode 100644 noise/fractal/hetero.cpp create mode 100644 noise/fractal/hetero.ipp delete mode 100644 noise/fractal/hmf.cpp create mode 100644 noise/fractal/hmf.ipp delete mode 100644 noise/fractal/rmf.cpp create mode 100644 noise/fractal/rmf.ipp diff --git a/Makefile.am b/Makefile.am index 8799738f..a352bd97 100644 --- a/Makefile.am +++ b/Makefile.am @@ -145,14 +145,14 @@ UTIL_FILES = \ noise/basis/perlin.hpp \ noise/basis/worley.cpp \ noise/basis/worley.hpp \ - noise/fractal/fbm.cpp \ noise/fractal/fbm.hpp \ - noise/fractal/hetero.cpp \ + noise/fractal/fbm.ipp \ noise/fractal/hetero.hpp \ - noise/fractal/hmf.cpp \ + noise/fractal/hetero.ipp \ noise/fractal/hmf.hpp \ - noise/fractal/rmf.cpp \ + noise/fractal/hmf.ipp \ noise/fractal/rmf.hpp \ + noise/fractal/rmf.ipp \ noise/lerp.cpp \ noise/lerp.hpp \ noise/lut.cpp \ diff --git a/noise.hpp b/noise.hpp index 00acc245..8f234190 100644 --- a/noise.hpp +++ b/noise.hpp @@ -22,12 +22,11 @@ #include "image.hpp" -namespace util { - namespace noise { +namespace util { namespace noise { template void fill (image::buffer&, const G&); - } -} +} } +#include "noise.ipp" #endif diff --git a/noise/fractal/fbm.cpp b/noise/fractal/fbm.cpp deleted file mode 100644 index 606f4e66..00000000 --- a/noise/fractal/fbm.cpp +++ /dev/null @@ -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 - */ - -#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 -fbm::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 -fbm::fbm (): - fbm (DEFAULT_OCTAVES, - DEFAULT_FREQUENCY, - DEFAULT_LACUNARITY, - DEFAULT_AMPLITUDE, - DEFAULT_GAIN, - rand ()) -{ ; } - - -//----------------------------------------------------------------------------- -template -T -fbm::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>; -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; - -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; -template struct util::noise::fractal::fbm>; - diff --git a/noise/fractal/fbm.hpp b/noise/fractal/fbm.hpp index 1ca93dad..5b2f337c 100644 --- a/noise/fractal/fbm.hpp +++ b/noise/fractal/fbm.hpp @@ -64,4 +64,6 @@ namespace util { namespace noise { namespace fractal { }; } } } +#include "fbm.ipp" + #endif diff --git a/noise/fractal/fbm.ipp b/noise/fractal/fbm.ipp new file mode 100644 index 00000000..474de734 --- /dev/null +++ b/noise/fractal/fbm.ipp @@ -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 + */ + +#ifdef __UTIL_NOISE_FRACTAL_FBM_IPP +#error +#endif +#define __UTIL_NOISE_FRACTAL_FBM_IPP + +#include "../../debug.hpp" + +namespace util { namespace noise { namespace fractal { + /////////////////////////////////////////////////////////////////////////// + template + fbm::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 + fbm::fbm (): + fbm (DEFAULT_OCTAVES, + DEFAULT_FREQUENCY, + DEFAULT_LACUNARITY, + DEFAULT_AMPLITUDE, + DEFAULT_GAIN, + rand ()) + { ; } + + + //------------------------------------------------------------------------- + template + T + fbm::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; + } +} } } diff --git a/noise/fractal/hetero.cpp b/noise/fractal/hetero.cpp deleted file mode 100644 index d0260677..00000000 --- a/noise/fractal/hetero.cpp +++ /dev/null @@ -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 - */ - -#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 -hetero::hetero(): - H (0.75f), - octaves (6), - frequency (0.1f), - lacunarity (2), - offset (0.7f), - amplitude (1), - gain (1) -{ ; } - - -//----------------------------------------------------------------------------- -template -T -hetero::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>; -template struct util::noise::fractal::hetero>; -template struct util::noise::fractal::hetero>; -template struct util::noise::fractal::hetero>; -template struct util::noise::fractal::hetero>; diff --git a/noise/fractal/hetero.hpp b/noise/fractal/hetero.hpp index 01c54d61..d84914d8 100644 --- a/noise/fractal/hetero.hpp +++ b/noise/fractal/hetero.hpp @@ -48,6 +48,8 @@ namespace util { namespace noise { namespace fractal { }; } } } +#include "hetero.ipp" + #endif diff --git a/noise/fractal/hetero.ipp b/noise/fractal/hetero.ipp new file mode 100644 index 00000000..648f0c39 --- /dev/null +++ b/noise/fractal/hetero.ipp @@ -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 + */ + +#ifdef __UTIL_NOISE_FRACTAL_HETERO_IPP +#error +#endif +#define __UTIL_NOISE_FRACTAL_HETERO_IPP + +namespace util { namespace noise { namespace fractal { + //------------------------------------------------------------------------- + template + hetero::hetero(): + H (0.75f), + octaves (6), + frequency (0.1f), + lacunarity (2), + offset (0.7f), + amplitude (1), + gain (1) + { ; } + + + //------------------------------------------------------------------------- + template + T + hetero::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; + } +} } } diff --git a/noise/fractal/hmf.cpp b/noise/fractal/hmf.cpp deleted file mode 100644 index 222d25cb..00000000 --- a/noise/fractal/hmf.cpp +++ /dev/null @@ -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 - */ - -#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 -hmf::hmf (): - H (0.25f), - octaves (6), - frequency (0.1f), - lacunarity (2), - offset (0.7f), - amplitude (1), - gain (1) -{ ; } - - -//----------------------------------------------------------------------------- -template -T -hmf::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>; -template struct util::noise::fractal::hmf>; -template struct util::noise::fractal::hmf>; -template struct util::noise::fractal::hmf>; -template struct util::noise::fractal::hmf>; diff --git a/noise/fractal/hmf.hpp b/noise/fractal/hmf.hpp index 6cbdf39e..06f482ce 100644 --- a/noise/fractal/hmf.hpp +++ b/noise/fractal/hmf.hpp @@ -48,5 +48,7 @@ namespace util { namespace noise { namespace fractal { }; } } } +#include "hmf.ipp" + #endif diff --git a/noise/fractal/hmf.ipp b/noise/fractal/hmf.ipp new file mode 100644 index 00000000..9b3d1219 --- /dev/null +++ b/noise/fractal/hmf.ipp @@ -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 + */ + +#ifdef __UTIL_NOISE_FRACTAL_HMF_IPP +#error +#endif +#define __UTIL_NOISE_FRACTAL_HMF_IPP + + +namespace util { namespace noise { namespace fractal { + //------------------------------------------------------------------------- + template + hmf::hmf (): + H (0.25f), + octaves (6), + frequency (0.1f), + lacunarity (2), + offset (0.7f), + amplitude (1), + gain (1) + { ; } + + + //------------------------------------------------------------------------- + template + T + hmf::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; + } +} } } diff --git a/noise/fractal/rmf.cpp b/noise/fractal/rmf.cpp deleted file mode 100644 index a20c7d1b..00000000 --- a/noise/fractal/rmf.cpp +++ /dev/null @@ -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 - */ - -#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 -rmf::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 -rmf::rmf (): - rmf (DEFAULT_OCTAVES, - DEFAULT_FREQUENCY, - DEFAULT_LACUNARITY, - DEFAULT_AMPLITUDE, - DEFAULT_GAIN, - rand ()) -{ ; } - - -//----------------------------------------------------------------------------- -template -T -rmf::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>; -template struct util::noise::fractal::rmf>; -template struct util::noise::fractal::rmf>; -template struct util::noise::fractal::rmf>; -template struct util::noise::fractal::rmf>; -template struct util::noise::fractal::rmf>; - -template struct util::noise::fractal::rmf>; -template struct util::noise::fractal::rmf>; -template struct util::noise::fractal::rmf>; -template struct util::noise::fractal::rmf>; -template struct util::noise::fractal::rmf>; diff --git a/noise/fractal/rmf.hpp b/noise/fractal/rmf.hpp index 77a1acf8..7c3c6ab7 100644 --- a/noise/fractal/rmf.hpp +++ b/noise/fractal/rmf.hpp @@ -59,4 +59,6 @@ namespace util { namespace noise { namespace fractal { }; } } } +#include "rmf.ipp" + #endif diff --git a/noise/fractal/rmf.ipp b/noise/fractal/rmf.ipp new file mode 100644 index 00000000..b15bccec --- /dev/null +++ b/noise/fractal/rmf.ipp @@ -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 + */ + +#ifdef __UTIL_NOISE_FRACTAL_RMF_IPP +#error +#endif +#define __UTIL_NOISE_FRACTAL_RMF_IPP + + +namespace util { namespace noise { namespace fractal { + /////////////////////////////////////////////////////////////////////////// + template + rmf::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 + rmf::rmf (): + rmf (DEFAULT_OCTAVES, + DEFAULT_FREQUENCY, + DEFAULT_LACUNARITY, + DEFAULT_AMPLITUDE, + DEFAULT_GAIN, + rand ()) + { ; } + + + //------------------------------------------------------------------------- + template + T + rmf::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; + } +} } } diff --git a/tools/noise.cpp b/tools/noise.cpp index 95d0477f..22d7f146 100644 --- a/tools/noise.cpp +++ b/tools/noise.cpp @@ -11,19 +11,20 @@ #include "noise/basis/perlin.hpp" #include "noise/basis/worley.hpp" #include "extent.hpp" - + int main (void) { // setup the output buffer - util::extent2u size (1920, 1080); + util::extent2u size {1920, 1080}; util::image::buffer img (size); // setup the noise generator - util::noise::fractal::fbm> b; + //util::noise::fractal::fbm> b; //util::noise::fractal::rmf> b; //util::noise::fractal::fbm> b; //util::noise::fractal::rmf> b; + util::noise::fractal::hmf> b; //util::noise::fractal::hetero> b; b.octaves = 8; @@ -50,7 +51,7 @@ main (void) std::cerr << "range: [" << *range.first << ", " << *range.second << "]\n"; std::transform (img.begin (), img.end (), img.begin (), [offset,div] (auto i) { return (i - offset) / div; }); - + // write the image to disk auto grey = img.clone (); util::pgm::write (grey, "noise.ppm");