Skip to content

Commit 527c2d9

Browse files
Jo-Byrfloryst
authored andcommitted
feat(vtkCutter): compute cut arrays with vtkCutter
Compute input dataset arrays interpolation along cut produced by vtkCutterr
1 parent d5cf3e5 commit 527c2d9

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Sources/Filters/Core/Cutter/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as macro from 'vtk.js/Sources/macros';
2+
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
23
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
34

45
const { vtkErrorMacro } = macro;
@@ -75,10 +76,18 @@ function vtkCutter(publicAPI, model) {
7576
function dataSetCutter(input, output) {
7677
const points = input.getPoints();
7778
const pointsData = points.getData();
79+
const pointData = input.getPointData();
7880
const numPts = points.getNumberOfPoints();
7981
const newPointsData = [];
8082
const newLinesData = [];
8183
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+
}
8291

8392
if (!model.cutScalars || model.cutScalars.length < numPts) {
8493
model.cutScalars = new Float32Array(numPts);
@@ -181,11 +190,27 @@ function vtkCutter(publicAPI, model) {
181190
x1[2] + t * (x2[2] - x1[2]),
182191
];
183192

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+
184208
// Keep track of it
185209
intersectedEdgesList.push({
186210
pointEdge1: pointID1, // id of one point of the edge
187211
pointEdge2: pointID2, // id of one point of the edge
188212
intersectedPoint: computedIntersectedPoint, // 3D coordinate of points that intersected edge
213+
intersectedArrays: computedIntersectedArrays, // value(s) of the intersected arrays
189214
newPointID: -1, // id of the intersected point when it will be added into vtkPoints
190215
});
191216
}
@@ -217,6 +242,9 @@ function vtkCutter(publicAPI, model) {
217242
newPointsData.push(intersectedEdge.intersectedPoint[0]);
218243
newPointsData.push(intersectedEdge.intersectedPoint[1]);
219244
newPointsData.push(intersectedEdge.intersectedPoint[2]);
245+
Object.keys(intersectedEdge.intersectedArrays).forEach((name) => {
246+
newPointData[name].push(...intersectedEdge.intersectedArrays[name]);
247+
});
220248
intersectedEdgesList[i].newPointID = newPointsData.length / 3 - 1;
221249
crossedEdges.push(intersectedEdgesList[i]);
222250
}
@@ -245,6 +273,20 @@ function vtkCutter(publicAPI, model) {
245273
3
246274
);
247275

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+
248290
// Set lines
249291
if (newLinesData.length !== 0) {
250292
output.getLines().setData(Uint16Array.from(newLinesData));

0 commit comments

Comments
 (0)