All files / packages/tools/src/stateManagement/annotation annotationState.ts

53.22% Statements 33/62
35% Branches 7/20
47.05% Functions 8/17
53.22% Lines 33/62

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291                                1x             1055x                                                                 422x 422x 422x       6x 6x                                                                                                                     2x                                                                     101x 101x     101x       101x 101x 101x 101x                 101x                                                             90x     90x 90x     90x 14x       76x       76x     76x   76x         76x               335x 335x   335x                               2x   2x 2x   2x                                                
import {
  triggerEvent,
  eventTarget,
  utilities as csUtils,
} from '@cornerstonejs/core';
import { Events } from '../../enums';
import { defaultFrameOfReferenceSpecificAnnotationManager } from './FrameOfReferenceSpecificAnnotationManager';
import { Annotations, Annotation } from '../../types/AnnotationTypes';
import { AnnotationRemovedEventDetail } from '../../types/EventTypes';
import { AnnotationGroupSelector } from '../../types';
import {
  triggerAnnotationAddedForElement,
  triggerAnnotationAddedForFOR,
} from './helpers/state';
 
// our default annotation manager
let defaultManager = defaultFrameOfReferenceSpecificAnnotationManager;
 
/**
 * It returns the default annotations manager.
 * @returns the singleton default annotations manager.
 */
function getAnnotationManager() {
  return defaultManager;
}
 
/**
 * Set the annotation manager to be used for rendering, adding, removing, etc.
 * @param annotationManager - The annotation manager to be used
 */
function setAnnotationManager(annotationManager) {
  defaultManager = annotationManager;
}
 
// set back to default frameOfReferenceSpecificAnnotationManager
function resetAnnotationManager() {
  defaultManager = defaultFrameOfReferenceSpecificAnnotationManager;
}
 
/**
 * Returns the annotations for a given tool with the provided options that is
 * used to filter annotations based on the annotation manager.
 *
 * In our default implementation, the options are the element and/or the FrameOfReferenceUID.
 * Hence, the getAnnotations function will return the annotations for the given tool
 * that are associated with the FrameOfReferenceUID.
 *
 * @param toolName - The name of the tool.
 * @param annotationGroupSelector - element or FrameOfReferenceUID that is used
 * to group annotations in the annotation manager.
 * @returns The annotations corresponding to the Frame of Reference and the toolName.
 */
function getAnnotations(
  toolName: string,
  annotationGroupSelector: AnnotationGroupSelector
): Annotations {
  const manager = getAnnotationManager();
  const groupKey = manager.getGroupKey(annotationGroupSelector);
  return manager.getAnnotations(groupKey, toolName) as Annotations;
}
 
function getAllAnnotations(): Annotations {
  const manager = getAnnotationManager();
  return manager.getAllAnnotations();
}
 
/**
 * Removes the association between the annotation passed as parameter and its
 * parent in case it has one (eg: contour holes).
 * @param annotation - Annotation
 */
function clearParentAnnotation(annotation: Annotation): void {
  const { annotationUID: childUID, parentAnnotationUID } = annotation;
 
  if (!parentAnnotationUID) {
    return;
  }
 
  const parentAnnotation = getAnnotation(parentAnnotationUID);
  const childUIDIndex = parentAnnotation.childAnnotationUIDs.indexOf(childUID);
 
  parentAnnotation.childAnnotationUIDs.splice(childUIDIndex, 1);
  annotation.parentAnnotationUID = undefined;
}
 
/**
 * Creates a parent/child association between annotations.
 * A annotation may have only one parent and multiple children (eg: a contour
 * may have multiple holes in it).
 * @param parentAnnotation - Parent annotation
 * @param childAnnotation - Child annotation
 */
function addChildAnnotation(
  parentAnnotation: Annotation,
  childAnnotation: Annotation
): void {
  const { annotationUID: parentUID } = parentAnnotation;
  const { annotationUID: childUID } = childAnnotation;
 
  // Make sure it is not associated with any other tool
  clearParentAnnotation(childAnnotation);
 
  if (!parentAnnotation.childAnnotationUIDs) {
    parentAnnotation.childAnnotationUIDs = [];
  }
 
  // Check if it is already a child
  if (parentAnnotation.childAnnotationUIDs.includes(childUID)) {
    return;
  }
 
  parentAnnotation.childAnnotationUIDs.push(childUID);
  childAnnotation.parentAnnotationUID = parentUID;
}
 
/**
 * Returns the parent annotation of a given one since annotations can be
 * associated in a parent/child way (eg: polyline holes)
 * @param annotation - Annotation
 * @returns Parent annotation
 */
function getParentAnnotation(annotation: Annotation) {
  return annotation.parentAnnotationUID
    ? getAnnotation(annotation.parentAnnotationUID)
    : undefined;
}
 
