All files / packages/tools/src/tools/annotation/planarFreehandROITool renderMethods.ts

28.57% Statements 28/98
5.88% Branches 2/34
41.66% Functions 5/12
28.57% Lines 28/98

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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431                          1x                       1x               1x         1x   1x                 1x                                                                                                                         1x       1x 1x             1x 640x     1x 1x 1x   1x                                                                                                                                                                                                                                                                                                                                                                                                                                                 1x 1x   1x   1x   1x                                                                                                                     8x 8x 8x 8x     8x     8x   8x   8x        
import type { Types } from '@cornerstonejs/core';
import {
  drawHandles as drawHandlesSvg,
  drawPolyline as drawPolylineSvg,
  drawPath as drawPathSvg,
} from '../../../drawingSvg';
import { polyline } from '../../../utilities/math';
import { findOpenUShapedContourVectorToPeakOnRender } from './findOpenUShapedContourVectorToPeak';
import { PlanarFreehandROIAnnotation } from '../../../types/ToolSpecificAnnotationTypes';
import { StyleSpecifier } from '../../../types/AnnotationStyle';
import { SVGDrawingHelper } from '../../../types';
import { getContourHolesDataCanvas } from '../../../utilities/contours';
 
const { pointsAreWithinCloseContourProximity } = polyline;
 
type PlanarFreehandROIRenderOptions = {
  color?: string;
  width?: number;
  connectFirstToLast?: boolean;
};
 
function _getRenderingOptions(
  enabledElement: Types.IEnabledElement,
  annotation: PlanarFreehandROIAnnotation
): PlanarFreehandROIRenderOptions {
  const styleSpecifier: StyleSpecifier = {
    toolGroupId: this.toolGroupId,
    toolName: this.getToolName(),
    viewportId: enabledElement.viewport.id,
    annotationUID: annotation.annotationUID,
  };
 
  const { lineWidth, lineDash, color, fillColor, fillOpacity } =
    this.getAnnotationStyle({
      annotation,
      styleSpecifier,
    });
 
  const { closed: isClosedContour } = annotation.data.contour;
 
  const options = {
    color,
    width: lineWidth,
    lineDash,
    fillColor,
    fillOpacity,
    closePath: isClosedContour,
  };
 
  return options;
}
 
/**
 * Renders a `PlanarFreehandROIAnnotation` that is not currently being drawn or edited.
 */
function renderContour(
  enabledElement: Types.IEnabledElement,
  svgDrawingHelper: SVGDrawingHelper,
  annotation: PlanarFreehandROIAnnotation
): void {
  // prevent drawing or further calculation in case viewport data is not ready yet
  if (!enabledElement?.viewport?.getImageData()) {
    return;
  }
  // Check if the contour is an open contour
  if (annotation.data.contour.closed) {
    this.renderClosedContour(enabledElement, svgDrawingHelper, annotation);
  } else {
    // If its an open contour, check i its a U-shaped contour
    if (annotation.data.isOpenUShapeContour) {
      calculateUShapeContourVectorToPeakIfNotPresent(
        enabledElement,
        annotation
      );
 
      this.renderOpenUShapedContour(
        enabledElement,
        svgDrawingHelper,
        annotation
      );
    } else {
      // If not a U-shaped contour, render standard open contour.
      this.renderOpenContour(enabledElement, svgDrawingHelper, annotation);
    }
  }
}
 
/**
 * If the open U-shaped contour does not have a peak.
 */
function calculateUShapeContourVectorToPeakIfNotPresent(
  enabledElement: Types.IEnabledElement,
  annotation: PlanarFreehandROIAnnotation
): void {
  if (!annotation.data.openUShapeContourVectorToPeak) {
    // Annotation just been set to be an open U-shaped contour.
    // calculate its peak vector here.
    annotation.data.openUShapeContourVectorToPeak =
      findOpenUShapedContourVectorToPeakOnRender(enabledElement, annotation);
  }
}
 
/**
 * Renders an closed `PlanarFreehandROIAnnotation` annotation.
 */
function renderClosedContour(
  enabledElement: Types.IEnabledElement,
  svgDrawingHelper: SVGDrawingHelper,
  annotation: PlanarFreehandROIAnnotation
): void {
  Iif (annotation.parentAnnotationUID) {
    return;
  }
 
  const { viewport } = enabledElement;
  const options = this._getRenderingOptions(enabledElement, annotation);
 
  // Its unfortunate that we have to do this for each annotation,
  // Even if its unchanged. In the future we could cache the canvas points per
  // element on the tool? That feels very weird also as we'd need to manage
  // it/clean them up. Its a pre-optimisation for now and we can tackle it if it
  // becomes a problem.
  const canvasPolyline = annotation.data.contour.polyline.map((worldPos) =>
    viewport.worldToCanvas(worldPos)
  );
 
  const childContours = getContourHolesDataCanvas(annotation, viewport);
  const allContours = [canvasPolyline, ...childContours];
  const polylineUID = '1';
 
  drawPathSvg(
    svgDrawingHelper,
    annotation.annotationUID,
    polylineUID,
    allContours,
    options
  );
}
 
