Skip to content

Commit 2fa9abc

Browse files
committed
Call double fns
1 parent d6d34e4 commit 2fa9abc

File tree

4 files changed

+79
-261
lines changed

4 files changed

+79
-261
lines changed

benchmark/matrix_bench.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,14 @@ class Matrix4TranslateByDoubleBenchmark extends BenchmarkBase {
393393
void setup() {
394394
for (var i = 0; i < 10; i++) {
395395
temp.translateByDouble(
396-
i.toDouble(), (i * 10).toDouble(), (i * 5).toDouble());
396+
i.toDouble(), (i * 10).toDouble(), (i * 5).toDouble(), 1.0);
397397
}
398398
}
399399

400400
@override
401401
void run() {
402402
for (var i = 0; i < 100; i++) {
403-
temp.translateByDouble(10.0, 20.0, 30.0);
403+
temp.translateByDouble(10.0, 20.0, 30.0, 1.0);
404404
}
405405
}
406406
}

lib/src/vector_math/matrix4.dart

Lines changed: 38 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -682,76 +682,17 @@ class Matrix4 {
682682
} else if (x is Vector4) {
683683
translateByVector4(x);
684684
} else if (x is double) {
685-
translateByDouble(x, y, z);
685+
translateByDouble(x, y, z, 1.0);
686686
} else {
687687
throw UnimplementedError();
688688
}
689689
}
690690

