@cornerstonejs/core 0.36.2 → 0.36.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/dist/cjs/loaders/volumeLoader.d.ts +1 -0
  2. package/dist/cjs/loaders/volumeLoader.js +5 -1
  3. package/dist/cjs/loaders/volumeLoader.js.map +1 -1
  4. package/dist/cjs/utilities/getSliceRange.js +3 -1
  5. package/dist/cjs/utilities/getSliceRange.js.map +1 -1
  6. package/dist/cjs/utilities/getTargetVolumeAndSpacingInNormalDir.js +7 -0
  7. package/dist/cjs/utilities/getTargetVolumeAndSpacingInNormalDir.js.map +1 -1
  8. package/dist/esm/loaders/volumeLoader.d.ts +1 -0
  9. package/dist/esm/loaders/volumeLoader.js +3 -0
  10. package/dist/esm/loaders/volumeLoader.js.map +1 -1
  11. package/dist/esm/utilities/getSliceRange.js +3 -1
  12. package/dist/esm/utilities/getSliceRange.js.map +1 -1
  13. package/dist/esm/utilities/getTargetVolumeAndSpacingInNormalDir.js +7 -0
  14. package/dist/esm/utilities/getTargetVolumeAndSpacingInNormalDir.js.map +1 -1
  15. package/dist/umd/index.js +1 -1
  16. package/dist/umd/index.js.map +1 -1
  17. package/package.json +4 -3
  18. package/src/RenderingEngine/BaseVolumeViewport.ts +847 -0
  19. package/src/RenderingEngine/RenderingEngine.ts +1364 -0
  20. package/src/RenderingEngine/StackViewport.ts +2690 -0
  21. package/src/RenderingEngine/Viewport.ts +1244 -0
  22. package/src/RenderingEngine/VolumeViewport.ts +420 -0
  23. package/src/RenderingEngine/VolumeViewport3D.ts +42 -0
  24. package/src/RenderingEngine/getRenderingEngine.ts +34 -0
  25. package/src/RenderingEngine/helpers/addVolumesToViewports.ts +52 -0
  26. package/src/RenderingEngine/helpers/cpuFallback/colors/colormap.ts +343 -0
  27. package/src/RenderingEngine/helpers/cpuFallback/colors/index.ts +4 -0
  28. package/src/RenderingEngine/helpers/cpuFallback/colors/lookupTable.ts +469 -0
  29. package/src/RenderingEngine/helpers/cpuFallback/drawImageSync.ts +58 -0
  30. package/src/RenderingEngine/helpers/cpuFallback/rendering/calculateTransform.ts +136 -0
  31. package/src/RenderingEngine/helpers/cpuFallback/rendering/canvasToPixel.ts +25 -0
  32. package/src/RenderingEngine/helpers/cpuFallback/rendering/computeAutoVoi.ts +47 -0
  33. package/src/RenderingEngine/helpers/cpuFallback/rendering/correctShift.ts +38 -0
  34. package/src/RenderingEngine/helpers/cpuFallback/rendering/createViewport.ts +64 -0
  35. package/src/RenderingEngine/helpers/cpuFallback/rendering/doesImageNeedToBeRendered.ts +36 -0
  36. package/src/RenderingEngine/helpers/cpuFallback/rendering/fitToWindow.ts +22 -0
  37. package/src/RenderingEngine/helpers/cpuFallback/rendering/generateColorLUT.ts +60 -0
  38. package/src/RenderingEngine/helpers/cpuFallback/rendering/generateLut.ts +83 -0
  39. package/src/RenderingEngine/helpers/cpuFallback/rendering/getDefaultViewport.ts +88 -0
  40. package/src/RenderingEngine/helpers/cpuFallback/rendering/getImageFitScale.ts +52 -0
  41. package/src/RenderingEngine/helpers/cpuFallback/rendering/getImageSize.ts +55 -0
  42. package/src/RenderingEngine/helpers/cpuFallback/rendering/getLut.ts +53 -0
  43. package/src/RenderingEngine/helpers/cpuFallback/rendering/getModalityLut.ts +55 -0
  44. package/src/RenderingEngine/helpers/cpuFallback/rendering/getTransform.ts +17 -0
  45. package/src/RenderingEngine/helpers/cpuFallback/rendering/getVOILut.ts +74 -0
  46. package/src/RenderingEngine/helpers/cpuFallback/rendering/initializeRenderCanvas.ts +37 -0
  47. package/src/RenderingEngine/helpers/cpuFallback/rendering/lutMatches.ts +21 -0
  48. package/src/RenderingEngine/helpers/cpuFallback/rendering/now.ts +13 -0
  49. package/src/RenderingEngine/helpers/cpuFallback/rendering/pixelToCanvas.ts +22 -0
  50. package/src/RenderingEngine/helpers/cpuFallback/rendering/renderColorImage.ts +193 -0
  51. package/src/RenderingEngine/helpers/cpuFallback/rendering/renderGrayscaleImage.ts +166 -0
  52. package/src/RenderingEngine/helpers/cpuFallback/rendering/renderPseudoColorImage.ts +203 -0
  53. package/src/RenderingEngine/helpers/cpuFallback/rendering/resetCamera.ts +32 -0
  54. package/src/RenderingEngine/helpers/cpuFallback/rendering/resize.ts +109 -0
  55. package/src/RenderingEngine/helpers/cpuFallback/rendering/saveLastRendered.ts +36 -0
  56. package/src/RenderingEngine/helpers/cpuFallback/rendering/setDefaultViewport.ts +17 -0
  57. package/src/RenderingEngine/helpers/cpuFallback/rendering/setToPixelCoordinateSystem.ts +32 -0
  58. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedColorPixelDataToCanvasImageData.ts +58 -0
  59. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedPixelDataToCanvasImageData.ts +76 -0
  60. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedPixelDataToCanvasImageDataColorLUT.ts +60 -0
  61. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedPixelDataToCanvasImageDataPET.ts +50 -0
  62. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedPixelDataToCanvasImageDataPseudocolorLUT.ts +66 -0
  63. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedPixelDataToCanvasImageDataPseudocolorLUTPET.ts +68 -0
  64. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedPixelDataToCanvasImageDataRGBA.ts +81 -0
  65. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedRGBAPixelDataToCanvasImageData.ts +56 -0
  66. package/src/RenderingEngine/helpers/cpuFallback/rendering/transform.ts +126 -0
  67. package/src/RenderingEngine/helpers/cpuFallback/rendering/validator.ts +31 -0
  68. package/src/RenderingEngine/helpers/createVolumeActor.ts +103 -0
  69. package/src/RenderingEngine/helpers/createVolumeMapper.ts +37 -0
  70. package/src/RenderingEngine/helpers/getOrCreateCanvas.ts +58 -0
  71. package/src/RenderingEngine/helpers/index.ts +15 -0
  72. package/src/RenderingEngine/helpers/isRgbaSourceRgbDest.ts +1 -0
  73. package/src/RenderingEngine/helpers/setDefaultVolumeVOI.ts +227 -0
  74. package/src/RenderingEngine/helpers/setVolumesForViewports.ts +52 -0
  75. package/src/RenderingEngine/helpers/viewportTypeToViewportClass.ts +14 -0
  76. package/src/RenderingEngine/helpers/viewportTypeUsesCustomRenderingPipeline.ts +7 -0
  77. package/src/RenderingEngine/helpers/volumeNewImageEventDispatcher.ts +75 -0
  78. package/src/RenderingEngine/index.ts +23 -0
  79. package/src/RenderingEngine/renderingEngineCache.ts +43 -0
  80. package/src/RenderingEngine/vtkClasses/index.js +11 -0
  81. package/src/RenderingEngine/vtkClasses/vtkOffscreenMultiRenderWindow.js +149 -0
  82. package/src/RenderingEngine/vtkClasses/vtkSharedVolumeMapper.js +52 -0
  83. package/src/RenderingEngine/vtkClasses/vtkSlabCamera.d.ts +781 -0
  84. package/src/RenderingEngine/vtkClasses/vtkSlabCamera.js +155 -0
  85. package/src/RenderingEngine/vtkClasses/vtkStreamingOpenGLRenderWindow.js +47 -0
  86. package/src/RenderingEngine/vtkClasses/vtkStreamingOpenGLTexture.js +272 -0
  87. package/src/RenderingEngine/vtkClasses/vtkStreamingOpenGLViewNodeFactory.js +159 -0
  88. package/src/RenderingEngine/vtkClasses/vtkStreamingOpenGLVolumeMapper.js +319 -0
  89. package/src/Settings.ts +294 -0
  90. package/src/cache/cache.ts +854 -0
  91. package/src/cache/classes/Contour.ts +70 -0
  92. package/src/cache/classes/ContourSet.ts +151 -0
  93. package/src/cache/classes/ImageVolume.ts +155 -0
  94. package/src/cache/index.ts +5 -0
  95. package/src/constants/cpuColormaps.ts +1537 -0
  96. package/src/constants/epsilon.ts +3 -0
  97. package/src/constants/index.ts +13 -0
  98. package/src/constants/mprCameraValues.ts +20 -0
  99. package/src/constants/rendering.ts +8 -0
  100. package/src/constants/viewportPresets.ts +357 -0
  101. package/src/enums/BlendModes.ts +23 -0
  102. package/src/enums/ContourType.ts +6 -0
  103. package/src/enums/Events.ts +196 -0
  104. package/src/enums/GeometryType.ts +5 -0
  105. package/src/enums/InterpolationType.ts +13 -0
  106. package/src/enums/OrientationAxis.ts +8 -0
  107. package/src/enums/RequestType.ts +13 -0
  108. package/src/enums/SharedArrayBufferModes.ts +11 -0
  109. package/src/enums/VOILUTFunctionType.ts +10 -0
  110. package/src/enums/ViewportType.ts +21 -0
  111. package/src/enums/index.ts +23 -0
  112. package/src/eventTarget.ts +67 -0
  113. package/src/getEnabledElement.ts +105 -0
  114. package/src/global.ts +8 -0
  115. package/src/index.ts +123 -0
  116. package/src/init.ts +247 -0
  117. package/src/loaders/geometryLoader.ts +108 -0
  118. package/src/loaders/imageLoader.ts +298 -0
  119. package/src/loaders/volumeLoader.ts +477 -0
  120. package/src/metaData.ts +84 -0
  121. package/src/requestPool/imageLoadPoolManager.ts +43 -0
  122. package/src/requestPool/imageRetrievalPoolManager.ts +25 -0
  123. package/src/requestPool/requestPoolManager.ts +329 -0
  124. package/src/types/ActorSliceRange.ts +17 -0
  125. package/src/types/CPUFallbackColormap.ts +23 -0
  126. package/src/types/CPUFallbackColormapData.ts +12 -0
  127. package/src/types/CPUFallbackColormapsData.ts +7 -0
  128. package/src/types/CPUFallbackEnabledElement.ts +71 -0
  129. package/src/types/CPUFallbackLUT.ts +5 -0
  130. package/src/types/CPUFallbackLookupTable.ts +17 -0
  131. package/src/types/CPUFallbackRenderingTools.ts +25 -0
  132. package/src/types/CPUFallbackTransform.ts +16 -0
  133. package/src/types/CPUFallbackViewport.ts +29 -0
  134. package/src/types/CPUFallbackViewportDisplayedArea.ts +15 -0
  135. package/src/types/CPUIImageData.ts +47 -0
  136. package/src/types/ContourData.ts +19 -0
  137. package/src/types/Cornerstone3DConfig.ts +31 -0
  138. package/src/types/CustomEventType.ts +14 -0
  139. package/src/types/EventTypes.ts +403 -0
  140. package/src/types/FlipDirection.ts +9 -0
  141. package/src/types/IActor.ts +23 -0
  142. package/src/types/ICache.ts +28 -0
  143. package/src/types/ICachedGeometry.ts +13 -0
  144. package/src/types/ICachedImage.ts +13 -0
  145. package/src/types/ICachedVolume.ts +12 -0
  146. package/src/types/ICamera.ts +36 -0
  147. package/src/types/IContour.ts +18 -0
  148. package/src/types/IContourSet.ts +56 -0
  149. package/src/types/IDynamicImageVolume.ts +18 -0
  150. package/src/types/IEnabledElement.ts +21 -0
  151. package/src/types/IGeometry.ts +12 -0
  152. package/src/types/IImage.ts +113 -0
  153. package/src/types/IImageData.ts +45 -0
  154. package/src/types/IImageVolume.ts +78 -0
  155. package/src/types/ILoadObject.ts +36 -0
  156. package/src/types/IRegisterImageLoader.ts +10 -0
  157. package/src/types/IRenderingEngine.ts +28 -0
  158. package/src/types/IStackViewport.ts +138 -0
  159. package/src/types/IStreamingImageVolume.ts +13 -0
  160. package/src/types/IStreamingVolumeProperties.ts +14 -0
  161. package/src/types/IViewport.ts +149 -0
  162. package/src/types/IViewportId.ts +9 -0
  163. package/src/types/IVolume.ts +45 -0
  164. package/src/types/IVolumeInput.ts +36 -0
  165. package/src/types/IVolumeViewport.ts +141 -0
  166. package/src/types/ImageLoaderFn.ts +16 -0
  167. package/src/types/ImageSliceData.ts +6 -0
  168. package/src/types/Mat3.ts +16 -0
  169. package/src/types/Metadata.ts +39 -0
  170. package/src/types/OrientationVectors.ts +36 -0
  171. package/src/types/Plane.ts +6 -0
  172. package/src/types/Point2.ts +6 -0
  173. package/src/types/Point3.ts +6 -0
  174. package/src/types/Point4.ts +6 -0
  175. package/src/types/ScalingParameters.ts +27 -0
  176. package/src/types/StackViewportProperties.ts +25 -0
  177. package/src/types/TransformMatrix2D.ts +4 -0
  178. package/src/types/ViewportInputOptions.ts +21 -0
  179. package/src/types/ViewportPreset.ts +14 -0
  180. package/src/types/VolumeLoaderFn.ts +18 -0
  181. package/src/types/VolumeViewportProperties.ts +14 -0
  182. package/src/types/index.ts +157 -0
  183. package/src/types/voi.ts +15 -0
  184. package/src/utilities/actorCheck.ts +24 -0
  185. package/src/utilities/applyPreset.ts +132 -0
  186. package/src/utilities/calculateViewportsSpatialRegistration.ts +74 -0
  187. package/src/utilities/calibratedPixelSpacingMetadataProvider.ts +38 -0
  188. package/src/utilities/createFloat32SharedArray.ts +45 -0
  189. package/src/utilities/createInt16SharedArray.ts +43 -0
  190. package/src/utilities/createLinearRGBTransferFunction.ts +22 -0
  191. package/src/utilities/createSigmoidRGBTransferFunction.ts +63 -0
  192. package/src/utilities/createUInt16SharedArray.ts +43 -0
  193. package/src/utilities/createUint8SharedArray.ts +45 -0
  194. package/src/utilities/deepFreeze.ts +19 -0
  195. package/src/utilities/deepMerge.ts +81 -0
  196. package/src/utilities/getClosestImageId.ts +80 -0
  197. package/src/utilities/getClosestStackImageIndexForPoint.ts +116 -0
  198. package/src/utilities/getImageSliceDataForVolumeViewport.ts +61 -0
  199. package/src/utilities/getMinMax.ts +31 -0
  200. package/src/utilities/getRuntimeId.ts +54 -0
  201. package/src/utilities/getScalarDataType.ts +31 -0
  202. package/src/utilities/getScalingParameters.ts +35 -0
  203. package/src/utilities/getSliceRange.ts +86 -0
  204. package/src/utilities/getSpacingInNormalDirection.ts +44 -0
  205. package/src/utilities/getTargetVolumeAndSpacingInNormalDir.ts +126 -0
  206. package/src/utilities/getViewportImageCornersInWorld.ts +102 -0
  207. package/src/utilities/getViewportsWithImageURI.ts +46 -0
  208. package/src/utilities/getViewportsWithVolumeId.ts +38 -0
  209. package/src/utilities/getVoiFromSigmoidRGBTransferFunction.ts +23 -0
  210. package/src/utilities/getVolumeActorCorners.ts +24 -0
  211. package/src/utilities/getVolumeSliceRangeInfo.ts +52 -0
  212. package/src/utilities/getVolumeViewportScrollInfo.ts +32 -0
  213. package/src/utilities/getVolumeViewportsContainingSameVolumes.ts +58 -0
  214. package/src/utilities/hasNaNValues.ts +12 -0
  215. package/src/utilities/imageIdToURI.ts +10 -0
  216. package/src/utilities/imageToWorldCoords.ts +54 -0
  217. package/src/utilities/index.ts +100 -0
  218. package/src/utilities/indexWithinDimensions.ts +27 -0
  219. package/src/utilities/invertRgbTransferFunction.ts +36 -0
  220. package/src/utilities/isEqual.ts +27 -0
  221. package/src/utilities/isOpposite.ts +23 -0
  222. package/src/utilities/isTypedArray.ts +20 -0
  223. package/src/utilities/loadImageToCanvas.ts +80 -0
  224. package/src/utilities/planar.ts +91 -0
  225. package/src/utilities/renderToCanvas.ts +32 -0
  226. package/src/utilities/scaleRgbTransferFunction.ts +37 -0
  227. package/src/utilities/snapFocalPointToSlice.ts +78 -0
  228. package/src/utilities/spatialRegistrationMetadataProvider.ts +50 -0
  229. package/src/utilities/transformWorldToIndex.ts +16 -0
  230. package/src/utilities/triggerEvent.ts +38 -0
  231. package/src/utilities/uuidv4.ts +13 -0
  232. package/src/utilities/windowLevel.ts +39 -0
  233. package/src/utilities/worldToImageCoords.ts +64 -0
