All files / tools/src/eventListeners/segmentation/labelmap onLabelmapSegmentationDataModified.ts

100% Statements 17/17
87.5% Branches 7/8
100% Functions 4/4
100% Lines 17/17

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                                428x     1353x   1353x   1353x   1353x 1353x 1353x     1353x 2231x 2231x     1353x   1353x 2231x   2231x           1437x             2231x 794x                  
import {
  VolumeViewport,
  getEnabledElementByViewportId,
  StackViewport,
} from '@cornerstonejs/core';
 
import type { SegmentationDataModifiedEventType } from '../../../types/EventTypes';
import { SegmentationRepresentations } from '../../../enums';
import { performVolumeLabelmapUpdate } from './performVolumeLabelmapUpdate';
import { performStackLabelmapUpdate } from './performStackLabelmapUpdate';
import { getSegmentation } from '../../../stateManagement/segmentation/getSegmentation';
import { getViewportIdsWithSegmentation } from '../../../stateManagement/segmentation/getViewportIdsWithSegmentation';
 
/** A callback function that is called when the segmentation data is modified which
 *  often is as a result of tool interactions e.g., scissors, eraser, etc.
 */
const onLabelmapSegmentationDataModified = function (
  evt: SegmentationDataModifiedEventType
): void {
  const { segmentationId, modifiedSlicesToUse } = evt.detail;
 
  const { representationData } = getSegmentation(segmentationId);
 
  const viewportIds = getViewportIdsWithSegmentation(segmentationId);
 
  const hasVolumeViewport = viewportIds.some((viewportId) => {
    const { viewport } = getEnabledElementByViewportId(viewportId);
    return viewport instanceof VolumeViewport;
  });
 
  const hasStackViewport = viewportIds.some((viewportId) => {
    const { viewport } = getEnabledElementByViewportId(viewportId);
    return viewport instanceof StackViewport;
  });
 
  const hasBothStackAndVolume = hasVolumeViewport && hasStackViewport;
 
  viewportIds.forEach((viewportId) => {
    const { viewport } = getEnabledElementByViewportId(viewportId);
 
    if (viewport instanceof VolumeViewport) {
      // For combined stack and volume scenarios in the rendering engine, updating only affected
      // slices is not ideal. Stack indices (e.g., 0 for just one image) don't
      // correspond to image indices in the volume. In this case, we update all slices.
      // However, for volume-only scenarios, we update only affected slices.
 
      performVolumeLabelmapUpdate({
        modifiedSlicesToUse: hasBothStackAndVolume ? [] : modifiedSlicesToUse,
        representationData,
        type: SegmentationRepresentations.Labelmap,
      });
    }
 
    if (viewport instanceof StackViewport) {
      performStackLabelmapUpdate({
        viewportIds,
        segmentationId,
      });
    }
  });
};
 
export default onLabelmapSegmentationDataModified;