|
| 1 | +import macro from '@kitware/vtk.js/macros'; |
| 2 | +import vtkBoundingBox from '@kitware/vtk.js/Common/DataModel/BoundingBox'; |
| 3 | +import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor'; |
| 4 | +import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper'; |
| 5 | +import vtkPolyData from '@kitware/vtk.js/Common/DataModel/PolyData'; |
| 6 | +import vtkWidgetRepresentation from '@kitware/vtk.js/Widgets/Representations/WidgetRepresentation'; |
| 7 | +import { Behavior } from '@kitware/vtk.js/Widgets/Representations/WidgetRepresentation/Constants'; |
| 8 | +import * as vtkMath from '@kitware/vtk.js/Common/Core/Math'; |
| 9 | + |
| 10 | +function vtkRectangleFillRepresentation(publicAPI, model) { |
| 11 | + model.classHierarchy.push('vtkRectangleFillRepresentation'); |
| 12 | + |
| 13 | + model.internalPolyData = vtkPolyData.newInstance(); |
| 14 | + |
| 15 | + model._pipeline = { |
| 16 | + source: publicAPI, |
| 17 | + mapper: vtkMapper.newInstance(), |
| 18 | + actor: vtkActor.newInstance({ pickable: true }), |
| 19 | + }; |
| 20 | + |
| 21 | + model._pipeline.actor.setMapper(model._pipeline.mapper); |
| 22 | + |
| 23 | + vtkWidgetRepresentation.connectPipeline(model._pipeline); |
| 24 | + publicAPI.addActor(model._pipeline.actor); |
| 25 | + |
| 26 | + publicAPI.getSelectedState = () => model.inputData[0]; |
| 27 | + |
| 28 | + const superBehavior = model.widgetAPI.behavior; |
| 29 | + let behaviorModel; |
| 30 | + model.widgetAPI.behavior = (publicAPIy, bModel) => { |
| 31 | + behaviorModel = bModel; |
| 32 | + return superBehavior(publicAPIy, bModel); |
| 33 | + }; |
| 34 | + |
| 35 | + publicAPI.requestData = (inData, outData) => { |
| 36 | + const states = publicAPI.getRepresentationStates(inData[0]); |
| 37 | + |
| 38 | + if (states.length < 2 || !behaviorModel) { |
| 39 | + model.internalPolyData.getPoints().setData(new Float32Array(0)); |
| 40 | + model.internalPolyData.getPolys().setData(new Uint32Array(0)); |
| 41 | + model.internalPolyData.modified(); |
| 42 | + outData[0] = model.internalPolyData; |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const box = [...vtkBoundingBox.INIT_BOUNDS]; |
| 47 | + states.forEach((handle) => { |
| 48 | + const displayPos = behaviorModel._apiSpecificRenderWindow.worldToDisplay( |
| 49 | + ...handle.getOrigin(), |
| 50 | + behaviorModel._renderer |
| 51 | + ); |
| 52 | + vtkBoundingBox.addPoint(box, ...displayPos); |
| 53 | + }); |
| 54 | + const corners = vtkBoundingBox.getCorners(box, []); |
| 55 | + |
| 56 | + const corners2D = corners.reduce((outCorners, corner) => { |
| 57 | + const duplicate = outCorners.some((outCorner) => |
| 58 | + vtkMath.areEquals(outCorner, corner) |
| 59 | + ); |
| 60 | + if (!duplicate) { |
| 61 | + outCorners.push(corner); |
| 62 | + } |
| 63 | + return outCorners; |
| 64 | + }, []); |
| 65 | + |
| 66 | + if (corners2D.length < 4) { |
| 67 | + model.internalPolyData.getPoints().setData(new Float32Array(0)); |
| 68 | + model.internalPolyData.getPolys().setData(new Uint32Array(0)); |
| 69 | + model.internalPolyData.modified(); |
| 70 | + outData[0] = model.internalPolyData; |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const worldCorners = [0, 2, 3, 1].map((cornerIndex) => |
| 75 | + behaviorModel._apiSpecificRenderWindow.displayToWorld( |
| 76 | + ...corners2D[cornerIndex], |
| 77 | + behaviorModel._renderer |
| 78 | + ) |
| 79 | + ); |
| 80 | + |
| 81 | + const points = new Float32Array(12); |
| 82 | + worldCorners.forEach((corner, i) => { |
| 83 | + points[i * 3] = corner[0]; |
| 84 | + points[i * 3 + 1] = corner[1]; |
| 85 | + points[i * 3 + 2] = corner[2]; |
| 86 | + }); |
| 87 | + |
| 88 | + const polys = new Uint32Array([4, 0, 1, 2, 3]); |
| 89 | + |
| 90 | + model.internalPolyData.getPoints().setData(points); |
| 91 | + model.internalPolyData.getPolys().setData(polys); |
| 92 | + model.internalPolyData.modified(); |
| 93 | + |
| 94 | + outData[0] = model.internalPolyData; |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +const DEFAULT_VALUES = { |
| 99 | + behavior: Behavior.HANDLE, |
| 100 | + widgetAPI: null, |
| 101 | +}; |
| 102 | + |
| 103 | +export function extend(publicAPI, model, initialValues = {}) { |
| 104 | + Object.assign(model, DEFAULT_VALUES, initialValues); |
| 105 | + vtkWidgetRepresentation.extend(publicAPI, model, initialValues); |
| 106 | + macro.setGet(publicAPI, model, ['widgetAPI']); |
| 107 | + macro.get(publicAPI, model._pipeline, ['mapper', 'actor']); |
| 108 | + vtkRectangleFillRepresentation(publicAPI, model); |
| 109 | +} |
| 110 | + |
| 111 | +export const newInstance = macro.newInstance( |
| 112 | + extend, |
| 113 | + 'vtkRectangleFillRepresentation' |
| 114 | +); |
| 115 | + |
| 116 | +export default { newInstance, extend }; |
0 commit comments