All files / packages/core/src/RenderingEngine VolumeViewport.ts

31.14% Statements 57/183
28.57% Branches 24/84
25% Functions 7/28
32.02% Lines 57/178

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570                                                                                  90x   90x   90x     90x 90x 90x                                       69x   69x           69x         69x       90x                 90x                                 22x   22x           22x         22x                                                                                                                                                                                                                                                                     181x   181x   181x 181x 181x 181x         181x 181x 115x     115x 115x   115x 91x 91x 91x   91x 91x       91x             91x 91x         181x                       181x                                                                                     90x 19x   19x   19x       19x   19x       19x 19x 19x       19x                     90x 1x   1x       1x 1x   1x       1x   1x                                                                                                                                                                                                                         90x                                                                                                                  
import vtkPlane from '@kitware/vtk.js/Common/DataModel/Plane';
import vtkVolume from '@kitware/vtk.js/Rendering/Core/Volume';
 
import { vec3 } from 'gl-matrix';
 
import cache from '../cache';
import { MPR_CAMERA_VALUES, RENDERING_DEFAULTS } from '../constants';
import { BlendModes, OrientationAxis, Events } from '../enums';
import type {
  ActorEntry,
  IImageVolume,
  IVolumeInput,
  OrientationVectors,
  Point3,
} from '../types';
import type { ViewportInput } from '../types/IViewport';
import {
  actorIsA,
  getClosestImageId,
  getSliceRange,
  getSpacingInNormalDirection,
  isImageActor,
  snapFocalPointToSlice,
  triggerEvent,
} from '../utilities';
import BaseVolumeViewport from './BaseVolumeViewport';
import setDefaultVolumeVOI from './helpers/setDefaultVolumeVOI';
import { setTransferFunctionNodes } from '../utilities/transferFunctionUtils';
import { ImageActor } from '../types/IActor';
import getImageSliceDataForVolumeViewport from '../utilities/getImageSliceDataForVolumeViewport';
 
/**
 * An object representing a VolumeViewport. VolumeViewports are used to render
 * 3D volumes from which various orientations can be viewed. Since VolumeViewports
 * use SharedVolumeMappers behind the scene, memory footprint of visualizations
 * of the same volume in different orientations is very small.
 *
 * For setting volumes on viewports you need to use {@link addVolumesToViewports}
 * which will add volumes to the specified viewports.
 */
class VolumeViewport extends BaseVolumeViewport {
  private _useAcquisitionPlaneForViewPlane = false;
  constructor(props: ViewportInput) {
    super(props);
 
    const { orientation } = this.options;
    // if the camera is set to be acquisition axis then we need to skip
    // it for now until the volume is set
    Eif (orientation && orientation !== OrientationAxis.ACQUISITION) {
      this.applyViewOrientation(orientation);
      return;
    }
 
    this._useAcquisitionPlaneForViewPlane = true;
  }
 
  /**
   * Creates volume actors for all volumes defined in the `volumeInputArray`.
   * For each entry, if a `callback` is supplied, it will be called with the new volume actor as input.
   * For each entry, if a `blendMode` and/or `slabThickness` is defined, this will be set on the actor's
   * `VolumeMapper`.
   *
   * @param volumeInputArray - The array of `VolumeInput`s which define the volumes to add.
   * @param immediate - Whether the `Viewport` should be rendered as soon as volumes are added.
   */
  public async setVolumes(
    volumeInputArray: Array<IVolumeInput>,
    immediate = false,
    suppressEvents = false
  ): Promise<void> {
    const firstImageVolume = cache.getVolume(volumeInputArray[0].volumeId);
 
    Iif (!firstImageVolume) {
      throw new Error(
        `imageVolume with id: ${firstImageVolume.volumeId} does not exist`
      );
    }
 
    Iif (this._useAcquisitionPlaneForViewPlane) {
      this._setViewPlaneToAcquisitionPlane(firstImageVolume);
      this._useAcquisitionPlaneForViewPlane = false;
    }
 
    return super.setVolumes(volumeInputArray, immediate, suppressEvents);
  }
 
