All files / packages/tools/src/utilities/segmentation/InterpolationManager deleteRelatedAnnotations.ts

86.66% Statements 26/30
84.21% Branches 16/19
100% Functions 3/3
86.66% Lines 26/30

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 92 93 94 95 96 97 98                                    3x 3x       3x 3x 3x 3x 6x 2x   4x 4x   4x 2x   2x 2x           3x 3x 6x         4x   2x 2x 2x     2x         3x 1x             1x             3x                                        
import { triggerEvent } from '@cornerstonejs/core';
import * as annotationStateManagement from '../../../stateManagement/annotation';
import interpolate from '../../contours/interpolation/interpolate';
import type { InterpolationViewportData } from '../../../types/InterpolationTypes';
import getInterpolationData from '../../contours/interpolation/getInterpolationData';
import type { InterpolationROIAnnotation } from '../../../types/ToolSpecificAnnotationTypes';
import EventTypes from '../../../enums/Events';
import type { AnnotationInterpolationRemovedEventDetail } from '../../../types/EventTypes';
 
/**
 * deleteRelatedAnnotations - Delete the same interpolation uid on deleting one of them.
 *
 * @param eventData - Object.
 * @returns null
 */
export default function deleteRelatedAnnotations(
  viewportData: InterpolationViewportData
) {
  const { annotation } = viewportData;
  const interpolationAnnotations = getInterpolationData(viewportData, [
    { key: 'interpolationUID', value: viewportData.interpolationUID },
  ]);
 
  const referencedSliceIndex = annotation.metadata.sliceIndex as number;
  let minInterpolation = -1;
  let maxInterpolation = viewportData.sliceData.numberOfSlices;
  for (const [sliceIndex, annotations] of interpolationAnnotations.entries()) {
    if (sliceIndex === referencedSliceIndex) {
      continue;
    }
    const nonInterpolated = annotations.find(
      (annotation) => !annotation.autoGenerated
    );
    if (!nonInterpolated) {
      continue;
    }
    if (sliceIndex < referencedSliceIndex) {
      minInterpolation = Math.max(sliceIndex, minInterpolation);
    } else E{
      maxInterpolation = Math.min(sliceIndex, maxInterpolation);
    }
  }
 
  const removedAnnotations = [];
  for (const [sliceIndex, annotations] of interpolationAnnotations.entries()) {
    if (
      sliceIndex <= minInterpolation ||
      sliceIndex >= maxInterpolation ||
      sliceIndex === referencedSliceIndex
    ) {
      continue;
    }
    annotations.forEach((annotationToDelete) => {
      Eif (annotationToDelete.autoGenerated) {
        annotationStateManagement.state.removeAnnotation(
          annotationToDelete.annotationUID
        );
        removedAnnotations.push(annotationToDelete);
      }
    });
  }
 
  if (removedAnnotations.length) {
    const eventDetails: AnnotationInterpolationRemovedEventDetail = {
      annotations: removedAnnotations,
      element: viewportData.viewport.element,
      viewportId: viewportData.viewport.id,
      renderingEngineId: viewportData.viewport.getRenderingEngine().id,
    };
 
    triggerEvent(
      viewportData.viewport.element,
      EventTypes.INTERPOLATED_ANNOTATIONS_REMOVED,
      eventDetails
    );
  }
 
  Iif (
    minInterpolation >= 0 &&
    maxInterpolation < viewportData.sliceData.numberOfSlices
  ) {
    const nextAnnotation = interpolationAnnotations.get(
      maxInterpolation
    )[0] as InterpolationROIAnnotation;
    // Trigger interpolation for the next non-interpolated annotation
    const viewportNewData: InterpolationViewportData = {
      viewport: viewportData.viewport,
      sliceData: {
        numberOfSlices: viewportData.sliceData.numberOfSlices,
        imageIndex: nextAnnotation.metadata.sliceIndex as number,
      },
      annotation: nextAnnotation,
      interpolationUID: nextAnnotation.interpolationUID,
    };
    interpolate(viewportNewData);
  }
}