• 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

42.32
/lib/src/vector_math/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.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

29
@Deprecated(
30
  'This API will be removed '
31
  '(see https:github.com/google/vector_math.dart/issues/270)',
32
)
33
class SimplexNoise {
34
  static final List<List<double>> _grad3 = <List<double>>[
3✔
35
    <double>[1.0, 1.0, 0.0],
1✔
36
    <double>[-1.0, 1.0, 0.0],
2✔
37
    <double>[1.0, -1.0, 0.0],
2✔
38
    <double>[-1.0, -1.0, 0.0],
3✔
39
    <double>[1.0, 0.0, 1.0],
1✔
40
    <double>[-1.0, 0.0, 1.0],
2✔
41
    <double>[1.0, 0.0, -1.0],
2✔
42
    <double>[-1.0, 0.0, -1.0],
3✔
43
    <double>[0.0, 1.0, 1.0],
1✔
44
    <double>[0.0, -1.0, 1.0],
2✔
45
    <double>[0.0, 1.0, -1.0],
2✔
46
    <double>[0.0, -1.0, -1.0],
3✔
47
  ];
48

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

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

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

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

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

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

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

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

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

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