@kitware/vtk.js 35.0.0 → 35.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,7 +13,34 @@ interface ISTLReaderOptions {
13
13
  /**
14
14
  *
15
15
  */
16
- export interface ISTLReaderInitialValues {}
16
+ export interface ISTLReaderInitialValues {
17
+ /**
18
+ * Data access helper used for loading remote resources.
19
+ */
20
+ dataAccessHelper?:
21
+ | HtmlDataAccessHelper
22
+ | HttpDataAccessHelper
23
+ | JSZipDataAccessHelper
24
+ | LiteHttpDataAccessHelper;
25
+
26
+ /**
27
+ * Point locator used for vertex merging.
28
+ */
29
+ locator?: any;
30
+
31
+ /**
32
+ * Enable/disable point merging.
33
+ * Default is true.
34
+ */
35
+ merging?: boolean;
36
+
37
+ /**
38
+ * Merge tolerance power.
39
+ * If >= 0, points within 10^-tolerance are merged.
40
+ * Default is -1 (disabled).
41
+ */
42
+ removeDuplicateVertices?: number;
43
+ }
17
44
 
18
45
  type vtkSTLReaderBase = vtkObject &
19
46
  Omit<
@@ -41,6 +68,16 @@ export interface vtkSTLReader extends vtkSTLReaderBase {
41
68
  | JSZipDataAccessHelper
42
69
  | LiteHttpDataAccessHelper;
43
70
 
71
+ /**
72
+ * Get the point locator used for vertex merging.
73
+ */
74
+ getLocator(): any;
75
+
76
+ /**
77
+ * Get whether point merging is enabled.
78
+ */
79
+ getMerging(): boolean;
80
+
44
81
  /**
45
82
  * Get the url of the object to load.
46
83
  */
@@ -74,6 +111,7 @@ export interface vtkSTLReader extends vtkSTLReaderBase {
74
111
  * @param {String} content The content to parse.
75
112
  */
76
113
  parseAsText(content: string): void;
114
+
77
115
  /**
78
116
  *
79
117
  * @param inData
@@ -93,6 +131,16 @@ export interface vtkSTLReader extends vtkSTLReaderBase {
93
131
  | LiteHttpDataAccessHelper
94
132
  ): boolean;
95
133
 
134
+ /**
135
+ * Set a point locator used for vertex merging.
136
+ */
137
+ setLocator(locator: any): boolean;
138
+
139
+ /**
140
+ * Enable/disable point merging.
141
+ */
142
+ setMerging(merging: boolean): boolean;
143
+
96
144
  /**
97
145
  * Set the url of the object to load.
98
146
  * @param {String} url the url of the object to load.
@@ -1,8 +1,10 @@
1
+ import { m as macro } from '../../macros2.js';
1
2
  import BinaryHelper from '../Core/BinaryHelper.js';
2
3
  import DataAccessHelper from '../Core/DataAccessHelper.js';
3
- import { m as macro } from '../../macros2.js';
4
4
  import vtkDataArray from '../../Common/Core/DataArray.js';
5
5
  import vtkMatrixBuilder from '../../Common/Core/MatrixBuilder.js';
6
+ import vtkPoints from '../../Common/Core/Points.js';
7
+ import vtkMergePoints from '../../Common/DataModel/MergePoints.js';
6
8
  import vtkPolyData from '../../Common/DataModel/PolyData.js';
7
9
  import '../Core/DataAccessHelper/LiteHttpDataAccessHelper.js';
8
10
 
@@ -11,7 +13,8 @@ import '../Core/DataAccessHelper/LiteHttpDataAccessHelper.js';
11
13
  // import 'vtk.js/Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper'; // zip
12
14
 
13
15
  const {
14
- vtkErrorMacro
16
+ vtkErrorMacro,
17
+ vtkWarningMacro
15
18
  } = macro;
16
19
  function parseHeader(headerString) {
17
20
  const headerSubStr = headerString.split(' ');
@@ -101,54 +104,83 @@ function vtkSTLReader(publicAPI, model) {
101
104
  progressCallback
102
105
  });
103
106
  }
104
- function removeDuplicateVertices(tolerance) {
107
+ function removeDuplicateVertices(tolerancePower) {
105
108
  const polydata = model.output[0];
106
109
  const points = polydata.getPoints().getData();
107
110
  const faces = polydata.getPolys().getData();
108
111
  if (!points || !faces) {
109
- console.warn('No valid polydata.');
112
+ vtkWarningMacro('No valid polydata.');
110
113
  return;
111
114
  }
112
- const vMap = new Map();
113
- const vIndexMap = new Map();
114
- let vInc = 0;
115
- let pointsChanged = false;
116
- for (let i = 0; i < points.length; i += 3) {
117
- const k1 = (points[i] * 10 ** tolerance).toFixed(0);
118
- const k2 = (points[i + 1] * 10 ** tolerance).toFixed(0);
119
- const k3 = (points[i + 2] * 10 ** tolerance).toFixed(0);
120
- const key = `${k1},${k2},${k3}`;
121
- if (vMap.get(key) !== undefined) {
122
- vIndexMap.set(i / 3, vMap.get(key));
123
- pointsChanged = true;
124
- } else {
125
- vIndexMap.set(i / 3, vInc);
126
- vMap.set(key, vInc);
127
- vInc++;
115
+ const inCellNormals = polydata.getCellData().getNormals();
116
+ const inCellScalars = polydata.getCellData().getScalars();
117
+ const mergedPoints = vtkPoints.newInstance({
118
+ dataType: points.constructor
119
+ });
120
+ mergedPoints.allocate(points.length / 6, 3);
121
+ let locator = model.locator;
122
+ if (!locator) {
123
+ locator = vtkMergePoints.newInstance();
124
+ }
125
+ if (tolerancePower >= 0) {
126
+ locator.setTolerance(10 ** -tolerancePower);
127
+ }
128
+ locator.initPointInsertion(mergedPoints, polydata.getBounds(), Math.floor(points.length / 3 / 2));
129
+ const mergedCells = [];
130
+ const keptCellIds = [];
131
+ for (let i = 0, cellId = 0; i < faces.length; i += 4, cellId++) {
132
+ if (faces[i] === 3) {
133
+ const nodeIds = [0, 0, 0];
134
+ for (let j = 0; j < 3; j++) {
135
+ const pid = faces[i + 1 + j];
136
+ const x = [points[pid * 3], points[pid * 3 + 1], points[pid * 3 + 2]];
137
+ nodeIds[j] = locator.insertUniquePoint(x).id;
138
+ }
139
+ if (nodeIds[0] !== nodeIds[1] && nodeIds[0] !== nodeIds[2] && nodeIds[1] !== nodeIds[2]) {
140
+ mergedCells.push(3, nodeIds[0], nodeIds[1], nodeIds[2]);
141
+ keptCellIds.push(cellId);
142
+ }
128
143
  }
129
144
  }
130
- if (pointsChanged) {
131
- const outVerts = new Float32Array(vMap.size * 3);
132
- const keys = Array.from(vMap.keys());
133
- for (let i = 0; i < keys.length; i++) {
134
- const k = keys[i];
135
- const j = vMap.get(k) * 3;
136
- const coords = k.split(',').map(e => +e * 10 ** -tolerance);
137
- outVerts[j] = coords[0];
138
- outVerts[j + 1] = coords[1];
139
- outVerts[j + 2] = coords[2];
145
+ const outCellArray = new faces.constructor(mergedCells);
146
+ polydata.getPoints().setData(mergedPoints.getData(), 3);
147
+ polydata.getPolys().setData(outCellArray);
148
+ if (inCellNormals && inCellNormals.getData()) {
149
+ const inData = inCellNormals.getData();
150
+ const numComp = inCellNormals.getNumberOfComponents();
151
+ const outData = new inData.constructor(keptCellIds.length * numComp);
152
+ for (let i = 0; i < keptCellIds.length; i++) {
153
+ const srcOffset = keptCellIds[i] * numComp;
154
+ const dstOffset = i * numComp;
155
+ for (let c = 0; c < numComp; c++) {
156
+ outData[dstOffset + c] = inData[srcOffset + c];
157
+ }
140
158
  }
141
- const outFaces = new Int32Array(faces.length);
142
- for (let i = 0; i < faces.length; i += 4) {
143
- outFaces[i] = 3;
144
- outFaces[i + 1] = vIndexMap.get(faces[i + 1]);
145
- outFaces[i + 2] = vIndexMap.get(faces[i + 2]);
146
- outFaces[i + 3] = vIndexMap.get(faces[i + 3]);
159
+ polydata.getCellData().setNormals(vtkDataArray.newInstance({
160
+ name: inCellNormals.getName() || 'Normals',
161
+ values: outData,
162
+ numberOfComponents: numComp
163
+ }));
164
+ }
165
+ if (inCellScalars && inCellScalars.getData()) {
166
+ const inData = inCellScalars.getData();
167
+ const numComp = inCellScalars.getNumberOfComponents();
168
+ const outData = new inData.constructor(keptCellIds.length * numComp);
169
+ for (let i = 0; i < keptCellIds.length; i++) {
170
+ const srcOffset = keptCellIds[i] * numComp;
171
+ const dstOffset = i * numComp;
172
+ for (let c = 0; c < numComp; c++) {
173
+ outData[dstOffset + c] = inData[srcOffset + c];
174
+ }
147
175
  }
148
- polydata.getPoints().setData(outVerts);
149
- polydata.getPolys().setData(outFaces);
150
- polydata.modified();
176
+ polydata.getCellData().setScalars(vtkDataArray.newInstance({
177
+ name: inCellScalars.getName() || 'Attribute',
178
+ values: outData,
179
+ numberOfComponents: numComp
180
+ }));
151
181
  }
182
+ locator.initialize();
183
+ polydata.modified();
152
184
  }
153
185
 
154
186
  // Set DataSet url
@@ -296,7 +328,7 @@ function vtkSTLReader(publicAPI, model) {
296
328
 
297
329
  // Add new output
298
330
  model.output[0] = polydata;
299
- if (model.removeDuplicateVertices >= 0) {
331
+ if (model.merging && model.removeDuplicateVertices >= 0) {
300
332
  removeDuplicateVertices(model.removeDuplicateVertices);
301
333
  }
302
334
  };
@@ -329,7 +361,7 @@ function vtkSTLReader(publicAPI, model) {
329
361
 
330
362
  // Add new output
331
363
  model.output[0] = polydata;
332
- if (model.removeDuplicateVertices >= 0) {
364
+ if (model.merging && model.removeDuplicateVertices >= 0) {
333
365
  removeDuplicateVertices(model.removeDuplicateVertices);
334
366
  }
335
367
  };
@@ -345,7 +377,9 @@ function vtkSTLReader(publicAPI, model) {
345
377
  const DEFAULT_VALUES = {
346
378
  // baseURL: null,
347
379
  // dataAccessHelper: null,
380
+ // locator: null,
348
381
  // url: null,
382
+ merging: true,
349
383
  removeDuplicateVertices: -1
350
384
  };
351
385
 
@@ -357,7 +391,7 @@ function extend(publicAPI, model, initialValues = {}) {
357
391
  // Build VTK API
358
392
  macro.obj(publicAPI, model);
359
393
  macro.get(publicAPI, model, ['url', 'baseURL']);
360
- macro.setGet(publicAPI, model, ['dataAccessHelper', 'removeDuplicateVertices']);
394
+ macro.setGet(publicAPI, model, ['dataAccessHelper', 'locator', 'merging', 'removeDuplicateVertices']);
361
395
  macro.algo(publicAPI, model, 0, 1);
362
396
 
363
397
  // vtkSTLReader methods
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kitware/vtk.js",
3
- "version": "35.0.0",
3
+ "version": "35.1.0",
4
4
  "description": "Visualization Toolkit for the Web",
5
5
  "keywords": [
6
6
  "3d",