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

69.23% Statements 18/26
50% Branches 10/20
75% Functions 3/4
68% Lines 17/25

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 87 88 89 90 91                                230x     230x         230x               230x   20x   20x 20x 20x 12x 12x         230x 16x     230x                     230x     230x 230x     230x                                              
import type {
  RenderingConfig,
  RepresentationPublicInput,
} from '../../types/SegmentationStateTypes';
import CORNERSTONE_COLOR_LUT from '../../constants/COLOR_LUT';
import { triggerAnnotationRenderForViewportIds } from '../../utilities/triggerAnnotationRenderForViewportIds';
import { SegmentationRepresentations } from '../../enums';
import { triggerSegmentationModified } from './triggerSegmentationEvents';
import { addColorLUT } from './addColorLUT';
import { defaultSegmentationStateManager } from './SegmentationStateManager';
import { getActiveSegmentIndex, setActiveSegmentIndex } from './segmentIndex';
 
function internalAddSegmentationRepresentation(
  viewportId: string,
  representationInput: RepresentationPublicInput
) {
  const { segmentationId, config } = representationInput;
 
  // need to be able to override from the outside
  const renderingConfig: RenderingConfig = {
    colorLUTIndex: getColorLUTIndex(config),
    ...config,
  };
 
  defaultSegmentationStateManager.addSegmentationRepresentation(
    viewportId,
    segmentationId,
    representationInput.type,
    renderingConfig
  );
 
  // If no active segment index is set, default to the first segment
  if (!getActiveSegmentIndex(segmentationId)) {
    const segmentation =
      defaultSegmentationStateManager.getSegmentation(segmentationId);
 
    Eif (segmentation) {
      const segmentKeys = Object.keys(segmentation.segments);
      if (segmentKeys.length > 0) {
        const firstSegmentIndex = segmentKeys.map((k) => Number(k)).sort()[0];
        setActiveSegmentIndex(segmentationId, firstSegmentIndex);
      }
    }
  }
 
  if (representationInput.type === SegmentationRepresentations.Contour) {
    triggerAnnotationRenderForViewportIds([viewportId]);
  }
 
  triggerSegmentationModified(segmentationId);
}
 
/**
 * Retrieves or adds a Color Lookup Table (LUT) index based on the provided configuration.
 *
 * @param config - Configuration object containing colorLUTOrIndex.
 * @returns The index of the Color LUT to be used.
 */
function getColorLUTIndex(config: RepresentationPublicInput['config']): number {
  // Destructure colorLUTOrIndex from the config, with a fallback to undefined if config is undefined
  const { colorLUTOrIndex } = config || {};
 
  // If no colorLUTOrIndex provided, get next available index and add default LUT
  Eif (colorLUTOrIndex === undefined) {
    const index = addColorLUT(
      JSON.parse(JSON.stringify(CORNERSTONE_COLOR_LUT))
    );
    return index;
  }
 
  // If numeric index provided, return it directly
  if (typeof colorLUTOrIndex === 'number') {
    return colorLUTOrIndex;
  }
 
  // If colorLUTOrIndex is a ColorLUT array, add it with a new index
  if (
    Array.isArray(colorLUTOrIndex) &&
    colorLUTOrIndex.every((item) => Array.isArray(item) && item.length === 4)
  ) {
    const index = addColorLUT(colorLUTOrIndex);
    return index;
  }
 
  // Fallback: use default LUT with next available index
  const index = addColorLUT(JSON.parse(JSON.stringify(CORNERSTONE_COLOR_LUT)));
  return index;
}
 
export { internalAddSegmentationRepresentation };