All files / Sources/Filters/General/ContourTriangulator index.js

14.28% Statements 13/91
0% Branches 0/30
40% Functions 2/5
14.77% Lines 13/88

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                                1x   1x 1x                                                                                                                                                                                                                                                                                                                                                                               1x       2x   2x                                                                                                   1x             2x     2x     2x   2x     2x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkCellArray from 'vtk.js/Sources/Common/Core/CellArray';
import vtkPolygon from 'vtk.js/Sources/Common/DataModel/Polygon';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import { VtkDataTypes } from 'vtk.js/Sources/Common/Core/DataArray/Constants';
 
import {
  vtkCCSCutHoleyPolys,
  vtkCCSFindTrueEdges,
  vtkCCSJoinLooseEnds,
  vtkCCSMakeHoleyPolys,
  vtkCCSMakePolysFromLines,
  vtkCCSSplitAtPinchPoints,
  vtkCCSTriangulate,
} from './helper';
 
const { vtkErrorMacro } = macro;
 
const TRIANGULATION_ERROR_DISPLAY = false;
const DIAGNOSE_ON_TRIANGULATION_ERROR = false;
 
//------------------------------------------------------------------------------
function triangulateContours(
  polyData,
  firstLine,
  numLines,
  polys,
  normal,
  triangulatePolys = true
) {
  let triangulationFailure = false;
 
  // If no cut lines were generated, there's nothing to do
  if (numLines <= 0) {
    return false;
  }
 
  const points = polyData.getPoints();
 
  // Join all the new lines into connected groups, i.e. polygons.
  // If we are lucky these will be simple, convex polygons. But
  // we can't count on that.
 
  const newPolys = [];
  const incompletePolys = [];
 
  const oriented = normal?.length < 3;
  vtkCCSMakePolysFromLines(
    polyData,
    firstLine,
    firstLine + numLines,
    oriented,
    newPolys,
    incompletePolys
  );
 
  // if no normal specified, then compute one from largest contour
  let computedNormal = normal;
  if (!oriented) {
    computedNormal = [0, 0, 1];
    let maxnorm = 0;
    const n = [];
    for (let i = 0; i < newPolys.length; i++) {
      const norm = vtkPolygon.getNormal(newPolys[i], points, n);
      if (norm > maxnorm) {
        maxnorm = norm;
        computedNormal[0] = n[0];
        computedNormal[1] = n[1];
        computedNormal[2] = n[2];
      }
    }
  }
 
  // Join any loose ends. If the input was a closed surface then there
  // will not be any loose ends, so this is provided as a service to users
  // who want to clip a non-closed surface.
  vtkCCSJoinLooseEnds(newPolys, incompletePolys, points, computedNormal);
 
  // Some points might be in the middle of straight line segments.
  // These points can be removed without changing the shape of the
  // polys, and removing them makes triangulation more stable.
  // Unfortunately removing these points also means that the polys
  // will no longer form a watertight cap over the cut.
 
  const polyEdges = [];
  const originalEdges = [];
  vtkCCSFindTrueEdges(newPolys, points, polyEdges, originalEdges);
 
  // Next we have to check for polygons with holes, i.e. polygons that
  // have other polygons inside. Each polygon is "grouped" with the
  // polygons that make up its holes.
 
  // Initialize each group to hold just one polygon.
  const numNewPolys = newPolys.length;
  const polyGroups = new Array(numNewPolys);
  for (let i = 0; i < numNewPolys; i++) {
    polyGroups[i] = [i];
  }
 
  // Find out which polys are holes in larger polys. Create a group
  // for each poly where the first member of the group is the larger
  // poly, and all other members are the holes. The number of polyGroups
  // will be the same as the number of polys, and any polys that are
  // holes will have a matching empty group.
 
  vtkCCSMakeHoleyPolys(
    newPolys,
    points,
    polyGroups,
    polyEdges,
    originalEdges,
    computedNormal,
    oriented
  );
 
  // Make cuts to create simple polygons out of the holey polys.
  // After this is done, each polyGroup will have exactly 1 polygon,
  // and no polys will be holes. This is currently the most computationally
  // expensive part of the process.
 
  if (
    !vtkCCSCutHoleyPolys(
      newPolys,
      points,
      polyGroups,
      polyEdges,
      computedNormal
    )
  ) {
    triangulationFailure = true;
  }
 
  // Some polys might be self-intersecting. Split the polys at each intersection point.
  vtkCCSSplitAtPinchPoints(
    newPolys,
    points,
    polyGroups,
    polyEdges,
    computedNormal,
    oriented
  );
 
  // ------ Triangulation code ------
 
  // Go through all polys and triangulate them
  for (let polyId = 0; polyId < polyGroups.length; polyId++) {
    // If group is empty, then poly was a hole without a containing poly
    if (polyGroups[polyId].length === 0) {
      // eslint-disable-next-line no-continue
      continue;
    }
 
    if (!triangulatePolys) {
      polys.insertNextCell(originalEdges.slice(1, originalEdges.length));
    } else if (
      !vtkCCSTriangulate(
        newPolys[polyId],
        points,
        polyEdges[polyId],
        originalEdges,
        polys,
        computedNormal
      )
    ) {
      triangulationFailure = false;
      // Diagnostic code: show the polys as outlines
      if (DIAGNOSE_ON_TRIANGULATION_ERROR) {
        const lines = polyData.getLines();
        const poly = newPolys[polyId];
        lines.insertNextCell([poly.length + 1, ...poly, poly[0]]);
      }
    }
  }
 
  return !triangulationFailure;
}
 
