123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- import macro from '@kitware/vtk.js/macros';
- import vtkAbstractWidgetFactory from '@kitware/vtk.js/Widgets/Core/AbstractWidgetFactory';
- import vtkConvexFaceContextRepresentation from '@kitware/vtk.js/Widgets/Representations/ConvexFaceContextRepresentation';
- import vtkPlaneManipulator from '@kitware/vtk.js/Widgets/Manipulators/PlaneManipulator';
- import vtkSphereHandleRepresentation from '@kitware/vtk.js/Widgets/Representations/SphereHandleRepresentation';
- import vtkStateBuilder from '@kitware/vtk.js/Widgets/Core/StateBuilder';
- import { ViewTypes } from '@kitware/vtk.js/Widgets/Core/WidgetManager/Constants';
- // ----------------------------------------------------------------------------
- // Widget linked to a view
- // ----------------------------------------------------------------------------
- function widgetBehavior(publicAPI, model) {
- let isDragging = null;
- publicAPI.setDisplayCallback = (callback) =>
- model.representations[0].setDisplayCallback(callback);
- publicAPI.handleLeftButtonPress = () => {
- if (
- !model.activeState ||
- !model.activeState.getActive() ||
- !model.pickable
- ) {
- return macro.VOID;
- }
- isDragging = true;
- model._interactor.requestAnimation(publicAPI);
- return macro.EVENT_ABORT;
- };
- publicAPI.handleMouseMove = (callData) => {
- if (isDragging && model.pickable) {
- return publicAPI.handleEvent(callData);
- }
- return macro.VOID;
- };
- publicAPI.handleLeftButtonRelease = () => {
- if (isDragging && model.pickable) {
- model._interactor.cancelAnimation(publicAPI);
- }
- isDragging = false;
- model.widgetState.deactivate();
- };
- publicAPI.handleEvent = (callData) => {
- if (
- model.pickable &&
- model.manipulator &&
- model.activeState &&
- model.activeState.getActive()
- ) {
- // model.manipulator.setNormal(model.camera.getDirectionOfProjection());
- const { worldCoords } = model.manipulator.handleEvent(
- callData,
- model._apiSpecificRenderWindow
- );
- if (worldCoords.length) {
- model.activeState.setOrigin(...worldCoords);
- }
- return macro.EVENT_ABORT;
- }
- return macro.VOID;
- };
- // --------------------------------------------------------------------------
- // initialization
- // --------------------------------------------------------------------------
- model.camera = model._renderer.getActiveCamera();
- model.classHierarchy.push('vtkBoxWidgetProp');
- }
- // ----------------------------------------------------------------------------
- // Factory
- // ----------------------------------------------------------------------------
- function vtkBoxWidget(publicAPI, model) {
- model.classHierarchy.push('vtkBoxWidget');
- // --- Widget Requirement ---------------------------------------------------
- model.behavior = widgetBehavior;
- model.methodsToLink = ['scaleInPixels'];
- publicAPI.getRepresentationsForViewType = (viewType) => {
- switch (viewType) {
- case ViewTypes.DEFAULT:
- case ViewTypes.GEOMETRY:
- case ViewTypes.SLICE:
- case ViewTypes.VOLUME:
- default:
- return [
- { builder: vtkSphereHandleRepresentation, labels: ['handles'] },
- {
- builder: vtkConvexFaceContextRepresentation,
- labels: ['---', '--+', '-++', '-+-'],
- },
- {
- builder: vtkConvexFaceContextRepresentation,
- labels: ['---', '+--', '+-+', '--+'],
- },
- {
- builder: vtkConvexFaceContextRepresentation,
- labels: ['+--', '++-', '+++', '+-+'],
- },
- {
- builder: vtkConvexFaceContextRepresentation,
- labels: ['++-', '-+-', '-++', '+++'],
- },
- {
- builder: vtkConvexFaceContextRepresentation,
- labels: ['--+', '+-+', '+++', '-++'],
- },
- {
- builder: vtkConvexFaceContextRepresentation,
- labels: ['---', '+--', '++-', '-+-'],
- },
- ];
- }
- };
- // --- Widget Requirement ---------------------------------------------------
- // Default state
- model.widgetState = vtkStateBuilder
- .createBuilder()
- .addStateFromMixin({
- labels: ['handles', '---'],
- mixins: ['origin', 'color', 'scale1', 'manipulator'],
- name: 'handle',
- initialValues: {
- scale1: 0.1,
- origin: [-1, -1, -1],
- },
- })
- .addStateFromMixin({
- labels: ['handles', '-+-'],
- mixins: ['origin', 'color', 'scale1', 'manipulator'],
- name: 'handle',
- initialValues: {
- scale1: 0.1,
- origin: [-1, 1, -1],
- },
- })
- .addStateFromMixin({
- labels: ['handles', '+--'],
- mixins: ['origin', 'color', 'scale1', 'manipulator'],
- name: 'handle',
- initialValues: {
- scale1: 0.1,
- origin: [1, -1, -1],
- },
- })
- .addStateFromMixin({
- labels: ['handles', '++-'],
- mixins: ['origin', 'color', 'scale1', 'manipulator'],
- name: 'handle',
- initialValues: {
- scale1: 0.1,
- origin: [1, 1, -1],
- },
- })
- .addStateFromMixin({
- labels: ['handles', '--+'],
- mixins: ['origin', 'color', 'scale1', 'manipulator'],
- name: 'handle',
- initialValues: {
- scale1: 0.1,
- origin: [-1, -1, 1],
- },
- })
- .addStateFromMixin({
- labels: ['handles', '-++'],
- mixins: ['origin', 'color', 'scale1', 'manipulator'],
- name: 'handle',
- initialValues: {
- scale1: 0.1,
- origin: [-1, 1, 1],
- },
- })
- .addStateFromMixin({
- labels: ['handles', '+-+'],
- mixins: ['origin', 'color', 'scale1', 'manipulator'],
- name: 'handle',
- initialValues: {
- scale1: 0.1,
- origin: [1, -1, 1],
- },
- })
- .addStateFromMixin({
- labels: ['handles', '+++'],
- mixins: ['origin', 'color', 'scale1', 'manipulator'],
- name: 'handle',
- initialValues: {
- scale1: 0.1,
- origin: [1, 1, 1],
- },
- })
- .build();
- const handles = model.widgetState.getStatesWithLabel('handles');
- // Default manipulator
- model.manipulator = vtkPlaneManipulator.newInstance({
- useCameraNormal: true,
- });
- handles.forEach((handle) => handle.setManipulator(model.manipulator));
- }
- // ----------------------------------------------------------------------------
- const DEFAULT_VALUES = {
- manipulator: null,
- };
- // ----------------------------------------------------------------------------
- export function extend(publicAPI, model, initialValues = {}) {
- Object.assign(model, DEFAULT_VALUES, initialValues);
- vtkAbstractWidgetFactory.extend(publicAPI, model, initialValues);
- macro.setGet(publicAPI, model, ['manipulator']);
- vtkBoxWidget(publicAPI, model);
- }
- // ----------------------------------------------------------------------------
- export const newInstance = macro.newInstance(extend, 'vtkBoxWidget');
- // ----------------------------------------------------------------------------
- export default { newInstance, extend };
|