AngleWidget

Source

behavior.js
import macro from 'vtk.js/Sources/macros';
import { add } from 'vtk.js/Sources/Common/Core/Math';
import vtkPointPicker from 'vtk.js/Sources/Rendering/Core/PointPicker';

const MAX_POINTS = 3;

export default function widgetBehavior(publicAPI, model) {
model.classHierarchy.push('vtkAngleWidgetProp');
model._isDragging = false;

const picker = vtkPointPicker.newInstance();
picker.setPickFromList(1);

// --------------------------------------------------------------------------
// Display 2D
// --------------------------------------------------------------------------

publicAPI.setDisplayCallback = (callback) =>
model.representations[0].setDisplayCallback(callback);

// --------------------------------------------------------------------------
// Interactor events
// --------------------------------------------------------------------------

function ignoreKey(e) {
return e.altKey || e.controlKey || e.shiftKey;
}

// --------------------------------------------------------------------------
// Left press: Select handle to drag
// --------------------------------------------------------------------------

publicAPI.handleLeftButtonPress = (e) => {
if (
!model.activeState ||
!model.activeState.getActive() ||
!model.pickable ||
ignoreKey(e)
) {
return macro.VOID;
}

picker.initializePickList();
picker.setPickList(publicAPI.getNestedProps());
const manipulator =
model.activeState?.getManipulator?.() ?? model.manipulator;
const { worldCoords } = manipulator.handleEvent(
e,
model._apiSpecificRenderWindow
);

if (
model.activeState === model.widgetState.getMoveHandle() &&
model.widgetState.getHandleList().length < MAX_POINTS &&
manipulator
) {
// Commit handle to location
const moveHandle = model.widgetState.getMoveHandle();
moveHandle.setOrigin(...worldCoords);
const newHandle = model.widgetState.addHandle();
newHandle.setOrigin(...moveHandle.getOrigin());
newHandle.setColor(moveHandle.getColor());
newHandle.setScale1(moveHandle.getScale1());
newHandle.setManipulator(manipulator);
} else if (model.dragable) {
model._isDragging = true;
model._apiSpecificRenderWindow.setCursor('grabbing');
model._interactor.requestAnimation(publicAPI);
}

publicAPI.invokeStartInteractionEvent();
return macro.EVENT_ABORT;
};

// --------------------------------------------------------------------------
// Mouse move: Drag selected handle / Handle follow the mouse
// --------------------------------------------------------------------------

publicAPI.handleMouseMove = (callData) => {
const manipulator =
model.activeState?.getManipulator?.() ?? model.manipulator;
if (
manipulator &&
model.pickable &&
model.dragable &&
model.activeState &&
model.activeState.getActive() &&
!ignoreKey(callData)
) {
const { worldCoords, worldDelta } = manipulator.handleEvent(
callData,
model._apiSpecificRenderWindow
);

const isHandleMoving =
model.activeState === model.widgetState.getMoveHandle() ||
model._isDragging;

if (isHandleMoving && worldCoords.length && model.activeState.setOrigin) {
const curOrigin = model.activeState.getOrigin();
if (curOrigin) {
model.activeState.setOrigin(add(curOrigin, worldDelta, []));
} else {
model.activeState.setOrigin(worldCoords);
}
publicAPI.invokeInteractionEvent();
return macro.EVENT_ABORT;
}
}
if (model.hasFocus) {
model._widgetManager.disablePicking();
}
return macro.VOID;
};

// --------------------------------------------------------------------------
// Left release: Finish drag / Create new handle
// --------------------------------------------------------------------------

publicAPI.handleLeftButtonRelease = () => {
if (
!model.activeState ||
!model.activeState.getActive() ||
!model.pickable
) {
return macro.VOID;
}
if (
model.hasFocus &&
model.widgetState.getHandleList().length === MAX_POINTS
) {
publicAPI.loseFocus();
return macro.VOID;
}

if (model._isDragging) {
model._apiSpecificRenderWindow.setCursor('pointer');
model.widgetState.deactivate();
model._interactor.cancelAnimation(publicAPI);
model._isDragging = false;
} else if (model.activeState !== model.widgetState.getMoveHandle()) {
model.widgetState.deactivate();
}

if (
(model.hasFocus && !model.activeState) ||
(model.activeState && !model.activeState.getActive())
) {
model._widgetManager.enablePicking();
model._interactor.render();
}

publicAPI.invokeEndInteractionEvent();
return macro.EVENT_ABORT;
};

// --------------------------------------------------------------------------
// Focus API - modeHandle follow mouse when widget has focus
// --------------------------------------------------------------------------

publicAPI.grabFocus = () => {
if (
!model.hasFocus &&
model.widgetState.getHandleList().length < MAX_POINTS
) {
model.activeState = model.widgetState.getMoveHandle();
model.activeState.activate();
model.activeState.setVisible(true);
model._interactor.requestAnimation(publicAPI);
publicAPI.invokeStartInteractionEvent();
}
model.hasFocus = true;
};

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

publicAPI.loseFocus = () => {
if (model.hasFocus) {
model._interactor.cancelAnimation(publicAPI);
publicAPI.invokeEndInteractionEvent();
}
model.widgetState.deactivate();
model.widgetState.getMoveHandle().deactivate();
model.widgetState.getMoveHandle().setVisible(false);
model.widgetState.getMoveHandle().setOrigin(null);
model.activeState = null;
model.hasFocus = false;
model._widgetManager.enablePicking();
model._interactor.render();
};
}
index.js
import macro from 'vtk.js/Sources/macros';
import vtkAbstractWidgetFactory from 'vtk.js/Sources/Widgets/Core/AbstractWidgetFactory';
import vtkPlanePointManipulator from 'vtk.js/Sources/Widgets/Manipulators/PlaneManipulator';
import vtkPolyLineRepresentation from 'vtk.js/Sources/Widgets/Representations/PolyLineRepresentation';
import vtkSphereHandleRepresentation from 'vtk.js/Sources/Widgets/Representations/SphereHandleRepresentation';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';