  /** Gets the number of slices the volume is broken up into in the camera direction */
  public getNumberOfSlices = (): number => {
    const { numberOfSlices } = getImageSliceDataForVolumeViewport(this);
    return numberOfSlices;
  };
 
  /**
   * Returns the image index associated with the volume viewport.
   * @returns The image index.
   */
  public getSliceIndex = (): number => {
    const { imageIndex } = getImageSliceDataForVolumeViewport(this);
    return imageIndex;
  };
 
  /**
   * Creates and adds volume actors for all volumes defined in the `volumeInputArray`.
   * For each entry, if a `callback` is supplied, it will be called with the new volume actor as input.
   *
   * @param volumeInputArray - The array of `VolumeInput`s which define the volumes to add.
   * @param immediate - Whether the `Viewport` should be rendered as soon as volumes are added.
   */
  public async addVolumes(
    volumeInputArray: Array<IVolumeInput>,
    immediate = false,
    suppressEvents = false
  ): Promise<void> {
    const firstImageVolume = cache.getVolume(volumeInputArray[0].volumeId);
 
    Iif (!firstImageVolume) {
      throw new Error(
        `imageVolume with id: ${firstImageVolume.volumeId} does not exist`
      );
    }
 
    Iif (this._useAcquisitionPlaneForViewPlane) {
      this._setViewPlaneToAcquisitionPlane(firstImageVolume);
      this._useAcquisitionPlaneForViewPlane = false;
    }
 
    return super.addVolumes(volumeInputArray, immediate, suppressEvents);
  }
 
  /**
   * It sets the orientation for the camera, the orientation can be one of the
   * following: axial, sagittal, coronal, default. Use the Enums.OrientationAxis
   * to set the orientation. The "default" orientation is the orientation that
   * the volume was acquired in (scan axis)
   *
   * @param orientation - The orientation to set the camera to.
   * @param immediate - Whether the `Viewport` should be rendered as soon as the camera is set.
   */
  public setOrientation(
    orientation: OrientationAxis | OrientationVectors,
    immediate = true
  ): void {
    let viewPlaneNormal, viewUp;
 
    // check if the orientation is a string or an object
    if (typeof orientation === 'string') {
      if (MPR_CAMERA_VALUES[orientation]) {
        ({ viewPlaneNormal, viewUp } = MPR_CAMERA_VALUES[orientation]);
      } else if (orientation === 'acquisition') {
        ({ viewPlaneNormal, viewUp } = this._getAcquisitionPlaneOrientation());
      } else {
        throw new Error(
          `Invalid orientation: ${orientation}. Use Enums.OrientationAxis instead.`
        );
      }
 
      this.setCamera({
        viewPlaneNormal,
        viewUp,
      });
 
      this.viewportProperties.orientation = orientation;
      this.resetCamera();
    } else {
      ({ viewPlaneNormal, viewUp } = orientation);
      this.applyViewOrientation(orientation);
    }
 
    if (immediate) {
      this.render();
    }
  }
 
  private _getAcquisitionPlaneOrientation(): OrientationVectors {
    const actorEntry = this.getDefaultActor();
 
    if (!actorEntry) {
      return;
    }
 
    // Todo: fix this after we add the volumeId reference to actorEntry later
    // in the segmentation refactor
    const volumeId = actorEntry.uid;
 
    const imageVolume = cache.getVolume(volumeId);
 
    if (!imageVolume) {
      throw new Error(
        `imageVolume with id: ${volumeId} does not exist in cache`
      );
    }
 
    const { direction } = imageVolume;
    const viewPlaneNormal = direction.slice(6, 9).map((x) => -x) as Point3;
    const viewUp = (direction.slice(3, 6) as Point3).map((x) => -x) as Point3;
 
    return {
      viewPlaneNormal,
      viewUp,
    };
  }
 
