All files / packages/tools/src/utilities/contours/interpolation findAnnotationForInterpolation.ts

84.12% Statements 53/63
64.86% Branches 24/37
100% Functions 6/6
83.05% Lines 49/59

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                                                  6x           6x 6x       6x       6x     6x 7x 7x           7x       7x         6x                                   6x 6x 6x   6x 9x 9x 9x 9x     6x       6x                             6x 9x 9x 6x                                           7x 7x                             7x 7x         7x                                         7x 7x     7x 12x 12x 7x           7x         7x 7x       7x         7x 12x 12x 7x           7x       7x 7x       7x       7x        
import getInterpolationData from './getInterpolationData';
import type { InterpolationViewportData, Annotation } from '../../../types';
 
/**
 * A pair of slice indices for contours, typically indicating the
 * pair of contours to interpolate between, or for a possible range to
 * compare against.
 */
export type ContourPair = [number, number];
 
/**
 * Finds the list of contours to interpolate,
 * including whether they are new contours, or auto generated contours that need
 * to be updated.
 *
 * @param toolData - object, The tool data of the roi contour.
 * @param viewportData - the annotation/viewport to start the interpolation from
 * @returns An object containing the interpolationData and the
 *       interpolationList.
 */
 
function findAnnotationsForInterpolation(
  toolData,
  viewportData: InterpolationViewportData
) {
  const interpolationData = getInterpolationData(viewportData, [
    {
      key: 'interpolationUID',
      value: viewportData.interpolationUID,
    },
  ]);
  const rangeToInterpolate = getRangeToInterpolate(interpolationData);
  Iif (!rangeToInterpolate) {
    console.warn('No annotations found to interpolate', interpolationData);
    return;
  }
  const sliceEdited = _getSlicePositionOfToolData(
    interpolationData,
    toolData.annotationUID
  );
  const interpolationList = [];
 
  // Check if contours between the extent can be interpolated.
  for (let i = rangeToInterpolate[0] + 1; i < rangeToInterpolate[1]; i++) {
    Eif (_sliceNeedsInterpolating(interpolationData, i)) {
      const contourPair = _getBoundingPair(
        i,
        rangeToInterpolate,
        interpolationData
      );
 
      Eif (
        contourPair?.[0] === sliceEdited ||
        contourPair?.[1] === sliceEdited
      ) {
        _appendInterpolationList(contourPair, interpolationList, i);
      }
    }
  }
 
  return {
    interpolationData,
    interpolationList,
  };
}
 
/**
 * Gets the range of interpolation data which has
 * annotations in it, as indices into the interpolationData
 *
 * @param interpolationData - Data on the slice location of contours to be
 *       interpolated.
 * @returns Number[], The slice locations of the top and bottom polygon of the ROIContour.
 */
 
function getRangeToInterpolate(
  interpolationData: Map<number, Annotation[]>
): ContourPair {
  let first = Infinity;
  let last = -Infinity;
  let found = false;
 
  for (const [sliceIndex, annotations] of interpolationData.entries()) {
    Eif (annotations.length) {
      first = Math.min(sliceIndex, first);
      last = Math.max(sliceIndex, last);
      found = true;
    }
  }
  Iif (!found) {
    return;
  }
 
  return [first, last];
}
 
/**
 * _getSlicePositionOfToolData - Finds the slice that was edited.
 *
 * @param interpolationData - Data on the slice location of contours to be
 *       interpolated.
 * @param annotationUID - the UID to find the slice position for
 * @returns index in the interpolationData containing that annotationUID
 */
function _getSlicePositionOfToolData(
  interpolationData: Map<number, Annotation[]>,
  annotationUID: string
): number {
  for (const [sliceIndex, annotations] of interpolationData) {
    for (let j = 0; j < annotations.length; j++) {
      if (annotations[j].annotationUID === annotationUID) {
        return sliceIndex;
      }
    }
  }
 
  return;
}
 
/**
 * Slices need interpolation when either:
 *   * There are no contours on this slice
 *   * There is a contour which is an interpolated contour.
 *
 * @param interpolationData - Data on the slice location of contours to be
 *       interpolated.
 * @param sliceIndex - The slice index.
 * @returns Whether or not the slice needs interpolating.
 */
function _sliceNeedsInterpolating(
  interpolationData: Map<number, Annotation[]>,
  sliceIndex: number
): boolean {
  const annotations = interpolationData.get(sliceIndex);
  return (
    !annotations?.length ||
    (annotations.length === 1 && annotations[0].autoGenerated)
  );
}
 
/**
 * If the contour on slice i can be updated, add it to the
 * interpolationList.
 *
 * @param contourPair - the pair of items to append to
 * @param interpolationList - The list of contours to be interpolated.
 * @param itemIndex - the item index to append data to
 */
function _appendInterpolationList(contourPair, interpolationList, itemIndex) {
  const [startIndex] = contourPair;
  interpolationList[startIndex] ||= {
    pair: contourPair,
    list: [],
  };
 
  interpolationList[startIndex].list.push(itemIndex);
}
 
/**
 * _getBoundingPair - Given the slice index and range of indices to apply the contour to,
 * get the pair of polygons to use for interpolation of the slice.
 * Returns undefined if there is an ambiguity and interpolation can't take place.
 *
 * @param sliceIndex - The slice index.
 * @param sliceRange - The extent of slice occupancy of the ROIContour.
 * @param interpolationData - Data on the slice location of contours to be
 *       interpolated.
 * @returns The pair of slice indices, or undefined if
 *       the contours to use for interpolation is ambiguous.
 */
 
function _getBoundingPair(
  sliceIndex: number,
  sliceRange: ContourPair,
  interpolationData: Map<number, Annotation[]>
): ContourPair {
  const annotationPair = [];
  let canInterpolate = true;
 
  // Check for nearest lowest sliceIndex containing contours.
  for (let i = sliceIndex - 1; i >= sliceRange[0]; i--) {
    const annotations = interpolationData.get(i);
    if (annotations?.length) {
      Iif (annotations[0].autoGenerated) {
        // This contour is interpolated. We need to find a solid contour to
        // interpolate from.
        continue;
      }
 
      Iif (annotations.length > 1) {
        canInterpolate = false;
      }
 
      // Found single, non interpolated contour to interpolate from.
      annotationPair.push(i);
      break;
    }
  }
 
  Iif (!canInterpolate || !annotationPair.length) {
    return;
  }
 
  // Check for nearest upper sliceIndex containing contours.
  for (let i = sliceIndex + 1; i <= sliceRange[1]; i++) {
    const annotations = interpolationData.get(i);
    if (annotations?.length) {
      Iif (annotations[0].autoGenerated) {
        // This contour is interpolated. We need to
        // Find a solid contour to interpolate from.
        continue;
      }
 
      Iif (annotations.length > 1) {
        canInterpolate = false;
      }
 
      annotationPair.push(i);
      break;
    }
  }
 
  Iif (!canInterpolate || annotationPair.length < 2) {
    return;
  }
 
  return annotationPair as ContourPair;
}
 
export default findAnnotationsForInterpolation;