import widgetBehavior from 'vtk.js/Sources/Widgets/Widgets3D/AngleWidget/behavior';
import stateGenerator from 'vtk.js/Sources/Widgets/Widgets3D/AngleWidget/state';

import { ViewTypes } from 'vtk.js/Sources/Widgets/Core/WidgetManager/Constants';

// ----------------------------------------------------------------------------
// Factory
// ----------------------------------------------------------------------------

function vtkAngleWidget(publicAPI, model) {
model.classHierarchy.push('vtkAngleWidget');

// --- Widget Requirement ---------------------------------------------------

model.methodsToLink = [
'activeScaleFactor',
'activeColor',
'useActiveColor',
'glyphResolution',
'defaultScale',
'scaleInPixels',
];

model._onManipulatorChanged = () => {
model.widgetState.getMoveHandle().setManipulator(model.manipulator);
model.widgetState.getHandleList().forEach((handle) => {
handle.setManipulator(model.manipulator);
});
};

publicAPI.getRepresentationsForViewType = (viewType) => {
switch (viewType) {
case ViewTypes.DEFAULT:
case ViewTypes.GEOMETRY:
case ViewTypes.SLICE:
case ViewTypes.VOLUME:
default:
return [
{
builder: vtkSphereHandleRepresentation,
labels: ['handles', 'moveHandle'],
},
{
builder: vtkPolyLineRepresentation,
labels: ['handles', 'moveHandle'],
},
];
}
};

// --- Public methods -------------------------------------------------------

// Returns angle in radians
publicAPI.getAngle = () => {
const handles = model.widgetState.getHandleList();
if (handles.length !== 3) {
return 0;
}
if (
!handles[0].getOrigin() ||
!handles[1].getOrigin() ||
!handles[2].getOrigin()
) {
return 0;
}
const vec1 = [0, 0, 0];
const vec2 = [0, 0, 0];
vtkMath.subtract(handles[0].getOrigin(), handles[1].getOrigin(), vec1);
vtkMath.subtract(handles[2].getOrigin(), handles[1].getOrigin(), vec2);
return vtkMath.angleBetweenVectors(vec1, vec2);
};

// --------------------------------------------------------------------------
// initialization
// --------------------------------------------------------------------------

model.widgetState.onBoundsChange((bounds) => {
const center = [
(bounds[0] + bounds[1]) * 0.5,
(bounds[2] + bounds[3]) * 0.5,
(bounds[4] + bounds[5]) * 0.5,
];
model.widgetState.getMoveHandle().setOrigin(center);
});

// Default manipulator
publicAPI.setManipulator(
model.manipulator ||
vtkPlanePointManipulator.newInstance({ useCameraNormal: true })
);
}

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

const defaultValues = (initialValues) => ({
// manipulator: null,
behavior: widgetBehavior,
widgetState: stateGenerator(),
...initialValues,
});

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

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

vtkAbstractWidgetFactory.extend(publicAPI, model, initialValues);
macro.setGet(publicAPI, model, ['manipulator']);

vtkAngleWidget(publicAPI, model);
}

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

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

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

export default { newInstance, extend };
state.js
import vtkStateBuilder from 'vtk.js/Sources/Widgets/Core/StateBuilder';

export default function generateState() {
return vtkStateBuilder
.createBuilder()
.addStateFromMixin({
labels: ['moveHandle'],
mixins: ['origin', 'color', 'scale1', 'visible', 'manipulator'],
name: 'moveHandle',
initialValues: {
scale1: 30,
visible: false,
},
})
.addDynamicMixinState({
labels: ['handles'],
mixins: ['origin', 'color', 'scale1', 'visible', 'manipulator'],
name: 'handle',
initialValues: {
scale1: 30,
},
})
.build();
}