import macro from 'vtk.js/Sources/macros'; import vtkMouseCameraTrackballPanManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraTrackballPanManipulator'; import { mat4, vec3 } from 'gl-matrix';
function transformVectorByTransformation( tempObjects, beforeMatrix, afterMatrix, vector ) { const { matrixA, matrixB, newCenter } = tempObjects;
mat4.transpose(matrixA, beforeMatrix);
mat4.transpose(matrixB, afterMatrix); mat4.invert(matrixB, matrixB);
mat4.multiply(matrixA, matrixB, matrixA);
vec3.transformMat4(newCenter, vector, matrixA); return newCenter; }
function computeNewCenterOfRotation( tempObjects, renderer, beforeCameraMatrix, oldCenterOfRotation ) { const cam = renderer.getActiveCamera(); if (!cam || !beforeCameraMatrix) { return oldCenterOfRotation; } const afterMatrixRowMajor = cam.getViewMatrix();
return transformVectorByTransformation( tempObjects, beforeCameraMatrix, afterMatrixRowMajor, oldCenterOfRotation ); }
function getCameraMatrix(renderer, tempMatrix) { const cam = renderer.getActiveCamera(); if (cam) { mat4.copy(tempMatrix, cam.getViewMatrix()); return tempMatrix; } return null; }
function vtkMouseCameraTrackballPanManipulatorAutoCenter(publicAPI, model) { model.classHierarchy.push('vtkMouseCameraTrackballPanManipulatorAutoCenter');
const tempCameraMatrix = mat4.create(); const tempComputeObjects = { matrixA: mat4.create(), matrixB: mat4.create(), newCenter: vec3.create(), };
const superOnMouseMove = publicAPI.onMouseMove;
publicAPI.onMouseMove = (interactor, renderer, position) => { if (!position) { return; } const beforeCameraMatrix = getCameraMatrix(renderer, tempCameraMatrix);
superOnMouseMove(interactor, renderer, position);
if (beforeCameraMatrix && model.center) { const newCenter = computeNewCenterOfRotation( tempComputeObjects, renderer, beforeCameraMatrix, model.center ); publicAPI.setCenter(newCenter);
const style = interactor.getInteractorStyle(); if (style && style.setCenterOfRotation) { style.setCenterOfRotation(newCenter); } } }; }
const DEFAULT_VALUES = {};
export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues);
vtkMouseCameraTrackballPanManipulator.extend(publicAPI, model, initialValues);
vtkMouseCameraTrackballPanManipulatorAutoCenter(publicAPI, model); }
export const newInstance = macro.newInstance( extend, 'vtkMouseCameraTrackballPanManipulatorAutoCenter' );
export default { newInstance, extend };
|