@@ -289,6 +289,10 @@ function vtkSTLReader(publicAPI, model) {
289
289
290
290
// Add new output
291
291
model . output [ 0 ] = polydata ;
292
+
293
+ if ( model . removeDuplicateVertices > 0 ) {
294
+ publicAPI . removeDuplicateVertices ( model . removeDuplicateVertices ) ;
295
+ }
292
296
} ;
293
297
294
298
publicAPI . parseAsText = ( content ) => {
@@ -326,37 +330,43 @@ function vtkSTLReader(publicAPI, model) {
326
330
327
331
// Add new output
328
332
model . output [ 0 ] = polydata ;
333
+
334
+ if ( model . removeDuplicateVertices > 0 ) {
335
+ publicAPI . removeDuplicateVertices ( model . removeDuplicateVertices ) ;
336
+ }
329
337
} ;
330
338
331
339
publicAPI . requestData = ( inData , outData ) => {
332
340
publicAPI . parse ( model . parseData ) ;
333
341
} ;
334
342
335
- publicAPI . removeDuplicateVertices = ( offset = 5 ) => {
343
+ publicAPI . removeDuplicateVertices = ( tolerance = 5 ) => {
336
344
if ( ! model . output || ! model . output [ 0 ] ) {
337
345
console . warn ( 'Load polydata first.' ) ;
338
346
return ;
339
347
}
340
348
const polydata = model . output [ 0 ] ;
341
349
342
- const vertices = polydata . getPoints ( ) . getData ( ) ;
350
+ const points = polydata . getPoints ( ) . getData ( ) ;
343
351
const faces = polydata . getPolys ( ) . getData ( ) ;
344
352
345
- if ( ! vertices || ! faces ) {
353
+ if ( ! points || ! faces ) {
346
354
console . warn ( 'No valid polydata.' ) ;
347
355
return ;
348
356
}
349
357
350
358
const vMap = new Map ( ) ;
351
359
const vIndexMap = new Map ( ) ;
352
360
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 ) ;
357
366
const key = `${ k1 } ,${ k2 } ,${ k3 } ` ;
358
367
if ( vMap . get ( key ) !== undefined ) {
359
368
vIndexMap . set ( i / 3 , vMap . get ( key ) ) ;
369
+ pointsChanged = true ;
360
370
} else {
361
371
vIndexMap . set ( i / 3 , vInc ) ;
362
372
vMap . set ( key , vInc ) ;
@@ -365,12 +375,15 @@ function vtkSTLReader(publicAPI, model) {
365
375
}
366
376
367
377
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 ] ;
369
381
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
+ }
374
387
375
388
const outFaces = new Int32Array ( faces ) ;
376
389
for ( let i = 0 ; i < faces . length ; i += 4 ) {
@@ -383,7 +396,9 @@ function vtkSTLReader(publicAPI, model) {
383
396
polydata . getPoints ( ) . setData ( outVerts ) ;
384
397
polydata . getPolys ( ) . setData ( outFaces ) ;
385
398
386
- publicAPI . modified ( ) ;
399
+ if ( pointsChanged ) {
400
+ publicAPI . modified ( ) ;
401
+ }
387
402
} ;
388
403
}
389
404
@@ -395,6 +410,7 @@ const DEFAULT_VALUES = {
395
410
// baseURL: null,
396
411
// dataAccessHelper: null,
397
412
// url: null,
413
+ removeDuplicateVertices : 0 ,
398
414
} ;
399
415
400
416
// ----------------------------------------------------------------------------
@@ -405,7 +421,10 @@ export function extend(publicAPI, model, initialValues = {}) {
405
421
// Build VTK API
406
422
macro . obj ( publicAPI , model ) ;
407
423
macro . get ( publicAPI , model , [ 'url' , 'baseURL' ] ) ;
408
- macro . setGet ( publicAPI , model , [ 'dataAccessHelper' ] ) ;
424
+ macro . setGet ( publicAPI , model , [
425
+ 'dataAccessHelper' ,
426
+ 'removeDuplicateVertices' ,
427
+ ] ) ;
409
428
macro . algo ( publicAPI , model , 0 , 1 ) ;
410
429
411
430
// vtkSTLReader methods
0 commit comments