All files / packages/tools/src/tools/base ContourBaseTool.ts

55.38% Statements 36/65
50% Branches 7/14
45.45% Functions 5/11
56.45% Lines 35/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                                                          6x                         4x 4x 4x     4x         4x   4x 2x     2x         2x 1x     1x 1x           1x 1x   1x   1x         1x       1x               1x 1x     1x       2x 2x 2x   2x 2x   2x 2x   2x             2x   2x                                                                                         2x                                                                                                             2x   2x                                                                                                                                  
import { getEnabledElement } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
import {
  addAnnotation,
  getAnnotations,
  getChildAnnotations,
} from '../../stateManagement/annotation/annotationState';
import type {
  Annotation,
  ContourAnnotation,
  EventTypes,
  PublicToolProps,
  ToolProps,
  SVGDrawingHelper,
  AnnotationRenderContext,
} from '../../types';
import { drawPath as drawPathSvg } from '../../drawingSvg';
import { StyleSpecifier } from '../../types/AnnotationStyle';
import AnnotationTool from './AnnotationTool';
import { updateContourPolyline } from '../../utilities/contours/';
import { getContourHolesDataCanvas } from '../../utilities/contours';
import { ContourWindingDirection } from '../../types/ContourAnnotation';
 
/**
 * A contour base class responsible for rendering contour instances such as
 * spline, freehand and livewire.
 */
abstract class ContourBaseTool extends AnnotationTool {
  constructor(toolProps: PublicToolProps, defaultToolProps: ToolProps) {
    super(toolProps, defaultToolProps);
  }
 
  /**
   * it is used to draw the annotation in each request animation frame. It
   * calculates the updated cached statistics if data is invalidated and cache it.
   * @param enabledElement - The Cornerstone's enabledElement.
   * @param svgDrawingHelper - The svgDrawingHelper providing the context for drawing.
   */
  public renderAnnotation(
    enabledElement: Types.IEnabledElement,
    svgDrawingHelper: SVGDrawingHelper
  ): boolean {
    let renderStatus = false;
    const { viewport } = enabledElement;
    const { element } = viewport;
 
    // If rendering engine has been destroyed while rendering
    Iif (!viewport.getRenderingEngine()) {
      console.warn('Rendering Engine has been destroyed');
      return renderStatus;
    }
 
    let annotations = getAnnotations(this.getToolName(), element);
 
    if (!annotations?.length) {
      return renderStatus;
    }
 
    annotations = this.filterInteractableAnnotationsForElement(
      element,
      annotations
    );
 
    if (!annotations?.length) {
      return renderStatus;
    }
 
    const targetId = this.getTargetId(viewport);
    const styleSpecifier: StyleSpecifier = {
      toolGroupId: this.toolGroupId,
      toolName: this.getToolName(),
      viewportId: enabledElement.viewport.id,
    };
 
    for (let i = 0; i < annotations.length; i++) {
      const annotation = annotations[i] as Annotation;
 
      styleSpecifier.annotationUID = annotation.annotationUID;
 
      const annotationStyle = this.getAnnotationStyle({
        annotation,
        styleSpecifier,
      });
 
      Iif (!annotationStyle.visibility) {
        continue;
      }
 
      const annotationRendered = this.renderAnnotationInstance({
        enabledElement,
        targetId,
        annotation,
        annotationStyle,
        svgDrawingHelper,
      });
 
      renderStatus ||= annotationRendered;
      annotation.invalidated = false;
    }
 
    return renderStatus;
  }
 
  protected createAnnotation(evt: EventTypes.InteractionEventType): Annotation {
    const eventDetail = evt.detail;
    const { currentPoints, element } = eventDetail;
    const { world: worldPos } = currentPoints;
 
    const enabledElement = getEnabledElement(element);
    const { viewport } = enabledElement;
 
    const camera = viewport.getCamera();
    const { viewPlaneNormal, viewUp, position: cameraPosition } = camera;
 
    const referencedImageId = this.getReferencedImageId(
      viewport,
      worldPos,
      viewPlaneNormal,
      viewUp
    );
 
    const viewReference = viewport.getViewReference({ points: [worldPos] });
 
    return <ContourAnnotation>{
      highlighted: true,
      invalidated: true,
      metadata: {
        toolName: this.getToolName(),
        ...viewReference,
        referencedImageId,
        viewUp,
        cameraPosition,
      },
      data: {
        handles: {
          points: [],
          activeHandleIndex: null,
          textBox: {
            hasMoved: false,
            worldPosition: <Types.Point3>[0, 0, 0],
            worldBoundingBox: {
              topLeft: <Types.Point3>[0, 0, 0],
              topRight: <Types.Point3>[0, 0, 0],
              bottomLeft: <Types.Point3>[0, 0, 0],
              bottomRight: <Types.Point3>[0, 0, 0],
            },
          },
        },
        contour: {
          polyline: [],
          closed: false,
        },
      },
      interpolationUID: '',
      autoGenerated: false,
    };
  }
 
