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

0% Statements 0/10
0% Branches 0/4
0% Functions 0/2
0% Lines 0/10

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                                                                                                                             
import { cache, eventTarget } from '@cornerstonejs/core';
import { Events, SegmentationRepresentations } from '../../../enums';
import { getSegmentation } from '../getSegmentation';
import type { LabelmapSegmentationDataVolume } from '../../../types/LabelmapTypes';
import { triggerSegmentationDataModified } from '../triggerSegmentationEvents';
import { addSegmentationRepresentations } from '../addSegmentationRepresentationsToViewport';
 
/**
 * Converts a volume segmentation to a stack segmentation.
 *
 * @param params - The parameters for the conversion.
 * @param params.segmentationId - The segmentationId to convert.
 * @param [params.options] - The conversion options.
 * @param params.options.viewportId - The viewportId to use for the segmentation.
 * @param [params.options.newSegmentationId] -  The new segmentationId to use for the segmentation. If not provided, a new ID will be generated.
 * @param [params.options.removeOriginal] - Whether or not to remove the original segmentation. Defaults to true.
 *
 * @returns A promise that resolves when the conversion is complete.
 */
export async function updateStackSegmentationState({
  segmentationId,
  viewportId,
  imageIds,
  options,
}: {
  segmentationId: string;
  viewportId: string;
  imageIds: string[];
  options?: {
    removeOriginal?: boolean;
  };
}): Promise<void> {
  const segmentation = getSegmentation(segmentationId);
 
  if (options?.removeOriginal) {
    const data = segmentation.representationData
      .Labelmap as LabelmapSegmentationDataVolume;
    if (cache.getVolume(data.volumeId)) {
      cache.removeVolumeLoadObject(data.volumeId);
    }
 
    segmentation.representationData.Labelmap = {
      imageIds,
    };
  } else {
    segmentation.representationData.Labelmap = {
      ...segmentation.representationData.Labelmap,
      imageIds,
    };
  }
 
  await addSegmentationRepresentations(viewportId, [
    {
      segmentationId,
      type: SegmentationRepresentations.Labelmap,
    },
  ]);
 
  eventTarget.addEventListenerOnce(Events.SEGMENTATION_RENDERED, () =>
    triggerSegmentationDataModified(segmentationId)
  );
}