Skip to content

Commit 6112615

Browse files
authored
Merge pull request #2556 from joannacirillo/fix-get-tuple
fix(dataarray): getTuple
2 parents eebd1f9 + 6397203 commit 6112615

File tree

14 files changed

+79
-64
lines changed

14 files changed

+79
-64
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,20 @@ export interface vtkDataArray extends vtkObject {
9393
/**
9494
* Get the tuple at the given index.
9595
*
96-
* The recommended use is the following:
96+
* For performance reasons, it is advised to pass a 'tupleToFill':
9797
* `const x = [];`
98-
* `dataArray.getTuple(idx, x);`
98+
* `for (int i = 0; i < N; ++i) {
99+
* ` dataArray.getTuple(idx, x);`
100+
* ` ...`
99101
* instead of:
100-
* `const x = dataArray.getTuple(idx);`
101-
*
102+
* `for (int i = 0; i < N; ++i) {
103+
* ` const x = dataArray.getTuple(idx);`
104+
* `...`
102105
* @param {Number} idx
103-
* @param {Array<Number>} [tupleToFill] (default [])
104-
* @returns {Array<Number>}
106+
* @param {Number[]|TypedArray} [tupleToFill] (default [])
107+
* @returns {Number[]|TypedArray}
105108
*/
106-
getTuple(idx: number, tupleToFill?: Array<number>): Array<number>;
109+
getTuple(idx: number, tupleToFill?: number[]|TypedArray): number[]|TypedArray;
107110

108111
/**
109112
* Get the tuples between fromId (inclusive) and toId (exclusive).

Sources/Common/Core/DataArray/index.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
44

55
const { vtkErrorMacro } = macro;
66
const { DefaultDataType } = Constants;
7-
const TUPLE_HOLDER = [];
87

98
// ----------------------------------------------------------------------------
109
// Global methods
@@ -103,8 +102,10 @@ function getDataType(typedArray) {
103102
function getMaxNorm(normArray) {
104103
const numComps = normArray.getNumberOfComponents();
105104
let maxNorm = 0.0;
105+
const tuple = new Array(numComps);
106106
for (let i = 0; i < normArray.getNumberOfTuples(); ++i) {
107-
const norm = vtkMath.norm(normArray.getTuple(i), numComps);
107+
normArray.getTuple(i, tuple);
108+
const norm = vtkMath.norm(tuple, numComps);
108109
if (norm > maxNorm) {
109110
maxNorm = norm;
110111
}
@@ -299,11 +300,8 @@ function vtkDataArray(publicAPI, model) {
299300
return publicAPI.insertTuples(idx, tuples);
300301
};
301302

302-
publicAPI.getTuple = (idx, tupleToFill = TUPLE_HOLDER) => {
303+
publicAPI.getTuple = (idx, tupleToFill = []) => {
303304
const numberOfComponents = model.numberOfComponents || 1;
304-
if (tupleToFill.length !== numberOfComponents) {
305-
tupleToFill.length = numberOfComponents;
306-
}
307305
const offset = idx * numberOfComponents;
308306
// Check most common component sizes first
309307
// to avoid doing a for loop if possible
@@ -321,7 +319,7 @@ function vtkDataArray(publicAPI, model) {
321319
tupleToFill[0] = model.values[offset];
322320
break;
323321
default:
324-
for (let i = 0; i < numberOfComponents; i++) {
322+
for (let i = numberOfComponents - 1; i >= 0; --i) {
325323
tupleToFill[i] = model.values[offset + i];
326324
}
327325
}
@@ -427,14 +425,11 @@ function vtkDataArray(publicAPI, model) {
427425
vtkErrorMacro('numberOfComponents must match');
428426
}
429427

430-
const tuple1 = [];
431-
const tuple2 = [];
428+
const tuple1 = source1.getTuple(source1Idx);
429+
const tuple2 = source2.getTuple(source2Idx);
432430
const out = [];
433431
out.length = numberOfComponents;
434432

435-
source1.getTuple(source1Idx, tuple1);
436-
source2.getTuple(source2Idx, tuple2);
437-
438433
// Check most common component sizes first
439434
// to avoid doing a for loop if possible
440435
switch (numberOfComponents) {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from 'tape-catch';
22
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
33
import { VtkDataTypes } from 'vtk.js/Sources/Common/Core/DataArray/Constants';
4+
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
45

56
test('Test vtkDataArray instance', (t) => {
67
t.ok(vtkDataArray, 'Make sure the class definition exists');
@@ -29,6 +30,33 @@ test('Test vtkDataArray getRange function with single-channel data.', (t) => {
2930
t.end();
3031
});
3132

33+
test('Test vtkDataArray getTuple', (t) => {
34+
const da = vtkDataArray.newInstance({
35+
numberOfComponents: 3,
36+
values: new Uint8Array([0, 1, 2, 3, 4, 5]),
37+
});
38+
const da2 = vtkDataArray.newInstance({
39+
numberOfComponents: 3,
40+
values: new Uint8Array([0, 1, 2, 3, 4, 5]),
41+
});
42+
43+
t.ok(vtkMath.areEquals(da.getTuple(0), [0, 1, 2]), 'get first tuple');
44+
t.ok(vtkMath.areEquals(da.getTuple(1), [3, 4, 5]), 'get 2nd tuple');
45+
t.ok(da.getTuple(0) !== da.getTuple(1), 'getTuple twice');
46+
t.ok(da.getTuple(0) !== da2.getTuple(0), 'getTuple twice');
47+
const tuple = [];
48+
t.equal(da.getTuple(0, tuple), tuple, 'getTuple with tupleToFill');
49+
t.equal(tuple.length, 3, 'getTuple length');
50+
const typedArray = new Uint8Array(3);
51+
t.equal(
52+
da.getTuple(0, typedArray),
53+
typedArray,
54+
'getTuple with typed tupleToFill'
55+
);
56+
t.ok(vtkMath.areEquals(typedArray, [0, 1, 2]), 'get typed first tuple');
57+
t.end();
58+
});
59+
3260
test('Test vtkDataArray getRange function with multi-channel data.', (t) => {
3361
// create a data array with 3 channel data.
3462
const newArray = new Uint16Array(256 * 3);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import vtkDataArray from '../../../Common/Core/DataArray';
2-
import { Bounds } from '../../../types';
2+
import { Bounds, TypedArray } from '../../../types';
33

44
/**
55
*
@@ -26,10 +26,10 @@ export interface vtkPoints extends vtkDataArray {
2626
/**
2727
* Get the coordinate of a point.
2828
* @param {Number} idx The index of point.
29-
* @param {Number[]} [tupleToFill]
30-
* @default []
29+
* @param {Number[]|TypedArray} [tupleToFill] (default [])
30+
* @returns {Number[]|TypedArray}
3131
*/
32-
getPoint(idx: number, tupleToFill?: number[]): number[];
32+
getPoint(idx: number, tupleToFill?: number[]|TypedArray): number[]|TypedArray;
3333

3434
/**
3535
* Get the number of points for this object can hold.

Sources/Common/Core/StringArray/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import macro from 'vtk.js/Sources/macros';
22

3-
const TUPLE_HOLDER = [];
4-
53
// ----------------------------------------------------------------------------
64
// vtkStringArray methods
75
// ----------------------------------------------------------------------------
@@ -31,7 +29,7 @@ function vtkStringArray(publicAPI, model) {
3129

3230
publicAPI.getData = () => model.values;
3331

34-
publicAPI.getTuple = (idx, tupleToFill = TUPLE_HOLDER) => {
32+
publicAPI.getTuple = (idx, tupleToFill = []) => {
3533
const numberOfComponents = model.numberOfComponents || 1;
3634
if (tupleToFill.length) {
3735
tupleToFill.length = numberOfComponents;

Sources/Common/Core/VariantArray/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import macro from 'vtk.js/Sources/macros';
22

3-
const TUPLE_HOLDER = [];
4-
53
// ----------------------------------------------------------------------------
64
// vtkVariantArray methods
75
// ----------------------------------------------------------------------------
@@ -31,7 +29,7 @@ function vtkVariantArray(publicAPI, model) {
3129

3230
publicAPI.getData = () => model.values;
3331

34-
publicAPI.getTuple = (idx, tupleToFill = TUPLE_HOLDER) => {
32+
publicAPI.getTuple = (idx, tupleToFill = []) => {
3533
const numberOfComponents = model.numberOfComponents || 1;
3634
if (tupleToFill.length) {
3735
tupleToFill.length = numberOfComponents;

Sources/Filters/Core/Cutter/test/testCutter.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ test('Test vtkCutter cutCube', (t) => {
3131
correctPoints.forEach((correctPoint) => {
3232
let isContaining = false;
3333
for (let i = 0; i < points.getNumberOfPoints(); i++) {
34-
const p = [];
35-
points.getPoint(i, p);
34+
const p = points.getPoint(i);
3635
isContaining =
3736
p[0] === correctPoint[0] &&
3837
p[1] === correctPoint[1] &&

Sources/Filters/General/ClipClosedSurface/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,8 @@ function vtkClipClosedSurface(publicAPI, model) {
7777
}
7878

7979
// Get the edge and interpolate the new point
80-
const p0 = [];
81-
const p1 = [];
82-
points.getPoint(i0, p0);
83-
points.getPoint(i1, p1);
80+
const p0 = points.getPoint(i0);
81+
const p1 = points.getPoint(i1);
8482

8583
const f = v0 / (v0 - v1);
8684
const s = 1.0 - f;

Sources/Filters/General/ImageStreamline/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ function vtkImageStreamline(publicAPI, model) {
137137
velAtArg[0] = 0.0;
138138
velAtArg[1] = 0.0;
139139
velAtArg[2] = 0.0;
140+
const vel = new Array(3);
140141
for (let i = 0; i < 8; i++) {
141-
const vel = velArray.getTuple(voxelIndices[i]);
142+
velArray.getTuple(voxelIndices[i], vel);
142143
for (let j = 0; j < 3; j++) {
143144
velAtArg[j] += weights[i] * vel[j];
144145
}
@@ -236,13 +237,10 @@ function vtkImageStreamline(publicAPI, model) {
236237
let offset = 0;
237238
const datas = [];
238239
const vectors = input.getPointData().getVectors();
240+
const point = [];
239241
for (let i = 0; i < nSeeds; i++) {
240-
const retVal = publicAPI.streamIntegrate(
241-
vectors,
242-
input,
243-
seedPts.getTuple(i),
244-
offset
245-
);
242+
seedPts.getTuple(i, point);
243+
const retVal = publicAPI.streamIntegrate(vectors, input, point, offset);
246244
offset += retVal[0].length / 3;
247245
datas.push(retVal);
248246
}

Sources/Filters/General/PaintFilter/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ function vtkPaintFilter(publicAPI, model) {
9898
let diffIdx = 0;
9999
if (model.voxelFunc) {
100100
const bgScalars = model.backgroundImage.getPointData().getScalars();
101+
const voxel = [];
101102
for (let i = 0; i < maskLabelMap.length; i++) {
102103
if (maskLabelMap[i]) {
103-
const voxel = bgScalars.getTuple(i);
104+
bgScalars.getTuple(i, voxel);
104105
// might not fill up snapshot
105106
if (model.voxelFunc(voxel, i, label)) {
106107
snapshot[diffIdx++] = [i, data[i]];

0 commit comments

Comments
 (0)