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

0% Statements 0/16
0% Branches 0/12
0% Functions 0/4
0% Lines 0/16

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 { cache } from '@cornerstonejs/core';
import { getSegmentation } from '../getSegmentation';
import { triggerSegmentationDataModified } from '../triggerSegmentationEvents';
 
/**
 * Clears the specified segment value from a segmentation.
 *
 * @param segmentationId - The unique identifier of the segmentation.
 * @param segmentIndex - The index of the segment to be cleared.
 *
 * @throws {Error} If the segmentation type is not supported (currently only labelmap is supported).
 *
 * @remarks
 * This function iterates through all voxels in the segmentation and sets the value to 0
 * for any voxel that matches the specified segment index. It supports both stack and volume
 * segmentations.
 */
export function clearSegmentValue(
  segmentationId: string,
  segmentIndex: number
) {
  const segmentation = getSegmentation(segmentationId);
 
  if (segmentation.representationData.Labelmap) {
    const { representationData } = segmentation;
    const labelmapData = representationData.Labelmap;
 
    if ('imageIds' in labelmapData || 'volumeId' in labelmapData) {
      const items =
        'imageIds' in labelmapData
          ? labelmapData.imageIds.map((imageId) => cache.getImage(imageId))
          : [cache.getVolume(labelmapData.volumeId)];
 
      items.forEach((item) => {
        if (!item) {
          return;
        }
 
        const { voxelManager } = item;
        voxelManager.forEach(({ value, index }) => {
          if (value === segmentIndex) {
            voxelManager.setAtIndex(index, 0);
          }
        });
      });
    }
 
    triggerSegmentationDataModified(segmentationId);
  } else {
    throw new Error(
      'Invalid segmentation type, only labelmap is supported right now'
    );
  }
}