  /**
   * Add the annotation to the annotation manager.
   * @param annotation - Contour annotation that is being added
   * @param element - HTMLDivElement
   */
  protected addAnnotation(
    annotation: Annotation,
    element: HTMLDivElement
  ): string {
    // Just to give a chance for child classes to override it
    return addAnnotation(annotation, element);
  }
 
  /**
   * Cancel an annotation when drawing.
   * @param annotation - Contour annotation that is being added
   * @param element - HTMLDivElement
   */
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  protected cancelAnnotation(annotation: Annotation): void {
    // noop method just to give a chance for child classes to override it
  }
 
  /**
   * Move an annotation and all its child annotations in a recursive way.
   *
   * That is useful when clicking on a spline contour to completely translate
   * it to a different place. In that case all holes (child annotations) must
   * also be translated too.
   *
   * @param annotation - Annotation
   * @param worldPosDelta - Delta in world space
   */
  protected moveAnnotation(
    annotation: Annotation,
    worldPosDelta: Types.Point3
  ): void {
    const { points } = annotation.data.handles;
 
    for (let i = 0, numPoints = points.length; i < numPoints; i++) {
      const point = points[i];
 
      point[0] += worldPosDelta[0];
      point[1] += worldPosDelta[1];
      point[2] += worldPosDelta[2];
    }
 
    annotation.invalidated = true;
 
    getChildAnnotations(annotation).forEach((childAnnotation) =>
      this.moveAnnotation(childAnnotation, worldPosDelta)
    );
  }
 
  protected updateContourPolyline(
    annotation: ContourAnnotation,
    polylineData: {
      points: Types.Point2[];
      closed?: boolean;
      targetWindingDirection?: ContourWindingDirection;
    },
    transforms: {
      canvasToWorld: (point: Types.Point2) => Types.Point3;
    }
  ) {
    const decimateConfig = this.configuration?.decimate || {};
 
    updateContourPolyline(annotation, polylineData, transforms, {
      decimate: {
        enabled: !!decimateConfig.enabled,
        epsilon: decimateConfig.epsilon,
      },
    });
  }
 
  /**
   * Get polyline points in world space.
   * Just to give a chance for child classes to override it.
   * @param annotation - Contour annotation
   * @returns Polyline points in world space
   */
  protected getPolylinePoints(annotation: ContourAnnotation): Types.Point3[] {
    // Attention: `contour.polyline` is the new way to store a polyline but it
    // may be undefined because it was `data.polyline` before (fallback)
    return annotation.data.contour?.polyline ?? annotation.data.polyline;
  }
 
  /**
   * Render a contour segmentation instance
   */
  protected renderAnnotationInstance(
    renderContext: AnnotationRenderContext
  ): boolean {
    const { enabledElement, annotationStyle, svgDrawingHelper } = renderContext;
    const annotation = renderContext.annotation as ContourAnnotation;
 
    // Do not render the contour because it must be rendered by the parent annotation
    if (annotation.parentAnnotationUID) {
      return;
    }
 
    const { annotationUID } = annotation;
    const { viewport } = enabledElement;
    const { worldToCanvas } = viewport;
    const polylineCanvasPoints = this.getPolylinePoints(annotation).map(
      (point) => worldToCanvas(point)
    );
    const { lineWidth, lineDash, color, fillColor, fillOpacity } =
      annotationStyle;
 
    const childContours = getContourHolesDataCanvas(annotation, viewport);
    const allContours = [polylineCanvasPoints, ...childContours];
 
    drawPathSvg(
      svgDrawingHelper,
      annotationUID,
      'contourPolyline',
      allContours,
      {
        color,
        lineDash,
        lineWidth: Math.max(0.1, lineWidth),
        fillColor: fillColor,
        fillOpacity,
      }
    );
 
    return true;
  }
}
 
export { ContourBaseTool as default, ContourBaseTool };