Noise: First pass of refactoring

Former-commit-id: c71e76f337fd3fc1793f105d189f3ceecb80e537 [formerly 5ac3cfc15257e407cb388bcedb1a96be5381ef67]
Former-commit-id: c97fb23feb0e4bd4d6965e83d91a38cec1382e48
This commit is contained in:
Jérôme Leclercq
2016-06-18 12:36:20 +02:00
parent 33f2241d95
commit b0fc1c9bf9
14 changed files with 485 additions and 535 deletions

View File

@@ -10,68 +10,27 @@
namespace Nz
{
Perlin::Perlin() :
gradient2{
{1.f,1.f},{-1.f,1.f},{1.f,-1.f},{-1.f,-1.f},
{1.f,0.f},{-1.f,0.f},{0.f,1.f},{0.f,-1.f}
},
gradient3{
{1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0},
{1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1},
{0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1},
{1,1,0},{-1,1,0},{0,-1,1},{0,-1,-1}
},
gradient4{
{0,1,1,1}, {0,1,1,-1}, {0,1,-1,1}, {0,1,-1,-1},
{0,-1,1,1},{0,-1,1,-1},{0,-1,-1,1},{0,-1,-1,-1},
{1,0,1,1}, {1,0,1,-1}, {1,0,-1,1}, {1,0,-1,-1},
{-1,0,1,1},{-1,0,1,-1},{-1,0,-1,1},{-1,0,-1,-1},
{1,1,0,1}, {1,1,0,-1}, {1,-1,0,1}, {1,-1,0,-1},
{-1,1,0,1},{-1,1,0,-1},{-1,-1,0,1},{-1,-1,0,-1},
{1,1,1,0}, {1,1,-1,0}, {1,-1,1,0}, {1,-1,-1,0},
{-1,1,1,0},{-1,1,-1,0},{-1,-1,1,0},{-1,-1,-1,0}
}
{
}
Perlin::Perlin(unsigned int seed) : Perlin()
Perlin::Perlin(unsigned int seed) :
Perlin()
{
SetSeed(seed);
Shuffle();
}
float Perlin::Get(std::initializer_list<float> coordinates, float scale) const
float Perlin::Get(float x, float y, float scale) const
{
switch(coordinates.size())
{
case 2:
return this->_2D(coordinates,scale);
case 3:
return this->_3D(coordinates,scale);
case 4:
return this->_4D(coordinates,scale);
default:
throw std::invalid_argument("Number of coordinates elements not comprised between 2 and 4");
}
}
float xc, yc;
int x0, y0;
int gi0,gi1,gi2,gi3;
int ii, jj;
float Perlin::_2D(std::initializer_list<float> coordinates, float scale) const
{
thread_local float xc, yc;
thread_local int x0, y0;
thread_local int gi0,gi1,gi2,gi3;
thread_local int ii, jj;
float s,t,u,v;
float Cx,Cy;
float Li1, Li2;
float tempx,tempy;
thread_local float s,t,u,v;
thread_local float Cx,Cy;
thread_local float Li1, Li2;
thread_local float tempx,tempy;
std::initializer_list<float>::const_iterator it = coordinates.begin();
xc = *(it ) * scale;
yc = *(++it) * scale;
xc = x * scale;
yc = y * scale;
x0 = fastfloor(xc);
y0 = fastfloor(yc);
@@ -79,10 +38,10 @@ namespace Nz
ii = x0 & 255;
jj = y0 & 255;
gi0 = perm[ii + perm[jj]] & 7;
gi1 = perm[ii + 1 + perm[jj]] & 7;
gi2 = perm[ii + perm[jj + 1]] & 7;
gi3 = perm[ii + 1 + perm[jj + 1]] & 7;
gi0 = m_permutations[ii + m_permutations[jj]] & 7;
gi1 = m_permutations[ii + 1 + m_permutations[jj]] & 7;
gi2 = m_permutations[ii + m_permutations[jj + 1]] & 7;
gi3 = m_permutations[ii + 1 + m_permutations[jj + 1]] & 7;
tempx = xc - x0;
tempy = yc - y0;
@@ -90,16 +49,16 @@ namespace Nz
Cx = tempx * tempx * tempx * (tempx * (tempx * 6 - 15) + 10);
Cy = tempy * tempy * tempy * (tempy * (tempy * 6 - 15) + 10);
s = gradient2[gi0][0]*tempx + gradient2[gi0][1]*tempy;
s = s_gradients2[gi0][0]*tempx + s_gradients2[gi0][1]*tempy;
tempx = xc - (x0 + 1);
t = gradient2[gi1][0]*tempx + gradient2[gi1][1]*tempy;
t = s_gradients2[gi1][0]*tempx + s_gradients2[gi1][1]*tempy;
tempy = yc - (y0 + 1);
v = gradient2[gi3][0]*tempx + gradient2[gi3][1]*tempy;
v = s_gradients2[gi3][0]*tempx + s_gradients2[gi3][1]*tempy;
tempx = xc - x0;
u = gradient2[gi2][0]*tempx + gradient2[gi2][1]*tempy;
u = s_gradients2[gi2][0]*tempx + s_gradients2[gi2][1]*tempy;
Li1 = s + Cx*(t-s);
Li2 = u + Cx*(v-u);
@@ -107,26 +66,24 @@ namespace Nz
return Li1 + Cy*(Li2-Li1);
}
float Perlin::_3D(std::initializer_list<float> coordinates, float scale) const
float Perlin::Get(float x, float y, float z, float scale) const
{
thread_local float xc, yc, zc;
thread_local int x0, y0, z0;
thread_local int gi0,gi1,gi2,gi3,gi4,gi5,gi6,gi7;
thread_local int ii, jj, kk;
float xc, yc, zc;
int x0, y0, z0;
int gi0,gi1,gi2,gi3,gi4,gi5,gi6,gi7;
int ii, jj, kk;
thread_local float Li1,Li2,Li3,Li4,Li5,Li6;
thread_local float s[2],t[2],u[2],v[2];
thread_local float Cx,Cy,Cz;
thread_local float nx,ny,nz;
float Li1,Li2,Li3,Li4,Li5,Li6;
float s[2],t[2],u[2],v[2];
float Cx,Cy,Cz;
float nx,ny,nz;
thread_local float tmp;
thread_local float tempx,tempy,tempz;
float tmp;
float tempx,tempy,tempz;
std::initializer_list<float>::const_iterator it = coordinates.begin();
xc = *(it ) * scale;
yc = *(++it) * scale;
zc = *(++it) * scale;
xc = x * scale;
yc = y * scale;
zc = z * scale;
x0 = fastfloor(xc);
y0 = fastfloor(yc);
@@ -136,15 +93,15 @@ namespace Nz
jj = y0 & 255;
kk = z0 & 255;
gi0 = perm[ii + perm[jj + perm[kk]]] & 15;
gi1 = perm[ii + 1 + perm[jj + perm[kk]]] & 15;
gi2 = perm[ii + perm[jj + 1 + perm[kk]]] & 15;
gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk]]] & 15;
gi0 = m_permutations[ii + m_permutations[jj + m_permutations[kk]]] & 15;
gi1 = m_permutations[ii + 1 + m_permutations[jj + m_permutations[kk]]] & 15;
gi2 = m_permutations[ii + m_permutations[jj + 1 + m_permutations[kk]]] & 15;
gi3 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk]]] & 15;
gi4 = perm[ii + perm[jj + perm[kk + 1]]] & 15;
gi5 = perm[ii + 1 + perm[jj + perm[kk + 1]]] & 15;
gi6 = perm[ii + perm[jj + 1 + perm[kk + 1]]] & 15;
gi7 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]] & 15;
gi4 = m_permutations[ii + m_permutations[jj + m_permutations[kk + 1]]] & 15;
gi5 = m_permutations[ii + 1 + m_permutations[jj + m_permutations[kk + 1]]] & 15;
gi6 = m_permutations[ii + m_permutations[jj + 1 + m_permutations[kk + 1]]] & 15;
gi7 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk + 1]]] & 15;
tempx = xc - x0;
tempy = yc - y0;
@@ -154,29 +111,29 @@ namespace Nz
Cy = tempy * tempy * tempy * (tempy * (tempy * 6 - 15) + 10);
Cz = tempz * tempz * tempz * (tempz * (tempz * 6 - 15) + 10);
s[0] = gradient3[gi0][0]*tempx + gradient3[gi0][1]*tempy + gradient3[gi0][2]*tempz;
s[0] = s_gradients3[gi0][0]*tempx + s_gradients3[gi0][1]*tempy + s_gradients3[gi0][2]*tempz;
tempx = xc - (x0 + 1);
t[0] = gradient3[gi1][0]*tempx + gradient3[gi1][1]*tempy + gradient3[gi1][2]*tempz;
t[0] = s_gradients3[gi1][0]*tempx + s_gradients3[gi1][1]*tempy + s_gradients3[gi1][2]*tempz;
tempy = yc - (y0 + 1);
v[0] = gradient3[gi3][0]*tempx + gradient3[gi3][1]*tempy + gradient3[gi3][2]*tempz;
v[0] = s_gradients3[gi3][0]*tempx + s_gradients3[gi3][1]*tempy + s_gradients3[gi3][2]*tempz;
tempx = xc - x0;
u[0] = gradient3[gi2][0]*tempx + gradient3[gi2][1]*tempy + gradient3[gi2][2]*tempz;
u[0] = s_gradients3[gi2][0]*tempx + s_gradients3[gi2][1]*tempy + s_gradients3[gi2][2]*tempz;
tempy = yc - y0;
tempz = zc - (z0 + 1);
s[1] = gradient3[gi4][0]*tempx + gradient3[gi4][1]*tempy + gradient3[gi4][2]*tempz;
s[1] = s_gradients3[gi4][0]*tempx + s_gradients3[gi4][1]*tempy + s_gradients3[gi4][2]*tempz;
tempx = xc - (x0 + 1);
t[1] = gradient3[gi5][0]*tempx + gradient3[gi5][1]*tempy + gradient3[gi5][2]*tempz;
t[1] = s_gradients3[gi5][0]*tempx + s_gradients3[gi5][1]*tempy + s_gradients3[gi5][2]*tempz;
tempy = yc - (y0 + 1);
v[1] = gradient3[gi7][0]*tempx + gradient3[gi7][1]*tempy + gradient3[gi7][2]*tempz;
v[1] = s_gradients3[gi7][0]*tempx + s_gradients3[gi7][1]*tempy + s_gradients3[gi7][2]*tempz;
tempx = xc - x0;
u[1] = gradient3[gi6][0]*tempx + gradient3[gi6][1]*tempy + gradient3[gi6][2]*tempz;
u[1] = s_gradients3[gi6][0]*tempx + s_gradients3[gi6][1]*tempy + s_gradients3[gi6][2]*tempz;
Li1 = s[0] + Cx*(t[0]-s[0]);
Li2 = u[0] + Cx*(v[0]-u[0]);
@@ -189,26 +146,24 @@ namespace Nz
return Li5 + Cz * (Li6-Li5);
}
float Perlin::_4D(std::initializer_list<float> coordinates, float scale) const
float Perlin::Get(float x, float y, float z, float w, float scale) const
{
thread_local float xc,yc,zc,wc;
thread_local int x0,y0,z0,w0;
thread_local int gi0,gi1,gi2,gi3,gi4,gi5,gi6,gi7,gi8,gi9,gi10,gi11,gi12,gi13,gi14,gi15;
thread_local int ii,jj,kk,ll;
float xc,yc,zc,wc;
int x0,y0,z0,w0;
int gi0,gi1,gi2,gi3,gi4,gi5,gi6,gi7,gi8,gi9,gi10,gi11,gi12,gi13,gi14,gi15;
int ii,jj,kk,ll;
thread_local float Li1,Li2,Li3,Li4,Li5,Li6,Li7,Li8,Li9,Li10,Li11,Li12,Li13,Li14;
thread_local float s[4],t[4],u[4],v[4];
thread_local float Cx,Cy,Cz,Cw;
float Li1,Li2,Li3,Li4,Li5,Li6,Li7,Li8,Li9,Li10,Li11,Li12,Li13,Li14;
float s[4],t[4],u[4],v[4];
float Cx,Cy,Cz,Cw;
thread_local float tmp;
thread_local float tempx,tempy,tempz,tempw;
float tmp;
float tempx,tempy,tempz,tempw;
std::initializer_list<float>::const_iterator it = coordinates.begin();
xc = *(it ) * scale;
yc = *(++it) * scale;
zc = *(++it) * scale;
wc = *(++it) * scale;
xc = x * scale;
yc = y * scale;
zc = z * scale;
wc = w * scale;
x0 = fastfloor(xc);
y0 = fastfloor(yc);
@@ -220,25 +175,25 @@ namespace Nz
kk = z0 & 255;
ll = w0 & 255;
gi0 = perm[ii + perm[jj + perm[kk + perm[ll]]]] & 31;
gi1 = perm[ii + 1 + perm[jj + perm[kk + perm[ll]]]] & 31;
gi2 = perm[ii + perm[jj + 1 + perm[kk + perm[ll]]]] & 31;
gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk + perm[ll]]]] & 31;
gi0 = m_permutations[ii + m_permutations[jj + m_permutations[kk + m_permutations[ll]]]] & 31;
gi1 = m_permutations[ii + 1 + m_permutations[jj + m_permutations[kk + m_permutations[ll]]]] & 31;
gi2 = m_permutations[ii + m_permutations[jj + 1 + m_permutations[kk + m_permutations[ll]]]] & 31;
gi3 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk + m_permutations[ll]]]] & 31;
gi4 = perm[ii + perm[jj + + perm[kk + 1 + perm[ll]]]] & 31;
gi5 = perm[ii + 1 + perm[jj + + perm[kk + 1 + perm[ll]]]] & 31;
gi6 = perm[ii + perm[jj + 1 + perm[kk + 1 + perm[ll]]]] & 31;
gi7 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll]]]] & 31;
gi4 = m_permutations[ii + m_permutations[jj + + m_permutations[kk + 1 + m_permutations[ll]]]] & 31;
gi5 = m_permutations[ii + 1 + m_permutations[jj + + m_permutations[kk + 1 + m_permutations[ll]]]] & 31;
gi6 = m_permutations[ii + m_permutations[jj + 1 + m_permutations[kk + 1 + m_permutations[ll]]]] & 31;
gi7 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk + 1 + m_permutations[ll]]]] & 31;
gi8 = perm[ii + perm[jj + perm[kk + perm[ll + 1]]]] & 31;
gi9 = perm[ii + 1 + perm[jj + perm[kk + perm[ll + 1]]]] & 31;
gi10 = perm[ii + perm[jj + 1 + perm[kk + perm[ll + 1]]]] & 31;
gi11 = perm[ii + 1 + perm[jj + 1 + perm[kk + perm[ll + 1]]]] & 31;
gi8 = m_permutations[ii + m_permutations[jj + m_permutations[kk + m_permutations[ll + 1]]]] & 31;
gi9 = m_permutations[ii + 1 + m_permutations[jj + m_permutations[kk + m_permutations[ll + 1]]]] & 31;
gi10 = m_permutations[ii + m_permutations[jj + 1 + m_permutations[kk + m_permutations[ll + 1]]]] & 31;
gi11 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk + m_permutations[ll + 1]]]] & 31;
gi12 = perm[ii + perm[jj + perm[kk + 1 + perm[ll + 1]]]] & 31;
gi13 = perm[ii + 1 + perm[jj + perm[kk + 1 + perm[ll + 1]]]] & 31;
gi14 = perm[ii + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] & 31;
gi15 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] & 31;
gi12 = m_permutations[ii + m_permutations[jj + m_permutations[kk + 1 + m_permutations[ll + 1]]]] & 31;
gi13 = m_permutations[ii + 1 + m_permutations[jj + m_permutations[kk + 1 + m_permutations[ll + 1]]]] & 31;
gi14 = m_permutations[ii + m_permutations[jj + 1 + m_permutations[kk + 1 + m_permutations[ll + 1]]]] & 31;
gi15 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk + 1 + m_permutations[ll + 1]]]] & 31;
tempx = xc - x0;
tempy = yc - y0;
@@ -250,58 +205,58 @@ namespace Nz
Cz = tempz * tempz * tempz * (tempz * (tempz * 6 - 15) + 10);
Cw = tempw * tempw * tempw * (tempw * (tempw * 6 - 15) + 10);
s[0] = gradient4[gi0][0]*tempx + gradient4[gi0][1]*tempy + gradient4[gi0][2]*tempz + gradient4[gi0][3]*tempw;
s[0] = s_gradients4[gi0][0]*tempx + s_gradients4[gi0][1]*tempy + s_gradients4[gi0][2]*tempz + s_gradients4[gi0][3]*tempw;
tempx = xc - (x0+1);
t[0] = gradient4[gi1][0]*tempx + gradient4[gi1][1]*tempy + gradient4[gi1][2]*tempz + gradient4[gi1][3]*tempw;
t[0] = s_gradients4[gi1][0]*tempx + s_gradients4[gi1][1]*tempy + s_gradients4[gi1][2]*tempz + s_gradients4[gi1][3]*tempw;
tempy = yc - (y0+1);
v[0] = gradient4[gi3][0]*tempx + gradient4[gi3][1]*tempy + gradient4[gi3][2]*tempz + gradient4[gi3][3]*tempw;
v[0] = s_gradients4[gi3][0]*tempx + s_gradients4[gi3][1]*tempy + s_gradients4[gi3][2]*tempz + s_gradients4[gi3][3]*tempw;
tempx = xc - x0;
u[0] = gradient4[gi2][0]*tempx + gradient4[gi2][1]*tempy + gradient4[gi2][2]*tempz + gradient4[gi2][3]*tempw;
u[0] = s_gradients4[gi2][0]*tempx + s_gradients4[gi2][1]*tempy + s_gradients4[gi2][2]*tempz + s_gradients4[gi2][3]*tempw;
tempy = yc - y0;
tempz = zc - (z0+1);
s[1] = gradient4[gi4][0]*tempx + gradient4[gi4][1]*tempy + gradient4[gi4][2]*tempz + gradient4[gi4][3]*tempw;
s[1] = s_gradients4[gi4][0]*tempx + s_gradients4[gi4][1]*tempy + s_gradients4[gi4][2]*tempz + s_gradients4[gi4][3]*tempw;
tempx = xc - (x0+1);
t[1] = gradient4[gi5][0]*tempx + gradient4[gi5][1]*tempy + gradient4[gi5][2]*tempz + gradient4[gi5][3]*tempw;
t[1] = s_gradients4[gi5][0]*tempx + s_gradients4[gi5][1]*tempy + s_gradients4[gi5][2]*tempz + s_gradients4[gi5][3]*tempw;
tempy = yc - (y0+1);
v[1] = gradient4[gi7][0]*tempx + gradient4[gi7][1]*tempy + gradient4[gi7][2]*tempz + gradient4[gi7][3]*tempw;
v[1] = s_gradients4[gi7][0]*tempx + s_gradients4[gi7][1]*tempy + s_gradients4[gi7][2]*tempz + s_gradients4[gi7][3]*tempw;
tempx = xc - x0;
u[1] = gradient4[gi6][0]*tempx + gradient4[gi6][1]*tempy + gradient4[gi6][2]*tempz + gradient4[gi6][3]*tempw;
u[1] = s_gradients4[gi6][0]*tempx + s_gradients4[gi6][1]*tempy + s_gradients4[gi6][2]*tempz + s_gradients4[gi6][3]*tempw;
tempy = yc - y0;
tempz = zc - z0;
tempw = wc - (w0+1);
s[2] = gradient4[gi8][0]*tempx + gradient4[gi8][1]*tempy + gradient4[gi8][2]*tempz + gradient4[gi8][3]*tempw;
s[2] = s_gradients4[gi8][0]*tempx + s_gradients4[gi8][1]*tempy + s_gradients4[gi8][2]*tempz + s_gradients4[gi8][3]*tempw;
tempx = xc - (x0+1);
t[2] = gradient4[gi9][0]*tempx + gradient4[gi9][1]*tempy + gradient4[gi9][2]*tempz + gradient4[gi9][3]*tempw;
t[2] = s_gradients4[gi9][0]*tempx + s_gradients4[gi9][1]*tempy + s_gradients4[gi9][2]*tempz + s_gradients4[gi9][3]*tempw;
tempy = yc - (y0+1);
v[2] = gradient4[gi11][0]*tempx + gradient4[gi11][1]*tempy + gradient4[gi11][2]*tempz + gradient4[gi11][3]*tempw;
v[2] = s_gradients4[gi11][0]*tempx + s_gradients4[gi11][1]*tempy + s_gradients4[gi11][2]*tempz + s_gradients4[gi11][3]*tempw;
tempx = xc - x0;
u[2] = gradient4[gi10][0]*tempx + gradient4[gi10][1]*tempy + gradient4[gi10][2]*tempz + gradient4[gi10][3]*tempw;
u[2] = s_gradients4[gi10][0]*tempx + s_gradients4[gi10][1]*tempy + s_gradients4[gi10][2]*tempz + s_gradients4[gi10][3]*tempw;
tempy = yc - y0;
tempz = zc - (z0+1);
s[3] = gradient4[gi12][0]*tempx + gradient4[gi12][1]*tempy + gradient4[gi12][2]*tempz + gradient4[gi12][3]*tempw;
s[3] = s_gradients4[gi12][0]*tempx + s_gradients4[gi12][1]*tempy + s_gradients4[gi12][2]*tempz + s_gradients4[gi12][3]*tempw;
tempx = xc - (x0+1);
t[3] = gradient4[gi13][0]*tempx + gradient4[gi13][1]*tempy + gradient4[gi13][2]*tempz + gradient4[gi13][3]*tempw;
t[3] = s_gradients4[gi13][0]*tempx + s_gradients4[gi13][1]*tempy + s_gradients4[gi13][2]*tempz + s_gradients4[gi13][3]*tempw;
tempy = yc - (y0+1);
v[3] = gradient4[gi15][0]*tempx + gradient4[gi15][1]*tempy + gradient4[gi15][2]*tempz + gradient4[gi15][3]*tempw;
v[3] = s_gradients4[gi15][0]*tempx + s_gradients4[gi15][1]*tempy + s_gradients4[gi15][2]*tempz + s_gradients4[gi15][3]*tempw;
tempx = xc - x0;
u[3] = gradient4[gi14][0]*tempx + gradient4[gi14][1]*tempy + gradient4[gi14][2]*tempz + gradient4[gi14][3]*tempw;
u[3] = s_gradients4[gi14][0]*tempx + s_gradients4[gi14][1]*tempy + s_gradients4[gi14][2]*tempz + s_gradients4[gi14][3]*tempw;
Li1 = s[0] + Cx*(t[0]-s[0]);
Li2 = u[0] + Cx*(v[0]-u[0]);