Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.util.noise;
2
3import org.bukkit.World;
4
5import java.util.Random;
6
7/**
8 * Generates simplex-based noise.
9 * <p>
10 * This is a modified version of the freely published version in the paper by
11 * Stefan Gustavson at http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
12 */
13public class SimplexNoiseGenerator extends PerlinNoiseGenerator {
14 protected static final double SQRT_3 = Math.sqrt(3);
15 protected static final double SQRT_5 = Math.sqrt(5);
16 protected static final double F2 = 0.5 * (SQRT_3 - 1);
17 protected static final double G2 = (3 - SQRT_3) / 6;
18 protected static final double G22 = G2 * 2.0 - 1;
19 protected static final double F3 = 1.0 / 3.0;
20 protected static final double G3 = 1.0 / 6.0;
21 protected static final double F4 = (SQRT_5 - 1.0) / 4.0;
22 protected static final double G4 = (5.0 - SQRT_5) / 20.0;
23 protected static final double G42 = G4 * 2.0;
24 protected static final double G43 = G4 * 3.0;
25 protected static final double G44 = G4 * 4.0 - 1.0;
26 protected static final int grad4[][] = { { 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 } };
27 protected static final int simplex[][] = { { 0, 1, 2, 3 }, { 0, 1, 3, 2 }, { 0, 0, 0, 0 }, { 0, 2, 3, 1 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 2, 3, 0 }, { 0, 2, 1, 3 }, { 0, 0, 0, 0 }, { 0, 3, 1, 2 }, { 0, 3, 2, 1 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 3, 2, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 2, 0, 3 }, { 0, 0, 0, 0 }, { 1, 3, 0, 2 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 2, 3, 0, 1 }, { 2, 3, 1, 0 }, { 1, 0, 2, 3 }, { 1, 0, 3, 2 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 2, 0, 3, 1 }, { 0, 0, 0, 0 }, { 2, 1, 3, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 2, 0, 1, 3 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 3, 0, 1, 2 }, { 3, 0, 2, 1 }, { 0, 0, 0, 0 }, { 3, 1, 2, 0 }, { 2, 1, 0, 3 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 3, 1, 0, 2 }, { 0, 0, 0, 0 }, { 3, 2, 0, 1 }, { 3, 2, 1, 0 } };
28 protected static double offsetW;
29 private static final SimplexNoiseGenerator instance = new SimplexNoiseGenerator();
30
31 protected SimplexNoiseGenerator() {
32 super();
33 }
34
35 /**
36 * Creates a seeded simplex noise generator for the given world
37 *
38 * @param world World to construct this generator for
39 */
40 public SimplexNoiseGenerator(World world) {
41 this(new Random(world.getSeed()));
42 }
43
44 /**
45 * Creates a seeded simplex noise generator for the given seed
46 *
47 * @param seed Seed to construct this generator for
48 */
49 public SimplexNoiseGenerator(long seed) {
50 this(new Random(seed));
51 }
52
53 /**
54 * Creates a seeded simplex noise generator with the given Random
55 *
56 * @param rand Random to construct with
57 */
58 public SimplexNoiseGenerator(Random rand) {
59 super(rand);
60 offsetW = rand.nextDouble() * 256;
61 }
62
63 protected static double dot(int g[], double x, double y) {
64 return g[0] * x + g[1] * y;
65 }
66
67 protected static double dot(int g[], double x, double y, double z) {
68 return g[0] * x + g[1] * y + g[2] * z;
69 }
70
71 protected static double dot(int g[], double x, double y, double z, double w) {
72 return g[0] * x + g[1] * y + g[2] * z + g[3] * w;
73 }
74
75 /**
76 * Computes and returns the 1D unseeded simplex noise for the given coordinates in 1D space
77 *
78 * @param xin X coordinate
79 * @return Noise at given location, from range -1 to 1
80 */
81 public static double getNoise(double xin) {
82 return instance.noise(xin);
83 }
84
85 /**
86 * Computes and returns the 2D unseeded simplex noise for the given coordinates in 2D space
87 *
88 * @param xin X coordinate
89 * @param yin Y coordinate
90 * @return Noise at given location, from range -1 to 1
91 */
92 public static double getNoise(double xin, double yin) {
93 return instance.noise(xin, yin);
94 }
95
96 /**
97 * Computes and returns the 3D unseeded simplex noise for the given coordinates in 3D space
98 *
99 * @param xin X coordinate
100 * @param yin Y coordinate
101 * @param zin Z coordinate
102 * @return Noise at given location, from range -1 to 1
103 */
104 public static double getNoise(double xin, double yin, double zin) {
105 return instance.noise(xin, yin, zin);
106 }
107
108 /**
109 * Computes and returns the 4D simplex noise for the given coordinates in 4D space
110 *
111 * @param x X coordinate
112 * @param y Y coordinate
113 * @param z Z coordinate
114 * @param w W coordinate
115 * @return Noise at given location, from range -1 to 1
116 */
117 public static double getNoise(double x, double y, double z, double w) {
118 return instance.noise(x, y, z, w);
119 }
120
121 @Override
122 public double noise(double xin, double yin, double zin) {
123 xin += offsetX;
124 yin += offsetY;
125 zin += offsetZ;
126
127 double n0, n1, n2, n3; // Noise contributions from the four corners
128
129 // Skew the input space to determine which simplex cell we're in
130 double s = (xin + yin + zin) * F3; // Very nice and simple skew factor for 3D
131 int i = floor(xin + s);
132 int j = floor(yin + s);
133 int k = floor(zin + s);
134 double t = (i + j + k) * G3;
135 double X0 = i - t; // Unskew the cell origin back to (x,y,z) space
136 double Y0 = j - t;
137 double Z0 = k - t;
138 double x0 = xin - X0; // The x,y,z distances from the cell origin
139 double y0 = yin - Y0;
140 double z0 = zin - Z0;
141
142 // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
143
144 // Determine which simplex we are in.
145 int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
146 int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
147 if (x0 >= y0) {
148 if (y0 >= z0) {
149 i1 = 1;
150 j1 = 0;
151 k1 = 0;
152 i2 = 1;
153 j2 = 1;
154 k2 = 0;
155 } // X Y Z order
156 else if (x0 >= z0) {
157 i1 = 1;
158 j1 = 0;
159 k1 = 0;
160 i2 = 1;
161 j2 = 0;
162 k2 = 1;
163 } // X Z Y order
164 else {
165 i1 = 0;
166 j1 = 0;
167 k1 = 1;
168 i2 = 1;
169 j2 = 0;
170 k2 = 1;
171 } // Z X Y order
172 } else { // x0<y0
173 if (y0 < z0) {
174 i1 = 0;
175 j1 = 0;
176 k1 = 1;
177 i2 = 0;
178 j2 = 1;
179 k2 = 1;
180 } // Z Y X order
181 else if (x0 < z0) {
182 i1 = 0;
183 j1 = 1;
184 k1 = 0;
185 i2 = 0;
186 j2 = 1;
187 k2 = 1;
188 } // Y Z X order
189 else {
190 i1 = 0;
191 j1 = 1;
192 k1 = 0;
193 i2 = 1;
194 j2 = 1;
195 k2 = 0;
196 } // Y X Z order
197 }
198
199 // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
200 // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
201 // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
202 // c = 1/6.
203 double x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
204 double y1 = y0 - j1 + G3;
205 double z1 = z0 - k1 + G3;
206 double x2 = x0 - i2 + 2.0 * G3; // Offsets for third corner in (x,y,z) coords
207 double y2 = y0 - j2 + 2.0 * G3;
208 double z2 = z0 - k2 + 2.0 * G3;
209 double x3 = x0 - 1.0 + 3.0 * G3; // Offsets for last corner in (x,y,z) coords
210 double y3 = y0 - 1.0 + 3.0 * G3;
211 double z3 = z0 - 1.0 + 3.0 * G3;
212
213 // Work out the hashed gradient indices of the four simplex corners
214 int ii = i & 255;
215 int jj = j & 255;
216 int kk = k & 255;
217 int gi0 = perm[ii + perm[jj + perm[kk]]] % 12;
218 int gi1 = perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]] % 12;
219 int gi2 = perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]] % 12;
220 int gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]] % 12;
221
222 // Calculate the contribution from the four corners
223 double t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
224 if (t0 < 0) {
225 n0 = 0.0;
226 } else {
227 t0 *= t0;
228 n0 = t0 * t0 * dot(grad3[gi0], x0, y0, z0);
229 }
230
231 double t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
232 if (t1 < 0) {
233 n1 = 0.0;
234 } else {
235 t1 *= t1;
236 n1 = t1 * t1 * dot(grad3[gi1], x1, y1, z1);
237 }
238
239 double t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
240 if (t2 < 0) {
241 n2 = 0.0;
242 } else {
243 t2 *= t2;
244 n2 = t2 * t2 * dot(grad3[gi2], x2, y2, z2);
245 }
246
247 double t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
248 if (t3 < 0) {
249 n3 = 0.0;
250 } else {
251 t3 *= t3;
252 n3 = t3 * t3 * dot(grad3[gi3], x3, y3, z3);
253 }
254
255 // Add contributions from each corner to get the final noise value.
256 // The result is scaled to stay just inside [-1,1]
257 return 32.0 * (n0 + n1 + n2 + n3);
258 }
259
260 @Override
261 public double noise(double xin, double yin) {
262 xin += offsetX;
263 yin += offsetY;
264
265 double n0, n1, n2; // Noise contributions from the three corners
266
267 // Skew the input space to determine which simplex cell we're in
268 double s = (xin + yin) * F2; // Hairy factor for 2D
269 int i = floor(xin + s);
270 int j = floor(yin + s);
271 double t = (i + j) * G2;
272 double X0 = i - t; // Unskew the cell origin back to (x,y) space
273 double Y0 = j - t;
274 double x0 = xin - X0; // The x,y distances from the cell origin
275 double y0 = yin - Y0;
276
277 // For the 2D case, the simplex shape is an equilateral triangle.
278
279 // Determine which simplex we are in.
280 int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
281 if (x0 > y0) {
282 i1 = 1;
283 j1 = 0;
284 } // lower triangle, XY order: (0,0)->(1,0)->(1,1)
285 else {
286 i1 = 0;
287 j1 = 1;
288 } // upper triangle, YX order: (0,0)->(0,1)->(1,1)
289
290 // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
291 // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
292 // c = (3-sqrt(3))/6
293
294 double x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
295 double y1 = y0 - j1 + G2;
296 double x2 = x0 + G22; // Offsets for last corner in (x,y) unskewed coords
297 double y2 = y0 + G22;
298
299 // Work out the hashed gradient indices of the three simplex corners
300 int ii = i & 255;
301 int jj = j & 255;
302 int gi0 = perm[ii + perm[jj]] % 12;
303 int gi1 = perm[ii + i1 + perm[jj + j1]] % 12;
304 int gi2 = perm[ii + 1 + perm[jj + 1]] % 12;
305
306 // Calculate the contribution from the three corners
307 double t0 = 0.5 - x0 * x0 - y0 * y0;
308 if (t0 < 0) {
309 n0 = 0.0;
310 } else {
311 t0 *= t0;
312 n0 = t0 * t0 * dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
313 }
314
315 double t1 = 0.5 - x1 * x1 - y1 * y1;
316 if (t1 < 0) {
317 n1 = 0.0;
318 } else {
319 t1 *= t1;
320 n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
321 }
322
323 double t2 = 0.5 - x2 * x2 - y2 * y2;
324 if (t2 < 0) {
325 n2 = 0.0;
326 } else {
327 t2 *= t2;
328 n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
329 }
330
331 // Add contributions from each corner to get the final noise value.
332 // The result is scaled to return values in the interval [-1,1].
333 return 70.0 * (n0 + n1 + n2);
334 }
335
336 /**
337 * Computes and returns the 4D simplex noise for the given coordinates in 4D space
338 *
339 * @param xin X coordinate
340 * @param yin Y coordinate
341 * @param zin Z coordinate
342 * @param win W coordinate
343 * @return Noise at given location, from range -1 to 1
344 */
345 public double noise(double x, double y, double z, double w) {
346 x += offsetX;
347 y += offsetY;
348 z += offsetZ;
349 w += offsetW;
350
351 double n0, n1, n2, n3, n4; // Noise contributions from the five corners
352
353 // Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
354 double s = (x + y + z + w) * F4; // Factor for 4D skewing
355 int i = floor(x + s);
356 int j = floor(y + s);
357 int k = floor(z + s);
358 int l = floor(w + s);
359
360 double t = (i + j + k + l) * G4; // Factor for 4D unskewing
361 double X0 = i - t; // Unskew the cell origin back to (x,y,z,w) space
362 double Y0 = j - t;
363 double Z0 = k - t;
364 double W0 = l - t;
365 double x0 = x - X0; // The x,y,z,w distances from the cell origin
366 double y0 = y - Y0;
367 double z0 = z - Z0;
368 double w0 = w - W0;
369
370 // For the 4D case, the simplex is a 4D shape I won't even try to describe.
371 // To find out which of the 24 possible simplices we're in, we need to
372 // determine the magnitude ordering of x0, y0, z0 and w0.
373 // The method below is a good way of finding the ordering of x,y,z,w and
374 // then find the correct traversal order for the simplex we’re in.
375 // First, six pair-wise comparisons are performed between each possible pair
376 // of the four coordinates, and the results are used to add up binary bits
377 // for an integer index.
378 int c1 = (x0 > y0) ? 32 : 0;
379 int c2 = (x0 > z0) ? 16 : 0;
380 int c3 = (y0 > z0) ? 8 : 0;
381 int c4 = (x0 > w0) ? 4 : 0;
382 int c5 = (y0 > w0) ? 2 : 0;
383 int c6 = (z0 > w0) ? 1 : 0;
384 int c = c1 + c2 + c3 + c4 + c5 + c6;
385 int i1, j1, k1, l1; // The integer offsets for the second simplex corner
386 int i2, j2, k2, l2; // The integer offsets for the third simplex corner
387 int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner
388
389 // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
390 // Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
391 // impossible. Only the 24 indices which have non-zero entries make any sense.
392 // We use a thresholding to set the coordinates in turn from the largest magnitude.
393
394 // The number 3 in the "simplex" array is at the position of the largest coordinate.
395 i1 = simplex[c][0] >= 3 ? 1 : 0;
396 j1 = simplex[c][1] >= 3 ? 1 : 0;
397 k1 = simplex[c][2] >= 3 ? 1 : 0;
398 l1 = simplex[c][3] >= 3 ? 1 : 0;
399
400 // The number 2 in the "simplex" array is at the second largest coordinate.
401 i2 = simplex[c][0] >= 2 ? 1 : 0;
402 j2 = simplex[c][1] >= 2 ? 1 : 0;
403 k2 = simplex[c][2] >= 2 ? 1 : 0;
404 l2 = simplex[c][3] >= 2 ? 1 : 0;
405
406 // The number 1 in the "simplex" array is at the second smallest coordinate.
407 i3 = simplex[c][0] >= 1 ? 1 : 0;
408 j3 = simplex[c][1] >= 1 ? 1 : 0;
409 k3 = simplex[c][2] >= 1 ? 1 : 0;
410 l3 = simplex[c][3] >= 1 ? 1 : 0;
411
412 // The fifth corner has all coordinate offsets = 1, so no need to look that up.
413
414 double x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w) coords
415 double y1 = y0 - j1 + G4;
416 double z1 = z0 - k1 + G4;
417 double w1 = w0 - l1 + G4;
418
419 double x2 = x0 - i2 + G42; // Offsets for third corner in (x,y,z,w) coords
420 double y2 = y0 - j2 + G42;
421 double z2 = z0 - k2 + G42;
422 double w2 = w0 - l2 + G42;
423
424 double x3 = x0 - i3 + G43; // Offsets for fourth corner in (x,y,z,w) coords
425 double y3 = y0 - j3 + G43;
426 double z3 = z0 - k3 + G43;
427 double w3 = w0 - l3 + G43;
428
429 double x4 = x0 + G44; // Offsets for last corner in (x,y,z,w) coords
430 double y4 = y0 + G44;
431 double z4 = z0 + G44;
432 double w4 = w0 + G44;
433
434 // Work out the hashed gradient indices of the five simplex corners
435 int ii = i & 255;
436 int jj = j & 255;
437 int kk = k & 255;
438 int ll = l & 255;
439
440 int gi0 = perm[ii + perm[jj + perm[kk + perm[ll]]]] % 32;
441 int gi1 = perm[ii + i1 + perm[jj + j1 + perm[kk + k1 + perm[ll + l1]]]] % 32;
442 int gi2 = perm[ii + i2 + perm[jj + j2 + perm[kk + k2 + perm[ll + l2]]]] % 32;
443 int gi3 = perm[ii + i3 + perm[jj + j3 + perm[kk + k3 + perm[ll + l3]]]] % 32;
444 int gi4 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] % 32;
445
446 // Calculate the contribution from the five corners
447 double t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0;
448 if (t0 < 0) {
449 n0 = 0.0;
450 } else {
451 t0 *= t0;
452 n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0);
453 }
454
455 double t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1;
456 if (t1 < 0) {
457 n1 = 0.0;
458 } else {
459 t1 *= t1;
460 n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1);
461 }
462
463 double t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2;
464 if (t2 < 0) {
465 n2 = 0.0;
466 } else {
467 t2 *= t2;
468 n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2);
469 }
470
471 double t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3;
472 if (t3 < 0) {
473 n3 = 0.0;
474 } else {
475 t3 *= t3;
476 n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3);
477 }
478
479 double t4 = 0.6 - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4;
480 if (t4 < 0) {
481 n4 = 0.0;
482 } else {
483 t4 *= t4;
484 n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4);
485 }
486
487 // Sum up and scale the result to cover the range [-1,1]
488 return 27.0 * (n0 + n1 + n2 + n3 + n4);
489 }
490
491 /**
492 * Gets the singleton unseeded instance of this generator
493 *
494 * @return Singleton
495 */
496 public static SimplexNoiseGenerator getInstance() {
497 return instance;
498 }
499}