Skip to content

Commit 894a3e3

Browse files
committed
refactor(fullscreenrenderwindow, dataarray, remoteview, macro): refactor constructors
Refactor of (some classes) constructors so that the setXXX() methods of coresponding properties are called when the fullScreenRenderWindow is instanciated (calling newInstance). In the macro.js newInstance() method, the set() method is called in order that the setXXX() methods for each entry of initial values is called if such methods exist. This is in order that side effects of setters are taken into account when creating new objects. Setters of both default and initial values are called Refactor of DataArray and child classes. The method "setData" is called so that the parameters are synchronised as it is not a setter of one property in itself but has that purpose. Tests have been added and updated. A new test for macro protected properties have been added. BREAKING CHANGE: If a custom handling is done on the initialValues in the extend function, the changes made might be overwritten by the call of the set function in newInstace. If it is not what is intended, those properties have to be removed from initialValues. If a setter of a property is called in the function creating an object (vtkObject(publicAPI, model)) such call should be removed to avoid witnessing twice the same side effects.
1 parent 230092f commit 894a3e3

File tree

10 files changed

+309
-59
lines changed

10 files changed

+309
-59
lines changed

Sources/Common/Core/CellArray/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ function vtkCellArray(publicAPI, model) {
8888

8989
function defaultValues(initialValues) {
9090
return {
91+
// empty is only here to be passed to the DataArray extend function in order
92+
// to create a cellArray without giving values
9193
empty: true,
9294
numberOfComponents: 1,
9395
dataType: VtkDataTypes.UNSIGNED_INT,
@@ -98,7 +100,8 @@ function defaultValues(initialValues) {
98100
// ----------------------------------------------------------------------------
99101

100102
export function extend(publicAPI, model, initialValues = {}) {
101-
vtkDataArray.extend(publicAPI, model, defaultValues(initialValues));
103+
Object.assign(initialValues, defaultValues(initialValues));
104+
vtkDataArray.extend(publicAPI, model, initialValues);
102105
vtkCellArray(publicAPI, model);
103106
}
104107

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ export function extend(publicAPI: object, model: object, initialValues?: object)
208208
/**
209209
* Method use to create a new instance of vtkDataArray
210210
* @param {object} [initialValues] for pre-setting some of its content
211+
* initialValues can have a property "empty: true" to be able to create a
212+
* model without giving values. This property will not be stored in the model
211213
*/
212214
export function newInstance(initialValues?: object): vtkDataArray;
213215

Sources/Common/Core/DataArray/index.js

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Constants from 'vtk.js/Sources/Common/Core/DataArray/Constants';
22
import * as macro from 'vtk.js/Sources/macros';
33
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
4+
const { vtkErrorMacro } = macro;
45

56
const { DefaultDataType } = Constants;
67
const TUPLE_HOLDER = [];
@@ -262,9 +263,19 @@ function vtkDataArray(publicAPI, model) {
262263
};
263264

264265
publicAPI.setData = (typedArray, numberOfComponents) => {
266+
if (!typedArray) {
267+
vtkErrorMacro(`Cannot call setData with given array : ${typedArray}`);
268+
return;
269+
}
270+
271+
if (Array.isArray(typedArray)) {
272+
// eslint-disable-next-line no-param-reassign
273+
typedArray = macro.newTypedArrayFrom(model.dataType, typedArray);
274+
}
265275
model.values = typedArray;
266276
model.size = typedArray.length;
267277
model.dataType = getDataType(typedArray);
278+
268279
if (numberOfComponents) {
269280
model.numberOfComponents = numberOfComponents;
270281
}
@@ -313,44 +324,58 @@ function vtkDataArray(publicAPI, model) {
313324
// Object factory
314325
// ----------------------------------------------------------------------------
315326

316-
const DEFAULT_VALUES = {
317-
name: '',
318-
numberOfComponents: 1,
319-
size: 0,
320-
dataType: DefaultDataType,
321-
rangeTuple: [0, 0],
322-
// values: null,
323-
// ranges: null,
324-
};
327+
function defaultValues(initialValues) {
328+
return {
329+
name: '',
330+
numberOfComponents: 1,
331+
size: 0,
332+
dataType: DefaultDataType,
333+
rangeTuple: [0, 0],
334+
// values: macro.newTypedArray(DefaultValues),
335+
// ranges: null,
336+
...initialValues,
337+
};
338+
}
325339

326340
// ----------------------------------------------------------------------------
327341

328342
export function extend(publicAPI, model, initialValues = {}) {
329-
Object.assign(model, DEFAULT_VALUES, initialValues);
330-
331-
if (!model.empty && !model.values && !model.size) {
343+
if (!initialValues.empty && !initialValues.values && !initialValues.size) {
332344
throw new TypeError(
333345
'Cannot create vtkDataArray object without: size > 0, values'
334346
);
335347
}
336-
337-
if (!model.values) {
338-
model.values = macro.newTypedArray(model.dataType, model.size);
339-
} else if (Array.isArray(model.values)) {
340-
model.values = macro.newTypedArrayFrom(model.dataType, model.values);
341-
}
342-
343-
if (model.values) {
344-
model.size = model.values.length;
345-
model.dataType = getDataType(model.values);
346-
}
348+
Object.assign(initialValues, defaultValues(initialValues));
347349

348350
// Object methods
349351
macro.obj(publicAPI, model);
350352
macro.set(publicAPI, model, ['name', 'numberOfComponents']);
351353

354+
model.dataType = initialValues.dataType
355+
? initialValues.dataType
356+
: DefaultDataType;
357+
if (!initialValues.values) {
358+
initialValues.values = macro.newTypedArray(
359+
model.dataType,
360+
initialValues.size
361+
);
362+
} else if (Array.isArray(initialValues.values)) {
363+
initialValues.values = macro.newTypedArrayFrom(
364+
model.dataType,
365+
initialValues.values
366+
);
367+
}
368+
352369
// Object specific methods
353370
vtkDataArray(publicAPI, model);
371+
// We need to call setData manually here because it is not a setter for a property
372+
// in itself but calling it is still the proper way to set the datas of the model
373+
publicAPI.setData(initialValues.values, initialValues.numberOfComponents);
374+
delete initialValues.values;
375+
delete initialValues.empty;
376+
delete initialValues.numberOfComponents;
377+
delete initialValues.size;
378+
delete initialValues.dataType;
354379
}
355380

356381
// ----------------------------------------------------------------------------

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

Lines changed: 210 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,221 @@
11
import test from 'tape-catch';
22
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
3+
import Constants from 'vtk.js/Sources/Common/Core/DataArray/Constants';
4+
import * as macro from 'vtk.js/Sources/macros';
5+
6+
const { DefaultDataType } = Constants;
7+
8+
function getDataArrayProperties(dataArray) {
9+
return {
10+
size: dataArray.get().size,
11+
numberOfComponents: dataArray.get().numberOfComponents,
12+
dataType: dataArray.get().dataType,
13+
values: dataArray.get().values,
14+
};
15+
}
316

417
test('Test vtkDataArray instance', (t) => {
518
t.ok(vtkDataArray, 'Make sure the class definition exists');
6-
const instance = vtkDataArray.newInstance({ size: 256 });
7-
t.ok(instance);
19+
20+
t.throws(
21+
() => vtkDataArray.newInstance({}),
22+
'Create instance without values'
23+
);
24+
25+
t.doesNotThrow(() => vtkDataArray.newInstance({ empty: true }));
26+
27+
t.throws(
28+
() => vtkDataArray.newInstance({ empty: false }),
29+
'Create instance with empty false, no values'
30+
);
31+
32+
t.doesNotThrow(
33+
() => vtkDataArray.newInstance({ size: 256 }),
34+
'Create instance with only size'
35+
);
36+
37+
const dataArray0 = vtkDataArray.newInstance({
38+
empty: true,
39+
values: null,
40+
});
41+
t.deepEqual(
42+
{
43+
dataType: DefaultDataType,
44+
size: 0,
45+
numberOfComponents: 1,
46+
values: macro.newTypedArray(DefaultDataType, 0),
47+
},
48+
getDataArrayProperties(dataArray0),
49+
'initialValues.values = null'
50+
);
51+
52+
const dataArray1 = vtkDataArray.newInstance({ size: 256 });
53+
t.deepEqual(
54+
{
55+
dataType: DefaultDataType,
56+
size: 256,
57+
numberOfComponents: 1,
58+
values: macro.newTypedArray(DefaultDataType, 256),
59+
},
60+
getDataArrayProperties(dataArray1),
61+
'Give only size to create instance'
62+
);
63+
64+
const dataArray2 = vtkDataArray.newInstance({
65+
values: Uint32Array.from([1, 2, 3]),
66+
});
67+
t.deepEqual(
68+
{
69+
dataType: 'Uint32Array',
70+
size: 3,
71+
numberOfComponents: 1,
72+
values: Uint32Array.from([1, 2, 3]),
73+
},
74+
getDataArrayProperties(dataArray2),
75+
'Create instance with values (typed array)'
76+
);
77+
78+
const dataArray3 = vtkDataArray.newInstance({
79+
values: [1, 2, 3],
80+
});
81+
t.deepEqual(
82+
{
83+
dataType: DefaultDataType,
84+
size: 3,
85+
numberOfComponents: 1,
86+
values: macro.newTypedArrayFrom(DefaultDataType, [1, 2, 3]),
87+
},
88+
getDataArrayProperties(dataArray3),
89+
'Create instance with values (untyped array)'
90+
);
91+
92+
const dataArray4 = vtkDataArray.newInstance({
93+
values: [1, 2, 3, 4, 5, 6, 7, 8, 9],
94+
numberOfComponents: 3,
95+
});
96+
t.deepEqual(
97+
{
98+
dataType: DefaultDataType,
99+
size: 9,
100+
numberOfComponents: 3,
101+
values: macro.newTypedArrayFrom(
102+
DefaultDataType,
103+
[1, 2, 3, 4, 5, 6, 7, 8, 9]
104+
),
105+
},
106+
getDataArrayProperties(dataArray4),
107+
'Change number of components at instanciation'
108+
);
109+
110+
const dataArray5 = vtkDataArray.newInstance({
111+
values: [1, 2, 3],
112+
dataType: null,
113+
});
114+
t.deepEqual(
115+
{
116+
dataType: DefaultDataType,
117+
size: 3,
118+
numberOfComponents: 1,
119+
values: macro.newTypedArrayFrom(DefaultDataType, [1, 2, 3]),
120+
},
121+
getDataArrayProperties(dataArray5),
122+
'Give null as dataType'
123+
);
124+
8125
t.end();
9126
});
10127

128+
test('Test vtkDataArray setData', (t) => {
129+
const dataArray = vtkDataArray.newInstance({ empty: true });
130+
131+
dataArray.setData([4, 5, 6, 7]);
132+
t.deepEqual(
133+
{
134+
dataType: DefaultDataType,
135+
size: 4,
136+
numberOfComponents: 1,
137+
values: macro.newTypedArrayFrom(DefaultDataType, [4, 5, 6, 7]),
138+
},
139+
getDataArrayProperties(dataArray),
140+
'Change data of existing instance'
141+
);
142+
143+
dataArray.setData([1, 2, 3, 4, 5, 6], 2);
144+
t.deepEqual(
145+
{
146+
dataType: DefaultDataType,
147+
size: 6,
148+
numberOfComponents: 2,
149+
values: macro.newTypedArrayFrom(DefaultDataType, [1, 2, 3, 4, 5, 6]),
150+
},
151+
getDataArrayProperties(dataArray),
152+
'Change number of components with setData'
153+
);
154+
155+
dataArray.setData([1, 2, 3, 4], 3);
156+
t.deepEqual(
157+
{
158+
dataType: DefaultDataType,
159+
size: 4,
160+
numberOfComponents: 1,
161+
values: macro.newTypedArrayFrom(DefaultDataType, [1, 2, 3, 4]),
162+
},
163+
getDataArrayProperties(dataArray),
164+
'Change number of components with setData but wrong numberOfComponents'
165+
);
166+
167+
dataArray.setData([]);
168+
t.deepEqual(
169+
{
170+
dataType: DefaultDataType,
171+
size: 0,
172+
numberOfComponents: 1,
173+
values: macro.newTypedArray(DefaultDataType),
174+
},
175+
getDataArrayProperties(dataArray),
176+
'Empty an instance (pass [] array)'
177+
);
178+
179+
// For both of those cases, check it does not change the DataArray
180+
// Not supposed to call setData with typedArray = null
181+
dataArray.setData(null);
182+
t.deepEqual(
183+
{
184+
dataType: DefaultDataType,
185+
size: 0,
186+
numberOfComponents: 1,
187+
values: macro.newTypedArray(DefaultDataType),
188+
},
189+
getDataArrayProperties(dataArray),
190+
'Call setData with typedArray = null'
191+
);
192+
193+
// Not supposed to call setData without parameters
194+
dataArray.setData();
195+
t.deepEqual(
196+
{
197+
dataType: DefaultDataType,
198+
size: 0,
199+
numberOfComponents: 1,
200+
values: macro.newTypedArray(DefaultDataType),
201+
},
202+
getDataArrayProperties(dataArray),
203+
'Call setData with typedArray = undefined'
204+
);
205+
206+
dataArray.setData(macro.newTypedArrayFrom('Uint32Array', [4, 5, 6, 7]));
207+
t.deepEqual(
208+
{
209+
dataType: 'Uint32Array',
210+
size: 4,
211+
numberOfComponents: 1,
212+
values: macro.newTypedArrayFrom('Uint32Array', [4, 5, 6, 7]),
213+
},
214+
getDataArrayProperties(dataArray),
215+
'Change data of existing instance with another type'
216+
);
217+
});
218+
11219
test('Test vtkDataArray getRange function with single-channel data.', (t) => {
12220
// create a data array with a single channel.
13221
const newArray = new Uint16Array(256 * 3);

Sources/Common/Core/Points/index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,20 @@ function vtkPoints(publicAPI, model) {
8080
// Object factory
8181
// ----------------------------------------------------------------------------
8282

83-
const DEFAULT_VALUES = {
84-
empty: true,
85-
numberOfComponents: 3,
86-
dataType: VtkDataTypes.FLOAT,
87-
bounds: [1, -1, 1, -1, 1, -1],
88-
};
83+
function defaultValues(initialValues) {
84+
return {
85+
empty: true,
86+
numberOfComponents: 3,
87+
dataType: VtkDataTypes.FLOAT,
88+
bounds: [1, -1, 1, -1, 1, -1],
89+
...initialValues,
90+
};
91+
}
8992

9093
// ----------------------------------------------------------------------------
9194

9295
export function extend(publicAPI, model, initialValues = {}) {
93-
Object.assign(model, DEFAULT_VALUES, initialValues);
94-
96+
Object.assign(initialValues, defaultValues(initialValues));
9597
vtkDataArray.extend(publicAPI, model, initialValues);
9698
vtkPoints(publicAPI, model);
9799
}

Sources/Common/DataModel/Cell/test/testCell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test('Test vtkCell deepCopy', (t) => {
5353
cell.initialize(points);
5454

5555
const cell2 = vtkCell.newInstance();
56-
cell2.deepCopy(cell);
56+
cell.deepCopy(cell2);
5757
t.notEqual(cell2.getPoints(), points);
5858
t.deepEqual(cell2.getPoints().getData(), points.getData());
5959

0 commit comments

Comments
 (0)