Skip to content

Commit 1618a20

Browse files
jmannaufloryst
authored andcommitted
fix(DataArray): update getRanges
add computeRanges arg. If set to false will clone the existing range. If true, will calculate the ranges. Added the magnitude ranges for multi-component ranges
1 parent f9aafc5 commit 1618a20

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

Sources/Common/Core/DataArray/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,10 @@ function vtkDataArray(publicAPI, model) {
281281
return model.rangeTuple;
282282
};
283283

284-
publicAPI.getRanges = () => {
284+
publicAPI.getRanges = (computeRanges = true) => {
285+
if (!computeRanges) {
286+
return structuredClone(model.ranges);
287+
}
285288
/** @type {import('../../../interfaces').vtkRange[]} */
286289
const ranges = [];
287290
for (let i = 0; i < model.numberOfComponents; i++) {
@@ -294,6 +297,18 @@ function vtkDataArray(publicAPI, model) {
294297
};
295298
ranges.push(range);
296299
}
300+
// where the number of components is greater than 1, the last element in
301+
// the range array is the min,max magnitude of the entire dataset.
302+
if (model.numberOfComponents > 1) {
303+
/** @type {import('../../../interfaces').vtkRange} */
304+
const [min, max] = publicAPI.getRange(-1);
305+
const range = {
306+
min,
307+
max,
308+
component: -1,
309+
};
310+
ranges.push(range);
311+
}
297312
return ranges;
298313
};
299314

Sources/Common/Core/DataArray/test/testDataArray.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
33
import { VtkDataTypes } from 'vtk.js/Sources/Common/Core/DataArray/Constants';
44
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
55

6+
function compareFloat(a, b) {
7+
return Math.abs(a - b) < Number.EPSILON;
8+
}
9+
610
test('Test vtkDataArray instance', (t) => {
711
t.ok(vtkDataArray, 'Make sure the class definition exists');
812
const instance = vtkDataArray.newInstance({ size: 256 });
@@ -219,7 +223,6 @@ test('Test vtkDataArray getRange function with multi-channel data.', (t) => {
219223
newArray[i * 3 + 2] = i;
220224
}
221225

222-
const compareFloat = (a, b) => Math.abs(a - b) < Number.EPSILON;
223226
const vecRange = da.getRange(-1);
224227
t.ok(
225228
compareFloat(vecRange[0].toFixed(2), 0.0),
@@ -252,15 +255,23 @@ test('Test vtkDataArray getRanges function with multi-channel data.', (t) => {
252255
const ranges = da.getRanges();
253256

254257
t.ok(
255-
ranges.length === 3,
256-
'getRanges should return an array of 3 vtkRange objects'
258+
ranges.length === 4,
259+
'getRanges should return an array of 4 vtkRange objects'
257260
);
258261
t.ok(ranges[0].min === 0, 'component:0 minimum value should be 0');
259262
t.ok(ranges[0].max === 255, 'component:0 maximum value should be 255');
260263
t.ok(ranges[1].min === 0, 'component:1 minimum value should be 0');
261264
t.ok(ranges[1].max === 510, 'component:1 maximum value should be 510');
262265
t.ok(ranges[2].min === 0, 'component:2 minimum value should be 0');
263266
t.ok(ranges[2].max === 765, 'component:2 maximum value should be 765');
267+
t.ok(
268+
ranges[2].min === 0,
269+
'component:-1 vector magnitude minimum should be 0'
270+
);
271+
t.ok(
272+
compareFloat(ranges[3].max, 954.1226336273551),
273+
'component:-1 vector magnitude maximum should be 954.1226336273551'
274+
);
264275

265276
t.end();
266277
});

0 commit comments

Comments
 (0)