Skip to content

Commit 0372a3a

Browse files
authored
Optimize creating an identity matrix (#365)
Since we're starting from zero, only update the needed elements
1 parent 932d814 commit 0372a3a

7 files changed

Lines changed: 32 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 2.4.0-wip
22

33
- Remove the deprecated `SimplexNoise` class.
4+
- Optimized the `identity` constructors on the `Matrix` classes.
45

56
## 2.3.0
67

lib/src/vector_math/matrix2.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ class Matrix2 {
6262
Matrix2.zero() : _m2storage = Float32List(4);
6363

6464
/// Identity matrix.
65-
factory Matrix2.identity() => Matrix2.zero()..setIdentity();
65+
factory Matrix2.identity() =>
66+
Matrix2.zero()
67+
.._m2storage[0] = 1.0
68+
.._m2storage[3] = 1.0;
6669

6770
/// Copies values from [other].
6871
factory Matrix2.copy(Matrix2 other) => Matrix2.zero()..setFrom(other);

lib/src/vector_math/matrix3.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ class Matrix3 {
131131
Matrix3.zero() : _m3storage = Float32List(9);
132132

133133
/// Identity matrix.
134-
factory Matrix3.identity() => Matrix3.zero()..setIdentity();
134+
factory Matrix3.identity() =>
135+
Matrix3.zero()
136+
.._m3storage[0] = 1.0
137+
.._m3storage[4] = 1.0
138+
.._m3storage[8] = 1.0;
135139

136140
/// Copes values from [other].
137141
factory Matrix3.copy(Matrix3 other) => Matrix3.zero()..setFrom(other);

lib/src/vector_math/matrix4.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,12 @@ class Matrix4 {
245245
Matrix4.zero() : _m4storage = Float32List(16);
246246

247247
/// Identity matrix.
248-
factory Matrix4.identity() => Matrix4.zero()..setIdentity();
248+
factory Matrix4.identity() =>
249+
Matrix4.zero()
250+
.._m4storage[0] = 1.0
251+
.._m4storage[5] = 1.0
252+
.._m4storage[10] = 1.0
253+
.._m4storage[15] = 1.0;
249254

250255
/// Copies values from [other].
251256
factory Matrix4.copy(Matrix4 other) => Matrix4.zero()..setFrom(other);
@@ -1777,7 +1782,7 @@ class Matrix4 {
17771782
/// Computes the result of `arg x this` and stores the result in-place in
17781783
/// `this`.
17791784
///
1780-
/// This method does not alter the [Matrix4] in `arg`.
1785+
/// This method does not alter the [Matrix4] in [arg].
17811786
void leftMultiply(Matrix4 arg) {
17821787
final argStorage = arg._m4storage;
17831788
final m00 = argStorage[0];

lib/src/vector_math_64/matrix2.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ class Matrix2 {
6262
Matrix2.zero() : _m2storage = Float64List(4);
6363

6464
/// Identity matrix.
65-
factory Matrix2.identity() => Matrix2.zero()..setIdentity();
65+
factory Matrix2.identity() =>
66+
Matrix2.zero()
67+
.._m2storage[0] = 1.0
68+
.._m2storage[3] = 1.0;
6669

6770
/// Copies values from [other].
6871
factory Matrix2.copy(Matrix2 other) => Matrix2.zero()..setFrom(other);

lib/src/vector_math_64/matrix3.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ class Matrix3 {
131131
Matrix3.zero() : _m3storage = Float64List(9);
132132

133133
/// Identity matrix.
134-
factory Matrix3.identity() => Matrix3.zero()..setIdentity();
134+
factory Matrix3.identity() =>
135+
Matrix3.zero()
136+
.._m3storage[0] = 1.0
137+
.._m3storage[4] = 1.0
138+
.._m3storage[8] = 1.0;
135139

136140
/// Copes values from [other].
137141
factory Matrix3.copy(Matrix3 other) => Matrix3.zero()..setFrom(other);

lib/src/vector_math_64/matrix4.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,12 @@ class Matrix4 {
245245
Matrix4.zero() : _m4storage = Float64List(16);
246246

247247
/// Identity matrix.
248-
factory Matrix4.identity() => Matrix4.zero()..setIdentity();
248+
factory Matrix4.identity() =>
249+
Matrix4.zero()
250+
.._m4storage[0] = 1.0
251+
.._m4storage[5] = 1.0
252+
.._m4storage[10] = 1.0
253+
.._m4storage[15] = 1.0;
249254

250255
/// Copies values from [other].
251256
factory Matrix4.copy(Matrix4 other) => Matrix4.zero()..setFrom(other);

0 commit comments

Comments
 (0)