Skip to content
Merged
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
14 changes: 5 additions & 9 deletions src/vtk/PaintWidget/behavior.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import macro from '@kitware/vtk.js/macro';
import { computeWorldCoords } from '@/src/vtk/ToolWidgetUtils/utils';

export function shouldIgnoreEvent(e: any) {
return e.altKey || e.controlKey || e.shiftKey;
Expand All @@ -7,6 +8,8 @@ export function shouldIgnoreEvent(e: any) {
export default function widgetBehavior(publicAPI: any, model: any) {
model.classHierarchy.push('vtkPaintWidgetProp');

const getWorldCoords = computeWorldCoords(model);

// support setting per-view widget manipulators
macro.setGet(publicAPI, model, ['manipulator']);

Expand All @@ -20,11 +23,7 @@ export default function widgetBehavior(publicAPI: any, model: any) {
return macro.VOID;
}

const { worldCoords } = model.manipulator.handleEvent(
eventData,
model._apiSpecificRenderWindow
)?.worldCoords;

const worldCoords = getWorldCoords(eventData);
if (!worldCoords) {
return macro.VOID;
}
Expand All @@ -45,10 +44,7 @@ export default function widgetBehavior(publicAPI: any, model: any) {
return macro.VOID;
}

const { worldCoords } = model.manipulator.handleEvent(
eventData,
model._apiSpecificRenderWindow
)?.worldCoords;
const worldCoords = getWorldCoords(eventData);

if (!worldCoords) {
return macro.VOID;
Expand Down
12 changes: 2 additions & 10 deletions src/vtk/PolygonWidget/behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Vector3 } from '@kitware/vtk.js/types';
import vtkRenderer from '@kitware/vtk.js/Rendering/Core/Renderer';

import { WidgetAction } from '@/src/vtk/ToolWidgetUtils/types';
import { computeWorldCoords } from '@/src/vtk/ToolWidgetUtils/utils';

type Position3d = { x: number; y: number; z: number };
type vtkMouseEvent = {
Expand Down Expand Up @@ -110,16 +111,7 @@ export default function widgetBehavior(publicAPI: any, model: any) {
return false;
}

function getWorldCoords(callData: any) {
const manipulator =
model.activeState?.getManipulator?.() ?? model.manipulator;
if (!manipulator) {
return undefined;
}

return manipulator.handleEvent(callData, model._apiSpecificRenderWindow)
.worldCoords;
}
const getWorldCoords = computeWorldCoords(model);

function updateActiveStateHandle(callData: any) {
const worldCoords = getWorldCoords(callData);
Expand Down
24 changes: 9 additions & 15 deletions src/vtk/RulerWidget/behavior.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import macro from '@kitware/vtk.js/macro';
import type { Vector3 } from '@kitware/vtk.js/types';
import { computeWorldCoords } from '@/src/vtk/ToolWidgetUtils/utils';

export enum InteractionState {
PlacingFirst = 'PlacingFirst',
Expand Down Expand Up @@ -65,6 +66,8 @@ export default function widgetBehavior(publicAPI: any, model: any) {
return overSegment;
};

const getWorldCoords = computeWorldCoords(model);

/**
* Places or drags a point.
*/
Expand All @@ -86,11 +89,8 @@ export default function widgetBehavior(publicAPI: any, model: any) {
return macro.VOID;
}

const { worldCoords } = model.manipulator.handleEvent(
eventData,
model._apiSpecificRenderWindow
)?.worldCoords;
if (!worldCoords.length) {
const worldCoords = getWorldCoords(eventData);
if (!worldCoords?.length) {
return macro.VOID;
}

Expand Down Expand Up @@ -141,11 +141,8 @@ export default function widgetBehavior(publicAPI: any, model: any) {
* Moves a point around.
*/
publicAPI.handleMouseMove = (eventData: any) => {
const { worldCoords } = model.manipulator.handleEvent(
eventData,
model._apiSpecificRenderWindow
)?.worldCoords;
if (!worldCoords.length) {
const worldCoords = getWorldCoords(eventData);
if (!worldCoords?.length) {
return macro.VOID;
}

Expand Down Expand Up @@ -181,11 +178,8 @@ export default function widgetBehavior(publicAPI: any, model: any) {
*/
publicAPI.handleLeftButtonRelease = (eventData: any) => {
if (draggingState) {
const { worldCoords } = model.manipulator.handleEvent(
eventData,
model._apiSpecificRenderWindow
)?.worldCoords;
if (worldCoords.length) {
const worldCoords = getWorldCoords(eventData);
if (worldCoords?.length) {
draggingState.setOrigin(worldCoords);
}

Expand Down
16 changes: 16 additions & 0 deletions src/vtk/ToolWidgetUtils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,19 @@ export function watchState(
originalDelete();
};
}

export const computeWorldCoords = (model: any) => (event: any) => {
const manipulator =
model.activeState?.getManipulator?.() ?? model.manipulator;
if (!manipulator) {
console.error('No manipulator');
return undefined;
}
const { worldCoords } = manipulator.handleEvent(
event,
model._apiSpecificRenderWindow
);
if (!worldCoords)
console.warn('Event cannot be converted to world coordinates');
Comment on lines +51 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think our current manipulators shouldn't trigger this unless there's an error case, so this is a good add. If we start getting them legitimately, then we can remove it.

return worldCoords;
};