1 /*--------------------------------------------------------------------
2 Simplex Noise C++ implementation
3 Based on a public domain code by Stefan Gustavson.
4 (The original header is below)
5 --------------------------------------------------------------------*/
6 /*-- start of the original header --*/
7 /*
8 * A speed-improved simplex noise algorithm for 2D, 3D and 4D in Java.
9 *
10 * Based on example code by Stefan Gustavson (stegu@itn.liu.se).
11 * Optimisations by Peter Eastman (peastman@drizzle.stanford.edu).
12 * Better rank ordering method by Stefan Gustavson in 2012.
13 *
14 * This could be speeded up even further, but it's useful as it is.
15 *
16 * Version 2012-03-09
17 *
18 * This code was placed in the public domain by its original author,
19 * Stefan Gustavson. You may use it as you see fit, but
20 * attribution is appreciated.
21 *
22 */
23 /*-- end of the original header --*/
24
25 #include "iwa_simplexnoise.h"
26
27 #include <math.h> //sqrt
28 #include <iostream>
29
30 namespace {
31 static Grad grad3[] = {Grad(1, 1, 0), Grad(-1, 1, 0), Grad(1, -1, 0),
32 Grad(-1, -1, 0), Grad(1, 0, 1), Grad(-1, 0, 1),
33 Grad(1, 0, -1), Grad(-1, 0, -1), Grad(0, 1, 1),
34 Grad(0, -1, 1), Grad(0, 1, -1), Grad(0, -1, -1)};
35 static Grad grad4[] = {
36 Grad(0, 1, 1, 1), Grad(0, 1, 1, -1), Grad(0, 1, -1, 1),
37 Grad(0, 1, -1, -1), Grad(0, -1, 1, 1), Grad(0, -1, 1, -1),
38 Grad(0, -1, -1, 1), Grad(0, -1, -1, -1), Grad(1, 0, 1, 1),
39 Grad(1, 0, 1, -1), Grad(1, 0, -1, 1), Grad(1, 0, -1, -1),
40 Grad(-1, 0, 1, 1), Grad(-1, 0, 1, -1), Grad(-1, 0, -1, 1),
41 Grad(-1, 0, -1, -1), Grad(1, 1, 0, 1), Grad(1, 1, 0, -1),
42 Grad(1, -1, 0, 1), Grad(1, -1, 0, -1), Grad(-1, 1, 0, 1),
43 Grad(-1, 1, 0, -1), Grad(-1, -1, 0, 1), Grad(-1, -1, 0, -1),
44 Grad(1, 1, 1, 0), Grad(1, 1, -1, 0), Grad(1, -1, 1, 0),
45 Grad(1, -1, -1, 0), Grad(-1, 1, 1, 0), Grad(-1, 1, -1, 0),
46 Grad(-1, -1, 1, 0), Grad(-1, -1, -1, 0)};
47
48 // To remove the need for index wrapping, double the permutation table length
49 static short perm[512] = {
50 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7,
51 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190,
52 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117,
53 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136,
54 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158,
55 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46,
56 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209,
57 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86,
58 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5,
59 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16,
60 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44,
61 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253,
62 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97,
63 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51,
64 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184,
65 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93,
66 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156,
67 180,
68
69 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7,
70 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190,
71 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117,
72 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136,
73 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158,
74 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46,
75 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209,
76 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86,
77 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5,
78 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16,
79 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44,
80 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253,
81 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97,
82 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51,
83 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184,
84 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93,
85 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156,
86 180};
87
88 static short permMod12[512] = {
89 7, 4, 5, 7, 6, 3, 11, 1, 9, 11, 0, 5, 2, 5, 7, 9, 8, 0, 7,
90 6, 9, 10, 8, 3, 1, 0, 9, 10, 11, 10, 6, 4, 7, 0, 6, 3, 0, 2,
91 5, 2, 10, 0, 3, 11, 9, 11, 11, 8, 9, 9, 9, 4, 9, 5, 8, 3, 6,
92 8, 5, 4, 3, 0, 8, 7, 2, 9, 11, 2, 7, 0, 3, 10, 5, 2, 2, 3,
93 11, 3, 1, 2, 0, 7, 1, 2, 4, 9, 8, 5, 7, 10, 5, 4, 4, 6, 11,
94 6, 5, 1, 3, 5, 1, 0, 8, 1, 5, 4, 0, 7, 4, 5, 6, 1, 8, 4,
95 3, 10, 8, 8, 3, 2, 8, 4, 1, 6, 5, 6, 3, 4, 4, 1, 10, 10, 4,
96 3, 5, 10, 2, 3, 10, 6, 3, 10, 1, 8, 3, 2, 11, 11, 11, 4, 10, 5,
97 2, 9, 4, 6, 7, 3, 2, 9, 11, 8, 8, 2, 8, 10, 7, 10, 5, 9, 5,
98 11, 11, 7, 4, 9, 9, 10, 3, 1, 7, 2, 0, 2, 7, 5, 8, 4, 10, 5,
99 4, 8, 2, 6, 1, 0, 11, 10, 2, 1, 10, 6, 0, 0, 11, 11, 6, 1, 9,
100 3, 1, 7, 9, 2, 11, 11, 1, 0, 10, 7, 1, 7, 10, 1, 4, 0, 0, 8,
101 7, 1, 2, 9, 7, 4, 6, 2, 6, 8, 1, 9, 6, 6, 7, 5, 0, 0, 3,
102 9, 8, 3, 6, 6, 11, 1, 0, 0,
103
104 7, 4, 5, 7, 6, 3, 11, 1, 9, 11, 0, 5, 2, 5, 7, 9, 8, 0, 7,
105 6, 9, 10, 8, 3, 1, 0, 9, 10, 11, 10, 6, 4, 7, 0, 6, 3, 0, 2,
106 5, 2, 10, 0, 3, 11, 9, 11, 11, 8, 9, 9, 9, 4, 9, 5, 8, 3, 6,
107 8, 5, 4, 3, 0, 8, 7, 2, 9, 11, 2, 7, 0, 3, 10, 5, 2, 2, 3,
108 11, 3, 1, 2, 0, 7, 1, 2, 4, 9, 8, 5, 7, 10, 5, 4, 4, 6, 11,
109 6, 5, 1, 3, 5, 1, 0, 8, 1, 5, 4, 0, 7, 4, 5, 6, 1, 8, 4,
110 3, 10, 8, 8, 3, 2, 8, 4, 1, 6, 5, 6, 3, 4, 4, 1, 10, 10, 4,
111 3, 5, 10, 2, 3, 10, 6, 3, 10, 1, 8, 3, 2, 11, 11, 11, 4, 10, 5,
112 2, 9, 4, 6, 7, 3, 2, 9, 11, 8, 8, 2, 8, 10, 7, 10, 5, 9, 5,
113 11, 11, 7, 4, 9, 9, 10, 3, 1, 7, 2, 0, 2, 7, 5, 8, 4, 10, 5,
114 4, 8, 2, 6, 1, 0, 11, 10, 2, 1, 10, 6, 0, 0, 11, 11, 6, 1, 9,
115 3, 1, 7, 9, 2, 11, 11, 1, 0, 10, 7, 1, 7, 10, 1, 4, 0, 0, 8,
116 7, 1, 2, 9, 7, 4, 6, 2, 6, 8, 1, 9, 6, 6, 7, 5, 0, 0, 3,
117 9, 8, 3, 6, 6, 11, 1, 0, 0};
118 };
119
120 //----------------------------------------
121 // 2D simplex noise
122 //----------------------------------------
noise(double xin,double yin)123 double SimplexNoise::noise(double xin, double yin) {
124 // Skewing and unskewing factors for 2 dimensions
125 static const double F2 = 0.5 * (sqrt(3.0) - 1.0);
126 static const double G2 = (3.0 - sqrt(3.0)) / 6.0;
127
128 double n0, n1, n2; // Noise contributions from the three corners
129 // Skew the input space to determine which simplex cell we're in
130 double s = (xin + yin) * F2; // Hairy factor for 2D
131 int i = fastfloor(xin + s);
132 int j = fastfloor(yin + s);
133 double t = (i + j) * G2;
134 double X0 = i - t; // Unskew the cell origin back to (x,y) space
135 double Y0 = j - t;
136 double x0 = xin - X0; // The x,y distances from the cell origin
137 double y0 = yin - Y0;
138
139 // For the 2D case, the simplex shape is an equilateral triangle.
140 // Determine which simplex we are in.
141 int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
142 if (x0 > y0) {
143 i1 = 1;
144 j1 = 0;
145 } // lower triangle, XY order: (0,0)->(1,0)->(1,1)
146 else {
147 i1 = 0;
148 j1 = 1;
149 } // upper triangle, YX order: (0,0)->(0,1)->(1,1)
150
151 // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
152 // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
153 // c = (3-sqrt(3))/6
154 double x1 =
155 x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
156 double y1 = y0 - j1 + G2;
157 double x2 =
158 x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords
159 double y2 = y0 - 1.0 + 2.0 * G2;
160
161 // Work out the hashed gradient indices of the three simplex corners
162 int ii = i & 255;
163 int jj = j & 255;
164 int gi0 = permMod12[ii + perm[jj]];
165 int gi1 = permMod12[ii + i1 + perm[jj + j1]];
166 int gi2 = permMod12[ii + 1 + perm[jj + 1]];
167
168 // Calculate the contribution from the three corners
169 double t0 = 0.5 - x0 * x0 - y0 * y0;
170 if (t0 < 0)
171 n0 = 0.0;
172 else {
173 t0 *= t0;
174 n0 = t0 * t0 *
175 dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
176 }
177 double t1 = 0.5 - x1 * x1 - y1 * y1;
178 if (t1 < 0)
179 n1 = 0.0;
180 else {
181 t1 *= t1;
182 n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
183 }
184 double t2 = 0.5 - x2 * x2 - y2 * y2;
185 if (t2 < 0)
186 n2 = 0.0;
187 else {
188 t2 *= t2;
189 n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
190 }
191
192 // Add contributions from each corner to get the final noise value.
193 // The result is scaled to return values in the interval [-1,1].
194 return 70.0 * (n0 + n1 + n2);
195 }
196
197 //----------------------------------------
198 // 3D simplex noise
199 //----------------------------------------
noise(double xin,double yin,double zin)200 double SimplexNoise::noise(double xin, double yin, double zin) {
201 // Skewing and unskewing factors for 3 dimensions
202 static const double F3 = 1.0 / 3.0;
203 static const double G3 = 1.0 / 6.0;
204
205 double n0, n1, n2, n3; // Noise contributions from the four corners
206 // Skew the input space to determine which simplex cell we're in
207 double s = (xin + yin + zin) * F3; // Very nice and simple skew factor for 3D
208 int i = fastfloor(xin + s);
209 int j = fastfloor(yin + s);
210 int k = fastfloor(zin + s);
211 double t = (i + j + k) * G3;
212 double X0 = i - t; // Unskew the cell origin back to (x,y,z) space
213 double Y0 = j - t;
214 double Z0 = k - t;
215 double x0 = xin - X0; // The x,y,z distances from the cell origin
216 double y0 = yin - Y0;
217 double z0 = zin - Z0;
218
219 // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
220 // Determine which simplex we are in.
221 int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
222 int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
223 if (x0 >= y0) {
224 if (y0 >= z0) {
225 i1 = 1;
226 j1 = 0;
227 k1 = 0;
228 i2 = 1;
229 j2 = 1;
230 k2 = 0;
231 } // X Y Z order
232 else if (x0 >= z0) {
233 i1 = 1;
234 j1 = 0;
235 k1 = 0;
236 i2 = 1;
237 j2 = 0;
238 k2 = 1;
239 } // X Z Y order
240 else {
241 i1 = 0;
242 j1 = 0;
243 k1 = 1;
244 i2 = 1;
245 j2 = 0;
246 k2 = 1;
247 } // Z X Y order
248 } else { // x0<y0
249 if (y0 < z0) {
250 i1 = 0;
251 j1 = 0;
252 k1 = 1;
253 i2 = 0;
254 j2 = 1;
255 k2 = 1;
256 } // Z Y X order
257 else if (x0 < z0) {
258 i1 = 0;
259 j1 = 1;
260 k1 = 0;
261 i2 = 0;
262 j2 = 1;
263 k2 = 1;
264 } // Y Z X order
265 else {
266 i1 = 0;
267 j1 = 1;
268 k1 = 0;
269 i2 = 1;
270 j2 = 1;
271 k2 = 0;
272 } // Y X Z order
273 }
274
275 // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
276 // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
277 // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
278 // c = 1/6.
279 double x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
280 double y1 = y0 - j1 + G3;
281 double z1 = z0 - k1 + G3;
282 double x2 = x0 - i2 + 2.0 * G3; // Offsets for third corner in (x,y,z) coords
283 double y2 = y0 - j2 + 2.0 * G3;
284 double z2 = z0 - k2 + 2.0 * G3;
285 double x3 = x0 - 1.0 + 3.0 * G3; // Offsets for last corner in (x,y,z) coords
286 double y3 = y0 - 1.0 + 3.0 * G3;
287 double z3 = z0 - 1.0 + 3.0 * G3;
288
289 // Work out the hashed gradient indices of the four simplex corners
290 int ii = i & 255;
291 int jj = j & 255;
292 int kk = k & 255;
293 int gi0 = permMod12[ii + perm[jj + perm[kk]]];
294 int gi1 = permMod12[ii + i1 + perm[jj + j1 + perm[kk + k1]]];
295 int gi2 = permMod12[ii + i2 + perm[jj + j2 + perm[kk + k2]]];
296 int gi3 = permMod12[ii + 1 + perm[jj + 1 + perm[kk + 1]]];
297
298 // Calculate the contribution from the four corners
299 double range = 0.6;
300 double t0 = range - x0 * x0 - y0 * y0 - z0 * z0;
301 if (t0 < 0)
302 n0 = 0.0;
303 else {
304 t0 *= t0;
305 n0 = t0 * t0 * dot(grad3[gi0], x0, y0, z0);
306 }
307 double t1 = range - x1 * x1 - y1 * y1 - z1 * z1;
308 if (t1 < 0)
309 n1 = 0.0;
310 else {
311 t1 *= t1;
312 n1 = t1 * t1 * dot(grad3[gi1], x1, y1, z1);
313 }
314 double t2 = range - x2 * x2 - y2 * y2 - z2 * z2;
315 if (t2 < 0)
316 n2 = 0.0;
317 else {
318 t2 *= t2;
319 n2 = t2 * t2 * dot(grad3[gi2], x2, y2, z2);
320 }
321 double t3 = range - x3 * x3 - y3 * y3 - z3 * z3;
322 if (t3 < 0)
323 n3 = 0.0;
324 else {
325 t3 *= t3;
326 n3 = t3 * t3 * dot(grad3[gi3], x3, y3, z3);
327 }
328 // Add contributions from each corner to get the final noise value.
329 // The result is scaled to stay just inside [-1,1]
330 /*- 変更: [-0.5,0.5] の範囲にする -*/
331 return 16.0 * (n0 + n1 + n2 + n3);
332 // return 32.0*(n0 + n1 + n2 + n3);
333 }
334
335 //----------------------------------------
336 // 4D simplex noise
337 //----------------------------------------
noise(double x,double y,double z,double w)338 double SimplexNoise::noise(double x, double y, double z, double w) {
339 // Skewing and unskewing factors for 4 dimensions
340 static const double F4 = (sqrt(5.0) - 1.0) / 4.0;
341 static const double G4 = (5.0 - sqrt(5.0)) / 20.0;
342
343 // The skewing and unskewing factors are hairy again for the 4D case
344 double n0, n1, n2, n3, n4; // Noise contributions from the five corners
345
346 // Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
347 double s = (x + y + z + w) * F4; // Factor for 4D skewing
348 int i = fastfloor(x + s);
349 int j = fastfloor(y + s);
350 int k = fastfloor(z + s);
351 int l = fastfloor(w + s);
352 double t = (i + j + k + l) * G4; // Factor for 4D unskewing
353 double X0 = i - t; // Unskew the cell origin back to (x,y,z,w) space
354 double Y0 = j - t;
355 double Z0 = k - t;
356 double W0 = l - t;
357 double x0 = x - X0; // The x,y,z,w distances from the cell origin
358 double y0 = y - Y0;
359 double z0 = z - Z0;
360 double w0 = w - W0;
361
362 // For the 4D case, the simplex is a 4D shape I won't even try to describe.
363 // To find out which of the 24 possible simplices we're in, we need to
364 // determine the magnitude ordering of x0, y0, z0 and w0.
365 // Six pair-wise comparisons are performed between each possible pair
366 // of the four coordinates, and the results are used to rank the numbers.
367 int rankx = 0;
368 int ranky = 0;
369 int rankz = 0;
370 int rankw = 0;
371 if (x0 > y0)
372 rankx++;
373 else
374 ranky++;
375 if (x0 > z0)
376 rankx++;
377 else
378 rankz++;
379 if (x0 > w0)
380 rankx++;
381 else
382 rankw++;
383 if (y0 > z0)
384 ranky++;
385 else
386 rankz++;
387 if (y0 > w0)
388 ranky++;
389 else
390 rankw++;
391 if (z0 > w0)
392 rankz++;
393 else
394 rankw++;
395 int i1, j1, k1, l1; // The integer offsets for the second simplex corner
396 int i2, j2, k2, l2; // The integer offsets for the third simplex corner
397 int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner
398
399 // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
400 // Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and
401 // x<w
402 // impossible. Only the 24 indices which have non-zero entries make any sense.
403 // We use a thresholding to set the coordinates in turn from the largest
404 // magnitude.
405 // Rank 3 denotes the largest coordinate.
406 i1 = rankx >= 3 ? 1 : 0;
407 j1 = ranky >= 3 ? 1 : 0;
408 k1 = rankz >= 3 ? 1 : 0;
409 l1 = rankw >= 3 ? 1 : 0;
410 // Rank 2 denotes the second largest coordinate.
411 i2 = rankx >= 2 ? 1 : 0;
412 j2 = ranky >= 2 ? 1 : 0;
413 k2 = rankz >= 2 ? 1 : 0;
414 l2 = rankw >= 2 ? 1 : 0;
415 // Rank 1 denotes the second smallest coordinate.
416 i3 = rankx >= 1 ? 1 : 0;
417 j3 = ranky >= 1 ? 1 : 0;
418 k3 = rankz >= 1 ? 1 : 0;
419 l3 = rankw >= 1 ? 1 : 0;
420
421 // The fifth corner has all coordinate offsets = 1, so no need to look that
422 // up.
423 double x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w) coords
424 double y1 = y0 - j1 + G4;
425 double z1 = z0 - k1 + G4;
426 double w1 = w0 - l1 + G4;
427 double x2 =
428 x0 - i2 + 2.0 * G4; // Offsets for third corner in (x,y,z,w) coords
429 double y2 = y0 - j2 + 2.0 * G4;
430 double z2 = z0 - k2 + 2.0 * G4;
431 double w2 = w0 - l2 + 2.0 * G4;
432 double x3 =
433 x0 - i3 + 3.0 * G4; // Offsets for fourth corner in (x,y,z,w) coords
434 double y3 = y0 - j3 + 3.0 * G4;
435 double z3 = z0 - k3 + 3.0 * G4;
436 double w3 = w0 - l3 + 3.0 * G4;
437 double x4 =
438 x0 - 1.0 + 4.0 * G4; // Offsets for last corner in (x,y,z,w) coords
439 double y4 = y0 - 1.0 + 4.0 * G4;
440 double z4 = z0 - 1.0 + 4.0 * G4;
441 double w4 = w0 - 1.0 + 4.0 * G4;
442
443 // Work out the hashed gradient indices of the five simplex corners
444 int ii = i & 255;
445 int jj = j & 255;
446 int kk = k & 255;
447 int ll = l & 255;
448 int gi0 = perm[ii + perm[jj + perm[kk + perm[ll]]]] % 32;
449 int gi1 = perm[ii + i1 + perm[jj + j1 + perm[kk + k1 + perm[ll + l1]]]] % 32;
450 int gi2 = perm[ii + i2 + perm[jj + j2 + perm[kk + k2 + perm[ll + l2]]]] % 32;
451 int gi3 = perm[ii + i3 + perm[jj + j3 + perm[kk + k3 + perm[ll + l3]]]] % 32;
452 int gi4 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] % 32;
453
454 /*- パラメータ調整 -*/
455 double range = 0.66;
456 // double range = 0.6;
457
458 // Calculate the contribution from the five corners
459 double t0 = range - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0;
460 if (t0 < 0)
461 n0 = 0.0;
462 else {
463 t0 *= t0;
464 n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0);
465 }
466 double t1 = range - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1;
467 if (t1 < 0)
468 n1 = 0.0;
469 else {
470 t1 *= t1;
471 n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1);
472 }
473 double t2 = range - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2;
474 if (t2 < 0)
475 n2 = 0.0;
476 else {
477 t2 *= t2;
478 n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2);
479 }
480 double t3 = range - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3;
481 if (t3 < 0)
482 n3 = 0.0;
483 else {
484 t3 *= t3;
485 n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3);
486 }
487 double t4 = range - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4;
488 if (t4 < 0)
489 n4 = 0.0;
490 else {
491 t4 *= t4;
492 n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4);
493 }
494
495 // Sum up and scale the result to cover the range [-1,1]
496 return 27.0 * (n0 + n1 + n2 + n3 + n4);
497 }
498
499 /*----------------------------------------
500 セルまたぎを防ぐために、現在の所属セルを得る
501 ----------------------------------------*/
getCellIds(double xin,double yin,double zin)502 CellIds SimplexNoise::getCellIds(double xin, double yin, double zin) {
503 // Skew the input space to determine which simplex cell we're in
504 const double F3 = 1.0 / 3.0;
505 double s = (xin + yin + zin) * F3; // Very nice and simple skew factor for 3D
506 int i = fastfloor(xin + s);
507 int j = fastfloor(yin + s);
508 int k = fastfloor(zin + s);
509 const double G3 = 1.0 / 6.0; // Very nice and simple unskew factor, too
510 double t = (i + j + k) * G3;
511 double X0 = i - t; // Unskew the cell origin back to (x,y,z) space
512 double Y0 = j - t;
513 double Z0 = k - t;
514 double x0 = xin - X0; // The x,y,z distances from the cell origin
515 double y0 = yin - Y0;
516 double z0 = zin - Z0;
517 // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
518 // Determine which simplex we are in.
519 int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
520 int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
521 if (x0 >= y0) {
522 if (y0 >= z0) {
523 i1 = 1;
524 j1 = 0;
525 k1 = 0;
526 i2 = 1;
527 j2 = 1;
528 k2 = 0;
529 } // X Y Z order
530 else if (x0 >= z0) {
531 i1 = 1;
532 j1 = 0;
533 k1 = 0;
534 i2 = 1;
535 j2 = 0;
536 k2 = 1;
537 } // X Z Y order
538 else {
539 i1 = 0;
540 j1 = 0;
541 k1 = 1;
542 i2 = 1;
543 j2 = 0;
544 k2 = 1;
545 } // Z X Y order
546 } else { // x0<y0
547 if (y0 < z0) {
548 i1 = 0;
549 j1 = 0;
550 k1 = 1;
551 i2 = 0;
552 j2 = 1;
553 k2 = 1;
554 } // Z Y X order
555 else if (x0 < z0) {
556 i1 = 0;
557 j1 = 1;
558 k1 = 0;
559 i2 = 0;
560 j2 = 1;
561 k2 = 1;
562 } // Y Z X order
563 else {
564 i1 = 0;
565 j1 = 1;
566 k1 = 0;
567 i2 = 1;
568 j2 = 1;
569 k2 = 0;
570 } // Y X Z order
571 }
572 return CellIds(i, j, k, i1, j1, k1, i2, j2, k2);
573 }