From 9fb7235d93ef530e725e0c571e4645e2ba852d73 Mon Sep 17 00:00:00 2001 From: crosstyan Date: Tue, 25 Mar 2025 10:17:08 +0800 Subject: [PATCH] Refactor preProcessExtrinsic function to handle both Matrix4 and array inputs for extrinsic parameters. Add error handling for invalid inputs and update transformation sequence for camera-to-world conversion. --- src/App.tsx | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index e4974fa..74903a4 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -119,12 +119,14 @@ const Scene = () => { let Rt: THREE.Matrix4 if (extrinsic instanceof THREE.Matrix4) { Rt = extrinsic - } else { + } else if (Array.isArray(extrinsic)) { + console.assert(extrinsic.length === 16, "extrinsic must be a 4x4 matrix") Rt = new THREE.Matrix4() // @ts-expect-error 16 elements Rt.set(...extrinsic) + } else { + throw new Error("extrinsic must be a 4x4 matrix or an array of 16 elements") } - console.assert(Rt.elements.length === 16, "extrinsic must be a 4x4 matrix") const font = new FontLoader().parse(HelvetikerRegular) const camera = new THREE.PerspectiveCamera(fov ?? 60, aspect ?? 4 / 3, near ?? DEFAULT_NEAR, far ?? DEFAULT_FAR) const helper = @@ -161,17 +163,25 @@ const Scene = () => { ) } - const preProcessExtrinsic = (extrinsic: number[]) => { - // Create the initial world-to-camera transform - const worldToCamera = new THREE.Matrix4() - // @ts-expect-error 16 elements - worldToCamera.set(...extrinsic) + const preProcessExtrinsic = (extrinsic: number[] | THREE.Matrix4) => { + let Rt: THREE.Matrix4 + if (extrinsic instanceof THREE.Matrix4) { + Rt = extrinsic + } else if (Array.isArray(extrinsic)) { + console.assert(extrinsic.length === 16, "extrinsic must be a 4x4 matrix") + Rt = new THREE.Matrix4() + // @ts-expect-error 16 elements + Rt.set(...extrinsic) + } else { + throw new Error("extrinsic must be a 4x4 matrix or an array of 16 elements") + } - // Convert from Z-up to Y-up first (this affects world coordinates) - const worldZupToYup = Z_UP_TO_Y_UP.clone() // Then handle OpenCV to OpenGL camera convention - const cameraConversion = CV_TO_GL_MAT.clone() + const cameraCvt = CV_TO_GL_MAT.clone() + + // Convert from Z-up to Y-up first (this affects world coordinates) + const worldCvt = Z_UP_TO_Y_UP.clone() // Final transformation: // 1. Convert world from Z-up to Y-up @@ -179,9 +189,9 @@ const Scene = () => { // 3. Convert camera coordinates from OpenCV to OpenGL const final = new THREE.Matrix4() final - .multiply(cameraConversion) - .multiply(worldToCamera) - .multiply(worldZupToYup) + .multiply(cameraCvt) + .multiply(Rt) + .multiply(worldCvt) // Invert to get the camera-to-world transform final.invert()