• 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/matrix3.dart
1
// Copyright (c) 2015, Google Inc. Please see the AUTHORS file for details.
2
// All rights reserved. Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4

5
part of '../../vector_math_64.dart';
6

7
/// 3D Matrix.
8
/// Values are stored in column major order.
9
class Matrix3 {
10
  final Float64List _m3storage;
11

12
  /// The components of the matrix.
13
  Float64List get storage => _m3storage;
×
14

15
  /// Solve [A] * [x] = [b].
16
  static void solve2(Matrix3 A, Vector2 x, Vector2 b) {
×
17
    final a11 = A.entry(0, 0);
×
18
    final a12 = A.entry(0, 1);
×
19
    final a21 = A.entry(1, 0);
×
20
    final a22 = A.entry(1, 1);
×
21
    final bx = b.x - A.storage[6];
×
22
    final by = b.y - A.storage[7];
×
23
    var det = a11 * a22 - a12 * a21;
×
24

25
    if (det != 0.0) {
×
26
      det = 1.0 / det;
×
27
    }
28

29
    x
30
      ..x = det * (a22 * bx - a12 * by)
×
31
      ..y = det * (a11 * by - a21 * bx);
×
32
  }
33

34
  /// Solve [A] * [x] = [b].
35
  static void solve(Matrix3 A, Vector3 x, Vector3 b) {
×
36
    final A0x = A.entry(0, 0);
×
37
    final A0y = A.entry(1, 0);
×
38
    final A0z = A.entry(2, 0);
×
39
    final A1x = A.entry(0, 1);
×
40
    final A1y = A.entry(1, 1);
×
41
    final A1z = A.entry(2, 1);
×
42
    final A2x = A.entry(0, 2);
×
43
    final A2y = A.entry(1, 2);
×
44
    final A2z = A.entry(2, 2);
×
45
    double rx, ry, rz;
46
    double det;
47

48
    // Column1 cross Column 2
49
    rx = A1y * A2z - A1z * A2y;
×
50
    ry = A1z * A2x - A1x * A2z;
×
51
    rz = A1x * A2y - A1y * A2x;
×
52

53
    // A.getColumn(0).dot(x)
54
    det = A0x * rx + A0y * ry + A0z * rz;
×
55
    if (det != 0.0) {
×
56
      det = 1.0 / det;
×
57
    }
58

59
    // b dot [Column1 cross Column 2]
60
    final x_ = det * (b.x * rx + b.y * ry + b.z * rz);
×
61

62
    // Column2 cross b
63
    rx = -(A2y * b.z - A2z * b.y);
×
64
    ry = -(A2z * b.x - A2x * b.z);
×
65
    rz = -(A2x * b.y - A2y * b.x);
×
66
    // Column0 dot -[Column2 cross b (Column3)]
67
    final y_ = det * (A0x * rx + A0y * ry + A0z * rz);
×
68

69
    // b cross Column 1
70
    rx = -(b.y * A1z - b.z * A1y);
×
71
    ry = -(b.z * A1x - b.x * A1z);
×
72
    rz = -(b.x * A1y - b.y * A1x);
×
73
    // Column0 dot -[b cross Column 1]
74
    final z_ = det * (A0x * rx + A0y * ry + A0z * rz);
×
75

76
    x
77
      ..x = x_
×
78
      ..y = y_
×
79
      ..z = z_;
×
80
  }
81

82
  /// Return index in storage for [row], [col] value.
83
  int index(int row, int col) => (col * 3) + row;
×
84

85
  /// Value at [row], [col].
86
  double entry(int row, int col) {
×
87
    assert((row >= 0) && (row < dimension));
×
88
    assert((col >= 0) && (col < dimension));
×
89

90
    return _m3storage[index(row, col)];
×
91
  }
92

93
  /// Set value at [row], [col] to be [v].
94
  void setEntry(int row, int col, double v) {
×
95
    assert((row >= 0) && (row < dimension));
×
96
    assert((col >= 0) && (col < dimension));
×
97

98
    _m3storage[index(row, col)] = v;
×
99
  }
100

101
  /// New matrix with specified values.
NEW
102
  factory Matrix3(
×
103
    double arg0,
104
    double arg1,
105
    double arg2,
106
    double arg3,
107
    double arg4,
108
    double arg5,
109
    double arg6,
110
    double arg7,
111
    double arg8,
112
  ) =>
113
      Matrix3.zero()
×
114
        ..setValues(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
×
115

116
  /// New matrix from [values].
NEW
117
  factory Matrix3.fromList(List<double> values) =>
×
NEW
118
      Matrix3.zero()..setValues(
×
NEW
119
        values[0],
×
NEW
120
        values[1],
×
NEW
121
        values[2],
×
NEW
122
        values[3],
×
NEW
123
        values[4],
×
NEW
124
        values[5],
×
NEW
125
        values[6],
×
NEW
126
        values[7],
×
NEW
127
        values[8],
×
128
      );
129

130
  /// Constructs a new [Matrix3] filled with zeros.
131
  Matrix3.zero() : _m3storage = Float64List(9);
×
132

133
  /// Identity matrix.
134
  factory Matrix3.identity() => Matrix3.zero()..setIdentity();
×
135

136
  /// Copes values from [other].
137
  factory Matrix3.copy(Matrix3 other) => Matrix3.zero()..setFrom(other);
×
138

139
  /// Constructs a new mat3 from columns.
140
  factory Matrix3.columns(Vector3 arg0, Vector3 arg1, Vector3 arg2) =>
×
141
      Matrix3.zero()..setColumns(arg0, arg1, arg2);
×
142

143
  /// Outer product of [u] and [v].
144
  factory Matrix3.outer(Vector3 u, Vector3 v) => Matrix3.zero()..setOuter(u, v);
×
145

146
  /// Rotation of [radians] around X axis.
147
  factory Matrix3.rotationX(double radians) =>
×
148
      Matrix3.zero()..setRotationX(radians);
×
149

150
  /// Rotation of [radians] around Y axis.
151
  factory Matrix3.rotationY(double radians) =>
×
152
      Matrix3.zero()..setRotationY(radians);
×
153

154
  /// Rotation of [radians] around Z axis.
155
  factory Matrix3.rotationZ(double radians) =>
×
156
      Matrix3.zero()..setRotationZ(radians);
×
157

158
  /// Sets the matrix with specified values.
NEW
159
  void setValues(
×
160
    double arg0,
161
    double arg1,
162
    double arg2,
163
    double arg3,
164
    double arg4,
165
    double arg5,
166
    double arg6,
167
    double arg7,
168
    double arg8,
169
  ) {
170
    _m3storage[8] = arg8;
×
171
    _m3storage[7] = arg7;
×
172
    _m3storage[6] = arg6;
×
173
    _m3storage[5] = arg5;
×
174
    _m3storage[4] = arg4;
×
175
    _m3storage[3] = arg3;
×
176
    _m3storage[2] = arg2;
×
177
    _m3storage[1] = arg1;
×
178
    _m3storage[0] = arg0;
×
179
  }
180

181
  /// Sets the entire matrix to the column values.
182
  void setColumns(Vector3 arg0, Vector3 arg1, Vector3 arg2) {
×
183
    final arg0Storage = arg0._v3storage;
×
184
    final arg1Storage = arg1._v3storage;
×
185
    final arg2Storage = arg2._v3storage;
×
186
    _m3storage[0] = arg0Storage[0];
×
187
    _m3storage[1] = arg0Storage[1];
×
188
    _m3storage[2] = arg0Storage[2];
×
189
    _m3storage[3] = arg1Storage[0];
×
190
    _m3storage[4] = arg1Storage[1];
×
191
    _m3storage[5] = arg1Storage[2];
×
192
    _m3storage[6] = arg2Storage[0];
×
193
    _m3storage[7] = arg2Storage[1];
×
194
    _m3storage[8] = arg2Storage[2];
×
195
  }
196

197
  /// Sets the entire matrix to the matrix in [arg].
198
  void setFrom(Matrix3 arg) {
×
199
    final argStorage = arg._m3storage;
×
200
    _m3storage[8] = argStorage[8];
×
201
    _m3storage[7] = argStorage[7];
×
202
    _m3storage[6] = argStorage[6];
×
203
    _m3storage[5] = argStorage[5];
×
204
    _m3storage[4] = argStorage[4];
×
205
    _m3storage[3] = argStorage[3];
×
206
    _m3storage[2] = argStorage[2];
×
207
    _m3storage[1] = argStorage[1];
×
208
    _m3storage[0] = argStorage[0];
×
209
  }
210

211
  /// Set this to the outer product of [u] and [v].
212
  void setOuter(Vector3 u, Vector3 v) {
×
213
    final uStorage = u._v3storage;
×
214
    final vStorage = v._v3storage;
×
215
    _m3storage[0] = uStorage[0] * vStorage[0];
×
216
    _m3storage[1] = uStorage[0] * vStorage[1];
×
217
    _m3storage[2] = uStorage[0] * vStorage[2];
×
218
    _m3storage[3] = uStorage[1] * vStorage[0];
×
219
    _m3storage[4] = uStorage[1] * vStorage[1];
×
220
    _m3storage[5] = uStorage[1] * vStorage[2];
×
221
    _m3storage[6] = uStorage[2] * vStorage[0];
×
222
    _m3storage[7] = uStorage[2] * vStorage[1];
×
223
    _m3storage[8] = uStorage[2] * vStorage[2];
×
224
  }
225

226
  /// Set the diagonal of the matrix.
227
  void splatDiagonal(double arg) {
×
228
    _m3storage[0] = arg;
×
229
    _m3storage[4] = arg;
×
230
    _m3storage[8] = arg;
×
231
  }
232

233
  /// Set the diagonal of the matrix.
234
  void setDiagonal(Vector3 arg) {
×
235
    _m3storage[0] = arg.storage[0];
×
236
    _m3storage[4] = arg.storage[1];
×
237
    _m3storage[8] = arg.storage[2];
×
238
  }
239

240
  /// Sets the upper 2x2 of the matrix to be [arg].
241
  void setUpper2x2(Matrix2 arg) {
×
242
    final argStorage = arg._m2storage;
×
243
    _m3storage[0] = argStorage[0];
×
244
    _m3storage[1] = argStorage[1];
×
245
    _m3storage[3] = argStorage[2];
×
246
    _m3storage[4] = argStorage[3];
×
247
  }
248

249
  /// Returns a printable string
250
  @override
×
251
  String toString() => '[0] ${getRow(0)}\n[1] ${getRow(1)}\n[2] ${getRow(2)}\n';
×
252

253
  /// Dimension of the matrix.
254
  int get dimension => 3;
×
255

256
  /// Access the element of the matrix at the index [i].
257
  double operator [](int i) => _m3storage[i];
×
258

259
  /// Set the element of the matrix at the index [i].
260
  void operator []=(int i, double v) {
×
261
    _m3storage[i] = v;
×
262
  }
263

264
  /// Check if two matrices are the same.
265
  @override
×
266
  bool operator ==(Object other) =>
267
      (other is Matrix3) &&
×
268
      (_m3storage[0] == other._m3storage[0]) &&
×
269
      (_m3storage[1] == other._m3storage[1]) &&
×
270
      (_m3storage[2] == other._m3storage[2]) &&
×
271
      (_m3storage[3] == other._m3storage[3]) &&
×
272
      (_m3storage[4] == other._m3storage[4]) &&
×
273
      (_m3storage[5] == other._m3storage[5]) &&
×
274
      (_m3storage[6] == other._m3storage[6]) &&
×
275
      (_m3storage[7] == other._m3storage[7]) &&
×
276
      (_m3storage[8] == other._m3storage[8]);
×
277

278
  @override
×
279
  int get hashCode => Object.hashAll(_m3storage);
×
280

281
  /// Returns row 0
282
  Vector3 get row0 => getRow(0);
×
283

284
  /// Returns row 1
285
  Vector3 get row1 => getRow(1);
×
286

287
  /// Returns row 2
288
  Vector3 get row2 => getRow(2);
×
289

290
  /// Sets row 0 to [arg]
291
  set row0(Vector3 arg) => setRow(0, arg);
×
292

293
  /// Sets row 1 to [arg]
294
  set row1(Vector3 arg) => setRow(1, arg);
×
295

296
  /// Sets row 2 to [arg]
297
  set row2(Vector3 arg) => setRow(2, arg);
×
298

299
  /// Assigns the [row] of to [arg].
300
  void setRow(int row, Vector3 arg) {
×
301
    final argStorage = arg._v3storage;
×
302
    _m3storage[index(row, 0)] = argStorage[0];
×
303
    _m3storage[index(row, 1)] = argStorage[1];
×
304
    _m3storage[index(row, 2)] = argStorage[2];
×
305
  }
306

307
  /// Gets the [row] of the matrix
308
  Vector3 getRow(int row) {
×
309
    final r = Vector3.zero();
×
310
    final rStorage = r._v3storage;
×
311
    rStorage[0] = _m3storage[index(row, 0)];
×
312
    rStorage[1] = _m3storage[index(row, 1)];
×
313
    rStorage[2] = _m3storage[index(row, 2)];
×
314
    return r;
315
  }
316

317
  /// Assigns the [column] of the matrix [arg]
318
  void setColumn(int column, Vector3 arg) {
×
319
    final argStorage = arg._v3storage;
×
320
    final entry = column * 3;
×
321
    _m3storage[entry + 2] = argStorage[2];
×
322
    _m3storage[entry + 1] = argStorage[1];
×
323
    _m3storage[entry + 0] = argStorage[0];
×
324
  }
325

326
  /// Gets the [column] of the matrix
327
  Vector3 getColumn(int column) {
×
328
    final r = Vector3.zero();
×
329
    final rStorage = r._v3storage;
×
330
    final entry = column * 3;
×
331
    rStorage[2] = _m3storage[entry + 2];
×
332
    rStorage[1] = _m3storage[entry + 1];
×
333
    rStorage[0] = _m3storage[entry + 0];
×
334
    return r;
335
  }
336

337
  /// Clone of this.
338
  Matrix3 clone() => Matrix3.copy(this);
×
339

340
  /// Copy this into [arg].
341
  Matrix3 copyInto(Matrix3 arg) {
×
342
    final argStorage = arg._m3storage;
×
343
    argStorage[0] = _m3storage[0];
×
344
    argStorage[1] = _m3storage[1];
×
345
    argStorage[2] = _m3storage[2];
×
346
    argStorage[3] = _m3storage[3];
×
347
    argStorage[4] = _m3storage[4];
×
348
    argStorage[5] = _m3storage[5];
×
349
    argStorage[6] = _m3storage[6];
×
350
    argStorage[7] = _m3storage[7];
×
351
    argStorage[8] = _m3storage[8];
×
352
    return arg;
353
  }
354

355
  /// Returns a new vector or matrix by multiplying this with [arg].
356
  @pragma('wasm:prefer-inline')
×
357
  @pragma('vm:prefer-inline')
358
  @pragma('dart2js:prefer-inline')
359
  dynamic operator *(dynamic arg) {
360
    final Object result;
361
    if (arg is double) {
×
362
      result = scaled(arg);
×
363
    } else if (arg is Vector3) {
×
364
      result = transformed(arg);
×
365
    } else if (arg is Matrix3) {
×
366
      result = multiplied(arg);
×
367
    } else {
368
      throw ArgumentError(arg);
×
369
    }
370
    return result;
371
  }
372

373
  /// Returns new matrix after component wise this + [arg]
374
  Matrix3 operator +(Matrix3 arg) => clone()..add(arg);
×
375

376
  /// Returns new matrix after component wise this - [arg]
377
  Matrix3 operator -(Matrix3 arg) => clone()..sub(arg);
×
378

379
  /// Returns new matrix -this
380
  Matrix3 operator -() => clone()..negate();
×
381

382
  /// Zeros this.
383
  void setZero() {
×
384
    _m3storage[0] = 0.0;
×
385
    _m3storage[1] = 0.0;
×
386
    _m3storage[2] = 0.0;
×
387
    _m3storage[3] = 0.0;
×
388
    _m3storage[4] = 0.0;
×
389
    _m3storage[5] = 0.0;
×
390
    _m3storage[6] = 0.0;
×
391
    _m3storage[7] = 0.0;
×
392
    _m3storage[8] = 0.0;
×
393
  }
394

395
  /// Makes this into the identity matrix.
396
  void setIdentity() {
×
397
    _m3storage[0] = 1.0;
×
398
    _m3storage[1] = 0.0;
×
399
    _m3storage[2] = 0.0;
×
400
    _m3storage[3] = 0.0;
×
401
    _m3storage[4] = 1.0;
×
402
    _m3storage[5] = 0.0;
×
403
    _m3storage[6] = 0.0;
×
404
    _m3storage[7] = 0.0;
×
405
    _m3storage[8] = 1.0;
×
406
  }
407

408
  /// Returns the tranpose of this.
409
  Matrix3 transposed() => clone()..transpose();
×
410

411
  /// Transpose this.
412
  void transpose() {
×
413
    double temp;
414
    temp = _m3storage[3];
×
415
    _m3storage[3] = _m3storage[1];
×
416
    _m3storage[1] = temp;
×
417
    temp = _m3storage[6];
×
418
    _m3storage[6] = _m3storage[2];
×
419
    _m3storage[2] = temp;
×
420
    temp = _m3storage[7];
×
421
    _m3storage[7] = _m3storage[5];
×
422
    _m3storage[5] = temp;
×
423
  }
424

425
  /// Returns the component wise absolute value of this.
426
  Matrix3 absolute() {
×
427
    final r = Matrix3.zero();
×
428
    final rStorage = r._m3storage;
×
429
    rStorage[0] = _m3storage[0].abs();
×
430
    rStorage[1] = _m3storage[1].abs();
×
431
    rStorage[2] = _m3storage[2].abs();
×
432
    rStorage[3] = _m3storage[3].abs();
×
433
    rStorage[4] = _m3storage[4].abs();
×
434
    rStorage[5] = _m3storage[5].abs();
×
435
    rStorage[6] = _m3storage[6].abs();
×
436
    rStorage[7] = _m3storage[7].abs();
×
437
    rStorage[8] = _m3storage[8].abs();
×
438
    return r;
439
  }
440

441
  /// Returns the determinant of this matrix.
442
  double determinant() {
×
443
    final x =
NEW
444
        _m3storage[0] *
×
445
        ((_m3storage[4] * _m3storage[8]) - (_m3storage[5] * _m3storage[7]));
×
446
    final y =
NEW
447
        _m3storage[1] *
×
448
        ((_m3storage[3] * _m3storage[8]) - (_m3storage[5] * _m3storage[6]));
×
449
    final z =
NEW
450
        _m3storage[2] *
×
451
        ((_m3storage[3] * _m3storage[7]) - (_m3storage[4] * _m3storage[6]));
×
452
    return x - y + z;
×
453
  }
454

455
  /// Returns the dot product of row [i] and [v].
456
  double dotRow(int i, Vector3 v) {
×
457
    final vStorage = v._v3storage;
×
458
    return _m3storage[i] * vStorage[0] +
×
459
        _m3storage[3 + i] * vStorage[1] +
×
460
        _m3storage[6 + i] * vStorage[2];
×
461
  }
462

463
  /// Returns the dot product of column [j] and [v].
464
  double dotColumn(int j, Vector3 v) {
×
465
    final vStorage = v._v3storage;
×
466
    return _m3storage[j * 3] * vStorage[0] +
×
467
        _m3storage[j * 3 + 1] * vStorage[1] +
×
468
        _m3storage[j * 3 + 2] * vStorage[2];
×
469
  }
470

471
  /// Returns the trace of the matrix. The trace of a matrix is the sum of
472
  /// the diagonal entries.
473
  double trace() {
×
474
    var t = 0.0;
475
    t += _m3storage[0];
×
476
    t += _m3storage[4];
×
477
    t += _m3storage[8];
×
478
    return t;
479
  }
480

481
  /// Returns infinity norm of the matrix. Used for numerical analysis.
482
  double infinityNorm() {
×
483
    var norm = 0.0;
484
    {
485
      var row_norm = 0.0;
486
      row_norm += _m3storage[0].abs();
×
487
      row_norm += _m3storage[1].abs();
×
488
      row_norm += _m3storage[2].abs();
×
489
      norm = row_norm > norm ? row_norm : norm;
×
490
    }
491
    {
492
      var row_norm = 0.0;
493
      row_norm += _m3storage[3].abs();
×
494
      row_norm += _m3storage[4].abs();
×
495
      row_norm += _m3storage[5].abs();
×
496
      norm = row_norm > norm ? row_norm : norm;
×
497
    }
498
    {
499
      var row_norm = 0.0;
500
      row_norm += _m3storage[6].abs();
×
501
      row_norm += _m3storage[7].abs();
×
502
      row_norm += _m3storage[8].abs();
×
503
      norm = row_norm > norm ? row_norm : norm;
×
504
    }
505
    return norm;
506
  }
507

508
  /// Returns relative error between this and [correct]
509
  double relativeError(Matrix3 correct) {
×
510
    final diff = correct - this;
×
511
    final correct_norm = correct.infinityNorm();
×
512
    final diff_norm = diff.infinityNorm();
×
513
    return diff_norm / correct_norm;
×
514
  }
515

516
  /// Returns absolute error between this and [correct]
517
  double absoluteError(Matrix3 correct) {
×
518
    final this_norm = infinityNorm();
×
519
    final correct_norm = correct.infinityNorm();
×
520
    final diff_norm = (this_norm - correct_norm).abs();
×
521
    return diff_norm;
522
  }
523

524
  /// Invert the matrix. Returns the determinant.
525
  double invert() => copyInverse(this);
×
526

527
  /// Set this matrix to be the inverse of [arg]
528
  double copyInverse(Matrix3 arg) {
×
529
    final det = arg.determinant();
×
530
    if (det == 0.0) {
×
531
      setFrom(arg);
×
532
      return 0.0;
533
    }
534
    final invDet = 1.0 / det;
×
535
    final argStorage = arg._m3storage;
×
536
    final ix =
NEW
537
        invDet *
×
538
        (argStorage[4] * argStorage[8] - argStorage[5] * argStorage[7]);
×
539
    final iy =
NEW
540
        invDet *
×
541
        (argStorage[2] * argStorage[7] - argStorage[1] * argStorage[8]);
×
542
    final iz =
NEW
543
        invDet *
×
544
        (argStorage[1] * argStorage[5] - argStorage[2] * argStorage[4]);
×
545
    final jx =
NEW
546
        invDet *
×
547
        (argStorage[5] * argStorage[6] - argStorage[3] * argStorage[8]);
×
548
    final jy =
NEW
549
        invDet *
×
550
        (argStorage[0] * argStorage[8] - argStorage[2] * argStorage[6]);
×
551
    final jz =
NEW
552
        invDet *
×
553
        (argStorage[2] * argStorage[3] - argStorage[0] * argStorage[5]);
×
554
    final kx =
NEW
555
        invDet *
×
556
        (argStorage[3] * argStorage[7] - argStorage[4] * argStorage[6]);
×
557
    final ky =
NEW
558
        invDet *
×
559
        (argStorage[1] * argStorage[6] - argStorage[0] * argStorage[7]);
×
560
    final kz =
NEW
561
        invDet *
×
562
        (argStorage[0] * argStorage[4] - argStorage[1] * argStorage[3]);
×
563
    _m3storage[0] = ix;
×
564
    _m3storage[1] = iy;
×
565
    _m3storage[2] = iz;
×
566
    _m3storage[3] = jx;
×
567
    _m3storage[4] = jy;
×
568
    _m3storage[5] = jz;
×
569
    _m3storage[6] = kx;
×
570
    _m3storage[7] = ky;
×
571
    _m3storage[8] = kz;
×
572
    return det;
573
  }
574

575
  /// Set this matrix to be the normal matrix of [arg].
576
  void copyNormalMatrix(Matrix4 arg) {
×
577
    copyInverse(arg.getRotation());
×
578
    transpose();
×
579
  }
580

581
  /// Turns the matrix into a rotation of [radians] around X
582
  void setRotationX(double radians) {
×
583
    final c = math.cos(radians);
×
584
    final s = math.sin(radians);
×
585
    _m3storage[0] = 1.0;
×
586
    _m3storage[1] = 0.0;
×
587
    _m3storage[2] = 0.0;
×
588
    _m3storage[3] = 0.0;
×
589
    _m3storage[4] = c;
×
590
    _m3storage[5] = s;
×
591
    _m3storage[6] = 0.0;
×
592
    _m3storage[7] = -s;
×
593
    _m3storage[8] = c;
×
594
  }
595

596
  /// Turns the matrix into a rotation of [radians] around Y
597
  void setRotationY(double radians) {
×
598
    final c = math.cos(radians);
×
599
    final s = math.sin(radians);
×
600
    _m3storage[0] = c;
×
601
    _m3storage[1] = 0.0;
×
602
    _m3storage[2] = -s;
×
603
    _m3storage[3] = 0.0;
×
604
    _m3storage[4] = 1.0;
×
605
    _m3storage[5] = 0.0;
×
606
    _m3storage[6] = s;
×
607
    _m3storage[7] = 0.0;
×
608
    _m3storage[8] = c;
×
609
  }
610

611
  /// Turns the matrix into a rotation of [radians] around Z
612
  void setRotationZ(double radians) {
×
613
    final c = math.cos(radians);
×
614
    final s = math.sin(radians);
×
615
    _m3storage[0] = c;
×
616
    _m3storage[1] = s;
×
617
    _m3storage[2] = 0.0;
×
618
    _m3storage[3] = -s;
×
619
    _m3storage[4] = c;
×
620
    _m3storage[5] = 0.0;
×
621
    _m3storage[6] = 0.0;
×
622
    _m3storage[7] = 0.0;
×
623
    _m3storage[8] = 1.0;
×
624
  }
625

626
  /// Converts into Adjugate matrix and scales by [scale]
627
  void scaleAdjoint(double scale) {
×
628
    final m00 = _m3storage[0];
×
629
    final m01 = _m3storage[3];
×
630
    final m02 = _m3storage[6];
×
631
    final m10 = _m3storage[1];
×
632
    final m11 = _m3storage[4];
×
633
    final m12 = _m3storage[7];
×
634
    final m20 = _m3storage[2];
×
635
    final m21 = _m3storage[5];
×
636
    final m22 = _m3storage[8];
×
637
    _m3storage[0] = (m11 * m22 - m12 * m21) * scale;
×
638
    _m3storage[1] = (m12 * m20 - m10 * m22) * scale;
×
639
    _m3storage[2] = (m10 * m21 - m11 * m20) * scale;
×
640
    _m3storage[3] = (m02 * m21 - m01 * m22) * scale;
×
641
    _m3storage[4] = (m00 * m22 - m02 * m20) * scale;
×
642
    _m3storage[5] = (m01 * m20 - m00 * m21) * scale;
×
643
    _m3storage[6] = (m01 * m12 - m02 * m11) * scale;
×
644
    _m3storage[7] = (m02 * m10 - m00 * m12) * scale;
×
645
    _m3storage[8] = (m00 * m11 - m01 * m10) * scale;
×
646
  }
647

648
  /// Rotates [arg] by the absolute rotation of this
649
  /// Returns [arg].
650
  /// Primarily used by AABB transformation code.
651
  Vector3 absoluteRotate(Vector3 arg) {
×
652
    final m00 = _m3storage[0].abs();
×
653
    final m01 = _m3storage[3].abs();
×
654
    final m02 = _m3storage[6].abs();
×
655
    final m10 = _m3storage[1].abs();
×
656
    final m11 = _m3storage[4].abs();
×
657
    final m12 = _m3storage[7].abs();
×
658
    final m20 = _m3storage[2].abs();
×
659
    final m21 = _m3storage[5].abs();
×
660
    final m22 = _m3storage[8].abs();
×
661
    final argStorage = arg._v3storage;
×
662
    final x = argStorage[0];
×
663
    final y = argStorage[1];
×
664
    final z = argStorage[2];
×
665
    argStorage[0] = x * m00 + y * m01 + z * m02;
×
666
    argStorage[1] = x * m10 + y * m11 + z * m12;
×
667
    argStorage[2] = x * m20 + y * m21 + z * m22;
×
668
    return arg;
669
  }
670

671
  /// Rotates [arg] by the absolute rotation of this
672
  /// Returns [arg].
673
  /// Primarily used by AABB transformation code.
674
  Vector2 absoluteRotate2(Vector2 arg) {
×
675
    final m00 = _m3storage[0].abs();
×
676
    final m01 = _m3storage[3].abs();
×
677
    final m10 = _m3storage[1].abs();
×
678
    final m11 = _m3storage[4].abs();
×
679
    final argStorage = arg._v2storage;
×
680
    final x = argStorage[0];
×
681
    final y = argStorage[1];
×
682
    argStorage[0] = x * m00 + y * m01;
×
683
    argStorage[1] = x * m10 + y * m11;
×
684
    return arg;
685
  }
686

687
  /// Transforms [arg] with this.
688
  Vector2 transform2(Vector2 arg) {
×
689
    final argStorage = arg._v2storage;
×
690
    final x_ =
NEW
691
        (_m3storage[0] * argStorage[0]) +
×
692
        (_m3storage[3] * argStorage[1]) +
×
693
        _m3storage[6];
×
694
    final y_ =
NEW
695
        (_m3storage[1] * argStorage[0]) +
×
696
        (_m3storage[4] * argStorage[1]) +
×
697
        _m3storage[7];
×
698
    argStorage[0] = x_;
×
699
    argStorage[1] = y_;
×
700
    return arg;
701
  }
702

703
  /// Scales this by [scale].
704
  void scale(double scale) {
×
705
    _m3storage[0] = _m3storage[0] * scale;
×
706
    _m3storage[1] = _m3storage[1] * scale;
×
707
    _m3storage[2] = _m3storage[2] * scale;
×
708
    _m3storage[3] = _m3storage[3] * scale;
×
709
    _m3storage[4] = _m3storage[4] * scale;
×
710
    _m3storage[5] = _m3storage[5] * scale;
×
711
    _m3storage[6] = _m3storage[6] * scale;
×
712
    _m3storage[7] = _m3storage[7] * scale;
×
713
    _m3storage[8] = _m3storage[8] * scale;
×
714
  }
715

716
  /// Create a copy of this and scale it by [scale].
717
  Matrix3 scaled(double scale) => clone()..scale(scale);
×
718

719
  /// Add [o] to this.
720
  void add(Matrix3 o) {
×
721
    final oStorage = o._m3storage;
×
722
    _m3storage[0] = _m3storage[0] + oStorage[0];
×
723
    _m3storage[1] = _m3storage[1] + oStorage[1];
×
724
    _m3storage[2] = _m3storage[2] + oStorage[2];
×
725
    _m3storage[3] = _m3storage[3] + oStorage[3];
×
726
    _m3storage[4] = _m3storage[4] + oStorage[4];
×
727
    _m3storage[5] = _m3storage[5] + oStorage[5];
×
728
    _m3storage[6] = _m3storage[6] + oStorage[6];
×
729
    _m3storage[7] = _m3storage[7] + oStorage[7];
×
730
    _m3storage[8] = _m3storage[8] + oStorage[8];
×
731
  }
732

733
  /// Subtract [o] from this.
734
  void sub(Matrix3 o) {
×
735
    final oStorage = o._m3storage;
×
736
    _m3storage[0] = _m3storage[0] - oStorage[0];
×
737
    _m3storage[1] = _m3storage[1] - oStorage[1];
×
738
    _m3storage[2] = _m3storage[2] - oStorage[2];
×
739
    _m3storage[3] = _m3storage[3] - oStorage[3];
×
740
    _m3storage[4] = _m3storage[4] - oStorage[4];
×
741
    _m3storage[5] = _m3storage[5] - oStorage[5];
×
742
    _m3storage[6] = _m3storage[6] - oStorage[6];
×
743
    _m3storage[7] = _m3storage[7] - oStorage[7];
×
744
    _m3storage[8] = _m3storage[8] - oStorage[8];
×
745
  }
746

747
  /// Negate this.
748
  void negate() {
×
749
    _m3storage[0] = -_m3storage[0];
×
750
    _m3storage[1] = -_m3storage[1];
×
751
    _m3storage[2] = -_m3storage[2];
×
752
    _m3storage[3] = -_m3storage[3];
×
753
    _m3storage[4] = -_m3storage[4];
×
754
    _m3storage[5] = -_m3storage[5];
×
755
    _m3storage[6] = -_m3storage[6];
×
756
    _m3storage[7] = -_m3storage[7];
×
757
    _m3storage[8] = -_m3storage[8];
×
758
  }
759

760
  /// Multiply this by [arg].
761
  void multiply(Matrix3 arg) {
×
762
    final m00 = _m3storage[0];
×
763
    final m01 = _m3storage[3];
×
764
    final m02 = _m3storage[6];
×
765
    final m10 = _m3storage[1];
×
766
    final m11 = _m3storage[4];
×
767
    final m12 = _m3storage[7];
×
768
    final m20 = _m3storage[2];
×
769
    final m21 = _m3storage[5];
×
770
    final m22 = _m3storage[8];
×
771
    final argStorage = arg._m3storage;
×
772
    final n00 = argStorage[0];
×
773
    final n01 = argStorage[3];
×
774
    final n02 = argStorage[6];
×
775
    final n10 = argStorage[1];
×
776
    final n11 = argStorage[4];
×
777
    final n12 = argStorage[7];
×
778
    final n20 = argStorage[2];
×
779
    final n21 = argStorage[5];
×
780
    final n22 = argStorage[8];
×
781
    _m3storage[0] = (m00 * n00) + (m01 * n10) + (m02 * n20);
×
782
    _m3storage[3] = (m00 * n01) + (m01 * n11) + (m02 * n21);
×
783
    _m3storage[6] = (m00 * n02) + (m01 * n12) + (m02 * n22);
×
784
    _m3storage[1] = (m10 * n00) + (m11 * n10) + (m12 * n20);
×
785
    _m3storage[4] = (m10 * n01) + (m11 * n11) + (m12 * n21);
×
786
    _m3storage[7] = (m10 * n02) + (m11 * n12) + (m12 * n22);
×
787
    _m3storage[2] = (m20 * n00) + (m21 * n10) + (m22 * n20);
×
788
    _m3storage[5] = (m20 * n01) + (m21 * n11) + (m22 * n21);
×
789
    _m3storage[8] = (m20 * n02) + (m21 * n12) + (m22 * n22);
×
790
  }
791

792
  /// Create a copy of this and multiply it by [arg].
793
  Matrix3 multiplied(Matrix3 arg) => clone()..multiply(arg);
×
794

795
  void transposeMultiply(Matrix3 arg) {
×
796
    final m00 = _m3storage[0];
×
797
    final m01 = _m3storage[1];
×
798
    final m02 = _m3storage[2];
×
799
    final m10 = _m3storage[3];
×
800
    final m11 = _m3storage[4];
×
801
    final m12 = _m3storage[5];
×
802
    final m20 = _m3storage[6];
×
803
    final m21 = _m3storage[7];
×
804
    final m22 = _m3storage[8];
×
805
    final argStorage = arg._m3storage;
×
806
    _m3storage[0] =
×
807
        (m00 * argStorage[0]) + (m01 * argStorage[1]) + (m02 * argStorage[2]);
×
808
    _m3storage[3] =
×
809
        (m00 * argStorage[3]) + (m01 * argStorage[4]) + (m02 * argStorage[5]);
×
810
    _m3storage[6] =
×
811
        (m00 * argStorage[6]) + (m01 * argStorage[7]) + (m02 * argStorage[8]);
×
812
    _m3storage[1] =
×
813
        (m10 * argStorage[0]) + (m11 * argStorage[1]) + (m12 * argStorage[2]);
×
814
    _m3storage[4] =
×
815
        (m10 * argStorage[3]) + (m11 * argStorage[4]) + (m12 * argStorage[5]);
×
816
    _m3storage[7] =
×
817
        (m10 * argStorage[6]) + (m11 * argStorage[7]) + (m12 * argStorage[8]);
×
818
    _m3storage[2] =
×
819
        (m20 * argStorage[0]) + (m21 * argStorage[1]) + (m22 * argStorage[2]);
×
820
    _m3storage[5] =
×
821
        (m20 * argStorage[3]) + (m21 * argStorage[4]) + (m22 * argStorage[5]);
×
822
    _m3storage[8] =
×
823
        (m20 * argStorage[6]) + (m21 * argStorage[7]) + (m22 * argStorage[8]);
×
824
  }
825

826
  void multiplyTranspose(Matrix3 arg) {
×
827
    final m00 = _m3storage[0];
×
828
    final m01 = _m3storage[3];
×
829
    final m02 = _m3storage[6];
×
830
    final m10 = _m3storage[1];
×
831
    final m11 = _m3storage[4];
×
832
    final m12 = _m3storage[7];
×
833
    final m20 = _m3storage[2];
×
834
    final m21 = _m3storage[5];
×
835
    final m22 = _m3storage[8];
×
836
    final argStorage = arg._m3storage;
×
837
    _m3storage[0] =
×
838
        (m00 * argStorage[0]) + (m01 * argStorage[3]) + (m02 * argStorage[6]);
×
839
    _m3storage[3] =
×
840
        (m00 * argStorage[1]) + (m01 * argStorage[4]) + (m02 * argStorage[7]);
×
841
    _m3storage[6] =
×
842
        (m00 * argStorage[2]) + (m01 * argStorage[5]) + (m02 * argStorage[8]);
×
843
    _m3storage[1] =
×
844
        (m10 * argStorage[0]) + (m11 * argStorage[3]) + (m12 * argStorage[6]);
×
845
    _m3storage[4] =
×
846
        (m10 * argStorage[1]) + (m11 * argStorage[4]) + (m12 * argStorage[7]);
×
847
    _m3storage[7] =
×
848
        (m10 * argStorage[2]) + (m11 * argStorage[5]) + (m12 * argStorage[8]);
×
849
    _m3storage[2] =
×
850
        (m20 * argStorage[0]) + (m21 * argStorage[3]) + (m22 * argStorage[6]);
×
851
    _m3storage[5] =
×
852
        (m20 * argStorage[1]) + (m21 * argStorage[4]) + (m22 * argStorage[7]);
×
853
    _m3storage[8] =
×
854
        (m20 * argStorage[2]) + (m21 * argStorage[5]) + (m22 * argStorage[8]);
×
855
  }
856

857
  /// Transform [arg] of type [Vector3] using the transformation defined by
858
  /// this.
859
  Vector3 transform(Vector3 arg) {
×
860
    final argStorage = arg._v3storage;
×
861
    final x_ =
NEW
862
        (_m3storage[0] * argStorage[0]) +
×
863
        (_m3storage[3] * argStorage[1]) +
×
864
        (_m3storage[6] * argStorage[2]);
×
865
    final y_ =
NEW
866
        (_m3storage[1] * argStorage[0]) +
×
867
        (_m3storage[4] * argStorage[1]) +
×
868
        (_m3storage[7] * argStorage[2]);
×
869
    final z_ =
NEW
870
        (_m3storage[2] * argStorage[0]) +
×
871
        (_m3storage[5] * argStorage[1]) +
×
872
        (_m3storage[8] * argStorage[2]);
×
873
    arg
874
      ..x = x_
×
875
      ..y = y_
×
876
      ..z = z_;
×
877
    return arg;
878
  }
879

880
  /// Transform a copy of [arg] of type [Vector3] using the transformation
881
  /// defined by this. If a [out] parameter is supplied, the copy is stored in
882
  /// [out].
883
  Vector3 transformed(Vector3 arg, [Vector3? out]) {
×
884
    if (out == null) {
885
      out = Vector3.copy(arg);
×
886
    } else {
887
      out.setFrom(arg);
×
888
    }
889
    return transform(out);
×
890
  }
891

892
  /// Copies this into [array] starting at [offset].
893
  void copyIntoArray(List<num> array, [int offset = 0]) {
×
894
    final i = offset;
895
    array[i + 8] = _m3storage[8];
×
896
    array[i + 7] = _m3storage[7];
×
897
    array[i + 6] = _m3storage[6];
×
898
    array[i + 5] = _m3storage[5];
×
899
    array[i + 4] = _m3storage[4];
×
900
    array[i + 3] = _m3storage[3];
×
901
    array[i + 2] = _m3storage[2];
×
902
    array[i + 1] = _m3storage[1];
×
903
    array[i + 0] = _m3storage[0];
×
904
  }
905

906
  /// Copies elements from [array] into this starting at [offset].
907
  void copyFromArray(List<double> array, [int offset = 0]) {
×
908
    final i = offset;
909
    _m3storage[8] = array[i + 8];
×
910
    _m3storage[7] = array[i + 7];
×
911
    _m3storage[6] = array[i + 6];
×
912
    _m3storage[5] = array[i + 5];
×
913
    _m3storage[4] = array[i + 4];
×
914
    _m3storage[3] = array[i + 3];
×
915
    _m3storage[2] = array[i + 2];
×
916
    _m3storage[1] = array[i + 1];
×
917
    _m3storage[0] = array[i + 0];
×
918
  }
919

920
  /// Multiply this to each set of xyz values in [array] starting at [offset].
921
  List<double> applyToVector3Array(List<double> array, [int offset = 0]) {
×
922
    for (var i = 0, j = offset; i < array.length; i += 3, j += 3) {
×
923
      final v = Vector3.array(array, j)..applyMatrix3(this);
×
924
      array[j] = v.storage[0];
×
925
      array[j + 1] = v.storage[1];
×
926
      array[j + 2] = v.storage[2];
×
927
    }
928

929
    return array;
930
  }
931

932
  Vector3 get right {
×
933
    final x = _m3storage[0];
×
934
    final y = _m3storage[1];
×
935
    final z = _m3storage[2];
×
936
    return Vector3(x, y, z);
×
937
  }
938

939
  Vector3 get up {
×
940
    final x = _m3storage[3];
×
941
    final y = _m3storage[4];
×
942
    final z = _m3storage[5];
×
943
    return Vector3(x, y, z);
×
944
  }
945

946
  Vector3 get forward {
×
947
    final x = _m3storage[6];
×
948
    final y = _m3storage[7];
×
949
    final z = _m3storage[8];
×
950
    return Vector3(x, y, z);
×
951
  }
952

953
  /// Is this the identity matrix?
954
  bool isIdentity() =>
×
NEW
955
      _m3storage[0] ==
×
956
          1.0 // col 1
957
          &&
958
      _m3storage[1] == 0.0 &&
×
959
      _m3storage[2] == 0.0 &&
×
NEW
960
      _m3storage[3] ==
×
961
          0.0 // col 2
962
          &&
963
      _m3storage[4] == 1.0 &&
×
964
      _m3storage[5] == 0.0 &&
×
NEW
965
      _m3storage[6] ==
×
966
          0.0 // col 3
967
          &&
968
      _m3storage[7] == 0.0 &&
×
969
      _m3storage[8] == 1.0;
×
970

971
  /// Is this the zero matrix?
972
  bool isZero() =>
×
NEW
973
      _m3storage[0] ==
×
974
          0.0 // col 1
975
          &&
976
      _m3storage[1] == 0.0 &&
×
977
      _m3storage[2] == 0.0 &&
×
NEW
978
      _m3storage[3] ==
×
979
          0.0 // col 2
980
          &&
981
      _m3storage[4] == 0.0 &&
×
982
      _m3storage[5] == 0.0 &&
×
NEW
983
      _m3storage[6] ==
×
984
          0.0 // col 3
985
          &&
986
      _m3storage[7] == 0.0 &&
×
987
      _m3storage[8] == 0.0;
×
988
}
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