  private _setViewPlaneToAcquisitionPlane(imageVolume: IImageVolume): void {
    let viewPlaneNormal, viewUp;
 
    if (imageVolume) {
      const { direction } = imageVolume;
      viewPlaneNormal = direction.slice(6, 9).map((x) => -x) as Point3;
      viewUp = (direction.slice(3, 6) as Point3).map((x) => -x) as Point3;
    } else {
      ({ viewPlaneNormal, viewUp } = this._getAcquisitionPlaneOrientation());
    }
 
    this.setCamera({
      viewPlaneNormal,
      viewUp,
    });
 
    this.initialViewUp = viewUp;
    this.resetCamera();
  }
 
  public setBlendMode(
    blendMode: BlendModes,
    filterActorUIDs = [],
    immediate = false
  ): void {
    let actorEntries = this.getActors();
 
    if (filterActorUIDs && filterActorUIDs.length > 0) {
      actorEntries = actorEntries.filter((actorEntry: ActorEntry) => {
        return filterActorUIDs.includes(actorEntry.uid);
      });
    }
 
    actorEntries.forEach((actorEntry) => {
      const { actor } = actorEntry;
 
      const mapper = actor.getMapper();
      // @ts-ignore vtk incorrect typing
      mapper.setBlendMode?.(blendMode);
    });
 
    if (immediate) {
      this.render();
    }
  }
 
  /**
   * Reset the camera for the volume viewport
   */
  public resetCamera(
    resetPan = true,
    resetZoom = true,
    resetToCenter = true,
    resetRotation = false
  ): boolean {
    super.resetCamera(resetPan, resetZoom, resetToCenter);
 
    this.resetVolumeViewportClippingRange();
 
    const activeCamera = this.getVtkActiveCamera();
    const viewPlaneNormal = <Point3>activeCamera.getViewPlaneNormal();
    const viewUp = <Point3>activeCamera.getViewUp();
    const focalPoint = <Point3>activeCamera.getFocalPoint();
 
    // always add clipping planes for the volume viewport. If a use case
    // arises where we don't want clipping planes, you should use the volume_3d
    // viewport instead.
    const actorEntries = this.getActors();
    actorEntries.forEach((actorEntry) => {
      Iif (!actorEntry.actor) {
        return;
      }
      const mapper = actorEntry.actor.getMapper();
      const vtkPlanes = mapper.getClippingPlanes();
 
      if (vtkPlanes.length === 0 && !actorEntry?.clippingFilter) {
        const clipPlane1 = vtkPlane.newInstance();
        const clipPlane2 = vtkPlane.newInstance();
        const newVtkPlanes = [clipPlane1, clipPlane2];
 
        let slabThickness = RENDERING_DEFAULTS.MINIMUM_SLAB_THICKNESS;
        Iif (actorEntry.slabThickness) {
          slabThickness = actorEntry.slabThickness;
        }
 
        this.setOrientationOfClippingPlanes(
          newVtkPlanes,
          slabThickness,
          viewPlaneNormal,
          focalPoint
        );
 
        mapper.addClippingPlane(clipPlane1);
        mapper.addClippingPlane(clipPlane2);
      }
    });
 
    //Only reset the rotation of the camera if wanted (so we don't reset everytime resetCamera is called) and also verify that the viewport has an orientation that we know (sagittal, coronal, axial)
    Iif (
      resetRotation &&
      MPR_CAMERA_VALUES[this.viewportProperties.orientation] !== undefined
    ) {
      const viewToReset =
        MPR_CAMERA_VALUES[this.viewportProperties.orientation];
      this.setCameraNoEvent({
        viewUp: viewToReset.viewUp,
        viewPlaneNormal: viewToReset.viewPlaneNormal,
      });
    }
 
    return true;
  }
 
