n/b/worley: performance improvements
This commit is contained in:
parent
9b548e56d9
commit
19def9d4fe
@ -23,92 +23,84 @@ using util::noise::basis::worley;
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
template <typename T>
|
template <typename T>
|
||||||
util::vector<2,T>
|
util::point<2,T>
|
||||||
generate (util::point<2,T> p, uint64_t seed)
|
generate (util::point<2,intmax_t> p, uint64_t seed)
|
||||||
{
|
{
|
||||||
using util::hash::murmur2::mix;
|
using util::hash::murmur2::mix;
|
||||||
|
|
||||||
auto u = mix (seed, mix (uint64_t (p.x), uint64_t (p.y)));
|
auto u = mix (seed, mix (uint64_t (p.x), uint64_t (p.y)));
|
||||||
auto v = mix (u, seed);
|
auto v = mix (u, seed);
|
||||||
|
|
||||||
auto r = util::vector<2,T> {
|
auto r = util::point<2,T> {
|
||||||
(u & 0xffff) / T{0xffff},
|
(u & 0xffff) / T{0xffff},
|
||||||
(v & 0xffff) / T{0xffff}
|
(v & 0xffff) / T{0xffff}
|
||||||
} * 2 - 1;
|
};
|
||||||
|
|
||||||
CHECK_GE (r, T{-1});
|
|
||||||
CHECK_LE (r, T{ 1});
|
|
||||||
|
|
||||||
|
CHECK_LIMIT (r, T{0}, T{1});
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
template <typename T>
|
template <typename T, size_t F>
|
||||||
worley<T>::worley (seed_t _seed):
|
worley<T,F>::worley (seed_t _seed):
|
||||||
seed (_seed)
|
seed (_seed)
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
template <typename T>
|
template <typename T, size_t F>
|
||||||
worley<T>::worley ():
|
worley<T,F>::worley ():
|
||||||
seed (time (nullptr))
|
worley (time (nullptr))
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
template <typename T>
|
template <typename T, size_t F>
|
||||||
util::range<T>
|
util::range<T>
|
||||||
worley<T>::bounds (void) const
|
worley<T,F>::bounds (void) const
|
||||||
{
|
{
|
||||||
return { 0.0, 1.5 };
|
return { 0.0, 1.5 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
template <typename T>
|
template <typename T, size_t F>
|
||||||
T
|
T
|
||||||
worley<T>::operator() (util::point<2,T> p) const
|
worley<T,F>::operator() (util::point<2,T> p) const
|
||||||
{
|
{
|
||||||
// extract integer and fractional parts. be careful to always round down
|
// extract integer and fractional parts. be careful to always round down
|
||||||
// (particularly with negatives) and avoid rounding errors.
|
// (particularly with negatives) and avoid rounding errors.
|
||||||
auto p_int = p.template cast<intmax_t> ();
|
auto p_int = p.template cast<intmax_t> ();
|
||||||
if (p.x < 0) p_int.x -= 1;
|
if (p.x < 0) p_int.x -= 1;
|
||||||
if (p.y < 0) p_int.y -= 1;
|
if (p.y < 0) p_int.y -= 1;
|
||||||
auto p_rem = abs (p - p_int);
|
auto p_rem = abs (p - p_int).template as<point> ();
|
||||||
|
|
||||||
// +---+---+---+
|
// setup an array of distances
|
||||||
// | 0 | 1 | 2 |
|
static constexpr size_t RADIUS = 1;
|
||||||
// +---+---+---+
|
static constexpr size_t COUNT = pow2 (RADIUS * 2 + 1);
|
||||||
// | 3 | 4 | 5 |
|
T distances[COUNT] = { std::numeric_limits<T>::quiet_NaN () };
|
||||||
// +---+-------+
|
|
||||||
// | 6 | 7 | 8 |
|
|
||||||
// +---+---+---+
|
|
||||||
|
|
||||||
T distances[9] = { std::numeric_limits<T>::quiet_NaN () };
|
|
||||||
T *cursor = distances;
|
T *cursor = distances;
|
||||||
|
|
||||||
for (signed y_off = -1; y_off <= 1 ; ++y_off)
|
// record the distances to each candidate point
|
||||||
for (signed x_off = -1; x_off <= 1; ++x_off) {
|
for (signed y_off = -signed(RADIUS); y_off <= signed(RADIUS) ; ++y_off) {
|
||||||
auto pos = vector<2,T> (T (x_off), T (y_off));
|
for (signed x_off = -signed(RADIUS); x_off <= signed(RADIUS); ++x_off) {
|
||||||
auto off = generate<T> (p_int + pos, this->seed);
|
auto off = vector<2,intmax_t> {x_off, y_off};
|
||||||
off += T{1};
|
auto pos = generate<T> (p_int + off, this->seed);
|
||||||
off /= T{2};
|
|
||||||
|
|
||||||
CHECK (off.x >= 0 && off.x <= 1);
|
CHECK_LIMIT (pos.x, T{0}, T{1});
|
||||||
CHECK (off.y >= 0 && off.y <= 1);
|
CHECK_LIMIT (pos.y, T{0}, T{1});
|
||||||
|
|
||||||
pos += off;
|
|
||||||
*cursor = distance2 (pos + off, p_rem);
|
*cursor = distance2 (pos + off, p_rem);
|
||||||
cursor++;
|
cursor++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::sort (std::begin (distances), std::end (distances));
|
// find the f'th lowest value
|
||||||
CHECK_GE (distances[0], 0);
|
static_assert (F < COUNT, "worley order must be less than search radius");
|
||||||
CHECK (bounds ().contains (distances[0]));
|
std::partial_sort (distances, distances + F, distances + COUNT);
|
||||||
return distances[0];
|
CHECK_GE (distances[F], 0);
|
||||||
|
return distances[F];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
#include "../../range.hpp"
|
#include "../../range.hpp"
|
||||||
|
|
||||||
namespace util { namespace noise { namespace basis {
|
namespace util { namespace noise { namespace basis {
|
||||||
template <typename T>
|
template <typename T, size_t F = 0>
|
||||||
struct worley {
|
struct worley {
|
||||||
worley (seed_t);
|
worley (seed_t);
|
||||||
worley ();
|
worley ();
|
||||||
|
Loading…
Reference in New Issue
Block a user