• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

google / vector_math.dart / 13087264509

01 Feb 2025 09:20AM UTC coverage: 26.372%. Remained the same
13087264509

Pull #339

github

web-flow
Merge 4cc64e59c into bd4b574b2
Pull Request #339: Bump the github-actions group with 2 updates

4325 of 16400 relevant lines covered (26.37%)

1.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lib/src/vector_math_64/noise.dart
1
/*
2
  Copyright (C) 2013 Andrew Magill
3

4
  This software is provided 'as-is', without any express or implied
5
  warranty.  In no event will the authors be held liable for any damages
6
  arising from the use of this software.
7

8
  Permission is granted to anyone to use this software for any purpose,
9
  including commercial applications, and to alter it and redistribute it
10
  freely, subject to the following restrictions:
11

12
  1. The origin of this software must not be misrepresented; you must not
13
     claim that you wrote the original software. If you use this software
14
     in a product, an acknowledgment in the product documentation would be
15
     appreciated but is not required.
16
  2. Altered source versions must be plainly marked as such, and must not be
17
     misrepresented as being the original software.
18
  3. This notice may not be removed or altered from any source distribution.
19

20
*/
21

22
part of '../../vector_math_64.dart';
23

24
/*
25
 * This is based on the implementation of Simplex Noise by Stefan Gustavson
26
 * found at: http://webstaff.itn.liu.se/~stegu/simplexnoise/SimplexNoise.java
27
 */
28
@Deprecated('This API will be removed '
29
    '(see https:github.com/google/vector_math.dart/issues/270)')