  /**
   * It sets the slabThickness of the actors of the viewport. If filterActorUIDs are
   * provided, only the actors with the given UIDs will be affected. If no
   * filterActorUIDs are provided, all actors will be affected.
   *
   * @param slabThickness - The slab thickness to set.
   * @param filterActorUIDs - Optional argument to filter the actors to apply
   * the slab thickness to (if not provided, all actors will be affected).
   */
  public setSlabThickness(slabThickness: number, filterActorUIDs = []): void {
    if (slabThickness < 0.1) {
      // Cannot render zero thickness
      slabThickness = 0.1;
    }
 
    let actorEntries = this.getActors();
 
    if (filterActorUIDs && filterActorUIDs.length > 0) {
      actorEntries = actorEntries.filter((actorEntry) => {
        return filterActorUIDs.includes(actorEntry.uid);
      });
    }
 
    actorEntries.forEach((actorEntry) => {
      if (actorIsA(actorEntry, 'vtkVolume')) {
        actorEntry.slabThickness = slabThickness;
      }
    });
 
    const currentCamera = this.getCamera();
    this.updateClippingPlanesForActors(currentCamera);
    this.triggerCameraModifiedEventIfNecessary(currentCamera, currentCamera);
    this.viewportProperties.slabThickness = slabThickness;
  }
 
  /**
   * Uses the origin and focalPoint to calculate the slice index.
   *
   * @returns The slice index in the direction of the view
   */
  public getCurrentImageIdIndex = (volumeId?: string): number => {
    const { viewPlaneNormal, focalPoint } = this.getCamera();
 
    const imageData = this.getImageData(volumeId);
 
    Iif (!imageData) {
      return;
    }
 
    const { origin, direction, spacing } = imageData;
 
    const spacingInNormal = getSpacingInNormalDirection(
      { direction, spacing },
      viewPlaneNormal
    );
    const sub = vec3.create();
    vec3.sub(sub, focalPoint, origin);
    const distance = vec3.dot(sub, viewPlaneNormal);
 
    // divide by the spacing in the normal direction to get the
    // number of steps, and subtract 1 to get the index
    return Math.round(Math.abs(distance) / spacingInNormal);
  };
 
  /**
   * Uses viewport camera and volume actor to decide if the viewport
   * is looking at the volume in the direction of acquisition (imageIds).
   * If so, it uses the origin and focalPoint to find which imageId is
   * currently being viewed.
   *
   * @returns ImageId
   */
  public getCurrentImageId = (): string | undefined => {
    const actorEntry = this.getDefaultActor();
 
    Iif (!actorEntry || !actorIsA(actorEntry, 'vtkVolume')) {
      return;
    }
 
    const { uid } = actorEntry;
    const volume = cache.getVolume(uid);
 
    Iif (!volume) {
      return;
    }
 
    const { viewPlaneNormal, focalPoint } = this.getCamera();
 
    return getClosestImageId(volume, focalPoint, viewPlaneNormal);
  };
 
  /**
   * Reset the viewport properties to the default values
   *
 
   * @param volumeId - Optional volume ID to specify which volume properties to reset.
   * If not provided, it will reset the properties of the default actor.
   *
   * @returns void
   */
  public resetProperties(volumeId?: string): void {
    this._resetProperties(volumeId);
  }
 
  private _resetProperties(volumeId?: string) {
    // Get the actor based on the volumeId if provided, otherwise use the default actor.
    const volumeActor = volumeId
      ? this.getActor(volumeId)
      : this.getDefaultActor();
 
    if (!volumeActor) {
      throw new Error(`No actor found for the given volumeId: ${volumeId}`);
    }
 
    // if a custom slabThickness was set, we need to reset it
    if (volumeActor.slabThickness) {
      volumeActor.slabThickness = RENDERING_DEFAULTS.MINIMUM_SLAB_THICKNESS;
      this.viewportProperties.slabThickness = undefined;
      this.updateClippingPlanesForActors(this.getCamera());
    }
 
    const imageVolume = cache.getVolume(volumeActor.uid);
    if (!imageVolume) {
      throw new Error(
        `imageVolume with id: ${volumeActor.uid} does not exist in cache`
      );
    }
    setDefaultVolumeVOI(volumeActor.actor as vtkVolume, imageVolume, false);
 
    if (isImageActor(volumeActor)) {
      const transferFunction = (volumeActor.actor as ImageActor)
        .getProperty()
        .getRGBTransferFunction(0);
 
      setTransferFunctionNodes(
        transferFunction,
        this.initialTransferFunctionNodes
      );
    }
 
    const eventDetails = {
      ...super.getVOIModifiedEventDetail(volumeId),
    };
 
    const resetPan = true;
    const resetZoom = true;
    const resetToCenter = true;
    const resetCameraRotation = true;
    this.resetCamera(resetPan, resetZoom, resetToCenter, resetCameraRotation);
 
    triggerEvent(this.element, Events.VOI_MODIFIED, eventDetails);
  }
 
