RectangleWidget

Source

behavior.js
import shapeBehavior from 'vtk.js/Sources/Widgets/Widgets3D/ShapeWidget/behavior';

export default function widgetBehavior(publicAPI, model) {
// We inherit shapeBehavior
shapeBehavior(publicAPI, model);
const superClass = Object.assign({}, publicAPI);

model.classHierarchy.push('vtkRectangleWidgetProp');

publicAPI.setBounds = (bounds) => {
if (superClass.setBounds) {
superClass.setBounds(bounds);
}

model.shapeHandle.setBounds(bounds);
};
}
index.js
import macro from 'vtk.js/Sources/macro';
import vtkAbstractWidgetFactory from 'vtk.js/Sources/Widgets/Core/AbstractWidgetFactory';
import vtkPlanePointManipulator from 'vtk.js/Sources/Widgets/Manipulators/PlaneManipulator';
import vtkSphereHandleRepresentation from 'vtk.js/Sources/Widgets/Representations/SphereHandleRepresentation';
import vtkRectangleContextRepresentation from 'vtk.js/Sources/Widgets/Representations/RectangleContextRepresentation';
import widgetBehavior from 'vtk.js/Sources/Widgets/Widgets3D/RectangleWidget/behavior';
import stateGenerator from 'vtk.js/Sources/Widgets/Widgets3D/RectangleWidget/state';

import SHAPE_DEFAULT_VALUES from 'vtk.js/Sources/Widgets/Widgets3D/ShapeWidget';

import {
BehaviorCategory,
ShapeBehavior,
} from 'vtk.js/Sources/Widgets/Widgets3D/ShapeWidget/Constants';

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

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

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

model.methodsToLink = [
'activeScaleFactor',
'activeColor',
'useActiveColor',
'drawBorder',
'drawFace',
'opacity',
];

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

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

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

// Default manipulator
model.manipulator = vtkPlanePointManipulator.newInstance();
model.widgetState = stateGenerator();
model.shapeHandle = model.widgetState.getRectangleHandle();
model.point1Handle = model.widgetState.getPoint1Handle();
model.point2Handle = model.widgetState.getPoint2Handle();
model.point1Handle.setManipulator(model.manipulator);
model.point2Handle.setManipulator(model.manipulator);
}

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

const DEFAULT_VALUES = {
modifierBehavior: {
None: {
[BehaviorCategory.PLACEMENT]:
ShapeBehavior[BehaviorCategory.PLACEMENT].CLICK_AND_DRAG,
[BehaviorCategory.POINTS]:
ShapeBehavior[BehaviorCategory.POINTS].CORNER_TO_CORNER,
[BehaviorCategory.RATIO]: ShapeBehavior[BehaviorCategory.RATIO].FREE,
},
Shift: {
[BehaviorCategory.RATIO]: ShapeBehavior[BehaviorCategory.RATIO].FIXED,
},
Control: {
[BehaviorCategory.POINTS]:
ShapeBehavior[BehaviorCategory.POINTS].CENTER_TO_CORNER,
},
},
};

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

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

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

vtkRectangleWidget(publicAPI, model);
}

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

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

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

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: 'point1Handle',
initialValues: {
scale1: 10,
origin: [1, 0, 0],
visible: false,
},
})
.addStateFromMixin({
labels: ['moveHandle'],
mixins: ['origin', 'color', 'scale1', 'visible', 'manipulator'],
name: 'point2Handle',
initialValues: {
scale1: 10,
origin: [1, 0, 0],
visible: false,
},
})
.addStateFromMixin({
labels: ['rectangleHandle'],
mixins: ['bounds', 'color', 'visible', 'direction'],
name: 'rectangleHandle',
initialValues: {
bounds: [0, 0, 0, 0, 0, 0],
visible: false,
direction: [0, 0, 1],
},
})
.build();
}