libcruft-util/noise/basis/patch.ipp

155 lines
4.7 KiB
Plaintext
Raw Normal View History

2015-07-24 01:37:36 +10:00
/*
* 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
2015-07-28 18:14:10 +10:00
#include "../../types.hpp"
#include "../../ray.hpp"
#include "../../vector.hpp"
namespace util { namespace noise { namespace basis {
///////////////////////////////////////////////////////////////////////////
2015-10-06 15:45:26 +11:00
template <size_t S, typename T>
patch<S,T>::patch (seed_t _seed, T _width):
2015-07-28 18:14:10 +10:00
m_width (_width),
m_power (exactly_zero (_width)
? std::numeric_limits<T>::infinity ()
: std::log (THRESHOLD) / std::log (1 - _width)),
m_seed (_seed)
{ ; }
///////////////////////////////////////////////////////////////////////////
2015-10-06 15:45:26 +11:00
template <size_t S, typename T>
2015-07-28 18:14:10 +10:00
range<T>
2015-10-06 15:45:26 +11:00
patch<S,T>::bounds (void) const
2015-07-28 18:14:10 +10:00
{
return { T{0}, T{1} };
}
///////////////////////////////////////////////////////////////////////////
2015-10-06 15:45:26 +11:00
template <size_t S, typename T>
2015-07-28 18:14:10 +10:00
T
patch<S,T>::operator () (point<S,T> p) const noexcept
2015-07-28 18:14:10 +10:00
{
2015-10-06 15:45:26 +11:00
static const size_t COUNT = type::distance<S,2>::OFFSET_SIZE;
2015-07-28 18:14:10 +10:00
// extract integer and fractional parts. be careful to always round down
auto p_int = floor (p).template cast<intmax_t> ();
2015-07-28 18:14:10 +10:00
auto p_rem = (p - p_int).template as<point> ();
2015-10-06 15:45:26 +11:00
// 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; });
2015-07-28 18:14:10 +10:00
T distances[COUNT];
2015-10-06 15:45:26 +11:00
std::transform (std::begin (centres),
std::end (centres),
std::begin (distances),
[p_rem] (auto i) { return util::distance (p_rem, i); });
2015-07-28 18:14:10 +10:00
2015-10-06 15:45:26 +11:00
// sort the distances using indices so we can reuse indices into
// 'OFFSETS' to generate the random patch values
2015-07-28 18:14:10 +10:00
unsigned indices[COUNT];
std::iota (std::begin (indices), std::end (indices), 0);
std::sort (std::begin (indices),
2015-10-06 15:45:26 +11:00
std::end (indices),
2015-07-28 18:14:10 +10:00
[&] (auto a, auto b) {
return distances[a] < distances[b];
});
2015-10-06 15:45:26 +11:00
// setup normalisation for the distances to the nearest points. the
2015-07-28 18:14:10 +10:00
// neighbourhood size is implicitly specified by the 1.5 unit maximum
// distance.
2015-10-06 15:45:26 +11:00
const size_t hi_off = pow(3,S);
const auto lo = distances[indices[0 ]];
const auto hi = distances[indices[hi_off]];
2015-07-28 18:14:10 +10:00
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.
2015-10-06 15:45:26 +11:00
for (size_t i = 0; i < hi_off; ++i)
2015-07-28 18:14:10 +10:00
{
auto v = rand::scalar<T> (
2015-10-06 15:45:26 +11:00
m_seed,
p_int + this->OFFSETS[indices[i]]
);
2015-07-28 18:14:10 +10:00
auto d = (distances[indices[i]] - lo) / (hi - lo);
2015-10-06 15:45:26 +11:00
auto w = std::pow (1 - d, m_power);
2015-07-28 18:14:10 +10:00
sumw += w;
out += v * w;
2015-07-24 01:37:36 +10:00
}
2015-07-28 18:14:10 +10:00
return out / sumw;
}
///////////////////////////////////////////////////////////////////////////
2015-10-06 15:45:26 +11:00
template <size_t S, typename T>
2015-07-28 18:14:10 +10:00
seed_t
2015-10-06 15:45:26 +11:00
patch<S,T>::seed (void) const
2015-07-28 18:14:10 +10:00
{
return m_seed;
}
//-------------------------------------------------------------------------
2015-10-06 15:45:26 +11:00
template <size_t S, typename T>
2015-07-28 18:14:10 +10:00
seed_t
2015-10-06 15:45:26 +11:00
patch<S,T>::seed (util::noise::seed_t _seed)
2015-07-28 18:14:10 +10:00
{
return m_seed = _seed;
}
///////////////////////////////////////////////////////////////////////////
2015-10-06 15:45:26 +11:00
template <size_t S, typename T>
2015-07-28 18:14:10 +10:00
T
2015-10-06 15:45:26 +11:00
patch<S,T>::width (void) const
2015-07-28 18:14:10 +10:00
{
return m_width;
}
//-------------------------------------------------------------------------
2015-10-06 15:45:26 +11:00
template <size_t S, typename T>
2015-07-28 18:14:10 +10:00
T
2015-10-06 15:45:26 +11:00
patch<S,T>::width (T _width)
2015-07-28 18:14:10 +10:00
{
m_width = _width;
m_power = exactly_zero (_width)
? std::numeric_limits<T>::infinity ()
: std::log (THRESHOLD) / std::log (1 - _width);
return m_width;
}
} } }