Skip to content

Commit

Permalink
add get3DScaling
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Sep 7, 2024
1 parent 0ca212e commit d88420a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/mat3-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import { QuatArg } from './quat';
import { Mat3Arg, Mat3Type } from './mat3';
import { Mat4Arg } from './mat4';
import { Vec2Arg } from './vec2';
import { Vec3Arg } from './vec3';
import { getAPI as getVec2API } from './vec2-impl';
import { getAPI as getVec3API } from './vec3-impl';
import { BaseArgType } from './types';

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

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

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

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


/**
* Returns the "3d" scaling component of the matrix
* @param m - The Matrix
* @param dst - The vector to set. If not passed a new one is created.
*/
function get3DScaling<T extends Vec3Arg = MatType>(m: Mat3Arg, dst?: T) {
const newDst = (dst ?? vec3.create());

const xx = m[0];
const xy = m[1];
const xz = m[2];
const yx = m[4];
const yy = m[5];
const yz = m[6];
const zx = m[8];
const zy = m[9];
const zz = m[10];

newDst[0] = Math.sqrt(xx * xx + xy * xy + xz * xz);
newDst[1] = Math.sqrt(yx * yx + yy * yy + yz * yz);
newDst[2] = Math.sqrt(zx * zx + zy * zy + zz * zz);

return newDst;
}

/**
* Creates a 3-by-3 matrix which translates by the given vector v.
* @param v - The vector by which to translate.
Expand Down Expand Up @@ -750,6 +779,7 @@ return {
getAxis,
setAxis,
getScaling,
get3DScaling,
translation,
translate,
rotation,
Expand Down
35 changes: 35 additions & 0 deletions test/tests/mat3-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ function check(mat3, Type) {
testV2WithDest(func, expected);
}

function testV3WithoutDest(func, expected) {
const d = func();
assertEqual(d, expected);
}

function testV3WithDest(func, expected) {
const d = new Type(3).fill(0);
const c = func(d);
assertStrictEqual(c, d);
assertEqual(c, expected);
}

function testVec3WithAndWithoutDest(func, expected) {
expected = createCopyOfType(expected);
testV3WithoutDest(func, expected);
testV3WithDest(func, expected);
}

it('should create', () => {
const tests = [
{e: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], args: []},
Expand Down Expand Up @@ -365,6 +383,23 @@ function check(mat3, Type) {
}, expected);
});

it('should get 3D scaling', () => {
const m = [
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16,
];
const expected = [
Math.sqrt(1 * 1 + 2 * 2 + 3 * 3),
Math.sqrt(5 * 5 + 6 * 6 + 7 * 7),
Math.sqrt(9 * 9 + 10 * 10 + 11 * 11),
];
testVec3WithAndWithoutDest((newDst) => {
return mat3.get3DScaling(m, newDst);
}, expected);
});

it('should make translation matrix', () => {
const expected = [
1, 0, 0, 0,
Expand Down

0 comments on commit d88420a

Please sign in to comment.