All files / tools/src/utilities annotationHydration.ts

33.33% Statements 13/39
18.75% Branches 3/16
44.44% Functions 4/9
31.57% Lines 12/38

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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                                                                                                                                                                                                                            32x 32x       32x 4320x 4320x         4320x     13400x   32x               4320x 4320x   4320x   4320x      
import type { Types } from '@cornerstonejs/core';
import {
  utilities,
  BaseVolumeViewport,
  StackViewport,
  cache,
  metaData,
} from '@cornerstonejs/core';
import type { Annotation } from '../types';
import { addAnnotation } from '../stateManagement/annotation/annotationState';
import { vec3 } from 'gl-matrix';
 
function annotationHydration(
  viewport: Types.IViewport,
  toolName: string,
  worldPoints: Types.Point3[],
  options?: {
    FrameOfReferenceUID?: string;
    annotationUID?: string;
  }
): Annotation {
  const viewReference = viewport.getViewReference();
  const { viewPlaneNormal, FrameOfReferenceUID } = viewReference;
  const annotation = {
    annotationUID: options?.annotationUID || utilities.uuidv4(),
    data: {
      handles: {
        points: worldPoints,
      },
    },
    highlighted: false,
    autoGenerated: false,
    invalidated: false,
    isLocked: false,
    isVisible: true,
    metadata: {
      toolName,
      viewPlaneNormal,
      FrameOfReferenceUID,
      referencedImageId: getReferencedImageId(
        viewport,
        worldPoints[0],
        viewPlaneNormal
      ),
      ...options,
    },
  };
  addAnnotation(annotation, viewport.element);
  return annotation;
}
 
function getReferencedImageId(
  viewport: Types.IViewport,
  worldPos: Types.Point3,
  viewPlaneNormal: Types.Point3
): string {
  let referencedImageId;
 
  if (viewport instanceof StackViewport) {
    referencedImageId = getClosestImageIdForStackViewport(
      viewport,
      worldPos,
      viewPlaneNormal
    );
  } else if (viewport instanceof BaseVolumeViewport) {
    const targetId = getTargetId(viewport);
    const volumeId = utilities.getVolumeId(targetId);
    const imageVolume = cache.getVolume(volumeId);
 
    referencedImageId = utilities.getClosestImageId(
      imageVolume,
      worldPos,
      viewPlaneNormal
    );
  } else {
    throw new Error(
      'getReferencedImageId: viewport must be a StackViewport or BaseVolumeViewport'
    );
  }
 
  return referencedImageId;
}
 
function getTargetId(viewport: Types.IViewport): string | undefined {
  const targetId = viewport.getViewReferenceId?.();
  if (targetId) {
    return targetId;
  }
  if (viewport instanceof BaseVolumeViewport) {
    return `volumeId:${getTargetVolumeId(viewport)}`;
  }
  throw new Error('getTargetId: viewport must have a getTargetId method');
}
 
function getTargetVolumeId(viewport: Types.IViewport): string | undefined {
  const actorEntries = viewport.getActors();
 
  if (!actorEntries) {
    return;
  }
  return actorEntries.find(
    (actorEntry) => actorEntry.actor.getClassName() === 'vtkVolume'
  )?.uid;
}
 
function getClosestImageIdForStackViewport(
  viewport: StackViewport,
  worldPos: Types.Point3,
  viewPlaneNormal: Types.Point3
): string {
  const imageIds = viewport.getImageIds();
  Iif (!imageIds || !imageIds.length) {
    return;
  }
 
  const distanceImagePairs = imageIds.map((imageId) => {
    const { imagePositionPatient } = metaData.get('imagePlaneModule', imageId);
    const distance = calculateDistanceToImage(
      worldPos,
      imagePositionPatient,
      viewPlaneNormal
    );
    return { imageId, distance };
  });
 
  distanceImagePairs.sort((a, b) => a.distance - b.distance);
 
  return distanceImagePairs[0].imageId;
}
 
function calculateDistanceToImage(
  worldPos: Types.Point3,
  ImagePositionPatient: Types.Point3,
  viewPlaneNormal: Types.Point3
): number {
  const dir = vec3.create();
  vec3.sub(dir, worldPos, ImagePositionPatient);
 
  const dot = vec3.dot(dir, viewPlaneNormal);
 
  return Math.abs(dot);
}
export { annotationHydration, getClosestImageIdForStackViewport };