Skip to content

Commit 27b5574

Browse files
jmannaufloryst
authored andcommitted
refactor: add getRanges function DataArray public api
1 parent d58e0ab commit 27b5574

File tree

4 files changed

+85
-11
lines changed

4 files changed

+85
-11
lines changed

Sources/Common/Core/DataArray/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ export interface vtkDataArray extends vtkObject {
8383
*/
8484
setRange(rangeValue: vtkRange, componentIndex: number): Range;
8585

86+
/**
87+
*
88+
*/
89+
getRanges(): vtkRange[];
90+
8691
/**
8792
* Set the given tuple at the given index.
8893
* @param {Number} idx

Sources/Common/Core/DataArray/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,22 @@ function vtkDataArray(publicAPI, model) {
281281
return model.rangeTuple;
282282
};
283283

284+
publicAPI.getRanges = () => {
285+
/** @type {import('../../../interfaces').vtkRange[]} */
286+
const ranges = [];
287+
for (let i = 0; i < model.numberOfComponents; i++) {
288+
const [min, max] = publicAPI.getRange(i);
289+
/** @type {import('../../../interfaces').vtkRange} */
290+
const range = {
291+
min,
292+
max,
293+
component: i,
294+
};
295+
ranges.push(range);
296+
}
297+
return ranges;
298+
};
299+
284300
publicAPI.setTuple = (idx, tuple) => {
285301
const offset = idx * model.numberOfComponents;
286302
for (let i = 0; i < model.numberOfComponents; i++) {

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,36 @@ test('Test vtkDataArray getRange function with NaN values.', (t) => {
131131
t.end();
132132
});
133133

134+
test('Test vtkDataArray getRanges function with single-channel data.', (t) => {
135+
// create a data array with a single channel.
136+
const newArray = new Uint16Array(256 * 3);
137+
138+
// fill the new array with the pattern 0,1,2,3,4,5, ..., 767.
139+
for (let i = 0; i < 256 * 3; ++i) {
140+
newArray[i] = i;
141+
}
142+
143+
const da = vtkDataArray.newInstance({
144+
numberOfComponents: 1,
145+
values: newArray,
146+
});
147+
148+
t.ok(
149+
da.getRanges().length === 1,
150+
'getRanges should return an array of 1 vtkRange objects'
151+
);
152+
t.ok(
153+
da.getRanges()[0].min === 0,
154+
'the first component returned by getRanges minimum value should be 0'
155+
);
156+
t.ok(
157+
da.getRanges()[0].max === 767,
158+
'the first component returned by getRanges maximum value should be 767'
159+
);
160+
161+
t.end();
162+
});
163+
134164
test('Test vtkDataArray getTuple', (t) => {
135165
const da = vtkDataArray.newInstance({
136166
numberOfComponents: 3,
@@ -202,6 +232,39 @@ test('Test vtkDataArray getRange function with multi-channel data.', (t) => {
202232
t.end();
203233
});
204234

235+
test('Test vtkDataArray getRanges function with multi-channel data.', (t) => {
236+
// create a data array with 3 channel data.
237+
const newArray = new Uint16Array(256 * 3);
238+
239+
// fill the new array with the pattern 1,2,3, 1,2,3
240+
// such that each channel has 1,1,1 2,2,2 3,3,3 respectively.
241+
for (let i = 0; i < 256; ++i) {
242+
newArray[i * 3] = i;
243+
newArray[i * 3 + 1] = i * 2;
244+
newArray[i * 3 + 2] = i * 3;
245+
}
246+
247+
const da = vtkDataArray.newInstance({
248+
numberOfComponents: 3,
249+
values: newArray,
250+
});
251+
252+
const ranges = da.getRanges();
253+
254+
t.ok(
255+
ranges.length === 3,
256+
'getRanges should return an array of 3 vtkRange objects'
257+
);
258+
t.ok(ranges[0].min === 0, 'component:0 minimum value should be 0');
259+
t.ok(ranges[0].max === 255, 'component:0 maximum value should be 255');
260+
t.ok(ranges[1].min === 0, 'component:1 minimum value should be 0');
261+
t.ok(ranges[1].max === 510, 'component:1 maximum value should be 510');
262+
t.ok(ranges[2].min === 0, 'component:2 minimum value should be 0');
263+
t.ok(ranges[2].max === 765, 'component:2 maximum value should be 765');
264+
265+
t.end();
266+
});
267+
205268
test('Test vtkDataArray insertNextTuple', (t) => {
206269
const dataArray = vtkDataArray.newInstance({
207270
dataType: VtkDataTypes.UNSIGNED_CHAR,

Sources/Rendering/OpenGL/ImageMapper/index.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,17 +1323,7 @@ function vtkOpenGLImageMapper(publicAPI, model) {
13231323
// range from the original scalars array and set the range to the
13241324
// scalars array. This means there is no need to re-calculate the
13251325
// ranges for the scalars array.
1326-
ranges = [];
1327-
for (let i = 0; i < numComp; i++) {
1328-
const [min, max] = imgScalars.getRange(i);
1329-
/** @type{ import("../../../interfaces").vtkRange } */
1330-
const range = {
1331-
min,
1332-
max,
1333-
component: i,
1334-
};
1335-
ranges.push(range);
1336-
}
1326+
ranges = imgScalars.getRanges();
13371327
}
13381328
} else {
13391329
vtkErrorMacro('Reformat slicing not yet supported.');

0 commit comments

Comments
 (0)