From e4d0d60fcf14eef2c205367503af32223928596e Mon Sep 17 00:00:00 2001 From: rodrigobasilio2022 Date: Fri, 17 May 2024 09:34:25 -0300 Subject: [PATCH] fix(cropper): refactor handle normal calculation --- .../Widgets3D/ImageCroppingWidget/behavior.js | 27 ++++++++++++------- .../Widgets3D/ImageCroppingWidget/helpers.js | 7 +++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Sources/Widgets/Widgets3D/ImageCroppingWidget/behavior.js b/Sources/Widgets/Widgets3D/ImageCroppingWidget/behavior.js index 84e5d69c184..641e2311bd0 100644 --- a/Sources/Widgets/Widgets3D/ImageCroppingWidget/behavior.js +++ b/Sources/Widgets/Widgets3D/ImageCroppingWidget/behavior.js @@ -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) { @@ -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, @@ -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, @@ -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 diff --git a/Sources/Widgets/Widgets3D/ImageCroppingWidget/helpers.js b/Sources/Widgets/Widgets3D/ImageCroppingWidget/helpers.js index f0c1cbace97..8b3e365c3d3 100644 --- a/Sources/Widgets/Widgets3D/ImageCroppingWidget/helpers.js +++ b/Sources/Widgets/Widgets3D/ImageCroppingWidget/helpers.js @@ -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; +}