Skip to content

Commit

Permalink
Fixed getMatrix3d conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneypattison committed Jan 25, 2024
1 parent b73c36e commit f2b4a5f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
5 changes: 3 additions & 2 deletions app/_lib/get-matrix3d.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ test("coverts 3x3 matrix to 4x4 matrix", () => {
{ x: 0, y: 300 },
];
const openCVMatrix = [
2.86405074, 1.89358727e-1, -2.04720453e2, 0, 7.10878995e-2, 2.79612405,
-1.6296901e3, 0, 1.60734029e-4, 1.73337133e-4, 1.0, 0, 0, 0, 0, 1,
2.864050742318774, 0.07108789949297858, 0, 0.00016073402857508592,
0.1893587267624101, 2.7961240467325417, 0, 0.00017333713252165822, 0, 0, 1,
0, -204.72045347183263, -1629.6900958816486, 0, 1,
];

const matrix = getMatrix3d(src, dst);
Expand Down
30 changes: 10 additions & 20 deletions app/_lib/get-matrix3d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AbstractMatrix } from "ml-matrix";

import getPerspectiveTransform from "./get-perspective-transform";
import Point from "./point";

Expand All @@ -8,26 +10,14 @@ import Point from "./point";
* @returns A 4x4 matrix of a perspective transform flattened into a array of length 16.
*/
export default function getMatrix3d(src: Point[], dst: Point[]): number[] {
const perspectiveTransform = getPerspectiveTransform(src, dst);
const matrix3d = Array<number>(16);
var m = AbstractMatrix.from1DArray(3, 3, getPerspectiveTransform(src, dst));

for (let i = 0, j = 0; i < matrix3d.length; i++) {
switch (i) {
case 3:
case 7:
case 11:
case 12:
case 13:
case 14:
matrix3d[i] = 0;
break;
case 15:
matrix3d[i] = 1;
break;
default:
matrix3d[i] = perspectiveTransform[j++];
}
}
// Make the 3x3 into 4x4 by inserting a z component in the 3rd column.
m.addColumn(2, AbstractMatrix.zeros(1, 3));
m.addRow(2, AbstractMatrix.from1DArray(1, 4, [0, 0, 1, 0]));

return matrix3d;
// Transpose since CSS.matrix3d is column major.
m = m.transpose();
console.log(Array.from(m));
return m.to1DArray();
}

0 comments on commit f2b4a5f

Please sign in to comment.