Skip to content

Commit d88420a

Browse files
committed
add get3DScaling
1 parent 0ca212e commit d88420a

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

src/mat3-impl.ts

+35-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import { QuatArg } from './quat';
2525
import { Mat3Arg, Mat3Type } from './mat3';
2626
import { Mat4Arg } from './mat4';
2727
import { Vec2Arg } from './vec2';
28+
import { Vec3Arg } from './vec3';
2829
import { getAPI as getVec2API } from './vec2-impl';
30+
import { getAPI as getVec3API } from './vec3-impl';
2931
import { BaseArgType } from './types';
3032

3133
export { Mat3Arg, Mat3Type };
@@ -37,6 +39,7 @@ type Mat3Ctor<T extends Mat3Arg = Float32Array> = new (n: number) => T;
3739
* */
3840
function getAPIImpl<MatType extends Mat3Arg = Float32Array>(Ctor: Mat3Ctor<MatType>) {
3941
const vec2 = getVec2API<MatType>(Ctor);
42+
const vec3 = getVec3API<MatType>(Ctor);
4043

4144
/**
4245
* Create a Mat3 from values
@@ -502,11 +505,11 @@ function setAxis<T extends Mat3Arg = MatType>(m: Mat3Arg, v: Vec2Arg, axis: numb
502505
return newDst;
503506
}
504507

505-
///**
506-
// * Returns the scaling component of the matrix
507-
// * @param m - The Matrix
508-
// * @param dst - The vector to set. If not passed a new one is created.
509-
// */
508+
/**
509+
* Returns the "2d" scaling component of the matrix
510+
* @param m - The Matrix
511+
* @param dst - The vector to set. If not passed a new one is created.
512+
*/
510513
function getScaling<T extends Vec2Arg = MatType>(m: Mat3Arg, dst?: T) {
511514
const newDst = (dst ?? vec2.create());
512515

@@ -521,6 +524,32 @@ function getScaling<T extends Vec2Arg = MatType>(m: Mat3Arg, dst?: T) {
521524
return newDst;
522525
}
523526

527+
528+
/**
529+
* Returns the "3d" scaling component of the matrix
530+
* @param m - The Matrix
531+
* @param dst - The vector to set. If not passed a new one is created.
532+
*/
533+
function get3DScaling<T extends Vec3Arg = MatType>(m: Mat3Arg, dst?: T) {
534+
const newDst = (dst ?? vec3.create());
535+
536+
const xx = m[0];
537+
const xy = m[1];
538+
const xz = m[2];
539+
const yx = m[4];
540+
const yy = m[5];
541+
const yz = m[6];
542+
const zx = m[8];
543+
const zy = m[9];
544+
const zz = m[10];
545+
546+
newDst[0] = Math.sqrt(xx * xx + xy * xy + xz * xz);
547+
newDst[1] = Math.sqrt(yx * yx + yy * yy + yz * yz);
548+
newDst[2] = Math.sqrt(zx * zx + zy * zy + zz * zz);
549+
550+
return newDst;
551+
}
552+
524553
/**
525554
* Creates a 3-by-3 matrix which translates by the given vector v.
526555
* @param v - The vector by which to translate.
@@ -750,6 +779,7 @@ return {
750779
getAxis,
751780
setAxis,
752781
getScaling,
782+
get3DScaling,
753783
translation,
754784
translate,
755785
rotation,

test/tests/mat3-test.js

+35
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ function check(mat3, Type) {
7070
testV2WithDest(func, expected);
7171
}
7272

73+
function testV3WithoutDest(func, expected) {
74+
const d = func();
75+
assertEqual(d, expected);
76+
}
77+
78+
function testV3WithDest(func, expected) {
79+
const d = new Type(3).fill(0);
80+
const c = func(d);
81+
assertStrictEqual(c, d);
82+
assertEqual(c, expected);
83+
}
84+
85+
function testVec3WithAndWithoutDest(func, expected) {
86+
expected = createCopyOfType(expected);
87+
testV3WithoutDest(func, expected);
88+
testV3WithDest(func, expected);
89+
}
90+
7391
it('should create', () => {
7492
const tests = [
7593
{e: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], args: []},
@@ -365,6 +383,23 @@ function check(mat3, Type) {
365383
}, expected);
366384
});
367385

386+
it('should get 3D scaling', () => {
387+
const m = [
388+
1, 2, 3, 4,
389+
5, 6, 7, 8,
390+
9, 10, 11, 12,
391+
13, 14, 15, 16,
392+
];
393+
const expected = [
394+
Math.sqrt(1 * 1 + 2 * 2 + 3 * 3),
395+
Math.sqrt(5 * 5 + 6 * 6 + 7 * 7),
396+
Math.sqrt(9 * 9 + 10 * 10 + 11 * 11),
397+
];
398+
testVec3WithAndWithoutDest((newDst) => {
399+
return mat3.get3DScaling(m, newDst);
400+
}, expected);
401+
});
402+
368403
it('should make translation matrix', () => {
369404
const expected = [
370405
1, 0, 0, 0,

0 commit comments

Comments
 (0)