All files / packages/tools/src/stateManagement/segmentation removeSegmentationsFromToolGroup.ts

20% Statements 4/20
20% Branches 2/10
20% Functions 1/5
20% Lines 4/20

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                                    78x   78x       78x       78x                                                                                                              
import SegmentationRepresentations from '../../enums/SegmentationRepresentations';
import { labelmapDisplay } from '../../tools/displayTools/Labelmap';
 
import {
  getSegmentationRepresentations,
  getSegmentationRepresentationByUID,
} from './segmentationState';
 
/**
 * Remove the segmentation representation (representation) from the viewports of the toolGroup.
 * @param toolGroupId - The Id of the toolGroup to remove the segmentation from.
 * @param segmentationRepresentationUIDs - The UIDs of the segmentation representations to remove.
 */
function removeSegmentationsFromToolGroup(
  toolGroupId: string,
  segmentationRepresentationUIDs?: string[] | undefined
): void {
  const toolGroupSegRepresentations =
    getSegmentationRepresentations(toolGroupId);
 
  Eif (
    !segmentationRepresentationUIDs ||
    segmentationRepresentationUIDs.length === 0
  ) {
    console.warn(
      'removeSegmentationsFromToolGroup: No segmentationRepresentations found for toolGroupId: ',
      toolGroupId
    );
    return;
  }
 
  const toolGroupSegRepresentationUIDs = toolGroupSegRepresentations.map(
    (representation) => representation.segmentationRepresentationUID
  );
 
  let segRepresentationUIDsToRemove = segmentationRepresentationUIDs;
  if (segRepresentationUIDsToRemove) {
    // make sure the segmentationDataUIDs that are going to be removed belong
    // to the toolGroup
    const invalidSegRepresentationUIDs = segmentationRepresentationUIDs.filter(
      (segRepresentationUID) =>
        !toolGroupSegRepresentationUIDs.includes(segRepresentationUID)
    );
 
    if (invalidSegRepresentationUIDs.length > 0) {
      throw new Error(
        `The following segmentationRepresentationUIDs are not part of the toolGroup: ${JSON.stringify(
          invalidSegRepresentationUIDs
        )}`
      );
    }
  } else {
    // remove all segmentation representations
    segRepresentationUIDsToRemove = toolGroupSegRepresentationUIDs;
  }
 
  segRepresentationUIDsToRemove.forEach((segmentationDataUID) => {
    _removeSegmentation(toolGroupId, segmentationDataUID);
  });
}
 
function _removeSegmentation(
  toolGroupId: string,
  segmentationRepresentationUID: string
): void {
  const segmentationRepresentation = getSegmentationRepresentationByUID(
    toolGroupId,
    segmentationRepresentationUID
  );
 
  const { type } = segmentationRepresentation;
 
  if (type === SegmentationRepresentations.Labelmap) {
    labelmapDisplay.removeSegmentationRepresentation(
      toolGroupId,
      segmentationRepresentationUID
    );
  } else {
    throw new Error(`The representation ${type} is not supported yet`);
  }
}
 
export default removeSegmentationsFromToolGroup;