@@ -0,0 +1,2690 @@
1
+ import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
2
+ import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
3
+ import type { vtkImageData as vtkImageDataType } from '@kitware/vtk.js/Common/DataModel/ImageData';
4
+ import _cloneDeep from 'lodash.clonedeep';
5
+ import vtkCamera from '@kitware/vtk.js/Rendering/Core/Camera';
6
+ import { vec2, vec3, mat4 } from 'gl-matrix';
7
+ import vtkImageMapper from '@kitware/vtk.js/Rendering/Core/ImageMapper';
8
+ import vtkImageSlice from '@kitware/vtk.js/Rendering/Core/ImageSlice';
9
+ import * as metaData from '../metaData';
10
+ import Viewport from './Viewport';
11
+ import eventTarget from '../eventTarget';
12
+ import Events from '../enums/Events';
13
+ import {
14
+ triggerEvent,
15
+ isEqual,
16
+ invertRgbTransferFunction,
17
+ createSigmoidRGBTransferFunction,
18
+ windowLevel as windowLevelUtil,
19
+ imageIdToURI,
20
+ isImageActor,
21
+ actorIsA,
22
+ } from '../utilities';
23
+ import {
24
+ Point2,
25
+ Point3,
26
+ VOIRange,
27
+ ICamera,
28
+ IImage,
29
+ IImageData,
30
+ CPUIImageData,
31
+ PTScaling,
32
+ Scaling,
33
+ StackViewportProperties,
34
+ FlipDirection,
35
+ ActorEntry,
36
+ CPUFallbackEnabledElement,
37
+ CPUFallbackColormapData,
38
+ EventTypes,
39
+ IStackViewport,
40
+ VolumeActor,
41
+ Mat3,
42
+ } from '../types';
43
+ import { ViewportInput } from '../types/IViewport';
44
+ import drawImageSync from './helpers/cpuFallback/drawImageSync';
45
+ import { getColormap } from './helpers/cpuFallback/colors/index';
46
+
47
+ import { loadAndCacheImage } from '../loaders/imageLoader';
48
+ import imageLoadPoolManager from '../requestPool/imageLoadPoolManager';
49
+ import InterpolationType from '../enums/InterpolationType';
50
+ import VOILUTFunctionType from '../enums/VOILUTFunctionType';
51
+ import canvasToPixel from './helpers/cpuFallback/rendering/canvasToPixel';
52
+ import pixelToCanvas from './helpers/cpuFallback/rendering/pixelToCanvas';
53
+ import getDefaultViewport from './helpers/cpuFallback/rendering/getDefaultViewport';
54
+ import calculateTransform from './helpers/cpuFallback/rendering/calculateTransform';
55
+ import resize from './helpers/cpuFallback/rendering/resize';
56
+
57
+ import resetCamera from './helpers/cpuFallback/rendering/resetCamera';
58
+ import { Transform } from './helpers/cpuFallback/rendering/transform';
59
+ import { getShouldUseCPURendering } from '../init';
60
+ import RequestType from '../enums/RequestType';
61
+ import {
62
+ StackViewportNewStackEventDetail,
63
+ StackViewportScrollEventDetail,
64
+ VoiModifiedEventDetail,
65
+ } from '../types/EventTypes';
66
+ import cache from '../cache';
67
+ import correctShift from './helpers/cpuFallback/rendering/correctShift';
68
+ import { ImageActor } from '../types/IActor';
69
+ import isRgbaSourceRgbDest from './helpers/isRgbaSourceRgbDest';
70
+ import createLinearRGBTransferFunction from '../utilities/createLinearRGBTransferFunction';
71
+
72
+ const EPSILON = 1; // Slice Thickness
73
+
74
+ interface ImagePixelModule {
75
+ bitsAllocated: number;
76
+ bitsStored: number;
77
+ samplesPerPixel: number;
78
+ highBit: number;
79
+ photometricInterpretation: string;
80
+ pixelRepresentation: string;
81
+ windowWidth: number | number[];
82
+ windowCenter: number | number[];
83
+ voiLUTFunction: VOILUTFunctionType;
84
+ modality: string;
85
+ }
86
+
87
+ interface ImageDataMetaData {
88
+ bitsAllocated: number;
89
+ numComps: number;
90
+ origin: Point3;
91
+ direction: Mat3;
92
+ dimensions: Point3;
93
+ spacing: Point3;
94
+ numVoxels: number;
95
+ imagePlaneModule: unknown;
96
+ imagePixelModule: ImagePixelModule;
97
+ }
98
+
99
+ interface ImagePlaneModule {
100
+ columnCosines?: Point3;
101
+ columnPixelSpacing?: number;
102
+ imageOrientationPatient?: Float32Array;
103
+ imagePositionPatient?: Point3;
104
+ pixelSpacing?: Point2;
105
+ rowCosines?: Point3;
106
+ rowPixelSpacing?: number;
107
+ sliceLocation?: number;
108
+ sliceThickness?: number;
109
+ frameOfReferenceUID: string;
110
+ columns: number;
111
+ rows: number;
112
+ }
113
+
114
+ // TODO This needs to be exposed as its published to consumers.
115
+ type CalibrationEvent = {
116
+ rowScale: number;
117
+ columnScale: number;
118
+ };
119
+
120
+ type SetVOIOptions = {
121
+ suppressEvents?: boolean;
122
+ forceRecreateLUTFunction?: boolean;
123
+ voiUpdatedWithSetProperties?: boolean;
124
+ };
125
+
126
+ /**
127
+ * An object representing a single stack viewport, which is a camera
128
+ * looking into an internal viewport, and an associated target output `canvas`.
129
+ *
130
+ * StackViewports can be rendered using both GPU and a fallback CPU is the GPU
131
+ * is not available (or low performance). Read more about StackViewports in
132
+ * the documentation section of this website.
133
+ */
134
+ class StackViewport extends Viewport implements IStackViewport {
135
+ private imageIds: Array<string>;
136
+ // current imageIdIndex that is rendered in the viewport
137
+ private currentImageIdIndex: number;
138
+ // the imageIdIndex that is targeted to be loaded with scrolling but has not initiated loading yet
139
+ private targetImageIdIndex: number;
140
+ // setTimeout if the image is debounced to be loaded
141
+ private debouncedTimeout: number;
142
+
143
+ // Viewport Properties
144
+ private voiRange: VOIRange;
145
+ private voiUpdatedWithSetProperties = false;
146
+ private VOILUTFunction: VOILUTFunctionType;
147
+ //
148
+ private invert = false;
149
+ private interpolationType: InterpolationType;
150
+
151
+ // Helpers
152
+ private _imageData: vtkImageDataType;
153
+ private cameraFocalPointOnRender: Point3; // we use focalPoint since flip manipulates the position and makes it useless to track
154
+ private stackInvalidated = false; // if true -> new actor is forced to be created for the stack
155
+ private _publishCalibratedEvent = false;
156
+ private _calibrationEvent: CalibrationEvent;
157
+ private _cpuFallbackEnabledElement?: CPUFallbackEnabledElement;
158
+ // CPU fallback
159
+ private useCPURendering: boolean;
160
+ private use16BitTexture = false;
161
+ private cpuImagePixelData: number[];
162
+ private cpuRenderingInvalidated: boolean;
163
+ private csImage: IImage;
164
+
165
+ // TODO: These should not be here and will be nuked
166
+ public modality: string; // this is needed for tools
167
+ public scaling: Scaling;
168
+
169
+ // Camera properties
170
+ private initialViewUp: Point3;
171
+
172
+ /**
173
+ * Constructor for the StackViewport class
174
+ * @param props - ViewportInput
175
+ */
176
+ constructor(props: ViewportInput) {
177
+ super(props);
178
+ this.scaling = {};
179
+ this.modality = null;
180
+ this.useCPURendering = getShouldUseCPURendering();
181
+ this.use16BitTexture = this._shouldUse16BitTexture();
182
+ this._configureRenderingPipeline();
183
+
184
+ if (this.useCPURendering) {
185
+ this._cpuFallbackEnabledElement = {
186
+ canvas: this.canvas,
187
+ renderingTools: {},
188
+ transform: new Transform(),
189
+ viewport: { rotation: 0 },
190
+ };
191
+ } else {
192
+ const renderer = this.getRenderer();
193
+ const camera = vtkCamera.newInstance();
194
+ renderer.setActiveCamera(camera);
195
+
196
+ const viewPlaneNormal = <Point3>[0, 0, -1];
197
+ this.initialViewUp = <Point3>[0, -1, 0];
198
+
199
+ camera.setDirectionOfProjection(
200
+ -viewPlaneNormal[0],
201
+ -viewPlaneNormal[1],
202
+ -viewPlaneNormal[2]
203
+ );
204
+ camera.setViewUp(...this.initialViewUp);
205
+ camera.setParallelProjection(true);
206
+ camera.setThicknessFromFocalPoint(0.1);
207
+ camera.setFreezeFocalPoint(true);
208
+ }
209
+
210
+ this.imageIds = [];
211
+ this.currentImageIdIndex = 0;
212
+ this.targetImageIdIndex = 0;
213
+ this.cameraFocalPointOnRender = [0, 0, 0];
214
+ this.resetCamera();
215
+
216
+ this.initializeElementDisabledHandler();
217
+ }
218
+
219
+ static get useCustomRenderingPipeline(): boolean {
220
+ return getShouldUseCPURendering();
221
+ }
222
+
223
+ public setUseCPURendering(value: boolean) {
224
+ this.useCPURendering = value;
225
+ this._configureRenderingPipeline();
226
+ }
227
+
228
+ private _configureRenderingPipeline() {
229
+ for (const [funcName, functions] of Object.entries(
230
+ this.renderingPipelineFunctions
231
+ )) {
232
+ this[funcName] = this.useCPURendering ? functions.cpu : functions.gpu;
233
+ }
234
+ }
235
+
236
+ /**
237
+ * Returns the image and its properties that is being shown inside the
238
+ * stack viewport. It returns, the image dimensions, image direction,
239
+ * image scalar data, vtkImageData object, metadata, and scaling (e.g., PET suvbw)
240
+ *
241
+ * @returns IImageData: dimensions, direction, scalarData, vtkImageData, metadata, scaling
242
+ */
243
+ public getImageData: () => IImageData | CPUIImageData;
244
+
245
+ /**
246
+ * Sets the colormap for the current viewport.
247
+ * @param colormap - The colormap data to use.
248
+ */
249
+ public setColormap: (colormap: CPUFallbackColormapData) => void;
250
+
251
+ /**
252
+ * If the user has selected CPU rendering, return the CPU camera, otherwise
253
+ * return the default camera
254
+ * @returns The camera object.
255
+ */
256
+ public getCamera: () => ICamera;
257
+
258
+ /**
259
+ * Set the camera based on the provided camera object.
260
+ * @param cameraInterface - The camera interface that will be used to
261
+ * render the scene.
262
+ */
263
+ public setCamera: (
264
+ cameraInterface: ICamera,
265
+ storeAsInitialCamera?: boolean
266
+ ) => void;
267
+
268
+ public getRotation: () => number;
269
+
270
+ /**
271
+ * It sets the colormap to the default colormap.
272
+ */
273
+ public unsetColormap: () => void;
274
+
275
+ /**
276
+ * Centers Pan and resets the zoom for stack viewport.
277
+ */
278
+ public resetCamera: (resetPan?: boolean, resetZoom?: boolean) => boolean;
279
+
280
+ /**
281
+ * canvasToWorld Returns the world coordinates of the given `canvasPos`
282
+ * projected onto the plane defined by the `Viewport`'s camera.
283
+ *
284
+ * @param canvasPos - The position in canvas coordinates.
285
+ * @returns The corresponding world coordinates.
286
+ * @public
287
+ */
288
+ public canvasToWorld: (canvasPos: Point2) => Point3;
289
+
290
+ /**
291
+ * Returns the canvas coordinates of the given `worldPos`
292
+ * projected onto the `Viewport`'s `canvas`.
293
+ *
294
+ * @param worldPos - The position in world coordinates.
295
+ * @returns The corresponding canvas coordinates.
296
+ * @public
297
+ */
298
+ public worldToCanvas: (worldPos: Point3) => Point2;
299
+
300
+ /**
301
+ * If the renderer is CPU based, throw an error. Otherwise, returns the `vtkRenderer` responsible for rendering the `Viewport`.
302
+ *
303
+ * @returns The `vtkRenderer` for the `Viewport`.
304
+ */
305
+ public getRenderer: () => any;
306
+
307
+ /**
308
+ * If the renderer is CPU based, throw an error. Otherwise, return the default
309
+ * actor which is the first actor in the renderer.
310
+ * @returns An actor entry.
311
+ */
312
+ public getDefaultActor: () => ActorEntry;
313
+
314
+ /**
315
+ * If the renderer is CPU based, throw an error. Otherwise, return the actors in the viewport
316
+ * @returns An array of ActorEntry objects.
317
+ */
318
+ public getActors: () => Array<ActorEntry>;
319
+ /**
320
+ * If the renderer is CPU based, throw an error. Otherwise, it returns the actor entry for the given actor UID.
321
+ * @param actorUID - The unique ID of the actor you want to get.
322
+ * @returns An ActorEntry object.
323
+ */
324
+ public getActor: (actorUID: string) => ActorEntry;
325
+
326
+ /**
327
+ * If the renderer is CPU-based, throw an error; otherwise, set the
328
+ * actors in the viewport.
329
+ * @param actors - An array of ActorEntry objects.
330
+ */
331
+ public setActors: (actors: Array<ActorEntry>) => void;
332
+
333
+ /**
334
+ * If the renderer is CPU based, throw an error. Otherwise, add a list of actors to the viewport
335
+ * @param actors - An array of ActorEntry objects.
336
+ */
337
+ public addActors: (actors: Array<ActorEntry>) => void;
338
+
339
+ /**
340
+ * If the renderer is CPU based, throw an error. Otherwise, add the
341
+ * actor to the viewport
342
+ * @param actorEntry - The ActorEntry object that was created by the
343
+ * user.
344
+ */
345
+ public addActor: (actorEntry: ActorEntry) => void;
346
+
347
+ /**
348
+ * It throws an error if the renderer is CPU based. Otherwise, it removes the actors from the viewport.
349
+ */
350
+ public removeAllActors: () => void;
351
+
352
+ private setVOI: (voiRange: VOIRange, options?: SetVOIOptions) => void;
353
+
354
+ private setInterpolationType: (interpolationType: InterpolationType) => void;
355
+
356
+ private setInvertColor: (invert: boolean) => void;
357
+
358
+ private initializeElementDisabledHandler() {
359
+ eventTarget.addEventListener(
360
+ Events.ELEMENT_DISABLED,
361
+ function elementDisabledHandler() {
362
+ clearTimeout(this.debouncedTimeout);
363
+
364
+ eventTarget.removeEventListener(
365
+ Events.ELEMENT_DISABLED,
366
+ elementDisabledHandler
367
+ );
368
+ }
369
+ );
370
+ }
371
+
372
+ /**
373
+ * Resizes the viewport - only used in CPU fallback for StackViewport. The
374
+ * GPU resizing happens inside the RenderingEngine.
375
+ */
376
+ public resize = (): void => {
377
+ // GPU viewport resize is handled inside the RenderingEngine
378
+ if (this.useCPURendering) {
379
+ this._resizeCPU();
380
+ }
381
+ };
382
+
383
+ private _resizeCPU = (): void => {
384
+ if (this._cpuFallbackEnabledElement.viewport) {
385
+ resize(this._cpuFallbackEnabledElement);
386
+ }
387
+ };
388
+
389
+ private getImageDataGPU(): IImageData | undefined {
390
+ const defaultActor = this.getDefaultActor();
391
+
392
+ if (!defaultActor) {
393
+ return;
394
+ }
395
+
396
+ if (!isImageActor(defaultActor)) {
397
+ return;
398
+ }
399
+
400
+ const { actor } = defaultActor;
401
+ const vtkImageData = actor.getMapper().getInputData();
402
+ return {
403
+ dimensions: vtkImageData.getDimensions(),
404
+ spacing: vtkImageData.getSpacing(),
405
+ origin: vtkImageData.getOrigin(),
406
+ direction: vtkImageData.getDirection(),
407
+ scalarData: vtkImageData.getPointData().getScalars().getData(),
408
+ imageData: actor.getMapper().getInputData(),
409
+ metadata: { Modality: this.modality },
410
+ scaling: this.scaling,
411
+ hasPixelSpacing: this.hasPixelSpacing,
412
+ preScale: {
413
+ ...this.csImage.preScale,
414
+ },
415
+ };
416
+ }
417
+
418
+ private getImageDataCPU(): CPUIImageData | undefined {
419
+ const { metadata } = this._cpuFallbackEnabledElement;
420
+
421
+ const spacing = metadata.spacing;
422
+
423
+ return {
424
+ dimensions: metadata.dimensions,
425
+ spacing,
426
+ origin: metadata.origin,
427
+ direction: metadata.direction,
428
+ metadata: { Modality: this.modality },
429
+ scaling: this.scaling,
430
+ imageData: {
431
+ getDirection: () => metadata.direction,
432
+ getDimensions: () => metadata.dimensions,
433
+ getScalarData: () => this.cpuImagePixelData,
434
+ getSpacing: () => spacing,
435
+ worldToIndex: (point: Point3) => {
436
+ const canvasPoint = this.worldToCanvasCPU(point);
437
+ const pixelCoord = canvasToPixel(
438
+ this._cpuFallbackEnabledElement,
439
+ canvasPoint
440
+ );
441
+ return [pixelCoord[0], pixelCoord[1], 0];
442
+ },
443
+ indexToWorld: (point: Point3) => {
444
+ const canvasPoint = pixelToCanvas(this._cpuFallbackEnabledElement, [
445
+ point[0],
446
+ point[1],
447
+ ]);
448
+ return this.canvasToWorldCPU(canvasPoint);
449
+ },
450
+ },
451
+ scalarData: this.cpuImagePixelData,
452
+ hasPixelSpacing: this.hasPixelSpacing,
453
+ preScale: {
454
+ ...this.csImage.preScale,
455
+ },
456
+ };
457
+ }
458
+
459
+ /**
460
+ * Returns the frame of reference UID, if the image doesn't have imagePlaneModule
461
+ * metadata, it returns undefined, otherwise, frameOfReferenceUID is returned.
462
+ * @returns frameOfReferenceUID : string representing frame of reference id
463
+ */
464
+ public getFrameOfReferenceUID = (): string | undefined => {
465
+ // Get the current image that is displayed in the viewport
466
+ const imageId = this.getCurrentImageId();
467
+
468
+ if (!imageId) {
469
+ return;
470
+ }
471
+
472
+ // Use the metadata provider to grab its imagePlaneModule metadata
473
+ const imagePlaneModule = metaData.get('imagePlaneModule', imageId);
474
+
475
+ // If nothing exists, return undefined
476
+ if (!imagePlaneModule) {
477
+ return;
478
+ }
479
+
480
+ // Otherwise, provide the FrameOfReferenceUID so we can map
481
+ // annotations made on VolumeViewports back to StackViewports
482
+ // and vice versa
483
+ return imagePlaneModule.frameOfReferenceUID;
484
+ };
485
+
486
+ /**
487
+ * Creates imageMapper based on the provided vtkImageData and also creates
488
+ * the imageSliceActor and connects it to the imageMapper.
489
+ * For color stack images, it sets the independent components to be false which
490
+ * is required in vtk.
491
+ *
492
+ * @param imageData - vtkImageData for the viewport
493
+ * @returns actor vtkActor
494
+ */
495
+ private createActorMapper = (imageData) => {
496
+ const mapper = vtkImageMapper.newInstance();
497
+ mapper.setInputData(imageData);
498
+
499
+ const actor = vtkImageSlice.newInstance();
500
+
501
+ actor.setMapper(mapper);
502
+
503
+ if (imageData.getPointData().getNumberOfComponents() > 1) {
504
+ actor.getProperty().setIndependentComponents(false);
505
+ }
506
+
507
+ return actor;
508
+ };
509
+
510
+ /**
511
+ * Retrieves the metadata from the metadata provider, and optionally adds the
512
+ * scaling to the viewport if modality is PET and scaling metadata is provided.
513
+ *
514
+ * @param imageId - a string representing the imageId for the image
515
+ * @returns imagePlaneModule and imagePixelModule containing the metadata for the image
516
+ */
517
+ private buildMetadata(image: IImage) {
518
+ const imageId = image.imageId;
519
+
520
+ const {
521
+ pixelRepresentation,
522
+ bitsAllocated,
523
+ bitsStored,
524
+ highBit,
525
+ photometricInterpretation,
526
+ samplesPerPixel,
527
+ } = metaData.get('imagePixelModule', imageId);
528
+
529
+ // we can grab the window center and width from the image object
530
+ // since it the loader already has used the metadata provider
531
+ // to get the values
532
+ const { windowWidth, windowCenter, voiLUTFunction } = image;
533
+
534
+ const { modality } = metaData.get('generalSeriesModule', imageId);
535
+ const imageIdScalingFactor = metaData.get('scalingModule', imageId);
536
+
537
+ if (modality === 'PT' && imageIdScalingFactor) {
538
+ this._addScalingToViewport(imageIdScalingFactor);
539
+ }
540
+
541
+ this.modality = modality;
542
+ const voiLUTFunctionEnum = this._getValidVOILUTFunction(voiLUTFunction);
543
+ this.VOILUTFunction = voiLUTFunctionEnum;
544
+
545
+ let imagePlaneModule = this._getImagePlaneModule(imageId);
546
+
547
+ if (!this.useCPURendering) {
548
+ imagePlaneModule = this.calibrateIfNecessary(imageId, imagePlaneModule);
549
+ }
550
+
551
+ return {
552
+ imagePlaneModule,
553
+ imagePixelModule: {
554
+ bitsAllocated,
555
+ bitsStored,
556
+ samplesPerPixel,
557
+ highBit,
558
+ photometricInterpretation,
559
+ pixelRepresentation,
560
+ windowWidth,
561
+ windowCenter,
562
+ modality,
563
+ voiLUTFunction: voiLUTFunctionEnum,
564
+ },
565
+ };
566
+ }
567
+
568
+ /**
569
+ * Checks the metadataProviders to see if a calibratedPixelSpacing is
570
+ * given. If so, checks the actor to see if it needs to be modified, and
571
+ * set the flags for imageCalibration if a new actor needs to be created
572
+ *
573
+ * @param imageId - imageId
574
+ * @param imagePlaneModule - imagePlaneModule
575
+ * @returns modified imagePlaneModule with the calibrated spacings
576
+ */
577
+ private calibrateIfNecessary(imageId, imagePlaneModule) {
578
+ const calibratedPixelSpacing = metaData.get(
579
+ 'calibratedPixelSpacing',
580
+ imageId
581
+ );
582
+
583
+ if (!calibratedPixelSpacing) {
584
+ return imagePlaneModule;
585
+ }
586
+
587
+ const [calibratedRowSpacing, calibratedColumnSpacing] =
588
+ calibratedPixelSpacing;
589
+
590
+ // Todo: This is necessary in general, but breaks an edge case when an image
591
+ // is calibrated to some other spacing, and it gets calibrated BACK to the
592
+ // original spacing.
593
+ if (
594
+ imagePlaneModule.rowPixelSpacing === calibratedRowSpacing &&
595
+ imagePlaneModule.columnPixelSpacing === calibratedColumnSpacing
596
+ ) {
597
+ return imagePlaneModule;
598
+ }
599
+
600
+ // Check if there is already an actor
601
+ const imageDataMetadata = this.getImageData();
602
+
603
+ // If no actor (first load) and calibration matches the dicom header
604
+ if (
605
+ !imageDataMetadata &&
606
+ imagePlaneModule.rowPixelSpacing === calibratedRowSpacing &&
607
+ imagePlaneModule.columnPixelSpacing === calibratedColumnSpacing
608
+ ) {
609
+ return imagePlaneModule;
610
+ }
611
+
612
+ // If no actor (first load) and calibration doesn't match headers
613
+ // -> needs calibration
614
+ if (
615
+ !imageDataMetadata &&
616
+ (imagePlaneModule.rowPixelSpacing !== calibratedRowSpacing ||
617
+ imagePlaneModule.columnPixelSpacing !== calibratedColumnSpacing)
618
+ ) {
619
+ this._publishCalibratedEvent = true;
620
+
621
+ this._calibrationEvent = <CalibrationEvent>{
622
+ rowScale: calibratedRowSpacing / imagePlaneModule.rowPixelSpacing,
623
+ columnScale:
624
+ calibratedColumnSpacing / imagePlaneModule.columnPixelSpacing,
625
+ };
626
+
627
+ // modify imagePlaneModule for actor to use calibrated spacing
628
+ imagePlaneModule.rowPixelSpacing = calibratedRowSpacing;
629
+ imagePlaneModule.columnPixelSpacing = calibratedColumnSpacing;
630
+ return imagePlaneModule;
631
+ }
632
+
633
+ // If there is already an actor, check if calibration is needed for the current actor
634
+ const { imageData } = imageDataMetadata;
635
+ const [columnPixelSpacing, rowPixelSpacing] = imageData.getSpacing();
636
+
637
+ imagePlaneModule.rowPixelSpacing = calibratedRowSpacing;
638
+ imagePlaneModule.columnPixelSpacing = calibratedColumnSpacing;
639
+
640
+ // If current actor spacing matches the calibrated spacing
641
+ if (
642
+ rowPixelSpacing === calibratedRowSpacing &&
643
+ columnPixelSpacing === calibratedPixelSpacing
644
+ ) {
645
+ // No calibration is required
646
+ return imagePlaneModule;
647
+ }
648
+
649
+ // Calibration is required
650
+ this._publishCalibratedEvent = true;
651
+
652
+ this._calibrationEvent = <CalibrationEvent>{
653
+ rowScale: calibratedRowSpacing / rowPixelSpacing,
654
+ columnScale: calibratedColumnSpacing / columnPixelSpacing,
655
+ };
656
+
657
+ return imagePlaneModule;
658
+ }
659
+
660
+ /**
661
+ * Sets the properties for the viewport on the default actor. Properties include
662
+ * setting the VOI, inverting the colors and setting the interpolation type, rotation
663
+ * @param voiRange - Sets the lower and upper voi
664
+ * @param invert - Inverts the colors
665
+ * @param interpolationType - Changes the interpolation type (1:linear, 0: nearest)
666
+ * @param rotation - image rotation in degrees
667
+ */
668
+ public setProperties(
669
+ {
670
+ voiRange,
671
+ VOILUTFunction,
672
+ invert,
673
+ interpolationType,
674
+ rotation,
675
+ }: StackViewportProperties = {},
676
+ suppressEvents = false
677
+ ): void {
678
+ // if voi is not applied for the first time, run the setVOI function
679
+ // which will apply the default voi based on the range
680
+ if (typeof voiRange !== 'undefined') {
681
+ const voiUpdatedWithSetProperties = true;
682
+ this.setVOI(voiRange, { suppressEvents, voiUpdatedWithSetProperties });
683
+ }
684
+
685
+ if (typeof VOILUTFunction !== 'undefined') {
686
+ this.setVOILUTFunction(VOILUTFunction, suppressEvents);
687
+ }
688
+
689
+ if (typeof invert !== 'undefined') {
690
+ this.setInvertColor(invert);
691
+ }
692
+
693
+ if (typeof interpolationType !== 'undefined') {
694
+ this.setInterpolationType(interpolationType);
695
+ }
696
+
697
+ if (typeof rotation !== 'undefined') {
698
+ // TODO: check with VTK about rounding errors here.
699
+ if (this.getRotation() !== rotation) {
700
+ this.setRotation(rotation);
701
+ }
702
+ }
703
+ }
704
+
705
+ /**
706
+ * Retrieve the viewport properties
707
+ * @returns viewport properties including voi, invert, interpolation type, rotation, flip
708
+ */
709
+ public getProperties = (): StackViewportProperties => {
710
+ const {
711
+ voiRange,
712
+ VOILUTFunction,
713
+ interpolationType,
714
+ invert,
715
+ voiUpdatedWithSetProperties,
716
+ } = this;
717
+ const rotation = this.getRotation();
718
+
719
+ return {
720
+ voiRange,
721
+ VOILUTFunction,
722
+ interpolationType,
723
+ invert,
724
+ rotation,
725
+ isComputedVOI: !voiUpdatedWithSetProperties,
726
+ };
727
+ };
728
+
729
+ /**
730
+ * Reset the viewport properties to the default values
731
+ */
732
+ public resetProperties(): void {
733
+ this.cpuRenderingInvalidated = true;
734
+ this.voiUpdatedWithSetProperties = false;
735
+
736
+ this.fillWithBackgroundColor();
737
+
738
+ if (this.useCPURendering) {
739
+ this._cpuFallbackEnabledElement.renderingTools = {};
740
+ }
741
+
742
+ this._resetProperties();
743
+
744
+ this.render();
745
+ }
746
+
747
+ private _resetProperties() {
748
+ let voiRange;
749
+ if (this._isCurrentImagePTPrescaled()) {
750
+ // if not set via setProperties; if it is a PT image and is already prescaled,
751
+ // use the default range for PT
752
+ voiRange = this._getDefaultPTPrescaledVOIRange();
753
+ } else {
754
+ // if not set via setProperties; if it is not a PT image or is not prescaled,
755
+ // use the voiRange for the current image from its metadata if found
756
+ // otherwise, use the cached voiRange
757
+ voiRange = this._getVOIRangeForCurrentImage();
758
+ }
759
+
760
+ this.setVOI(voiRange);
761
+
762
+ if (this.getRotation() !== 0) {
763
+ this.setRotation(0);
764
+ }
765
+ this.setInterpolationType(InterpolationType.LINEAR);
766
+ this.setInvertColor(false);
767
+ }
768
+
769
+ private _setPropertiesFromCache(): void {
770
+ const { interpolationType, invert } = this;
771
+
772
+ let voiRange;
773
+ if (this.voiUpdatedWithSetProperties) {
774
+ // use the cached voiRange if the voiRange is locked (if the user has
775
+ // manually set the voi with tools or setProperties api)
776
+ voiRange = this.voiRange;
777
+ } else if (this._isCurrentImagePTPrescaled()) {
778
+ // if not set via setProperties; if it is a PT image and is already prescaled,
779
+ // use the default range for PT
780
+ voiRange = this._getDefaultPTPrescaledVOIRange();
781
+ } else {
782
+ // if not set via setProperties; if it is not a PT image or is not prescaled,
783
+ // use the voiRange for the current image from its metadata if found
784
+ // otherwise, use the cached voiRange
785
+ voiRange = this._getVOIRangeForCurrentImage() ?? this.voiRange;
786
+ }
787
+
788
+ this.setVOI(voiRange);
789
+ this.setInterpolationType(interpolationType);
790
+ this.setInvertColor(invert);
791
+ }
792
+
793
+ private getCameraCPU(): Partial<ICamera> {
794
+ const { metadata, viewport } = this._cpuFallbackEnabledElement;
795
+ const { direction } = metadata;
796
+
797
+ // focalPoint and position of CPU camera is just a placeholder since
798
+ // tools need focalPoint to be defined
799
+ const viewPlaneNormal = direction.slice(6, 9).map((x) => -x) as Point3;
800
+ let viewUp = direction.slice(3, 6).map((x) => -x) as Point3;
801
+
802
+ // If camera is rotated, we need the correct rotated viewUp along the
803
+ // viewPlaneNormal vector
804
+ if (viewport.rotation) {
805
+ const rotationMatrix = mat4.fromRotation(
806
+ mat4.create(),
807
+ (viewport.rotation * Math.PI) / 180,
808
+ viewPlaneNormal
809
+ );
810
+ viewUp = vec3.transformMat4(
811
+ vec3.create(),
812
+ viewUp,
813
+ rotationMatrix
814
+ ) as Point3;
815
+ }
816
+
817
+ const canvasCenter: Point2 = [
818
+ this.element.clientWidth / 2,
819
+ this.element.clientHeight / 2,
820
+ ];
821
+
822
+ // Focal point is the center of the canvas in world coordinate by construction
823
+ const canvasCenterWorld = this.canvasToWorld(canvasCenter);
824
+
825
+ // parallel scale is half of the viewport height in the world units (mm)
826
+
827
+ const topLeftWorld = this.canvasToWorld([0, 0]);
828
+ const bottomLeftWorld = this.canvasToWorld([0, this.element.clientHeight]);
829
+
830
+ const parallelScale = vec3.distance(topLeftWorld, bottomLeftWorld) / 2;
831
+
832
+ return {
833
+ parallelProjection: true,
834
+ focalPoint: canvasCenterWorld,
835
+ position: [0, 0, 0],
836
+ parallelScale,
837
+ scale: viewport.scale,
838
+ viewPlaneNormal: [
839
+ viewPlaneNormal[0],
840
+ viewPlaneNormal[1],
841
+ viewPlaneNormal[2],
842
+ ],
843
+ viewUp: [viewUp[0], viewUp[1], viewUp[2]],
844
+ flipHorizontal: this.flipHorizontal,
845
+ flipVertical: this.flipVertical,
846
+ };
847
+ }
848
+
849
+ private setCameraCPU(cameraInterface: ICamera): void {
850
+ const { viewport, image } = this._cpuFallbackEnabledElement;
851
+ const previousCamera = this.getCameraCPU();
852
+
853
+ const { focalPoint, parallelScale, scale, flipHorizontal, flipVertical } =
854
+ cameraInterface;
855
+
856
+ const { clientHeight } = this.element;
857
+
858
+ if (focalPoint) {
859
+ const focalPointCanvas = this.worldToCanvasCPU(focalPoint);
860
+ const focalPointPixel = canvasToPixel(
861
+ this._cpuFallbackEnabledElement,
862
+ focalPointCanvas
863
+ );
864
+
865
+ const prevFocalPointCanvas = this.worldToCanvasCPU(
866
+ previousCamera.focalPoint
867
+ );
868
+ const prevFocalPointPixel = canvasToPixel(
869
+ this._cpuFallbackEnabledElement,
870
+ prevFocalPointCanvas
871
+ );
872
+
873
+ const deltaPixel = vec2.create();
874
+ vec2.subtract(
875
+ deltaPixel,
876
+ vec2.fromValues(focalPointPixel[0], focalPointPixel[1]),
877
+ vec2.fromValues(prevFocalPointPixel[0], prevFocalPointPixel[1])
878
+ );
879
+
880
+ const shift = correctShift(
881
+ { x: deltaPixel[0], y: deltaPixel[1] },
882
+ viewport
883
+ );
884
+
885
+ viewport.translation.x -= shift.x;
886
+ viewport.translation.y -= shift.y;
887
+ }
888
+
889
+ if (parallelScale) {
890
+ // We need to convert he parallelScale which has a physical meaning to
891
+ // camera scale factor (since CPU works with scale). Since parallelScale represents
892
+ // half of the height of the viewport in the world unit (mm), we can use that
893
+ // to compute the scale factor which is the ratio of the viewport height in pixels
894
+ // to the current rendered image height.
895
+ const { rowPixelSpacing } = image;
896
+ const scale = (clientHeight * rowPixelSpacing * 0.5) / parallelScale;
897
+
898
+ viewport.scale = scale;
899
+ viewport.parallelScale = parallelScale;
900
+ }
901
+
902
+ if (scale) {
903
+ const { rowPixelSpacing } = image;
904
+ viewport.scale = scale;
905
+ viewport.parallelScale = (clientHeight * rowPixelSpacing * 0.5) / scale;
906
+ }
907
+
908
+ if (flipHorizontal !== undefined || flipVertical !== undefined) {
909
+ this.setFlipCPU({ flipHorizontal, flipVertical });
910
+ }
911
+
912
+ // re-calculate the transforms
913
+ this._cpuFallbackEnabledElement.transform = calculateTransform(
914
+ this._cpuFallbackEnabledElement
915
+ );
916
+
917
+ const eventDetail: EventTypes.CameraModifiedEventDetail = {
918
+ previousCamera,
919
+ camera: this.getCamera(),
920
+ element: this.element,
921
+ viewportId: this.id,
922
+ renderingEngineId: this.renderingEngineId,
923
+ rotation: this.getRotation(),
924
+ };
925
+
926
+ triggerEvent(this.element, Events.CAMERA_MODIFIED, eventDetail);
927
+ }
928
+
929
+ private setFlipCPU({ flipHorizontal, flipVertical }: FlipDirection): void {
930
+ const { viewport } = this._cpuFallbackEnabledElement;
931
+
932
+ if (flipHorizontal !== undefined) {
933
+ viewport.hflip = flipHorizontal;
934
+ this.flipHorizontal = viewport.hflip;
935
+ }
936
+
937
+ if (flipVertical !== undefined) {
938
+ viewport.vflip = flipVertical;
939
+ this.flipVertical = viewport.vflip;
940
+ }
941
+ }
942
+
943
+ private getRotationCPU = (): number => {
944
+ const { viewport } = this._cpuFallbackEnabledElement;
945
+ return viewport.rotation;
946
+ };
947
+
948
+ /**
949
+ * Gets the rotation resulting from the value set in setRotation AND taking into
950
+ * account any flips that occurred subsequently.
951
+ *
952
+ * @returns the rotation resulting from the value set in setRotation AND taking into
953
+ * account any flips that occurred subsequently.
954
+ */
955
+ private getRotationGPU = (): number => {
956
+ const {
957
+ viewUp: currentViewUp,
958
+ viewPlaneNormal,
959
+ flipVertical,
960
+ } = this.getCamera();
961
+
962
+ // The initial view up vector without any rotation, but incorporating vertical flip.
963
+ const initialViewUp = flipVertical
964
+ ? vec3.negate(vec3.create(), this.initialViewUp)
965
+ : this.initialViewUp;
966
+
967
+ // The angle between the initial and current view up vectors.
968
+ // TODO: check with VTK about rounding errors here.
969
+ const initialToCurrentViewUpAngle =
970
+ (vec3.angle(initialViewUp, currentViewUp) * 180) / Math.PI;
971
+
972
+ // Now determine if initialToCurrentViewUpAngle is positive or negative by comparing
973
+ // the direction of the initial/current view up cross product with the current
974
+ // viewPlaneNormal.
975
+
976
+ const initialToCurrentViewUpCross = vec3.cross(
977
+ vec3.create(),
978
+ initialViewUp,
979
+ currentViewUp
980
+ );
981
+
982
+ // The sign of the dot product of the start/end view up cross product and
983
+ // the viewPlaneNormal indicates a positive or negative rotation respectively.
984
+ const normalDot = vec3.dot(initialToCurrentViewUpCross, viewPlaneNormal);
985
+
986
+ return normalDot >= 0
987
+ ? initialToCurrentViewUpAngle
988
+ : (360 - initialToCurrentViewUpAngle) % 360;
989
+ };
990
+
991
+ private setRotation(rotation: number): void {
992
+ const previousCamera = this.getCamera();
993
+
994
+ this.useCPURendering
995
+ ? this.setRotationCPU(rotation)
996
+ : this.setRotationGPU(rotation);
997
+
998
+ // New camera after rotation
999
+ const camera = this.getCamera();
1000
+
1001
+ const eventDetail: EventTypes.CameraModifiedEventDetail = {
1002
+ previousCamera,
1003
+ camera,
1004
+ element: this.element,
1005
+ viewportId: this.id,
1006
+ renderingEngineId: this.renderingEngineId,
1007
+ rotation,
1008
+ };
1009
+
1010
+ triggerEvent(this.element, Events.CAMERA_MODIFIED, eventDetail);
1011
+ }
1012
+
1013
+ private setVOILUTFunction(
1014
+ voiLUTFunction: VOILUTFunctionType,
1015
+ suppressEvents?: boolean
1016
+ ): void {
1017
+ if (this.useCPURendering) {
1018
+ throw new Error('VOI LUT function is not supported in CPU rendering');
1019
+ }
1020
+
1021
+ // make sure the VOI LUT function is valid in the VOILUTFunctionType which is enum
1022
+ const newVOILUTFunction = this._getValidVOILUTFunction(voiLUTFunction);
1023
+
1024
+ let forceRecreateLUTFunction = false;
1025
+ if (
1026
+ this.VOILUTFunction !== VOILUTFunctionType.LINEAR &&
1027
+ newVOILUTFunction === VOILUTFunctionType.LINEAR
1028
+ ) {
1029
+ forceRecreateLUTFunction = true;
1030
+ }
1031
+
1032
+ this.VOILUTFunction = newVOILUTFunction;
1033
+
1034
+ const { voiRange } = this.getProperties();
1035
+ this.setVOI(voiRange, { suppressEvents, forceRecreateLUTFunction });
1036
+ }
1037
+
1038
+ private setRotationCPU(rotation: number): void {
1039
+ const { viewport } = this._cpuFallbackEnabledElement;
1040
+ viewport.rotation = rotation;
1041
+ }
1042
+
1043
+ private setRotationGPU(rotation: number): void {
1044
+ const { flipVertical } = this.getCamera();
1045
+
1046
+ // Moving back to zero rotation, for new scrolled slice rotation is 0 after camera reset
1047
+ const initialViewUp = flipVertical
1048
+ ? vec3.negate(vec3.create(), this.initialViewUp)
1049
+ : this.initialViewUp;
1050
+
1051
+ this.setCamera({
1052
+ viewUp: initialViewUp as Point3,
1053
+ });
1054
+
1055
+ // rotating camera to the new value
1056
+ this.getVtkActiveCamera().roll(-rotation);
1057
+ }
1058
+
1059
+ private setInterpolationTypeGPU(interpolationType: InterpolationType): void {
1060
+ const defaultActor = this.getDefaultActor();
1061
+
1062
+ if (!defaultActor) {
1063
+ return;
1064
+ }
1065
+
1066
+ if (!isImageActor(defaultActor)) {
1067
+ return;
1068
+ }
1069
+ const { actor } = defaultActor;
1070
+ const volumeProperty = actor.getProperty();
1071
+
1072
+ // @ts-ignore
1073
+ volumeProperty.setInterpolationType(interpolationType);
1074
+ this.interpolationType = interpolationType;
1075
+ }
1076
+
1077
+ private setInterpolationTypeCPU(interpolationType: InterpolationType): void {
1078
+ const { viewport } = this._cpuFallbackEnabledElement;
1079
+
1080
+ viewport.pixelReplication =
1081
+ interpolationType === InterpolationType.LINEAR ? false : true;
1082
+
1083
+ this.interpolationType = interpolationType;
1084
+ }
1085
+
1086
+ private setInvertColorCPU(invert: boolean): void {
1087
+ const { viewport } = this._cpuFallbackEnabledElement;
1088
+
1089
+ if (!viewport) {
1090
+ return;
1091
+ }
1092
+
1093
+ viewport.invert = invert;
1094
+ this.invert = invert;
1095
+ }
1096
+
1097
+ private setInvertColorGPU(invert: boolean): void {
1098
+ const defaultActor = this.getDefaultActor();
1099
+
1100
+ if (!defaultActor) {
1101
+ return;
1102
+ }
1103
+
1104
+ if (!isImageActor(defaultActor)) {
1105
+ return;
1106
+ }
1107
+
1108
+ // Duplicated logic to make sure typescript stops complaining
1109
+ // about vtkActor not having the correct property
1110
+ if (actorIsA(defaultActor, 'vtkVolume')) {
1111
+ const volumeActor = defaultActor.actor as VolumeActor;
1112
+ const tfunc = volumeActor.getProperty().getRGBTransferFunction(0);
1113
+
1114
+ if ((!this.invert && invert) || (this.invert && !invert)) {
1115
+ invertRgbTransferFunction(tfunc);
1116
+ }
1117
+ this.invert = invert;
1118
+ } else if (actorIsA(defaultActor, 'vtkImageSlice')) {
1119
+ const imageSliceActor = defaultActor.actor as vtkImageSlice;
1120
+ const tfunc = imageSliceActor.getProperty().getRGBTransferFunction(0);
1121
+
1122
+ if ((!this.invert && invert) || (this.invert && !invert)) {
1123
+ invertRgbTransferFunction(tfunc);
1124
+ }
1125
+ this.invert = invert;
1126
+ }
1127
+ }
1128
+
1129
+ private setVOICPU(voiRange: VOIRange, options: SetVOIOptions = {}): void {
1130
+ const { suppressEvents = false } = options;
1131
+ // TODO: Account for VOILUTFunction
1132
+ const { viewport, image } = this._cpuFallbackEnabledElement;
1133
+
1134
+ if (!viewport || !image) {
1135
+ return;
1136
+ }
1137
+
1138
+ if (typeof voiRange === 'undefined') {
1139
+ const { windowWidth: ww, windowCenter: wc } = image;
1140
+
1141
+ const wwToUse = Array.isArray(ww) ? ww[0] : ww;
1142
+ const wcToUse = Array.isArray(wc) ? wc[0] : wc;
1143
+ viewport.voi = {
1144
+ windowWidth: wwToUse,
1145
+ windowCenter: wcToUse,
1146
+ };
1147
+
1148
+ const { lower, upper } = windowLevelUtil.toLowHighRange(wwToUse, wcToUse);
1149
+ voiRange = { lower, upper };
1150
+ } else {
1151
+ const { lower, upper } = voiRange;
1152
+ const { windowCenter, windowWidth } = windowLevelUtil.toWindowLevel(
1153
+ lower,
1154
+ upper
1155
+ );
1156
+
1157
+ if (!viewport.voi) {
1158
+ viewport.voi = {
1159
+ windowWidth: 0,
1160
+ windowCenter: 0,
1161
+ };
1162
+ }
1163
+
1164
+ viewport.voi.windowWidth = windowWidth;
1165
+ viewport.voi.windowCenter = windowCenter;
1166
+ }
1167
+
1168
+ this.voiRange = voiRange;
1169
+ const eventDetail: VoiModifiedEventDetail = {
1170
+ viewportId: this.id,
1171
+ range: voiRange,
1172
+ };
1173
+
1174
+ if (!suppressEvents) {
1175
+ triggerEvent(this.element, Events.VOI_MODIFIED, eventDetail);
1176
+ }
1177
+ }
1178
+
1179
+ private setVOIGPU(voiRange: VOIRange, options: SetVOIOptions = {}): void {
1180
+ const {
1181
+ suppressEvents = false,
1182
+ forceRecreateLUTFunction = false,
1183
+ voiUpdatedWithSetProperties = false,
1184
+ } = options;
1185
+
1186
+ if (
1187
+ voiRange &&
1188
+ this.voiRange &&
1189
+ this.voiRange.lower === voiRange.lower &&
1190
+ this.voiRange.upper === voiRange.upper &&
1191
+ !forceRecreateLUTFunction
1192
+ ) {
1193
+ return;
1194
+ }
1195
+
1196
+ const defaultActor = this.getDefaultActor();
1197
+ if (!defaultActor) {
1198
+ return;
1199
+ }
1200
+
1201
+ if (!isImageActor(defaultActor)) {
1202
+ return;
1203
+ }
1204
+ const imageActor = defaultActor.actor as ImageActor;
1205
+
1206
+ let voiRangeToUse = voiRange;
1207
+
1208
+ if (typeof voiRangeToUse === 'undefined') {
1209
+ const imageData = imageActor.getMapper().getInputData();
1210
+ const range = imageData.getPointData().getScalars().getRange();
1211
+ const maxVoiRange = { lower: range[0], upper: range[1] };
1212
+ voiRangeToUse = maxVoiRange;
1213
+ }
1214
+
1215
+ // scaling logic here
1216
+ // https://github.com/Kitware/vtk-js/blob/master/Sources/Rendering/OpenGL/ImageMapper/index.js#L540-L549
1217
+ imageActor.getProperty().setUseLookupTableScalarRange(true);
1218
+
1219
+ let transferFunction = imageActor.getProperty().getRGBTransferFunction(0);
1220
+
1221
+ const isSigmoidTFun =
1222
+ this.VOILUTFunction === VOILUTFunctionType.SAMPLED_SIGMOID;
1223
+
1224
+ // use the old cfun if it exists for linear case
1225
+ if (isSigmoidTFun || !transferFunction || forceRecreateLUTFunction) {
1226
+ const transferFunctionCreator = isSigmoidTFun
1227
+ ? createSigmoidRGBTransferFunction
1228
+ : createLinearRGBTransferFunction;
1229
+
1230
+ transferFunction = transferFunctionCreator(voiRangeToUse);
1231
+
1232
+ if (this.invert) {
1233
+ invertRgbTransferFunction(transferFunction);
1234
+ }
1235
+
1236
+ imageActor.getProperty().setRGBTransferFunction(0, transferFunction);
1237
+ }
1238
+
1239
+ if (!isSigmoidTFun) {
1240
+ // @ts-ignore vtk type error
1241
+ transferFunction.setRange(voiRangeToUse.lower, voiRangeToUse.upper);
1242
+ }
1243
+
1244
+ this.voiRange = voiRangeToUse;
1245
+
1246
+ // if voiRange is set by setProperties we need to lock it if it is not locked already
1247
+ if (!this.voiUpdatedWithSetProperties) {
1248
+ this.voiUpdatedWithSetProperties = voiUpdatedWithSetProperties;
1249
+ }
1250
+
1251
+ if (suppressEvents) {
1252
+ return;
1253
+ }
1254
+
1255
+ const eventDetail: VoiModifiedEventDetail = {
1256
+ viewportId: this.id,
1257
+ range: voiRangeToUse,
1258
+ VOILUTFunction: this.VOILUTFunction,
1259
+ };
1260
+
1261
+ triggerEvent(this.element, Events.VOI_MODIFIED, eventDetail);
1262
+ }
1263
+
1264
+ /**
1265
+ * Adds scaling parameters to the viewport to be used along all slices
1266
+ *
1267
+ * @param imageIdScalingFactor - suvbw, suvlbm, suvbsa
1268
+ */
1269
+ private _addScalingToViewport(imageIdScalingFactor) {
1270
+ if (this.scaling.PET) {
1271
+ return;
1272
+ }
1273
+
1274
+ // if don't exist
1275
+ // These ratios are constant across all frames, so only need one.
1276
+ const { suvbw, suvlbm, suvbsa } = imageIdScalingFactor;
1277
+
1278
+ const petScaling = <PTScaling>{};
1279
+
1280
+ if (suvlbm) {
1281
+ petScaling.suvbwToSuvlbm = suvlbm / suvbw;
1282
+ }
1283
+
1284
+ if (suvbsa) {
1285
+ petScaling.suvbwToSuvbsa = suvbsa / suvbw;
1286
+ }
1287
+
1288
+ this.scaling.PET = petScaling;
1289
+ }
1290
+
1291
+ /**
1292
+ * Calculates number of components based on the dicom metadata
1293
+ *
1294
+ * @param photometricInterpretation - string dicom tag
1295
+ * @returns number representing number of components
1296
+ */
1297
+ private _getNumCompsFromPhotometricInterpretation(
1298
+ photometricInterpretation: string
1299
+ ): number {
1300
+ // TODO: this function will need to have more logic later
1301
+ // see http://dicom.nema.org/medical/Dicom/current/output/chtml/part03/sect_C.7.6.3.html#sect_C.7.6.3.1.2
1302
+ let numberOfComponents = 1;
1303
+ if (
1304
+ photometricInterpretation === 'RGB' ||
1305
+ photometricInterpretation.indexOf('YBR') !== -1 ||
1306
+ photometricInterpretation === 'PALETTE COLOR'
1307
+ ) {
1308
+ numberOfComponents = 3;
1309
+ }
1310
+
1311
+ return numberOfComponents;
1312
+ }
1313
+
1314
+ /**
1315
+ * Calculates image metadata based on the image object. It calculates normal
1316
+ * axis for the images, and output image metadata
1317
+ *
1318
+ * @param image - stack image containing cornerstone image
1319
+ * @returns image metadata: bitsAllocated, number of components, origin,
1320
+ * direction, dimensions, spacing, number of voxels.
1321
+ */
1322
+ private _getImageDataMetadata(image: IImage): ImageDataMetaData {
1323
+ // TODO: Creating a single image should probably not require a metadata provider.
1324
+ // We should define the minimum we need to display an image and it should live on
1325
+ // the Image object itself. Additional stuff (e.g. pixel spacing, direction, origin, etc)
1326
+ // should be optional and used if provided through a metadata provider.
1327
+
1328
+ const { imagePlaneModule, imagePixelModule } = this.buildMetadata(image);
1329
+
1330
+ let rowCosines, columnCosines;
1331
+
1332
+ rowCosines = <Point3>imagePlaneModule.rowCosines;
1333
+ columnCosines = <Point3>imagePlaneModule.columnCosines;
1334
+
1335
+ // if null or undefined
1336
+ if (rowCosines == null || columnCosines == null) {
1337
+ rowCosines = <Point3>[1, 0, 0];
1338
+ columnCosines = <Point3>[0, 1, 0];
1339
+ }
1340
+
1341
+ const rowCosineVec = vec3.fromValues(
1342
+ rowCosines[0],
1343
+ rowCosines[1],
1344
+ rowCosines[2]
1345
+ );
1346
+ const colCosineVec = vec3.fromValues(
1347
+ columnCosines[0],
1348
+ columnCosines[1],
1349
+ columnCosines[2]
1350
+ );
1351
+ const scanAxisNormal = vec3.create();
1352
+ vec3.cross(scanAxisNormal, rowCosineVec, colCosineVec);
1353
+
1354
+ let origin = imagePlaneModule.imagePositionPatient;
1355
+ // if null or undefined
1356
+ if (origin == null) {
1357
+ origin = [0, 0, 0];
1358
+ }
1359
+
1360
+ const xSpacing =
1361
+ imagePlaneModule.columnPixelSpacing || image.columnPixelSpacing;
1362
+ const ySpacing = imagePlaneModule.rowPixelSpacing || image.rowPixelSpacing;
1363
+ const xVoxels = image.columns;
1364
+ const yVoxels = image.rows;
1365
+
1366
+ // Note: For rendering purposes, we use the EPSILON as the z spacing.
1367
+ // This is purely for internal implementation logic since we are still
1368
+ // technically rendering 3D objects with vtk.js, but the abstracted intention
1369
+ // of the stack viewport is to render 2D images
1370
+ const zSpacing = EPSILON;
1371
+ const zVoxels = 1;
1372
+
1373
+ const numComps =
1374
+ image.numComps ||
1375
+ this._getNumCompsFromPhotometricInterpretation(
1376
+ imagePixelModule.photometricInterpretation
1377
+ );
1378
+
1379
+ return {
1380
+ bitsAllocated: imagePixelModule.bitsAllocated,
1381
+ numComps,
1382
+ origin,
1383
+ direction: [...rowCosineVec, ...colCosineVec, ...scanAxisNormal] as Mat3,
1384
+ dimensions: [xVoxels, yVoxels, zVoxels],
1385
+ spacing: [xSpacing, ySpacing, zSpacing],
1386
+ numVoxels: xVoxels * yVoxels * zVoxels,
1387
+ imagePlaneModule,
1388
+ imagePixelModule,
1389
+ };
1390
+ }
1391
+
1392
+ /**
1393
+ * Converts the image direction to camera viewUp and viewplaneNormal
1394
+ *
1395
+ * @param imageDataDirection - vtkImageData direction
1396
+ * @returns viewplane normal and viewUp of the camera
1397
+ */
1398
+ private _getCameraOrientation(imageDataDirection: Mat3): {
1399
+ viewPlaneNormal: Point3;
1400
+ viewUp: Point3;
1401
+ } {
1402
+ const viewPlaneNormal = imageDataDirection.slice(6, 9).map((x) => -x);
1403
+
1404
+ const viewUp = imageDataDirection.slice(3, 6).map((x) => -x);
1405
+ return {
1406
+ viewPlaneNormal: [
1407
+ viewPlaneNormal[0],
1408
+ viewPlaneNormal[1],
1409
+ viewPlaneNormal[2],
1410
+ ],
1411
+ viewUp: [viewUp[0], viewUp[1], viewUp[2]],
1412
+ };
1413
+ }
1414
+
1415
+ /**
1416
+ * Creates vtkImagedata based on the image object, it creates
1417
+ * and empty scalar data for the image based on the metadata
1418
+ * tags (e.g., bitsAllocated)
1419
+ *
1420
+ * @param image - cornerstone Image object
1421
+ */
1422
+ private _createVTKImageData({
1423
+ origin,
1424
+ direction,
1425
+ dimensions,
1426
+ spacing,
1427
+ bitsAllocated,
1428
+ numComps,
1429
+ numVoxels,
1430
+ TypedArray,
1431
+ }): void {
1432
+ let pixelArray;
1433
+ switch (bitsAllocated) {
1434
+ case 8:
1435
+ pixelArray = new Uint8Array(numVoxels * numComps);
1436
+ break;
1437
+ case 16:
1438
+ if (this.use16BitTexture) {
1439
+ pixelArray = new TypedArray(numVoxels * numComps);
1440
+ } else {
1441
+ pixelArray = new Float32Array(numVoxels * numComps);
1442
+ }
1443
+
1444
+ break;
1445
+ case 24:
1446
+ pixelArray = new Uint8Array(numVoxels * 3 * numComps);
1447
+
1448
+ break;
1449
+ default:
1450
+ console.log('bit allocation not implemented');
1451
+ }
1452
+
1453
+ const scalarArray = vtkDataArray.newInstance({
1454
+ name: 'Pixels',
1455
+ numberOfComponents: numComps,
1456
+ values: pixelArray,
1457
+ });
1458
+
1459
+ this._imageData = vtkImageData.newInstance();
1460
+
1461
+ this._imageData.setDimensions(dimensions);
1462
+ this._imageData.setSpacing(spacing);
1463
+ this._imageData.setDirection(direction);
1464
+ this._imageData.setOrigin(origin);
1465
+ this._imageData.getPointData().setScalars(scalarArray);
1466
+ }
1467
+
1468
+ /**
1469
+ * Sets the imageIds to be visualized inside the stack viewport. It accepts
1470
+ * list of imageIds, the index of the first imageId to be viewed. It is a
1471
+ * asynchronous function that returns a promise resolving to imageId being
1472
+ * displayed in the stack viewport.
1473
+ *
1474
+ *
1475
+ * @param imageIds - list of strings, that represents list of image Ids
1476
+ * @param currentImageIdIndex - number representing the index of the initial image to be displayed
1477
+ */
1478
+ public async setStack(
1479
+ imageIds: Array<string>,
1480
+ currentImageIdIndex = 0
1481
+ ): Promise<string> {
1482
+ this.imageIds = imageIds;
1483
+ this.currentImageIdIndex = currentImageIdIndex;
1484
+ this.targetImageIdIndex = currentImageIdIndex;
1485
+
1486
+ // reset the stack
1487
+ this.stackInvalidated = true;
1488
+ this.flipVertical = false;
1489
+ this.flipHorizontal = false;
1490
+ this.voiRange = null;
1491
+ this.interpolationType = InterpolationType.LINEAR;
1492
+ this.invert = false;
1493
+
1494
+ this.fillWithBackgroundColor();
1495
+
1496
+ if (this.useCPURendering) {
1497
+ this._cpuFallbackEnabledElement.renderingTools = {};
1498
+ delete this._cpuFallbackEnabledElement.viewport.colormap;
1499
+ }
1500
+
1501
+ const imageId = await this._setImageIdIndex(currentImageIdIndex);
1502
+
1503
+ const eventDetail: StackViewportNewStackEventDetail = {
1504
+ imageIds,
1505
+ viewportId: this.id,
1506
+ element: this.element,
1507
+ currentImageIdIndex: currentImageIdIndex,
1508
+ };
1509
+
1510
+ triggerEvent(eventTarget, Events.STACK_VIEWPORT_NEW_STACK, eventDetail);
1511
+
1512
+ return imageId;
1513
+ }
1514
+
1515
+ /**
1516
+ * It checks if the new image object matches the dimensions, spacing,
1517
+ * and direction of the previously displayed image in the viewport or not.
1518
+ * It returns a boolean
1519
+ *
1520
+ * @param image - Cornerstone Image object
1521
+ * @param imageData - vtkImageData
1522
+ * @returns boolean
1523
+ */
1524
+ private _checkVTKImageDataMatchesCornerstoneImage(
1525
+ image: IImage,
1526
+ imageData: vtkImageDataType
1527
+ ): boolean {
1528
+ if (!imageData) {
1529
+ return false;
1530
+ }
1531
+ const [xSpacing, ySpacing] = imageData.getSpacing();
1532
+ const [xVoxels, yVoxels] = imageData.getDimensions();
1533
+ const imagePlaneModule = this._getImagePlaneModule(image.imageId);
1534
+ const direction = imageData.getDirection();
1535
+ const rowCosines = direction.slice(0, 3);
1536
+ const columnCosines = direction.slice(3, 6);
1537
+ const dataType = imageData.getPointData().getScalars().getDataType();
1538
+
1539
+ // using spacing, size, and direction only for now
1540
+ return (
1541
+ (xSpacing === image.rowPixelSpacing ||
1542
+ (image.rowPixelSpacing === null && xSpacing === 1.0)) &&
1543
+ (ySpacing === image.columnPixelSpacing ||
1544
+ (image.columnPixelSpacing === null && ySpacing === 1.0)) &&
1545
+ xVoxels === image.columns &&
1546
+ yVoxels === image.rows &&
1547
+ isEqual(imagePlaneModule.rowCosines, <Point3>rowCosines) &&
1548
+ isEqual(imagePlaneModule.columnCosines, <Point3>columnCosines) &&
1549
+ (!this.use16BitTexture ||
1550
+ dataType === image.getPixelData().constructor.name)
1551
+ );
1552
+ }
1553
+
1554
+ /**
1555
+ * It Updates the vtkImageData of the viewport with the new pixel data
1556
+ * from the provided image object.
1557
+ *
1558
+ * @param image - Cornerstone Image object
1559
+ */
1560
+ private _updateVTKImageDataFromCornerstoneImage(image: IImage): void {
1561
+ const imagePlaneModule = this._getImagePlaneModule(image.imageId);
1562
+ let origin = imagePlaneModule.imagePositionPatient;
1563
+
1564
+ if (origin == null) {
1565
+ origin = [0, 0, 0];
1566
+ }
1567
+
1568
+ this._imageData.setOrigin(origin);
1569
+
1570
+ // 1. Update the pixel data in the vtkImageData object with the pixelData
1571
+ // from the loaded Cornerstone image
1572
+ const pixelData = image.getPixelData();
1573
+ const scalars = this._imageData.getPointData().getScalars();
1574
+ const scalarData = scalars.getData() as
1575
+ | Uint8Array
1576
+ | Float32Array
1577
+ | Uint16Array
1578
+ | Int16Array;
1579
+
1580
+ if (image.rgba || isRgbaSourceRgbDest(pixelData, scalarData)) {
1581
+ if (!image.rgba) {
1582
+ console.warn('rgba not specified but data looks rgba ish', image);
1583
+ }
1584
+ // if image is already cached with rgba for any reason (cpu fallback),
1585
+ // we need to convert it to rgb for the pixel data set
1586
+ // RGB case
1587
+ const numPixels = pixelData.length / 4;
1588
+
1589
+ let rgbIndex = 0;
1590
+ let index = 0;
1591
+
1592
+ for (let i = 0; i < numPixels; i++) {
1593
+ scalarData[index++] = pixelData[rgbIndex++]; // red
1594
+ scalarData[index++] = pixelData[rgbIndex++]; // green
1595
+ scalarData[index++] = pixelData[rgbIndex++]; // blue
1596
+ rgbIndex++; // skip alpha
1597
+ }
1598
+ } else {
1599
+ scalarData.set(pixelData);
1600
+ }
1601
+
1602
+ // Trigger modified on the VTK Object so the texture is updated
1603
+ // TODO: evaluate directly changing things with texSubImage3D later
1604
+ this._imageData.modified();
1605
+ }
1606
+
1607
+ /**
1608
+ * It uses imageLoadPoolManager to add request for the imageId. It loadsAndCache
1609
+ * the image and triggers the STACK_NEW_IMAGE when the request successfully retrieves
1610
+ * the image. Next, the volume actor gets updated with the new new retrieved image.
1611
+ *
1612
+ * @param imageId - string representing the imageId
1613
+ * @param imageIdIndex - index of the imageId in the imageId list
1614
+ */
1615
+ private async _loadAndDisplayImage(
1616
+ imageId: string,
1617
+ imageIdIndex: number
1618
+ ): Promise<string> {
1619
+ await (this.useCPURendering
1620
+ ? this._loadAndDisplayImageCPU(imageId, imageIdIndex)
1621
+ : this._loadAndDisplayImageGPU(imageId, imageIdIndex));
1622
+
1623
+ return imageId;
1624
+ }
1625
+
1626
+ private _loadAndDisplayImageCPU(
1627
+ imageId: string,
1628
+ imageIdIndex: number
1629
+ ): Promise<string> {
1630
+ return new Promise((resolve, reject) => {
1631
+ // 1. Load the image using the Image Loader
1632
+ function successCallback(
1633
+ image: IImage,
1634
+ imageIdIndex: number,
1635
+ imageId: string
1636
+ ) {
1637
+ // Perform this check after the image has finished loading
1638
+ // in case the user has already scrolled away to another image.
1639
+ // In that case, do not render this image.
1640
+ if (this.currentImageIdIndex !== imageIdIndex) {
1641
+ return;
1642
+ }
1643
+
1644
+ image.isPreScaled = image.preScale?.scaled;
1645
+ this.csImage = image;
1646
+
1647
+ const eventDetail: EventTypes.StackNewImageEventDetail = {
1648
+ image,
1649
+ imageId,
1650
+ imageIdIndex,
1651
+ viewportId: this.id,
1652
+ renderingEngineId: this.renderingEngineId,
1653
+ };
1654
+
1655
+ triggerEvent(this.element, Events.STACK_NEW_IMAGE, eventDetail);
1656
+
1657
+ const metadata = this._getImageDataMetadata(image) as ImageDataMetaData;
1658
+
1659
+ const viewport = getDefaultViewport(
1660
+ this.canvas,
1661
+ image,
1662
+ this.modality,
1663
+ this._cpuFallbackEnabledElement.viewport.colormap
1664
+ );
1665
+
1666
+ this._cpuFallbackEnabledElement.image = image;
1667
+ this._cpuFallbackEnabledElement.metadata = {
1668
+ ...metadata,
1669
+ };
1670
+ this.cpuImagePixelData = image.getPixelData();
1671
+
1672
+ const viewportSettingToUse = Object.assign(
1673
+ {},
1674
+ viewport,
1675
+ this._cpuFallbackEnabledElement.viewport
1676
+ );
1677
+
1678
+ // Important: this.stackInvalidated is different than cpuRenderingInvalidated. The
1679
+ // former is being used to maintain the previous state of the viewport
1680
+ // in the same stack, the latter is used to trigger drawImageSync
1681
+ this._cpuFallbackEnabledElement.viewport = this.stackInvalidated
1682
+ ? viewport
1683
+ : viewportSettingToUse;
1684
+
1685
+ // used the previous state of the viewport, then stackInvalidated is set to false
1686
+ this.stackInvalidated = false;
1687
+
1688
+ // new viewport is set to the current viewport, then cpuRenderingInvalidated is set to true
1689
+ this.cpuRenderingInvalidated = true;
1690
+
1691
+ this._cpuFallbackEnabledElement.transform = calculateTransform(
1692
+ this._cpuFallbackEnabledElement
1693
+ );
1694
+
1695
+ // Todo: trigger an event to allow applications to hook into END of loading state
1696
+ // Currently we use loadHandlerManagers for this
1697
+
1698
+ // Trigger the image to be drawn on the next animation frame
1699
+ this.render();
1700
+
1701
+ // Update the viewport's currentImageIdIndex to reflect the newly
1702
+ // rendered image
1703
+ this.currentImageIdIndex = imageIdIndex;
1704
+ resolve(imageId);
1705
+ }
1706
+
1707
+ function errorCallback(
1708
+ error: Error,
1709
+ imageIdIndex: number,
1710
+ imageId: string
1711
+ ) {
1712
+ const eventDetail = {
1713
+ error,
1714
+ imageIdIndex,
1715
+ imageId,
1716
+ };
1717
+
1718
+ if (!this.suppressEvents) {
1719
+ triggerEvent(eventTarget, Events.IMAGE_LOAD_ERROR, eventDetail);
1720
+ }
1721
+
1722
+ reject(error);
1723
+ }
1724
+
1725
+ function sendRequest(imageId, imageIdIndex, options) {
1726
+ return loadAndCacheImage(imageId, options).then(
1727
+ (image) => {
1728
+ successCallback.call(this, image, imageIdIndex, imageId);
1729
+ },
1730
+ (error) => {
1731
+ errorCallback.call(this, error, imageIdIndex, imageId);
1732
+ }
1733
+ );
1734
+ }
1735
+
1736
+ const priority = -5;
1737
+ const requestType = RequestType.Interaction;
1738
+ const additionalDetails = { imageId };
1739
+ const options = {
1740
+ targetBuffer: {
1741
+ type: this.use16BitTexture ? undefined : 'Float32Array',
1742
+ },
1743
+ preScale: {
1744
+ enabled: true,
1745
+ },
1746
+ useRGBA: true,
1747
+ };
1748
+
1749
+ imageLoadPoolManager.addRequest(
1750
+ sendRequest.bind(this, imageId, imageIdIndex, options),
1751
+ requestType,
1752
+ additionalDetails,
1753
+ priority
1754
+ );
1755
+ });
1756
+ }
1757
+
1758
+ private _loadAndDisplayImageGPU(imageId: string, imageIdIndex: number) {
1759
+ return new Promise((resolve, reject) => {
1760
+ // 1. Load the image using the Image Loader
1761
+ function successCallback(image, imageIdIndex, imageId) {
1762
+ // Todo: trigger an event to allow applications to hook into END of loading state
1763
+ // Currently we use loadHandlerManagers for this
1764
+ // Perform this check after the image has finished loading
1765
+ // in case the user has already scrolled away to another image.
1766
+ // In that case, do not render this image.
1767
+ if (this.currentImageIdIndex !== imageIdIndex) {
1768
+ return;
1769
+ }
1770
+
1771
+ // cornerstone image
1772
+ image.isPreScaled = image.preScale?.scaled;
1773
+ this.csImage = image;
1774
+
1775
+ const eventDetail: EventTypes.StackNewImageEventDetail = {
1776
+ image,
1777
+ imageId,
1778
+ imageIdIndex,
1779
+ viewportId: this.id,
1780
+ renderingEngineId: this.renderingEngineId,
1781
+ };
1782
+
1783
+ triggerEvent(this.element, Events.STACK_NEW_IMAGE, eventDetail);
1784
+ this._updateActorToDisplayImageId(image);
1785
+
1786
+ // Trigger the image to be drawn on the next animation frame
1787
+ this.render();
1788
+
1789
+ // Update the viewport's currentImageIdIndex to reflect the newly
1790
+ // rendered image
1791
+ this.currentImageIdIndex = imageIdIndex;
1792
+ resolve(imageId);
1793
+ }
1794
+
1795
+ function errorCallback(error, imageIdIndex, imageId) {
1796
+ const eventDetail = {
1797
+ error,
1798
+ imageIdIndex,
1799
+ imageId,
1800
+ };
1801
+
1802
+ triggerEvent(eventTarget, Events.IMAGE_LOAD_ERROR, eventDetail);
1803
+ reject(error);
1804
+ }
1805
+
1806
+ function sendRequest(imageId, imageIdIndex, options) {
1807
+ return loadAndCacheImage(imageId, options).then(
1808
+ (image) => {
1809
+ successCallback.call(this, image, imageIdIndex, imageId);
1810
+ },
1811
+ (error) => {
1812
+ errorCallback.call(this, error, imageIdIndex, imageId);
1813
+ }
1814
+ );
1815
+ }
1816
+
1817
+ /**
1818
+ * CSWIL will automatically choose the array type when no targetBuffer
1819
+ * is provided. When CSWIL is initialized, the use16bit should match
1820
+ * the settings of cornerstone3D (either preferSizeOverAccuracy or norm16
1821
+ * textures need to be enabled)
1822
+ */
1823
+ const priority = -5;
1824
+ const requestType = RequestType.Interaction;
1825
+ const additionalDetails = { imageId };
1826
+ const options = {
1827
+ targetBuffer: {
1828
+ type: this.use16BitTexture ? undefined : 'Float32Array',
1829
+ },
1830
+ preScale: {
1831
+ enabled: true,
1832
+ },
1833
+ useRGBA: false,
1834
+ };
1835
+
1836
+ const eventDetail: EventTypes.PreStackNewImageEventDetail = {
1837
+ imageId,
1838
+ imageIdIndex,
1839
+ viewportId: this.id,
1840
+ renderingEngineId: this.renderingEngineId,
1841
+ };
1842
+ triggerEvent(this.element, Events.PRE_STACK_NEW_IMAGE, eventDetail);
1843
+
1844
+ imageLoadPoolManager.addRequest(
1845
+ sendRequest.bind(this, imageId, imageIdIndex, options),
1846
+ requestType,
1847
+ additionalDetails,
1848
+ priority
1849
+ );
1850
+ });
1851
+ }
1852
+
1853
+ /**
1854
+ * It updates the volume actor with the retrieved cornerstone image.
1855
+ * It first checks if the new image has the same dimensions, spacings, and
1856
+ * dimensions of the previous one: 1) If yes, it updates the pixel data 2) if not,
1857
+ * it creates a whole new volume actor for the image.
1858
+ * Note: Camera gets reset for both situations. Therefore, each image renders at
1859
+ * its exact 3D location in the space, and both image and camera moves while scrolling.
1860
+ *
1861
+ * @param image - Cornerstone image
1862
+ * @returns
1863
+ */
1864
+ private _updateActorToDisplayImageId(image) {
1865
+ // This function should do the following:
1866
+ // - Get the existing actor's vtkImageData that is being used to render the current image and check if we can reuse the vtkImageData that is in place (i.e. do the image dimensions and data type match?)
1867
+ // - If we can reuse it, replace the scalar data under the hood
1868
+ // - If we cannot reuse it, create a new actor, remove the old one, and reset the camera
1869
+
1870
+ // 2. Check if we can reuse the existing vtkImageData object, if one is present.
1871
+ const sameImageData = this._checkVTKImageDataMatchesCornerstoneImage(
1872
+ image,
1873
+ this._imageData
1874
+ );
1875
+
1876
+ const activeCamera = this.getRenderer().getActiveCamera();
1877
+
1878
+ // Cache camera props so we can trigger one camera changed event after
1879
+ // The full transition.
1880
+ const previousCameraProps = _cloneDeep(this.getCamera());
1881
+ if (sameImageData && !this.stackInvalidated) {
1882
+ // 3a. If we can reuse it, replace the scalar data under the hood
1883
+ this._updateVTKImageDataFromCornerstoneImage(image);
1884
+
1885
+ // Since the 3D location of the imageData is changing as we scroll, we need
1886
+ // to modify the camera position to render this properly. However, resetting
1887
+ // causes problem related to zoom and pan tools: upon rendering of a new slice
1888
+ // the pan and zoom will get reset. To solve this, 1) we store the camera
1889
+ // properties related to pan and zoom 2) reset the camera to correctly place
1890
+ // it in the space 3) restore the pan, zoom props.
1891
+ const cameraProps = this.getCamera();
1892
+
1893
+ const panCache = vec3.subtract(
1894
+ vec3.create(),
1895
+ this.cameraFocalPointOnRender,
1896
+ cameraProps.focalPoint
1897
+ );
1898
+
1899
+ // Reset the camera to point to the new slice location, reset camera doesn't
1900
+ // modify the direction of projection and viewUp
1901
+ this.resetCameraNoEvent();
1902
+
1903
+ // set the flip and view up back to the previous value since the restore camera props
1904
+ // rely on the correct flip value
1905
+ this.setCameraNoEvent({
1906
+ flipHorizontal: previousCameraProps.flipHorizontal,
1907
+ flipVertical: previousCameraProps.flipVertical,
1908
+ viewUp: previousCameraProps.viewUp,
1909
+ });
1910
+
1911
+ const { focalPoint } = this.getCamera();
1912
+ this.cameraFocalPointOnRender = focalPoint;
1913
+
1914
+ // This is necessary to initialize the clipping range and it is not related
1915
+ // to our custom slabThickness.
1916
+ // @ts-ignore: vtkjs incorrect typing
1917
+ activeCamera.setFreezeFocalPoint(true);
1918
+
1919
+ // We shouldn't restore the focalPoint, position and parallelScale after reset
1920
+ // if it is the first render or we have completely re-created the vtkImageData
1921
+ this._restoreCameraProps(
1922
+ cameraProps,
1923
+ previousCameraProps,
1924
+ panCache as Point3
1925
+ );
1926
+
1927
+ this._setPropertiesFromCache();
1928
+
1929
+ return;
1930
+ }
1931
+
1932
+ const {
1933
+ origin,
1934
+ direction,
1935
+ dimensions,
1936
+ spacing,
1937
+ bitsAllocated,
1938
+ numComps,
1939
+ numVoxels,
1940
+ imagePixelModule,
1941
+ } = this._getImageDataMetadata(image);
1942
+
1943
+ // 3b. If we cannot reuse the vtkImageData object (either the first render
1944
+ // or the size has changed), create a new one
1945
+ this._createVTKImageData({
1946
+ origin,
1947
+ direction,
1948
+ dimensions,
1949
+ spacing,
1950
+ bitsAllocated,
1951
+ numComps,
1952
+ numVoxels,
1953
+ TypedArray: image.getPixelData().constructor,
1954
+ });
1955
+
1956
+ // Set the scalar data of the vtkImageData object from the Cornerstone
1957
+ // Image's pixel data
1958
+ this._updateVTKImageDataFromCornerstoneImage(image);
1959
+
1960
+ // Create a VTK Image Slice actor to display the vtkImageData object
1961
+ const actor = this.createActorMapper(this._imageData);
1962
+ const actors = [];
1963
+ actors.push({ uid: this.id, actor });
1964
+ this.setActors(actors);
1965
+ // Adjusting the camera based on slice axis. this is required if stack
1966
+ // contains various image orientations (axial ct, sagittal xray)
1967
+ const { viewPlaneNormal, viewUp } = this._getCameraOrientation(direction);
1968
+
1969
+ this.setCameraNoEvent({ viewUp, viewPlaneNormal });
1970
+
1971
+ // Setting this makes the following comment about resetCameraNoEvent not modifying viewUp true.
1972
+ this.initialViewUp = viewUp;
1973
+
1974
+ // Reset the camera to point to the new slice location, reset camera doesn't
1975
+ // modify the direction of projection and viewUp
1976
+ this.resetCameraNoEvent();
1977
+
1978
+ this.triggerCameraEvent(this.getCamera(), previousCameraProps);
1979
+
1980
+ // This is necessary to initialize the clipping range and it is not related
1981
+ // to our custom slabThickness.
1982
+ // @ts-ignore: vtkjs incorrect typing
1983
+ activeCamera.setFreezeFocalPoint(true);
1984
+
1985
+ this.setVOI(this._getInitialVOIRange(image));
1986
+ this.setInvertColor(
1987
+ imagePixelModule.photometricInterpretation === 'MONOCHROME1'
1988
+ );
1989
+
1990
+ // Saving position of camera on render, to cache the panning
1991
+ this.cameraFocalPointOnRender = this.getCamera().focalPoint;
1992
+ this.stackInvalidated = false;
1993
+
1994
+ if (this._publishCalibratedEvent) {
1995
+ this.triggerCalibrationEvent();
1996
+ }
1997
+ }
1998
+
1999
+ private _getInitialVOIRange(image: IImage) {
2000
+ if (this.voiRange && this.voiUpdatedWithSetProperties) {
2001
+ return this.voiRange;
2002
+ }
2003
+ const { windowCenter, windowWidth } = image;
2004
+
2005
+ let voiRange = this._getVOIRangeFromWindowLevel(windowWidth, windowCenter);
2006
+
2007
+ // Get the range for the PT since if it is prescaled
2008
+ // we set a default range of 0-5
2009
+ voiRange = this._getPTPreScaledRange() || voiRange;
2010
+
2011
+ return voiRange;
2012
+ }
2013
+
2014
+ private _getPTPreScaledRange() {
2015
+ if (!this._isCurrentImagePTPrescaled()) {
2016
+ return undefined;
2017
+ }
2018
+
2019
+ return this._getDefaultPTPrescaledVOIRange();
2020
+ }
2021
+
2022
+ private _isCurrentImagePTPrescaled() {
2023
+ if (this.modality !== 'PT' || !this.csImage.isPreScaled) {
2024
+ return false;
2025
+ }
2026
+
2027
+ return true;
2028
+ }
2029
+
2030
+ private _getDefaultPTPrescaledVOIRange() {
2031
+ return { lower: 0, upper: 5 };
2032
+ }
2033
+
2034
+ private _getVOIRangeFromWindowLevel(
2035
+ windowWidth: number | number[],
2036
+ windowCenter: number | number[]
2037
+ ): { lower: number; upper: number } | undefined {
2038
+ return typeof windowCenter === 'number' && typeof windowWidth === 'number'
2039
+ ? windowLevelUtil.toLowHighRange(windowWidth, windowCenter)
2040
+ : undefined;
2041
+ }
2042
+
2043
+ /**
2044
+ * Loads the image based on the provided imageIdIndex
2045
+ * @param imageIdIndex - number represents imageId index
2046
+ */
2047
+ private async _setImageIdIndex(imageIdIndex: number): Promise<string> {
2048
+ if (imageIdIndex >= this.imageIds.length) {
2049
+ throw new Error(
2050
+ `ImageIdIndex provided ${imageIdIndex} is invalid, the stack only has ${this.imageIds.length} elements`
2051
+ );
2052
+ }
2053
+
2054
+ // Update the state of the viewport to the new imageIdIndex;
2055
+ this.currentImageIdIndex = imageIdIndex;
2056
+ this.hasPixelSpacing = true;
2057
+
2058
+ // Todo: trigger an event to allow applications to hook into START of loading state
2059
+ // Currently we use loadHandlerManagers for this
2060
+ const imageId = await this._loadAndDisplayImage(
2061
+ this.imageIds[imageIdIndex],
2062
+ imageIdIndex
2063
+ );
2064
+
2065
+ return imageId;
2066
+ }
2067
+
2068
+ private resetCameraCPU(resetPan, resetZoom) {
2069
+ const { image } = this._cpuFallbackEnabledElement;
2070
+
2071
+ if (!image) {
2072
+ return;
2073
+ }
2074
+
2075
+ resetCamera(this._cpuFallbackEnabledElement, resetPan, resetZoom);
2076
+
2077
+ const { scale } = this._cpuFallbackEnabledElement.viewport;
2078
+
2079
+ // canvas center is the focal point
2080
+ const { clientWidth, clientHeight } = this.element;
2081
+ const center: Point2 = [clientWidth / 2, clientHeight / 2];
2082
+
2083
+ const centerWorld = this.canvasToWorldCPU(center);
2084
+
2085
+ this.setCameraCPU({
2086
+ focalPoint: centerWorld,
2087
+ scale,
2088
+ });
2089
+ }
2090
+
2091
+ private resetCameraGPU(resetPan, resetZoom): boolean {
2092
+ // Todo: we need to make the rotation a camera properties so that
2093
+ // we can reset it there, right now it is not possible to reset the rotation
2094
+ // without this
2095
+
2096
+ // We do not know the ordering of various flips and rotations that have been applied,
2097
+ // so the rotation and flip must be reset together.
2098
+ this.setCamera({
2099
+ flipHorizontal: false,
2100
+ flipVertical: false,
2101
+ viewUp: this.initialViewUp,
2102
+ });
2103
+
2104
+ // For stack Viewport we since we have only one slice
2105
+ // it should be enough to reset the camera to the center of the image
2106
+ const resetToCenter = true;
2107
+ return super.resetCamera(resetPan, resetZoom, resetToCenter);
2108
+ }
2109
+
2110
+ /**
2111
+ * It scrolls the stack of imageIds by the delta amount provided. If the debounce
2112
+ * flag is set, it will only scroll the stack if the delta is greater than the
2113
+ * debounceThreshold which is 40 milliseconds by default.
2114
+ * @param delta - number of indices to scroll, it can be positive or negative
2115
+ * @param debounce - whether to debounce the scroll event
2116
+ * @param loop - whether to loop the stack
2117
+ */
2118
+ public scroll(delta: number, debounce = true, loop = false): void {
2119
+ const imageIds = this.imageIds;
2120
+
2121
+ const currentTargetImageIdIndex = this.targetImageIdIndex;
2122
+ const numberOfFrames = imageIds.length;
2123
+
2124
+ let newTargetImageIdIndex = currentTargetImageIdIndex + delta;
2125
+ newTargetImageIdIndex = Math.max(0, newTargetImageIdIndex);
2126
+
2127
+ if (loop) {
2128
+ newTargetImageIdIndex = newTargetImageIdIndex % numberOfFrames;
2129
+ } else {
2130
+ newTargetImageIdIndex = Math.min(
2131
+ numberOfFrames - 1,
2132
+ newTargetImageIdIndex
2133
+ );
2134
+ }
2135
+
2136
+ this.targetImageIdIndex = newTargetImageIdIndex;
2137
+
2138
+ const targetImageId = imageIds[newTargetImageIdIndex];
2139
+
2140
+ const imageAlreadyLoaded = cache.isImageIdCached(targetImageId);
2141
+
2142
+ // If image is already cached we want to scroll right away; however, if it is
2143
+ // not cached, we can debounce the scroll event to avoid firing multiple scroll
2144
+ // events for the images that might happen to be passing by (as a result of infinite
2145
+ // scrolling).
2146
+ if (imageAlreadyLoaded || !debounce) {
2147
+ this.setImageIdIndex(newTargetImageIdIndex);
2148
+ } else {
2149
+ clearTimeout(this.debouncedTimeout);
2150
+ this.debouncedTimeout = window.setTimeout(() => {
2151
+ this.setImageIdIndex(newTargetImageIdIndex);
2152
+ }, 40);
2153
+ }
2154
+
2155
+ const eventData: StackViewportScrollEventDetail = {
2156
+ newImageIdIndex: newTargetImageIdIndex,
2157
+ imageId: targetImageId,
2158
+ direction: delta,
2159
+ };
2160
+
2161
+ if (newTargetImageIdIndex !== currentTargetImageIdIndex) {
2162
+ triggerEvent(this.element, Events.STACK_VIEWPORT_SCROLL, eventData);
2163
+ }
2164
+ }
2165
+
2166
+ /**
2167
+ * Loads the image based on the provided imageIdIndex. It is an Async function which
2168
+ * returns a promise that resolves to the imageId.
2169
+ *
2170
+ * @param imageIdIndex - number represents imageId index in the list of
2171
+ * provided imageIds in setStack
2172
+ */
2173
+ public async setImageIdIndex(imageIdIndex: number): Promise<string> {
2174
+ // If we are already on this imageId index, stop here
2175
+ if (this.currentImageIdIndex === imageIdIndex) {
2176
+ return this.getCurrentImageId();
2177
+ }
2178
+
2179
+ // Otherwise, get the imageId and attempt to display it
2180
+ const imageId = this._setImageIdIndex(imageIdIndex);
2181
+
2182
+ return imageId;
2183
+ }
2184
+
2185
+ /**
2186
+ * Calibrates the image with new metadata that has been added for imageId. To calibrate
2187
+ * a viewport, you should add your calibration data manually to
2188
+ * calibratedPixelSpacingMetadataProvider and call viewport.calibrateSpacing
2189
+ * for it get applied.
2190
+ *
2191
+ * @param imageId - imageId to be calibrated
2192
+ */
2193
+ public calibrateSpacing(imageId: string): void {
2194
+ const imageIdIndex = this.getImageIds().indexOf(imageId);
2195
+ this.stackInvalidated = true;
2196
+ this._loadAndDisplayImage(imageId, imageIdIndex);
2197
+ }
2198
+
2199
+ /**
2200
+ * Restores the camera props such zooming and panning after an image is
2201
+ * changed, if needed (after scroll)
2202
+ *
2203
+ * @param parallelScale - camera parallel scale
2204
+ */
2205
+ private _restoreCameraProps(
2206
+ { parallelScale: prevScale }: ICamera,
2207
+ previousCamera: ICamera,
2208
+ panCache: Point3
2209
+ ): void {
2210
+ const renderer = this.getRenderer();
2211
+
2212
+ // get the focalPoint and position after the reset
2213
+ const { position, focalPoint } = this.getCamera();
2214
+
2215
+ const newPosition = vec3.subtract(vec3.create(), position, panCache);
2216
+ const newFocal = vec3.subtract(vec3.create(), focalPoint, panCache);
2217
+
2218
+ // Restoring previous state x,y and scale, keeping the new z
2219
+ // we need to break the flip operations since they also work on the
2220
+ // camera position and focal point
2221
+ this.setCameraNoEvent({
2222
+ parallelScale: prevScale,
2223
+ position: newPosition as Point3,
2224
+ focalPoint: newFocal as Point3,
2225
+ });
2226
+
2227
+ const camera = this.getCamera();
2228
+
2229
+ this.triggerCameraEvent(camera, previousCamera);
2230
+
2231
+ // Invoking render
2232
+ const RESET_CAMERA_EVENT = {
2233
+ type: 'ResetCameraEvent',
2234
+ renderer,
2235
+ };
2236
+
2237
+ renderer.invokeEvent(RESET_CAMERA_EVENT);
2238
+ }
2239
+
2240
+ private triggerCameraEvent(camera: ICamera, previousCamera: ICamera) {
2241
+ // Finally emit event for the full camera change cause during load image.
2242
+ const eventDetail: EventTypes.CameraModifiedEventDetail = {
2243
+ previousCamera,
2244
+ camera,
2245
+ element: this.element,
2246
+ viewportId: this.id,
2247
+ renderingEngineId: this.renderingEngineId,
2248
+ };
2249
+
2250
+ if (!this.suppressEvents) {
2251
+ // For crosshairs to adapt to new viewport size
2252
+ triggerEvent(this.element, Events.CAMERA_MODIFIED, eventDetail);
2253
+ }
2254
+ }
2255
+
2256
+ private triggerCalibrationEvent() {
2257
+ // Update the indexToWorld and WorldToIndex for viewport
2258
+ const { imageData } = this.getImageData();
2259
+ // Finally emit event for the full camera change cause during load image.
2260
+ const eventDetail: EventTypes.ImageSpacingCalibratedEventDetail = {
2261
+ element: this.element,
2262
+ viewportId: this.id,
2263
+ renderingEngineId: this.renderingEngineId,
2264
+ imageId: this.getCurrentImageId(),
2265
+ // Todo: why do we need to pass imageData? isn't' indexToWorld and worldToIndex enough?
2266
+ imageData: imageData as vtkImageData,
2267
+ worldToIndex: imageData.getWorldToIndex() as mat4,
2268
+ ...this._calibrationEvent,
2269
+ };
2270
+
2271
+ if (!this.suppressEvents) {
2272
+ // Let the tools know the image spacing has been calibrated
2273
+ triggerEvent(this.element, Events.IMAGE_SPACING_CALIBRATED, eventDetail);
2274
+ }
2275
+
2276
+ this._publishCalibratedEvent = false;
2277
+ }
2278
+
2279
+ private canvasToWorldCPU = (canvasPos: Point2): Point3 => {
2280
+ if (!this._cpuFallbackEnabledElement.image) {
2281
+ return;
2282
+ }
2283
+ // compute the pixel coordinate in the image
2284
+ const [px, py] = canvasToPixel(this._cpuFallbackEnabledElement, canvasPos);
2285
+
2286
+ // convert pixel coordinate to world coordinate
2287
+ const { origin, spacing, direction } = this.getImageData();
2288
+
2289
+ const worldPos = vec3.fromValues(0, 0, 0);
2290
+
2291
+ // Calculate size of spacing vector in normal direction
2292
+ const iVector = direction.slice(0, 3) as Point3;
2293
+ const jVector = direction.slice(3, 6) as Point3;
2294
+
2295
+ // Calculate the world coordinate of the pixel
2296
+ vec3.scaleAndAdd(worldPos, origin, iVector, px * spacing[0]);
2297
+ vec3.scaleAndAdd(worldPos, worldPos, jVector, py * spacing[1]);
2298
+
2299
+ return [worldPos[0], worldPos[1], worldPos[2]] as Point3;
2300
+ };
2301
+
2302
+ private worldToCanvasCPU = (worldPos: Point3): Point2 => {
2303
+ // world to pixel
2304
+ const { spacing, direction, origin } = this.getImageData();
2305
+
2306
+ const iVector = direction.slice(0, 3) as Point3;
2307
+ const jVector = direction.slice(3, 6) as Point3;
2308
+
2309
+ const diff = vec3.subtract(vec3.create(), worldPos, origin);
2310
+
2311
+ const worldPoint: Point2 = [
2312
+ vec3.dot(diff, iVector) / spacing[0],
2313
+ vec3.dot(diff, jVector) / spacing[1],
2314
+ ];
2315
+
2316
+ // pixel to canvas
2317
+ const canvasPoint = pixelToCanvas(
2318
+ this._cpuFallbackEnabledElement,
2319
+ worldPoint
2320
+ );
2321
+ return canvasPoint;
2322
+ };
2323
+
2324
+ private canvasToWorldGPU = (canvasPos: Point2): Point3 => {
2325
+ const renderer = this.getRenderer();
2326
+
2327
+ // Temporary setting the clipping range to the distance and distance + 0.1
2328
+ // in order to calculate the transformations correctly.
2329
+ // This is similar to the vtkSlabCamera isPerformingCoordinateTransformations
2330
+ // You can read more about it here there.
2331
+ const vtkCamera = this.getVtkActiveCamera();
2332
+ const crange = vtkCamera.getClippingRange();
2333
+ const distance = vtkCamera.getDistance();
2334
+
2335
+ vtkCamera.setClippingRange(distance, distance + 0.1);
2336
+
2337
+ const offscreenMultiRenderWindow =
2338
+ this.getRenderingEngine().offscreenMultiRenderWindow;
2339
+ const openGLRenderWindow =
2340
+ offscreenMultiRenderWindow.getOpenGLRenderWindow();
2341
+ const size = openGLRenderWindow.getSize();
2342
+
2343
+ const devicePixelRatio = window.devicePixelRatio || 1;
2344
+ const canvasPosWithDPR = [
2345
+ canvasPos[0] * devicePixelRatio,
2346
+ canvasPos[1] * devicePixelRatio,
2347
+ ];
2348
+ const displayCoord = [
2349
+ canvasPosWithDPR[0] + this.sx,
2350
+ canvasPosWithDPR[1] + this.sy,
2351
+ ];
2352
+
2353
+ // The y axis display coordinates are inverted with respect to canvas coords
2354
+ displayCoord[1] = size[1] - displayCoord[1];
2355
+
2356
+ const worldCoord = openGLRenderWindow.displayToWorld(
2357
+ displayCoord[0],
2358
+ displayCoord[1],
2359
+ 0,
2360
+ renderer
2361
+ );
2362
+
2363
+ // set clipping range back to original to be able
2364
+ vtkCamera.setClippingRange(crange[0], crange[1]);
2365
+
2366
+ return [worldCoord[0], worldCoord[1], worldCoord[2]];
2367
+ };
2368
+
2369
+ private worldToCanvasGPU = (worldPos: Point3): Point2 => {
2370
+ const renderer = this.getRenderer();
2371
+
2372
+ // Temporary setting the clipping range to the distance and distance + 0.1
2373
+ // in order to calculate the transformations correctly.
2374
+ // This is similar to the vtkSlabCamera isPerformingCoordinateTransformations
2375
+ // You can read more about it here there.
2376
+ const vtkCamera = this.getVtkActiveCamera();
2377
+ const crange = vtkCamera.getClippingRange();
2378
+ const distance = vtkCamera.getDistance();
2379
+
2380
+ vtkCamera.setClippingRange(distance, distance + 0.1);
2381
+
2382
+ const offscreenMultiRenderWindow =
2383
+ this.getRenderingEngine().offscreenMultiRenderWindow;
2384
+ const openGLRenderWindow =
2385
+ offscreenMultiRenderWindow.getOpenGLRenderWindow();
2386
+ const size = openGLRenderWindow.getSize();
2387
+ const displayCoord = openGLRenderWindow.worldToDisplay(
2388
+ ...worldPos,
2389
+ renderer
2390
+ );
2391
+
2392
+ // The y axis display coordinates are inverted with respect to canvas coords
2393
+ displayCoord[1] = size[1] - displayCoord[1];
2394
+
2395
+ const canvasCoord = <Point2>[
2396
+ displayCoord[0] - this.sx,
2397
+ displayCoord[1] - this.sy,
2398
+ ];
2399
+
2400
+ // set clipping range back to original to be able
2401
+ vtkCamera.setClippingRange(crange[0], crange[1]);
2402
+
2403
+ const devicePixelRatio = window.devicePixelRatio || 1;
2404
+ const canvasCoordWithDPR = <Point2>[
2405
+ canvasCoord[0] / devicePixelRatio,
2406
+ canvasCoord[1] / devicePixelRatio,
2407
+ ];
2408
+
2409
+ return canvasCoordWithDPR;
2410
+ };
2411
+
2412
+ private _getVOIRangeForCurrentImage() {
2413
+ const { windowCenter, windowWidth } = this.csImage;
2414
+
2415
+ return this._getVOIRangeFromWindowLevel(windowWidth, windowCenter);
2416
+ }
2417
+
2418
+ private _getValidVOILUTFunction(voiLUTFunction: any) {
2419
+ if (Object.values(VOILUTFunctionType).indexOf(voiLUTFunction) === -1) {
2420
+ voiLUTFunction = VOILUTFunctionType.LINEAR;
2421
+ }
2422
+ return voiLUTFunction;
2423
+ }
2424
+
2425
+ /**
2426
+ * Returns the index of the imageId being renderer
2427
+ *
2428
+ * @returns currently shown imageId index
2429
+ */
2430
+ public getCurrentImageIdIndex = (): number => {
2431
+ return this.currentImageIdIndex;
2432
+ };
2433
+
2434
+ /**
2435
+ *
2436
+ * Returns the imageIdIndex that is targeted to be loaded, in case of debounced
2437
+ * loading (with scroll), the targetImageIdIndex is the latest imageId
2438
+ * index that is requested to be loaded but debounced.
2439
+ */
2440
+ public getTargetImageIdIndex = (): number => {
2441
+ return this.targetImageIdIndex;
2442
+ };
2443
+
2444
+ /**
2445
+ * Returns the list of image Ids for the current viewport
2446
+ * @returns list of strings for image Ids
2447
+ */
2448
+ public getImageIds = (): Array<string> => {
2449
+ return this.imageIds;
2450
+ };
2451
+
2452
+ /**
2453
+ * Returns the currently rendered imageId
2454
+ * @returns string for imageId
2455
+ */
2456
+ public getCurrentImageId = (): string => {
2457
+ return this.imageIds[this.currentImageIdIndex];
2458
+ };
2459
+
2460
+ /**
2461
+ * Returns true if the viewport contains the given imageId
2462
+ * @param imageId - imageId
2463
+ * @returns boolean if imageId is in viewport
2464
+ */
2465
+ public hasImageId = (imageId: string): boolean => {
2466
+ return this.imageIds.includes(imageId);
2467
+ };
2468
+
2469
+ /**
2470
+ * Returns true if the viewport contains the given imageURI (no data loader scheme)
2471
+ * @param imageURI - imageURI
2472
+ * @returns boolean if imageURI is in viewport
2473
+ */
2474
+ public hasImageURI = (imageURI: string): boolean => {
2475
+ const imageIds = this.imageIds;
2476
+ for (let i = 0; i < imageIds.length; i++) {
2477
+ if (imageIdToURI(imageIds[i]) === imageURI) return true;
2478
+ }
2479
+
2480
+ return false;
2481
+ };
2482
+
2483
+ private getCPUFallbackError(method: string): Error {
2484
+ return new Error(
2485
+ `method ${method} cannot be used during CPU Fallback mode`
2486
+ );
2487
+ }
2488
+
2489
+ private fillWithBackgroundColor() {
2490
+ const renderingEngine = this.getRenderingEngine();
2491
+
2492
+ if (renderingEngine) {
2493
+ renderingEngine.fillCanvasWithBackgroundColor(
2494
+ this.canvas,
2495
+ this.options.background
2496
+ );
2497
+ }
2498
+ }
2499
+
2500
+ public customRenderViewportToCanvas = () => {
2501
+ if (!this.useCPURendering) {
2502
+ throw new Error(
2503
+ 'Custom cpu rendering pipeline should only be hit in CPU rendering mode'
2504
+ );
2505
+ }
2506
+
2507
+ if (this._cpuFallbackEnabledElement.image) {
2508
+ drawImageSync(
2509
+ this._cpuFallbackEnabledElement,
2510
+ this.cpuRenderingInvalidated
2511
+ );
2512
+ // reset flags
2513
+ this.cpuRenderingInvalidated = false;
2514
+ } else {
2515
+ this.fillWithBackgroundColor();
2516
+ }
2517
+
2518
+ return {
2519
+ canvas: this.canvas,
2520
+ element: this.element,
2521
+ viewportId: this.id,
2522
+ renderingEngineId: this.renderingEngineId,
2523
+ };
2524
+ };
2525
+
2526
+ private unsetColormapCPU() {
2527
+ delete this._cpuFallbackEnabledElement.viewport.colormap;
2528
+ this._cpuFallbackEnabledElement.renderingTools = {};
2529
+
2530
+ this.cpuRenderingInvalidated = true;
2531
+
2532
+ this.fillWithBackgroundColor();
2533
+
2534
+ this.render();
2535
+ }
2536
+
2537
+ private setColormapCPU(colormapData: CPUFallbackColormapData) {
2538
+ const colormap = getColormap(colormapData.name, colormapData);
2539
+
2540
+ this._cpuFallbackEnabledElement.viewport.colormap = colormap;
2541
+ this._cpuFallbackEnabledElement.renderingTools = {};
2542
+
2543
+ this.fillWithBackgroundColor();
2544
+ this.cpuRenderingInvalidated = true;
2545
+
2546
+ this.render();
2547
+ }
2548
+
2549
+ private setColormapGPU(colormap: CPUFallbackColormapData) {
2550
+ // TODO -> vtk has full colormaps which are piecewise and frankly better?
2551
+ // Do we really want a pre defined 256 color map just for the sake of harmonization?
2552
+ throw new Error('setColorMapGPU not implemented.');
2553
+ }
2554
+
2555
+ private unsetColormapGPU() {
2556
+ // TODO -> vtk has full colormaps which are piecewise and frankly better?
2557
+ // Do we really want a pre defined 256 color map just for the sake of harmonization?
2558
+ throw new Error('unsetColormapGPU not implemented.');
2559
+ }
2560
+
2561
+ // create default values for imagePlaneModule if values are undefined
2562
+ private _getImagePlaneModule(imageId: string): ImagePlaneModule {
2563
+ const imagePlaneModule = metaData.get('imagePlaneModule', imageId);
2564
+
2565
+ const newImagePlaneModule: ImagePlaneModule = {
2566
+ ...imagePlaneModule,
2567
+ };
2568
+
2569
+ if (!newImagePlaneModule.columnPixelSpacing) {
2570
+ newImagePlaneModule.columnPixelSpacing = 1;
2571
+ this.hasPixelSpacing = false;
2572
+ }
2573
+
2574
+ if (!newImagePlaneModule.rowPixelSpacing) {
2575
+ newImagePlaneModule.rowPixelSpacing = 1;
2576
+ this.hasPixelSpacing = false;
2577
+ }
2578
+
2579
+ if (!newImagePlaneModule.columnCosines) {
2580
+ newImagePlaneModule.columnCosines = [0, 1, 0];
2581
+ }
2582
+
2583
+ if (!newImagePlaneModule.rowCosines) {
2584
+ newImagePlaneModule.rowCosines = [1, 0, 0];
2585
+ }
2586
+
2587
+ if (!newImagePlaneModule.imagePositionPatient) {
2588
+ newImagePlaneModule.imagePositionPatient = [0, 0, 0];
2589
+ }
2590
+
2591
+ if (!newImagePlaneModule.imageOrientationPatient) {
2592
+ newImagePlaneModule.imageOrientationPatient = new Float32Array([
2593
+ 1, 0, 0, 0, 1, 0,
2594
+ ]);
2595
+ }
2596
+
2597
+ return newImagePlaneModule;
2598
+ }
2599
+
2600
+ private renderingPipelineFunctions = {
2601
+ getImageData: {
2602
+ cpu: this.getImageDataCPU,
2603
+ gpu: this.getImageDataGPU,
2604
+ },
2605
+ setColormap: {
2606
+ cpu: this.setColormapCPU,
2607
+ gpu: this.setColormapGPU,
2608
+ },
2609
+ getCamera: {
2610
+ cpu: this.getCameraCPU,
2611
+ gpu: super.getCamera,
2612
+ },
2613
+ setCamera: {
2614
+ cpu: this.setCameraCPU,
2615
+ gpu: super.setCamera,
2616
+ },
2617
+ setVOI: {
2618
+ cpu: this.setVOICPU,
2619
+ gpu: this.setVOIGPU,
2620
+ },
2621
+ getRotation: {
2622
+ cpu: this.getRotationCPU,
2623
+ gpu: this.getRotationGPU,
2624
+ },
2625
+ setInterpolationType: {
2626
+ cpu: this.setInterpolationTypeCPU,
2627
+ gpu: this.setInterpolationTypeGPU,
2628
+ },
2629
+ setInvertColor: {
2630
+ cpu: this.setInvertColorCPU,
2631
+ gpu: this.setInvertColorGPU,
2632
+ },
2633
+ resetCamera: {
2634
+ cpu: (resetPan = true, resetZoom = true): boolean => {
2635
+ this.resetCameraCPU(resetPan, resetZoom);
2636
+ return true;
2637
+ },
2638
+ gpu: (resetPan = true, resetZoom = true): boolean => {
2639
+ this.resetCameraGPU(resetPan, resetZoom);
2640
+ return true;
2641
+ },
2642
+ },
2643
+ canvasToWorld: {
2644
+ cpu: this.canvasToWorldCPU,
2645
+ gpu: this.canvasToWorldGPU,
2646
+ },
2647
+ worldToCanvas: {
2648
+ cpu: this.worldToCanvasCPU,
2649
+ gpu: this.worldToCanvasGPU,
2650
+ },
2651
+ getRenderer: {
2652
+ cpu: () => this.getCPUFallbackError('getRenderer'),
2653
+ gpu: super.getRenderer,
2654
+ },
2655
+ getDefaultActor: {
2656
+ cpu: () => this.getCPUFallbackError('getDefaultActor'),
2657
+ gpu: super.getDefaultActor,
2658
+ },
2659
+ getActors: {
2660
+ cpu: () => this.getCPUFallbackError('getActors'),
2661
+ gpu: super.getActors,
2662
+ },
2663
+ getActor: {
2664
+ cpu: () => this.getCPUFallbackError('getActor'),
2665
+ gpu: super.getActor,
2666
+ },
2667
+ setActors: {
2668
+ cpu: () => this.getCPUFallbackError('setActors'),
2669
+ gpu: super.setActors,
2670
+ },
2671
+ addActors: {
2672
+ cpu: () => this.getCPUFallbackError('addActors'),
2673
+ gpu: super.addActors,
2674
+ },
2675
+ addActor: {
2676
+ cpu: () => this.getCPUFallbackError('addActor'),
2677
+ gpu: super.addActor,
2678
+ },
2679
+ removeAllActors: {
2680
+ cpu: () => this.getCPUFallbackError('removeAllActors'),
2681
+ gpu: super.removeAllActors,
2682
+ },
2683
+ unsetColormap: {
2684
+ cpu: this.unsetColormapCPU,
2685
+ gpu: this.unsetColormapGPU,
2686
+ },
2687
+ };
2688
+ }
2689
+
2690
+ export default StackViewport;