Skip to content

Commit

Permalink
fix(🐛): fix mapPoint3d implementation (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Jan 6, 2024
1 parent 5cff0da commit 2cc3c56
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
39 changes: 28 additions & 11 deletions src/Matrix4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
Expand Down Expand Up @@ -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),
Expand All @@ -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],
];
};

Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/Matrix4.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
concat4,
identity4,
translate,
mapPoint3d,
matrixVecMul4,
multiply4,
Expand Down Expand Up @@ -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);
});
});
10 changes: 5 additions & 5 deletions src/__tests__/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};

Expand Down

0 comments on commit 2cc3c56

Please sign in to comment.