/**
 * Returns all children annotation of a given one since annotations can be
 * associated in a parent/child way (eg: polyline holes)
 * @param annotation - Annotation
 * @returns Child annotations
 */
function getChildAnnotations(annotation: Annotation) {
  return (
    annotation.childAnnotationUIDs?.map((childAnnotationUID) =>
      getAnnotation(childAnnotationUID)
    ) ?? []
  );
}
 
/**
 * Add the annotation to the annotation manager along with the options that is
 * used to filter the annotation manager and the annotation group that
 * the annotation belongs to.
 *
 * As a result, our default implementation will add the annotation to the
 * default manager using the FrameOfReferenceUID as the group key.
 *
 * @param annotation - The annotation that is being added to the annotations manager.
 * @param annotationGroupSelector - element or FrameOfReferenceUID that is used
 * to group annotations in the annotation manager.
 */
function addAnnotation(
  annotation: Annotation,
  annotationGroupSelector: AnnotationGroupSelector
): string {
  Eif (!annotation.annotationUID) {
    annotation.annotationUID = csUtils.uuidv4() as string;
  }
 
  const manager = getAnnotationManager();
 
  // if the annotation manager selector is an element, trigger the
  // annotation added event for that element.
  if (annotationGroupSelector instanceof HTMLDivElement) {
    const groupKey = manager.getGroupKey(annotationGroupSelector);
    manager.addAnnotation(annotation, groupKey);
    triggerAnnotationAddedForElement(annotation, annotationGroupSelector);
  } else E{
    // if no element is provided, render all viewports that have the
    // same frame of reference.
    // Todo: we should do something else here for other types of annotation managers.
    manager.addAnnotation(annotation);
    triggerAnnotationAddedForFOR(annotation);
  }
 
  return annotation.annotationUID;
}
 
/**
 * Get the number of annotations for a given tool with the provided options that is
 * used to filter annotations based on the annotation manager.
 *
 * In our default implementation, the options are the element and/or the FrameOfReferenceUID.
 * Hence, the getNumberOfAnnotations function will return the number of annotations for the given tool
 * that are associated with the FrameOfReferenceUID.
 *
 * @param toolName - The name of the tool
 * @param annotationGroupSelector - element or FrameOfReferenceUID that is used
 * to group annotations in the annotation manager.
 *
 */
function getNumberOfAnnotations(
  toolName: string,
  annotationGroupSelector: AnnotationGroupSelector
): number {
  const manager = getAnnotationManager();
  const groupKey = manager.getGroupKey(annotationGroupSelector);
 
  return manager.getNumberOfAnnotations(groupKey, toolName);
}
 
/**
 * Remove the annotation by UID of the annotation.
 * @param annotationUID - The unique identifier for the annotation.
 */
function removeAnnotation(annotationUID: string): void {
  Iif (!annotationUID) {
    return;
  }
  const manager = getAnnotationManager();
  const annotation = manager.getAnnotation(annotationUID);
 
  // no need to continue in case there is no annotation.
  if (!annotation) {
    return;
  }
 
  // Remove all child annotations first
  annotation.childAnnotationUIDs?.forEach((childAnnotationUID) =>
    removeAnnotation(childAnnotationUID)
  );
 
  manager.removeAnnotation(annotationUID);
 
  // trigger annotation removed
  const eventType = Events.ANNOTATION_REMOVED;
 
  const eventDetail: AnnotationRemovedEventDetail = {
    annotation,
    annotationManagerUID: manager.uid,
  };
 
  triggerEvent(eventTarget, eventType, eventDetail);
}
 
/**
 * Get the Annotation object by its UID
 * @param annotationUID - The unique identifier of the annotation.
 */
function getAnnotation(annotationUID: string): Annotation {
  const manager = getAnnotationManager();
  const annotation = manager.getAnnotation(annotationUID);
 
  return annotation;
}
 
/**
 * It removes all annotations from the default annotation manager
 */
function removeAllAnnotations(): void {
  const manager = getAnnotationManager();
  manager.removeAllAnnotations();
}
 
/**
 * Invalidate current and all parent annotations (eg: contour holes)
 * @param annotation - Annotation
 */
function invalidateAnnotation(annotation: Annotation): void {
  let currAnnotation = annotation;
 
  while (currAnnotation) {
    currAnnotation.invalidated = true;
 
    currAnnotation = currAnnotation.parentAnnotationUID
      ? getAnnotation(currAnnotation.parentAnnotationUID)
      : undefined;
  }
}
 
export {
  getAllAnnotations,
  getAnnotations,
  getParentAnnotation,
  getChildAnnotations,
  clearParentAnnotation,
  addChildAnnotation,
  getNumberOfAnnotations,
  addAnnotation,
  getAnnotation,
  removeAnnotation,
  removeAllAnnotations,
  // annotation manager
  setAnnotationManager,
  getAnnotationManager,
  resetAnnotationManager,
  invalidateAnnotation,
};