Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | 1x 1x | import macro from 'vtk.js/Sources/macros'; import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor'; import vtkArrow2DSource from 'vtk.js/Sources/Filters/Sources/Arrow2DSource/'; import vtkGlyphRepresentation from 'vtk.js/Sources/Widgets/Representations/GlyphRepresentation'; import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder'; import vtkPixelSpaceCallbackMapper from 'vtk.js/Sources/Rendering/Core/PixelSpaceCallbackMapper'; import vtkConeSource from 'vtk.js/Sources/Filters/Sources/ConeSource'; import vtkSphereSource from 'vtk.js/Sources/Filters/Sources/SphereSource'; import vtkCircleSource from 'vtk.js/Sources/Filters/Sources/CircleSource'; import vtkCubeSource from 'vtk.js/Sources/Filters/Sources/CubeSource'; import vtkViewFinderSource from 'vtk.js/Sources/Filters/Sources/ViewFinderSource'; import Constants from 'vtk.js/Sources/Widgets/Widgets3D/LineWidget/Constants'; import { vec3, mat3, mat4 } from 'gl-matrix'; import { RenderingTypes } from 'vtk.js/Sources/Widgets/Core/WidgetManager/Constants'; import { OrientationModes } from 'vtk.js/Sources/Rendering/Core/Glyph3DMapper/Constants'; import { allocateArray } from 'vtk.js/Sources/Widgets/Representations/WidgetRepresentation'; const { ShapeType, Shapes2D, ShapesOrientable } = Constants; // ---------------------------------------------------------------------------- // vtkArrowHandleRepresentation methods // ---------------------------------------------------------------------------- function vtkArrowHandleRepresentation(publicAPI, model) { // Set our className model.classHierarchy.push('vtkArrowHandleRepresentation'); const superClass = { ...publicAPI }; // -------------------------------------------------------------------------- // Internal polydata dataset // -------------------------------------------------------------------------- /** * Set the shape for the glyph according to lineWidget state inputs */ function createGlyph(shape) { const representationToSource = { [ShapeType.STAR]: { builder: vtkArrow2DSource, initialValues: { shape: 'star', height: 0.6 }, }, [ShapeType.ARROWHEAD3]: { builder: vtkArrow2DSource, initialValues: { shape: 'triangle' }, }, [ShapeType.ARROWHEAD4]: { builder: vtkArrow2DSource, initialValues: { shape: 'arrow4points' }, }, [ShapeType.ARROWHEAD6]: { builder: vtkArrow2DSource, initialValues: { shape: 'arrow6points' }, }, [ShapeType.CONE]: { builder: vtkConeSource, initialValues: { direction: [0, 1, 0], }, }, [ShapeType.SPHERE]: { builder: vtkSphereSource, }, [ShapeType.CUBE]: { builder: vtkCubeSource, initialValues: { xLength: 0.8, yLength: 0.8, zLength: 0.8 }, }, [ShapeType.DISK]: { builder: vtkCircleSource, initialValues: { resolution: 30, radius: 0.5, direction: [0, 0, 1], lines: false, face: true, }, }, [ShapeType.CIRCLE]: { builder: vtkCircleSource, initialValues: { resolution: 30, radius: 0.5, direction: [0, 0, 1], lines: true, face: false, }, }, [ShapeType.VIEWFINDER]: { builder: vtkViewFinderSource, initialValues: { radius: 0.1, spacing: 0.3, width: 1.4 }, }, [ShapeType.NONE]: { builder: vtkSphereSource, }, }; const rep = representationToSource[shape]; return rep.builder.newInstance(rep.initialValues); } // -------------------------------------------------------------------------- // Generic rendering pipeline // -------------------------------------------------------------------------- // displayActors and displayMappers are used to render objects in HTML, // allowing objects to be 'rendered' internally in a VTK scene without // being visible on the final output. model.displayMapper = vtkPixelSpaceCallbackMapper.newInstance(); model.displayActor = vtkActor.newInstance({ parentProp: publicAPI }); // model.displayActor.getProperty().setOpacity(0); // don't show in 3D model.displayActor.setMapper(model.displayMapper); model.displayMapper.setInputConnection(publicAPI.getOutputPort()); publicAPI.addActor(model.displayActor); model.alwaysVisibleActors = [model.displayActor]; // -------------------------------------------------------------------------- publicAPI.setGlyphResolution = macro.chain( publicAPI.setGlyphResolution, (r) => model._pipeline.glyph.setPhiResolution(r), (r) => model._pipeline.glyph.setThetaResolution(r) ); // -------------------------------------------------------------------------- function callbackProxy(coords) { if (model.displayCallback) { const filteredList = []; const states = publicAPI.getRepresentationStates(); for (let i = 0; i < states.length; i++) { if (states[i].getActive()) { filteredList.push(coords[i]); } } if (filteredList.length) { model.displayCallback(filteredList); return; } } model.displayCallback(); } publicAPI.setDisplayCallback = (callback) => { model.displayCallback = callback; model.displayMapper.setCallback(callback ? callbackProxy : null); }; // -------------------------------------------------------------------------- publicAPI.is2DShape = () => Shapes2D.includes(model.shape); publicAPI.isOrientableShape = () => ShapesOrientable.includes(model.shape); /** * Returns the orientation matrix to align glyph on model.orientation. * */ function getOrientationRotation(viewMatrixInv) { const displayOrientation = new Float64Array(3); const baseDir = [0, 1, 0]; vec3.transformMat3(displayOrientation, model.orientation, viewMatrixInv); displayOrientation[2] = 0; const displayMatrix = vtkMatrixBuilder .buildFromDegree() .rotateFromDirections(baseDir, displayOrientation) .getMatrix(); const displayRotation = new Float64Array(9); mat3.fromMat4(displayRotation, displayMatrix); return displayRotation; } function getCameraFacingRotation(scale3, displayRotation, viewMatrix) { const rotation = new Float64Array(9); mat3.multiply(rotation, viewMatrix, displayRotation); vec3.transformMat3(scale3, scale3, rotation); return rotation; } /** * Computes the rotation matrix of the glyph. There are 2 rotations: * - a first rotation to be oriented along model.rotation * - an optional second rotation to face the camera * @param {vec3} scale3 Scale of the glyph, rotated when glyph is rotated. */ function getGlyphRotation(scale3) { const shouldFaceCamera = model.faceCamera === true || (model.faceCamera == null && publicAPI.is2DShape()); const viewMatrix = new Float64Array(9); mat3.fromMat4(viewMatrix, model.viewMatrix); const viewMatrixInv = mat3.identity(new Float64Array(9)); if (shouldFaceCamera) { mat3.invert(viewMatrixInv, viewMatrix); } let orientationRotation = null; if (publicAPI.isOrientableShape()) { orientationRotation = getOrientationRotation(viewMatrixInv); } else { orientationRotation = mat3.identity(new Float64Array(9)); } if (shouldFaceCamera) { orientationRotation = getCameraFacingRotation( scale3, orientationRotation, viewMatrix ); } return orientationRotation; } function applyOrientation(polyData, states) { model._pipeline.mapper.setOrientationArray('orientation'); model._pipeline.mapper.setOrientationMode(OrientationModes.MATRIX); const orientation = allocateArray( polyData, 'orientation', states.length, 'Float32Array', 9 ).getData(); const defaultScale3 = [1, 1, 1]; for (let i = 0; i < states.length; ++i) { const scale3 = states[i].getScale3?.() ?? defaultScale3; const rotation = getGlyphRotation(scale3); orientation.set(rotation, 9 * i); } } publicAPI.setDirection(applyOrientation); publicAPI.setNoOrientation(applyOrientation); publicAPI.requestData = (inData, outData) => { // FIXME: shape should NOT be mixin, but a representation property. const shape = publicAPI.getRepresentationStates(inData[0])[0]?.getShape(); let shouldCreateGlyph = model._pipeline.glyph == null; if (model.shape !== shape && Object.values(ShapeType).includes(shape)) { model.shape = shape; shouldCreateGlyph = true; } if (shouldCreateGlyph && model.shape) { model._pipeline.glyph = createGlyph(model.shape); model._pipeline.mapper.setInputConnection( model._pipeline.glyph.getOutputPort(), 1 ); } return superClass.requestData(inData, outData); }; publicAPI.updateActorVisibility = ( renderingType = RenderingTypes.FRONT_BUFFER, ctxVisible = true, handleVisible = true ) => { const hasValidState = publicAPI.getRepresentationStates().length > 0; superClass.updateActorVisibility( renderingType, ctxVisible, handleVisible && hasValidState ); }; } // ---------------------------------------------------------------------------- // Object factory // ---------------------------------------------------------------------------- /** * 'shape' default value is used first time 'shape' mixin is invalid. * 'faceCamera' controls wether the glyph should face camera or not: * - null or undefined to leave it to shape type (i.e. 2D are facing camera, * 3D are not) * - true to face camera * - false to not face camera */ function defaultValues(initialValues) { return { faceCamera: null, orientation: [1, 0, 0], shape: ShapeType.SPHERE, viewMatrix: mat4.identity(new Float64Array(16)), ...initialValues, }; } // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, defaultValues(initialValues)); vtkGlyphRepresentation.extend(publicAPI, model, initialValues); macro.setGetArray(publicAPI, model, ['visibilityFlagArray'], 2); macro.setGetArray(publicAPI, model, ['orientation'], 3); macro.setGetArray(publicAPI, model, ['viewMatrix'], 16); macro.setGet(publicAPI, model, ['faceCamera']); // Object specific methods vtkArrowHandleRepresentation(publicAPI, model); } // ---------------------------------------------------------------------------- export const newInstance = macro.newInstance( extend, 'vtkArrowHandleRepresentation' ); // ---------------------------------------------------------------------------- export default { newInstance, extend }; |