30
class SimplexNoise {
31
  static final List<List<double>> _grad3 = <List<double>>[
×
32
    <double>[1.0, 1.0, 0.0],
×
33
    <double>[-1.0, 1.0, 0.0],
×
34
    <double>[1.0, -1.0, 0.0],
×
35
    <double>[-1.0, -1.0, 0.0],
×
36
    <double>[1.0, 0.0, 1.0],
×
37
    <double>[-1.0, 0.0, 1.0],
×
38
    <double>[1.0, 0.0, -1.0],
×
39
    <double>[-1.0, 0.0, -1.0],
×
40
    <double>[0.0, 1.0, 1.0],
×
41
    <double>[0.0, -1.0, 1.0],
×
42
    <double>[0.0, 1.0, -1.0],
×
43
    <double>[0.0, -1.0, -1.0]
×
44
  ];
45

46
  static final List<List<double>> _grad4 = <List<double>>[
×
47
    <double>[0.0, 1.0, 1.0, 1.0],
×
48
    <double>[0.0, 1.0, 1.0, -1.0],
×
49
    <double>[0.0, 1.0, -1.0, 1.0],
×
50
    <double>[0.0, 1.0, -1.0, -1.0],
×
51
    <double>[0.0, -1.0, 1.0, 1.0],
×
52
    <double>[0.0, -1.0, 1.0, -1.0],
×
53
    <double>[0.0, -1.0, -1.0, 1.0],
×
54
    <double>[0.0, -1.0, -1.0, -1.0],
×
55
    <double>[1.0, 0.0, 1.0, 1.0],
×
56
    <double>[1.0, 0.0, 1.0, -1.0],
×
57
    <double>[1.0, 0.0, -1.0, 1.0],
×
58
    <double>[1.0, 0.0, -1.0, -1.0],
×
59
    <double>[-1.0, 0.0, 1.0, 1.0],
×
60
    <double>[-1.0, 0.0, 1.0, -1.0],
×
61
    <double>[-1.0, 0.0, -1.0, 1.0],
×
62
    <double>[-1.0, 0.0, -1.0, -1.0],
×
63
    <double>[1.0, 1.0, 0.0, 1.0],
×
64
    <double>[1.0, 1.0, 0.0, -1.0],
×
65
    <double>[1.0, -1.0, 0.0, 1.0],
×
66
    <double>[1.0, -1.0, 0.0, -1.0],
×
67
    <double>[-1.0, 1.0, 0.0, 1.0],
×
68
    <double>[-1.0, 1.0, 0.0, -1.0],
×
69
    <double>[-1.0, -1.0, 0.0, 1.0],
×
70
    <double>[-1.0, -1.0, 0.0, -1.0],
×
71
    <double>[1.0, 1.0, 1.0, 0.0],
×
72
    <double>[1.0, 1.0, -1.0, 0.0],
×
73
    <double>[1.0, -1.0, 1.0, 0.0],
×
74
    <double>[1.0, -1.0, -1.0, 0.0],
×
75
    <double>[-1.0, 1.0, 1.0, 0.0],
×
76
    <double>[-1.0, 1.0, -1.0, 0.0],
×
77
    <double>[-1.0, -1.0, 1.0, 0.0],
×
78
    <double>[-1.0, -1.0, -1.0, 0.0]
×
79
  ];
80

81
  // To remove the need for index wrapping, double the permutation table length
82
  late final List<int> _perm;
83
  late final List<int> _permMod12;
84

85
  // Skewing and unskewing factors for 2, 3, and 4 dimensions
86
  static final _F2 = 0.5 * (math.sqrt(3.0) - 1.0);
×
87
  static final _G2 = (3.0 - math.sqrt(3.0)) / 6.0;
×
88
  static const double _f3 = 1.0 / 3.0;
89
  static const double _g3 = 1.0 / 6.0;
90
  static final _F4 = (math.sqrt(5.0) - 1.0) / 4.0;
×
91
  static final _G4 = (5.0 - math.sqrt(5.0)) / 20.0;
×
92

93
  double _dot2(List<double> g, double x, double y) => g[0] * x + g[1] * y;
×
94

95
  double _dot3(List<double> g, double x, double y, double z) =>
×
96
      g[0] * x + g[1] * y + g[2] * z;
×
97

98
  double _dot4(List<double> g, double x, double y, double z, double w) =>
×
99
      g[0] * x + g[1] * y + g[2] * z + g[3] * w;
×
100

101
  SimplexNoise([math.Random? r]) {
×
102
    r ??= math.Random();
×
103
    final p = List<int>.generate(256, (_) => r!.nextInt(256), growable: false);
×
104
    _perm = List<int>.generate(p.length * 2, (int i) => p[i % p.length],
×
105
        growable: false);
106
    _permMod12 = List<int>.generate(_perm.length, (int i) => _perm[i] % 12,
×
107
        growable: false);
108
  }
109

110
  double noise2D(double xin, double yin) {
×
111
    double n0, n1, n2; // Noise contributions from the three corners
112
    // Skew the input space to determine which simplex cell we're in
113
    final s = (xin + yin) * _F2; // Hairy factor for 2D
×
114
    final i = (xin + s).floor();
×
115
    final j = (yin + s).floor();
×
116
    final t = (i + j) * _G2;
×
117
    final X0 = i - t; // Unskew the cell origin back to (x,y) space
×
118
    final Y0 = j - t;
×
119
    final x0 = xin - X0; // The x,y distances from the cell origin
×
120
    final y0 = yin - Y0;
×
121
    // For the 2D case, the simplex shape is an equilateral triangle.
122
    // Determine which simplex we are in.
123
    int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
124
    if (x0 > y0) {
×
125
      i1 = 1;
126
      j1 = 0;
127
    } // lower triangle, XY order: (0,0)->(1,0)->(1,1)
128
    else {
129
      i1 = 0;
130
      j1 = 1;
131
    } // upper triangle, YX order: (0,0)->(0,1)->(1,1)
132
    // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
133
    // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
134
    // c = (3-sqrt(3))/6
135
    final x1 =
136
        x0 - i1 + _G2; // Offsets for middle corner in (x,y) unskewed coords
×
137
    final y1 = y0 - j1 + _G2;
×
138
    final x2 = x0 -
×
139
        1.0 +
×
140
        2.0 * _G2; // Offsets for last corner in (x,y) unskewed coords
×
141
    final y2 = y0 - 1.0 + 2.0 * _G2;
×
142
    // Work out the hashed gradient indices of the three simplex corners
143
    final ii = i & 255;
×
144
    final jj = j & 255;
×
145
    final gi0 = _permMod12[ii + _perm[jj]];
×
146
    final gi1 = _permMod12[ii + i1 + _perm[jj + j1]];
×
147
    final gi2 = _permMod12[ii + 1 + _perm[jj + 1]];
×
148
    // Calculate the contribution from the three corners
149
    var t0 = 0.5 - x0 * x0 - y0 * y0;
×
150
    if (t0 < 0) {
×
151
      n0 = 0.0;
152
    } else {
153
      t0 *= t0;
×
154
      n0 = t0 *
×
155
          t0 *
×
156
          _dot2(_grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
×
157
    }
158
    var t1 = 0.5 - x1 * x1 - y1 * y1;
×
159
    if (t1 < 0) {
×
160
      n1 = 0.0;
161
    } else {
162
      t1 *= t1;
×
163
      n1 = t1 * t1 * _dot2(_grad3[gi1], x1, y1);
×
164
    }
165
    var t2 = 0.5 - x2 * x2 - y2 * y2;
×
166
    if (t2 < 0) {
×
167
      n2 = 0.0;
168
    } else {
169
      t2 *= t2;
×
170
      n2 = t2 * t2 * _dot2(_grad3[gi2], x2, y2);
×
171
    }
172
    // Add contributions from each corner to get the final noise value.
173
    // The result is scaled to return values in the interval [-1,1].
174
    return 70.0 * (n0 + n1 + n2);
×
175
  }
176

177
  // 3D simplex noise
178
  double noise3D(double xin, double yin, double zin) {
×
179
    double n0, n1, n2, n3; // Noise contributions from the four corners
180
    // Skew the input space to determine which simplex cell we're in
181
    final s =
182
        (xin + yin + zin) * _f3; // Very nice and simple skew factor for 3D
×
183
    final i = (xin + s).floor();
×
184
    final j = (yin + s).floor();
×
185
    final k = (zin + s).floor();
×
186
    final t = (i + j + k) * _g3;
×
187
    final X0 = i - t; // Unskew the cell origin back to (x,y,z) space
×
188
    final Y0 = j - t;
×
189
    final Z0 = k - t;
×
190
    final x0 = xin - X0; // The x,y,z distances from the cell origin
×
191
    final y0 = yin - Y0;
×
192
    final z0 = zin - Z0;
×
193
    // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
194
    // Determine which simplex we are in.
195
    int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
196
    int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
197
    if (x0 >= y0) {
×
198
      if (y0 >= z0) {
×
199
        i1 = 1;
200
        j1 = 0;
201
        k1 = 0;
202
        i2 = 1;
203
        j2 = 1;
204
        k2 = 0;
205
      } // X Y Z order
206
      else if (x0 >= z0) {
×
207
        i1 = 1;
208
        j1 = 0;
209
        k1 = 0;
210
        i2 = 1;
211
        j2 = 0;
212
        k2 = 1;
213
      } // X Z Y order
214
      else {
215
        i1 = 0;
216
        j1 = 0;
217
        k1 = 1;
218
        i2 = 1;
219
        j2 = 0;
220
        k2 = 1;
221
      } // Z X Y order
222
    } else {
223
      // x0<y0
224
      if (y0 < z0) {
×
225
        i1 = 0;
226
        j1 = 0;
227
        k1 = 1;
228
        i2 = 0;
229
        j2 = 1;
230
        k2 = 1;
231
      } // Z Y X order
232
      else if (x0 < z0) {
×
233
        i1 = 0;
234
        j1 = 1;
235
        k1 = 0;
236
        i2 = 0;
237
        j2 = 1;
238
        k2 = 1;
239
      } // Y Z X order
240
      else {
241
        i1 = 0;
242
        j1 = 1;
243
        k1 = 0;
244
        i2 = 1;
245
        j2 = 1;
246
        k2 = 0;
247
      } // Y X Z order
248
    }
249
    // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
250
    // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
251
    // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z),
252
    // where c = 1/6.
253
    final x1 = x0 - i1 + _g3; // Offsets for second corner in (x,y,z) coords
×
254
    final y1 = y0 - j1 + _g3;
×
255
    final z1 = z0 - k1 + _g3;
×
256
    final x2 =
257
        x0 - i2 + 2.0 * _g3; // Offsets for third corner in (x,y,z) coords
×
258
    final y2 = y0 - j2 + 2.0 * _g3;
×
259
    final z2 = z0 - k2 + 2.0 * _g3;
×
260
    final x3 =
261
        x0 - 1.0 + 3.0 * _g3; // Offsets for last corner in (x,y,z) coords
×
262
    final y3 = y0 - 1.0 + 3.0 * _g3;
×
263
    final z3 = z0 - 1.0 + 3.0 * _g3;
×
264
    // Work out the hashed gradient indices of the four simplex corners
265
    final ii = i & 255;
×
266
    final jj = j & 255;
×
267
    final kk = k & 255;
×
268
    final gi0 = _permMod12[ii + _perm[jj + _perm[kk]]];
×
269
    final gi1 = _permMod12[ii + i1 + _perm[jj + j1 + _perm[kk + k1]]];
×
270
    final gi2 = _permMod12[ii + i2 + _perm[jj + j2 + _perm[kk + k2]]];
×
271
    final gi3 = _permMod12[ii + 1 + _perm[jj + 1 + _perm[kk + 1]]];
×
272
    // Calculate the contribution from the four corners
273
    var t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
×
274
    if (t0 < 0) {
×
275
      n0 = 0.0;
276
    } else {
277
      t0 *= t0;
×
278
      n0 = t0 * t0 * _dot3(_grad3[gi0], x0, y0, z0);
×
279
    }
280
    var t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
×
281
    if (t1 < 0) {
×
282
      n1 = 0.0;
283
    } else {
284
      t1 *= t1;
×
285
      n1 = t1 * t1 * _dot3(_grad3[gi1], x1, y1, z1);
×
286
    }
287
    var t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
×
288
    if (t2 < 0) {
×
289
      n2 = 0.0;
290
    } else {
291
      t2 *= t2;
×
292
      n2 = t2 * t2 * _dot3(_grad3[gi2], x2, y2, z2);
×
293
    }
294
    var t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
×
295
    if (t3 < 0) {
×
296
      n3 = 0.0;
297
    } else {
298
      t3 *= t3;
×
299
      n3 = t3 * t3 * _dot3(_grad3[gi3], x3, y3, z3);
×
300
    }
301
    // Add contributions from each corner to get the final noise value.
302
    // The result is scaled to stay just inside [-1,1]
303
    return 32.0 * (n0 + n1 + n2 + n3);
×
304
  }
305

306
  // 4D simplex noise, better simplex rank ordering method 2012-03-09
307
  double noise4D(double x, double y, double z, double w) {
×
308
    double n0, n1, n2, n3, n4; // Noise contributions from the five corners
309
    // Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
310
    final s = (x + y + z + w) * _F4; // Factor for 4D skewing
×
311
    final i = (x + s).floor();
×
312
    final j = (y + s).floor();
×
313
    final k = (z + s).floor();
×
314
    final l = (w + s).floor();
×
315
    final t = (i + j + k + l) * _G4; // Factor for 4D unskewing
×
316
    final X0 = i - t; // Unskew the cell origin back to (x,y,z,w) space
×
317
    final Y0 = j - t;
×
318
    final Z0 = k - t;
×
319
    final W0 = l - t;
×
320
    final x0 = x - X0; // The x,y,z,w distances from the cell origin
×
321
    final y0 = y - Y0;
×
322
    final z0 = z - Z0;
×
323
    final w0 = w - W0;
×
324
    // For the 4D case, the simplex is a 4D shape I won't even try to describe.
325
    // To find out which of the 24 possible simplices we're in, we need to
326
    // determine the magnitude ordering of x0, y0, z0 and w0.
327
    // Six pair-wise comparisons are performed between each possible pair
328
    // of the four coordinates, and the results are used to rank the numbers.
329
    var rankx = 0;
330
    var ranky = 0;
331
    var rankz = 0;
332
    var rankw = 0;
333
    if (x0 > y0) {
×
334
      rankx++;
×
335
    } else {
336
      ranky++;
×
337
    }
338
    if (x0 > z0) {
×
339
      rankx++;
×
340
    } else {
341
      rankz++;
×
342
    }
343
    if (x0 > w0) {
×
344
      rankx++;
×
345
    } else {
346
      rankw++;
×
347
    }
348
    if (y0 > z0) {
×
349
      ranky++;
×
350
    } else {
351
      rankz++;
×
352
    }
353
    if (y0 > w0) {
×
354
      ranky++;
×
355
    } else {
356
      rankw++;
×
357
    }
358
    if (z0 > w0) {
×
359
      rankz++;
×
360
    } else {
361
      rankw++;
×
362
    }
363
    int i1, j1, k1, l1; // The integer offsets for the second simplex corner
364
    int i2, j2, k2, l2; // The integer offsets for the third simplex corner
365
    int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner
366
    // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
367
    // Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and
368
    // x<w impossible. Only the 24 indices which have non-zero entries make any
369
    // sense.
370
    // We use a thresholding to set the coordinates in turn from the largest
371
    // magnitude.
372
    // Rank 3 denotes the largest coordinate.
373
    i1 = rankx >= 3 ? 1 : 0;
×
374
    j1 = ranky >= 3 ? 1 : 0;
×
375
    k1 = rankz >= 3 ? 1 : 0;
×
376
    l1 = rankw >= 3 ? 1 : 0;
×
377
    // Rank 2 denotes the second largest coordinate.
378
    i2 = rankx >= 2 ? 1 : 0;
×
379
    j2 = ranky >= 2 ? 1 : 0;
×
380
    k2 = rankz >= 2 ? 1 : 0;
×
381
    l2 = rankw >= 2 ? 1 : 0;
×
382
    // Rank 1 denotes the second smallest coordinate.
383
    i3 = rankx >= 1 ? 1 : 0;
×
384
    j3 = ranky >= 1 ? 1 : 0;
×
385
    k3 = rankz >= 1 ? 1 : 0;
×
386
    l3 = rankw >= 1 ? 1 : 0;
×
387
    // The fifth corner has all coordinate offsets = 1, so no need to compute
388
    // that.
389
    final x1 = x0 - i1 + _G4; // Offsets for second corner in (x,y,z,w) coords
×
390
    final y1 = y0 - j1 + _G4;
×
391
    final z1 = z0 - k1 + _G4;
×
392
    final w1 = w0 - l1 + _G4;
×
393
    final x2 =
394
        x0 - i2 + 2.0 * _G4; // Offsets for third corner in (x,y,z,w) coords
×
395
    final y2 = y0 - j2 + 2.0 * _G4;
×
396
    final z2 = z0 - k2 + 2.0 * _G4;
×
397
    final w2 = w0 - l2 + 2.0 * _G4;
×
398
    final x3 =
399
        x0 - i3 + 3.0 * _G4; // Offsets for fourth corner in (x,y,z,w) coords
×
400
    final y3 = y0 - j3 + 3.0 * _G4;
×
401
    final z3 = z0 - k3 + 3.0 * _G4;
×
402
    final w3 = w0 - l3 + 3.0 * _G4;
×
403
    final x4 =
404
        x0 - 1.0 + 4.0 * _G4; // Offsets for last corner in (x,y,z,w) coords
×
405
    final y4 = y0 - 1.0 + 4.0 * _G4;
×
406
    final z4 = z0 - 1.0 + 4.0 * _G4;
×
407
    final w4 = w0 - 1.0 + 4.0 * _G4;
×
408
    // Work out the hashed gradient indices of the five simplex corners
409
    final ii = i & 255;
×
410
    final jj = j & 255;
×
411
    final kk = k & 255;
×
412
    final ll = l & 255;
×
413
    final gi0 = _perm[ii + _perm[jj + _perm[kk + _perm[ll]]]] % 32;
×
414
    final gi1 =
415
        _perm[ii + i1 + _perm[jj + j1 + _perm[kk + k1 + _perm[ll + l1]]]] % 32;
×
416
    final gi2 =
417
        _perm[ii + i2 + _perm[jj + j2 + _perm[kk + k2 + _perm[ll + l2]]]] % 32;
×
418
    final gi3 =
419
        _perm[ii + i3 + _perm[jj + j3 + _perm[kk + k3 + _perm[ll + l3]]]] % 32;
×
420
    final gi4 =
421
        _perm[ii + 1 + _perm[jj + 1 + _perm[kk + 1 + _perm[ll + 1]]]] % 32;
×
422
    // Calculate the contribution from the five corners
423
    var t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0;
×
424
    if (t0 < 0) {
×
425
      n0 = 0.0;
426
    } else {
427
      t0 *= t0;
×
428
      n0 = t0 * t0 * _dot4(_grad4[gi0], x0, y0, z0, w0);
×
429
    }
430
    var t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1;
×
431
    if (t1 < 0) {
×
432
      n1 = 0.0;
433
    } else {
434
      t1 *= t1;
×
435
      n1 = t1 * t1 * _dot4(_grad4[gi1], x1, y1, z1, w1);
×
436
    }
437
    var t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2;
×
438
    if (t2 < 0) {
×
439
      n2 = 0.0;
440
    } else {
441
      t2 *= t2;
×
442
      n2 = t2 * t2 * _dot4(_grad4[gi2], x2, y2, z2, w2);
×
443
    }
444
    var t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3;
×
445
    if (t3 < 0) {
×
446
      n3 = 0.0;
447
    } else {
448
      t3 *= t3;
×
449
      n3 = t3 * t3 * _dot4(_grad4[gi3], x3, y3, z3, w3);
×
450
    }
451
    var t4 = 0.6 - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4;
×
452
    if (t4 < 0) {
×
453
      n4 = 0.0;
454
    } else {
455
      t4 *= t4;
×
456
      n4 = t4 * t4 * _dot4(_grad4[gi4], x4, y4, z4, w4);
×
457
    }
458
    // Sum up and scale the result to cover the range [-1,1]
459
    return 27.0 * (n0 + n1 + n2 + n3 + n4);
×
460
  }
461
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc