Skip to content

Commit

Permalink
refactor(function): cropper center function 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 e4d0d60 commit 23f7444
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
20 changes: 4 additions & 16 deletions Sources/Widgets/Widgets3D/ImageCroppingWidget/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
transformVec3,
handleTypeFromName,
calculateDirection,
calculateCropperCenter,
} from 'vtk.js/Sources/Widgets/Widgets3D/ImageCroppingWidget/helpers';

export default function widgetBehavior(publicAPI, model) {
Expand Down Expand Up @@ -76,14 +77,8 @@ export default function widgetBehavior(publicAPI, model) {

if (type === 'faces') {
// 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);
const worldCenter = calculateCropperCenter(planes, indexToWorldT);

manipulator.setHandleOrigin(worldCenter);
manipulator.setHandleNormal(
calculateDirection(model.activeState.getOrigin(), worldCenter)
Expand All @@ -100,14 +95,7 @@ export default function widgetBehavior(publicAPI, model) {
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);
const worldCenter = calculateCropperCenter(planes, indexToWorldT);

manipulator.setHandleNormal(
calculateDirection(handle.getOrigin(), worldCenter)
Expand Down
10 changes: 10 additions & 0 deletions Sources/Widgets/Widgets3D/ImageCroppingWidget/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export function handleTypeFromName(name) {
return 'faces';
}

export function calculateCropperCenter(planes, transform) {
// get center of current crop box
const center = [
(planes[0] + planes[1]) / 2,
(planes[2] + planes[3]) / 2,
(planes[4] + planes[5]) / 2,
];
return transformVec3(center, transform);
}

export function calculateDirection(v1, v2) {
const direction = vec3.create();
vec3.subtract(direction, v1, v2);
Expand Down

0 comments on commit 23f7444

Please sign in to comment.