Skip to content

Commit f666163

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

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import vtkPolyDataNormals from '@kitware/vtk.js/Filters/Core/PolyDataNormals';
1313
// ----------------------------------------------------------------------------
1414

1515
const reader = vtkSTLReader.newInstance();
16+
reader.setRemoveDuplicateVertices(5);
1617
const mapper = vtkMapper.newInstance({ scalarVisibility: false });
1718
const actor = vtkActor.newInstance();
1819

@@ -56,7 +57,6 @@ function handleFile(event) {
5657
const fileReader = new FileReader();
5758
fileReader.onload = function onLoad(e) {
5859
reader.parseAsArrayBuffer(fileReader.result);
59-
reader.removeDuplicateVertices(5);
6060
update();
6161
};
6262
fileReader.readAsArrayBuffer(files[0]);

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export interface vtkSTLReader extends vtkSTLReaderBase {
4646
*/
4747
getUrl(): string;
4848

49+
/**
50+
* Get tolerance when removeDuplicateVertices is set
51+
*/
52+
getRemoveDuplicateVertices(): number;
53+
4954
/**
5055
* Load the object data.
5156
* @param {ISTLReaderOptions} [options]
@@ -76,6 +81,12 @@ export interface vtkSTLReader extends vtkSTLReaderBase {
7681
*/
7782
requestData(inData: any, outData: any): void;
7883

84+
/**
85+
*
86+
* @param tolerance
87+
*/
88+
removeDuplicateVertices(tolerance: number = 5): void;
89+
7990
/**
8091
*
8192
* @param dataAccessHelper
@@ -96,10 +107,10 @@ export interface vtkSTLReader extends vtkSTLReaderBase {
96107
setUrl(url: string, option?: ISTLReaderOptions): Promise<string | any>;
97108

98109
/**
99-
*
100-
* @param offset
110+
* Turn on/off automatic removeDuplicateVertices
111+
* @param {Number} tolerance
101112
*/
102-
removeDuplicateVertices(offset: number = 5): void;
113+
setRemoveDuplicateVertices(tolerance: number): boolean;
103114
}
104115

105116
/**

Sources/IO/Geometry/STLReader/index.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ function vtkSTLReader(publicAPI, model) {
289289

290290
// Add new output
291291
model.output[0] = polydata;
292+
293+
if (model.removeDuplicateVertices > 0) {
294+
publicAPI.removeDuplicateVertices(model.removeDuplicateVertices);
295+
}
292296
};
293297

294298
publicAPI.parseAsText = (content) => {
@@ -326,37 +330,43 @@ function vtkSTLReader(publicAPI, model) {
326330

327331
// Add new output
328332
model.output[0] = polydata;
333+
334+
if (model.removeDuplicateVertices > 0) {
335+
publicAPI.removeDuplicateVertices(model.removeDuplicateVertices);
336+
}
329337
};
330338

331339
publicAPI.requestData = (inData, outData) => {
332340
publicAPI.parse(model.parseData);
333341
};
334342

335-
publicAPI.removeDuplicateVertices = (offset = 5) => {
343+
publicAPI.removeDuplicateVertices = (tolerance = 5) => {
336344
if (!model.output || !model.output[0]) {
337345
console.warn('Load polydata first.');
338346
return;
339347
}
340348
const polydata = model.output[0];
341349

342-
const vertices = polydata.getPoints().getData();
350+
const points = polydata.getPoints().getData();
343351
const faces = polydata.getPolys().getData();
344352

345-
if (!vertices || !faces) {
353+
if (!points || !faces) {
346354
console.warn('No valid polydata.');
347355
return;
348356
}
349357

350358
const vMap = new Map();
351359
const vIndexMap = new Map();
352360
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);
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);
357366
const key = `${k1},${k2},${k3}`;
358367
if (vMap.get(key) !== undefined) {
359368
vIndexMap.set(i / 3, vMap.get(key));
369+
pointsChanged = true;
360370
} else {
361371
vIndexMap.set(i / 3, vInc);
362372
vMap.set(key, vInc);
@@ -365,12 +375,15 @@ function vtkSTLReader(publicAPI, model) {
365375
}
366376

367377
const outVerts = new Float32Array(vMap.size * 3);
368-
vMap.keys().forEach((k) => {
378+
const keys = Array.from(vMap.keys());
379+
for (let i = 0; i < keys.length; i++) {
380+
const k = keys[i];
369381
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-
});
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+
}
374387

375388
const outFaces = new Int32Array(faces);
376389
for (let i = 0; i < faces.length; i += 4) {
@@ -383,7 +396,9 @@ function vtkSTLReader(publicAPI, model) {
383396
polydata.getPoints().setData(outVerts);
384397
polydata.getPolys().setData(outFaces);
385398

386-
publicAPI.modified();
399+
if (pointsChanged) {
400+
publicAPI.modified();
401+
}
387402
};
388403
}
389404

@@ -395,6 +410,7 @@ const DEFAULT_VALUES = {
395410
// baseURL: null,
396411
// dataAccessHelper: null,
397412
// url: null,
413+
removeDuplicateVertices: 0,
398414
};
399415

400416
// ----------------------------------------------------------------------------
@@ -405,7 +421,10 @@ export function extend(publicAPI, model, initialValues = {}) {
405421
// Build VTK API
406422
macro.obj(publicAPI, model);
407423
macro.get(publicAPI, model, ['url', 'baseURL']);
408-
macro.setGet(publicAPI, model, ['dataAccessHelper']);
424+
macro.setGet(publicAPI, model, [
425+
'dataAccessHelper',
426+
'removeDuplicateVertices',
427+
]);
409428
macro.algo(publicAPI, model, 0, 1);
410429

411430
// vtkSTLReader methods

0 commit comments

Comments
 (0)