/**
 * Renders an open `PlanarFreehandROIAnnotation` annotation.
 */
function renderOpenContour(
  enabledElement: Types.IEnabledElement,
  svgDrawingHelper: SVGDrawingHelper,
  annotation: PlanarFreehandROIAnnotation
): void {
  const { viewport } = enabledElement;
  const options = this._getRenderingOptions(enabledElement, annotation);
 
  const canvasPoints = annotation.data.contour.polyline.map((worldPos) =>
    viewport.worldToCanvas(worldPos)
  );
 
  const polylineUID = '1';
 
  drawPolylineSvg(
    svgDrawingHelper,
    annotation.annotationUID,
    polylineUID,
    canvasPoints,
    options
  );
 
  const activeHandleIndex = annotation.data.handles.activeHandleIndex;
 
  if (this.configuration.alwaysRenderOpenContourHandles?.enabled === true) {
    const radius = this.configuration.alwaysRenderOpenContourHandles.radius;
 
    // Draw highlighted points
    const handleGroupUID = '0';
 
    // We already mapped all the points, so don't do the mapping again.
    // The activeHandleIndex can only be one of two points.
    const handlePoints = [
      canvasPoints[0],
      canvasPoints[canvasPoints.length - 1],
    ];
 
    // Don't render a hovered handle, as this will be rendered larger in
    // the next block.
    if (activeHandleIndex === 0) {
      handlePoints.shift();
    } else if (activeHandleIndex === 1) {
      handlePoints.pop();
    }
 
    drawHandlesSvg(
      svgDrawingHelper,
      annotation.annotationUID,
      handleGroupUID,
      handlePoints,
      {
        color: options.color,
        handleRadius: radius,
      }
    );
  }
 
  if (activeHandleIndex !== null) {
    // Draw highlighted points
    const handleGroupUID = '1';
 
    // We already mapped all the points, so don't do the mapping again.
    // The activeHandleIndex can only be one of two points.
    const indexOfCanvasPoints =
      activeHandleIndex === 0 ? 0 : canvasPoints.length - 1;
 
    const handlePoint = canvasPoints[indexOfCanvasPoints];
 
    drawHandlesSvg(
      svgDrawingHelper,
      annotation.annotationUID,
      handleGroupUID,
      [handlePoint],
      { color: options.color }
    );
  }
}
 
function renderOpenUShapedContour(
  enabledElement: Types.IEnabledElement,
  svgDrawingHelper: SVGDrawingHelper,
  annotation: PlanarFreehandROIAnnotation
): void {
  const { viewport } = enabledElement;
  const { openUShapeContourVectorToPeak } = annotation.data;
  const { polyline } = annotation.data.contour;
 
  this.renderOpenContour(enabledElement, svgDrawingHelper, annotation);
 
  // prevent rendering u shape in case openUShapeContourVectorToPeak is not set yet
  if (!openUShapeContourVectorToPeak) {
    return;
  }
 
  const firstCanvasPoint = viewport.worldToCanvas(polyline[0]);
  const lastCanvasPoint = viewport.worldToCanvas(polyline[polyline.length - 1]);
 
  const openUShapeContourVectorToPeakCanvas = [
    viewport.worldToCanvas(openUShapeContourVectorToPeak[0]),
    viewport.worldToCanvas(openUShapeContourVectorToPeak[1]),
  ];
 
  const options = this._getRenderingOptions(enabledElement, annotation);
 
  // Join first and last points
  drawPolylineSvg(
    svgDrawingHelper,
    annotation.annotationUID,
    'first-to-last',
    [firstCanvasPoint, lastCanvasPoint],
    {
      color: options.color,
      width: options.width,
      closePath: false,
      lineDash: '2,2',
    }
  );
 
  // Render midpoint to open contour surface line
  drawPolylineSvg(
    svgDrawingHelper,
    annotation.annotationUID,
    'midpoint-to-open-contour',
    [
      openUShapeContourVectorToPeakCanvas[0],
      openUShapeContourVectorToPeakCanvas[1],
    ],
    {
      color: options.color,
      width: options.width,
      closePath: false,
      lineDash: '2,2',
    }
  );
}
 
