All files / tools/src/stateManagement/segmentation/helpers getSegmentationActor.ts

85.18% Statements 23/27
75% Branches 12/16
100% Functions 10/10
85.18% Lines 23/27

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                                740x   740x       740x   740x       740x 740x   740x                           8912x   8912x       8912x   8912x       8912x 8912x   8912x                         4x 4x                         8912x 19858x                               728x 1560x                                   12x       20x                             24x    
import type { Types } from '@cornerstonejs/core';
import { getEnabledElementByViewportId } from '@cornerstonejs/core';
import { SegmentationRepresentations } from '../../../enums';
 
/**
 * Retrieves the actor entry based on the given criteria.
 * @param viewportId - The ID of the viewport.
 * @param segmentationId - The ID of the segmentation.
 * @param filterFn - A function to filter the actors.
 * @returns The actor entry if found, undefined otherwise.
 */
function getActorEntry(
  viewportId: string,
  segmentationId: string,
  filterFn: (actor: Types.ActorEntry) => boolean
): Types.ActorEntry | undefined {
  const enabledElement = getEnabledElementByViewportId(viewportId);
 
  Iif (!enabledElement) {
    return;
  }
 
  const { renderingEngine, viewport } = enabledElement;
 
  Iif (!renderingEngine || !viewport) {
    return;
  }
 
  const actors = viewport.getActors();
  const filteredActors = actors.filter(filterFn);
 
  return filteredActors.length > 0 ? filteredActors[0] : undefined;
}
 
/**
 * Retrieves the actor entry based on the given criteria.
 * @param viewportId - The ID of the viewport.
 * @param segmentationId - The ID of the segmentation.
 * @param filterFn - A function to filter the actors.
 * @returns The actor entry if found, undefined otherwise.
 */
function getActorEntries(
  viewportId: string,
  filterFn: (actor: Types.ActorEntry) => boolean
): Types.ActorEntry[] | undefined {
  const enabledElement = getEnabledElementByViewportId(viewportId);
 
  Iif (!enabledElement) {
    return;
  }
 
  const { renderingEngine, viewport } = enabledElement;
 
  Iif (!renderingEngine || !viewport) {
    return;
  }
 
  const actors = viewport.getActors();
  const filteredActors = actors.filter(filterFn);
 
  return filteredActors.length > 0 ? filteredActors : undefined;
}
 
/**
 * Retrieves the UID of the labelmap actor for the given viewport and segmentation.
 * @param viewportId - The ID of the viewport.
 * @param segmentationId - The ID of the segmentation.
 * @returns The UID of the labelmap actor if found, undefined otherwise.
 */
export function getLabelmapActorUID(
  viewportId: string,
  segmentationId: string
): string | undefined {
  const actorEntry = getLabelmapActorEntry(viewportId, segmentationId);
  return actorEntry?.uid;
}
 
/**
 * Retrieves the labelmap actor entries for the given viewport and segmentation.
 * @param viewportId - The ID of the viewport.
 * @param segmentationId - The ID of the segmentation.
 * @returns The labelmap actor entry if found, undefined otherwise.
 */
export function getLabelmapActorEntries(
  viewportId: string,
  segmentationId: string
) {
  return getActorEntries(viewportId, (actor) =>
    (actor.representationUID as string)?.startsWith(
      `${segmentationId}-${SegmentationRepresentations.Labelmap}`
    )
  );
}
 
/**
 * Retrieves the labelmap actor entry for the given viewport and segmentation.
 * @param viewportId - The ID of the viewport.
 * @param segmentationId - The ID of the segmentation.
 * @returns The labelmap actor entry if found, undefined otherwise.
 */
export function getLabelmapActorEntry(
  viewportId: string,
  segmentationId: string
) {
  return getActorEntry(viewportId, segmentationId, (actor) =>
    (actor.representationUID as string)?.startsWith(
      `${segmentationId}-${SegmentationRepresentations.Labelmap}`
    )
  );
}
 
/**
 * Retrieves the surface actor entry for the given viewport, segmentation, and segment index.
 * @param viewportId - The ID of the viewport.
 * @param segmentationId - The ID of the segmentation.
 * @param segmentIndex - The index of the segment.
 * @returns The surface actor entry if found, undefined otherwise.
 */
export function getSurfaceActorEntry(
  viewportId: string,
  segmentationId: string,
  segmentIndex?: number | string
) {
  return getActorEntry(
    viewportId,
    segmentationId,
    (actor) =>
      actor.representationUID ===
      getSurfaceRepresentationUID(segmentationId, segmentIndex)
  );
}
 
/**
 * Generates a unique identifier for a surface representation of a segmentation segment
 * @param segmentationId - The ID of the segmentation
 * @param segmentIndex - Optional index of the specific segment within the segmentation
 * @returns A string UID combining the segmentation ID, surface representation type, and segment index
 */
export function getSurfaceRepresentationUID(
  segmentationId: string,
  segmentIndex?: number | string
) {
  return `${segmentationId}-${SegmentationRepresentations.Surface}-${segmentIndex}`;
}