Skip to content

Commit 93026bf

Browse files
committed
add mat3 scale3D, scaling3D, uniformScale3D, uniformScaling3D
1 parent 7a7e4c1 commit 93026bf

File tree

2 files changed

+145
-4
lines changed

2 files changed

+145
-4
lines changed

src/mat3-impl.ts

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ const rotateZ = rotate;
792792

793793
/**
794794
* Creates a 3-by-3 matrix which scales in each dimension by an amount given by
795-
* the corresponding entry in the given vector; assumes the vector has three
795+
* the corresponding entry in the given vector; assumes the vector has two
796796
* entries.
797797
* @param v - A vector of
798798
* 2 entries specifying the factor by which to scale in each dimension.
@@ -812,7 +812,7 @@ function scaling<T extends Mat3Arg = MatType>(v: Vec2Arg, dst?: T) {
812812
/**
813813
* Scales the given 3-by-3 matrix in each dimension by an amount
814814
* given by the corresponding entry in the given vector; assumes the vector has
815-
* three entries.
815+
* two entries.
816816
* @param m - The matrix to be modified.
817817
* @param v - A vector of 2 entries specifying the
818818
* factor by which to scale in each dimension.
@@ -843,7 +843,58 @@ function scale<T extends Mat3Arg = MatType>(m: Mat3Arg, v: Vec2Arg, dst?: T) {
843843
}
844844

845845
/**
846-
* Creates a 3-by-3 matrix which scales uniformly in each dimension
846+
* Creates a 3-by-3 matrix which scales in each dimension by an amount given by
847+
* the corresponding entry in the given vector; assumes the vector has three
848+
* entries.
849+
* @param v - A vector of
850+
* 3 entries specifying the factor by which to scale in each dimension.
851+
* @param dst - matrix to hold result. If not passed a new one is created.
852+
* @returns The scaling matrix.
853+
*/
854+
function scaling3D<T extends Mat3Arg = MatType>(v: Vec3Arg, dst?: T) {
855+
const newDst = (dst ?? new Ctor(12)) as T;
856+
857+
newDst[ 0] = v[0]; newDst[ 1] = 0; newDst[ 2] = 0;
858+
newDst[ 4] = 0; newDst[ 5] = v[1]; newDst[ 6] = 0;
859+
newDst[ 8] = 0; newDst[ 9] = 0; newDst[10] = v[2];
860+
861+
return newDst;
862+
}
863+
864+
/**
865+
* Scales the given 3-by-3 matrix in each dimension by an amount
866+
* given by the corresponding entry in the given vector; assumes the vector has
867+
* three entries.
868+
* @param m - The matrix to be modified.
869+
* @param v - A vector of 3 entries specifying the
870+
* factor by which to scale in each dimension.
871+
* @param dst - matrix to hold result. If not passed a new one is created.
872+
* @returns The scaled matrix.
873+
*/
874+
function scale3D<T extends Mat3Arg = MatType>(m: Mat3Arg, v: Vec3Arg, dst?: T) {
875+
const newDst = (dst ?? new Ctor(12)) as T;
876+
877+
const v0 = v[0];
878+
const v1 = v[1];
879+
const v2 = v[2];
880+
881+
newDst[ 0] = v0 * m[0 * 4 + 0];
882+
newDst[ 1] = v0 * m[0 * 4 + 1];
883+
newDst[ 2] = v0 * m[0 * 4 + 2];
884+
885+
newDst[ 4] = v1 * m[1 * 4 + 0];
886+
newDst[ 5] = v1 * m[1 * 4 + 1];
887+
newDst[ 6] = v1 * m[1 * 4 + 2];
888+
889+
newDst[ 8] = v2 * m[2 * 4 + 0];
890+
newDst[ 9] = v2 * m[2 * 4 + 1];
891+
newDst[10] = v2 * m[2 * 4 + 2];
892+
893+
return newDst;
894+
}
895+
896+
/**
897+
* Creates a 3-by-3 matrix which scales uniformly in the X and Y dimensions
847898
* @param s - Amount to scale
848899
* @param dst - matrix to hold result. If not passed a new one is created.
849900
* @returns The scaling matrix.
@@ -859,7 +910,7 @@ function uniformScaling<T extends Mat3Arg = MatType>(s: number, dst?: T) {
859910
}
860911

861912
/**
862-
* Scales the given 3-by-3 matrix in each dimension by an amount
913+
* Scales the given 3-by-3 matrix in the X and Y dimension by an amount
863914
* given.
864915
* @param m - The matrix to be modified.
865916
* @param s - Amount to scale.
@@ -886,6 +937,48 @@ function uniformScale<T extends Mat3Arg = MatType>(m: Mat3Arg, s: number, dst?:
886937
return newDst;
887938
}
888939

940+
/**
941+
* Creates a 3-by-3 matrix which scales uniformly in each dimension
942+
* @param s - Amount to scale
943+
* @param dst - matrix to hold result. If not passed a new one is created.
944+
* @returns The scaling matrix.
945+
*/
946+
function uniformScaling3D<T extends Mat3Arg = MatType>(s: number, dst?: T) {
947+
const newDst = (dst ?? new Ctor(12)) as T;
948+
949+
newDst[ 0] = s; newDst[ 1] = 0; newDst[ 2] = 0;
950+
newDst[ 4] = 0; newDst[ 5] = s; newDst[ 6] = 0;
951+
newDst[ 8] = 0; newDst[ 9] = 0; newDst[10] = s;
952+
953+
return newDst;
954+
}
955+
956+
/**
957+
* Scales the given 3-by-3 matrix in each dimension by an amount
958+
* given.
959+
* @param m - The matrix to be modified.
960+
* @param s - Amount to scale.
961+
* @param dst - matrix to hold result. If not passed a new one is created.
962+
* @returns The scaled matrix.
963+
*/
964+
function uniformScale3D<T extends Mat3Arg = MatType>(m: Mat3Arg, s: number, dst?: T) {
965+
const newDst = (dst ?? new Ctor(12)) as T;
966+
967+
newDst[ 0] = s * m[0 * 4 + 0];
968+
newDst[ 1] = s * m[0 * 4 + 1];
969+
newDst[ 2] = s * m[0 * 4 + 2];
970+
971+
newDst[ 4] = s * m[1 * 4 + 0];
972+
newDst[ 5] = s * m[1 * 4 + 1];
973+
newDst[ 6] = s * m[1 * 4 + 2];
974+
975+
newDst[ 8] = s * m[2 * 4 + 0];
976+
newDst[ 9] = s * m[2 * 4 + 1];
977+
newDst[10] = s * m[2 * 4 + 2];
978+
979+
return newDst;
980+
}
981+
889982
return {
890983
clone,
891984
create,
@@ -923,6 +1016,10 @@ return {
9231016
scale,
9241017
uniformScaling,
9251018
uniformScale,
1019+
scaling3D,
1020+
scale3D,
1021+
uniformScaling3D,
1022+
uniformScale3D,
9261023
};
9271024

9281025
}

