|
| 1 | +import macro from 'vtk.js/Sources/macros'; |
| 2 | +import vtkPlane from 'vtk.js/Sources/Common/DataModel/Plane'; |
| 3 | + |
| 4 | +function vtkSliceHelper(publicAPI, model) { |
| 5 | + model.classHierarchy.push('vtkSliceHelper'); |
| 6 | + |
| 7 | + const superClass = { ...publicAPI }; |
| 8 | + |
| 9 | + function update() { |
| 10 | + const [x, y, z] = model.normal; |
| 11 | + model.clipPlane1.setNormal([x, y, z]); |
| 12 | + model.clipPlane2.setNormal([-x, -y, -z]); |
| 13 | + |
| 14 | + const origin1 = [ |
| 15 | + model.origin[0] - (x * model.thickness) / 2, |
| 16 | + model.origin[1] - (y * model.thickness) / 2, |
| 17 | + model.origin[2] - (z * model.thickness) / 2, |
| 18 | + ]; |
| 19 | + model.clipPlane1.setOrigin(origin1); |
| 20 | + |
| 21 | + const origin2 = [ |
| 22 | + model.origin[0] + (x * model.thickness) / 2, |
| 23 | + model.origin[1] + (y * model.thickness) / 2, |
| 24 | + model.origin[2] + (z * model.thickness) / 2, |
| 25 | + ]; |
| 26 | + model.clipPlane2.setOrigin(origin2); |
| 27 | + } |
| 28 | + |
| 29 | + publicAPI.addMapper = (mapper) => { |
| 30 | + mapper.addClippingPlane(model.clipPlane1); |
| 31 | + mapper.addClippingPlane(model.clipPlane2); |
| 32 | + }; |
| 33 | + |
| 34 | + publicAPI.setThickness = (thickness) => { |
| 35 | + superClass.setThickness(thickness); |
| 36 | + update(); |
| 37 | + }; |
| 38 | + publicAPI.setOrigin = (origin) => { |
| 39 | + superClass.setOrigin(origin); |
| 40 | + update(); |
| 41 | + }; |
| 42 | + publicAPI.setNormal = (normal) => { |
| 43 | + superClass.setNormal(normal); |
| 44 | + update(); |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +// ---------------------------------------------------------------------------- |
| 49 | +// Object factory |
| 50 | +// ---------------------------------------------------------------------------- |
| 51 | + |
| 52 | +const DEFAULT_VALUES = { |
| 53 | + thickness: 3, |
| 54 | + origin: [0, 0, 0], |
| 55 | + normal: [1, 0, 0], |
| 56 | +}; |
| 57 | + |
| 58 | +export function extend(publicAPI, model, initialValues = {}) { |
| 59 | + Object.assign(model, DEFAULT_VALUES, initialValues); |
| 60 | + |
| 61 | + model.clipPlane1 = vtkPlane.newInstance(); |
| 62 | + model.clipPlane2 = vtkPlane.newInstance(); |
| 63 | + |
| 64 | + // Build VTK API |
| 65 | + macro.obj(publicAPI, model); |
| 66 | + macro.setGet(publicAPI, model, ['thickness', 'origin', 'normal']); |
| 67 | + |
| 68 | + // Object methods |
| 69 | + vtkSliceHelper(publicAPI, model); |
| 70 | + |
| 71 | + if (initialValues.mapper) { |
| 72 | + publicAPI.addMapper(initialValues.mapper); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// ---------------------------------------------------------------------------- |
| 77 | + |
| 78 | +export const newInstance = macro.newInstance(extend, 'vtkSliceHelper'); |
| 79 | + |
| 80 | +// ---------------------------------------------------------------------------- |
| 81 | + |
| 82 | +export default { newInstance, extend }; |
0 commit comments