Skip to content

Commit

Permalink
fix(cropper): refactor handle normal calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigobasilio2022 authored and finetjul committed May 23, 2024
1 parent bfd1a63 commit e4d0d60
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
27 changes: 18 additions & 9 deletions Sources/Widgets/Widgets3D/ImageCroppingWidget/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import macro from 'vtk.js/Sources/macros';
import {
AXES,
transformVec3,
rotateVec3,
handleTypeFromName,
calculateDirection,
} from 'vtk.js/Sources/Widgets/Widgets3D/ImageCroppingWidget/helpers';

export default function widgetBehavior(publicAPI, model) {
Expand Down Expand Up @@ -75,11 +75,6 @@ export default function widgetBehavior(publicAPI, model) {
}

if (type === 'faces') {
// constraint axis is line defined by the index and center point.
// Since our index point is defined inside a box [0, 2, 0, 2, 0, 2],
// center point is [1, 1, 1].
const constraintAxis = [1 - index[0], 1 - index[1], 1 - index[2]];

// get center of current crop box
const center = [
(planes[0] + planes[1]) / 2,
Expand All @@ -88,9 +83,10 @@ export default function widgetBehavior(publicAPI, model) {
];

// manipulator should be a line manipulator
manipulator.setHandleOrigin(transformVec3(center, indexToWorldT));
const worldCenter = transformVec3(center, indexToWorldT);
manipulator.setHandleOrigin(worldCenter);
manipulator.setHandleNormal(
rotateVec3(constraintAxis, indexToWorldT)
calculateDirection(model.activeState.getOrigin(), worldCenter)
);
worldCoords = manipulator.handleEvent(
callData,
Expand All @@ -101,8 +97,21 @@ export default function widgetBehavior(publicAPI, model) {
if (type === 'edges') {
// constrain to a plane with a normal parallel to the edge
const edgeAxis = index.map((a) => (a === 1 ? a : 0));
const faceName = edgeAxis.map((i) => AXES[i + 1]).join('');
const handle = model.widgetState.getStatesWithLabel(faceName)[0];
// get center of current crop box
const center = [
(planes[0] + planes[1]) / 2,
(planes[2] + planes[3]) / 2,
(planes[4] + planes[5]) / 2,
];

// manipulator should be a line manipulator
const worldCenter = transformVec3(center, indexToWorldT);

manipulator.setHandleNormal(rotateVec3(edgeAxis, indexToWorldT));
manipulator.setHandleNormal(
calculateDirection(handle.getOrigin(), worldCenter)
);
worldCoords = manipulator.handleEvent(
callData,
model._apiSpecificRenderWindow
Expand Down
7 changes: 7 additions & 0 deletions Sources/Widgets/Widgets3D/ImageCroppingWidget/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ export function handleTypeFromName(name) {
}
return 'faces';
}

export function calculateDirection(v1, v2) {
const direction = vec3.create();
vec3.subtract(direction, v1, v2);
vec3.normalize(direction, direction);
return direction;
}

0 comments on commit e4d0d60

Please sign in to comment.