2013-10-01 08:58:04 -06:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace DFHack {
|
|
|
|
namespace Random {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A good explanation:
|
|
|
|
* http://webstaff.itn.liu.se/~stegu/TNM022-2005/perlinnoiselinks/perlin-noise-math-faq.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Interpolation functions
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline T s_curve(T t)
|
|
|
|
{
|
2013-10-02 08:55:41 -06:00
|
|
|
// Classical function
|
|
|
|
//return t * t * (3 - 2*t);
|
|
|
|
|
|
|
|
// 2002 version from http://mrl.nyu.edu/~perlin/paper445.pdf
|
|
|
|
return t * t * t * (t * (t * 6 - 15) + 10);
|
2013-10-01 08:58:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline T lerp(T s, T a, T b)
|
|
|
|
{
|
|
|
|
return a + s * (b-a);
|
|
|
|
}
|
|
|
|
|
2013-10-02 08:55:41 -06:00
|
|
|
// Dot product of VSIZE vectors pointed by pa, pb
|
|
|
|
|
|
|
|
template<class T, unsigned i>
|
|
|
|
struct DotProduct {
|
|
|
|
static inline T eval(T *pa, T *pb);
|
|
|
|
};
|
|
|
|
template<class T>
|
|
|
|
struct DotProduct<T,0> {
|
|
|
|
static inline T eval(T *pa, T *pb) { return pa[0]*pb[0]; }
|
|
|
|
};
|
|
|
|
template<class T, unsigned i>
|
|
|
|
inline T DotProduct<T,i>::eval(T *pa, T *pb) {
|
|
|
|
return DotProduct<T,i-1>::eval(pa, pb) + pa[i]*pb[i];
|
|
|
|
}
|
|
|
|
|
2013-10-01 08:58:04 -06:00
|
|
|
// Templates used to force unrolling and inlining of the loops
|
|
|
|
|
|
|
|
template<class T, unsigned VSIZE, unsigned BITS, class IDXT>
|
|
|
|
template<unsigned mask>
|
2013-10-02 08:55:41 -06:00
|
|
|
struct PerlinNoise<T,VSIZE,BITS,IDXT>::Impl<mask,-1> {
|
2013-10-01 08:58:04 -06:00
|
|
|
typedef typename PerlinNoise<T,VSIZE,BITS,IDXT>::Temp Temp;
|
2013-10-02 08:55:41 -06:00
|
|
|
static inline void setup(PerlinNoise<T,VSIZE,BITS,IDXT> *, const T *, Temp *) {}
|
2013-10-01 08:58:04 -06:00
|
|
|
static inline T eval(PerlinNoise<T,VSIZE,BITS,IDXT> *self, Temp *pt, unsigned idx, T *pq);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialization of the temporaries from input coordinates
|
|
|
|
|
|
|
|
template<class T, unsigned VSIZE, unsigned BITS, class IDXT>
|
2013-10-02 08:55:41 -06:00
|
|
|
template<unsigned mask, int i>
|
|
|
|
inline void PerlinNoise<T,VSIZE,BITS,IDXT>::Impl<mask,i>::setup(
|
|
|
|
PerlinNoise<T,VSIZE,BITS,IDXT> *self, const T *pv, Temp *pt
|
|
|
|
) {
|
|
|
|
Impl<mask,i-1>::setup(self, pv, pt);
|
2013-10-01 08:58:04 -06:00
|
|
|
|
2013-10-08 08:16:33 -06:00
|
|
|
int32_t t = int32_t(pv[i]);
|
|
|
|
t -= (pv[i]<t);
|
2013-10-02 08:55:41 -06:00
|
|
|
pt[i].s = s_curve(pt[i].r0 = pv[i] - t);
|
|
|
|
|
|
|
|
unsigned b = unsigned(int32_t(t));
|
|
|
|
pt[i].b0 = self->idxmap[i][b & mask];
|
|
|
|
pt[i].b1 = self->idxmap[i][(b+1) & mask];
|
2013-10-01 08:58:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Main recursion. Uses tables from self and pt.
|
|
|
|
// Recursion changes current index idx, and current offset vector pq.
|
|
|
|
|
|
|
|
template<class T, unsigned VSIZE, unsigned BITS, class IDXT>
|
|
|
|
template<unsigned mask>
|
2013-10-02 08:55:41 -06:00
|
|
|
inline T PerlinNoise<T,VSIZE,BITS,IDXT>::Impl<mask, -1>::eval(
|
2013-10-01 08:58:04 -06:00
|
|
|
PerlinNoise<T,VSIZE,BITS,IDXT> *self, Temp *pt, unsigned idx, T *pq
|
|
|
|
) {
|
2013-10-02 08:55:41 -06:00
|
|
|
return DotProduct<T,VSIZE-1>::eval(pq, self->gradients[idx]);
|
2013-10-01 08:58:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, unsigned VSIZE, unsigned BITS, class IDXT>
|
2013-10-02 08:55:41 -06:00
|
|
|
template<unsigned mask, int i>
|
2013-10-01 08:58:04 -06:00
|
|
|
inline T PerlinNoise<T,VSIZE,BITS,IDXT>::Impl<mask,i>::eval(
|
|
|
|
PerlinNoise<T,VSIZE,BITS,IDXT> *self, Temp *pt, unsigned idx, T *pq
|
|
|
|
) {
|
|
|
|
pq[i] = pt[i].r0;
|
2013-10-02 08:55:41 -06:00
|
|
|
T u = Impl<mask,i-1>::eval(self, pt, idx ^ pt[i].b0, pq);
|
2013-10-01 08:58:04 -06:00
|
|
|
|
|
|
|
pq[i] -= 1;
|
2013-10-02 08:55:41 -06:00
|
|
|
T v = Impl<mask,i-1>::eval(self, pt, idx ^ pt[i].b1, pq);
|
2013-10-01 08:58:04 -06:00
|
|
|
|
|
|
|
return lerp(pt[i].s, u, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actual methods of the object
|
|
|
|
|
|
|
|
template<class T, unsigned VSIZE, unsigned BITS, class IDXT>
|
|
|
|
void PerlinNoise<T,VSIZE,BITS,IDXT>::init(MersenneRNG &rng)
|
|
|
|
{
|
|
|
|
STATIC_ASSERT(VSIZE > 0 && BITS <= 8*sizeof(IDXT));
|
|
|
|
|
|
|
|
// Random unit gradient vectors
|
|
|
|
for (unsigned i = 0; i < TSIZE; i++)
|
|
|
|
rng.unitvector(gradients[i], VSIZE);
|
|
|
|
|
2013-10-02 08:55:41 -06:00
|
|
|
// Random permutation tables
|
|
|
|
for (unsigned j = 0; j < VSIZE; j++)
|
2013-10-01 08:58:04 -06:00
|
|
|
{
|
2013-10-02 08:55:41 -06:00
|
|
|
for (unsigned i = 0; i < TSIZE; i++)
|
|
|
|
idxmap[j][i] = i;
|
2013-10-01 08:58:04 -06:00
|
|
|
|
2013-10-02 08:55:41 -06:00
|
|
|
rng.permute(idxmap[j], TSIZE);
|
2013-10-01 08:58:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, unsigned VSIZE, unsigned BITS, class IDXT>
|
|
|
|
T PerlinNoise<T,VSIZE,BITS,IDXT>::eval(const T coords[VSIZE])
|
|
|
|
{
|
|
|
|
// Precomputed properties from the coordinates
|
|
|
|
Temp tmp[VSIZE];
|
|
|
|
// Temporary used to build the current offset vector
|
|
|
|
T q[VSIZE];
|
|
|
|
|
2013-10-02 08:55:41 -06:00
|
|
|
Impl<TSIZE-1,VSIZE-1>::setup(this, coords, tmp);
|
2013-10-01 08:58:04 -06:00
|
|
|
|
|
|
|
return Impl<TSIZE-1,VSIZE-1>::eval(this, tmp, 0, q);
|
|
|
|
}
|
|
|
|
|
|
|
|
}} // namespace
|