|
| 1 | +import macro from 'vtk.js/Sources/macro'; |
| 2 | +import vtk from 'vtk.js/Sources/vtk'; |
| 3 | +import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor'; |
| 4 | +import vtkProperty from 'vtk.js/Sources/Rendering/Core/Property'; |
| 5 | +import vtkGlyph3DMapper from 'vtk.js/Sources/Rendering/Core/Glyph3DMapper'; |
| 6 | +import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray'; |
| 7 | +import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData'; |
| 8 | +import vtkColorTransferFunction from 'vtk.js/Sources/Rendering/Core/ColorTransferFunction'; |
| 9 | + |
| 10 | +import vtkAbstractRepresentationProxy from 'vtk.js/Sources/Proxy/Core/AbstractRepresentationProxy'; |
| 11 | + |
| 12 | +// For Glyph creation using the vtk factory |
| 13 | +import 'vtk.js/Sources/Filters/Sources'; |
| 14 | + |
| 15 | +// ---------------------------------------------------------------------------- |
| 16 | +// vtkGlyphRepresentationProxy methods |
| 17 | +// ---------------------------------------------------------------------------- |
| 18 | + |
| 19 | +function vtkGlyphRepresentationProxy(publicAPI, model) { |
| 20 | + // Set our className |
| 21 | + model.classHierarchy.push('vtkGlyphRepresentationProxy'); |
| 22 | + |
| 23 | + model.property = vtkProperty.newInstance(); |
| 24 | + |
| 25 | + function processJSON(description) { |
| 26 | + model.actors.length = 0; |
| 27 | + |
| 28 | + // Handle colors |
| 29 | + const lookupTable = vtkColorTransferFunction.newInstance(); |
| 30 | + lookupTable.applyColorMap({ RGBPoints: description.rgbPoints }); |
| 31 | + |
| 32 | + // Handle glyph |
| 33 | + model.glyph = {}; |
| 34 | + let count = description.glyph.length; |
| 35 | + while (count--) { |
| 36 | + const glyph = description.glyph[count]; |
| 37 | + model.glyph[glyph.id] = vtk(glyph); |
| 38 | + } |
| 39 | + |
| 40 | + // Handle mapping |
| 41 | + count = description.mapping.length; |
| 42 | + while (count--) { |
| 43 | + const sourceDesc = description.mapping[count]; |
| 44 | + const glyph = model.glyph[sourceDesc.glyphId]; |
| 45 | + const source = vtkPolyData.newInstance(); |
| 46 | + source.getPoints().setData(Float32Array.from(sourceDesc.coordinates), 3); |
| 47 | + if (sourceDesc.scale) { |
| 48 | + source.getPointData().addArray( |
| 49 | + vtkDataArray.newInstance({ |
| 50 | + name: 'scaling', |
| 51 | + values: Float32Array.from(sourceDesc.scale), |
| 52 | + numberOfComponents: 3, |
| 53 | + }) |
| 54 | + ); |
| 55 | + } |
| 56 | + const mapper = vtkGlyph3DMapper.newInstance({ |
| 57 | + useLookupTableScalarRange: true, |
| 58 | + lookupTable, |
| 59 | + orient: false, |
| 60 | + scaling: !!sourceDesc.scale, |
| 61 | + scaleArray: 'scaling', |
| 62 | + scaleMode: vtkGlyph3DMapper.ScaleModes.SCALE_BY_COMPONENTS, |
| 63 | + }); |
| 64 | + const actor = vtkActor.newInstance(); |
| 65 | + if (model.property) { |
| 66 | + actor.setProperty(model.property); |
| 67 | + } |
| 68 | + |
| 69 | + actor.setMapper(mapper); |
| 70 | + mapper.setInputData(source, 0); |
| 71 | + mapper.setInputConnection(glyph.getOutputPort(), 1); |
| 72 | + |
| 73 | + model.actors.push(actor); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + model.sourceDependencies.push({ setInputData: processJSON }); |
| 78 | + |
| 79 | + // Add actors |
| 80 | + // model.actors.push(model.sphereActor); |
| 81 | + // model.actors.push(model.stickActor); |
| 82 | + |
| 83 | + // API ---------------------------------------------------------------------- |
| 84 | + |
| 85 | + publicAPI.setColorBy = () => {}; |
| 86 | + publicAPI.getColorBy = () => []; |
| 87 | + publicAPI.listDataArrays = () => []; |
| 88 | +} |
| 89 | + |
| 90 | +// ---------------------------------------------------------------------------- |
| 91 | +// Object factory |
| 92 | +// ---------------------------------------------------------------------------- |
| 93 | + |
| 94 | +const DEFAULT_VALUES = {}; |
| 95 | + |
| 96 | +// ---------------------------------------------------------------------------- |
| 97 | + |
| 98 | +export function extend(publicAPI, model, initialValues = {}) { |
| 99 | + Object.assign(model, DEFAULT_VALUES, initialValues); |
| 100 | + |
| 101 | + // Object methods |
| 102 | + vtkAbstractRepresentationProxy.extend(publicAPI, model); |
| 103 | + |
| 104 | + // Object specific methods |
| 105 | + vtkGlyphRepresentationProxy(publicAPI, model); |
| 106 | + macro.proxyPropertyMapping(publicAPI, model, { |
| 107 | + edgeVisibility: { modelKey: 'property', property: 'edgeVisibility' }, |
| 108 | + }); |
| 109 | +} |
| 110 | + |
| 111 | +// ---------------------------------------------------------------------------- |
| 112 | + |
| 113 | +export const newInstance = macro.newInstance( |
| 114 | + extend, |
| 115 | + 'vtkGlyphRepresentationProxy' |
| 116 | +); |
| 117 | + |
| 118 | +// ---------------------------------------------------------------------------- |
| 119 | + |
| 120 | +export default { newInstance, extend }; |
0 commit comments