// ---------------------------------------------------
function triangulatePolygon(polygon, points, triangles) {
  const poly = [...polygon];
  const polys = [poly];
 
  const originalEdges = [];
  const polyEdges = [];
  vtkCCSFindTrueEdges(polys, points, polyEdges, originalEdges);
  const edges = polyEdges[0];
 
  let success = true;
  const normal = [];
  const norm = vtkPolygon.getNormal(poly, points, normal);
  if (norm !== 0) {
    success = vtkCCSTriangulate(
      poly,
      points,
      edges,
      originalEdges,
      triangles,
      normal
    );
  }
  return success;
}
 
export const STATIC = { triangulateContours, triangulatePolygon };
 
function vtkContourTriangulator(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkContourTriangulator');
 
  publicAPI.requestData = (inData, outData) => {
    // implement requestData
    const input = inData[0];
    // FIXME: do not instantiate a new polydata each time the filter is executed.
    const output = vtkPolyData.newInstance();
    outData[0] = output;
 
    if (!input) {
      vtkErrorMacro('Invalid or missing input');
      return false;
    }
 
    let triangulationError = false;
 
    const lines = input.getLines();
    if (lines == null || lines.getNumberOfCells() === 0) {
      return true;
    }
 
    input.buildCells();
 
    const polysArray = vtkCellArray.newInstance({
      dataType: VtkDataTypes.DOUBLE,
      empty: true,
    });
    output.setPolys(polysArray);
    output.setPoints(input.getPoints());
    output.getPointData().passData(input.getPointData());
 
    triangulationError = !triangulateContours(
      input,
      input.getNumberOfVerts(),
      lines.getNumberOfCells(),
      polysArray,
      null,
      model.triangulatePolys
    );
 
    if (triangulationError && TRIANGULATION_ERROR_DISPLAY) {
      vtkErrorMacro('Triangulation failed, output might have holes.');
    }
 
    return true;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  triangulatePolys: true,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Make this a VTK object
  macro.obj(publicAPI, model);
 
  // Also make it an algorithm with one input and one output
  macro.algo(publicAPI, model, 1, 1);
 
  macro.setGet(publicAPI, model, ['triangulatePolys']);
 
  // Object specific methods
  vtkContourTriangulator(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkContourTriangulator');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, ...STATIC };