From 2cc3c56c03ca6bfbd0c1ea2b542da16dbd0bb282 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Sat, 6 Jan 2024 13:28:36 +0100 Subject: [PATCH] =?UTF-8?q?fix(=F0=9F=90=9B):=20fix=20mapPoint3d=20impleme?= =?UTF-8?q?ntation=20(#521)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Matrix4.ts | 39 +++++++++++++++++++++++++---------- src/__tests__/Matrix4.test.ts | 13 ++++++++++++ src/__tests__/matrix.ts | 10 ++++----- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/Matrix4.ts b/src/Matrix4.ts index d7888c4..6c7f296 100644 --- a/src/Matrix4.ts +++ b/src/Matrix4.ts @@ -78,27 +78,42 @@ export const identity4: Matrix4 = [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, ]; -const translate = (x: number, y: number, z: number): Matrix4 => { +/** + * @worklet + */ +export const translate = (x: number, y: number, z: number): Matrix4 => { "worklet"; return [1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1]; }; +/** + * @worklet + */ const scale = (sx: number, sy: number, sz: number): Matrix4 => { "worklet"; return [sx, 0, 0, 0, 0, sy, 0, 0, 0, 0, sz, 0, 0, 0, 0, 1]; }; -const skewX = (s: number): Matrix4 => { +/** + * @worklet + */ +export const skewX = (s: number): Matrix4 => { "worklet"; return [1, 0, 0, 0, Math.tan(s), 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; }; -const skewY = (s: number): Matrix4 => { +/** + * @worklet + */ +export const skewY = (s: number): Matrix4 => { "worklet"; return [1, Math.tan(s), 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; }; -const perspective = (p: number): Matrix4 => { +/** + * @worklet + */ +export const perspective = (p: number): Matrix4 => { "worklet"; return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -1 / p, 1]; }; @@ -144,7 +159,10 @@ const rotatedUnitSinCos = ( ]; }; -const rotate = (axis: Vec3, value: number) => { +/** + * @worklet + */ +export const rotate = (axis: Vec3, value: number) => { "worklet"; return rotatedUnitSinCos( normalizeVec(axis), @@ -156,14 +174,13 @@ const rotate = (axis: Vec3, value: number) => { /** * @worklet */ -export const matrixVecMul4 = (m: Matrix4, v: Vec4) => { +export const matrixVecMul4 = (m: Matrix4, v: Vec4): Vec4 => { "worklet"; - const [vx, vy, vz, vw] = v; return [ - vx * m[0] + vy * m[4] + vz * m[8] + vw * m[12], - vx * m[1] + vy * m[5] + vz * m[9] + vw * m[13], - vx * m[2] + vy * m[6] + vz * m[10] + vw * m[14], - vx * m[3] + vy * m[7] + vz * m[11] + vw * m[15], + m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3] * v[3], + m[4] * v[0] + m[5] * v[1] + m[6] * v[2] + m[7] * v[3], + m[8] * v[0] + m[9] * v[1] + m[10] * v[2] + m[11] * v[3], + m[12] * v[0] + m[13] * v[1] + m[14] * v[2] + m[15] * v[3], ]; }; diff --git a/src/__tests__/Matrix4.test.ts b/src/__tests__/Matrix4.test.ts index 2a5932b..df5a81b 100644 --- a/src/__tests__/Matrix4.test.ts +++ b/src/__tests__/Matrix4.test.ts @@ -1,6 +1,7 @@ import { concat4, identity4, + translate, mapPoint3d, matrixVecMul4, multiply4, @@ -139,4 +140,16 @@ describe("4x4 matrices", () => { ]) ); }); + it("should correctly transform a point with an identity matrix", () => { + const point = [100, -100, 200] as const; // Define some test point + const result = mapPoint3d(identity4, point); + expect(result).toEqual(point); + }); + it("should correctly transform a point with a translation matrix", () => { + const translationMatrix = translate(100, 100, 100); + const point = [100, -100, 200] as const; // Define some test point + const expectedResult = [200, 0, 300] as const; + const result = mapPoint3d(translationMatrix, point); + expect(result).toEqual(expectedResult); + }); }); diff --git a/src/__tests__/matrix.ts b/src/__tests__/matrix.ts index e4e8006..e03a506 100644 --- a/src/__tests__/matrix.ts +++ b/src/__tests__/matrix.ts @@ -150,11 +150,11 @@ const rotatedUnitSinCos = ( ]; }; -export const transformPoint = (m4: number[], t: number[]): number[] => { - const x = m4[0] * t[0] + m4[4] * t[1] + m4[8] * t[2] + m4[12] * t[3]; - const y = m4[1] * t[0] + m4[5] * t[1] + m4[9] * t[2] + m4[13] * t[3]; - const z = m4[2] * t[0] + m4[6] * t[1] + m4[10] * t[2] + m4[14] * t[3]; - const w = m4[3] * t[0] + m4[7] * t[1] + m4[11] * t[2] + m4[15] * t[3]; +export const transformPoint = (m: number[], t: number[]): number[] => { + const x = m[0] * t[0] + m[1] * t[1] + m[2] * t[2] + m[3] * t[3]; + const y = m[4] * t[0] + m[5] * t[1] + m[6] * t[2] + m[7] * t[3]; + const z = m[8] * t[0] + m[9] * t[1] + m[10] * t[2] + m[11] * t[3]; + const w = m[12] * t[0] + m[13] * t[1] + m[14] * t[2] + m[15] * t[3]; return [x, y, z, w]; };