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

90% Statements 18/20
75% Branches 6/8
100% Functions 5/5
90% Lines 18/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                                            32x   32x           32x 26x     32x 12x                 32x 32x   32x         32x     32x 32x       32x 32x 12x             32x 32x 32x          
import { getToolGroupForViewport } from '../../store/ToolGroupManager';
import { invalidateBrushCursor } from '../../utilities/segmentation/invalidateBrushCursor';
import { getSegmentation } from './getSegmentation';
import { getViewportIdsWithSegmentation } from './getViewportIdsWithSegmentation';
import { triggerSegmentationModified } from './triggerSegmentationEvents';
import { getActiveSegmentIndex } from './getActiveSegmentIndex';
import { getSegmentationRepresentations } from './getSegmentationRepresentation';
 
/**
 * Set the active segment index for a segmentation Id. It fires a global state
 * modified event. Also it invalidates the brush cursor for all toolGroups that
 * has the segmentationId as active segment (since the brush cursor color
 * should change as well)
 *
 * @triggers SEGMENTATION_MODIFIED
 * @param segmentationId - The id of the segmentation that the segment belongs to.
 * @param segmentIndex - The index of the segment to be activated.
 */
function setActiveSegmentIndex(
  segmentationId: string,
  segmentIndex: number
): void {
  const segmentation = getSegmentation(segmentationId);
 
  Iif (typeof segmentIndex === 'string') {
    console.warn('segmentIndex is a string, converting to number');
    segmentIndex = Number(segmentIndex);
  }
 
  // set all other segments to inactive
  Object.values(segmentation.segments).forEach((segment) => {
    segment.active = false;
  });
 
  if (!segmentation.segments[segmentIndex]) {
    segmentation.segments[segmentIndex] = {
      segmentIndex,
      label: '',
      locked: false,
      cachedStats: {},
      active: false,
    };
  }
 
  Eif (segmentation.segments[segmentIndex].active !== true) {
    segmentation.segments[segmentIndex].active = true;
 
    triggerSegmentationModified(segmentationId);
  }
 
  // get all toolGroups that has the segmentationId as active
  // segment and call invalidateBrushCursor on them
  const viewportIds = getViewportIdsWithSegmentation(segmentationId);
 
  // check if the viewportId does not have a segment in representations
  viewportIds.forEach((viewportId) => {
    const representations = getSegmentationRepresentations(viewportId, {
      segmentationId,
    });
 
    representations.forEach((representation) => {
      if (!representation.segments[segmentIndex]) {
        representation.segments[segmentIndex] = {
          visible: true,
        };
      }
    });
  });
 
  viewportIds.forEach((viewportId) => {
    const toolGroup = getToolGroupForViewport(viewportId);
    invalidateBrushCursor(toolGroup.id);
  });
}
 
export { setActiveSegmentIndex, getActiveSegmentIndex };