Debian Codegraph @

vendor/github.com/xyproto/gfx/simplex.go

raw
1 package gfx
2
3 import (
4 "math"
5 "math/rand"
6 )
7
8 // SimplexNoise is a speed-improved simplex noise algorithm for 2D, 3D and 4D.
9 //
10 // Based on example code by Stefan Gustavson ([email protected]).
11 // Optimisations by Peter Eastman ([email protected]).
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 type SimplexNoise struct {
22 perm []uint8
23 permMod12 []uint8
24 }
25
26 // NewSimplexNoise creates a new simplex noise instance with the given seed
27 func NewSimplexNoise(seed int64) *SimplexNoise {
28 var p [256]uint8
29
30 perm := make([]uint8, 512)
31 permMod12 := make([]uint8, 512)
32
33 for i := 0; i < 256; i++ {
34 p[i] = uint8(i)
35 }
36
37 r := rand.New(rand.NewSource(seed))
38
39 for i := 0; i < 256; i++ {
40 si := r.Int() & 255
41 p[i], p[si] = p[si], p[i]
42 }
43
44 // To remove the need for index wrapping, double the permutation table length
45
46 for i := 0; i < 512; i++ {
47 perm[i] = p[i&255]
48 permMod12[i] = perm[i] % 12
49 }
50
51 return &SimplexNoise{perm, permMod12}
52 }
53
54 // Noise2D performs 2D simplex noise
55 func (sn *SimplexNoise) Noise2D(xin, yin float64) float64 {
56 var n0, n1, n2 float64 // Noise contributions from the three corners
57
58 // Skew the input space to determine which simplex cell we're in
59 s := (xin + yin) * f2 // Hairy factor for 2D
60 i := fastfloor(xin + s)
61 j := fastfloor(yin + s)
62 t := float64(i+j) * g2
63 X0 := float64(i) - t // Unskew the cell origin back to (x,y) space
64 Y0 := float64(j) - t
65 x0 := xin - X0 // The x,y distances from the cell origin
66 y0 := yin - Y0
67
68 // For the 2D case, the simplex shape is an equilateral triangle.
69 // Determine which simplex we are in.
70 var i1, j1 int32 // Offsets for second (middle) corner of simplex in (i,j) coords
71
72 if x0 > y0 {
73 i1, j1 = 1, 0 // lower triangle, XY order: (0,0)->(1,0)->(1,1)
74 } else {
75 i1, j1 = 0, 1 // upper triangle, YX order: (0,0)->(0,1)->(1,1)
76 }
77
78 // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
79 // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
80 // c = (3-sqrt(3))/6
81
82 x1 := x0 - float64(i1) + g2 // Offsets for middle corner in (x,y) unskewed coords
83 y1 := y0 - float64(j1) + g2
84 x2 := x0 - 1.0 + 2.0*g2 // Offsets for last corner in (x,y) unskewed coords
85 y2 := y0 - 1.0 + 2.0*g2
86
87 // Work out the hashed gradient indices of the three simplex corners
88 ii := i & 255
89 jj := j & 255
90 perm := sn.perm
91 m12 := sn.permMod12
92 gi0 := m12[ii+int32(perm[jj])]
93 gi1 := m12[ii+i1+int32(perm[jj+j1])]
94 gi2 := m12[ii+1+int32(perm[jj+1])]
95
96 // Calculate the contribution from the three corners
97 t0 := 0.5 - x0*x0 - y0*y0
98
99 if t0 < 0.0 {
100 n0 = 0.0
101 } else {
102 t0 *= t0
103 n0 = t0 * t0 * grad3[gi0].dot2(x0, y0) // (x,y) of grad3 used for 2D gradient
104 }
105
106 t1 := 0.5 - x1*x1 - y1*y1
107
108 if t1 < 0.0 {
109 n1 = 0.0
110 } else {
111 t1 *= t1
112 n1 = t1 * t1 * grad3[gi1].dot2(x1, y1)
113 }
114
115 t2 := 0.5 - x2*x2 - y2*y2
116
117 if t2 < 0.0 {
118 n2 = 0.0
119 } else {
120 t2 *= t2
121 n2 = t2 * t2 * grad3[gi2].dot2(x2, y2)
122 }
123
124 // Add contributions from each corner to get the final noise value.
125 // The result is scaled to return values in the interval [-1,1].
126 return 70.0 * (n0 + n1 + n2)
127 }
128
129 // Noise3D performs 3D simplex noise
130 func (sn *SimplexNoise) Noise3D(xin, yin, zin float64) float64 {
131 var n0, n1, n2, n3 float64 // Noise contributions from the four corners
132
133 // Skew the input space to determine which simplex cell we're in
134 s := (xin + yin + zin) * f3 // Very nice and simple skew factor for 3D
135 i := fastfloor(xin + s)
136 j := fastfloor(yin + s)
137 k := fastfloor(zin + s)
138 t := float64(i+j+k) * g3
139 X0 := float64(i) - t // Unskew the cell origin back to (x,y,z) space
140 Y0 := float64(j) - t
141 Z0 := float64(k) - t
142 x0 := xin - X0 // The x,y,z distances from the cell origin
143 y0 := yin - Y0
144 z0 := zin - Z0
145
146 // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
147 // Determine which simplex we are in.
148 var i1, j1, k1 int32 // Offsets for second corner of simplex in (i,j,k) coords
149 var i2, j2, k2 int32 // Offsets for third corner of simplex in (i,j,k) coords
150
151 if x0 >= y0 {
152 if y0 >= z0 {
153 i1, j1, k1, i2, j2, k2 = 1, 0, 0, 1, 1, 0 // X Y Z order
154 } else if x0 >= z0 {
155 i1, j1, k1, i2, j2, k2 = 1, 0, 0, 1, 0, 1 // X Z Y order
156 } else {
157 i1, j1, k1, i2, j2, k2 = 0, 0, 1, 1, 0, 1 // Z X Y order
158 }
159 } else { // x0<y0
160 if y0 < z0 {
161 i1, j1, k1, i2, j2, k2 = 0, 0, 1, 0, 1, 1 // Z Y X order
162 } else if x0 < z0 {
163 i1, j1, k1, i2, j2, k2 = 0, 1, 0, 0, 1, 1 // Y Z X order
164 } else {
165 i1, j1, k1, i2, j2, k2 = 0, 1, 0, 1, 1, 0 // Y X Z order
166 }
167 }
168
169 // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
170 // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
171 // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
172 // c = 1/6.
173 x1 := x0 - float64(i1) + g3 // Offsets for second corner in (x,y,z) coords
174 y1 := y0 - float64(j1) + g3
175 z1 := z0 - float64(k1) + g3
176 x2 := x0 - float64(i2) + 2.0*g3 // Offsets for third corner in (x,y,z) coords
177 y2 := y0 - float64(j2) + 2.0*g3
178 z2 := z0 - float64(k2) + 2.0*g3
179 x3 := x0 - 1.0 + 3.0*g3 // Offsets for last corner in (x,y,z) coords
180 y3 := y0 - 1.0 + 3.0*g3
181 z3 := z0 - 1.0 + 3.0*g3
182
183 // Work out the hashed gradient indices of the four simplex corners
184 ii := i & 255
185 jj := j & 255
186 kk := k & 255
187 perm := sn.perm
188 m12 := sn.permMod12
189 gi0 := m12[ii+int32(perm[jj+int32(perm[kk])])]
190 gi1 := m12[ii+i1+int32(perm[jj+j1+int32(perm[kk+k1])])]
191 gi2 := m12[ii+i2+int32(perm[jj+j2+int32(perm[kk+k2])])]
192 gi3 := m12[ii+1+int32(perm[jj+1+int32(perm[kk+1])])]
193
194 // Calculate the contribution from the four corners
195 t0 := 0.6 - x0*x0 - y0*y0 - z0*z0
196
197 if t0 < 0.0 {
198 n0 = 0.0
199 } else {
200 t0 *= t0
201 n0 = t0 * t0 * grad3[gi0].dot3(x0, y0, z0)
202 }
203
204 t1 := 0.6 - x1*x1 - y1*y1 - z1*z1
205
206 if t1 < 0.0 {
207 n1 = 0.0
208 } else {
209 t1 *= t1
210 n1 = t1 * t1 * grad3[gi1].dot3(x1, y1, z1)
211 }
212
213 t2 := 0.6 - x2*x2 - y2*y2 - z2*z2
214
215 if t2 < 0.0 {
216 n2 = 0.0
217 } else {
218 t2 *= t2
219 n2 = t2 * t2 * grad3[gi2].dot3(x2, y2, z2)
220 }
221
222 t3 := 0.6 - x3*x3 - y3*y3 - z3*z3
223
224 if t3 < 0.0 {
225 n3 = 0.0
226 } else {
227 t3 *= t3
228 n3 = t3 * t3 * grad3[gi3].dot3(x3, y3, z3)
229 }
230
231 // Add contributions from each corner to get the final noise value.
232 // The result is scaled to stay just inside [-1,1]
233 return 32.0 * (n0 + n1 + n2 + n3)
234 }
235
236 // Noise4D performs 4D simplex noise, better simplex rank ordering method 2012-03-09
237 func (sn *SimplexNoise) Noise4D(x, y, z, w float64) float64 {
238 var n0, n1, n2, n3, n4 float64 // Noise contributions from the five corners
239
240 // Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
241 s := (x + y + z + w) * f4 // Factor for 4D skewing
242 i := fastfloor(x + s)
243 j := fastfloor(y + s)
244 k := fastfloor(z + s)
245 l := fastfloor(w + s)
246 t := float64(i+j+k+l) * g4 // Factor for 4D unskewing
247 X0 := float64(i) - t // Unskew the cell origin back to (x,y,z,w) space
248 Y0 := float64(j) - t
249 Z0 := float64(k) - t
250 W0 := float64(l) - t
251 x0 := x - X0 // The x,y,z,w distances from the cell origin
252 y0 := y - Y0
253 z0 := z - Z0
254 w0 := w - W0
255
256 // For the 4D case, the simplex is a 4D shape I won't even try to describe.
257 // To find out which of the 24 possible simplices we're in, we need to
258 // determine the magnitude ordering of x0, y0, z0 and w0.
259 // Six pair-wise comparisons are performed between each possible pair
260 // of the four coordinates, and the results are used to rank the numbers.
261 rankx := 0
262 ranky := 0
263 rankz := 0
264 rankw := 0
265
266 if x0 > y0 {
267 rankx++
268 } else {
269 ranky++
270 }
271
272 if x0 > z0 {
273 rankx++
274 } else {
275 rankz++
276 }
277
278 if x0 > w0 {
279 rankx++
280 } else {
281 rankw++
282 }
283
284 if y0 > z0 {
285 ranky++
286 } else {
287 rankz++
288 }
289
290 if y0 > w0 {
291 ranky++
292 } else {
293 rankw++
294 }
295
296 if z0 > w0 {
297 rankz++
298 } else {
299 rankw++
300 }
301
302 // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
303 // Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
304 // impossible. Only the 24 indices which have non-zero entries make any sense.
305 // We use a thresholding to set the coordinates in turn from the largest magnitude.
306 // Rank 3 denotes the largest coordinate.
307 i1 := one(rankx >= 3) // The integer offsets for the second simplex corner
308 j1 := one(ranky >= 3)
309 k1 := one(rankz >= 3)
310 l1 := one(rankw >= 3)
311
312 // Rank 2 denotes the second largest coordinate.
313 i2 := one(rankx >= 2) // The integer offsets for the third simplex corner
314 j2 := one(ranky >= 2)
315 k2 := one(rankz >= 2)
316 l2 := one(rankw >= 2)
317
318 // Rank 1 denotes the second smallest coordinate.
319 i3 := one(rankx >= 1) // The integer offsets for the fourth simplex corner
320 j3 := one(ranky >= 1)
321 k3 := one(rankz >= 1)
322 l3 := one(rankw >= 1)
323
324 // The fifth corner has all coordinate offsets = 1, so no need to compute that.
325 x1 := x0 - float64(i1) + g4 // Offsets for second corner in (x,y,z,w) coords
326 y1 := y0 - float64(j1) + g4
327 z1 := z0 - float64(k1) + g4
328 w1 := w0 - float64(l1) + g4
329 x2 := x0 - float64(i2) + 2.0*g4 // Offsets for third corner in (x,y,z,w) coords
330 y2 := y0 - float64(j2) + 2.0*g4
331 z2 := z0 - float64(k2) + 2.0*g4
332 w2 := w0 - float64(l2) + 2.0*g4
333 x3 := x0 - float64(i3) + 3.0*g4 // Offsets for fourth corner in (x,y,z,w) coords
334 y3 := y0 - float64(j3) + 3.0*g4
335 z3 := z0 - float64(k3) + 3.0*g4
336 w3 := w0 - float64(l3) + 3.0*g4
337 x4 := x0 - 1.0 + 4.0*g4 // Offsets for last corner in (x,y,z,w) coords
338 y4 := y0 - 1.0 + 4.0*g4
339 z4 := z0 - 1.0 + 4.0*g4
340 w4 := w0 - 1.0 + 4.0*g4
341
342 // Work out the hashed gradient indices of the five simplex corners
343 ii := i & 255
344 jj := j & 255
345 kk := k & 255
346 ll := l & 255
347 p := sn.perm
348 gi0 := p[ii+int32(p[jj+int32(p[kk+int32(p[ll])])])] % 32
349 gi1 := p[ii+i1+int32(p[jj+j1+int32(p[kk+k1+int32(p[ll+l1])])])] % 32
350 gi2 := p[ii+i2+int32(p[jj+j2+int32(p[kk+k2+int32(p[ll+l2])])])] % 32
351 gi3 := p[ii+i3+int32(p[jj+j3+int32(p[kk+k3+int32(p[ll+l3])])])] % 32
352 gi4 := p[ii+1+int32(p[jj+1+int32(p[kk+1+int32(p[ll+1])])])] % 32
353
354 // Calculate the contribution from the five corners
355 t0 := 0.6 - x0*x0 - y0*y0 - z0*z0 - w0*w0
356
357 if t0 < 0.0 {
358 n0 = 0.0
359 } else {
360 t0 *= t0
361 n0 = t0 * t0 * grad4[gi0].dot4(x0, y0, z0, w0)
362 }
363
364 t1 := 0.6 - x1*x1 - y1*y1 - z1*z1 - w1*w1
365
366 if t1 < 0.0 {
367 n1 = 0.0
368 } else {
369 t1 *= t1
370 n1 = t1 * t1 * grad4[gi1].dot4(x1, y1, z1, w1)
371 }
372
373 t2 := 0.6 - x2*x2 - y2*y2 - z2*z2 - w2*w2
374
375 if t2 < 0.0 {
376 n2 = 0.0
377 } else {
378 t2 *= t2
379 n2 = t2 * t2 * grad4[gi2].dot4(x2, y2, z2, w2)
380 }
381
382 t3 := 0.6 - x3*x3 - y3*y3 - z3*z3 - w3*w3
383
384 if t3 < 0.0 {
385 n3 = 0.0
386 } else {
387 t3 *= t3
388 n3 = t3 * t3 * grad4[gi3].dot4(x3, y3, z3, w3)
389 }
390
391 t4 := 0.6 - x4*x4 - y4*y4 - z4*z4 - w4*w4
392
393 if t4 < 0.0 {
394 n4 = 0.0
395 } else {
396 t4 *= t4
397 n4 = t4 * t4 * grad4[gi4].dot4(x4, y4, z4, w4)
398 }
399
400 // Sum up and scale the result to cover the range [-1,1]
401 return 27.0 * (n0 + n1 + n2 + n3 + n4)
402 }
403
404 func one(x bool) int32 {
405 if x {
406 return 1
407 }
408 return 0
409 }
410
411 func fastfloor(x float64) int32 {
412 if x >= 0.0 {
413 return int32(x)
414 }
415 return int32(x) - 1
416 }
417
418 type grad struct {
419 x float64
420 y float64
421 z float64
422 w float64
423 }
424
425 func (g *grad) dot2(x, y float64) float64 {
426 return g.x*x + g.y*y
427 }
428
429 func (g *grad) dot3(x, y, z float64) float64 {
430 return g.x*x + g.y*y + g.z*z
431 }
432
433 func (g *grad) dot4(x, y, z, w float64) float64 {
434 return g.x*x + g.y*y + g.z*z + g.w*w
435 }
436
437 var grad3 = []grad{
438 {1, 1, 0, 0}, {-1, 1, 0, 0}, {1, -1, 0, 0}, {-1, -1, 0, 0},
439 {1, 0, 1, 0}, {-1, 0, 1, 0}, {1, 0, -1, 0}, {-1, 0, -1, 0},
440 {0, 1, 1, 0}, {0, -1, 1, 0}, {0, 1, -1, 0}, {0, -1, -1, 0},
441 }
442
443 var grad4 = []grad{
444 {0, 1, 1, 1}, {0, 1, 1, -1}, {0, 1, -1, 1}, {0, 1, -1, -1},
445 {0, -1, 1, 1}, {0, -1, 1, -1}, {0, -1, -1, 1}, {0, -1, -1, -1},
446 {1, 0, 1, 1}, {1, 0, 1, -1}, {1, 0, -1, 1}, {1, 0, -1, -1},
447 {-1, 0, 1, 1}, {-1, 0, 1, -1}, {-1, 0, -1, 1}, {-1, 0, -1, -1},
448 {1, 1, 0, 1}, {1, 1, 0, -1}, {1, -1, 0, 1}, {1, -1, 0, -1},
449 {-1, 1, 0, 1}, {-1, 1, 0, -1}, {-1, -1, 0, 1}, {-1, -1, 0, -1},
450 {1, 1, 1, 0}, {1, 1, -1, 0}, {1, -1, 1, 0}, {1, -1, -1, 0},
451 {-1, 1, 1, 0}, {-1, 1, -1, 0}, {-1, -1, 1, 0}, {-1, -1, -1, 0},
452 }
453
454 // Skewing and unskewing factors for 2, 3, and 4 dimensions
455 var (
456 f2 = 0.5 * (math.Sqrt(3.0) - 1.0)
457 g2 = (3.0 - math.Sqrt(3.0)) / 6.0
458 f3 = 1.0 / 3.0
459 g3 = 1.0 / 6.0
460 f4 = (math.Sqrt(5.0) - 1.0) / 4.0
461 g4 = (5.0 - math.Sqrt(5.0)) / 20.0
462 )