All files / packages/tools/src/utilities/contours getDeduplicatedVTKPolyDataPoints.ts

0% Statements 0/31
0% Branches 0/16
0% Functions 0/7
0% Lines 0/30

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                                                                                                                                     
/**
 * Iterate through polyData from vtkjs and merge any points that are the same
 * then update merged point references within lines array
 * @param polyData - vtkPolyData
 * @param bypass - bypass the duplicate point removal
 * @returns the updated polyData
 */
export function getDeduplicatedVTKPolyDataPoints(polyData, bypass = false) {
  const points = polyData.getPoints();
  const lines = polyData.getLines();
 
  // Todo: This is cloning which is not ideal, we should move to use the PointsArrayManager
  // that will get merged soon
  const pointsArray = new Array(points.getNumberOfPoints())
    .fill(0)
    .map((_, i) => points.getPoint(i).slice());
 
  const linesArray = new Array(lines.getNumberOfCells()).fill(0).map((_, i) => {
    const cell = lines.getCell(i * 3).slice();
    return { a: cell[0], b: cell[1] };
  });
 
  if (bypass) {
    return { points: pointsArray, lines: linesArray };
  }
 
  const newPoints = [];
  for (const [i, pt] of pointsArray.entries()) {
    // Todo: This is an n^2 algorithm - consider using a Map<string,Point3>.
    // Generates a reasonable amount of garbage, but I think the performance
    //  of that is better than doing repeated compares across the entire array.
    const index = newPoints.findIndex(
      (point) => point[0] === pt[0] && point[1] === pt[1] && point[2] === pt[2]
    );
 
    if (index >= 0) {
      linesArray.map((line) => {
        if (line.a === i) {
          line.a = index;
        }
        if (line.b === i) {
          line.b = index;
        }
        return line;
      });
    } else {
      const newIndex = newPoints.length;
      newPoints.push(pt);
      linesArray.map((line) => {
        if (line.a === i) {
          line.a = newIndex;
        }
        if (line.b === i) {
          line.b = newIndex;
        }
        return line;
      });
    }
  }
 
  const newLines = linesArray.filter((line) => line.a !== line.b);
 
  return { points: newPoints, lines: newLines };
}
 
export default { getDeduplicatedVTKPolyDataPoints };