|
1 | 1 | import * as macro from 'vtk.js/Sources/macros';
|
| 2 | +import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray'; |
2 | 3 | import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
|
3 | 4 |
|
4 | 5 | const { vtkErrorMacro } = macro;
|
@@ -75,10 +76,18 @@ function vtkCutter(publicAPI, model) {
|
75 | 76 | function dataSetCutter(input, output) {
|
76 | 77 | const points = input.getPoints();
|
77 | 78 | const pointsData = points.getData();
|
| 79 | + const pointData = input.getPointData(); |
78 | 80 | const numPts = points.getNumberOfPoints();
|
79 | 81 | const newPointsData = [];
|
80 | 82 | const newLinesData = [];
|
81 | 83 | const newPolysData = [];
|
| 84 | + const newPointData = {}; // TODO: cell data must also be processed |
| 85 | + |
| 86 | + // Initialize arrays |
| 87 | + const numberOfArrays = pointData.getNumberOfArrays(); |
| 88 | + for (let arrayIdx = 0; arrayIdx < numberOfArrays; arrayIdx++) { |
| 89 | + newPointData[pointData.getArrayName(arrayIdx)] = []; |
| 90 | + } |
82 | 91 |
|
83 | 92 | if (!model.cutScalars || model.cutScalars.length < numPts) {
|
84 | 93 | model.cutScalars = new Float32Array(numPts);
|
@@ -181,11 +190,27 @@ function vtkCutter(publicAPI, model) {
|
181 | 190 | x1[2] + t * (x2[2] - x1[2]),
|
182 | 191 | ];
|
183 | 192 |
|
| 193 | + const computedIntersectedArrays = {}; |
| 194 | + for (let arrayIdx = 0; arrayIdx < numberOfArrays; arrayIdx++) { |
| 195 | + const array = pointData.getArrayByIndex(arrayIdx); |
| 196 | + const name = pointData.getArrayName(arrayIdx); |
| 197 | + const data = array.getData(); |
| 198 | + const n = array.getNumberOfComponents(); |
| 199 | + const computedIntersectedArray = new Array(n); |
| 200 | + for (let j = 0; j < n; j++) { |
| 201 | + const scalar1 = data[n * pointID1 + j]; |
| 202 | + const scalar2 = data[n * pointID2 + j]; |
| 203 | + computedIntersectedArray.push(scalar1 + t * (scalar2 - scalar1)); // FIXME: won't work when the array contains "normals" or "IDs" |
| 204 | + } |
| 205 | + computedIntersectedArrays[name] = computedIntersectedArray; |
| 206 | + } |
| 207 | + |
184 | 208 | // Keep track of it
|
185 | 209 | intersectedEdgesList.push({
|
186 | 210 | pointEdge1: pointID1, // id of one point of the edge
|
187 | 211 | pointEdge2: pointID2, // id of one point of the edge
|
188 | 212 | intersectedPoint: computedIntersectedPoint, // 3D coordinate of points that intersected edge
|
| 213 | + intersectedArrays: computedIntersectedArrays, // value(s) of the intersected arrays |
189 | 214 | newPointID: -1, // id of the intersected point when it will be added into vtkPoints
|
190 | 215 | });
|
191 | 216 | }
|
@@ -217,6 +242,9 @@ function vtkCutter(publicAPI, model) {
|
217 | 242 | newPointsData.push(intersectedEdge.intersectedPoint[0]);
|
218 | 243 | newPointsData.push(intersectedEdge.intersectedPoint[1]);
|
219 | 244 | newPointsData.push(intersectedEdge.intersectedPoint[2]);
|
| 245 | + Object.keys(intersectedEdge.intersectedArrays).forEach((name) => { |
| 246 | + newPointData[name].push(...intersectedEdge.intersectedArrays[name]); |
| 247 | + }); |
220 | 248 | intersectedEdgesList[i].newPointID = newPointsData.length / 3 - 1;
|
221 | 249 | crossedEdges.push(intersectedEdgesList[i]);
|
222 | 250 | }
|
@@ -245,6 +273,20 @@ function vtkCutter(publicAPI, model) {
|
245 | 273 | 3
|
246 | 274 | );
|
247 | 275 |
|
| 276 | + // Set scalars |
| 277 | + const outputPointData = output.getPointData(); |
| 278 | + for (let arrayIdx = 0; arrayIdx < numberOfArrays; arrayIdx++) { |
| 279 | + const name = pointData.getArrayName(arrayIdx); |
| 280 | + const array = vtkDataArray.newInstance({ |
| 281 | + name, |
| 282 | + values: newPointData[name], |
| 283 | + numberOfComponents: pointData |
| 284 | + .getArrayByIndex(arrayIdx) |
| 285 | + .getNumberOfComponents(), |
| 286 | + }); |
| 287 | + outputPointData.addArray(array); |
| 288 | + } |
| 289 | + |
248 | 290 | // Set lines
|
249 | 291 | if (newLinesData.length !== 0) {
|
250 | 292 | output.getLines().setData(Uint16Array.from(newLinesData));
|
|
0 commit comments