All files / core/src/utilities resolveGenericViewportVolumeId.ts

0% Statements 0/6
0% Branches 0/11
0% Functions 0/3
0% Lines 0/6

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                                                                                     
import type { IViewport } from '../types/IViewport';
import { getVolumeId } from './getVolumeId';
 
type GenericViewportActorSurface = IViewport & {
  getActors?: () => Array<{ referencedId?: string }>;
  getDefaultActor?: () => { referencedId?: string } | undefined;
};
 
/**
 * Resolves the cache volume id backing a generic ("next" / PLANAR_NEXT)
 * viewport target.
 *
 * VOI tooling and the colorbar identify a target by its display-set `dataId`,
 * which is NOT the cache volume id - the volume id ends in `:<dataId>`. This
 * maps the `dataId` to the matching actor's `referencedId` (the real volume id):
 * an exact match first, then the `<scheme>:<dataId>` suffix. Anchoring to the
 * `:` separator avoids the unintended-actor matches a loose substring search
 * could produce in a fusion viewport. When no `targetId` is given (or it matches
 * nothing) it falls back to the bound default actor's referenced volume.
 *
 * This idiom was previously duplicated verbatim in `getViewportModality` and
 * `isViewportPreScaled`; it now lives here as the single source of truth and
 * feeds the shared {@link getScalingDescriptor}.
 */
export function resolveGenericViewportVolumeId(
  viewport: IViewport,
  targetId?: string
): string | undefined {
  const genericViewport = viewport as GenericViewportActorSurface;
  const actors = genericViewport.getActors?.() ?? [];
 
  const matchedActor = targetId
    ? (actors.find((actor) => actor.referencedId === targetId) ??
      actors.find((actor) => actor.referencedId?.endsWith(`:${targetId}`)))
    : undefined;
 
  return (
    matchedActor?.referencedId ??
    (targetId ? getVolumeId(targetId) : undefined) ??
    genericViewport.getDefaultActor?.()?.referencedId
  );
}