All files / polymorphic-segmentation/src/Contour/utils createAndAddContourSegmentationsFromClippedSurfaces.ts

1.92% Statements 2/104
0% Branches 0/34
0% Functions 0/4
2.02% Lines 2/99

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                                                                                                                                                                                                                                                                                                                                                                              428x                                                                                                                                                                                           428x                                      
import { utilities, type Types } from '@cornerstonejs/core';
import {
  PlanarFreehandContourSegmentationTool,
  annotation,
} from '@cornerstonejs/tools';
import type { RawContourData } from '../contourComputationStrategies';
import { vec3 } from 'gl-matrix';
 
/**
 * Finds the best contour from a lines array for normal calculation.
 * Prioritizes contours with more than the specified minimum points for better accuracy.
 *
 * @param lines - Flat array encoding multiple polylines. Format: [pointCount1, id1, id2, ..., pointCount2, id1, id2, ...].
 * @param [minPreferredPoints=3] - The minimum number of points to prefer for better normal calculation accuracy.
 * @returns The best contour information or null if no suitable contour is found.
 */
function findBestContourForNormalCalculation(
  lines: number[],
  minPreferredPoints: number = 3
): {
  contourIndex: number;
  pointCount: number;
  pointIndices: number[];
} | null {
  let bestContourIndex = 0;
  let bestCount = 0;
  let currentIndex = 0;
 
  // Iterate through all contours to find the one with the most points (preferably > minPreferredPoints)
  while (currentIndex < lines.length) {
    const count = lines[currentIndex];
    if (count < 2) {
      currentIndex += count + 1;
      continue;
    }
 
    // Prefer contours with more than minPreferredPoints, otherwise take the largest available
    if (count > minPreferredPoints && bestCount <= minPreferredPoints) {
      bestContourIndex = currentIndex;
      bestCount = count;
      break; // Found a good contour with > minPreferredPoints, use it
    } else if (count > bestCount) {
      bestContourIndex = currentIndex;
      bestCount = count;
    }
 
    currentIndex += count + 1;
  }
 
  if (bestCount < 2) {
    return null;
  }
 
  const pointIndices = lines.slice(
    bestContourIndex + 1,
    bestContourIndex + 1 + bestCount
  );
 
  return {
    contourIndex: bestContourIndex,
    pointCount: bestCount,
    pointIndices,
  };
}
 
/**
 * Checks if a planar polyline lies in a plane that is parallel to a reference plane.
 *
 * This function operates under the assumption that the polyline is flat (all its
 * points lie on a single plane).
 *
 * When multiple contours are present in the lines array, the function searches for
 * the best contour to use for normal calculation, prioritizing contours with more
 * than 3 points for better accuracy in determining the polyline's plane normal.
 *
 * It determines the polyline's own plane normal and checks if that normal is parallel
 * to the given `planeNormal`. When the normals are parallel, the planes are also parallel.
 *
 * If the polyline is collinear (a straight line), it checks if the line's
 * direction is parallel to the `planeNormal`, meaning the line lies in a plane
 * parallel to the reference plane.
 *
 * @param lines - Flat array encoding multiple polylines. Format: [pointCount1, id1, id2, ..., pointCount2, id1, id2, ...].
 * @param points - Flat array of 3D point coordinates [x1, y1, z1, ...].
 * @param planeNormal - The normal vector of the reference plane to check against.
 * @param [dotProductTolerance=0.99] - The minimum absolute value the dot
 *   product can have for the normals to be considered parallel. A value of 1.0 indicates perfect parallelism.
 * @returns True if the polyline lies in a plane parallel to the reference plane.
 */
