Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 122x 246x 122x 246x 8017x 246x 246x 492x 492x 492x 24876x 24876x 24876x 15820x 23191x 8x 16x 16x 16x 262x 262x 9633x | import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData'; import imageIdToURI from '../../utilities/imageIdToURI'; import VoxelManager from '../../utilities/VoxelManager'; import { vtkStreamingOpenGLTexture } from '../../RenderingEngine/vtkClasses'; import type { Metadata, Point3, Mat3, ImageVolumeProps, IImage, PixelDataTypedArrayString, RGB, IVoxelManager, } from '../../types'; import cache from '../cache'; import type vtkOpenGLTexture from '@kitware/vtk.js/Rendering/OpenGL/Texture'; export interface vtkStreamingOpenGLTexture extends vtkOpenGLTexture { setUpdatedFrame: (frame: number) => void; setVolumeId: (volumeId: string) => void; releaseGraphicsResources: () => void; } /** The base class for volume data. It includes the volume metadata * and the volume data along with the loading status. */ export class ImageVolume { private _imageIds: string[]; private _imageIdsIndexMap = new Map(); private _imageURIsIndexMap = new Map(); /** volume scalar data 3D or 4D */ protected totalNumFrames: number; protected cornerstoneImageMetaData = null; /** Read-only unique identifier for the volume */ readonly volumeId: string; isPreScaled = false; /** Dimensions of the volume */ dimensions: Point3; /** volume direction in world space */ direction: Mat3; /** volume metadata */ metadata: Metadata; /** volume origin, Note this is an opinionated origin for the volume */ origin: Point3; /** Whether preScaling has been performed on the volume */ /** volume scaling parameters if it contains scaled data */ scaling?: { PT?: { // @TODO: Do these values exist? SUVlbmFactor?: number; SUVbsaFactor?: number; // accessed in ProbeTool suvbwToSuvlbm?: number; suvbwToSuvbsa?: number; }; }; /** volume spacing in 3d world space */ spacing: Point3; /** volume number of voxels */ numVoxels: number; /** volume image data */ imageData?: vtkImageData; /** open gl texture for the volume */ vtkOpenGLTexture: vtkStreamingOpenGLTexture; /** load status object for the volume */ loadStatus?: Record<string, unknown>; /** optional reference volume id if the volume is derived from another volume */ referencedVolumeId?: string; /** optional reference image ids if the volume is derived from a set of images in the image cache */ referencedImageIds?: string[]; /** whether the metadata for the pixel spacing is not undefined */ hasPixelSpacing: boolean; /** Property to store additional information */ additionalDetails?: Record<string, unknown>; /** * The new volume model which solely relies on the separate image data * and do not cache the volume data at all */ voxelManager?: IVoxelManager<number> | IVoxelManager<RGB>; dataType?: PixelDataTypedArrayString; // @deprecated numTimePoints? = null as number; numFrames = null as number; suppressWarnings: boolean; constructor(props: ImageVolumeProps) { const { imageIds, scaling, dimensions, spacing, origin, direction, dataType, volumeId, referencedVolumeId, metadata, referencedImageIds, additionalDetails, voxelManager, numberOfComponents, } = props; Iif (!dataType) { throw new Error( 'Data type is required, please provide a data type as string such as "Uint8Array", "Float32Array", etc.' ); } let { imageData } = props; this.suppressWarnings = true; this.imageIds = imageIds; this.volumeId = volumeId; this.metadata = metadata; this.dimensions = dimensions; this.spacing = spacing; this.origin = origin; this.direction = direction; this.dataType = dataType; this.vtkOpenGLTexture = vtkStreamingOpenGLTexture.newInstance(); this.vtkOpenGLTexture.setVolumeId(volumeId); this.voxelManager = voxelManager ?? VoxelManager.createImageVolumeVoxelManager({ dimensions, imageIds, numberOfComponents, }); this.numVoxels = this.dimensions[0] * this.dimensions[1] * this.dimensions[2]; Eif (!imageData) { imageData = vtkImageData.newInstance(); imageData.setDimensions(dimensions); imageData.setSpacing(spacing); imageData.setDirection(direction); imageData.setOrigin(origin); } imageData.set( { dataType: dataType, voxelManager: this.voxelManager, id: volumeId, numberOfComponents: numberOfComponents || 1, }, this.suppressWarnings ); imageData.set( { hasScalarVolume: false, }, this.suppressWarnings ); this.imageData = imageData; this.numFrames = this._getNumFrames(); this._reprocessImageIds(); Iif (scaling) { this.scaling = scaling; } if (referencedVolumeId) { this.referencedVolumeId = referencedVolumeId; } if (referencedImageIds) { this.referencedImageIds = referencedImageIds; } Iif (additionalDetails) { this.additionalDetails = additionalDetails; } } public get sizeInBytes(): number { return this.voxelManager.sizeInBytes; } /** return the image ids for the volume if it is made of separated images */ public get imageIds(): string[] { return this._imageIds; } /** updates the image ids */ public set imageIds(newImageIds: string[]) { this._imageIds = newImageIds; this._reprocessImageIds(); } private _reprocessImageIds() { this._imageIdsIndexMap.clear(); this._imageURIsIndexMap.clear(); this._imageIds.forEach((imageId, i) => { const imageURI = imageIdToURI(imageId); this._imageIdsIndexMap.set(imageId, i); this._imageURIsIndexMap.set(imageURI, i); }); } cancelLoading: () => void; /** return true if it is a 4D volume or false if it is 3D volume */ public isDynamicVolume(): boolean { return this.numTimePoints > 1; } /** * return the index of a given imageId * @param imageId - imageId * @returns imageId index */ public getImageIdIndex(imageId: string): number { return this._imageIdsIndexMap.get(imageId); } public getImageIdByIndex(imageIdIndex: number): string { return this._imageIds[imageIdIndex]; } /** * return the index of a given imageURI * @param imageId - imageURI * @returns imageURI index */ public getImageURIIndex(imageURI: string): number { return this._imageURIsIndexMap.get(imageURI); } public load(callback?: (...args: unknown[]) => void): void { // TODO: Implement } /** * destroy the volume and make it unusable */ destroy(): void { // TODO: GPU memory associated with volume is not cleared. this.imageData.delete(); this.imageData = null; this.voxelManager.clear(); this.vtkOpenGLTexture.releaseGraphicsResources(); this.vtkOpenGLTexture.delete(); } public invalidate() { for (let i = 0; i < this.imageIds.length; i++) { this.vtkOpenGLTexture.setUpdatedFrame(i); } this.imageData.modified(); } /** * Updates the internals of the volume to reflect the changes in the * underlying scalar data. This should be called when the scalar data * is modified externally */ public modified() { this.imageData.modified(); this.vtkOpenGLTexture.modified(); this.numFrames = this._getNumFrames(); } public removeFromCache() { cache.removeVolumeLoadObject(this.volumeId); } public getScalarDataLength(): number { return this.voxelManager.getScalarDataLength(); } /** * Returns the number of frames stored in a scalarData object. The number of * frames is equal to the number of images for 3D volumes or the number of * frames per time poins for 4D volumes. * @returns number of frames per volume */ private _getNumFrames(): number { Eif (!this.isDynamicVolume()) { return this.imageIds.length; } return this.numTimePoints; } /** * Converts imageIdIndex into frameIndex which will be the same * for 3D volumes but different for 4D volumes. The indices are 0 based. */ protected imageIdIndexToFrameIndex(imageIdIndex: number): number { return imageIdIndex % this.numFrames; } /** * Returns an array of all the volume's images as Cornerstone images. * It iterates over all the imageIds and converts them to Cornerstone images. * * @returns An array of Cornerstone images. */ public getCornerstoneImages(): IImage[] { const { imageIds } = this; return imageIds.map((imageId) => { return cache.getImage(imageId); }); } } export default ImageVolume; |