n/basis: use murmur mixing for random generator
This commit is contained in:
parent
b67bb283d1
commit
749e279073
@ -21,6 +21,7 @@
|
|||||||
#include "../point.hpp"
|
#include "../point.hpp"
|
||||||
#include "../random.hpp"
|
#include "../random.hpp"
|
||||||
#include "../vector.hpp"
|
#include "../vector.hpp"
|
||||||
|
#include "../hash/murmur/murmur2.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
@ -36,8 +37,15 @@ template <typename T>
|
|||||||
T
|
T
|
||||||
generate (intmax_t x, intmax_t y, uint64_t seed)
|
generate (intmax_t x, intmax_t y, uint64_t seed)
|
||||||
{
|
{
|
||||||
size_t idx = util::noise::permute (x, y, seed);
|
using util::hash::murmur2::mix;
|
||||||
return T(util::noise::LUT[idx]);
|
|
||||||
|
T v = mix (seed, mix (uint64_t (y), uint64_t (x))) & 0xffff;
|
||||||
|
v = v / T{0xffff} * 2 - 1;
|
||||||
|
|
||||||
|
CHECK_GE (v, T{0});
|
||||||
|
CHECK_LE (v, T{1});
|
||||||
|
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -46,13 +54,20 @@ template <>
|
|||||||
util::vector2d
|
util::vector2d
|
||||||
generate (intmax_t x, intmax_t y, uint64_t seed)
|
generate (intmax_t x, intmax_t y, uint64_t seed)
|
||||||
{
|
{
|
||||||
auto u = util::noise::permute (x, y, seed);
|
using util::hash::murmur2::mix;
|
||||||
auto v = util::noise::permute (u ^ seed);
|
|
||||||
|
|
||||||
return {
|
auto u = mix (seed, mix (uint64_t (x), uint64_t (y)));
|
||||||
util::noise::LUT[u],
|
auto v = mix (u, seed);
|
||||||
util::noise::LUT[v]
|
|
||||||
};
|
auto r = util::vector2d {
|
||||||
|
(u & 0xffff) / double{0xffff},
|
||||||
|
(v & 0xffff) / double{0xffff}
|
||||||
|
} * 2.0 - 1.0;
|
||||||
|
|
||||||
|
CHECK_GE (r, double{-1});
|
||||||
|
CHECK_LE (r, double{ 1});
|
||||||
|
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
@ -60,13 +75,20 @@ template <>
|
|||||||
util::vector2f
|
util::vector2f
|
||||||
generate (intmax_t x, intmax_t y, uint64_t seed)
|
generate (intmax_t x, intmax_t y, uint64_t seed)
|
||||||
{
|
{
|
||||||
auto u = util::noise::permute (x, y, seed);
|
using util::hash::murmur2::mix;
|
||||||
auto v = util::noise::permute (u ^ seed);
|
|
||||||
|
|
||||||
return {
|
auto u = mix (seed, mix (uint64_t (x), uint64_t (y)));
|
||||||
float (util::noise::LUT[u]),
|
auto v = mix (u, seed);
|
||||||
float (util::noise::LUT[v])
|
|
||||||
};
|
auto r = util::vector2f {
|
||||||
|
(u & 0xffff) / float{0xffff},
|
||||||
|
(v & 0xffff) / float{0xffff}
|
||||||
|
} * 2.f - 1.f;
|
||||||
|
|
||||||
|
CHECK_GE (r, float (-1));
|
||||||
|
CHECK_LE (r, float ( 1));
|
||||||
|
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user