Skip to content

Revert center of rotation change after pan #3310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import macro from 'vtk.js/Sources/macros';
import vtkMouseCameraTrackballPanManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraTrackballPanManipulator';
import { mat4, vec3 } from 'gl-matrix';

// ----------------------------------------------------------------------------
// Helper functions for center of rotation adjustment
// ----------------------------------------------------------------------------

/**
* Transforms a vector by the transformation delta between two matrices.
*
* @param {Object} tempObjects - Temporary matrices/vectors for computation
* @param {mat4} beforeMatrix - Matrix before transformation
* @param {mat4} afterMatrix - Matrix after transformation
* @param {Array} vector - Vector to transform [x, y, z]
* @returns {Array} Transformed vector [x, y, z]
*/
function transformVectorByTransformation(
tempObjects,
beforeMatrix,
afterMatrix,
vector
) {
const { matrixA, matrixB, newCenter } = tempObjects;

// The view matrix from vtk.js is row-major, but gl-matrix expects column-major.
// We need to transpose them before use.
mat4.transpose(matrixA, beforeMatrix);

mat4.transpose(matrixB, afterMatrix);
mat4.invert(matrixB, matrixB);

// Compute delta transformation matrix
mat4.multiply(matrixA, matrixB, matrixA);

vec3.transformMat4(newCenter, vector, matrixA);
return newCenter;
}

/**
* Computes the new center of rotation based on camera movement.
* When the camera moves (pan), the center of rotation should move
* by the same transformation.
*
* @param {Object} tempObjects - Temporary matrices/vectors for computation
* @param {Object} renderer - VTK renderer
* @param {mat4} beforeCameraMatrix - Camera view matrix before movement
* @param {Array} oldCenterOfRotation - Previous center of rotation [x, y, z]
* @returns {Array} New center of rotation [x, y, z]
*/
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);
}
}
};
}

// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------

const DEFAULT_VALUES = {};

// ----------------------------------------------------------------------------

export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);

// Inheritance
vtkMouseCameraTrackballPanManipulator.extend(publicAPI, model, initialValues);

// Object specific methods
vtkMouseCameraTrackballPanManipulatorAutoCenter(publicAPI, model);
}

// ----------------------------------------------------------------------------

export const newInstance = macro.newInstance(
extend,
'vtkMouseCameraTrackballPanManipulatorAutoCenter'
);

// ----------------------------------------------------------------------------

export default { newInstance, extend };
2 changes: 2 additions & 0 deletions Sources/Interaction/Style/InteractorStyleManipulator/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ sent to it. Also, changing the CenterOfRotation during interaction i.e. after a
button press but before a button up has no effect until the next button press. The
default value is [0, 0, 0].

For automatic center adjustment during panning operations, use `MouseCameraTrackballPanManipulatorAutoCenter` instead of the standard `MouseCameraTrackballPanManipulator`.

### rotationFactor

Set/Get the rotation factor. Propagates the rotation factor to the manipulators.
Expand Down
104 changes: 2 additions & 102 deletions Sources/Interaction/Style/InteractorStyleManipulator/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import macro from 'vtk.js/Sources/macros';
import { MouseButton } from 'vtk.js/Sources/Rendering/Core/RenderWindowInteractor/Constants';
import vtkInteractorStyle from 'vtk.js/Sources/Rendering/Core/InteractorStyle';
import { mat4, vec3 } from 'gl-matrix';

const { vtkDebugMacro } = macro;
const { States } = vtkInteractorStyle;
Expand Down Expand Up @@ -135,77 +134,6 @@ function dollyByFactor(interactor, renderer, factor) {
}
}

function getCameraMatrix(renderer, tempMatrix) {
const cam = renderer.getActiveCamera();
if (cam) {
mat4.copy(tempMatrix, cam.getViewMatrix());
return tempMatrix;
}
return null;
}

/**
* Transforms a vector by the transformation delta between two matrices.
*
* @param {Object} tempObjects - Temporary matrices/vectors for computation
* @param {mat4} beforeMatrix - Matrix before transformation
* @param {mat4} afterMatrix - Matrix after transformation
* @param {Array} vector - Vector to transform [x, y, z]
* @returns {Array} Transformed vector [x, y, z]
*/
function transformVectorByTransformation(
tempObjects,
beforeMatrix,
afterMatrix,
vector
) {
const { matrixA, matrixB, newCenter } = tempObjects;

// The view matrix from vtk.js is row-major, but gl-matrix expects column-major.
// We need to transpose them before use.
mat4.transpose(matrixA, beforeMatrix);

mat4.transpose(matrixB, afterMatrix);
mat4.invert(matrixB, matrixB);

// Compute delta transformation matrix
mat4.multiply(matrixA, matrixB, matrixA);

vec3.transformMat4(newCenter, vector, matrixA);
return newCenter;
}

/**
* Computes the new center of rotation based on camera movement.
* When the camera moves (pan), the center of rotation should move
* by the same transformation to maintain consistent rotation behavior.
*
* @param {Object} tempObjects - Temporary matrices/vectors for computation
* @param {Object} renderer - VTK renderer
* @param {mat4} beforeCameraMatrix - Camera view matrix before movement
* @param {Array} oldCenterOfRotation - Previous center of rotation [x, y, z]
* @returns {Array} New center of rotation [x, y, z]
*/
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
);
}

// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------
Expand All @@ -224,14 +152,6 @@ function vtkInteractorStyleManipulator(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkInteractorStyleManipulator');

// Initialize temporary objects to reduce garbage collection
const tempCameraMatrix = mat4.create();
const tempComputeObjects = {
matrixA: mat4.create(),
matrixB: mat4.create(),
newCenter: vec3.create(),
};

model.currentVRManipulators = new Map();
model.mouseManipulators = [];
model.keyboardManipulators = [];
Expand Down Expand Up @@ -584,23 +504,11 @@ function vtkInteractorStyleManipulator(publicAPI, model) {
publicAPI.handleMouseMove = (callData) => {
model.cachedMousePosition = callData.position;
if (model.currentManipulator && model.currentManipulator.onMouseMove) {
const renderer = model.getRenderer(callData);
const beforeCameraMatrix = getCameraMatrix(renderer, tempCameraMatrix);

model.currentManipulator.onMouseMove(
model._interactor,
renderer,
model.getRenderer(callData),
callData.position
);

const newCenter = computeNewCenterOfRotation(
tempComputeObjects,
renderer,
beforeCameraMatrix,
model.centerOfRotation
);
publicAPI.setCenterOfRotation(newCenter);

publicAPI.invokeInteractionEvent(INTERACTION_EVENT);
}
};
Expand Down Expand Up @@ -765,7 +673,6 @@ function vtkInteractorStyleManipulator(publicAPI, model) {
//----------------------------------------------------------------------------
publicAPI.handlePan = (callData) => {
const renderer = model.getRenderer(callData);
const beforeCameraMatrix = getCameraMatrix(renderer, tempCameraMatrix);

let count = model.gestureManipulators.length;
let actionCount = 0;
Expand All @@ -776,15 +683,8 @@ function vtkInteractorStyleManipulator(publicAPI, model) {
actionCount++;
}
}
if (actionCount) {
const newCenter = computeNewCenterOfRotation(
tempComputeObjects,
renderer,
beforeCameraMatrix,
model.centerOfRotation
);
publicAPI.setCenterOfRotation(newCenter);

if (actionCount) {
publicAPI.invokeInteractionEvent(INTERACTION_EVENT);
}
};
Expand Down
Loading