test/tests/mat3-test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,28 @@ function check(mat3, Type) {
542542
}, expected);
543543
});
544544

545+
it('should make 3D scaling matrix', () => {
546+
const expected = [
547+
2, 0, 0, 0,
548+
0, 3, 0, 0,
549+
0, 0, 4, 0,
550+
];
551+
testMat3WithAndWithoutDest((newDst) => {
552+
return mat3.scaling3D([2, 3, 4], newDst);
553+
}, expected);
554+
});
555+
556+
it('should scale 3D', () => {
557+
const expected = [
558+
0, 2, 4, 0,
559+
12, 15, 18, 0,
560+
32, 36, 40, 0,
561+
];
562+
testMat3WithAndWithoutDest((newDst) => {
563+
return mat3.scale3D(m, [2, 3, 4], newDst);
564+
}, expected);
565+
});
566+
545567
it('should make uniform scaling matrix', () => {
546568
const expected = [
547569
2, 0, 0, 0,
@@ -564,6 +586,28 @@ function check(mat3, Type) {
564586
}, expected);
565587
});
566588

589+
it('should make uniform scaling 3D matrix', () => {
590+
const expected = [
591+
2, 0, 0, 0,
592+
0, 2, 0, 0,
593+
0, 0, 2, 0,
594+
];
595+
testMat3WithAndWithoutDest((newDst) => {
596+
return mat3.uniformScaling3D(2, newDst);
597+
}, expected);
598+
});
599+
600+
it('should uniformly scale 3D', () => {
601+
const expected = [
602+
0, 2, 4, 0,
603+
8, 10, 12, 0,
604+
16, 18, 20, 0,
605+
];
606+
testMat3WithAndWithoutDest((newDst) => {
607+
return mat3.uniformScale3D(m, 2, newDst);
608+
}, expected);
609+
});
610+
567611
it('should make a mat3 from mat4', () => {
568612
const expected = [
569613
1, 2, 3, 0,

0 commit comments

Comments
 (0)