All files / core/src/RenderingEngine VolumeViewport3D.ts

36.61% Statements 26/71
23.68% Branches 9/38
35.29% Functions 6/17
35.71% Lines 25/70

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                                            4x   4x   4x   4x       4x         4x                                                                   4x                         12x 12x   12x 12x                       12x 12x 12x     244x   4x       4x                                                                                                                                     4x                 4x       48x 48x 48x       48x 48x 48x                                    
import { RENDERING_DEFAULTS } from '../constants';
import type { BlendModes } from '../enums';
import { OrientationAxis, Events } from '../enums';
import cache from '../cache/cache';
import setDefaultVolumeVOI from './helpers/setDefaultVolumeVOI';
import triggerEvent from '../utilities/triggerEvent';
import { actorIsA, isImageActor } from '../utilities/actorCheck';
import { setTransferFunctionNodes } from '../utilities/transferFunctionUtils';
import type vtkVolume from '@kitware/vtk.js/Rendering/Core/Volume';
import type { ViewportInput } from '../types/IViewport';
import type { ImageActor } from '../types/IActor';
import BaseVolumeViewport from './BaseVolumeViewport';
import type { Types } from '@cornerstonejs/core';
/**
 * An object representing a 3-dimensional volume viewport. VolumeViewport3Ds are used to render
 * 3D volumes in their entirety, and not just load a single slice at a time.
 *
 * For setting volumes on viewports you need to use addVolumesToViewports
 * which will add volumes to the specified viewports.
 */
class VolumeViewport3D extends BaseVolumeViewport {
  constructor(props: ViewportInput) {
    super(props);
 
    const { parallelProjection, orientation } = this.options;
 
    const activeCamera = this.getVtkActiveCamera();
 
    Iif (parallelProjection != null) {
      activeCamera.setParallelProjection(parallelProjection);
    }
 
    Iif (orientation && orientation !== OrientationAxis.ACQUISITION) {
      this.applyViewOrientation(orientation);
    }
  }
 
  public setSampleDistanceMultiplier = (multiplier: number): void => {
    const actors = this.getActors();
    actors.forEach((actorEntry) => {
      if (actorIsA(actorEntry, 'vtkVolume')) {
        const actor = actorEntry.actor as Types.VolumeActor;
        const mapper = actor.getMapper();
 
        if (mapper && mapper.getInputData) {
          const imageData = mapper.getInputData();
 
          if (imageData) {
            const spacing = imageData.getSpacing();
 
            //Calculate sample distance
            const defaultSampleDistance =
              (spacing[0] + spacing[1] + spacing[2]) / 6;
 
            const sampleDistanceMultiplier = multiplier || 1;
            let sampleDistance =
              defaultSampleDistance * sampleDistanceMultiplier;
 
            // Apply sample distance if specified
            if (sampleDistance !== undefined && mapper.setSampleDistance) {
              const currentSampleDistance = mapper.getSampleDistance();
              mapper.setSampleDistance(sampleDistance);
            }
          }
        }
      }
    });
 
    this.render();
  };
 
  public getNumberOfSlices = (): number => {
    return 1;
  };
 
  public isInAcquisitionPlane(): boolean {
    return false;
  }
 
  public resetCamera({
    resetPan = true,
    resetZoom = true,
    resetToCenter = true,
  } = {}): boolean {
    super.resetCamera({ resetPan, resetZoom, resetToCenter });
    const activeCamera = this.getVtkActiveCamera();
 
    if (activeCamera.getParallelProjection()) {
      activeCamera.setClippingRange(
        -RENDERING_DEFAULTS.MAXIMUM_RAY_DISTANCE,
        RENDERING_DEFAULTS.MAXIMUM_RAY_DISTANCE
      );
    } else E{
      activeCamera.setClippingRange(
        RENDERING_DEFAULTS.MINIMUM_SLAB_THICKNESS,
        RENDERING_DEFAULTS.MAXIMUM_RAY_DISTANCE
      );
    }
 
    // reset camera clipping range
    const renderer = this.getRenderer();
    renderer.resetCameraClippingRange();
    return true;
  }
 
  getRotation = (): number => 0;
 
  getCurrentImageIdIndex = (): number => {
    return 0;
  };
 
  getCurrentImageId = (): string => {
    return null;
  };
 
  setSlabThickness(slabThickness: number, filterActorUIDs?: string[]): void {
    return null;
  }
 
  setBlendMode(
    blendMode: BlendModes,
    filterActorUIDs?: string[],
    immediate?: boolean
  ): void {
    return null;
  }
 
  /**
   * Resets the properties of the volume to their default values.
   *
   * @param [volumeId] - The id of the volume to reset. If not provided, the default volume will be reset.
   */
  resetProperties(volumeId?: string): void {
    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());
    }
 
    volumeId ||= this.getVolumeId();
    const imageVolume = cache.getVolume(volumeId);
 
    if (!imageVolume) {
      throw new Error(
        `imageVolume with id: ${volumeId} does not exist in cache`
      );
    }
 
    setDefaultVolumeVOI(volumeActor.actor as vtkVolume, imageVolume);
 
    if (isImageActor(volumeActor)) {
      const transferFunction = (volumeActor.actor as ImageActor)
        .getProperty()
        .getRGBTransferFunction(0);
 
      setTransferFunctionNodes(
        transferFunction,
        this.initialTransferFunctionNodes
      );
    }
 
    this.setCamera(this.initialCamera);
    triggerEvent(
      this.element,
      Events.VOI_MODIFIED,
      super.getVOIModifiedEventDetail(volumeId)
    );
  }
 
  public resetCameraForResize = (): boolean => {
    return this.resetCamera({
      resetPan: true,
      resetZoom: true,
      resetToCenter: true,
    });
  };
 
  public getSliceIndex(): number {
    return null;
  }
 
  public setCamera(props) {
    super.setCamera(props);
    this.getRenderer().resetCameraClippingRange();
    this.render();
  }
 
  protected setCameraClippingRange() {
    const activeCamera = this.getVtkActiveCamera();
    if (activeCamera.getParallelProjection()) {
      activeCamera.setClippingRange(
        -RENDERING_DEFAULTS.MAXIMUM_RAY_DISTANCE,
        RENDERING_DEFAULTS.MAXIMUM_RAY_DISTANCE
      );
    } else E{
      activeCamera.setClippingRange(
        RENDERING_DEFAULTS.MINIMUM_SLAB_THICKNESS,
        RENDERING_DEFAULTS.MAXIMUM_RAY_DISTANCE
      );
    }
  }
 
  resetSlabThickness(): void {
    return null;
  }
}
 
export default VolumeViewport3D;