import macro from 'vtk.js/Sources/macros'; import vtkPolygon from 'vtk.js/Sources/Common/DataModel/Polygon'; import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
const { vtkWarningMacro } = macro;
function vtkTriangleFilter(publicAPI, model) { model.classHierarchy.push('vtkTriangleFilter');
publicAPI.requestData = (inData, outData) => { const input = inData[0]; const points = input.getPoints().getData(); const polys = input.getPolys().getData(); const cellsDataType = input.getPolys().getDataType(); const pointsDataType = input.getPoints().getDataType(); const newCells = []; const newPoints = [];
model.errorCount = 0;
if (polys) { let npts = 0; let isLastPointDuplicated = false; for (let c = 0; c < polys.length; c += npts + 1) { npts = polys[c]; isLastPointDuplicated = polys[c + 1] === polys[c + npts]; if (isLastPointDuplicated) { --npts; }
const cellPoints = []; cellPoints.length = npts; for (let i = 0; i < npts; i++) { const pointId = polys[c + i + 1]; cellPoints[i] = [ points[3 * pointId], points[3 * pointId + 1], points[3 * pointId + 2], ]; }
if (npts === 3) { const newIdStart = newPoints.length / 3; newCells.push(3, newIdStart, newIdStart + 1, newIdStart + 2); newPoints.push(...cellPoints[0], ...cellPoints[1], ...cellPoints[2]); } else if (npts > 3) { const polygon = vtkPolygon.newInstance(); polygon.setPoints(cellPoints);
if (!polygon.triangulate()) { vtkWarningMacro(`Triangulation failed at cellOffset ${c}`); ++model.errorCount; }
const newCellPoints = polygon.getPointArray(); const numSimplices = Math.floor(newCellPoints.length / 9); const triPts = []; triPts.length = 9; for (let i = 0; i < numSimplices; i++) { for (let j = 0; j < 9; j++) { triPts[j] = newCellPoints[9 * i + j]; } const newIdStart = newPoints.length / 3; newCells.push(3, newIdStart, newIdStart + 1, newIdStart + 2); newPoints.push(...triPts); } } if (isLastPointDuplicated) { ++npts; } } }
const dataset = vtkPolyData.newInstance(); dataset .getPoints() .setData(macro.newTypedArrayFrom(pointsDataType, newPoints)); dataset .getPolys() .setData(macro.newTypedArrayFrom(cellsDataType, newCells));
outData[0] = dataset; }; }
const DEFAULT_VALUES = { errorCount: 0, };
export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues);
macro.setGet(publicAPI, model, []); macro.get(publicAPI, model, ['errorCount']);
macro.obj(publicAPI, model);
macro.algo(publicAPI, model, 1, 1);
vtkTriangleFilter(publicAPI, model); }
export const newInstance = macro.newInstance(extend, 'vtkTriangleFilter');
export default { newInstance, extend };
|