Skip to content

Commit 0b0c594

Browse files
committed
refactor(dataarray): update/create set methods
call setData at the end of the extend function to synchronise parameters. Check on the parameters is done through initialValues. CellArray is adapted to the changes made. Tests updates.
1 parent 36868ba commit 0b0c594

File tree

5 files changed

+39
-39
lines changed

5 files changed

+39
-39
lines changed

Sources/Common/Core/DataArray/index.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ function vtkDataArray(publicAPI, model) {
241241
publicAPI.getNumberOfComponents = () => model.numberOfComponents;
242242
publicAPI.getNumberOfValues = () => model.values.length;
243243
publicAPI.getNumberOfTuples = () =>
244-
model.values.length / model.numberOfComponents;
244+
model.values ? model.values.length / model.numberOfComponents : 0;
245245
publicAPI.getDataType = () => model.dataType;
246246
/* eslint-disable no-use-before-define */
247247
publicAPI.newClone = () =>
@@ -262,9 +262,14 @@ function vtkDataArray(publicAPI, model) {
262262
};
263263

264264
publicAPI.setData = (typedArray, numberOfComponents) => {
265-
model.values = typedArray;
266-
model.size = typedArray.length;
267-
model.dataType = getDataType(typedArray);
265+
if (typedArray) {
266+
model.values = typedArray;
267+
model.size = typedArray.length;
268+
model.empty = model.size === 0;
269+
const type = getDataType(typedArray);
270+
model.dataType = type === 'Array' ? model.dataType : type;
271+
}
272+
268273
if (numberOfComponents) {
269274
model.numberOfComponents = numberOfComponents;
270275
}
@@ -313,44 +318,38 @@ function vtkDataArray(publicAPI, model) {
313318
// Object factory
314319
// ----------------------------------------------------------------------------
315320

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-
};
321+
function defaultValues(initialValues) {
322+
return {
323+
name: '',
324+
numberOfComponents: 1,
325+
size: 0,
326+
dataType: DefaultDataType,
327+
rangeTuple: [0, 0],
328+
// values: null,
329+
// ranges: null,
330+
...initialValues,
331+
};
332+
}
325333

326334
// ----------------------------------------------------------------------------
327335

328336
export function extend(publicAPI, model, initialValues = {}) {
329-
Object.assign(model, DEFAULT_VALUES, initialValues);
330-
331-
if (!model.empty && !model.values && !model.size) {
332-
throw new TypeError(
333-
'Cannot create vtkDataArray object without: size > 0, values'
334-
);
335-
}
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-
}
337+
Object.assign(model, defaultValues(model));
347338

348339
// Object methods
349340
macro.obj(publicAPI, model);
350341
macro.set(publicAPI, model, ['name', 'numberOfComponents']);
351342

343+
const type = initialValues.dataType ? initialValues.dataType : model.dataType;
344+
if (!initialValues.values) {
345+
initialValues.values = macro.newTypedArray(type, model.size);
346+
} else if (Array.isArray(initialValues.values)) {
347+
initialValues.values = macro.newTypedArrayFrom(type, initialValues.values);
348+
}
349+
352350
// Object specific methods
353351
vtkDataArray(publicAPI, model);
352+
publicAPI.setData(initialValues.values, initialValues.numberOfComponents);
354353
}
355354

356355
// ----------------------------------------------------------------------------

Sources/Common/Core/Points/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ const DEFAULT_VALUES = {
8989

9090
// ----------------------------------------------------------------------------
9191

92-
export function extend(publicAPI, model, initialValues = {}) {
93-
Object.assign(model, DEFAULT_VALUES, initialValues);
92+
export function extend(publicAPI, model) {
93+
Object.assign(model, DEFAULT_VALUES);
9494

95-
vtkDataArray.extend(publicAPI, model, initialValues);
95+
vtkDataArray.extend(publicAPI, model);
9696
vtkPoints(publicAPI, model);
9797
}
9898

Sources/Common/DataModel/Cell/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ const DEFAULT_VALUES = {
129129

130130
// ----------------------------------------------------------------------------
131131

132-
export function extend(publicAPI, model, initialValues = {}) {
133-
Object.assign(model, DEFAULT_VALUES, initialValues);
132+
export function extend(publicAPI, model) {
133+
Object.assign(model, DEFAULT_VALUES);
134134

135135
macro.obj(publicAPI, model);
136136

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ test('Test vtkCell initialize with pointsIds', (t) => {
4747

4848
test('Test vtkCell deepCopy', (t) => {
4949
const points = vtkPoints.newInstance();
50-
points.setData([0, 0, 0, 2, 0, 0, 2, 2, 0]);
50+
points.setData(Float64Array.from([0, 0, 0, 2, 0, 0, 2, 2, 0]));
5151

5252
const cell = vtkCell.newInstance();
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

Sources/macros.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,8 @@ export function newInstance(extend, className) {
972972
const model = {};
973973
const publicAPI = {};
974974
extend(publicAPI, model, initialValues);
975-
publicAPI.set(initialValues);
975+
const val = { ...initialValues, ...model };
976+
publicAPI.set(val);
976977

977978
return Object.freeze(publicAPI);
978979
};

0 commit comments

Comments
 (0)