All files / tools/src/stateManagement/segmentation removeSegmentation.ts

0% Statements 0/16
100% Branches 0/0
0% Functions 0/8
0% Lines 0/15

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                                                                                                             
import { defaultSegmentationStateManager } from './SegmentationStateManager';
import { triggerSegmentationRemoved } from './triggerSegmentationEvents';
import { removeSegmentationRepresentations } from './removeSegmentationRepresentations';
 
/**
 * Removes the segmentation from the segmentation state manager and its representations from all viewports
 *
 * @triggers SEGMENTATION_REMOVED
 *
 * @param segmentationId - The id of the segmentation
 */
export function removeSegmentation(segmentationId: string): void {
  const segmentationStateManager = defaultSegmentationStateManager;
 
  // Get all viewport IDs that have a representation of this segmentation
  const viewportsWithSegmentation = segmentationStateManager
    .getAllViewportSegmentationRepresentations()
    .filter(({ representations }) =>
      representations.some((rep) => rep.segmentationId === segmentationId)
    )
    .map(({ viewportId }) => viewportId);
 
  // Remove segmentation representations from all affected viewports
  viewportsWithSegmentation.forEach((viewportId) => {
    removeSegmentationRepresentations(viewportId, { segmentationId });
  });
 
  // Remove the segmentation from the state
  segmentationStateManager.removeSegmentation(segmentationId);
 
  // Trigger the removal event
  triggerSegmentationRemoved(segmentationId);
}
 
/**
 * Removes all segmentations from the segmentation state manager and their representations from all viewports
 *
 * @triggers SEGMENTATION_REMOVED for each segmentation
 */
export function removeAllSegmentations(): void {
  const segmentationStateManager = defaultSegmentationStateManager;
  const segmentations = segmentationStateManager.getState().segmentations;
 
  // Remove each segmentation
  const segmentationIds = segmentations.map(
    (segmentation) => segmentation.segmentationId
  );
  segmentationIds.forEach((segmentationId) => {
    removeSegmentation(segmentationId);
  });
 
  // Clear the state
  segmentationStateManager.resetState();
}