All files / packages/tools/src/utilities/planarFreehandROITool smoothPoints.ts

6.66% Statements 3/45
8.82% Branches 3/34
11.11% Functions 1/9
6.97% Lines 3/43

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                2x       2x     2x                                                                                                                                                                                                                                                                                                                                                                                                                            
import { Types } from '@cornerstonejs/core';
import { point } from '../math';
import interpolateSegmentPoints from './interpolation/interpolateSegmentPoints';
 
export function shouldSmooth(
  configuration: Record<any, any>,
  annotation?
): boolean {
  Iif (annotation?.autoGenerated) {
    return false;
  }
  const shouldSmooth =
    configuration?.smoothing?.smoothOnAdd === true ||
    configuration?.smoothing?.smoothOnEdit === true;
 
  return shouldSmooth;
}
 
/**
 * Tells whether two points are equal by proximity or not as far as interpolation goes.
 */
function isEqualByProximity(pointA, pointB) {
  return point.distanceToPoint(pointA, pointB) < 0.001;
}
 
/**
 * Tells whether two points are strictly equal or not as far as interpolation goes.
 */
function isEqual(pointA, pointB) {
  return point.distanceToPoint(pointA, pointB) === 0;
}
 
/**
 * Finds the indexes of points list and otherPoints list that points are identical.
 */
function findMatchIndexes(
  points: Types.Point2[],
  otherPoints: Types.Point2[]
): [number, number] | undefined {
  for (let i = 0; i < points.length; i++) {
    for (let j = 0; j < otherPoints.length; j++) {
      if (isEqual(points[i], otherPoints[j])) {
        return [i, j];
      }
    }
  }
}
/**
 * Returns the following index value (on circular basis) of index param on the given direction.
 */
function followingIndex(
  index: number,
  size: number,
  direction: number
): number {
  return (index + size + direction) % size;
}
/**
 * Array of params to be used on circular find next index.
 * The values respresent start index, indexDelimiter, list of points
 */
type ListParamsType = [number, number, Types.Point2[]];
 
/**
 * Circular finding that returns the next index for two list where the criteria is met.
 *
 * It can compare two lists out of sync considering it does a circular iteration over them.
 *
 * @example
 *
 * ```
 * const pointsA = [[0, 1], [1, 3], [1, 5], [1,2]];
 * const pointsB = [[1, 2], [1, 5], [1, 3], [0,0]];
 * let firstParam = [0, 0, pointsA]
 * let secondParam = [1, 1, pointsB]
 * const criteria = (pointA, pointB) => areSamePosition(pointA, pointB)
 * const direction = 1;
 * let result = circularFindNextIndexBy(firstParam, secondParam, criteria,direction);
 * console.log(result);
 * // prints [1, 2]
 * // use this result and find again
 * firstParam = [result[0]+1, result[0], pointsA]
 * secondParam = [result[1]+1, result[1], pointsB]
 * result = circularFindNextIndexBy(firstParam, secondParam, criteria,direction);
 * * // prints [3, 0]
 *
 */
function circularFindNextIndexBy(
  listParams: ListParamsType,
  otherListParams: ListParamsType,
  criteria: (pointA: Types.Point2, pointB: Types.Point2) => boolean,
  direction: number
): [number | undefined, number | undefined] {
  const [, indexDelimiter, points] = listParams;
  const [, otherIndexDelimiter, otherPoints] = otherListParams;
 
  const pointsLength = points.length;
  const otherPointsLength = otherPoints.length;
 
  let startIndex = listParams[0];
  let otherStartIndex = otherListParams[0];
 
  if (
    !points[startIndex] ||
    !otherPoints[otherStartIndex] ||
    !points[indexDelimiter] ||
    !otherPoints[otherIndexDelimiter]
  ) {
    return [undefined, undefined];
  }
 
  while (
    startIndex !== indexDelimiter &&
    otherStartIndex !== otherIndexDelimiter
  ) {
    if (criteria(otherPoints[otherStartIndex], points[startIndex])) {
      return [startIndex, otherStartIndex];
    }
 
    startIndex = followingIndex(startIndex, pointsLength, direction);
    otherStartIndex = followingIndex(
      otherStartIndex,
      otherPointsLength,
      direction
    );
  }
 
  return [undefined, undefined];
}
 
/**
 * Given two list it will find the first and last index of segment from points that diverges from previousPoints
 */
function findChangedSegment(
  points: Types.Point2[],
  previousPoints: Types.Point2[]
): [number, number] {
  const [firstMatchIndex, previousFirstMatchIndex] =
    findMatchIndexes(points, previousPoints) || [];
 
  const toBeNotEqualCriteria = (pointA, pointB) =>
    isEqualByProximity(pointA, pointB) === false;
 
  const [lowDiffIndex, lowOtherDiffIndex] = circularFindNextIndexBy(
    [
      followingIndex(firstMatchIndex, points.length, 1),
      firstMatchIndex,
      points,
    ],
    [
      followingIndex(previousFirstMatchIndex, previousPoints.length, 1),
      previousFirstMatchIndex,
      previousPoints,
    ],
    toBeNotEqualCriteria,
    1
  );
 
  const [highIndex] = circularFindNextIndexBy(
    [followingIndex(lowDiffIndex, points.length, -1), lowDiffIndex, points],
    [
      followingIndex(lowOtherDiffIndex, previousPoints.length, -1),
      lowOtherDiffIndex,
      previousPoints,
    ],
    toBeNotEqualCriteria,
    -1
  );
 
  return [lowDiffIndex, highIndex];
}
 
/**
 * Interpolates the given list of points. In case there is a pointsOfReference the interpolation will occur only on segment disjoint of two list. I.e list of points from param points that are not on list of points from param pointsOfReference.
 */
export function getInterpolatedPoints(
  configuration: Record<any, any>,
  points: Types.Point2[],
  pointsOfReference?: Types.Point2[]
): Types.Point2[] {
  const { interpolation, smoothing } = configuration;
 
  const result = points;
 
  if (interpolation) {
    const {
      knotsRatioPercentageOnAdd,
      knotsRatioPercentageOnEdit,
      smoothOnAdd = false,
      smoothOnEdit = false,
    } = smoothing;
 
    const knotsRatioPercentage = pointsOfReference
      ? knotsRatioPercentageOnEdit
      : knotsRatioPercentageOnAdd;
    const isEnabled = pointsOfReference ? smoothOnEdit : smoothOnAdd;
 
    if (isEnabled) {
      // partial or total interpolation
      const [changedIniIndex, changedEndIndex] = pointsOfReference
        ? findChangedSegment(points, pointsOfReference)
        : [0, points.length - 1];
 
      // do not interpolate if there is no valid segment
      if (!points[changedIniIndex] || !points[changedEndIndex]) {
        return points;
      }
 
      return <Types.Point2[]>(
        interpolateSegmentPoints(
          points,
          changedIniIndex,
          changedEndIndex,
          knotsRatioPercentage
        )
      );
    }
  }
 
  return result;
}