Skip to content

Commit aa72845

Browse files
jreyero12floryst
authored andcommitted
feat(stlreader): add removeDuplicateVertices method to vtkSTLReader
1 parent 413f07a commit aa72845

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

Sources/IO/Geometry/STLReader/example/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
77
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow';
88
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
99
import vtkSTLReader from '@kitware/vtk.js/IO/Geometry/STLReader';
10-
10+
import vtkPolyDataNormals from '@kitware/vtk.js/Filters/Core/PolyDataNormals';
1111
// ----------------------------------------------------------------------------
1212
// Example code
1313
// ----------------------------------------------------------------------------
@@ -17,7 +17,9 @@ const mapper = vtkMapper.newInstance({ scalarVisibility: false });
1717
const actor = vtkActor.newInstance();
1818

1919
actor.setMapper(mapper);
20-
mapper.setInputConnection(reader.getOutputPort());
20+
const normals = vtkPolyDataNormals.newInstance();
21+
normals.setInputConnection(reader.getOutputPort());
22+
mapper.setInputConnection(normals.getOutputPort());
2123

2224
// ----------------------------------------------------------------------------
2325

@@ -54,6 +56,7 @@ function handleFile(event) {
5456
const fileReader = new FileReader();
5557
fileReader.onload = function onLoad(e) {
5658
reader.parseAsArrayBuffer(fileReader.result);
59+
reader.removeDuplicateVertices(5);
5760
update();
5861
};
5962
fileReader.readAsArrayBuffer(files[0]);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ export interface vtkSTLReader extends vtkSTLReaderBase {
9494
* @param {ISTLReaderOptions} [option] The STL reader options.
9595
*/
9696
setUrl(url: string, option?: ISTLReaderOptions): Promise<string | any>;
97+
98+
/**
99+
*
100+
* @param offset
101+
*/
102+
removeDuplicateVertices(offset: number = 5): void;
97103
}
98104

99105
/**

Sources/IO/Geometry/STLReader/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,60 @@ function vtkSTLReader(publicAPI, model) {
331331
publicAPI.requestData = (inData, outData) => {
332332
publicAPI.parse(model.parseData);
333333
};
334+
335+
publicAPI.removeDuplicateVertices = (offset = 5) => {
336+
if (!model.output || !model.output[0]) {
337+
console.warn('Load polydata first.');
338+
return;
339+
}
340+
const polydata = model.output[0];
341+
342+
const vertices = polydata.getPoints().getData();
343+
const faces = polydata.getPolys().getData();
344+
345+
if (!vertices || !faces) {
346+
console.warn('No valid polydata.');
347+
return;
348+
}
349+
350+
const vMap = new Map();
351+
const vIndexMap = new Map();
352+
let vInc = 0;
353+
for (let i = 0; i < vertices.length; i += 3) {
354+
const k1 = (vertices[i] * 10 ** offset).toFixed(0);
355+
const k2 = (vertices[i + 1] * 10 ** offset).toFixed(0);
356+
const k3 = (vertices[i + 2] * 10 ** offset).toFixed(0);
357+
const key = `${k1},${k2},${k3}`;
358+
if (vMap.get(key) !== undefined) {
359+
vIndexMap.set(i / 3, vMap.get(key));
360+
} else {
361+
vIndexMap.set(i / 3, vInc);
362+
vMap.set(key, vInc);
363+
vInc++;
364+
}
365+
}
366+
367+
const outVerts = new Float32Array(vMap.size * 3);
368+
vMap.keys().forEach((k) => {
369+
const j = vMap.get(k) * 3;
370+
[outVerts[j], outVerts[j + 1], outVerts[j + 2]] = k
371+
.split(',')
372+
.map((e) => +e * 10 ** -offset);
373+
});
374+
375+
const outFaces = new Int32Array(faces);
376+
for (let i = 0; i < faces.length; i += 4) {
377+
outFaces[i] = 3;
378+
outFaces[i + 1] = vIndexMap.get(faces[i + 1]);
379+
outFaces[i + 2] = vIndexMap.get(faces[i + 2]);
380+
outFaces[i + 3] = vIndexMap.get(faces[i + 3]);
381+
}
382+
383+
polydata.getPoints().setData(outVerts);
384+
polydata.getPolys().setData(outFaces);
385+
386+
publicAPI.modified();
387+
};
334388
}
335389

336390
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)