noise: move into seperate repository
This commit is contained in:
parent
c31082716a
commit
46e1ac8e68
53
Makefile.am
53
Makefile.am
@ -197,56 +197,6 @@ UTIL_FILES = \
|
||||
net/types.cpp \
|
||||
net/types.hpp \
|
||||
nocopy.hpp \
|
||||
noise.hpp \
|
||||
noise.ipp \
|
||||
noise/fwd.hpp \
|
||||
noise/basis/constant.cpp \
|
||||
noise/basis/constant.hpp \
|
||||
noise/basis/constant.ipp \
|
||||
noise/basis/gradient/uniform.hpp \
|
||||
noise/basis/gradient/uniform.ipp \
|
||||
noise/basis/gradient/exp.hpp \
|
||||
noise/basis/gradient/exp.ipp \
|
||||
noise/basis/patch.hpp \
|
||||
noise/basis/patch.ipp \
|
||||
noise/basis/perlin.hpp \
|
||||
noise/basis/perlin.ipp \
|
||||
noise/basis/runtime.cpp \
|
||||
noise/basis/runtime.hpp \
|
||||
noise/basis/type/distance.cpp \
|
||||
noise/basis/type/distance.hpp \
|
||||
noise/basis/type/gradient.hpp \
|
||||
noise/basis/type/gradient.cpp \
|
||||
noise/basis/value.hpp \
|
||||
noise/basis/value.ipp \
|
||||
noise/basis/worley.hpp \
|
||||
noise/basis/worley.ipp \
|
||||
noise/fractal/base.hpp \
|
||||
noise/fractal/base.ipp \
|
||||
noise/fractal/fbm.hpp \
|
||||
noise/fractal/fbm.ipp \
|
||||
noise/fractal/hetero.hpp \
|
||||
noise/fractal/hetero.ipp \
|
||||
noise/fractal/hmf.hpp \
|
||||
noise/fractal/hmf.ipp \
|
||||
noise/fractal/rmf.hpp \
|
||||
noise/fractal/rmf.ipp \
|
||||
noise/fractal/runtime.cpp \
|
||||
noise/fractal/runtime.hpp \
|
||||
noise/lerp.cpp \
|
||||
noise/lerp.hpp \
|
||||
noise/lut.cpp \
|
||||
noise/lut.hpp \
|
||||
noise/midpoint.cpp \
|
||||
noise/midpoint.hpp \
|
||||
noise/rand.hpp \
|
||||
noise/rand/hash.hpp \
|
||||
noise/rand/hash.ipp \
|
||||
noise/rand/permute.cpp \
|
||||
noise/rand/permute.hpp \
|
||||
noise/rand/permute.ipp \
|
||||
noise/turbulence.hpp \
|
||||
noise/turbulence.ipp \
|
||||
pascal.cpp \
|
||||
pascal.hpp \
|
||||
platform.hpp \
|
||||
@ -379,8 +329,7 @@ bin_PROGRAMS = \
|
||||
tools/json-clean \
|
||||
tools/json-validate \
|
||||
tools/json-schema \
|
||||
tools/hash \
|
||||
tools/noise
|
||||
tools/hash
|
||||
|
||||
noinst_PROGRAMS = tools/scratch
|
||||
|
||||
|
55
noise.cpp
55
noise.cpp
@ -1,55 +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 2011-2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
|
||||
#include "noise.hpp"
|
||||
|
||||
#include "range.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
void
|
||||
util::noise::fill (image::buffer<1,T> &pixels,
|
||||
const util::noise::fractal<T> &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)});
|
||||
}
|
||||
|
||||
template void util::noise::fill (image::buffer<1,float>&, const util::noise::fractal<float>&);
|
||||
template void util::noise::fill (image::buffer<1,double>&, const util::noise::fractal<double>&);
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void
|
||||
util::noise::image2d (uint8_t *restrict pixels,
|
||||
size_t width,
|
||||
size_t height,
|
||||
const util::noise::fractal<float> &p) {
|
||||
for (size_t y = 0; y < height; ++y)
|
||||
for (size_t x = 0; x < width; ++x) {
|
||||
float v = p ({float(x), float(y)});
|
||||
pixels[x + y * width] = static_cast<uint8_t> (v * std::numeric_limits<uint8_t>::max ());
|
||||
}
|
||||
}
|
32
noise.hpp
32
noise.hpp
@ -1,32 +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 2011-2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_HPP
|
||||
#define __UTIL_NOISE_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "image/buffer.hpp"
|
||||
|
||||
namespace util { namespace noise {
|
||||
template <typename T, typename G>
|
||||
void fill (image::buffer<1,T>&, const G&);
|
||||
} }
|
||||
|
||||
#include "noise.ipp"
|
||||
|
||||
#endif
|
35
noise.ipp
35
noise.ipp
@ -1,35 +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 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<1,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)});
|
||||
}
|
||||
} }
|
@ -1,35 +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 "constant.hpp"
|
||||
|
||||
using util::noise::basis::constant;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T>
|
||||
constant<S,T>::constant (seed_t _seed):
|
||||
seed (_seed),
|
||||
value (42)
|
||||
{ ; }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template struct util::noise::basis::constant<2,float>;
|
||||
template struct util::noise::basis::constant<2,double>;
|
||||
|
||||
template struct util::noise::basis::constant<3,float>;
|
||||
template struct util::noise::basis::constant<3,double>;
|
@ -1,43 +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>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_BASIS_CONSTANT_HPP
|
||||
#define __UTIL_NOISE_BASIS_CONSTANT_HPP
|
||||
|
||||
#include "../rand.hpp"
|
||||
#include "../../point.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace util { namespace noise { namespace basis {
|
||||
template <size_t S, typename T>
|
||||
struct constant {
|
||||
using value_t = T;
|
||||
using point_t = point<S,T>;
|
||||
|
||||
constant (seed_t);
|
||||
|
||||
T operator() (point_t) const noexcept;
|
||||
|
||||
seed_t seed;
|
||||
T value;
|
||||
};
|
||||
} } }
|
||||
|
||||
|
||||
#include "constant.ipp"
|
||||
|
||||
#endif
|
@ -1,29 +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>
|
||||
*/
|
||||
|
||||
#ifdef __UTIL_NOISE_BASIS_CONSTANT_IPP
|
||||
#error
|
||||
#endif
|
||||
#define __UTIL_NOISE_BASIS_CONSTANT_IPP
|
||||
|
||||
namespace util { namespace noise { namespace basis {
|
||||
template <size_t S, typename T>
|
||||
T
|
||||
constant<S,T>::operator() (point_t) const noexcept
|
||||
{
|
||||
return value;
|
||||
}
|
||||
} } }
|
@ -1,57 +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 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __UTIL_NOISE_BASIS_GRADIENT_EXP_HPP
|
||||
#define __UTIL_NOISE_BASIS_GRADIENT_EXP_HPP
|
||||
|
||||
#include "./uniform.hpp"
|
||||
|
||||
#include "../../fwd.hpp"
|
||||
#include "../../../point.hpp"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// modifies a standard uniforma gradient generator to follow an exponential
|
||||
// distribution, base^(-t*exp)
|
||||
//
|
||||
//
|
||||
namespace util { namespace noise { namespace basis { namespace gradient {
|
||||
template <
|
||||
size_t S, // probe point dimensionality
|
||||
typename T // probe point value_type
|
||||
>
|
||||
struct exp : public uniform<S,T> {
|
||||
explicit exp (seed_t seed,
|
||||
T base = (T)1.02,
|
||||
T exponent = T{256});
|
||||
|
||||
T base (void) const;
|
||||
T base (T);
|
||||
|
||||
T exponent (void) const;
|
||||
T exponent (T);
|
||||
|
||||
protected:
|
||||
vector<S,T> generate (pointi<S>) const;
|
||||
|
||||
T m_base;
|
||||
T m_exponent;
|
||||
};
|
||||
} } } }
|
||||
|
||||
#include "exp.ipp"
|
||||
|
||||
#endif
|
@ -1,45 +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>
|
||||
*/
|
||||
|
||||
#ifdef __UTIL_NOISE_BASIS_EXP_IPP
|
||||
#error
|
||||
#endif
|
||||
#define __UTIL_NOISE_BASIS_EXP_IPP
|
||||
|
||||
#include "../../rand.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace util { namespace noise { namespace basis { namespace gradient {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T>
|
||||
exp<S,T>::exp (seed_t _seed, T _base, T _exponent):
|
||||
uniform<S,T> (_seed),
|
||||
m_base (_base),
|
||||
m_exponent (_exponent)
|
||||
{ ; }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T>
|
||||
vector<S,T>
|
||||
exp<S,T>::generate (pointi<S> p) const
|
||||
{
|
||||
auto t = rand::scalar<float> (this->seed (), p);
|
||||
auto factor = std::pow (m_base, -t * m_exponent);
|
||||
return factor * uniform<S,T>::generate (p);
|
||||
}
|
||||
} } } }
|
@ -1,46 +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>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __UTIL_NOISE_BASIS_GRADIENT_UNIFORM_HPP
|
||||
#define __UTIL_NOISE_BASIS_GRADIENT_UNIFORM_HPP
|
||||
|
||||
#include "../../fwd.hpp"
|
||||
#include "../../../point.hpp"
|
||||
|
||||
namespace util { namespace noise { namespace basis { namespace gradient {
|
||||
/// Perlin: interpolated value across each grid space
|
||||
template <
|
||||
size_t S, // probe point dimensionality
|
||||
typename T // probe point value_type
|
||||
>
|
||||
struct uniform {
|
||||
uniform (seed_t);
|
||||
|
||||
seed_t seed (void) const;
|
||||
seed_t seed (seed_t);
|
||||
|
||||
protected:
|
||||
vector<S,T> generate (pointi<S>) const;
|
||||
|
||||
seed_t m_seed;
|
||||
};
|
||||
} } } }
|
||||
|
||||
#include "uniform.ipp"
|
||||
|
||||
#endif
|
||||
|
@ -1,57 +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>
|
||||
*/
|
||||
|
||||
#ifdef __UTIL_NOISE_BASIS_GRADIENT_UNIFORM_IPP
|
||||
#error
|
||||
#endif
|
||||
#define __UTIL_NOISE_BASIS_GRADIENT_UNIFORM_IPP
|
||||
|
||||
#include "../../rand.hpp"
|
||||
|
||||
namespace util { namespace noise { namespace basis { namespace gradient {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T>
|
||||
uniform<S,T>::uniform (seed_t _seed):
|
||||
m_seed (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T>
|
||||
seed_t
|
||||
uniform<S,T>::seed (void) const
|
||||
{
|
||||
return m_seed;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T>
|
||||
seed_t
|
||||
uniform<S,T>::seed (seed_t _seed)
|
||||
{
|
||||
return m_seed = _seed;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T>
|
||||
vector<S,T>
|
||||
uniform<S,T>::generate (pointi<S> p) const
|
||||
{
|
||||
return rand::coord<vector,T> (m_seed, p) * 2 - 1;
|
||||
}
|
||||
} } } }
|
@ -1,51 +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 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_BASIS_PATCH_HPP
|
||||
#define __UTIL_NOISE_BASIS_PATCH_HPP
|
||||
|
||||
#include "./type/distance.hpp"
|
||||
|
||||
#include "../fwd.hpp"
|
||||
#include "../../point.hpp"
|
||||
|
||||
namespace util { namespace noise { namespace basis {
|
||||
template <size_t S, typename T>
|
||||
struct patch : public type::distance<S,2> {
|
||||
patch (seed_t, T width = 0);
|
||||
|
||||
range<T> bounds (void) const;
|
||||
|
||||
T operator() (point<S,T>) const noexcept;
|
||||
|
||||
seed_t seed (void) const;
|
||||
seed_t seed (seed_t);
|
||||
|
||||
T width (void) const;
|
||||
T width (T);
|
||||
|
||||
private:
|
||||
static constexpr T THRESHOLD = 1 - T(0.999);
|
||||
|
||||
T m_width;
|
||||
T m_power;
|
||||
seed_t m_seed;
|
||||
};
|
||||
} } }
|
||||
|
||||
#include "patch.ipp"
|
||||
|
||||
#endif
|
@ -1,153 +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 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#if defined(__UTIL_NOISE_BASIS_PATCH_IPP)
|
||||
#error
|
||||
#endif
|
||||
#define __UTIL_NOISE_BASIS_PATCH_IPP
|
||||
|
||||
#include "../../types.hpp"
|
||||
#include "../../vector.hpp"
|
||||
|
||||
|
||||
namespace util { namespace noise { namespace basis {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T>
|
||||
patch<S,T>::patch (seed_t _seed, T _width):
|
||||
m_width (_width),
|
||||
m_power (exactly_zero (_width)
|
||||
? std::numeric_limits<T>::infinity ()
|
||||
: std::log (THRESHOLD) / std::log (1 - _width)),
|
||||
m_seed (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T>
|
||||
range<T>
|
||||
patch<S,T>::bounds (void) const
|
||||
{
|
||||
return { T{0}, T{1} };
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T>
|
||||
T
|
||||
patch<S,T>::operator () (point<S,T> p) const noexcept
|
||||
{
|
||||
static const size_t COUNT = type::distance<S,2>::OFFSET_SIZE;
|
||||
|
||||
// extract integer and fractional parts. be careful to always round down
|
||||
auto p_int = floor (p).template cast<intmax_t> ();
|
||||
auto p_rem = (p - p_int).template as<point> ();
|
||||
|
||||
// find the distances to each neighbour's centroid.
|
||||
util::point<S,T> centres[COUNT];
|
||||
std::transform (std::begin (this->OFFSETS),
|
||||
std::end (this->OFFSETS),
|
||||
std::begin (centres),
|
||||
[this,p_int] (auto i) { return rand::coord<point,T> (m_seed, p_int + i) + i; });
|
||||
|
||||
T distances[COUNT];
|
||||
std::transform (std::begin (centres),
|
||||
std::end (centres),
|
||||
std::begin (distances),
|
||||
[p_rem] (auto i) { return util::distance (p_rem, i); });
|
||||
|
||||
// sort the distances using indices so we can reuse indices into
|
||||
// 'OFFSETS' to generate the random patch values
|
||||
unsigned indices[COUNT];
|
||||
std::iota (std::begin (indices), std::end (indices), 0);
|
||||
|
||||
std::sort (std::begin (indices),
|
||||
std::end (indices),
|
||||
[&] (auto a, auto b) {
|
||||
return distances[a] < distances[b];
|
||||
});
|
||||
|
||||
// setup normalisation for the distances to the nearest points. the
|
||||
// neighbourhood size is implicitly specified by the 1.5 unit maximum
|
||||
// distance.
|
||||
const size_t hi_off = pow(3,S);
|
||||
|
||||
const auto lo = distances[indices[0 ]];
|
||||
const auto hi = distances[indices[hi_off]];
|
||||
|
||||
T out = 0.f;
|
||||
T sumw = 0.f;
|
||||
|
||||
// sum the weight values of each neighbour. weight by a function of
|
||||
// the distance. we use an power function which allows a known width
|
||||
// to blend.
|
||||
for (size_t i = 0; i < hi_off; ++i)
|
||||
{
|
||||
auto v = rand::scalar<T> (
|
||||
m_seed,
|
||||
p_int + this->OFFSETS[indices[i]]
|
||||
);
|
||||
|
||||
auto d = (distances[indices[i]] - lo) / (hi - lo);
|
||||
auto w = std::pow (1 - d, m_power);
|
||||
|
||||
sumw += w;
|
||||
out += v * w;
|
||||
}
|
||||
|
||||
return out / sumw;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T>
|
||||
seed_t
|
||||
patch<S,T>::seed (void) const
|
||||
{
|
||||
return m_seed;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T>
|
||||
seed_t
|
||||
patch<S,T>::seed (util::noise::seed_t _seed)
|
||||
{
|
||||
return m_seed = _seed;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T>
|
||||
T
|
||||
patch<S,T>::width (void) const
|
||||
{
|
||||
return m_width;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T>
|
||||
T
|
||||
patch<S,T>::width (T _width)
|
||||
{
|
||||
m_width = _width;
|
||||
m_power = exactly_zero (_width)
|
||||
? std::numeric_limits<T>::infinity ()
|
||||
: std::log (THRESHOLD) / std::log (1 - _width);
|
||||
|
||||
return m_width;
|
||||
}
|
||||
} } }
|
@ -1,55 +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>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __UTIL_NOISE_BASIS_PERLIN_HPP
|
||||
#define __UTIL_NOISE_BASIS_PERLIN_HPP
|
||||
|
||||
#include "./gradient/uniform.hpp"
|
||||
#include "./type/gradient.hpp"
|
||||
|
||||
#include "../fwd.hpp"
|
||||
#include "../../point.hpp"
|
||||
#include "../../range.hpp"
|
||||
|
||||
namespace util { namespace noise { namespace basis {
|
||||
/// Perlin: interpolated value across each grid space
|
||||
template <
|
||||
size_t S, // point point dimensionality
|
||||
typename T, // arithmetic and result value_type, must be floating point
|
||||
template < // gradient interpolation function
|
||||
typename
|
||||
> class L,
|
||||
template < // gradient provider class, must provide generate(point_t)
|
||||
size_t,
|
||||
typename
|
||||
> class G = gradient::uniform
|
||||
>
|
||||
struct perlin : public G<S,T>, public type::gradient<S> {
|
||||
using value_t = T;
|
||||
using point_t = point<S,T>;
|
||||
|
||||
perlin (seed_t);
|
||||
|
||||
range<T> bounds (void) const;
|
||||
|
||||
T operator() (point_t) const noexcept;
|
||||
};
|
||||
} } }
|
||||
|
||||
#include "perlin.ipp"
|
||||
|
||||
#endif
|
@ -1,80 +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>
|
||||
*/
|
||||
|
||||
#ifdef __UTIL_NOISE_BASIS_PERLIN_IPP
|
||||
#error
|
||||
#endif
|
||||
#define __UTIL_NOISE_BASIS_PERLIN_IPP
|
||||
|
||||
#include "../../types.hpp"
|
||||
|
||||
|
||||
namespace util { namespace noise { namespace basis {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T, template <typename> class L, template <size_t,typename> class G>
|
||||
perlin<S,T,L,G>::perlin (seed_t _seed):
|
||||
G<S,T> (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T, template <typename> class L, template <size_t,typename> class G>
|
||||
util::range<T>
|
||||
perlin<S,T,L,G>::bounds (void) const
|
||||
{
|
||||
return {
|
||||
-std::sqrt (T{2}) / 2,
|
||||
std::sqrt (T{2}) / 2
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T, template <typename> class L, template <size_t,typename> class G>
|
||||
T
|
||||
perlin<S,T,L,G>::operator() (point_t p) const noexcept
|
||||
{
|
||||
// extract integer and fractional parts. be careful to always round down
|
||||
auto p_int = floor (p).template cast<intmax_t> ();
|
||||
auto p_rem = p - p_int;
|
||||
|
||||
// generate the corner positions
|
||||
pointi<S> p_[pow(2,S)];
|
||||
std::transform (std::begin (this->CORNERS), std::end (this->CORNERS),
|
||||
std::begin (p_),
|
||||
[p_int] (auto i) { return i + p_int; });
|
||||
|
||||
// generate the corner gradients
|
||||
vector<S,T> g_[pow(2,S)];
|
||||
std::transform (std::begin (p_), std::end (p_),
|
||||
std::begin (g_),
|
||||
[this] (auto i) { return this->generate (i); });
|
||||
|
||||
// compute the dot products
|
||||
T v_[pow(2,S)];
|
||||
for (size_t i = 0; i < elems (v_); ++i)
|
||||
v_[i] = dot (g_[i], p - p_[i]);
|
||||
|
||||
// interpolate the results
|
||||
T l_[pow(2,S)];
|
||||
std::copy (std::begin (v_), std::end (v_), std::begin (l_));
|
||||
|
||||
for (size_t i = S; i; --i)
|
||||
for (size_t j = 0; j < std::pow(2,i); j += 2)
|
||||
l_[j / 2] = L<T>() (l_[j], l_[j+1], p_rem[S-i]);
|
||||
return l_[0];
|
||||
}
|
||||
} } }
|
@ -1,20 +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 "runtime.hpp"
|
||||
|
||||
|
||||
template struct util::noise::basis::runtime<2,float>;
|
@ -1,84 +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>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __UTIL_NOISE_BASIS_RUNTIME_HPP
|
||||
#define __UTIL_NOISE_BASIS_RUNTIME_HPP
|
||||
|
||||
#include "../fwd.hpp"
|
||||
#include "../../point.hpp"
|
||||
#include "../../range.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace util { namespace noise { namespace basis {
|
||||
template <size_t S, typename T>
|
||||
struct runtime {
|
||||
public:
|
||||
using value_t = T;
|
||||
using point_t = point<S,T>;
|
||||
|
||||
runtime (seed_t) {}
|
||||
runtime (runtime&&) = default;
|
||||
runtime (const runtime&) = delete;
|
||||
runtime& operator= (const runtime&) = delete;
|
||||
|
||||
// basis functions
|
||||
T operator () (util::point<S,T> p) const noexcept { return (*m_child) (p); }
|
||||
range<T> bounds (void) const { return m_child->bounds (); }
|
||||
|
||||
seed_t seed (void) const { return m_child->seed (); }
|
||||
seed_t seed (seed_t) { return m_child->seed (); }
|
||||
|
||||
private:
|
||||
struct base {
|
||||
virtual ~base () = default;
|
||||
virtual T operator() (util::point<S,T>) const noexcept = 0;
|
||||
virtual range<T> bounds (void) const = 0;
|
||||
virtual seed_t seed (void) const = 0;
|
||||
virtual seed_t seed (seed_t) = 0;
|
||||
};
|
||||
|
||||
template <typename B>
|
||||
struct child : public base {
|
||||
template <typename ...Args>
|
||||
child (seed_t _seed, Args&& ...args): data (_seed, std::forward<Args> (args)...) { }
|
||||
virtual T operator() (util::point<S,T> p) const noexcept override { return data (p); }
|
||||
virtual range<T> bounds (void) const override { return data.bounds (); }
|
||||
virtual seed_t seed (void) const override { return data.seed (); }
|
||||
virtual seed_t seed (seed_t _seed) override { return data.seed (_seed); }
|
||||
|
||||
private:
|
||||
B data;
|
||||
};
|
||||
|
||||
std::unique_ptr<base> m_child;
|
||||
|
||||
public:
|
||||
template <typename B, typename ...Args>
|
||||
void
|
||||
reset (seed_t _seed, Args&& ...args)
|
||||
{
|
||||
using basis_t = B;
|
||||
using child_t = child<basis_t>;
|
||||
|
||||
m_child.reset (new child_t (_seed, std::forward<Args> (args)...));
|
||||
}
|
||||
};
|
||||
} } }
|
||||
|
||||
#endif
|
||||
|
@ -1,51 +0,0 @@
|
||||
#include "./distance.hpp"
|
||||
#include "../../../extent.hpp"
|
||||
#include "../../../cast.hpp"
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, size_t R>
|
||||
static const std::array<
|
||||
util::vectori<S>,
|
||||
util::pow(R*2+1,S)
|
||||
>
|
||||
generate (void)
|
||||
{
|
||||
using value_type = typename util::vectori<S>::value_type;
|
||||
|
||||
static const util::extent_range<
|
||||
S,typename util::vectori<S>::value_type
|
||||
> area (util::extent<S,value_type> {R*2+1});
|
||||
|
||||
std::array<
|
||||
util::vectori<S>,
|
||||
util::pow(R*2+1,S)
|
||||
> out;
|
||||
|
||||
std::transform (area.begin (), area.end (),
|
||||
out.begin (),
|
||||
[] (auto i) {
|
||||
return i.template as<util::vector> () - sign_cast<value_type> (R);
|
||||
});
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, size_t R>
|
||||
const std::array<
|
||||
util::vectori<S>,
|
||||
util::pow(R*2+1,S)
|
||||
>
|
||||
util::noise::basis::type::distance<S,R>::OFFSETS = generate<S,R> ();
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template struct util::noise::basis::type::distance<1,1>;
|
||||
template struct util::noise::basis::type::distance<2,1>;
|
||||
template struct util::noise::basis::type::distance<3,1>;
|
||||
|
||||
template struct util::noise::basis::type::distance<1,2>;
|
||||
template struct util::noise::basis::type::distance<2,2>;
|
||||
template struct util::noise::basis::type::distance<3,2>;
|
@ -1,22 +0,0 @@
|
||||
|
||||
#ifndef __UTIL_NOISE_BASIS_TYPE_HPP
|
||||
#define __UTIL_NOISE_BASIS_TYPE_HPP
|
||||
|
||||
#include "../../../vector.hpp"
|
||||
#include "../../../maths.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace util { namespace noise { namespace basis { namespace type {
|
||||
template <size_t S, size_t R>
|
||||
struct distance {
|
||||
protected:
|
||||
static constexpr size_t OFFSET_SIZE = util::pow(R*2+1,S);
|
||||
|
||||
static const std::array<
|
||||
vectori<S>, util::pow(R*2+1,S)
|
||||
> OFFSETS;
|
||||
};
|
||||
} } } }
|
||||
|
||||
#endif
|
@ -1,44 +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 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "./gradient.hpp"
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S>
|
||||
static std::array<util::vectori<S>,util::pow(2,S)>
|
||||
generate (void)
|
||||
{
|
||||
std::array<util::vectori<S>,util::pow(2,S)> out;
|
||||
|
||||
for (size_t i = 0; i < util::pow(2,S); ++i)
|
||||
for (size_t s = 0; s < S; ++s)
|
||||
out[i][s] = (i >> s) & 1;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S>
|
||||
const std::array<util::vectori<S>,util::pow(2,S)>
|
||||
util::noise::basis::type::gradient<S>::CORNERS = generate<S> ();
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template struct util::noise::basis::type::gradient<1>;
|
||||
template struct util::noise::basis::type::gradient<2>;
|
||||
template struct util::noise::basis::type::gradient<3>;
|
||||
template struct util::noise::basis::type::gradient<4>;
|
@ -1,36 +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 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_BASIS_TYPE_GRADIENT_HPP
|
||||
#define __UTIL_NOISE_BASIS_TYPE_GRADIENT_HPP
|
||||
|
||||
#include "../../../point.hpp"
|
||||
#include "../../../maths.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace util { namespace noise { namespace basis { namespace type {
|
||||
template <size_t S>
|
||||
struct gradient {
|
||||
|
||||
protected:
|
||||
static const std::array<vectori<S>,util::pow(2,S)> CORNERS;
|
||||
};
|
||||
} } } }
|
||||
|
||||
#endif
|
@ -1,56 +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>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __UTIL_NOISE_BASIS_VALUE_HPP
|
||||
#define __UTIL_NOISE_BASIS_VALUE_HPP
|
||||
|
||||
#include "./type/gradient.hpp"
|
||||
|
||||
#include "../fwd.hpp"
|
||||
#include "../../range.hpp"
|
||||
#include "../../point.hpp"
|
||||
|
||||
namespace util { namespace noise { namespace basis {
|
||||
/// Single value per grid space
|
||||
template <
|
||||
size_t S,
|
||||
typename T,
|
||||
template <
|
||||
typename
|
||||
> class L
|
||||
>
|
||||
struct value : public type::gradient<S> {
|
||||
using value_t = T;
|
||||
using point_t = point<S,T>;
|
||||
|
||||
value (seed_t);
|
||||
|
||||
range<T> bounds (void) const;
|
||||
|
||||
value_t operator() (point_t) const noexcept;
|
||||
|
||||
seed_t seed (void) const;
|
||||
seed_t seed (seed_t);
|
||||
|
||||
private:
|
||||
seed_t m_seed;
|
||||
};
|
||||
} } }
|
||||
|
||||
#include "value.ipp"
|
||||
|
||||
#endif
|
@ -1,91 +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>
|
||||
*/
|
||||
|
||||
#ifdef __UTIL_NOISE_BASIS_VALUE_IPP
|
||||
#error
|
||||
#endif
|
||||
#define __UTIL_NOISE_BASIS_VALIE_IPP
|
||||
|
||||
#include "../rand.hpp"
|
||||
#include "../../types.hpp"
|
||||
|
||||
|
||||
namespace util { namespace noise { namespace basis {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T, template <typename> class L>
|
||||
value<S,T,L>::value (seed_t _seed):
|
||||
m_seed (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T, template <typename> class L>
|
||||
util::range<T>
|
||||
value<S,T,L>::bounds (void) const
|
||||
{
|
||||
return { -1, 1 };
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T, template <typename> class L>
|
||||
seed_t
|
||||
value<S,T,L>::seed (void) const
|
||||
{
|
||||
return m_seed;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T, template <typename> class L>
|
||||
seed_t
|
||||
value<S,T,L>::seed (seed_t _seed)
|
||||
{
|
||||
return m_seed = _seed;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T, template <typename> class L>
|
||||
T
|
||||
value<S,T,L>::operator() (util::point<S,T> p) const noexcept
|
||||
{
|
||||
// extract integer and fractional parts. be careful to always round down
|
||||
auto p_int = floor (p).template cast<intmax_t> ();
|
||||
auto p_rem = p - p_int;
|
||||
|
||||
// generate the corner points
|
||||
std::array<pointi<S>,pow(2,S)> p_;
|
||||
std::transform (std::begin (this->CORNERS), std::end (this->CORNERS),
|
||||
std::begin (p_),
|
||||
[p_int] (auto i) { return p_int + i; });
|
||||
|
||||
// Generate the corner values
|
||||
std::array<T,pow(2,S)> g_;
|
||||
std::transform (std::begin (p_), std::end (p_),
|
||||
std::begin (g_),
|
||||
[this] (auto i) { return rand::scalar<value_t> (m_seed, i) * 2 - 1; });
|
||||
|
||||
// Interpolate on one dimension, then the other.
|
||||
T l_[pow(2,S)];
|
||||
std::copy (std::begin (g_), std::end (g_), std::begin (l_));
|
||||
|
||||
for (size_t i = S; i; --i)
|
||||
for (size_t j = 0; j < std::pow(2,i); j += 2)
|
||||
l_[j / 2] = L<T>() (l_[j], l_[j+1], p_rem[S-i]);
|
||||
return l_[0];
|
||||
}
|
||||
} } }
|
@ -1,51 +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>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __UTIL_NOISE_BASIS_WORLEY_HPP
|
||||
#define __UTIL_NOISE_BASIS_WORLEY_HPP
|
||||
|
||||
#include "./type/distance.hpp"
|
||||
|
||||
#include "../fwd.hpp"
|
||||
#include "../../point.hpp"
|
||||
#include "../../range.hpp"
|
||||
|
||||
namespace util { namespace noise { namespace basis {
|
||||
template <size_t S, typename T, size_t F = 0>
|
||||
struct worley : public type::distance<S,1> {
|
||||
using value_t = T;
|
||||
using point_t = point<S,T>;
|
||||
|
||||
worley (seed_t);
|
||||
|
||||
range<T> bounds (void) const;
|
||||
|
||||
value_t operator() (point_t) const noexcept;
|
||||
|
||||
seed_t seed (void) const;
|
||||
seed_t seed (seed_t);
|
||||
|
||||
private:
|
||||
point<S,T> generate (point<S,intmax_t>) const;
|
||||
|
||||
seed_t m_seed;
|
||||
};
|
||||
} } }
|
||||
|
||||
#include "worley.ipp"
|
||||
|
||||
#endif
|
@ -1,96 +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>
|
||||
*/
|
||||
|
||||
#ifdef __UTIL_NOISE_BASIS_WORLEY_IPP
|
||||
#error
|
||||
#endif
|
||||
#define __UTIL_NOISE_BASIS_WORLEY_IPP
|
||||
|
||||
|
||||
#include "../../debug.hpp"
|
||||
|
||||
|
||||
namespace util { namespace noise { namespace basis {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T, size_t F>
|
||||
worley<S,T,F>::worley (seed_t _seed):
|
||||
m_seed (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T, size_t F>
|
||||
util::range<T>
|
||||
worley<S,T,F>::bounds (void) const
|
||||
{
|
||||
return { 0.0, 1.5 };
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T, size_t F>
|
||||
seed_t
|
||||
worley<S,T,F>::seed (void) const
|
||||
{
|
||||
return m_seed;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T, size_t F>
|
||||
seed_t
|
||||
worley<S,T,F>::seed (seed_t _seed)
|
||||
{
|
||||
return m_seed = _seed;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <size_t S, typename T, size_t F>
|
||||
T
|
||||
worley<S,T,F>::operator() (point<S,T> p) const noexcept
|
||||
{
|
||||
// extract integer and fractional parts. be careful to always round down
|
||||
auto p_int = floor (p).template cast<intmax_t> ();
|
||||
auto p_rem = (p - p_int).template as<point> ();
|
||||
|
||||
// setup an array of distances
|
||||
constexpr size_t COUNT = type::distance<S,1>::OFFSET_SIZE;
|
||||
T distances[COUNT];
|
||||
|
||||
std::transform (std::begin (this->OFFSETS), std::end (this->OFFSETS),
|
||||
distances,
|
||||
[p_int,p_rem,this] (auto i) {
|
||||
auto q = this->generate (p_int + i);
|
||||
return distance2 (q + i, p_rem);
|
||||
});
|
||||
|
||||
// find the f'th lowest value
|
||||
static_assert (F < COUNT, "worley order must be less than search radius");
|
||||
std::partial_sort (distances, distances + F, distances + COUNT);
|
||||
CHECK_GE (distances[F], 0);
|
||||
return distances[F];
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T, size_t F>
|
||||
point<S,T>
|
||||
worley<S,T,F>::generate (point<S,intmax_t> p) const
|
||||
{
|
||||
return rand::coord<util::point,T> (m_seed, p);
|
||||
}
|
||||
} } }
|
@ -1,95 +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,
|
||||
* WITHOUvalue_t 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"
|
||||
#include "../rand.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 <class B>
|
||||
struct base {
|
||||
using value_t = typename B::value_t;
|
||||
using point_t = typename B::point_t;
|
||||
|
||||
// constructors
|
||||
base (seed_t,
|
||||
unsigned octaves,
|
||||
value_t H,
|
||||
value_t frequency,
|
||||
value_t lacunarity,
|
||||
value_t amplitude,
|
||||
value_t gain);
|
||||
|
||||
// accessors
|
||||
constexpr unsigned octaves (void) const;
|
||||
unsigned octaves (unsigned);
|
||||
|
||||
constexpr value_t H (void) const;
|
||||
value_t H (value_t);
|
||||
|
||||
constexpr value_t frequency (void) const;
|
||||
value_t frequency (value_t);
|
||||
|
||||
constexpr value_t lacunarity (void) const;
|
||||
value_t lacunarity (value_t);
|
||||
|
||||
constexpr value_t amplitude (void) const;
|
||||
value_t amplitude (value_t);
|
||||
|
||||
constexpr value_t gain (void) const;
|
||||
value_t gain (value_t);
|
||||
|
||||
seed_t seed (void) const;
|
||||
seed_t seed (seed_t);
|
||||
|
||||
const B& basis (void) const;
|
||||
B& basis (void);
|
||||
|
||||
protected:
|
||||
unsigned m_octaves;
|
||||
value_t m_H;
|
||||
|
||||
value_t m_frequency;
|
||||
value_t m_lacunarity;
|
||||
|
||||
value_t m_amplitude;
|
||||
value_t m_gain;
|
||||
|
||||
B m_basis;
|
||||
|
||||
value_t m_invAH;
|
||||
value_t m_invGH;
|
||||
};
|
||||
} } }
|
||||
|
||||
#include "base.ipp"
|
||||
|
||||
#endif
|
||||
|
@ -1,202 +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,
|
||||
* WITHOUvalue_t 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 <class B>
|
||||
base<B>::base (seed_t _seed,
|
||||
unsigned _octaves,
|
||||
value_t _H,
|
||||
value_t _frequency,
|
||||
value_t _lacunarity,
|
||||
value_t _amplitude,
|
||||
value_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_NEZ (m_octaves);
|
||||
CHECK_NEZ (m_frequency);
|
||||
CHECK_NEZ (m_amplitude);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <class B>
|
||||
unsigned
|
||||
base<B>::octaves (unsigned _octaves)
|
||||
{
|
||||
return m_octaves = _octaves;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
constexpr unsigned
|
||||
base<B>::octaves (void) const
|
||||
{
|
||||
return m_octaves;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
typename base<B>::value_t
|
||||
base<B>::H (value_t _h)
|
||||
{
|
||||
m_H = _h;
|
||||
m_invAH = std::pow (m_amplitude, -m_H);
|
||||
m_invGH = std::pow (m_gain, m_H);
|
||||
return m_H;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
constexpr typename base<B>::value_t
|
||||
base<B>::H (void) const
|
||||
{
|
||||
return m_H;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
typename base<B>::value_t
|
||||
base<B>::frequency (value_t _frequency)
|
||||
{
|
||||
return m_frequency = _frequency;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
constexpr typename base<B>::value_t
|
||||
base<B>::frequency (void) const
|
||||
{
|
||||
return m_frequency;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
typename base<B>::value_t
|
||||
base<B>::lacunarity (value_t _lacunarity)
|
||||
{
|
||||
return m_lacunarity = _lacunarity;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
constexpr typename base<B>::value_t
|
||||
base<B>::lacunarity (void) const
|
||||
{
|
||||
return m_lacunarity;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
constexpr typename base<B>::value_t
|
||||
base<B>::amplitude (void) const
|
||||
{
|
||||
return m_amplitude;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
typename base<B>::value_t
|
||||
base<B>::amplitude (value_t _amplitude)
|
||||
{
|
||||
m_amplitude = _amplitude;
|
||||
m_invAH = std::pow (m_amplitude, -m_H);
|
||||
return m_amplitude;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
constexpr typename base<B>::value_t
|
||||
base<B>::gain (void) const
|
||||
{
|
||||
return m_gain;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
typename base<B>::value_t
|
||||
base<B>::gain (value_t _gain)
|
||||
{
|
||||
m_gain = _gain;
|
||||
m_invGH = std::pow (_gain, m_H);
|
||||
return m_gain;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
seed_t
|
||||
base<B>::seed (seed_t _seed)
|
||||
{
|
||||
return m_basis.seed (_seed);
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
seed_t
|
||||
base<B>::seed (void) const
|
||||
{
|
||||
return m_basis.seed ();
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
const B&
|
||||
base<B>::basis (void) const
|
||||
{
|
||||
return m_basis;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
B&
|
||||
base<B>::basis (void)
|
||||
{
|
||||
return m_basis;
|
||||
}
|
||||
} } }
|
@ -1,63 +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,
|
||||
* WITHOUvalue_t 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 "base.hpp"
|
||||
#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 <class B>
|
||||
struct fbm : public base<B> {
|
||||
using value_t = typename base<B>::value_t;
|
||||
using point_t = typename base<B>::point_t;
|
||||
|
||||
static constexpr unsigned DEFAULT_OCTAVES = 8;
|
||||
static constexpr value_t DEFAULT_H = 1;
|
||||
static constexpr value_t DEFAULT_FREQUENCY = value_t(0.1);
|
||||
static constexpr value_t DEFAULT_LACUNARITY = 2;
|
||||
static constexpr value_t DEFAULT_AMPLITUDE = 1;
|
||||
static constexpr value_t DEFAULT_GAIN = 1 / DEFAULT_LACUNARITY;
|
||||
|
||||
fbm (seed_t seed,
|
||||
unsigned octaves,
|
||||
value_t H,
|
||||
value_t frequency,
|
||||
value_t lacunarity,
|
||||
value_t amplitude,
|
||||
value_t gain);
|
||||
explicit fbm (seed_t = 0);
|
||||
|
||||
value_t operator() (point_t) const noexcept;
|
||||
};
|
||||
} } }
|
||||
|
||||
#include "fbm.ipp"
|
||||
|
||||
#endif
|
@ -1,77 +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,
|
||||
* WITHOUvalue_t 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 <class B>
|
||||
fbm<B>::fbm (seed_t _seed,
|
||||
unsigned _octaves,
|
||||
value_t _H,
|
||||
value_t _frequency,
|
||||
value_t _lacunarity,
|
||||
value_t _amplitude,
|
||||
value_t _gain):
|
||||
base<B> (_seed,
|
||||
_octaves,
|
||||
_H,
|
||||
_frequency,
|
||||
_lacunarity,
|
||||
_amplitude,
|
||||
_gain)
|
||||
{ ; }
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
fbm<B>::fbm (seed_t _seed):
|
||||
fbm<B> (_seed,
|
||||
DEFAULT_OCTAVES,
|
||||
DEFAULT_H,
|
||||
DEFAULT_FREQUENCY,
|
||||
DEFAULT_LACUNARITY,
|
||||
DEFAULT_AMPLITUDE,
|
||||
DEFAULT_GAIN)
|
||||
{ ; }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <class B>
|
||||
typename fbm<B>::value_t
|
||||
fbm<B>::operator() (point_t p) const noexcept
|
||||
{
|
||||
value_t total = 0;
|
||||
value_t scale = this->m_invAH;
|
||||
|
||||
p *= this->m_frequency;
|
||||
|
||||
for (size_t i = 0; i < this->m_octaves; ++i) {
|
||||
total += this->m_basis (p) * scale;
|
||||
|
||||
p += PI<value_t>;
|
||||
p *= this->m_lacunarity;
|
||||
scale *= this->m_invGH;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
} } }
|
@ -1,74 +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,
|
||||
* WITHOUvalue_t 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 "base.hpp"
|
||||
#include "../../point.hpp"
|
||||
|
||||
namespace util { namespace noise { namespace fractal {
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
/// Heterogeneous procedural terrain fucntion: stats by altitude method
|
||||
template <class B>
|
||||
struct hetero : public base<B> {
|
||||
using value_t = typename base<B>::value_t;
|
||||
using point_t = typename base<B>::point_t;
|
||||
|
||||
static constexpr unsigned DEFAULT_OCTAVES = 6;
|
||||
static constexpr value_t DEFAULT_H = value_t(0.75);
|
||||
static constexpr value_t DEFAULT_FREQUENCY = value_t(0.1);
|
||||
static constexpr value_t DEFAULT_LACUNARITY = 2;
|
||||
static constexpr value_t DEFAULT_AMPLITUDE = 1;
|
||||
static constexpr value_t DEFAULT_GAIN = 1 / DEFAULT_LACUNARITY;
|
||||
static constexpr value_t DEFAULT_OFFSET = value_t(0.7);
|
||||
|
||||
hetero (seed_t,
|
||||
unsigned octaves,
|
||||
value_t H,
|
||||
value_t frequency,
|
||||
value_t lacunarity,
|
||||
value_t amplitude,
|
||||
value_t gain);
|
||||
|
||||
hetero (seed_t,
|
||||
unsigned octaves,
|
||||
value_t H,
|
||||
value_t frequency,
|
||||
value_t lacunarity,
|
||||
value_t amplitude,
|
||||
value_t gain,
|
||||
value_t offset);
|
||||
|
||||
explicit hetero (seed_t = 0);
|
||||
|
||||
constexpr value_t offset (void) const;
|
||||
value_t offset (value_t);
|
||||
|
||||
value_t operator() (point_t) const;
|
||||
|
||||
private:
|
||||
value_t m_offset;
|
||||
};
|
||||
} } }
|
||||
|
||||
#include "hetero.ipp"
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -1,124 +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,
|
||||
* WITHOUvalue_t 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 <class B>
|
||||
hetero<B>::hetero(seed_t _seed,
|
||||
unsigned _octaves,
|
||||
value_t _H,
|
||||
value_t _frequency,
|
||||
value_t _lacunarity,
|
||||
value_t _amplitude,
|
||||
value_t _gain):
|
||||
hetero<B> (_seed,
|
||||
_octaves,
|
||||
_H,
|
||||
_frequency,
|
||||
_lacunarity,
|
||||
_amplitude,
|
||||
_gain,
|
||||
-this->basis ().bounds ().min + this->basis ().bounds ().magnitude () / value_t{2})
|
||||
{ ; }
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
hetero<B>::hetero(seed_t _seed,
|
||||
unsigned _octaves,
|
||||
value_t _H,
|
||||
value_t _frequency,
|
||||
value_t _lacunarity,
|
||||
value_t _amplitude,
|
||||
value_t _gain,
|
||||
value_t _offset):
|
||||
base<B> (_seed,
|
||||
_octaves,
|
||||
_H,
|
||||
_frequency,
|
||||
_lacunarity,
|
||||
_amplitude,
|
||||
_gain),
|
||||
m_offset (_offset)
|
||||
{ ; }
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
hetero<B>::hetero (seed_t _seed):
|
||||
hetero<B> (_seed,
|
||||
DEFAULT_OCTAVES,
|
||||
DEFAULT_H,
|
||||
DEFAULT_FREQUENCY,
|
||||
DEFAULT_LACUNARITY,
|
||||
DEFAULT_AMPLITUDE,
|
||||
DEFAULT_GAIN,
|
||||
DEFAULT_OFFSET)
|
||||
{ ; }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <class B>
|
||||
constexpr typename hetero<B>::value_t
|
||||
hetero<B>::offset (void) const
|
||||
{
|
||||
return m_offset;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
typename hetero<B>::value_t
|
||||
hetero<B>::offset (value_t _offset)
|
||||
{
|
||||
return m_offset = _offset;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <class B>
|
||||
typename hetero<B>::value_t
|
||||
hetero<B>::operator() (point_t p) const
|
||||
{
|
||||
value_t scale = this->m_invAH;
|
||||
|
||||
p *= this->m_frequency;
|
||||
value_t result = (this->m_basis (p) + m_offset) * scale;
|
||||
p *= this->m_lacunarity;
|
||||
|
||||
value_t increment = 0;
|
||||
|
||||
for (size_t i = 1; i < this->m_octaves; ++i) {
|
||||
scale *= this->m_invGH;
|
||||
|
||||
increment = this->m_basis (p) + m_offset;
|
||||
increment *= scale;
|
||||
increment *= result;
|
||||
|
||||
result += increment;
|
||||
|
||||
p += PI<value_t>;
|
||||
p *= this->m_lacunarity;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
} } }
|
@ -1,62 +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>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_FRACTAL_HMF_HPP
|
||||
#define __UTIL_NOISE_FRACTAL_HMF_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "../../point.hpp"
|
||||
|
||||
namespace util { namespace noise { namespace fractal {
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
/// Musgrave's "Hybrid MultiFractal"
|
||||
template <class B>
|
||||
struct hmf : public base<B> {
|
||||
using value_t = typename base<B>::value_t;
|
||||
using point_t = typename base<B>::point_t;
|
||||
|
||||
// H should be fairly low due to the decreasing weight parameter in eval
|
||||
static constexpr unsigned DEFAULT_OCTAVES = 6;
|
||||
static constexpr value_t DEFAULT_H = value_t(0.25);
|
||||
static constexpr value_t DEFAULT_FREQUENCY = value_t(0.1);
|
||||
static constexpr value_t DEFAULT_LACUNARITY = 2;
|
||||
static constexpr value_t DEFAULT_AMPLITUDE = 1;
|
||||
static constexpr value_t DEFAULT_GAIN = 1 / DEFAULT_LACUNARITY;
|
||||
static constexpr value_t DEFAULT_OFFSET = value_t(0.7);
|
||||
|
||||
hmf (seed_t,
|
||||
unsigned octaves,
|
||||
value_t H,
|
||||
value_t frequency,
|
||||
value_t lacunarity,
|
||||
value_t amplitude,
|
||||
value_t gain,
|
||||
value_t offset);
|
||||
|
||||
explicit hmf (seed_t = 0);
|
||||
|
||||
value_t operator() (point_t) const;
|
||||
|
||||
private:
|
||||
value_t m_offset;
|
||||
};
|
||||
} } }
|
||||
|
||||
#include "hmf.ipp"
|
||||
|
||||
#endif
|
||||
|
@ -1,86 +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>
|
||||
*/
|
||||
|
||||
#ifdef __UTIL_NOISE_FRACTAL_HMF_IPP
|
||||
#error
|
||||
#endif
|
||||
#define __UTIL_NOISE_FRACTAL_HMF_IPP
|
||||
|
||||
|
||||
namespace util { namespace noise { namespace fractal {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <class B>
|
||||
hmf<B>::hmf (seed_t _seed,
|
||||
unsigned _octaves,
|
||||
value_t _H,
|
||||
value_t _frequency,
|
||||
value_t _lacunarity,
|
||||
value_t _amplitude,
|
||||
value_t _gain,
|
||||
value_t _offset):
|
||||
base<B> (_seed,
|
||||
_octaves,
|
||||
_H,
|
||||
_frequency,
|
||||
_lacunarity,
|
||||
_amplitude,
|
||||
_gain),
|
||||
m_offset (_offset)
|
||||
{ ; }
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
hmf<B>::hmf (seed_t _seed):
|
||||
hmf<B> (_seed,
|
||||
DEFAULT_OCTAVES,
|
||||
DEFAULT_H,
|
||||
DEFAULT_FREQUENCY,
|
||||
DEFAULT_LACUNARITY,
|
||||
DEFAULT_AMPLITUDE,
|
||||
DEFAULT_GAIN,
|
||||
DEFAULT_OFFSET)
|
||||
{ ; }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <class B>
|
||||
typename hmf<B>::value_t
|
||||
hmf<B>::operator() (point_t p) const
|
||||
{
|
||||
value_t scale = this->m_invAH;
|
||||
|
||||
value_t result = 0;
|
||||
value_t signal = 0;
|
||||
value_t weight = 1;
|
||||
|
||||
p *= this->m_frequency;
|
||||
|
||||
for (size_t i = 0; i < this->m_octaves; ++i) {
|
||||
signal = (this->m_basis (p) + m_offset) * scale;
|
||||
result += signal * weight;
|
||||
|
||||
weight *= signal;
|
||||
weight = min (weight, value_t{1});
|
||||
|
||||
scale *= this->m_invGH;
|
||||
p += PI<value_t>;
|
||||
p *= this->m_lacunarity;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
} } }
|
@ -1,71 +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>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_FRACTAL_RMF_HPP
|
||||
#define __UTIL_NOISE_FRACTAL_RMF_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "base.hpp"
|
||||
#include "../../point.hpp"
|
||||
|
||||
namespace util { namespace noise { namespace fractal {
|
||||
/// Rigid Multifractal summation, based on Musgrave's algorithm
|
||||
///
|
||||
/// octaves: count of layers to be summed
|
||||
/// H: hurst parameter (~roughness)
|
||||
/// offset: TODO
|
||||
/// frequency: point scaling factor for the base octave
|
||||
/// lacunarity: incremental octave frequency scaling factor
|
||||
/// amplitude: value scaling factor for the base octave
|
||||
/// gain: incremental octave value scaling factor
|
||||
template <class B>
|
||||
struct rmf : public base<B> {
|
||||
using value_t = typename base<B>::value_t;
|
||||
using point_t = typename base<B>::point_t;
|
||||
|
||||
static constexpr unsigned DEFAULT_OCTAVES = 5;
|
||||
static constexpr value_t DEFAULT_H = 1;
|
||||
static constexpr value_t DEFAULT_OFFSET = 1;
|
||||
static constexpr value_t DEFAULT_FREQUENCY = 1;
|
||||
static constexpr value_t DEFAULT_LACUNARITY = 2;
|
||||
static constexpr value_t DEFAULT_AMPLITUDE = 2;
|
||||
static constexpr value_t DEFAULT_GAIN = 1 / DEFAULT_LACUNARITY;
|
||||
|
||||
rmf (seed_t,
|
||||
unsigned octaves,
|
||||
value_t H,
|
||||
value_t frequency,
|
||||
value_t lacunarity,
|
||||
value_t amplitude,
|
||||
value_t gain,
|
||||
value_t offset);
|
||||
|
||||
explicit rmf (seed_t = 0);
|
||||
|
||||
value_t operator() (point_t) const;
|
||||
|
||||
value_t offset (void) const;
|
||||
value_t offset (value_t);
|
||||
|
||||
private:
|
||||
value_t m_offset;
|
||||
};
|
||||
} } }
|
||||
|
||||
#include "rmf.ipp"
|
||||
|
||||
#endif
|
@ -1,119 +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>
|
||||
*/
|
||||
|
||||
#ifdef __UTIL_NOISE_FRACTAL_RMF_IPP
|
||||
#error
|
||||
#endif
|
||||
#define __UTIL_NOISE_FRACTAL_RMF_IPP
|
||||
|
||||
|
||||
namespace util { namespace noise { namespace fractal {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <class B>
|
||||
rmf<B>::rmf (seed_t _seed,
|
||||
unsigned _octaves,
|
||||
value_t _H,
|
||||
value_t _offset,
|
||||
value_t _frequency,
|
||||
value_t _lacunarity,
|
||||
value_t _amplitude,
|
||||
value_t _gain):
|
||||
base<B> (_seed,
|
||||
_octaves,
|
||||
_H,
|
||||
_frequency,
|
||||
_lacunarity,
|
||||
_amplitude,
|
||||
_gain),
|
||||
m_offset (_offset)
|
||||
{ ; }
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
rmf<B>::rmf (seed_t _seed):
|
||||
rmf<B> (_seed,
|
||||
DEFAULT_OCTAVES,
|
||||
DEFAULT_H,
|
||||
DEFAULT_FREQUENCY,
|
||||
DEFAULT_LACUNARITY,
|
||||
DEFAULT_AMPLITUDE,
|
||||
DEFAULT_GAIN,
|
||||
DEFAULT_OFFSET)
|
||||
{ ; }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// we use the name 'amplitude' instead of musgrave's 'gain'.
|
||||
// assumes basis distribution [-1,1] and offset ~= 1
|
||||
template <class B>
|
||||
typename rmf<B>::value_t
|
||||
rmf<B>::operator() (point_t p) const
|
||||
{
|
||||
value_t scale = this->m_invAH;
|
||||
|
||||
value_t signal = 0;
|
||||
value_t result = 0;
|
||||
value_t weight = 1;
|
||||
|
||||
p *= this->m_frequency;
|
||||
|
||||
for (size_t i = 0; i < this->m_octaves; ++i) {
|
||||
// generates ridged noise
|
||||
signal = this->m_basis (p);
|
||||
signal = std::fabs (signal);
|
||||
signal = m_offset - signal;
|
||||
|
||||
// sharpens the ridges
|
||||
signal *= signal;
|
||||
|
||||
// influence by sharpness of previous iteration
|
||||
signal *= weight;
|
||||
|
||||
// contribute to the weight
|
||||
weight = signal * this->m_amplitude;
|
||||
weight = limit (weight, 0, 1);
|
||||
|
||||
// record and continue
|
||||
result += signal * scale;
|
||||
|
||||
scale *= this->m_invGH;
|
||||
|
||||
p += PI<value_t>;
|
||||
p *= this->m_lacunarity;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <class B>
|
||||
typename rmf<B>::value_t
|
||||
rmf<B>::offset (void) const
|
||||
{
|
||||
return m_offset;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <class B>
|
||||
typename rmf<B>::value_t
|
||||
rmf<B>::offset (value_t _offset)
|
||||
{
|
||||
return m_offset = _offset;
|
||||
}
|
||||
} } }
|
@ -1,22 +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 "runtime.hpp"
|
||||
#include "../basis/runtime.hpp"
|
||||
|
||||
template struct util::noise::fractal::runtime<
|
||||
util::noise::basis::runtime<2,float>
|
||||
>;
|
@ -1,164 +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>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __UTIL_NOISE_FRACTAL_RUNTIME_HPP
|
||||
#define __UTIL_NOISE_FRACTAL_RUNTIME_HPP
|
||||
|
||||
#include "base.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace util { namespace noise { namespace fractal {
|
||||
template <class B>
|
||||
struct runtime {
|
||||
public:
|
||||
using value_t = typename B::value_t;
|
||||
using point_t = typename B::point_t;
|
||||
|
||||
runtime (seed_t) { }
|
||||
runtime () = default;
|
||||
runtime (runtime&&) = default;
|
||||
runtime (const runtime&) = delete;
|
||||
runtime& operator= (const runtime&) = delete;
|
||||
|
||||
// basis functions
|
||||
value_t operator () (point_t p) const noexcept { return (*m_child) (p); }
|
||||
|
||||
unsigned octaves (void) const { return m_child->octaves (); }
|
||||
unsigned octaves (unsigned _octaves) { return m_child->octaves (_octaves); }
|
||||
|
||||
value_t H (void) const { return m_child->H (); }
|
||||
value_t H (value_t _H) { return m_child->H (_H); }
|
||||
|
||||
value_t frequency (void) const { return m_child->frequency (); }
|
||||
value_t frequency (value_t _frequency) { return m_child->frequency (_frequency); }
|
||||
|
||||
value_t lacunarity (void) const { return m_child->lacunarity (); }
|
||||
value_t lacunarity (value_t _lacunarity) { return m_child->lacunarity (_lacunarity); }
|
||||
|
||||
value_t amplitude (void) const { return m_child->amplitude (); }
|
||||
value_t amplitude (value_t _amplitude) { return m_child->amplitude (_amplitude); }
|
||||
|
||||
value_t gain (void) const { return m_child->gain (); }
|
||||
value_t gain (value_t _gain) { return m_child->gain (_gain); }
|
||||
|
||||
B& basis (void) { return m_child->basis (); }
|
||||
const B& basis (void) const { return m_child->basis (); }
|
||||
|
||||
seed_t seed (void) const { return m_child->seed (); }
|
||||
seed_t seed (seed_t) { return m_child->seed (); }
|
||||
|
||||
private:
|
||||
struct base {
|
||||
virtual ~base () = default;
|
||||
|
||||
virtual value_t operator() (point_t) const noexcept = 0;
|
||||
|
||||
virtual unsigned octaves (void) const = 0;
|
||||
virtual unsigned octaves (unsigned) = 0;
|
||||
|
||||
virtual value_t H (void) const = 0;
|
||||
virtual value_t H (value_t) = 0;
|
||||
|
||||
virtual value_t frequency (void) const = 0;
|
||||
virtual value_t frequency (value_t) = 0;
|
||||
|
||||
virtual value_t lacunarity (void) const = 0;
|
||||
virtual value_t lacunarity (value_t) = 0;
|
||||
|
||||
virtual value_t amplitude (void) const = 0;
|
||||
virtual value_t amplitude (value_t) = 0;
|
||||
|
||||
virtual value_t gain (void) const = 0;
|
||||
virtual value_t gain (value_t) = 0;
|
||||
|
||||
virtual B& basis (void) = 0;
|
||||
virtual const B& basis (void) const = 0;
|
||||
|
||||
virtual seed_t seed (void) const = 0;
|
||||
virtual seed_t seed (seed_t) = 0;
|
||||
};
|
||||
|
||||
template <typename F>
|
||||
struct child final : public base {
|
||||
child (seed_t _seed):
|
||||
data (_seed)
|
||||
{ ; }
|
||||
|
||||
child (seed_t _seed,
|
||||
unsigned _octaves,
|
||||
value_t _H,
|
||||
value_t _frequency,
|
||||
value_t _lacunarity,
|
||||
value_t _amplitude,
|
||||
value_t _gain):
|
||||
data (_seed,
|
||||
_octaves,
|
||||
_H,
|
||||
_frequency,
|
||||
_lacunarity,
|
||||
_amplitude,
|
||||
_gain)
|
||||
{ ; }
|
||||
|
||||
value_t operator() (point_t p) const noexcept override { return data (p); }
|
||||
|
||||
unsigned octaves (void) const override { return data.octaves (); }
|
||||
unsigned octaves (unsigned _octaves) override { return data.octaves (_octaves); }
|
||||
|
||||
value_t H (void) const override { return data.H (); }
|
||||
value_t H (value_t _H) override { return data.H (_H); }
|
||||
|
||||
value_t frequency (void) const override { return data.frequency (); }
|
||||
value_t frequency (value_t _frequency) override { return data.frequency (_frequency); }
|
||||
|
||||
value_t lacunarity (void) const override { return data.lacunarity (); }
|
||||
value_t lacunarity (value_t _lacunarity) override { return data.lacunarity (_lacunarity); }
|
||||
|
||||
value_t amplitude (void) const override { return data.amplitude (); }
|
||||
value_t amplitude (value_t _amplitude) override { return data.amplitude (_amplitude); }
|
||||
|
||||
value_t gain (void) const override { return data.gain (); }
|
||||
value_t gain (value_t _gain) override { return data.gain (_gain); }
|
||||
|
||||
B& basis (void) override { return data.basis (); }
|
||||
const B& basis (void) const override { return data.basis (); }
|
||||
|
||||
seed_t seed (void) const override { return data.seed (); }
|
||||
seed_t seed (seed_t _seed) override { return data.seed (_seed); }
|
||||
|
||||
F data;
|
||||
};
|
||||
|
||||
std::unique_ptr<base> m_child;
|
||||
|
||||
public:
|
||||
template <typename F>
|
||||
F&
|
||||
reset (seed_t _seed)
|
||||
{
|
||||
using fractal_t = F;
|
||||
using child_t = child<fractal_t>;
|
||||
|
||||
child_t *out;
|
||||
m_child.reset (out= new child_t (_seed));
|
||||
return out->data;
|
||||
}
|
||||
};
|
||||
} } }
|
||||
|
||||
#endif
|
@ -1,27 +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>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_FWD_HPP
|
||||
#define __UTIL_NOISE_FWD_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace util { namespace noise {
|
||||
template <typename T> using lerp_t = T (*)(T,T,T);
|
||||
typedef uint64_t seed_t;
|
||||
} }
|
||||
|
||||
#endif
|
114
noise/lerp.cpp
114
noise/lerp.cpp
@ -1,114 +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 2011-2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "lerp.hpp"
|
||||
|
||||
#include "debug.hpp"
|
||||
#include "maths.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
using util::lerp::linear;
|
||||
using util::lerp::cosine;
|
||||
using util::lerp::cubic;
|
||||
using util::lerp::quintic;
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T
|
||||
util::lerp::truncate<T>::operator() (T a, T, T weight) noexcept
|
||||
{
|
||||
static_assert (std::is_floating_point<T>::value,
|
||||
"lerp is only defined for floating types");
|
||||
|
||||
CHECK_LIMIT (weight, 0, 1);
|
||||
(void)weight;
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
template struct util::lerp::truncate<float>;
|
||||
template struct util::lerp::truncate<double>;
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T
|
||||
linear<T>::operator() (T a, T b, T weight) noexcept
|
||||
{
|
||||
static_assert (std::is_floating_point<T>::value,
|
||||
"lerp is only defined for floating types");
|
||||
CHECK_LIMIT (weight, 0, 1);
|
||||
|
||||
return a * (1 - weight) + b * weight;
|
||||
}
|
||||
|
||||
template struct util::lerp::linear<float>;
|
||||
template struct util::lerp::linear<double>;
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T
|
||||
cosine<T>::operator() (T a, T b, T weight) noexcept
|
||||
{
|
||||
static_assert (std::is_floating_point<T>::value,
|
||||
"lerp is only defined for floating types");
|
||||
CHECK_LIMIT (weight, 0, 1);
|
||||
|
||||
T t = (1 - std::cos (weight * PI<T>)) * T(0.5);
|
||||
return a * (1 - t) + b * t;
|
||||
}
|
||||
|
||||
template struct util::lerp::cosine<float>;
|
||||
template struct util::lerp::cosine<double>;
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T
|
||||
cubic<T>::operator() (T a, T b, T weight) noexcept
|
||||
{
|
||||
static_assert (std::is_floating_point<T>::value,
|
||||
"lerp is only defined for floating types");
|
||||
CHECK_LIMIT (weight, 0, 1);
|
||||
|
||||
// -2w^3 * 3w^2
|
||||
T t = weight * weight * (3 - 2 * weight);
|
||||
return a * (1 - t) + b * t;
|
||||
}
|
||||
|
||||
template struct util::lerp::cubic<float>;
|
||||
template struct util::lerp::cubic<double>;
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T
|
||||
quintic<T>::operator() (T a, T b, T weight) noexcept
|
||||
{
|
||||
static_assert (std::is_floating_point<T>::value,
|
||||
"lerp is only defined for floating types");
|
||||
CHECK_LIMIT (weight, 0, 1);
|
||||
|
||||
// from perlin's improved noise: 6w^5 -15w^4 +10w^3
|
||||
T t = weight * weight * weight * (weight * (weight * 6 - 15) + 10);
|
||||
return a * (1 - t) + b * t;
|
||||
}
|
||||
|
||||
template struct util::lerp::quintic<float>;
|
||||
template struct util::lerp::quintic<double>;
|
@ -1,28 +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 2011-2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_LERP_HPP
|
||||
#define __UTIL_NOISE_LERP_HPP
|
||||
|
||||
namespace util { namespace lerp {
|
||||
template <typename T> struct linear { T operator() (T, T, T weight) noexcept; };
|
||||
template <typename T> struct cosine { T operator() (T, T, T weight) noexcept; };
|
||||
template <typename T> struct cubic { T operator() (T, T, T weight) noexcept; };
|
||||
template <typename T> struct quintic { T operator() (T, T, T weight) noexcept; };
|
||||
template <typename T> struct truncate { T operator() (T, T, T weight) noexcept; };
|
||||
} }
|
||||
|
||||
#endif
|
120
noise/lut.cpp
120
noise/lut.cpp
@ -1,120 +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 "lut.hpp"
|
||||
|
||||
const size_t util::noise::PERM[256] = {
|
||||
114, 247, 195, 115, 164, 64, 53, 51,
|
||||
82, 75, 244, 19, 255, 1, 43, 77,
|
||||
126, 141, 77, 23, 169, 7, 29, 72,
|
||||
92, 97, 69, 211, 218, 71, 47, 25,
|
||||
178, 10, 135, 139, 152, 38, 51, 90,
|
||||
206, 98, 130, 32, 192, 6, 2, 34,
|
||||
74, 50, 162, 111, 188, 7, 24, 27,
|
||||
22, 87, 49, 67, 202, 35, 22, 79,
|
||||
253, 197, 73, 94, 182, 39, 37, 51,
|
||||
121, 85, 17, 18, 109, 63, 48, 66,
|
||||
173, 245, 183, 201, 68, 21, 81, 70,
|
||||
200, 0, 191, 86, 52, 80, 29, 76,
|
||||
15, 180, 41, 58, 128, 2, 38, 33,
|
||||
132, 119, 60, 242, 6, 46, 7, 99,
|
||||
237, 108, 30, 93, 153, 12, 43, 16,
|
||||
163, 209, 219, 117, 83, 13, 26, 14,
|
||||
55, 159, 190, 81, 224, 78, 8, 76,
|
||||
118, 203, 8, 193, 248, 84, 13, 48,
|
||||
240, 38, 65, 227, 160, 9, 36, 36,
|
||||
122, 185, 33, 123, 158, 56, 47, 88,
|
||||
246, 215, 231, 131, 5, 24, 4, 25,
|
||||
230, 174, 189, 168, 241, 56, 31, 20,
|
||||
166, 186, 184, 155, 29, 42, 25, 0,
|
||||
35, 105, 221, 39, 216, 96, 28, 23,
|
||||
28, 61, 196, 101, 149, 13, 65, 16,
|
||||
175, 194, 44, 34, 187, 95, 99, 20,
|
||||
45, 112, 252, 11, 59, 26, 12, 17,
|
||||
171, 14, 70, 54, 142, 4, 67, 46,
|
||||
4, 127, 20, 133, 205, 54, 43, 3,
|
||||
110, 249, 172, 161, 140, 57, 40, 89,
|
||||
3, 150, 210, 232, 57, 45, 44, 54,
|
||||
91, 79, 250, 62, 37, 98, 36, 34,
|
||||
};
|
||||
|
||||
|
||||
const double util::noise::LUT[256] = {
|
||||
-0.8984982139196056, 0.4750710231804418, -0.6519676730721629, 0.34818237268881935,
|
||||
-0.18205970986171605, 0.3451314956848195, 0.6414228668402755, -0.804052019344714,
|
||||
0.6838765435328942, 0.04489123586160382, 0.9387123107757223, -0.8286734231526627,
|
||||
-0.5752448478162355, -0.7177473684716851, 0.9935836223400552, 0.7280624253259176,
|
||||
0.17859563727209138, -0.03606344150253982, -0.15882231678491388, 0.4578698195539206,
|
||||
-0.4154225972126202, -0.5194028373429245, 0.24561279025507043, -0.8817931383760225,
|
||||
0.051621615744299465, -0.432105334458762, 0.1565615530978024, 0.6597775199873575,
|
||||
0.22566777177285857, -0.30483300012361036, -0.6958206141316641, -0.2930693331180243,
|
||||
-0.7690503602769119, -0.7171818210252867, 0.8800261811087657, 0.8601395971416754,
|
||||
0.5784335763333026, 0.060900655607116994, 0.2837112260220951, -0.08799217109739876,
|
||||
-0.2384174709999518, -0.770742115936714, 0.9217986500940929, 0.33319037162928433,
|
||||
0.2338527159073147, 0.9326260912305022, -0.3277100775267676, -0.6923414380512052,
|
||||
0.2743829582530313, 0.021903777340825714, -0.99594589251304, 0.08310122202568193,
|
||||
0.031989194381878194, 0.322274671882117, 0.15855440115773267, 0.7221472232498565,
|
||||
-0.5369740081438523, -0.013966337693399833, -0.3797229432191469, -0.1449216443374528,
|
||||
0.4780175549902941, 0.3253792901693007, 0.8302471035699828, -0.19705446486920075,
|
||||
-0.05145564508096312, 0.07804387053425987, -0.43766576462644347, -0.6841198355261591,
|
||||
0.7137706240638149, -0.05484055085953887, -0.8299960004843008, 0.18050577055894035,
|
||||
-0.8541390034524383, -0.26341201926055424, 0.7023808019022437, 0.6230332593788754,
|
||||
-0.2959807962046559, 0.8971411582261257, -0.37285148660593714, -0.9689413803647513,
|
||||
0.36271555405510414, -0.2369795401687047, -0.057686227499222476, 0.45924926449705117,
|
||||
0.5909634897508582, -0.8641968475739907, -0.9868274716835026, 0.15367421531182823,
|
||||
0.47868338795465704, 0.22205133716275216, 0.9342712841005696, 0.574755268557622,
|
||||
-0.30781440722062237, 0.08940264916288165, -0.7364035085406784, -0.42804724032315544,
|
||||
-0.3028896080136707, 0.08043452763728154, 0.35597931924492854, 0.8218045295996388,
|
||||
-0.2639539020450863, 0.14018881766976743, -0.9679978866175825, 0.917534812674309,
|
||||
0.8315532029453319, -0.26657576198722377, 0.5292911314793278, 0.05106195480414244,
|
||||
-0.20703582048726044, 0.9363562930545231, 0.07180492189859389, -0.9307494388376307,
|
||||
0.48401747690392316, -0.47666025737165385, -0.2995451643535043, 0.9645238474628381,
|
||||
-0.9869506025182451, -0.6267193760632264, 0.9417871950627361, -0.3712682888227079,
|
||||
0.17116236816682484, 0.1637861802304843, 0.5979849303287379, -0.7943555560703199,
|
||||
-0.8605593297161265, 0.24271029593235993, 0.8838806440116482, 0.6563769551024692,
|
||||
0.9357703879808301, -0.76818344297623, 0.7661179391954425, 0.426530438580041,
|
||||
-0.7280736750184795, 0.5258880971264928, -0.3736324615581794, -0.3726200928816683,
|
||||
-0.652698944621871, 0.1312826210242899, 0.022999070967927526, 0.8362192026388207,
|
||||
-0.20524579974666657, -0.39989736807434273, 0.097666397673847, 0.25215779272486416,
|
||||
-0.6800982672863105, 0.8013507802109068, 0.969941009294419, -0.2760313084806676,
|
||||
0.8961286070268608, 0.9455740223217473, -0.3908254141167051, 0.8195441725353367,
|
||||
-0.571435969794009, 0.18987091511038434, -0.04181589713692069, -0.963609489444007,
|
||||
-0.5281459088656999, 0.984266295584705, 0.5027560371807003, -0.1597738785923013,
|
||||
0.3411131481434917, 0.12429799530598684, -0.5159001480993852, 0.8865722840594523,
|
||||
0.32969200413777755, 0.26288894768315707, -0.054978081511632526, -0.37725903459638177,
|
||||
0.18451775548204163, -0.6691455193869638, 0.9285268978526233, -0.49526622636914563,
|
||||
-0.12647685137552322, -0.08438978664878105, -0.4263493078967011, 0.04134592579064478,
|
||||
0.5682513712371484, 0.3144579798890128, -0.5560826395168046, -0.7651643315076799,
|
||||
-0.4919051361482554, 0.4326849315222068, 0.9745607803884215, 0.5459754840343867,
|
||||
-0.3314574932052319, 0.10102331343981996, -0.8762141515646111, -0.8182536989519216,
|
||||
0.5460251598764976, 0.11958795888450968, -0.7226482729866581, -0.5362654501182944,
|
||||
0.9461689136255056, 0.7302075935427872, 0.2067222384707743, -0.5515294144965857,
|
||||
0.5261119777013636, -0.6058524684579083, -0.7984088236296627, 0.5515069318003252,
|
||||
0.6842126925238694, 0.9653367596972298, -0.4209547631571078, -0.200564767100335,
|
||||
-0.45471383032946067, -0.5407480745656619, 0.7853038244196648, 0.08741550867662506,
|
||||
0.46188948442462063, 0.32857850460858384, 0.2519455394025394, -0.5647397901411311,
|
||||
-0.8010538532270075, 0.03511555124942123, 0.8126644809142252, -0.6165437893290324,
|
||||
-0.769144309230668, 0.5811756737759881, 0.04347549540355833, -0.1516485473331033,
|
||||
-0.8025604153817143, 0.2078203269944925, -0.8281743083984969, -0.5483796887270644,
|
||||
-0.67075976237068, 0.08046291156170216, -0.726116057042915, 0.6064692851498683,
|
||||
-0.2326853771750068, -0.1199137086641211, -0.007902895438233637, -0.024018850623878762,
|
||||
0.23518281506286076, 0.8739589142046267, 0.9461972684232018, -0.6298769685383707,
|
||||
-0.37298229425868623, -0.0443367550367304, -0.9688376819157003, 0.09999263734937558,
|
||||
-0.3975824040014859, 0.3353645267506893, -0.2693845909559329, -0.1133673242389015,
|
||||
-0.8453467042866694, 0.5313565920953589, -0.5313028936659494, 0.23266080060836347,
|
||||
0.19579971665018436, 0.9392643282850877, 0.08508300565389537, -0.43964224026992116,
|
||||
0.5862798492854766, 0.7001806971895554, 0.9050597336377173, -0.5789943930802939,
|
||||
};
|
@ -1,45 +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_LUT_HPP
|
||||
#define __UTIL_NOISE_LUT_HPP
|
||||
|
||||
#include <cstdlib>
|
||||
#include <type_traits>
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
namespace util {
|
||||
namespace noise {
|
||||
extern const size_t PERM[256];
|
||||
extern const double LUT [256];
|
||||
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_integral<T>::value, size_t>::type
|
||||
permute (T idx) {
|
||||
return PERM[(size_t)idx % elems (PERM)];
|
||||
}
|
||||
|
||||
template <typename T1, typename T2, typename ...R>
|
||||
size_t permute (T1 t1, T2 t2, R ...r) {
|
||||
auto p = permute (t1);
|
||||
p += permute (t2, r...);
|
||||
return p % elems (PERM);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -1,121 +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 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "midpoint.hpp"
|
||||
|
||||
#include "region.hpp"
|
||||
#include "point.hpp"
|
||||
#include "rand.hpp"
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// assumes corner points have been assigned their weights
|
||||
template <typename T>
|
||||
static void
|
||||
fill (util::image::buffer<1,T> &img,
|
||||
uint64_t seed,
|
||||
util::region2u target,
|
||||
float scale,
|
||||
float persistence,
|
||||
float sides)
|
||||
{
|
||||
CHECK_EQ (target.e.w % 2, 1u);
|
||||
CHECK_EQ (target.e.h % 2, 1u);
|
||||
CHECK_GE (target.area (), 9u);
|
||||
|
||||
CHECK_GT (scale, 0);
|
||||
CHECK_GT (persistence, 0);
|
||||
CHECK_GE (sides, 0);
|
||||
|
||||
const auto w = target.w;
|
||||
const auto h = target.h;
|
||||
|
||||
// 0--1
|
||||
// | |
|
||||
// 2--3
|
||||
|
||||
const auto p0 = target.p + util::vector2u {0, 0 };
|
||||
const auto p1 = target.p + util::vector2u {w-1, 0 };
|
||||
const auto p2 = target.p + util::vector2u {0, h-1};
|
||||
const auto p3 = target.p + util::vector2u {w-1, h-1};
|
||||
|
||||
const auto v0 = img[p0];
|
||||
const auto v1 = img[p1];
|
||||
const auto v2 = img[p2];
|
||||
const auto v3 = img[p3];
|
||||
|
||||
// do the centre
|
||||
{
|
||||
const auto avg = (v0 + v1 + v2 + v3) / 4;
|
||||
const auto val = avg + scale * util::noise::rand::scalar<T> (seed, target.centre ());
|
||||
const auto pos = target.p + target.e / 2;
|
||||
|
||||
img[pos] = val * 2 - 1;
|
||||
}
|
||||
|
||||
// average the sides
|
||||
{
|
||||
const auto p01 = target.p + util::vector2u{w/2, 0};
|
||||
const auto p13 = target.p + util::vector2u{w-1, h/2};
|
||||
const auto p32 = target.p + util::vector2u{w/2, h-1};
|
||||
const auto p20 = target.p + util::vector2u{0, h/2};
|
||||
|
||||
const auto v01 = (v0 + v1) / 2 + sides * scale * util::noise::rand::scalar<T> (seed, p01);
|
||||
const auto v13 = (v1 + v3) / 2 + sides * scale * util::noise::rand::scalar<T> (seed, p13);
|
||||
const auto v32 = (v3 + v2) / 2 + sides * scale * util::noise::rand::scalar<T> (seed, p32);
|
||||
const auto v20 = (v2 + v0) / 2 + sides * scale * util::noise::rand::scalar<T> (seed, p20);
|
||||
|
||||
img[p01] = v01 * 2 - 1;
|
||||
img[p13] = v13 * 2 - 1;
|
||||
img[p32] = v32 * 2 - 1;
|
||||
img[p20] = v20 * 2 - 1;
|
||||
}
|
||||
|
||||
// recurse
|
||||
if (target.area () > 9) {
|
||||
auto e = target.e / 2 + 1;
|
||||
|
||||
fill (img, seed, {target.p + util::vector2u{ 0, 0 }, e}, scale * persistence, persistence, sides);
|
||||
fill (img, seed, {target.p + util::vector2u{ w/2, 0 }, e}, scale * persistence, persistence, sides);
|
||||
fill (img, seed, {target.p + util::vector2u{ 0, h/2 }, e}, scale * persistence, persistence, sides);
|
||||
fill (img, seed, {target.p + util::vector2u{ w/2, h/2 }, e}, scale * persistence, persistence, sides);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
void
|
||||
util::noise::midpoint (image::buffer<1,T> &img, uint64_t seed, float persistence, float sides)
|
||||
{
|
||||
auto ext = img.extent ();
|
||||
|
||||
static const util::point2u CORNERS[] = {
|
||||
{ 0, 0 },
|
||||
{ 0, ext.w - 1 },
|
||||
{ ext.h - 1, 0 },
|
||||
{ ext.h - 1, ext.w - 1 }
|
||||
};
|
||||
|
||||
for (auto i: CORNERS)
|
||||
img[i] = util::noise::rand::scalar<T> (seed, i) * 2 - 1;
|
||||
|
||||
fill (img, seed, { { 0, 0 }, img.extent () }, 1.f, persistence, sides);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template void util::noise::midpoint (image::buffer<1,float>&, uint64_t, float, float);
|
@ -1,28 +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 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_MIDPOINT_HPP
|
||||
#define __UTIL_NOISE_MIDPOINT_HPP
|
||||
|
||||
#include "../image/buffer.hpp"
|
||||
|
||||
namespace util { namespace noise {
|
||||
template <typename T>
|
||||
void
|
||||
midpoint (image::buffer<1,T>&, uint64_t seed, float persistence = 0.65f, float sides = 0.25f);
|
||||
} }
|
||||
|
||||
#endif
|
@ -1,66 +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 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_RAND_HPP
|
||||
#define __UTIL_NOISE_RAND_HPP
|
||||
|
||||
#include "./rand/permute.hpp"
|
||||
#include "./rand/hash.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace util { namespace noise {
|
||||
using seed_t = uint64_t;
|
||||
} }
|
||||
|
||||
namespace util { namespace noise { namespace rand {
|
||||
/// generate a uniform random floating point in the range [0, 1] from a seed and vector
|
||||
template <
|
||||
typename U,
|
||||
size_t S,
|
||||
typename T,
|
||||
template <size_t,typename> class Q
|
||||
>
|
||||
U
|
||||
scalar (uint64_t seed, Q<S,T> value) noexcept
|
||||
{
|
||||
#if 1
|
||||
return permute::scalar<U> (seed, value);
|
||||
#else
|
||||
return hash::scalar<U> (seed, value);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// generate a coordinate type with uniform random components in the range [0, 1]
|
||||
template <
|
||||
template <size_t,typename> class R,
|
||||
typename U,
|
||||
size_t S,
|
||||
typename T,
|
||||
template <size_t,typename> class Q
|
||||
>
|
||||
R<S,U>
|
||||
coord (uint64_t seed, Q<S,T> value) noexcept
|
||||
{
|
||||
#if 1
|
||||
return permute::coord<R,U,S,T,Q> (seed, value);
|
||||
#else
|
||||
return hash::coord<R,U,S,T,Q> (seed, value);
|
||||
#endif
|
||||
}
|
||||
} } }
|
||||
|
||||
#endif
|
@ -1,49 +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 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __UTIL_NOISE_RAND_HASH_HPP
|
||||
#define __UTIL_NOISE_RAND_HASH_HPP
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace util { namespace noise { namespace rand {
|
||||
struct hash {
|
||||
/// generate a uniform random floating point in the range [0, 1] from a seed and vector
|
||||
template <
|
||||
typename U,
|
||||
size_t S,
|
||||
typename T,
|
||||
template <size_t,typename> class Q
|
||||
>
|
||||
static U
|
||||
scalar (uint64_t seed, Q<S,T> value) noexcept;
|
||||
|
||||
/// generate a coordinate type with uniform random components in the range [0, 1]
|
||||
template <
|
||||
template <size_t,typename> class R,
|
||||
typename U,
|
||||
size_t S,
|
||||
typename T,
|
||||
template <size_t,typename> class Q
|
||||
>
|
||||
static R<S,U>
|
||||
coord (uint64_t seed, Q<S,T> value) noexcept;
|
||||
};
|
||||
} } }
|
||||
|
||||
#include "./hash.hpp"
|
||||
|
||||
#endif
|
@ -1,74 +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 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
|
||||
#ifdef __UTIL_NOISE_RAND_HASH_IPP
|
||||
#error
|
||||
#endif
|
||||
|
||||
#define __UTIL_NOISE_RAND_HASH_IPP
|
||||
|
||||
namespace util { namespace noise { namespace rand {
|
||||
//-------------------------------------------------------------------------
|
||||
template <
|
||||
typename U,
|
||||
size_t S,
|
||||
typename T,
|
||||
template <size_t,typename> class Q
|
||||
>
|
||||
U
|
||||
hash::scalar (uint64_t seed, Q<S,T> query) noexcept
|
||||
{
|
||||
constexpr decltype(seed) BITS = 0xFFFF;
|
||||
static_assert (std::is_integral<T>::value,
|
||||
"mixing only works on integral types");
|
||||
|
||||
uint64_t accum = seed;
|
||||
for (auto i: query)
|
||||
accum = util::hash::murmur2::mix (accum, i);
|
||||
|
||||
return (accum & BITS) / U{BITS};
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <
|
||||
template <size_t,typename> class R,
|
||||
typename U,
|
||||
size_t S,
|
||||
typename T,
|
||||
template <size_t,typename> class Q
|
||||
>
|
||||
R<S,U>
|
||||
hash::coord (uint64_t seed, Q<S,T> query) noexcept
|
||||
{
|
||||
constexpr decltype(seed) PERTURB = 0x96c39996c36c36c3;
|
||||
constexpr decltype(seed) BITS = 0xFFFF;
|
||||
|
||||
auto accum = seed ^ PERTURB;
|
||||
for (auto i: query)
|
||||
accum = util::hash::murmur2::mix (accum, i);
|
||||
|
||||
R<S,U> result;
|
||||
for (auto &i: result) {
|
||||
i = (accum & BITS);
|
||||
i /= BITS;
|
||||
accum = util::hash::murmur2::mix (accum, seed);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
} } }
|
@ -1,25 +0,0 @@
|
||||
#include "./permute.hpp"
|
||||
|
||||
|
||||
using util::noise::rand::permute;
|
||||
|
||||
// static permutation offsets. consists of numbers [0,255] in a randomised
|
||||
// pattern. the exact arrangement is irrelevant.
|
||||
const std::array<uint8_t,256> permute::PERMUTE = {
|
||||
148, 94, 173, 74, 199, 189, 102, 184, 149, 18, 177, 211, 16, 221, 252, 232,
|
||||
82, 104, 164, 196, 78, 4, 247, 11, 206, 176, 137, 45, 30, 118, 72, 59,
|
||||
39, 198, 158, 220, 213, 230, 197, 209, 145, 172, 188, 10, 214, 144, 241, 242,
|
||||
84, 128, 168, 229, 64, 32, 20, 249, 236, 194, 253, 183, 132, 6, 43, 96,
|
||||
181, 27, 127, 106, 63, 21, 134, 120, 170, 166, 93, 191, 70, 240, 46, 85,
|
||||
23, 56, 228, 28, 160, 67, 175, 161, 36, 61, 204, 71, 140, 103, 190, 66,
|
||||
100, 130, 192, 251, 174, 201, 79, 169, 231, 53, 48, 50, 119, 178, 87, 205,
|
||||
243, 165, 207, 13, 116, 235, 24, 224, 47, 81, 195, 218, 97, 83, 238, 227,
|
||||
113, 180, 33, 111, 126, 200, 153, 5, 146, 112, 179, 255, 250, 19, 92, 80,
|
||||
135, 222, 95, 22, 142, 40, 15, 107, 217, 41, 77, 110, 210, 246, 185, 54,
|
||||
163, 105, 114, 193, 215, 73, 150, 129, 208, 155, 171, 154, 115, 0, 8, 233,
|
||||
162, 138, 109, 157, 248, 26, 25, 244, 31, 62, 75, 35, 29, 60, 182, 156,
|
||||
55, 223, 125, 51, 76, 88, 9, 151, 117, 122, 38, 159, 124, 49, 44, 254,
|
||||
202, 147, 225, 219, 90, 234, 133, 212, 14, 7, 167, 2, 91, 68, 187, 216,
|
||||
65, 42, 69, 245, 123, 152, 89, 52, 37, 121, 143, 1, 57, 226, 3, 239,
|
||||
99, 98, 108, 186, 17, 139, 203, 58, 86, 131, 141, 136, 34, 101, 12, 237
|
||||
};
|
@ -1,55 +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 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __UTIL_NOISE_RAND_PERMUTE_HPP
|
||||
#define __UTIL_NOISE_RAND_PERMUTE_HPP
|
||||
|
||||
#include <array>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace util { namespace noise { namespace rand {
|
||||
struct permute {
|
||||
/// generate a uniform random floating point in the range [0, 1] from a seed and vector
|
||||
template <
|
||||
typename U,
|
||||
size_t S,
|
||||
typename T,
|
||||
template <size_t,typename> class Q
|
||||
>
|
||||
static U
|
||||
scalar (uint64_t seed, Q<S,T> value) noexcept;
|
||||
|
||||
/// generate a coordinate type with uniform random components in the range [0, 1]
|
||||
template <
|
||||
template <size_t,typename> class R,
|
||||
typename U,
|
||||
size_t S,
|
||||
typename T,
|
||||
template <size_t,typename> class Q
|
||||
>
|
||||
static R<S,U>
|
||||
coord (uint64_t seed, Q<S,T> value) noexcept;
|
||||
|
||||
private:
|
||||
static const std::array<uint8_t, 256> PERMUTE;
|
||||
};
|
||||
} } }
|
||||
|
||||
#include "./permute.ipp"
|
||||
|
||||
#endif
|
||||
|
@ -1,47 +0,0 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace util { namespace noise { namespace rand {
|
||||
template <
|
||||
typename U,
|
||||
size_t S,
|
||||
typename T,
|
||||
template <size_t,typename> class Q
|
||||
>
|
||||
U
|
||||
permute::scalar (uint64_t seed, Q<S,T> query) noexcept
|
||||
{
|
||||
|
||||
size_t idx = PERMUTE[seed&0xff];
|
||||
for (auto i: query)
|
||||
idx = PERMUTE[(idx+i)&0xff];
|
||||
return PERMUTE[idx] / U(0xff);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <
|
||||
template <size_t,typename> class R,
|
||||
typename U,
|
||||
size_t S,
|
||||
typename T,
|
||||
template <size_t,typename> class Q
|
||||
>
|
||||
R<S,U>
|
||||
permute::coord (uint64_t seed, Q<S,T> query) noexcept
|
||||
{
|
||||
auto accum = seed;
|
||||
for (auto q: query) {
|
||||
size_t idx = q + accum;
|
||||
accum = PERMUTE[idx & 0xff];
|
||||
}
|
||||
|
||||
R<S,U> res;
|
||||
for (size_t i = 0; i < S; ++i) {
|
||||
res[i] = PERMUTE[accum];
|
||||
res[i] /= 0xff;
|
||||
|
||||
accum = PERMUTE[(accum + i) & 0xff];
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
} } }
|
@ -1,68 +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>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_TURBULENCE_HPP
|
||||
#define __UTIL_NOISE_TURBULENCE_HPP
|
||||
|
||||
#include "../point.hpp"
|
||||
#include "../vector.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace util { namespace noise {
|
||||
/// perturb the evaluation point of a noise function by the results of
|
||||
/// a per-dimension noise function
|
||||
///
|
||||
/// assumes the pertubation function is roughly symetrical around 0.
|
||||
/// nothing will explode if it isn't, but you'll see strong directional
|
||||
/// artefacts with higher scaling factors.
|
||||
template <
|
||||
typename D, // data fractal
|
||||
typename P // pertubation fractal
|
||||
>
|
||||
struct turbulence {
|
||||
using value_t = typename D::value_t;
|
||||
using point_t = typename D::point_t;
|
||||
using scale_t = vector<point_t::dimension,value_t>;
|
||||
|
||||
static constexpr size_t S = D::point_t::dimension;
|
||||
|
||||
static_assert (std::is_same<typename D::value_t, typename P::value_t>::value,
|
||||
"data and perturbation value types must match");
|
||||
static_assert (std::is_same<typename D::point_t, typename P::point_t>::value,
|
||||
"data and perturbation point types must match");
|
||||
|
||||
turbulence (seed_t, scale_t);
|
||||
|
||||
seed_t seed (seed_t);
|
||||
seed_t seed (void) const;
|
||||
|
||||
constexpr value_t operator() (point_t) const;
|
||||
|
||||
D data;
|
||||
|
||||
// XXX: use a union to defer initialization of pertubation fractals in
|
||||
// the constructor. i know this is horrible, but there's no time to
|
||||
// write the proper generator constructor to pass out the seeds.
|
||||
P perturb[S];
|
||||
|
||||
scale_t scale;
|
||||
};
|
||||
} }
|
||||
|
||||
#include "turbulence.ipp"
|
||||
|
||||
#endif
|
@ -1,73 +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>
|
||||
*/
|
||||
|
||||
#ifdef __UTIL_NOISE_TURBULENCE_IPP
|
||||
#error
|
||||
#endif
|
||||
#define __UTIL_NOISE_TURBULENCE_IPP
|
||||
|
||||
#include "../hash/wang.hpp"
|
||||
|
||||
|
||||
namespace util { namespace noise {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <typename D, typename P>
|
||||
turbulence<D,P>::turbulence (seed_t _seed,
|
||||
scale_t _scale):
|
||||
data (_seed),
|
||||
scale (_scale)
|
||||
{
|
||||
for (auto &p: perturb)
|
||||
p.seed (_seed = hash::wang (_seed));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
template <typename D, typename P>
|
||||
seed_t
|
||||
turbulence<D,P>::seed (void) const
|
||||
{
|
||||
return data.seed ();
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <typename D, typename P>
|
||||
seed_t
|
||||
turbulence<D,P>::seed (seed_t _seed)
|
||||
{
|
||||
auto ret = _seed;
|
||||
data.seed (_seed);
|
||||
|
||||
for (size_t i = 0; i < S; ++i)
|
||||
perturb[i].seed (_seed = hash::wang (_seed));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <typename D, typename P>
|
||||
constexpr typename turbulence<D,P>::value_t
|
||||
turbulence<D,P>::operator() (point_t p) const
|
||||
{
|
||||
scale_t n;
|
||||
for (size_t i = 0; i < S; ++i)
|
||||
n[i] = perturb[i] (p);
|
||||
|
||||
// scale by the data frequency so that we match scale
|
||||
return data (p + n * scale / data.frequency ());
|
||||
}
|
||||
} }
|
Loading…
Reference in New Issue
Block a user