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

google / vector_math.dart / 17181727464

04 Aug 2025 07:19PM UTC coverage: 26.702% (+0.3%) from 26.388%
17181727464

push

github

web-flow
Bump min SDK to 3.7, update dependencies, reformat (#348)

496 of 1182 new or added lines in 55 files covered. (41.96%)

18 existing lines in 8 files now uncovered.

4463 of 16714 relevant lines covered (26.7%)

1.18 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(
29
  'This API will be removed '
30
  '(see https:github.com/google/vector_math.dart/issues/270)',
31
)
32
class SimplexNoise {
33
  static final List<List<double>> _grad3 = <List<double>>[
×
34
    <double>[1.0, 1.0, 0.0],
×
35
    <double>[-1.0, 1.0, 0.0],
×
36
    <double>[1.0, -1.0, 0.0],
×
37
    <double>[-1.0, -1.0, 0.0],
×
38
    <double>[1.0, 0.0, 1.0],
×
39
    <double>[-1.0, 0.0, 1.0],
×
40
    <double>[1.0, 0.0, -1.0],
×
41
    <double>[-1.0, 0.0, -1.0],
×
42
    <double>[0.0, 1.0, 1.0],
×
43
    <double>[0.0, -1.0, 1.0],
×
44
    <double>[0.0, 1.0, -1.0],
×
NEW
45
    <double>[0.0, -1.0, -1.0],
×
46
  ];
47

48
  static final List<List<double>> _grad4 = <List<double>>[
×
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>[0.0, -1.0, -1.0, 1.0],
×
56
    <double>[0.0, -1.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, 0.0, -1.0, 1.0],
×
64
    <double>[-1.0, 0.0, -1.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, 0.0, 1.0],
×
72
    <double>[-1.0, -1.0, 0.0, -1.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
    <double>[-1.0, -1.0, 1.0, 0.0],
×
NEW
80
    <double>[-1.0, -1.0, -1.0, 0.0],
×
81
  ];
82

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

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc