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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import macro from 'vtk.js/Sources/macros'; import * as vtkMath from 'vtk.js/Sources/Common/Core/Math'; import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder'; import vtkInteractorStyleManipulator from 'vtk.js/Sources/Interaction/Style/InteractorStyleManipulator'; import vtkMouseCameraTrackballRotateManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraTrackballRotateManipulator'; import vtkMouseCameraTrackballPanManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraTrackballPanManipulator'; import vtkMouseCameraTrackballZoomManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraTrackballZoomManipulator'; import vtkMouseRangeManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseRangeManipulator'; // ---------------------------------------------------------------------------- // Global methods // ---------------------------------------------------------------------------- function boundsToCorners(bounds) { return [ [bounds[0], bounds[2], bounds[4]], [bounds[0], bounds[2], bounds[5]], [bounds[0], bounds[3], bounds[4]], [bounds[0], bounds[3], bounds[5]], [bounds[1], bounds[2], bounds[4]], [bounds[1], bounds[2], bounds[5]], [bounds[1], bounds[3], bounds[4]], [bounds[1], bounds[3], bounds[5]], ]; } // ---------------------------------------------------------------------------- function clamp(value, min, max) { if (value < min) { return min; } if (value > max) { return max; } return value; } // ---------------------------------------------------------------------------- // vtkInteractorStyleMPRSlice methods // ---------------------------------------------------------------------------- function vtkInteractorStyleMPRSlice(publicAPI, model) { // Set our className model.classHierarchy.push('vtkInteractorStyleMPRSlice'); model.trackballManipulator = vtkMouseCameraTrackballRotateManipulator.newInstance({ button: 1, }); model.panManipulator = vtkMouseCameraTrackballPanManipulator.newInstance({ button: 1, shift: true, }); model.zoomManipulator = vtkMouseCameraTrackballZoomManipulator.newInstance({ button: 3, }); model.scrollManipulator = vtkMouseRangeManipulator.newInstance({ scrollEnabled: true, dragEnabled: false, }); // cache for sliceRange const cache = { sliceNormal: [0, 0, 0], sliceRange: [0, 0], }; let cameraSub = null; function updateScrollManipulator() { const range = publicAPI.getSliceRange(); model.scrollManipulator.removeScrollListener(); model.scrollManipulator.setScrollListener( range[0], range[1], 1, publicAPI.getSlice, publicAPI.setSlice ); } function setManipulators() { publicAPI.removeAllMouseManipulators(); publicAPI.addMouseManipulator(model.trackballManipulator); publicAPI.addMouseManipulator(model.panManipulator); publicAPI.addMouseManipulator(model.zoomManipulator); publicAPI.addMouseManipulator(model.scrollManipulator); updateScrollManipulator(); } const superSetInteractor = publicAPI.setInteractor; publicAPI.setInteractor = (interactor) => { superSetInteractor(interactor); if (cameraSub) { cameraSub.unsubscribe(); cameraSub = null; } if (interactor) { const renderer = interactor.getCurrentRenderer(); const camera = renderer.getActiveCamera(); cameraSub = camera.onModified(() => { updateScrollManipulator(); publicAPI.modified(); }); } }; publicAPI.handleMouseMove = macro.chain(publicAPI.handleMouseMove, () => { const renderer = model._interactor.getCurrentRenderer(); const camera = renderer.getActiveCamera(); const dist = camera.getDistance(); camera.setClippingRange(dist, dist + 0.1); }); const superSetVolumeMapper = publicAPI.setVolumeMapper; publicAPI.setVolumeMapper = (mapper) => { if (superSetVolumeMapper(mapper)) { const renderer = model._interactor.getCurrentRenderer(); const camera = renderer.getActiveCamera(); if (mapper) { // prevent zoom manipulator from messing with our focal point camera.setFreezeFocalPoint(true); publicAPI.setSliceNormal(...publicAPI.getSliceNormal()); } else { camera.setFreezeFocalPoint(false); } } }; publicAPI.getSlice = () => { const renderer = model._interactor.getCurrentRenderer(); const camera = renderer.getActiveCamera(); const sliceNormal = publicAPI.getSliceNormal(); // Get rotation matrix from normal to +X (since bounds is aligned to XYZ) const transform = vtkMatrixBuilder .buildFromDegree() .identity() .rotateFromDirections(sliceNormal, [1, 0, 0]); const fp = camera.getFocalPoint(); transform.apply(fp); return fp[0]; }; publicAPI.setSlice = (slice) => { const renderer = model._interactor.getCurrentRenderer(); const camera = renderer.getActiveCamera(); if (model.volumeMapper) { const range = publicAPI.getSliceRange(); const bounds = model.volumeMapper.getBounds(); const clampedSlice = clamp(slice, ...range); const center = [ (bounds[0] + bounds[1]) / 2.0, (bounds[2] + bounds[3]) / 2.0, (bounds[4] + bounds[5]) / 2.0, ]; const distance = camera.getDistance(); const dop = camera.getDirectionOfProjection(); vtkMath.normalize(dop); const midPoint = (range[1] + range[0]) / 2.0; const zeroPoint = [ center[0] - dop[0] * midPoint, center[1] - dop[1] * midPoint, center[2] - dop[2] * midPoint, ]; const slicePoint = [ zeroPoint[0] + dop[0] * clampedSlice, zeroPoint[1] + dop[1] * clampedSlice, zeroPoint[2] + dop[2] * clampedSlice, ]; const newPos = [ slicePoint[0] - dop[0] * distance, slicePoint[1] - dop[1] * distance, slicePoint[2] - dop[2] * distance, ]; camera.setPosition(...newPos); camera.setFocalPoint(...slicePoint); } }; publicAPI.getSliceRange = () => { Iif (model.volumeMapper) { const sliceNormal = publicAPI.getSliceNormal(); if ( sliceNormal[0] === cache.sliceNormal[0] && sliceNormal[1] === cache.sliceNormal[1] && sliceNormal[2] === cache.sliceNormal[2] ) { return cache.sliceRange; } const bounds = model.volumeMapper.getBounds(); const points = boundsToCorners(bounds); // Get rotation matrix from normal to +X (since bounds is aligned to XYZ) const transform = vtkMatrixBuilder .buildFromDegree() .identity() .rotateFromDirections(sliceNormal, [1, 0, 0]); points.forEach((pt) => transform.apply(pt)); // range is now maximum X distance let minX = Infinity; let maxX = -Infinity; for (let i = 0; i < 8; i++) { const x = points[i][0]; if (x > maxX) { maxX = x; } if (x < minX) { minX = x; } } cache.sliceNormal = sliceNormal; cache.sliceRange = [minX, maxX]; return cache.sliceRange; } return [0, 0]; }; // Slice normal is just camera DOP publicAPI.getSliceNormal = () => { if (model.volumeMapper) { const renderer = model._interactor.getCurrentRenderer(); const camera = renderer.getActiveCamera(); return camera.getDirectionOfProjection(); } return [0, 0, 0]; }; // in world space publicAPI.setSliceNormal = (...normal) => { const renderer = model._interactor.getCurrentRenderer(); const camera = renderer.getActiveCamera(); vtkMath.normalize(normal); if (model.volumeMapper) { const bounds = model.volumeMapper.getBounds(); // diagonal will be used as "width" of camera scene const diagonal = Math.sqrt( vtkMath.distance2BetweenPoints( [bounds[0], bounds[2], bounds[4]], [bounds[1], bounds[3], bounds[5]] ) ); // center will be used as initial focal point const center = [ (bounds[0] + bounds[1]) / 2.0, (bounds[2] + bounds[3]) / 2.0, (bounds[4] + bounds[5]) / 2.0, ]; const angle = 90; // distance from camera to focal point const dist = diagonal / (2 * Math.tan((angle / 360) * Math.PI)); const cameraPos = [ center[0] - normal[0] * dist, center[1] - normal[1] * dist, center[2] - normal[2] * dist, ]; // set viewUp based on DOP rotation const oldDop = camera.getDirectionOfProjection(); const transform = vtkMatrixBuilder .buildFromDegree() .identity() .rotateFromDirections(oldDop, normal); const viewUp = [0, 1, 0]; transform.apply(viewUp); camera.setPosition(...cameraPos); camera.setDistance(dist); // should be set after pos and distance camera.setDirectionOfProjection(...normal); camera.setViewUp(...viewUp); camera.setViewAngle(angle); camera.setClippingRange(dist, dist + 0.1); publicAPI.setCenterOfRotation(center); } }; setManipulators(); } // ---------------------------------------------------------------------------- // Object factory // ---------------------------------------------------------------------------- const DEFAULT_VALUES = {}; // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues); // Inheritance vtkInteractorStyleManipulator.extend(publicAPI, model, initialValues); macro.setGet(publicAPI, model, ['volumeMapper']); // Object specific methods vtkInteractorStyleMPRSlice(publicAPI, model); } // ---------------------------------------------------------------------------- export const newInstance = macro.newInstance( extend, 'vtkInteractorStyleMPRSlice' ); // ---------------------------------------------------------------------------- export default { newInstance, extend }; |