All files / packages/core/src/utilities getTargetVolumeAndSpacingInNormalDir.ts

95.23% Statements 20/21
83.33% Branches 10/12
100% Functions 3/3
95.23% Lines 20/21

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                                                        239x 239x   239x     239x   239x     311x 311x       239x 1x 1x     1x         1x       238x         238x 310x   310x         310x 238x 238x       238x    
import cache from '../cache/cache';
// import type { VolumeViewport } from '../RenderingEngine'
import { ICamera, IImageVolume, IVolumeViewport } from '../types';
import getSpacingInNormalDirection from './getSpacingInNormalDirection';
 
/**
 * Given a volume viewport and camera, find the target volume.
 * The imageVolume is retrieved from cache for the specified targetVolumeId or
 * in case it is not provided, it chooses the volumeId on the viewport (there
 * might be more than one in case of fusion) that has the finest resolution in the
 * direction of view (normal).
 *
 * @param viewport - volume viewport
 * @param camera - current camera
 * @param targetVolumeId - If a target volumeId is given that volume
 * is forced to be used.
 *
 * @returns An object containing the imageVolume and spacingInNormalDirection.
 *
 */
export default function getTargetVolumeAndSpacingInNormalDir(
  viewport: IVolumeViewport,
  camera: ICamera,
  targetVolumeId?: string
): {
  imageVolume: IImageVolume;
  spacingInNormalDirection: number;
} {
  const { viewPlaneNormal } = camera;
  const volumeActors = viewport.getActors();
 
  Iif (!volumeActors || !volumeActors.length) {
    return { spacingInNormalDirection: null, imageVolume: null };
  }
  const numVolumeActors = volumeActors.length;
 
  const imageVolumes = volumeActors.map((va) => {
    // prefer the referenceUID if it is set, since it can be a derived actor
    // and the uid does not necessarily match the volumeId
    const uid = va.referenceId ?? va.uid;
    return cache.getVolume(uid);
  });
 
  // If a volumeId is defined, set that volume as the target
  if (targetVolumeId) {
    const imageVolume = imageVolumes.find(
      (iv) => iv.volumeId === targetVolumeId
    );
 
    const spacingInNormalDirection = getSpacingInNormalDirection(
      imageVolume,
      viewPlaneNormal
    );
 
    return { imageVolume, spacingInNormalDirection };
  }
 
  // Fetch volume actor with finest resolution in direction of projection.
  const smallest = {
    spacingInNormalDirection: Infinity,
    imageVolume: null,
  };
 
  for (let i = 0; i < numVolumeActors; i++) {
    const imageVolume = imageVolumes[i];
 
    const spacingInNormalDirection = getSpacingInNormalDirection(
      imageVolume,
      viewPlaneNormal
    );
 
    if (spacingInNormalDirection < smallest.spacingInNormalDirection) {
      smallest.spacingInNormalDirection = spacingInNormalDirection;
      smallest.imageVolume = imageVolume;
    }
  }
 
  return smallest;
}