RectangleWidget

Source

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

export default function widgetBehavior(publicAPI, model) {
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);

// We inherit shapeBehavior
shapeBehavior(publicAPI, model);
const superClass = { ...publicAPI };

model.classHierarchy.push('vtkRectangleWidgetProp');

publicAPI.setCorners = (point1, point2) => {
if (superClass.setCorners) {
superClass.setCorners(point1, point2);
}
model.shapeHandle.setOrigin(point1);
model.shapeHandle.setCorner(point2);
};
}
index.js
import macro from 'vtk.js/Sources/macros';
import vtkPlanePointManipulator from 'vtk.js/Sources/Widgets/Manipulators/PlaneManipulator';
import vtkShapeWidget from 'vtk.js/Sources/Widgets/Widgets3D/ShapeWidget';
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 {
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 = [
...model.methodsToLink,
'activeScaleFactor',
'activeColor',
'useActiveColor',
'drawBorder',
'drawFace',
'opacity',
];

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

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
// --------------------------------------------------------------------------

model.manipulator = vtkPlanePointManipulator.newInstance({
useCameraNormal: true,
});
}

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

function defaultValues(initialValues) {
return {
behavior: widgetBehavior,
widgetState: stateGenerator(),
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,
},
},
...initialValues,
};
}

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

export function extend(publicAPI, model, initialValues = {}) {
vtkShapeWidget.extend(publicAPI, model, defaultValues(initialValues));
macro.setGet(publicAPI, model, ['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';
import { TextPosition } from 'vtk.js/Sources/Widgets/Widgets3D/ShapeWidget/Constants';

export default function generateState() {
return (
vtkStateBuilder
.createBuilder()
.addStateFromMixin({
labels: ['moveHandle'],
mixins: ['origin', 'color', 'scale1', 'visible', 'manipulator'],
name: 'point1Handle',
initialValues: {
scale1: 10,
visible: false,
},
})
.addStateFromMixin({
labels: ['moveHandle'],
mixins: ['origin', 'color', 'scale1', 'visible', 'manipulator'],
name: 'point2Handle',
initialValues: {
scale1: 10,
visible: false,
},
})
.addStateFromMixin({
labels: ['rectangleHandle'],
mixins: ['origin', 'corner', 'color', 'visible', 'orientation'],
name: 'rectangleHandle',
initialValues: {
visible: false,
},
})
// FIXME: How to not duplicate with EllipseWidget
.addStateFromMixin({
labels: ['SVGtext'],
mixins: ['origin', 'color', 'text', 'visible'],
name: 'text',
initialValues: {
/* text is empty to set a text filed in the SVGLayer and to avoid
* displaying text before positioning the handles */
text: '',
},
})
// FIXME: to move in text handle sub state
.addField({
name: 'textPosition',
initialValue: [
TextPosition.CENTER,
TextPosition.CENTER,
TextPosition.CENTER,
],
})
.addField({
name: 'textWorldMargin',
initialValue: 0,
})
.build()
);
}