691691
/// Translate this matrix by x, y, z.
692-
void translateByDouble(double tx, double ty, double tz) {
693-
final t1 = _m4storage[0] * tx +
694-
_m4storage[4] * ty +
695-
_m4storage[8] * tz +
696-
_m4storage[12];
697-
_m4storage[12] = t1;
698-
699-
final t2 = _m4storage[1] * tx +
700-
_m4storage[5] * ty +
701-
_m4storage[9] * tz +
702-
_m4storage[13];
703-
_m4storage[13] = t2;
704-
705-
final t3 = _m4storage[2] * tx +
706-
_m4storage[6] * ty +
707-
_m4storage[10] * tz +
708-
_m4storage[14];
709-
_m4storage[14] = t3;
710-
711-
final t4 = _m4storage[3] * tx +
712-
_m4storage[7] * ty +
713-
_m4storage[11] * tz +
714-
_m4storage[15];
715-
_m4storage[15] = t4;
716-
}
717-
718-
/// Translate this matrix by a [Vector3].
719-
void translateByVector3(Vector3 v3) {
720-
final tx = v3.x;
721-
final ty = v3.y;
722-
final tz = v3.z;
723-
final t1 = _m4storage[0] * tx +
724-
_m4storage[4] * ty +
725-
_m4storage[8] * tz +
726-
_m4storage[12];
727-
_m4storage[12] = t1;
728-
729-
final t2 = _m4storage[1] * tx +
730-
_m4storage[5] * ty +
731-
_m4storage[9] * tz +
732-
_m4storage[13];
733-
_m4storage[13] = t2;
734-
735-
final t3 = _m4storage[2] * tx +
736-
_m4storage[6] * ty +
737-
_m4storage[10] * tz +
738-
_m4storage[14];
739-
_m4storage[14] = t3;
740-
741-
final t4 = _m4storage[3] * tx +
742-
_m4storage[7] * ty +
743-
_m4storage[11] * tz +
744-
_m4storage[15];
745-
_m4storage[15] = t4;
746-
}
747-
748-
/// Translate this matrix by a [Vector4].
749-
void translateByVector4(Vector4 v4) {
750-
final tx = v4.x;
751-
final ty = v4.y;
752-
final tz = v4.z;
753-
final tw = v4.w;
754-
692+
@pragma('wasm:prefer-inline')
693+
@pragma('vm:prefer-inline')
694+
@pragma('dart2js:prefer-inline')
695+
void translateByDouble(double tx, double ty, double tz, double tw) {
755696
final t1 = _m4storage[0] * tx +
756697
_m4storage[4] * ty +
757698
_m4storage[8] * tz +
@@ -777,6 +718,20 @@ class Matrix4 {
777718
_m4storage[15] = t4;
778719
}
779720

721+
/// Translate this matrix by a [Vector3].
722+
@pragma('wasm:prefer-inline')
723+
@pragma('vm:prefer-inline')
724+
@pragma('dart2js:prefer-inline')
725+
void translateByVector3(Vector3 v3) =>
726+
translateByDouble(v3.x, v3.y, v3.z, 1.0);
727+
728+
/// Translate this matrix by a [Vector4].
729+
@pragma('wasm:prefer-inline')
730+
@pragma('vm:prefer-inline')
731+
@pragma('dart2js:prefer-inline')
732+
void translateByVector4(Vector4 v4) =>
733+
translateByDouble(v4.x, v4.y, v4.z, v4.w);
734+
780735
/// Multiply this by a translation from the left.
781736
///
782737
/// The translation can be specified with a [Vector3], [Vector4], or x, y, z
@@ -794,77 +749,17 @@ class Matrix4 {
794749
} else if (x is Vector4) {
795750
leftTranslateByVector4(x);
796751
} else if (x is double) {
797-
leftTranslateByDouble(x, y, z);
752+
leftTranslateByDouble(x, y, z, 1.0);
798753
} else {
799754
throw UnimplementedError();
800755
}
801756
}
802757

803758
/// Multiply this by a translation from the left.
804-
void leftTranslateByDouble(double tx, double ty, double tz) {
805-
// Column 1
806-
final r1 = _m4storage[3];
807-
_m4storage[0] += tx * r1;
808-
_m4storage[1] += ty * r1;
809-
_m4storage[2] += tz * r1;
810-
811-
// Column 2
812-
final r2 = _m4storage[7];
813-
_m4storage[4] += tx * r2;
814-
_m4storage[5] += ty * r2;
815-
_m4storage[6] += tz * r2;
816-
817-
// Column 3
818-
final r3 = _m4storage[11];
819-
_m4storage[8] += tx * r3;
820-
_m4storage[9] += ty * r3;
821-
_m4storage[10] += tz * r3;
822-
823-
// Column 4
824-
final r4 = _m4storage[15];
825-
_m4storage[12] += tx * r4;
826-
_m4storage[13] += ty * r4;
827-
_m4storage[14] += tz * r4;
828-
}
829-
830-
/// Multiply this by a translation from the left.
831-
void leftTranslateByVector3(Vector3 v3) {
832-
final tx = v3.x;
833-
final ty = v3.y;
834-
final tz = v3.z;
835-
836-
// Column 1
837-
final r1 = _m4storage[3];
838-
_m4storage[0] += tx * r1;
839-
_m4storage[1] += ty * r1;
840-
_m4storage[2] += tz * r1;
841-
842-
// Column 2
843-
final r2 = _m4storage[7];
844-
_m4storage[4] += tx * r2;
845-
_m4storage[5] += ty * r2;
846-
_m4storage[6] += tz * r2;
847-
848-
// Column 3
849-
final r3 = _m4storage[11];
850-
_m4storage[8] += tx * r3;
851-
_m4storage[9] += ty * r3;
852-
_m4storage[10] += tz * r3;
853-
854-
// Column 4
855-
final r4 = _m4storage[15];
856-
_m4storage[12] += tx * r4;
857-
_m4storage[13] += ty * r4;
858-
_m4storage[14] += tz * r4;
859-
}
860-
861-
/// Multiply this by a translation from the left.
862-
void leftTranslateByVector4(Vector4 v4) {
863-
final tx = v4.x;
864-
final ty = v4.y;
865-
final tz = v4.z;
866-
final tw = v4.w;
867-
759+
@pragma('wasm:prefer-inline')
760+
@pragma('vm:prefer-inline')
761+
@pragma('dart2js:prefer-inline')
762+
void leftTranslateByDouble(double tx, double ty, double tz, double tw) {
868763
// Column 1
869764
final r1 = _m4storage[3];
870765
_m4storage[0] += tx * r1;
@@ -894,6 +789,20 @@ class Matrix4 {
894789
_m4storage[15] = tw * r4;
895790
}
896791

792+
/// Multiply this by a translation from the left.
793+
@pragma('wasm:prefer-inline')
794+
@pragma('vm:prefer-inline')
795+
@pragma('dart2js:prefer-inline')
796+
void leftTranslateByVector3(Vector3 v3) =>
797+
leftTranslateByDouble(v3.x, v3.y, v3.z, 1.0);
798+
799+
/// Multiply this by a translation from the left.
800+
@pragma('wasm:prefer-inline')
801+
@pragma('vm:prefer-inline')
802+
@pragma('dart2js:prefer-inline')
803+
void leftTranslateByVector4(Vector4 v4) =>
804+
leftTranslateByDouble(v4.x, v4.y, v4.z, v4.w);
805+
897806
/// Rotate this [angle] radians around [axis]
898807
void rotate(Vector3 axis, double angle) {
899808
final len = axis.length;

0 commit comments

Comments
 (0)