Skip to content

Commit 64ab9b7

Browse files
jreyero12floryst
authored andcommitted
fix(STLReader): apply new requested changes in removeDuplicateVertices
1 parent f666163 commit 64ab9b7

File tree

2 files changed

+66
-72
lines changed

2 files changed

+66
-72
lines changed

Sources/IO/Geometry/STLReader/index.d.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ export interface vtkSTLReader extends vtkSTLReaderBase {
8181
*/
8282
requestData(inData: any, outData: any): void;
8383

84-
/**
85-
*
86-
* @param tolerance
87-
*/
88-
removeDuplicateVertices(tolerance: number = 5): void;
89-
9084
/**
9185
*
9286
* @param dataAccessHelper
@@ -108,7 +102,10 @@ export interface vtkSTLReader extends vtkSTLReaderBase {
108102

109103
/**
110104
* Turn on/off automatic removeDuplicateVertices
111-
* @param {Number} tolerance
105+
* After reading the STL file, if `tolerance` is >= 0, then points with the same coordinates at 10 power tolerance are merged.
106+
* For a smooth rendering, you might want to compute normals with vtkPolyDataNormals.
107+
*
108+
* @param {Number} tolerance
112109
*/
113110
setRemoveDuplicateVertices(tolerance: number): boolean;
114111
}

Sources/IO/Geometry/STLReader/index.js

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,64 @@ function vtkSTLReader(publicAPI, model) {
122122
});
123123
}
124124

125+
function removeDuplicateVertices(tolerance) {
126+
const polydata = model.output[0];
127+
128+
const points = polydata.getPoints().getData();
129+
const faces = polydata.getPolys().getData();
130+
131+
if (!points || !faces) {
132+
console.warn('No valid polydata.');
133+
return;
134+
}
135+
136+
const vMap = new Map();
137+
const vIndexMap = new Map();
138+
let vInc = 0;
139+
let pointsChanged = false;
140+
for (let i = 0; i < points.length; i += 3) {
141+
const k1 = (points[i] * 10 ** tolerance).toFixed(0);
142+
const k2 = (points[i + 1] * 10 ** tolerance).toFixed(0);
143+
const k3 = (points[i + 2] * 10 ** tolerance).toFixed(0);
144+
const key = `${k1},${k2},${k3}`;
145+
if (vMap.get(key) !== undefined) {
146+
vIndexMap.set(i / 3, vMap.get(key));
147+
pointsChanged = true;
148+
} else {
149+
vIndexMap.set(i / 3, vInc);
150+
vMap.set(key, vInc);
151+
vInc++;
152+
}
153+
}
154+
155+
const outVerts = new Float32Array(vMap.size * 3);
156+
const keys = Array.from(vMap.keys());
157+
158+
for (let i = 0; i < keys.length; i++) {
159+
const k = keys[i];
160+
const j = vMap.get(k) * 3;
161+
const coords = k.split(',').map((e) => +e * 10 ** -tolerance);
162+
outVerts[j] = coords[0];
163+
outVerts[j + 1] = coords[1];
164+
outVerts[j + 2] = coords[2];
165+
}
166+
167+
const outFaces = new Int32Array(faces);
168+
for (let i = 0; i < faces.length; i += 4) {
169+
outFaces[i] = 3;
170+
outFaces[i + 1] = vIndexMap.get(faces[i + 1]);
171+
outFaces[i + 2] = vIndexMap.get(faces[i + 2]);
172+
outFaces[i + 3] = vIndexMap.get(faces[i + 3]);
173+
}
174+
175+
polydata.getPoints().setData(outVerts);
176+
polydata.getPolys().setData(outFaces);
177+
178+
if (pointsChanged) {
179+
publicAPI.modified();
180+
}
181+
}
182+
125183
// Set DataSet url
126184
publicAPI.setUrl = (url, option = { binary: true }) => {
127185
model.url = url;
@@ -290,8 +348,8 @@ function vtkSTLReader(publicAPI, model) {
290348
// Add new output
291349
model.output[0] = polydata;
292350

293-
if (model.removeDuplicateVertices > 0) {
294-
publicAPI.removeDuplicateVertices(model.removeDuplicateVertices);
351+
if (model.removeDuplicateVertices >= 0) {
352+
removeDuplicateVertices(model.removeDuplicateVertices);
295353
}
296354
};
297355

@@ -331,75 +389,14 @@ function vtkSTLReader(publicAPI, model) {
331389
// Add new output
332390
model.output[0] = polydata;
333391

334-
if (model.removeDuplicateVertices > 0) {
335-
publicAPI.removeDuplicateVertices(model.removeDuplicateVertices);
392+
if (model.removeDuplicateVertices >= 0) {
393+
removeDuplicateVertices(model.removeDuplicateVertices);
336394
}
337395
};
338396

339397
publicAPI.requestData = (inData, outData) => {
340398
publicAPI.parse(model.parseData);
341399
};
342-
343-
publicAPI.removeDuplicateVertices = (tolerance = 5) => {
344-
if (!model.output || !model.output[0]) {
345-
console.warn('Load polydata first.');
346-
return;
347-
}
348-
const polydata = model.output[0];
349-
350-
const points = polydata.getPoints().getData();
351-
const faces = polydata.getPolys().getData();
352-
353-
if (!points || !faces) {
354-
console.warn('No valid polydata.');
355-
return;
356-
}
357-
358-
const vMap = new Map();
359-
const vIndexMap = new Map();
360-
let vInc = 0;
361-
let pointsChanged = false;
362-
for (let i = 0; i < points.length; i += 3) {
363-
const k1 = (points[i] * 10 ** tolerance).toFixed(0);
364-
const k2 = (points[i + 1] * 10 ** tolerance).toFixed(0);
365-
const k3 = (points[i + 2] * 10 ** tolerance).toFixed(0);
366-
const key = `${k1},${k2},${k3}`;
367-
if (vMap.get(key) !== undefined) {
368-
vIndexMap.set(i / 3, vMap.get(key));
369-
pointsChanged = true;
370-
} else {
371-
vIndexMap.set(i / 3, vInc);
372-
vMap.set(key, vInc);
373-
vInc++;
374-
}
375-
}
376-
377-
const outVerts = new Float32Array(vMap.size * 3);
378-
const keys = Array.from(vMap.keys());
379-
for (let i = 0; i < keys.length; i++) {
380-
const k = keys[i];
381-
const j = vMap.get(k) * 3;
382-
const coords = k.split(',').map((e) => +e * 10 ** -tolerance);
383-
outVerts[j] = coords[0];
384-
outVerts[j + 1] = coords[1];
385-
outVerts[j + 2] = coords[2];
386-
}
387-
388-
const outFaces = new Int32Array(faces);
389-
for (let i = 0; i < faces.length; i += 4) {
390-
outFaces[i] = 3;
391-
outFaces[i + 1] = vIndexMap.get(faces[i + 1]);
392-
outFaces[i + 2] = vIndexMap.get(faces[i + 2]);
393-
outFaces[i + 3] = vIndexMap.get(faces[i + 3]);
394-
}
395-
396-
polydata.getPoints().setData(outVerts);
397-
polydata.getPolys().setData(outFaces);
398-
399-
if (pointsChanged) {
400-
publicAPI.modified();
401-
}
402-
};
403400
}
404401

405402
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)