UNPKG

@kitware/vtk.js

Version:

Visualization Toolkit for the Web

128 lines (96 loc) 4.48 kB
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import macro from '../../../macros.js'; import { handleTypeFromName, AXES, transformVec3, rotateVec3 } from './helpers.js'; function widgetBehavior(publicAPI, model) { model._isDragging = false; publicAPI.setDisplayCallback = function (callback) { return model.representations[0].setDisplayCallback(callback); }; publicAPI.handleLeftButtonPress = function () { if (!model.activeState || !model.activeState.getActive() || !model.pickable) { return macro.VOID; } if (model.dragable) { model._isDragging = true; model._apiSpecificRenderWindow.setCursor('grabbing'); model._interactor.requestAnimation(publicAPI); } return macro.EVENT_ABORT; }; publicAPI.handleMouseMove = function (callData) { if (model._isDragging) { return publicAPI.handleEvent(callData); } return macro.VOID; }; publicAPI.handleLeftButtonRelease = function () { if (!model.activeState || !model.activeState.getActive() || !model.pickable) { return macro.VOID; } if (model._isDragging) { model._isDragging = false; model._interactor.cancelAnimation(publicAPI); model.widgetState.deactivate(); } return macro.EVENT_ABORT; }; publicAPI.handleEvent = function (callData) { if (model.pickable && model.activeState && model.activeState.getActive()) { var manipulator = model.activeState.getManipulator(); if (manipulator) { var name = model.activeState.getName(); var type = handleTypeFromName(name); var index = name.split('').map(function (l) { return AXES.indexOf(l); }); var planes = model.widgetState.getCroppingPlanes().getPlanes(); var indexToWorldT = model.widgetState.getIndexToWorldT(); var worldCoords = []; if (type === 'corners') { // manipulator should be a plane manipulator worldCoords = manipulator.handleEvent(callData, model._apiSpecificRenderWindow); } 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]. var constraintAxis = [1 - index[0], 1 - index[1], 1 - index[2]]; // get center of current crop box var center = [(planes[0] + planes[1]) / 2, (planes[2] + planes[3]) / 2, (planes[4] + planes[5]) / 2]; // manipulator should be a line manipulator manipulator.setHandleOrigin(transformVec3(center, indexToWorldT)); manipulator.setHandleNormal(rotateVec3(constraintAxis, indexToWorldT)); worldCoords = manipulator.handleEvent(callData, model._apiSpecificRenderWindow); } if (type === 'edges') { // constrain to a plane with a normal parallel to the edge var edgeAxis = index.map(function (a) { return a === 1 ? a : 0; }); manipulator.setHandleNormal(rotateVec3(edgeAxis, indexToWorldT)); worldCoords = manipulator.handleEvent(callData, model._apiSpecificRenderWindow); } if (worldCoords.length) { var _model$activeState, _model$widgetState$ge; // transform worldCoords to indexCoords, and then update the croppingPlanes() state with setPlanes(). var worldToIndexT = model.widgetState.getWorldToIndexT(); var indexCoords = transformVec3(worldCoords, worldToIndexT); for (var i = 0; i < 3; i++) { if (index[i] === 0) { planes[i * 2] = indexCoords[i]; } else if (index[i] === 2) { planes[i * 2 + 1] = indexCoords[i]; } } (_model$activeState = model.activeState).setOrigin.apply(_model$activeState, _toConsumableArray(worldCoords)); (_model$widgetState$ge = model.widgetState.getCroppingPlanes()).setPlanes.apply(_model$widgetState$ge, _toConsumableArray(planes)); return macro.EVENT_ABORT; } } } return macro.VOID; }; // -------------------------------------------------------------------------- // initialization // -------------------------------------------------------------------------- model._camera = model._renderer.getActiveCamera(); model.classHierarchy.push('vtkImageCroppingWidgetProp'); } export { widgetBehavior as default };