All files / packages/tools/src/utilities/planar filterAnnotationsForDisplay.ts

84.21% Statements 16/19
62.5% Branches 5/8
100% Functions 2/2
84.21% Lines 16/19

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                                        88x   83x         83x 83x             83x 85x       85x   85x           85x 85x 85x   5x 5x   5x 5x     5x                  
import {
  StackViewport,
  VolumeViewport,
  Types,
  utilities as csUtils,
} from '@cornerstonejs/core';
 
import filterAnnotationsWithinSlice from './filterAnnotationsWithinSlice';
import { Annotations } from '../../types';
 
/**
 * Given the viewport and the annotations, it filters the annotations array and only
 * return those annotation that should be displayed on the viewport
 * @param annotations - Annotations
 * @returns A filtered version of the annotations.
 */
export default function filterAnnotationsForDisplay(
  viewport: Types.IViewport,
  annotations: Annotations
): Annotations {
  if (viewport instanceof StackViewport) {
    // 1. Get the currently displayed imageId from the StackViewport
    const imageId = viewport.getCurrentImageId();
 
    // 2. remove the dataLoader scheme since it might be an annotation that was
    // created on the volumeViewport initially and has the volumeLoader scheme
    // but shares the same imageId
    const colonIndex = imageId.indexOf(':');
    const imageURI = imageId.substring(colonIndex + 1);
 
    // 3. Filter annotation in the frame of reference by the referenced image ID property
    // Note: With the current implementation drawing on the stack (PT stack) will not
    // show the annotation on a volume that does not share the same imageURIs (CT Volume),
    // and we don't have a proper way to check distance either since a stack can be
    // composed of multiple unrelated images
    return annotations.filter((annotation) => {
      Iif (!annotation.isVisible) {
        return false;
      }
 
      const imageId = annotation.metadata.referencedImageId;
 
      Iif (imageId === undefined) {
        // This annotation was not drawn on a non-coplanar reformat, and such does
        // note have a referenced imageId.
        return false;
      }
 
      const colonIndex = imageId.indexOf(':');
      const referenceImageURI = imageId.substring(colonIndex + 1);
      return referenceImageURI === imageURI;
    });
  } else Eif (viewport instanceof VolumeViewport) {
    const camera = viewport.getCamera();
 
    const { spacingInNormalDirection } =
      csUtils.getTargetVolumeAndSpacingInNormalDir(viewport, camera);
 
    // Get data with same normal and within the same slice
    return filterAnnotationsWithinSlice(
      annotations,
      camera,
      spacingInNormalDirection
    );
  } else {
    throw new Error(`Viewport Type ${viewport.type} not supported`);
  }
}