Skip to content

Commit e22dead

Browse files
committed
Do the same for scale
1 parent f940717 commit e22dead

File tree

2 files changed

+104
-28
lines changed

2 files changed

+104
-28
lines changed

lib/src/vector_math/matrix4.dart

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -982,27 +982,63 @@ class Matrix4 {
982982
_m4storage[7] = t8;
983983
}
984984

985-
/// Scale this matrix by a [Vector3], [Vector4], or x,y,z
985+
/// Scale this matrix by a [Vector3], [Vector4], or x,y,z as [double]s.
986+
///
987+
/// If you know the argument types in a call site, prefer [scaleByDouble],
988+
/// [scaleByVector3], or [scaleByVector4] for performance.
986989
void scale(dynamic x, [double? y, double? z]) {
987-
double sx;
988-
double sy;
989-
double sz;
990-
final sw = x is Vector4 ? x.w : 1.0;
991990
if (x is Vector3) {
992-
sx = x.x;
993-
sy = x.y;
994-
sz = x.z;
991+
scaleByVector3(x);
995992
} else if (x is Vector4) {
996-
sx = x.x;
997-
sy = x.y;
998-
sz = x.z;
993+
scaleByVector4(x);
999994
} else if (x is double) {
1000-
sx = x;
1001-
sy = y ?? x;
1002-
sz = z ?? x;
995+
scaleByDouble(x, y ?? x, z ?? x);
1003996
} else {
1004997
throw UnimplementedError();
1005998
}
999+
}
1000+
1001+
/// Scale this matrix.
1002+
void scaleByDouble(double sx, double sy, double sz) {
1003+
_m4storage[0] *= sx;
1004+
_m4storage[1] *= sx;
1005+
_m4storage[2] *= sx;
1006+
_m4storage[3] *= sx;
1007+
_m4storage[4] *= sy;
1008+
_m4storage[5] *= sy;
1009+
_m4storage[6] *= sy;
1010+
_m4storage[7] *= sy;
1011+
_m4storage[8] *= sz;
1012+
_m4storage[9] *= sz;
1013+
_m4storage[10] *= sz;
1014+
_m4storage[11] *= sz;
1015+
}
1016+
1017+
/// Scale this matrix.
1018+
void scaleByVector3(Vector3 v3) {
1019+
final sx = v3.x;
1020+
final sy = v3.y;
1021+
final sz = v3.z;
1022+
_m4storage[0] *= sx;
1023+
_m4storage[1] *= sx;
1024+
_m4storage[2] *= sx;
1025+
_m4storage[3] *= sx;
1026+
_m4storage[4] *= sy;
1027+
_m4storage[5] *= sy;
1028+
_m4storage[6] *= sy;
1029+
_m4storage[7] *= sy;
1030+
_m4storage[8] *= sz;
1031+
_m4storage[9] *= sz;
1032+
_m4storage[10] *= sz;
1033+
_m4storage[11] *= sz;
1034+
}
1035+
1036+
/// Scale this matrix.
1037+
void scaleByVector4(Vector4 v4) {
1038+
final sx = v4.x;
1039+
final sy = v4.y;
1040+
final sz = v4.z;
1041+
final sw = v4.w;
10061042
_m4storage[0] *= sx;
10071043
_m4storage[1] *= sx;
10081044
_m4storage[2] *= sx;
@@ -1023,6 +1059,8 @@ class Matrix4 {
10231059

10241060
/// Create a copy of this scaled by a [Vector3], [Vector4] or [x],[y], and
10251061
/// [z].
1062+
@pragma('wasm:prefer-inline')
1063+
@pragma('vm:prefer-inline')
10261064
Matrix4 scaled(dynamic x, [double? y, double? z]) => clone()..scale(x, y, z);
10271065

10281066
/// Zeros this.

lib/src/vector_math_64/matrix4.dart

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -982,27 +982,63 @@ class Matrix4 {
982982
_m4storage[7] = t8;
983983
}
984984

985-
/// Scale this matrix by a [Vector3], [Vector4], or x,y,z
985+
/// Scale this matrix by a [Vector3], [Vector4], or x,y,z as [double]s.
986+
///
987+
/// If you know the argument types in a call site, prefer [scaleByDouble],
988+
/// [scaleByVector3], or [scaleByVector4] for performance.
986989
void scale(dynamic x, [double? y, double? z]) {
987-
double sx;
988-
double sy;
989-
double sz;
990-
final sw = x is Vector4 ? x.w : 1.0;
991990
if (x is Vector3) {
992-
sx = x.x;
993-
sy = x.y;
994-
sz = x.z;
991+
scaleByVector3(x);
995992
} else if (x is Vector4) {
996-
sx = x.x;
997-
sy = x.y;
998-
sz = x.z;
993+
scaleByVector4(x);
999994
} else if (x is double) {
1000-
sx = x;
1001-
sy = y ?? x;
1002-
sz = z ?? x;
995+
scaleByDouble(x, y ?? x, z ?? x);
1003996
} else {
1004997
throw UnimplementedError();
1005998
}
999+
}
1000+
1001+
/// Scale this matrix.
1002+
void scaleByDouble(double sx, double sy, double sz) {
1003+
_m4storage[0] *= sx;
1004+
_m4storage[1] *= sx;
1005+
_m4storage[2] *= sx;
1006+
_m4storage[3] *= sx;
1007+
_m4storage[4] *= sy;
1008+
_m4storage[5] *= sy;
1009+
_m4storage[6] *= sy;
1010+
_m4storage[7] *= sy;
1011+
_m4storage[8] *= sz;
1012+
_m4storage[9] *= sz;
1013+
_m4storage[10] *= sz;
1014+
_m4storage[11] *= sz;
1015+
}
1016+
1017+
/// Scale this matrix.
1018+
void scaleByVector3(Vector3 v3) {
1019+
final sx = v3.x;
1020+
final sy = v3.y;
1021+
final sz = v3.z;
1022+
_m4storage[0] *= sx;
1023+
_m4storage[1] *= sx;
1024+
_m4storage[2] *= sx;
1025+
_m4storage[3] *= sx;
1026+
_m4storage[4] *= sy;
1027+
_m4storage[5] *= sy;
1028+
_m4storage[6] *= sy;
1029+
_m4storage[7] *= sy;
1030+
_m4storage[8] *= sz;
1031+
_m4storage[9] *= sz;
1032+
_m4storage[10] *= sz;
1033+
_m4storage[11] *= sz;
1034+
}
1035+
1036+
/// Scale this matrix.
1037+
void scaleByVector4(Vector4 v4) {
1038+
final sx = v4.x;
1039+
final sy = v4.y;
1040+
final sz = v4.z;
1041+
final sw = v4.w;
10061042
_m4storage[0] *= sx;
10071043
_m4storage[1] *= sx;
10081044
_m4storage[2] *= sx;
@@ -1023,6 +1059,8 @@ class Matrix4 {
10231059

10241060
/// Create a copy of this scaled by a [Vector3], [Vector4] or [x],[y], and
10251061
/// [z].
1062+
@pragma('wasm:prefer-inline')
1063+
@pragma('vm:prefer-inline')
10261064
Matrix4 scaled(dynamic x, [double? y, double? z]) => clone()..scale(x, y, z);
10271065

10281066
/// Zeros this.

0 commit comments

Comments
 (0)