Skip to content

Commit 91c8a3c

Browse files
committed
refactor(dataarray): change setData
Rename values to data in extend function and accept null as array param setData accept undefined, newinstance with "data" as param
1 parent 34a6681 commit 91c8a3c

File tree

7 files changed

+46
-56
lines changed

7 files changed

+46
-56
lines changed

Sources/Common/Core/CellArray/test/testCellArray.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test('Test cell array constructor', (t) => {
1313
);
1414
t.equal(emptyCellArray.getNumberOfCells(), 0, 'empty init number of cells');
1515

16-
const uintCellArray = vtkCellArray.newInstance({ values: [3, 0, 1, 2] });
16+
const uintCellArray = vtkCellArray.newInstance({ data: [3, 0, 1, 2] });
1717
t.equal(
1818
uintCellArray.getDataType(),
1919
VtkDataTypes.UNSIGNED_INT,
@@ -23,7 +23,7 @@ test('Test cell array constructor', (t) => {
2323

2424
const charCellArray = vtkCellArray.newInstance({
2525
dataType: VtkDataTypes.CHAR,
26-
values: [3, 0, 1, 2],
26+
data: [3, 0, 1, 2],
2727
});
2828
t.equal(
2929
charCellArray.getDataType(),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export interface vtkDataArray extends vtkObject {
118118
* @param {TypedArray} typedArray
119119
* @param {Number} [numberOfComponents]
120120
*/
121-
setData(typedArray: TypedArray, numberOfComponents?: number): void;
121+
setData(typedArray?: TypedArray, numberOfComponents?: number): void;
122122

123123
/**
124124
*
@@ -209,7 +209,7 @@ export function extend(publicAPI: object, model: object, initialValues?: object)
209209
* Method use to create a new instance of vtkDataArray
210210
* @param {object} [initialValues] for pre-setting some of its content
211211
* 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
212+
* model without giving data. This property will not be stored in the model
213213
*/
214214
export function newInstance(initialValues?: object): vtkDataArray;
215215

Sources/Common/Core/DataArray/index.js

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ 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';
44

5-
const { vtkErrorMacro } = macro;
6-
75
const { DefaultDataType } = Constants;
86
const TUPLE_HOLDER = [];
97

@@ -263,24 +261,20 @@ function vtkDataArray(publicAPI, model) {
263261
return model.name;
264262
};
265263

266-
publicAPI.setData = (typedArray, numberOfComponents) => {
267-
if (!typedArray) {
268-
vtkErrorMacro(`Cannot call setData with given array : ${typedArray}`);
269-
return;
270-
}
271-
264+
publicAPI.setData = (typedArray = null, numberOfComponents = undefined) => {
272265
if (Array.isArray(typedArray)) {
273266
// eslint-disable-next-line no-param-reassign
274267
typedArray = macro.newTypedArrayFrom(model.dataType, typedArray);
275268
}
269+
276270
model.values = typedArray;
277-
model.size = typedArray.length;
278-
model.dataType = getDataType(typedArray);
271+
model.size = typedArray ? typedArray.length : 0;
272+
model.dataType = typedArray ? getDataType(typedArray) : model.dataType;
279273

280274
if (numberOfComponents) {
281275
model.numberOfComponents = numberOfComponents;
282276
}
283-
if (model.size % model.numberOfComponents !== 0) {
277+
if (model.size % model.numberOfComponents !== 0 || !typedArray) {
284278
model.numberOfComponents = 1;
285279
}
286280
dataChange();
@@ -341,11 +335,14 @@ function defaultValues(initialValues) {
341335
// ----------------------------------------------------------------------------
342336

343337
export function extend(publicAPI, model, initialValues = {}) {
344-
if (!initialValues.empty && !initialValues.values && !initialValues.size) {
338+
if (!initialValues.empty && !initialValues.data && !initialValues.size) {
345339
throw new TypeError(
346340
'Cannot create vtkDataArray object without: size > 0, values'
347341
);
348342
}
343+
let initialData = initialValues.data;
344+
delete initialValues.empty;
345+
delete initialValues.data;
349346
Object.assign(initialValues, defaultValues(initialValues));
350347

351348
// Object methods
@@ -355,28 +352,17 @@ export function extend(publicAPI, model, initialValues = {}) {
355352
model.dataType = initialValues.dataType
356353
? initialValues.dataType
357354
: DefaultDataType;
358-
if (!initialValues.values) {
359-
initialValues.values = macro.newTypedArray(
360-
model.dataType,
361-
initialValues.size
362-
);
363-
} else if (Array.isArray(initialValues.values)) {
364-
initialValues.values = macro.newTypedArrayFrom(
365-
model.dataType,
366-
initialValues.values
367-
);
355+
delete initialValues.dataType;
356+
if (!initialData) {
357+
initialData = macro.newTypedArray(model.dataType, initialValues.size);
358+
} else if (Array.isArray(initialData)) {
359+
initialData = macro.newTypedArrayFrom(model.dataType, initialData);
368360
}
369361

362+
initialValues.data = initialData;
363+
370364
// Object specific methods
371365
vtkDataArray(publicAPI, model);
372-
// We need to call setData manually here because it is not a setter for a property
373-
// in itself but calling it is still the proper way to set the datas of the model
374-
publicAPI.setData(initialValues.values, initialValues.numberOfComponents);
375-
delete initialValues.values;
376-
delete initialValues.empty;
377-
delete initialValues.numberOfComponents;
378-
delete initialValues.size;
379-
delete initialValues.dataType;
380366
}
381367

382368
// ----------------------------------------------------------------------------

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,27 @@ test('Test vtkDataArray instance', (t) => {
1919

2020
t.throws(
2121
() => vtkDataArray.newInstance({}),
22-
'Create instance without values'
22+
'Not allowed to create instance without initialValues'
2323
);
2424

25-
t.doesNotThrow(() => vtkDataArray.newInstance({ empty: true }));
25+
t.doesNotThrow(
26+
() => vtkDataArray.newInstance({ empty: true }),
27+
'Allowed to create instance with empty true, no data'
28+
);
2629

2730
t.throws(
2831
() => vtkDataArray.newInstance({ empty: false }),
29-
'Create instance with empty false, no values'
32+
'Not allowed to create instance with empty false, no data'
3033
);
3134

3235
t.doesNotThrow(
3336
() => vtkDataArray.newInstance({ size: 256 }),
34-
'Create instance with only size'
37+
'Allowed to create instance with only size'
3538
);
3639

3740
const dataArray0 = vtkDataArray.newInstance({
3841
empty: true,
39-
values: null,
42+
data: null,
4043
});
4144
t.deepEqual(
4245
{
@@ -46,7 +49,7 @@ test('Test vtkDataArray instance', (t) => {
4649
values: macro.newTypedArray(DefaultDataType, 0),
4750
},
4851
getDataArrayProperties(dataArray0),
49-
'initialValues.values = null'
52+
'initialValues.data = null'
5053
);
5154

5255
const dataArray1 = vtkDataArray.newInstance({ size: 256 });
@@ -62,7 +65,7 @@ test('Test vtkDataArray instance', (t) => {
6265
);
6366

6467
const dataArray2 = vtkDataArray.newInstance({
65-
values: Uint32Array.from([1, 2, 3]),
68+
data: Uint32Array.from([1, 2, 3]),
6669
});
6770
t.deepEqual(
6871
{
@@ -72,11 +75,11 @@ test('Test vtkDataArray instance', (t) => {
7275
values: Uint32Array.from([1, 2, 3]),
7376
},
7477
getDataArrayProperties(dataArray2),
75-
'Create instance with values (typed array)'
78+
'Create instance with data (typed array)'
7679
);
7780

7881
const dataArray3 = vtkDataArray.newInstance({
79-
values: [1, 2, 3],
82+
data: [1, 2, 3],
8083
});
8184
t.deepEqual(
8285
{
@@ -86,11 +89,11 @@ test('Test vtkDataArray instance', (t) => {
8689
values: macro.newTypedArrayFrom(DefaultDataType, [1, 2, 3]),
8790
},
8891
getDataArrayProperties(dataArray3),
89-
'Create instance with values (untyped array)'
92+
'Create instance with data (untyped array)'
9093
);
9194

9295
const dataArray4 = vtkDataArray.newInstance({
93-
values: [1, 2, 3, 4, 5, 6, 7, 8, 9],
96+
data: [1, 2, 3, 4, 5, 6, 7, 8, 9],
9497
numberOfComponents: 3,
9598
});
9699
t.deepEqual(
@@ -108,7 +111,7 @@ test('Test vtkDataArray instance', (t) => {
108111
);
109112

110113
const dataArray5 = vtkDataArray.newInstance({
111-
values: [1, 2, 3],
114+
data: [1, 2, 3],
112115
dataType: null,
113116
});
114117
t.deepEqual(
@@ -176,28 +179,29 @@ test('Test vtkDataArray setData', (t) => {
176179
'Empty an instance (pass [] array)'
177180
);
178181

179-
// For both of those cases, check it does not change the DataArray
180-
// Not supposed to call setData with typedArray = null
182+
// Fill the DataArray before so that we are sure the size and the numberOfComponents is updated
183+
dataArray.setData([1, 2, 3], 3);
181184
dataArray.setData(null);
182185
t.deepEqual(
183186
{
184187
dataType: DefaultDataType,
185188
size: 0,
186189
numberOfComponents: 1,
187-
values: macro.newTypedArray(DefaultDataType),
190+
values: null,
188191
},
189192
getDataArrayProperties(dataArray),
190193
'Call setData with typedArray = null'
191194
);
192195

193-
// Not supposed to call setData without parameters
196+
// Fill the DataArray before so that we are sure the size and the numberOfComponents is updated
197+
dataArray.setData([1, 2, 3], 3);
194198
dataArray.setData();
195199
t.deepEqual(
196200
{
197201
dataType: DefaultDataType,
198202
size: 0,
199203
numberOfComponents: 1,
200-
values: macro.newTypedArray(DefaultDataType),
204+
values: null,
201205
},
202206
getDataArrayProperties(dataArray),
203207
'Call setData with typedArray = undefined'
@@ -227,7 +231,7 @@ test('Test vtkDataArray getRange function with single-channel data.', (t) => {
227231

228232
const da = vtkDataArray.newInstance({
229233
numberOfComponents: 1,
230-
values: newArray,
234+
data: newArray,
231235
});
232236

233237
t.ok(da.getRange(0)[0] === 0, 'getRange minimum value should be 0');
@@ -250,7 +254,7 @@ test('Test vtkDataArray getRange function with multi-channel data.', (t) => {
250254

251255
const da = vtkDataArray.newInstance({
252256
numberOfComponents: 3,
253-
values: newArray,
257+
data: newArray,
254258
});
255259

256260
t.ok(da.getRange(0)[0] === 0, 'component:0 minimum value should be 0');

Sources/Common/Core/Points/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ function vtkPoints(publicAPI, model) {
8282

8383
function defaultValues(initialValues) {
8484
return {
85+
// empty is only here to be passed to the DataArray extend function in order
86+
// to create Points without giving values
8587
empty: true,
8688
numberOfComponents: 3,
8789
dataType: VtkDataTypes.FLOAT,

Sources/Rendering/Misc/FullScreenRenderWindow/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export function extend(publicAPI: object, model: object, initialValues?: IFullSc
128128
/**
129129
* Method used to create a new instance of vtkFullScreenRenderWindow
130130
* @param {IFullScreenRenderWindowInitialValues} [initialValues] for pre-setting some of its content
131+
* a background property can be given in the initialValues to set the background of the renderer
131132
*/
132133
export function newInstance(initialValues?: IFullScreenRenderWindowInitialValues): vtkFullScreenRenderWindow;
133134

Sources/Rendering/Misc/FullScreenRenderWindow/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@ function defaultValues(initialValues) {
200200
// ----------------------------------------------------------------------------
201201

202202
export function extend(publicAPI, model, initialValues = {}) {
203-
// a background property can be set in the initialValues but we do not want
204-
// to "store" background, only forward it to the renderer
205-
206203
Object.assign(initialValues, defaultValues(initialValues));
207204

208205
// Object methods

0 commit comments

Comments
 (0)