/**
 * Renders a new `PlanarFreehandROIAnnotation` annotation during
 * creation/drawing.
 */
function renderContourBeingDrawn(
  enabledElement: Types.IEnabledElement,
  svgDrawingHelper: SVGDrawingHelper,
  annotation: PlanarFreehandROIAnnotation
): void {
  const options = this._getRenderingOptions(enabledElement, annotation);
 
  const { allowOpenContours } = this.configuration;
  const { canvasPoints } = this.drawData;
 
  // Override rendering whilst drawing the contour, we don't know if its open
  // or closed yet
  options.closePath = false;
 
  drawPolylineSvg(
    svgDrawingHelper,
    annotation.annotationUID,
    '1',
    canvasPoints,
    options
  );
 
  if (allowOpenContours) {
    const firstPoint = canvasPoints[0];
    const lastPoint = canvasPoints[canvasPoints.length - 1];
 
    // Check if start and end are within close proximity
    if (
      pointsAreWithinCloseContourProximity(
        firstPoint,
        lastPoint,
        this.configuration.closeContourProximity
      )
    ) {
      // Preview join last points
      drawPolylineSvg(
        svgDrawingHelper,
        annotation.annotationUID,
        '2',
        [lastPoint, firstPoint],
        options
      );
    } else {
      // Draw start point
      const handleGroupUID = '0';
 
      drawHandlesSvg(
        svgDrawingHelper,
        annotation.annotationUID,
        handleGroupUID,
        [firstPoint],
        { color: options.color, handleRadius: 2 }
      );
    }
  }
}
 
/**
 * Renders a closed `PlanarFreehandROIAnnotation` being edited.
 */
function renderClosedContourBeingEdited(
  enabledElement,
  svgDrawingHelper,
  annotation
): void {
  const { viewport } = enabledElement;
  const { fusedCanvasPoints } = this.editData;
 
  Eif (fusedCanvasPoints === undefined) {
    // No edit to render yet, render closed contour.
    this.renderClosedContour(enabledElement, svgDrawingHelper, annotation);
 
    return;
  }
 
  // Get the polylines from child annotations (holes)
  const childContours = getContourHolesDataCanvas(annotation, viewport);
 
  const allContours = [fusedCanvasPoints, ...childContours];
  const options = this._getRenderingOptions(enabledElement, annotation);
  const polylineUIDToRender = 'preview-1';
 
  // Set `fillOpacity` to zero if it is a child annotation (hole) otherwise
  // it would "close" the hole when editing it
  if (annotation.parentAnnotationUID && options.fillOpacity) {
    options.fillOpacity = 0;
  }
 
  drawPathSvg(
    svgDrawingHelper,
    annotation.annotationUID,
    polylineUIDToRender,
    allContours,
    options
  );
}
 
/**
 * Renders an open `PlanarFreehandROIAnnotation` being edited.
 */
function renderOpenContourBeingEdited(
  enabledElement: Types.IEnabledElement,
  svgDrawingHelper: SVGDrawingHelper,
  annotation: PlanarFreehandROIAnnotation
): void {
  const { fusedCanvasPoints } = this.editData;
 
  if (fusedCanvasPoints === undefined) {
    // No edit to render yet, render closed contour.
    this.renderOpenContour(enabledElement, svgDrawingHelper, annotation);
 
    return;
  }
 
  const options = this._getRenderingOptions(enabledElement, annotation);
 
  const polylineUIDToRender = 'preview-1';
 
  drawPolylineSvg(
    svgDrawingHelper,
    annotation.annotationUID,
    polylineUIDToRender,
    fusedCanvasPoints,
    options
  );
}
 
/**
 * Registers the render methods of various contour states to the tool instance.
 */
function registerRenderMethods(toolInstance) {
  toolInstance.renderContour = renderContour.bind(toolInstance);
  toolInstance.renderClosedContour = renderClosedContour.bind(toolInstance);
  toolInstance.renderOpenContour = renderOpenContour.bind(toolInstance);
  toolInstance.renderOpenUShapedContour =
    renderOpenUShapedContour.bind(toolInstance);
 
  toolInstance.renderContourBeingDrawn =
    renderContourBeingDrawn.bind(toolInstance);
 
  toolInstance.renderClosedContourBeingEdited =
    renderClosedContourBeingEdited.bind(toolInstance);
  toolInstance.renderOpenContourBeingEdited =
    renderOpenContourBeingEdited.bind(toolInstance);
  toolInstance._getRenderingOptions = _getRenderingOptions.bind(toolInstance);
}
 
export default registerRenderMethods;