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

66.66% Statements 14/21
50% Branches 6/12
80% Functions 4/5
66.66% Lines 14/21

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                                        78x   78x       63x     15x 17x     15x 15x                                 15x     15x 17x                 17x         17x   17x 17x                          
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.
 * @param immediate - if True the viewport will be re-rendered immediately.
 */
function removeSegmentationsFromToolGroup(
  toolGroupId: string,
  segmentationRepresentationUIDs?: string[] | undefined,
  immediate?: boolean
): void {
  const toolGroupSegRepresentations =
    getSegmentationRepresentations(toolGroupId);
 
  if (
    !toolGroupSegRepresentations ||
    toolGroupSegRepresentations.length === 0
  ) {
    return;
  }
 
  const toolGroupSegRepresentationUIDs = toolGroupSegRepresentations.map(
    (representation) => representation.segmentationRepresentationUID
  );
 
  let segRepresentationUIDsToRemove = segmentationRepresentationUIDs;
  Iif (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, immediate);
  });
}
 
function _removeSegmentation(
  toolGroupId: string,
  segmentationRepresentationUID: string,
  immediate?: boolean
): void {
  const segmentationRepresentation = getSegmentationRepresentationByUID(
    toolGroupId,
    segmentationRepresentationUID
  );
 
  const { type } = segmentationRepresentation;
 
  Eif (type === SegmentationRepresentations.Labelmap) {
    labelmapDisplay.removeSegmentationRepresentation(
      toolGroupId,
      segmentationRepresentationUID,
      immediate
    );
  } else if (type === SegmentationRepresentations.Contour) {
    console.debug('Contour representation is not supported yet, ignoring...');
  } else {
    throw new Error(`The representation ${type} is not supported yet`);
  }
}
 
export default removeSegmentationsFromToolGroup;