summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/perlin.cc
diff options
context:
space:
mode:
authorAdam Borowski <kilobyte@angband.pl>2013-06-27 12:39:36 +0200
committerAdam Borowski <kilobyte@angband.pl>2013-06-28 23:45:12 +0200
commit5623267e1204b304e83f046072532a9912208755 (patch)
tree627be15a7e151468e90640fc9df903dc33e1001f /crawl-ref/source/perlin.cc
parent072dbc411b4a6be31ceddc6ab9e0d118f8592776 (diff)
downloadcrawl-ref-5623267e1204b304e83f046072532a9912208755.tar.gz
crawl-ref-5623267e1204b304e83f046072532a9912208755.zip
Reformat perlin.cc
I left it aside in the past, but it stands in the way of automated checks.
Diffstat (limited to 'crawl-ref/source/perlin.cc')
-rw-r--r--crawl-ref/source/perlin.cc136
1 files changed, 85 insertions, 51 deletions
diff --git a/crawl-ref/source/perlin.cc b/crawl-ref/source/perlin.cc
index e060e8cd8d..23d8abdea6 100644
--- a/crawl-ref/source/perlin.cc
+++ b/crawl-ref/source/perlin.cc
@@ -100,12 +100,12 @@ namespace perlin
}
// Skewing and unskewing factors for 2, 3, and 4 dimensions
- static const double F2 = 0.5*(sqrt(3.0)-1.0);
- static const double G2 = (3.0-sqrt(3.0))/6.0;
- static const double F3 = 1.0/3.0;
- static const double G3 = 1.0/6.0;
- static const double F4 = (sqrt(5.0)-1.0)/4.0;
- static const double G4 = (5.0-sqrt(5.0))/20.0;
+ static const double F2 = 0.5 * (sqrt(3.0) - 1.0);
+ static const double G2 = (3.0 - sqrt(3.0)) / 6.0;
+ static const double F3 = 1.0 / 3.0;
+ static const double G3 = 1.0 / 6.0;
+ static const double F4 = (sqrt(5.0) - 1.0) / 4.0;
+ static const double G4 = (5.0 - sqrt(5.0)) / 20.0;
// Use uint64_t so that noise() can work sensibly for
// coordinates from the full range of uint32_t; otherwise scaling,
@@ -113,7 +113,7 @@ namespace perlin
static uint64_t fastfloor(const double x)
{
uint64_t xi = (uint64_t) x;
- return x<xi ? xi-1 : xi;
+ return x < xi ? xi-1 : xi;
}
static double dot(Grad g, double x, double y)
@@ -146,8 +146,10 @@ namespace perlin
// For the 2D case, the simplex shape is an equilateral triangle.
// Determine which simplex we are in.
int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
- if(x0>y0) {i1=1; j1=0;} // lower triangle, XY order: (0,0)->(1,0)->(1,1)
- else {i1=0; j1=1;} // upper triangle, YX order: (0,0)->(0,1)->(1,1)
+ if (x0 > y0)
+ i1=1, j1=0; // lower triangle, XY order: (0,0)->(1,0)->(1,1)
+ else
+ i1=0, j1=1; // upper triangle, YX order: (0,0)->(0,1)->(1,1)
// A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
// a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
// c = (3-sqrt(3))/6
@@ -163,20 +165,26 @@ namespace perlin
int gi2 = permMod12(ii+1+perm[jj+1]);
// Calculate the contribution from the three corners
double t0 = 0.5 - x0*x0-y0*y0;
- if(t0<0) n0 = 0.0;
- else {
+ if (t0 < 0)
+ n0 = 0.0;
+ else
+ {
t0 *= t0;
n0 = t0 * t0 * dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
}
double t1 = 0.5 - x1*x1-y1*y1;
- if(t1<0) n1 = 0.0;
- else {
+ if (t1 < 0)
+ n1 = 0.0;
+ else
+ {
t1 *= t1;
n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
}
double t2 = 0.5 - x2*x2-y2*y2;
- if(t2<0) n2 = 0.0;
- else {
+ if (t2 < 0)
+ n2 = 0.0;
+ else
+ {
t2 *= t2;
n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
}
@@ -205,16 +213,23 @@ namespace perlin
// Determine which simplex we are in.
int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
- if(x0>=y0) {
- if(y0>=z0)
- { i1=1; j1=0; k1=0; i2=1; j2=1; k2=0; } // X Y Z order
- else if(x0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=0; k2=1; } // X Z Y order
- else { i1=0; j1=0; k1=1; i2=1; j2=0; k2=1; } // Z X Y order
+ if (x0 >= y0)
+ {
+ if (y0 >= z0)
+ i1=1, j1=0, k1=0, i2=1, j2=1, k2=0; // X Y Z order
+ else if (x0 >= z0)
+ i1=1, j1=0, k1=0, i2=1, j2=0, k2=1; // X Z Y order
+ else
+ i1=0, j1=0, k1=1, i2=1, j2=0, k2=1; // Z X Y order
}
- else { // x0<y0
- if(y0<z0) { i1=0; j1=0; k1=1; i2=0; j2=1; k2=1; } // Z Y X order
- else if(x0<z0) { i1=0; j1=1; k1=0; i2=0; j2=1; k2=1; } // Y Z X order
- else { i1=0; j1=1; k1=0; i2=1; j2=1; k2=0; } // Y X Z order
+ else
+ { // x0 < y0
+ if (y0 < z0)
+ i1=0, j1=0, k1=1, i2=0, j2=1, k2=1; // Z Y X order
+ else if (x0 < z0)
+ i1=0, j1=1, k1=0, i2=0, j2=1, k2=1; // Y Z X order
+ else
+ i1=0, j1=1, k1=0, i2=1, j2=1, k2=0; // Y X Z order
}
// A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
// a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
@@ -239,32 +254,40 @@ namespace perlin
int gi3 = permMod12(ii+1+perm[jj+1+perm[kk+1]]);
// Calculate the contribution from the four corners
double t0 = 0.6 - x0*x0 - y0*y0 - z0*z0;
- if(t0<0) n0 = 0.0;
- else {
+ if (t0 < 0)
+ n0 = 0.0;
+ else
+ {
t0 *= t0;
n0 = t0 * t0 * dot(grad3[gi0], x0, y0, z0);
}
double t1 = 0.6 - x1*x1 - y1*y1 - z1*z1;
- if(t1<0) n1 = 0.0;
- else {
+ if (t1 < 0)
+ n1 = 0.0;
+ else
+ {
t1 *= t1;
n1 = t1 * t1 * dot(grad3[gi1], x1, y1, z1);
}
double t2 = 0.6 - x2*x2 - y2*y2 - z2*z2;
- if(t2<0) n2 = 0.0;
- else {
+ if (t2 < 0)
+ n2 = 0.0;
+ else
+ {
t2 *= t2;
n2 = t2 * t2 * dot(grad3[gi2], x2, y2, z2);
}
double t3 = 0.6 - x3*x3 - y3*y3 - z3*z3;
- if(t3<0) n3 = 0.0;
- else {
+ if (t3<0)
+ n3 = 0.0;
+ else
+ {
t3 *= t3;
n3 = t3 * t3 * dot(grad3[gi3], x3, y3, z3);
}
// Add contributions from each corner to get the final noise value.
// The result is scaled to stay just inside [-1,1]
- return 32.0*(n0 + n1 + n2 + n3);
+ return 32.0 * (n0 + n1 + n2 + n3);
}
@@ -297,12 +320,12 @@ namespace perlin
int ranky = 0;
int rankz = 0;
int rankw = 0;
- if(x0 > y0) rankx++; else ranky++;
- if(x0 > z0) rankx++; else rankz++;
- if(x0 > w0) rankx++; else rankw++;
- if(y0 > z0) ranky++; else rankz++;
- if(y0 > w0) ranky++; else rankw++;
- if(z0 > w0) rankz++; else rankw++;
+ ++(x0 > y0 ? rankx : ranky);
+ ++(x0 > z0 ? rankx : rankz);
+ ++(x0 > w0 ? rankx : rankw);
+ ++(y0 > z0 ? ranky : rankz);
+ ++(y0 > w0 ? ranky : rankw);
+ ++(z0 > w0 ? rankz : rankw);
int i1, j1, k1, l1; // The integer offsets for the second simplex corner
int i2, j2, k2, l2; // The integer offsets for the third simplex corner
int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner
@@ -354,32 +377,42 @@ namespace perlin
int gi4 = perm[ii+1+perm[jj+1+perm[kk+1+perm[ll+1]]]] % 32;
// Calculate the contribution from the five corners
double t0 = 0.6 - x0*x0 - y0*y0 - z0*z0 - w0*w0;
- if(t0<0) n0 = 0.0;
- else {
+ if (t0 < 0)
+ n0 = 0.0;
+ else
+ {
t0 *= t0;
n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0);
}
double t1 = 0.6 - x1*x1 - y1*y1 - z1*z1 - w1*w1;
- if(t1<0) n1 = 0.0;
- else {
+ if (t1 < 0)
+ n1 = 0.0;
+ else
+ {
t1 *= t1;
n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1);
}
double t2 = 0.6 - x2*x2 - y2*y2 - z2*z2 - w2*w2;
- if(t2<0) n2 = 0.0;
- else {
+ if (t2 < 0)
+ n2 = 0.0;
+ else
+ {
t2 *= t2;
n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2);
}
double t3 = 0.6 - x3*x3 - y3*y3 - z3*z3 - w3*w3;
- if(t3<0) n3 = 0.0;
- else {
+ if (t3 < 0)
+ n3 = 0.0;
+ else
+ {
t3 *= t3;
n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3);
}
double t4 = 0.6 - x4*x4 - y4*y4 - z4*z4 - w4*w4;
- if(t4<0) n4 = 0.0;
- else {
+ if (t4 < 0)
+ n4 = 0.0;
+ else
+ {
t4 *= t4;
n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4);
}
@@ -402,9 +435,10 @@ namespace perlin
double xi = x;
double yi = y;
double zi = z;
- for (uint32_t octave = 0; octave < octaves; ++octave) {
+ for (uint32_t octave = 0; octave < octaves; ++octave)
+ {
value += noise(xi / divisor, yi / divisor, zi / divisor) / divisor;
- norm += 1/divisor;
+ norm += 1 / divisor;
divisor *= 2;
double xt = yi * sin(1.41421356) + cos(1.41421356);
yi = yi * cos(1.41421356) + sin(1.41421356);