  /**
   * Retrieves the clipping planes for the slices in the volume viewport.
   * @returns An array of vtkPlane objects representing the clipping planes, or an array of objects with normal and origin properties if raw is true.
   */
  getSlicesClippingPlanes(): Array<{
    sliceIndex: number;
    planes: Array<{
      normal: Point3;
      origin: Point3;
    }>;
  }> {
    const focalPoints = this.getSlicePlaneCoordinates();
    const { viewPlaneNormal } = this.getCamera();
    const slabThickness = RENDERING_DEFAULTS.MINIMUM_SLAB_THICKNESS;
 
    return focalPoints.map(({ point, sliceIndex }) => {
      const vtkPlanes = [vtkPlane.newInstance(), vtkPlane.newInstance()];
 
      this.setOrientationOfClippingPlanes(
        vtkPlanes,
        slabThickness,
        viewPlaneNormal,
        point
      );
 
      return {
        sliceIndex,
        planes: vtkPlanes.map((plane) => ({
          normal: plane.getNormal(),
          origin: plane.getOrigin(),
        })),
      };
    });
  }
 
  /**
   * Returns an array of 3D coordinates representing the slice plane positions.
   * It starts by the focal point as a reference point on the current slice that
   * the camera is looking at, and then it calculates the slice plane positions
   * by moving the focal point in the direction of the view plane normal back and
   * forward, and snaps them to the slice.
   *
   * @returns An array of Point3 representing the slice plane coordinates.
   */
  public getSlicePlaneCoordinates = (): Array<{
    sliceIndex: number;
    point: Point3;
  }> => {
    const actorEntry = this.getDefaultActor();
 
    if (!actorEntry?.actor) {
      console.warn('No image data found for calculating vtkPlanes.');
      return [];
    }
 
    const volumeId = actorEntry.uid;
    const imageVolume = cache.getVolume(volumeId);
 
    const camera = this.getCamera();
    const { focalPoint, position, viewPlaneNormal } = camera;
    const spacingInNormalDirection = getSpacingInNormalDirection(
      imageVolume,
      viewPlaneNormal
    );
    const sliceRange = getSliceRange(
      actorEntry.actor as vtkVolume,
      viewPlaneNormal,
      focalPoint
    );
 
    // calculate the number of slices that is possible to visit
    // in the direction of the view back and forward
    const numSlicesBackward = Math.round(
      (sliceRange.current - sliceRange.min) / spacingInNormalDirection
    );
 
    const numSlicesForward = Math.round(
      (sliceRange.max - sliceRange.current) / spacingInNormalDirection
    );
 
    const currentSliceIndex = this.getSliceIndex();
    const focalPoints = [];
 
    for (let i = -numSlicesBackward; i <= numSlicesForward; i++) {
      const { newFocalPoint: point } = snapFocalPointToSlice(
        focalPoint,
        position,
        sliceRange,
        viewPlaneNormal,
        spacingInNormalDirection,
        i
      );
 
      focalPoints.push({ sliceIndex: currentSliceIndex + i, point });
    }
 
    return focalPoints;
  };
}
 
export default VolumeViewport;