function isPolylineParallelToPlane(
  lines: number[],
  points: number[],
  planeNormal: vec3,
  dotProductTolerance: number = 0.99
) {
  // Find the best contour for normal calculation (preferably with > 3 points)
  const bestContour = findBestContourForNormalCalculation(lines);
 
  if (!bestContour) {
    // Cannot determine direction or plane.
    return false;
  }
 
  const { pointCount: count, pointIndices: ids } = bestContour;
  const _p_v1 = vec3.create();
  const _p_v2 = vec3.create();
  const _p_p0 = vec3.create();
  const _p_p1 = vec3.create();
  const _p_p2 = vec3.create();
  const _p_polylineNormal = vec3.create();
  const _p_planeNormal = vec3.create();
 
  // --- Find the normal of the plane containing the polyline ---
 
  // Get first point P0.
  vec3.set(
    _p_p0,
    points[ids[0] * 3],
    points[ids[0] * 3 + 1],
    points[ids[0] * 3 + 2]
  );
 
  // Find a second, distinct point P1.
  let p1_found = false;
  for (let i = 1; i < count; i++) {
    vec3.set(
      _p_p1,
      points[ids[i] * 3],
      points[ids[i] * 3 + 1],
      points[ids[i] * 3 + 2]
    );
    if (!vec3.equals(_p_p0, _p_p1)) {
      p1_found = true;
      break;
    }
  }
 
  if (!p1_found) {
    // Only one unique point exists, no direction or plane.
    return false;
  }
 
  // Create first vector for the cross product.
  vec3.subtract(_p_v1, _p_p1, _p_p0);
 
  // Find a third point P2 that is not collinear with P0 and P1.
  let p2_found = false;
  for (let i = 2; i < count; i++) {
    vec3.set(
      _p_p2,
      points[ids[i] * 3],
      points[ids[i] * 3 + 1],
      points[ids[i] * 3 + 2]
    );
    vec3.subtract(_p_v2, _p_p2, _p_p0);
    vec3.cross(_p_polylineNormal, _p_v1, _p_v2);
    // If the cross product has a non-zero length, the points are not collinear.
    if (vec3.length(_p_polylineNormal) > 1e-6) {
      p2_found = true;
      break;
    }
  }
 
  // Normalize the input plane normal for a consistent dot product.
  vec3.normalize(_p_planeNormal, planeNormal);
 
  if (p2_found) {
    // CASE 1: The polyline is a plane. Check if its normal is parallel to the given normal.
    // When normals are parallel, the planes are also parallel.
    vec3.normalize(_p_polylineNormal, _p_polylineNormal);
    const dot = vec3.dot(_p_polylineNormal, _p_planeNormal);
    return Math.abs(dot) >= dotProductTolerance;
  } else {
    // CASE 2: The polyline is a straight line (collinear).
    // The direction is already in _p_v1 (from P1 - P0).
    // Check if its direction is parallel to the given normal.
    // When the line direction is parallel to the normal, the line lies in a plane parallel to the reference plane.
    vec3.normalize(_p_v1, _p_v1);
    const dot = vec3.dot(_p_v1, _p_planeNormal);
    return Math.abs(dot) >= dotProductTolerance;
  }
}
 
const { addAnnotation } = annotation.state;
/**
 * Creates and adds contour segmentations from a clipped surface.
 *
 * @param rawContourData - The raw contour data.
 * @param viewport - The viewport.
 * @param segmentationId - The segmentation ID.
 */
export function createAndAddContourSegmentationsFromClippedSurfaces(
  rawContourData: RawContourData,
  viewport: Types.IViewport,
  segmentationId: string
) {
  const annotationUIDsMap = new Map<number, Set<string>>();
  if (!viewport) {
    console.warn('Invalid viewport given');
    return;
  }
 
  const camera = viewport.getCamera();
  if (!camera) {
    console.warn('Camera not available in viewport');
    return;
  }
  const planeNormal = camera.viewPlaneNormal;
 
  for (const [segmentIndex, contoursData] of rawContourData) {
    for (const contourData of contoursData) {
      const { points, lines } = contourData;
      if (!isPolylineParallelToPlane(lines, points, planeNormal)) {
        continue;
      }
 
      const { lineSegments, linesNumberOfPoints } =
        _extractLineSegments(contourData);
 
      // There may be a few lines as the surface might not be closed and could have holes in it.
      // Currently, we simply render the generated contour as empty fill to indicate
      // the presence of holes. However, filling the proper area with
      //  fillAlpha requires further work.
      for (let i = 0; i < lineSegments.length; i++) {
        const line = lineSegments[i];
        const polyline = [];
 
        for (let j = 0; j < linesNumberOfPoints[i]; j++) {
          const pointIndex = line[j];
          polyline.push([
            points[3 * pointIndex],
            points[3 * pointIndex + 1],
            points[3 * pointIndex + 2],
          ]);
        }
 
        if (polyline.length < 3) {
          continue;
        }
 
        const contourSegmentationAnnotation = {
          annotationUID: utilities.uuidv4(),
          data: {
            contour: {
              closed: true,
              polyline,
            },
            segmentation: {
              segmentationId,
              segmentIndex,
            },
            handles: {},
          },
          handles: {},
          highlighted: false,
          autoGenerated: false,
          invalidated: false,
          isLocked: false,
          isVisible: true,
          metadata: {
            toolName: PlanarFreehandContourSegmentationTool.toolName,
            ...viewport.getViewReference(),
          },
        };
 
        addAnnotation(contourSegmentationAnnotation, viewport.element);
 
        const currentSet = annotationUIDsMap?.get(segmentIndex) || new Set();
        currentSet.add(contourSegmentationAnnotation.annotationUID);
        annotationUIDsMap.set(segmentIndex, currentSet);
      }
    }
  }
 
  return annotationUIDsMap;
}
 
const _extractLineSegments = (contourData) => {
  const { numberOfCells, lines } = contourData;
 
  const lineSegments = [];
  const linesNumberOfPoints = [];
 
  for (let i = 0; i < lines.length; ) {
    const pointsInLine = lines[i];
    linesNumberOfPoints.push(pointsInLine);
    lineSegments.push(lines.slice(i + 1, i + pointsInLine + 1));
    i += pointsInLine + 1;
 
    if (lineSegments.length === numberOfCells) {
      break;
    }
  }
 
  return { lineSegments, linesNumberOfPoints };
};