All files / extensions/cornerstone/src/services/ViewportService/backends LegacyViewportOperations.ts

27.82% Statements 32/115
12.19% Branches 5/41
31.57% Functions 6/19
28.57% Lines 32/112

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                                                                            182x   2x   2x 2x       2x                             2x 2x 2x       2x                     2x 2x 2x   2x                               2x         2x 2x 2x                                 7x             8x 5x     3x 3x 3x 3x 3x 3x 3x   3x 3x 1x 1x   3x                                                                                                                                                                                                                                                          
import { utilities as csUtils, Types as CoreTypes } from '@cornerstonejs/core';
import { mat4, vec3 } from 'gl-matrix';
import {
  isStackViewportType,
  isVolumeViewportType,
  isOrthographicViewportType,
} from '../../../utils/getLegacyViewportType';
import { getCenterExtent } from '../../../utils/getCenterExtent';
import { isMeasurementWithinViewport } from '../../../utils/isMeasurementWithinViewport';
import type {
  IViewportOperations,
  FlipValue,
  RotationMode,
  VolumeLightingOptions,
  WindowLevelParams,
  ColormapParams,
} from './IViewportOperations';
 
// Loose view of the VTK actor/mapper/property chain used by the 3D VR ops. These
// live on vtk.js objects, not cornerstone types, so they are accessed structurally
// (mirrors the previously-untyped commandsModule bodies).
type VtkActorChain = {
  actor: {
    getMapper: () => Record<string, (...args: unknown[]) => unknown>;
    getProperty: () => Record<string, (...args: unknown[]) => unknown>;
  };
};
 
/**
 * Legacy lane of IViewportOperations: every method is the corresponding
 * commandsModule body lifted verbatim, using the legacy cornerstone APIs directly
 * (getCamera/setCamera, getProperties/setProperties, getViewPresentation/
 * setViewPresentation, resetCamera, getActors). This is the byte-identical flag-off
 * path; the dispatcher only routes non-generic viewports here.
 *
 * No method calls viewport.render() — the command renders (matching per-command
 * render timing).
 */
export const legacyViewportOperations: IViewportOperations = {
  flipHorizontal(viewport: CoreTypes.IViewport, newValue: FlipValue = 'toggle'): void {
    const vp = viewport as CoreTypes.IStackViewport;
    let flipHorizontal: boolean;
    if (newValue === 'toggle') {
      flipHorizontal = !vp.getCamera().flipHorizontal;
    } else E{
      flipHorizontal = newValue;
    }
    vp.setCamera({ flipHorizontal });
  },
 
  flipVertical(viewport: CoreTypes.IViewport, newValue: FlipValue = 'toggle'): void {
    const vp = viewport as CoreTypes.IStackViewport;
    let flipVertical: boolean;
    if (newValue === 'toggle') {
      flipVertical = !vp.getCamera().flipVertical;
    } else {
      flipVertical = newValue;
    }
    vp.setCamera({ flipVertical });
  },
 
  invert(viewport: CoreTypes.IViewport): void {
    const vp = viewport as CoreTypes.IStackViewport;
    const { invert } = vp.getProperties();
    vp.setProperties({ invert: !invert });
  },
 
  rotate(viewport: CoreTypes.IViewport, rotation: number, mode: RotationMode = 'apply'): void {
    Iif (isVolumeViewportType(viewport)) {
      const vp = viewport as CoreTypes.IVolumeViewport;
      const camera = vp.getCamera();
      const rotAngle = (rotation * Math.PI) / 180;
      const rotMat = mat4.identity(new Float32Array(16));
      mat4.rotate(rotMat, rotMat, rotAngle, camera.viewPlaneNormal);
      const rotatedViewUp = vec3.transformMat4(vec3.create(), camera.viewUp, rotMat);
      vp.setCamera({ viewUp: rotatedViewUp as CoreTypes.Point3 });
      return;
    }
 
    const vp = viewport as CoreTypes.IStackViewport;
    if (vp.getRotation !== undefined) {
      const { rotation: currentRotation } = vp.getViewPresentation();
      const newRotation =
        mode === 'apply'
          ? (currentRotation + rotation + 360) % 360
          : (() => {
              // In 'set' mode, account for the effect horizontal/vertical flips
              // have on the perceived rotation direction. A single flip mirrors
              // the image and inverses rotation direction, while two flips
              // restore the original parity. We therefore invert the rotation
              // angle when an odd number of flips are applied so that the
              // requested absolute rotation matches the user expectation.
              const { flipHorizontal = false, flipVertical = false } = vp.getViewPresentation();
 
              const flipsParity = (flipHorizontal ? 1 : 0) + (flipVertical ? 1 : 0);
              const effectiveRotation = flipsParity % 2 === 1 ? -rotation : rotation;
 
              return (effectiveRotation + 360) % 360;
            })();
      vp.setViewPresentation({ rotation: newRotation });
    }
  },
 
  reset(viewport: CoreTypes.IViewport): void {
    const vp = viewport as CoreTypes.IStackViewport;
    vp.resetProperties?.();
    vp.resetCamera();
  },
 
  scaleBy(viewport: CoreTypes.IViewport, direction: number): void {
    const scaleFactor = direction > 0 ? 0.9 : 1.1;
    Iif (isStackViewportType(viewport)) {
      const vp = viewport as CoreTypes.IStackViewport;
      if (direction) {
        const { parallelScale } = vp.getCamera();
        vp.setCamera({ parallelScale: parallelScale * scaleFactor });
      } else {
        vp.resetCamera();
      }
    }
  },
 
  getViewPlaneNormal(viewport: CoreTypes.IViewport): CoreTypes.Point3 | undefined {
    return (viewport as CoreTypes.IStackViewport).getCamera().viewPlaneNormal;
  },
 
  centerOnMeasurement(
    viewport: CoreTypes.IViewport,
    measurement: Record<string, unknown>
  ): boolean {
    if (isMeasurementWithinViewport(viewport, measurement)) {
      return false;
    }
 
    const vp = viewport as CoreTypes.IStackViewport;
    const camera = vp.getCamera();
    const { focalPoint: cameraFocalPoint, position: cameraPosition } = camera;
    const { center, extent } = getCenterExtent(measurement);
    const position = vec3.sub(vec3.create(), cameraPosition, cameraFocalPoint);
    vec3.add(position, position, center);
    vp.setCamera({ focalPoint: center, position: position as unknown as CoreTypes.Point3 });
    // Zoom out if the measurement is too large
    const measurementSize = vec3.dist(extent.min, extent.max);
    if (measurementSize > camera.parallelScale) {
      const scaleFactor = measurementSize / camera.parallelScale;
      vp.setZoom(vp.getZoom() / scaleFactor);
    }
    return true;
  },
 
  setWindowLevel(viewport: CoreTypes.IViewport, params: WindowLevelParams): void {
    const { lower, upper } = csUtils.windowLevel.toLowHighRange(
      params.windowWidth,
      params.windowCenter
    );
    if (isVolumeViewportType(viewport)) {
      (viewport as CoreTypes.IVolumeViewport).setProperties(
        { voiRange: { upper, lower } },
        params.volumeId
      );
    } else {
      (viewport as CoreTypes.IStackViewport).setProperties({ voiRange: { upper, lower } });
    }
  },
 
  setColormap(viewport: CoreTypes.IViewport, params: ColormapParams): void {
    const { colormap, displaySetInstanceUID } = params;
    Iif (isStackViewportType(viewport)) {
      (viewport as CoreTypes.IStackViewport).setProperties({ colormap });
    }
 
    Iif (isOrthographicViewportType(viewport)) {
      const vp = viewport as CoreTypes.IVolumeViewport;
      // ToDo: Find a better way of obtaining the volumeId that corresponds to the displaySetInstanceUID
      const volumeId =
        vp.getAllVolumeIds().find((_volumeId: string) => _volumeId.includes(displaySetInstanceUID)) ??
        vp.getVolumeId();
      vp.setProperties({ colormap }, volumeId);
    }
  },
 
  setPreset(viewport: CoreTypes.IViewport, preset: string): void {
    (viewport as CoreTypes.IVolumeViewport).setProperties({ preset });
  },
 
  setVolumeRenderingQuality(viewport: CoreTypes.IViewport, volumeQuality: number): void {
    const actorEntry = (viewport as unknown as CoreTypes.IVolumeViewport).getActors()[0];
    Iif (!actorEntry) {
      return;
    }
    const { actor } = actorEntry;
    const mapper = (actor as unknown as VtkActorChain['actor']).getMapper();
    const image = mapper.getInputData() as {
      getDimensions: () => number[];
      getSpacing: () => number[];
    };
    const dims = image.getDimensions();
    const spacing = image.getSpacing();
    const spatialDiagonal = vec3.length(
      vec3.fromValues(dims[0] * spacing[0], dims[1] * spacing[1], dims[2] * spacing[2])
    );
 
    let sampleDistance = spacing.reduce((a, b) => a + b) / 3.0;
    sampleDistance /= volumeQuality > 1 ? 0.5 * volumeQuality ** 2 : 1.0;
    const samplesPerRay = spatialDiagonal / sampleDistance + 1;
    mapper.setMaximumSamplesPerRay(samplesPerRay);
    mapper.setSampleDistance(sampleDistance);
  },
 
  shiftVolumeOpacityPoints(viewport: CoreTypes.IViewport, shift: number): void {
    const actorEntry = (viewport as unknown as CoreTypes.IVolumeViewport).getActors()[0];
    Iif (!actorEntry) {
      return;
    }
    const { actor } = actorEntry;
    const ofun = (actor as unknown as VtkActorChain['actor']).getProperty().getScalarOpacity(0) as {
      getSize: () => number;
      getNodeValue: (i: number, v: number[]) => void;
      removeAllPoints: () => void;
      addPoint: (...args: number[]) => void;
    };
 
    const opacityPointValues: number[][] = []; // Array to hold values
    // Gather Existing Values
    const size = ofun.getSize();
    for (let pointIdx = 0; pointIdx < size; pointIdx++) {
      const opacityPointValue = [0, 0, 0, 0];
      ofun.getNodeValue(pointIdx, opacityPointValue);
      // opacityPointValue now holds [xLocation, opacity, midpoint, sharpness]
      opacityPointValues.push(opacityPointValue);
    }
    // Add offset
    opacityPointValues.forEach(opacityPointValue => {
      opacityPointValue[0] += shift; // Change the location value
    });
    // Set new values
    ofun.removeAllPoints();
    opacityPointValues.forEach(opacityPointValue => {
      ofun.addPoint(...opacityPointValue);
    });
  },
 
  setVolumeLighting(viewport: CoreTypes.IViewport, options: VolumeLightingOptions): void {
    const actorEntry = (viewport as unknown as CoreTypes.IVolumeViewport).getActors()[0];
    Iif (!actorEntry) {
      return;
    }
    const { actor } = actorEntry;
    const property = (actor as unknown as VtkActorChain['actor']).getProperty() as {
      setShade: (v: boolean) => void;
      setAmbient: (v: number) => void;
      setDiffuse: (v: number) => void;
      setSpecular: (v: number) => void;
    };
 
    Iif (options.shade !== undefined) {
      property.setShade(options.shade);
    }
 
    Iif (options.ambient !== undefined) {
      property.setAmbient(options.ambient);
    }
 
    Iif (options.diffuse !== undefined) {
      property.setDiffuse(options.diffuse);
    }
 
    Iif (options.specular !== undefined) {
      property.setSpecular(options.specular);
    }
  },
};