diff --git a/Sources/Common/Core/CellArray/index.js b/Sources/Common/Core/CellArray/index.js index 504b0167974..4c593074bc4 100644 --- a/Sources/Common/Core/CellArray/index.js +++ b/Sources/Common/Core/CellArray/index.js @@ -18,6 +18,7 @@ function extractCellSizes(cellArray) { } function getNumberOfCells(cellArray) { + if (!cellArray) return 0; let cellId = 0; for (let cellArrayIndex = 0; cellArrayIndex < cellArray.length; ) { cellArrayIndex += cellArray[cellArrayIndex] + 1; @@ -88,6 +89,8 @@ function vtkCellArray(publicAPI, model) { function defaultValues(initialValues) { return { + // empty is only here to be passed to the DataArray extend function in order + // to create a cellArray without giving values empty: true, numberOfComponents: 1, dataType: VtkDataTypes.UNSIGNED_INT, @@ -98,13 +101,14 @@ function defaultValues(initialValues) { // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - vtkDataArray.extend(publicAPI, model, defaultValues(initialValues)); + Object.assign(initialValues, defaultValues(initialValues)); + vtkDataArray.extend(publicAPI, model, initialValues); vtkCellArray(publicAPI, model); } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkCellArray'); +export const newInstance = macro.newInstance(extend, 'vtkCellArray', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/Core/DataArray/index.d.ts b/Sources/Common/Core/DataArray/index.d.ts index 57b7469f6b1..35d4a33cc45 100644 --- a/Sources/Common/Core/DataArray/index.d.ts +++ b/Sources/Common/Core/DataArray/index.d.ts @@ -118,7 +118,7 @@ export interface vtkDataArray extends vtkObject { * @param {TypedArray} typedArray * @param {Number} [numberOfComponents] */ - setData(typedArray: TypedArray, numberOfComponents?: number): void; + setData(typedArray?: TypedArray, numberOfComponents?: number): void; /** * @@ -208,6 +208,8 @@ export function extend(publicAPI: object, model: object, initialValues?: object) /** * Method use to create a new instance of vtkDataArray * @param {object} [initialValues] for pre-setting some of its content + * initialValues can have a property "empty: true" to be able to create a + * model without giving data. This property will not be stored in the model */ export function newInstance(initialValues?: object): vtkDataArray; diff --git a/Sources/Common/Core/DataArray/index.js b/Sources/Common/Core/DataArray/index.js index b8f5bdf5236..c4e73db146a 100644 --- a/Sources/Common/Core/DataArray/index.js +++ b/Sources/Common/Core/DataArray/index.js @@ -239,9 +239,9 @@ function vtkDataArray(publicAPI, model) { publicAPI.getTupleLocation = (idx = 1) => idx * model.numberOfComponents; publicAPI.getNumberOfComponents = () => model.numberOfComponents; - publicAPI.getNumberOfValues = () => model.values.length; + publicAPI.getNumberOfValues = () => (model.values ? model.values.length : 0); publicAPI.getNumberOfTuples = () => - model.values.length / model.numberOfComponents; + model.values ? model.values.length / model.numberOfComponents : 0; publicAPI.getDataType = () => model.dataType; /* eslint-disable no-use-before-define */ publicAPI.newClone = () => @@ -261,10 +261,16 @@ function vtkDataArray(publicAPI, model) { return model.name; }; - publicAPI.setData = (typedArray, numberOfComponents) => { + publicAPI.setData = (typedArray = null, numberOfComponents = undefined) => { + if (Array.isArray(typedArray)) { + // eslint-disable-next-line no-param-reassign + typedArray = macro.newTypedArrayFrom(model.dataType, typedArray); + } + model.values = typedArray; - model.size = typedArray.length; - model.dataType = getDataType(typedArray); + model.size = typedArray ? typedArray.length : 0; + model.dataType = typedArray ? getDataType(typedArray) : model.dataType; + if (numberOfComponents) { model.numberOfComponents = numberOfComponents; } @@ -313,49 +319,68 @@ function vtkDataArray(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - name: '', - numberOfComponents: 1, - size: 0, - dataType: DefaultDataType, - rangeTuple: [0, 0], - // values: null, - // ranges: null, -}; +function defaultValues(initialValues) { + return { + name: '', + numberOfComponents: 1, + size: 0, + dataType: DefaultDataType, + rangeTuple: [0, 0], + // values: macro.newTypedArray(DefaultValues), + // ranges: null, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); - - if (!model.empty && !model.values && !model.size) { + if (!initialValues.empty && !initialValues.values && !initialValues.size) { throw new TypeError( 'Cannot create vtkDataArray object without: size > 0, values' ); } - - if (!model.values) { - model.values = macro.newTypedArray(model.dataType, model.size); - } else if (Array.isArray(model.values)) { - model.values = macro.newTypedArrayFrom(model.dataType, model.values); - } - - if (model.values) { - model.size = model.values.length; - model.dataType = getDataType(model.values); - } + delete initialValues.empty; + Object.assign(initialValues, defaultValues(initialValues)); // Object methods macro.obj(publicAPI, model); macro.set(publicAPI, model, ['name', 'numberOfComponents']); + model.dataType = initialValues.dataType + ? initialValues.dataType + : DefaultDataType; + delete initialValues.dataType; + if (!initialValues.values) { + if (!initialValues.size) initialValues.values = null; + else + initialValues.values = macro.newTypedArray( + model.dataType, + initialValues.size + ); + } else if (Array.isArray(initialValues.values)) { + initialValues.values = macro.newTypedArrayFrom( + model.dataType, + initialValues.values + ); + } + // Object specific methods vtkDataArray(publicAPI, model); + + // We call customly setData here to keep coherence between parameters before + // the call of publicAPI.set(initialValues) in the constructor + // Warning : setData cannot be overwritten in a child class + publicAPI.setData(initialValues.values, initialValues.numberOfComponents); + delete initialValues.values; + delete initialValues.dataType; + delete initialValues.numberOfComponents; + delete initialValues.size; } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkDataArray'); +export const newInstance = macro.newInstance(extend, 'vtkDataArray', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/Core/DataArray/test/testDataArray.js b/Sources/Common/Core/DataArray/test/testDataArray.js index fc7c7a40376..ec6eff47961 100644 --- a/Sources/Common/Core/DataArray/test/testDataArray.js +++ b/Sources/Common/Core/DataArray/test/testDataArray.js @@ -1,10 +1,254 @@ import test from 'tape-catch'; import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray'; +import Constants from 'vtk.js/Sources/Common/Core/DataArray/Constants'; +import * as macro from 'vtk.js/Sources/macros'; + +const { DefaultDataType } = Constants; + +function getDataArrayProperties(dataArray) { + return { + size: dataArray.get().size, + numberOfComponents: dataArray.get().numberOfComponents, + dataType: dataArray.get().dataType, + values: dataArray.get().values, + }; +} test('Test vtkDataArray instance', (t) => { t.ok(vtkDataArray, 'Make sure the class definition exists'); - const instance = vtkDataArray.newInstance({ size: 256 }); - t.ok(instance); + + t.throws( + () => vtkDataArray.newInstance({}), + 'Not allowed to create instance without initialValues' + ); + + t.doesNotThrow( + () => vtkDataArray.newInstance({ empty: true }), + 'Allowed to create instance with empty true, no data' + ); + + t.throws( + () => vtkDataArray.newInstance({ empty: false }), + 'Not allowed to create instance with empty false, no data' + ); + + t.doesNotThrow( + () => vtkDataArray.newInstance({ size: 256 }), + 'Allowed to create instance with only size' + ); + + const dataArray0 = vtkDataArray.newInstance({ + empty: true, + values: null, + }); + t.deepEqual( + { + dataType: DefaultDataType, + size: 0, + numberOfComponents: 1, + values: null, + }, + getDataArrayProperties(dataArray0), + 'initialValues.values = null' + ); + + const dataArray1 = vtkDataArray.newInstance({ size: 256 }); + t.deepEqual( + { + dataType: DefaultDataType, + size: 256, + numberOfComponents: 1, + values: macro.newTypedArray(DefaultDataType, 256), + }, + getDataArrayProperties(dataArray1), + 'Give only size to create instance' + ); + + const dataArray2 = vtkDataArray.newInstance({ + values: Uint32Array.from([1, 2, 3]), + }); + t.deepEqual( + { + dataType: 'Uint32Array', + size: 3, + numberOfComponents: 1, + values: Uint32Array.from([1, 2, 3]), + }, + getDataArrayProperties(dataArray2), + 'Create instance with data (typed array)' + ); + + const dataArray3 = vtkDataArray.newInstance({ + values: [1, 2, 3], + }); + t.deepEqual( + { + dataType: DefaultDataType, + size: 3, + numberOfComponents: 1, + values: macro.newTypedArrayFrom(DefaultDataType, [1, 2, 3]), + }, + getDataArrayProperties(dataArray3), + 'Create instance with data (untyped array)' + ); + + const dataArray4 = vtkDataArray.newInstance({ + values: [1, 2, 3, 4, 5, 6, 7, 8, 9], + numberOfComponents: 3, + }); + t.deepEqual( + { + dataType: DefaultDataType, + size: 9, + numberOfComponents: 3, + values: macro.newTypedArrayFrom( + DefaultDataType, + [1, 2, 3, 4, 5, 6, 7, 8, 9] + ), + }, + getDataArrayProperties(dataArray4), + 'Change number of components at instanciation' + ); + + const dataArray5 = vtkDataArray.newInstance({ + values: [1, 2, 3], + dataType: null, + }); + t.deepEqual( + { + dataType: DefaultDataType, + size: 3, + numberOfComponents: 1, + values: macro.newTypedArrayFrom(DefaultDataType, [1, 2, 3]), + }, + getDataArrayProperties(dataArray5), + 'Give null as dataType' + ); + + const dataArray6 = vtkDataArray.newInstance({ + empty: true, + values: null, + numberOfComponents: 3, + }); + t.deepEqual( + { + dataType: DefaultDataType, + size: 0, + numberOfComponents: 3, + values: null, + }, + getDataArrayProperties(dataArray6), + 'Give numberOfComponents!=1 with empty array' + ); + + const dataArray7 = vtkDataArray.newInstance({ + size: 3, + numberOfComponents: 3, + }); + t.deepEqual( + { + dataType: DefaultDataType, + size: 3, + numberOfComponents: 3, + values: macro.newTypedArray(DefaultDataType, 3), + }, + getDataArrayProperties(dataArray7), + 'Give only size to create instance' + ); + + t.end(); +}); + +test('Test vtkDataArray setData', (t) => { + const dataArray = vtkDataArray.newInstance({ empty: true }); + + dataArray.setData([4, 5, 6, 7]); + t.deepEqual( + { + dataType: DefaultDataType, + size: 4, + numberOfComponents: 1, + values: macro.newTypedArrayFrom(DefaultDataType, [4, 5, 6, 7]), + }, + getDataArrayProperties(dataArray), + 'Change data of existing instance' + ); + + dataArray.setData([1, 2, 3, 4, 5, 6], 2); + t.deepEqual( + { + dataType: DefaultDataType, + size: 6, + numberOfComponents: 2, + values: macro.newTypedArrayFrom(DefaultDataType, [1, 2, 3, 4, 5, 6]), + }, + getDataArrayProperties(dataArray), + 'Change number of components with setData' + ); + + dataArray.setData([1, 2, 3, 4], 3); + t.deepEqual( + { + dataType: DefaultDataType, + size: 4, + numberOfComponents: 1, + values: macro.newTypedArrayFrom(DefaultDataType, [1, 2, 3, 4]), + }, + getDataArrayProperties(dataArray), + 'Change number of components with setData but wrong numberOfComponents' + ); + + dataArray.setData([]); + t.deepEqual( + { + dataType: DefaultDataType, + size: 0, + numberOfComponents: 1, + values: macro.newTypedArray(DefaultDataType), + }, + getDataArrayProperties(dataArray), + 'Empty an instance (pass [] array)' + ); + + // Fill the DataArray before so that we are sure the size and the numberOfComponents are updated + dataArray.setData([1, 2, 3], 3); + dataArray.setData(null); + t.deepEqual( + { + dataType: DefaultDataType, + size: 0, + numberOfComponents: 3, + values: null, + }, + getDataArrayProperties(dataArray), + 'Call setData with typedArray = null' + ); + + // Fill the DataArray before so that we are sure the size and the numberOfComponents are updated + dataArray.setData([1, 2, 3], 3); + dataArray.setData(); + t.deepEqual( + { + dataType: DefaultDataType, + size: 0, + numberOfComponents: 3, + values: null, + }, + getDataArrayProperties(dataArray), + 'Call setData with typedArray = undefined' + ); + + dataArray.setData(macro.newTypedArrayFrom('Uint32Array', [4, 5, 6, 7])); + t.deepEqual( + { + dataType: 'Uint32Array', + size: 4, + numberOfComponents: 1, + values: macro.newTypedArrayFrom('Uint32Array', [4, 5, 6, 7]), + }, + getDataArrayProperties(dataArray), + 'Change data of existing instance with another type' + ); t.end(); }); diff --git a/Sources/Common/Core/LookupTable/index.js b/Sources/Common/Core/LookupTable/index.js index f80a7ad5047..10e667b318c 100644 --- a/Sources/Common/Core/LookupTable/index.js +++ b/Sources/Common/Core/LookupTable/index.js @@ -294,61 +294,57 @@ function vtkLookupTable(publicAPI, model) { publicAPI.forceBuild(); } }; - - if (model.table.length > 0) { - // ensure insertTime is more recently modified than buildTime if - // a table is provided via the constructor - model.insertTime.modified(); - } } // ---------------------------------------------------------------------------- // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - numberOfColors: 256, - // table: null, - - hueRange: [0.0, 0.66667], - saturationRange: [1.0, 1.0], - valueRange: [1.0, 1.0], - alphaRange: [1.0, 1.0], - - nanColor: [0.5, 0.0, 0.0, 1.0], - belowRangeColor: [0.0, 0.0, 0.0, 1.0], - aboveRangeColor: [1.0, 1.0, 1.0, 1.0], - useAboveRangeColor: false, - useBelowRangeColor: false, - - alpha: 1.0, - // buildTime: null, - // opaqueFlagBuildTime: null, - // insertTime: null, -}; +function defaultValues(initialValues) { + return { + // Internal objects + table: [], + buildTime: macro.obj({}), + opaqueFlagBuildTime: macro.obj({}, { mtime: 0 }), + insertTime: macro.obj({}, { mtime: 0 }), + + numberOfColors: 256, + + hueRange: [0.0, 0.66667], + saturationRange: [1.0, 1.0], + valueRange: [1.0, 1.0], + alphaRange: [1.0, 1.0], + + nanColor: [0.5, 0.0, 0.0, 1.0], + belowRangeColor: [0.0, 0.0, 0.0, 1.0], + aboveRangeColor: [1.0, 1.0, 1.0, 1.0], + useAboveRangeColor: false, + useBelowRangeColor: false, + + alpha: 1.0, + // buildTime: null, + // opaqueFlagBuildTime: null, + // insertTime: null, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkScalarsToColors.extend(publicAPI, model, initialValues); // Internal objects initialization - if (!model.table) { - model.table = []; + // model.table needs to be instanciated manually if it is an array to not call + // setTable in that case + if (Array.isArray(initialValues.table)) { + model.table = initialValues.table; + delete initialValues.table; } - model.buildTime = {}; - macro.obj(model.buildTime); - - model.opaqueFlagBuildTime = {}; - macro.obj(model.opaqueFlagBuildTime, { mtime: 0 }); - - model.insertTime = {}; - macro.obj(model.insertTime, { mtime: 0 }); - // Create get-only macros macro.get(publicAPI, model, ['buildTime']); @@ -389,11 +385,17 @@ export function extend(publicAPI, model, initialValues = {}) { // Object specific methods vtkLookupTable(publicAPI, model); + + if (initialValues.table && initialValues.table.length > 0) { + // ensure insertTime is more recently modified than buildTime if + // a table is provided via the constructor + initialValues.insertTime.modified(); + } } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkLookupTable'); +export const newInstance = macro.newInstance(extend, 'vtkLookupTable', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/Core/Points/index.js b/Sources/Common/Core/Points/index.js index b3f71e46f7f..c3f8108464c 100644 --- a/Sources/Common/Core/Points/index.js +++ b/Sources/Common/Core/Points/index.js @@ -80,25 +80,30 @@ function vtkPoints(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - empty: true, - numberOfComponents: 3, - dataType: VtkDataTypes.FLOAT, - bounds: [1, -1, 1, -1, 1, -1], -}; +function defaultValues(initialValues) { + return { + // empty is only here to be passed to the DataArray extend function in order + // to create Points without giving values + empty: true, + numberOfComponents: 3, + dataType: VtkDataTypes.FLOAT, + bounds: [1, -1, 1, -1, 1, -1], + values: [], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); - + Object.assign(initialValues, defaultValues(initialValues)); vtkDataArray.extend(publicAPI, model, initialValues); vtkPoints(publicAPI, model); } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkPoints'); +export const newInstance = macro.newInstance(extend, 'vtkPoints', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/Core/Points/test/testPoints.js b/Sources/Common/Core/Points/test/testPoints.js new file mode 100644 index 00000000000..4825c2c5c62 --- /dev/null +++ b/Sources/Common/Core/Points/test/testPoints.js @@ -0,0 +1,130 @@ +import test from 'tape-catch'; +import vtkPoints from 'vtk.js/Sources/Common/Core/Points'; +import Constants from 'vtk.js/Sources/Common/Core/DataArray/Constants'; +import * as macro from 'vtk.js/Sources/macros'; + +const { DefaultDataType } = Constants; + +function getPointsProperties(points) { + return { + size: points.get().size, + numberOfComponents: points.get().numberOfComponents, + dataType: points.get().dataType, + values: points.get().values, + }; +} + +test('Test vtkPoints instance', (t) => { + t.ok(vtkPoints, 'Make sure the class definition exists'); + + t.doesNotThrow( + () => vtkPoints.newInstance({ size: 256 }), + 'Allowed to create instance with only size' + ); + + const points0 = vtkPoints.newInstance({ + empty: true, + values: null, + }); + t.deepEqual( + { + dataType: DefaultDataType, + size: 0, + numberOfComponents: 3, + values: null, + }, + getPointsProperties(points0), + 'initialValues.values = null' + ); + + const points1 = vtkPoints.newInstance({ size: 3 }); + t.deepEqual( + { + dataType: DefaultDataType, + size: 3, + numberOfComponents: 3, + values: macro.newTypedArray(DefaultDataType, 3), + }, + getPointsProperties(points1), + 'Give only size to create instance' + ); + + const points2 = vtkPoints.newInstance({ + values: Uint32Array.from([1, 2, 3]), + }); + t.deepEqual( + { + dataType: 'Uint32Array', + size: 3, + numberOfComponents: 3, + values: Uint32Array.from([1, 2, 3]), + }, + getPointsProperties(points2), + 'Create instance with data (typed array)' + ); + + const points3 = vtkPoints.newInstance({ + values: [1, 2, 3], + }); + t.deepEqual( + { + dataType: DefaultDataType, + size: 3, + numberOfComponents: 3, + values: macro.newTypedArrayFrom(DefaultDataType, [1, 2, 3]), + }, + getPointsProperties(points3), + 'Create instance with data (untyped array)' + ); + + const points4 = vtkPoints.newInstance({ + values: [1, 2, 3, 4, 5, 6, 7, 8, 9], + numberOfComponents: 3, + }); + t.deepEqual( + { + dataType: DefaultDataType, + size: 9, + numberOfComponents: 3, + values: macro.newTypedArrayFrom( + DefaultDataType, + [1, 2, 3, 4, 5, 6, 7, 8, 9] + ), + }, + getPointsProperties(points4), + 'Change number of components at instanciation' + ); + + const points5 = vtkPoints.newInstance({ + values: [1, 2, 3], + dataType: null, + }); + t.deepEqual( + { + dataType: DefaultDataType, + size: 3, + numberOfComponents: 3, + values: macro.newTypedArrayFrom(DefaultDataType, [1, 2, 3]), + }, + getPointsProperties(points5), + 'Give null as dataType' + ); + + const points6 = vtkPoints.newInstance({ + empty: true, + values: null, + numberOfComponents: 3, + }); + t.deepEqual( + { + dataType: DefaultDataType, + size: 0, + numberOfComponents: 3, + values: null, + }, + getPointsProperties(points6), + 'Give numberOfComponents!=1 with empty array' + ); + + t.end(); +}); diff --git a/Sources/Common/Core/PriorityQueue/index.js b/Sources/Common/Core/PriorityQueue/index.js index ed22e222e00..156e7f9ca16 100644 --- a/Sources/Common/Core/PriorityQueue/index.js +++ b/Sources/Common/Core/PriorityQueue/index.js @@ -34,14 +34,17 @@ function vtkPriorityQueue(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - elements: [], -}; +function defaultValues(initialValues) { + return { + elements: [], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -50,7 +53,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkPriorityQueue'); +export const newInstance = macro.newInstance(extend, 'vtkPriorityQueue', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/Core/ProgressHandler/index.js b/Sources/Common/Core/ProgressHandler/index.js index d0a4ea757a5..423a8899f09 100644 --- a/Sources/Common/Core/ProgressHandler/index.js +++ b/Sources/Common/Core/ProgressHandler/index.js @@ -50,14 +50,17 @@ function vtkProgressHandler(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - workCount: 0, -}; +function defaultValues(initialValues) { + return { + workCount: 0, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Object methods macro.obj(publicAPI, model); @@ -70,7 +73,11 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkProgressHandler'); +export const newInstance = macro.newInstance( + extend, + 'vtkProgressHandler', + true +); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/Core/ScalarsToColors/index.js b/Sources/Common/Core/ScalarsToColors/index.js index ef5aaf07cd3..9f519d6f90c 100644 --- a/Sources/Common/Core/ScalarsToColors/index.js +++ b/Sources/Common/Core/ScalarsToColors/index.js @@ -517,29 +517,28 @@ function vtkScalarsToColors(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - alpha: 1.0, - vectorComponent: 0, - vectorSize: -1, - vectorMode: VectorMode.COMPONENT, - mappingRange: null, - annotationArray: null, - annotatedValueMap: null, - indexedLookup: false, -}; +function defaultValues(initialValues) { + return { + alpha: 1.0, + vectorComponent: 0, + vectorSize: -1, + vectorMode: VectorMode.COMPONENT, + mappingRange: [0, 255], + annotationArray: [], + annotatedValueMap: [], + indexedLookup: false, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Object methods macro.obj(publicAPI, model); - model.mappingRange = [0, 255]; - model.annotationArray = []; - model.annotatedValueMap = []; - // Create get-set macros macro.setGet(publicAPI, model, [ 'vectorSize', @@ -563,7 +562,11 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkScalarsToColors'); +export const newInstance = macro.newInstance( + extend, + 'vtkScalarsToColors', + true +); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/Core/StringArray/index.js b/Sources/Common/Core/StringArray/index.js index 0ec1533f4bb..639ab08175c 100644 --- a/Sources/Common/Core/StringArray/index.js +++ b/Sources/Common/Core/StringArray/index.js @@ -66,8 +66,14 @@ function vtkStringArray(publicAPI, model) { }; publicAPI.setData = (array, numberOfComponents) => { + if (Array.isArray(array)) { + // eslint-disable-next-line no-param-reassign + array = macro.newTypedArrayFrom(model.dataType, array); + } + model.values = array; - model.size = array.length; + model.size = array ? array.length : 0; + if (numberOfComponents) { model.numberOfComponents = numberOfComponents; } @@ -82,46 +88,64 @@ function vtkStringArray(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - name: '', - numberOfComponents: 1, - size: 0, - // values: null, - dataType: 'string', -}; +function defaultValues(initialValues) { + return { + name: '', + numberOfComponents: 1, + size: 0, + // values: null, + dataType: 'string', + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); - - if (!model.empty && !model.values && !model.size) { + if (!initialValues.empty && !initialValues.values && !initialValues.size) { throw new TypeError( - 'Cannot create vtkStringArray object without: size > 0, values' + 'Cannot create vtkDataArray object without: size > 0, values' ); } - - if (!model.values) { - model.values = []; - } else if (Array.isArray(model.values)) { - model.values = [...model.values]; - } - - if (model.values) { - model.size = model.values.length; - } + delete initialValues.empty; + Object.assign(initialValues, defaultValues(initialValues)); // Object methods macro.obj(publicAPI, model); - macro.set(publicAPI, model, ['name']); + macro.set(publicAPI, model, ['name', 'numberOfComponents']); + + model.dataType = initialValues.dataType ? initialValues.dataType : 'string'; + delete initialValues.dataType; + if (!initialValues.values) { + if (!initialValues.size) initialValues.values = null; + else + initialValues.values = macro.newTypedArray( + model.dataType, + initialValues.size + ); + } else if (Array.isArray(initialValues.values)) { + initialValues.values = macro.newTypedArrayFrom( + model.dataType, + initialValues.values + ); + } // Object specific methods vtkStringArray(publicAPI, model); + + // We call customly setData here to keep coherence between parameters before + // the call of publicAPI.set(initialValues) in the constructor + // Warning : setData cannot be overwritten in a child class + publicAPI.setData(initialValues.values, initialValues.numberOfComponents); + delete initialValues.values; + delete initialValues.dataType; + delete initialValues.numberOfComponents; + delete initialValues.size; } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkStringArray'); +export const newInstance = macro.newInstance(extend, 'vtkStringArray', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/Core/VariantArray/index.js b/Sources/Common/Core/VariantArray/index.js index 38b12c8dd17..7795aa1a46a 100644 --- a/Sources/Common/Core/VariantArray/index.js +++ b/Sources/Common/Core/VariantArray/index.js @@ -66,8 +66,14 @@ function vtkVariantArray(publicAPI, model) { }; publicAPI.setData = (array, numberOfComponents) => { + if (Array.isArray(array)) { + // eslint-disable-next-line no-param-reassign + array = macro.newTypedArrayFrom(model.dataType, array); + } + model.values = array; - model.size = array.length; + model.size = array ? array.length : 0; + if (numberOfComponents) { model.numberOfComponents = numberOfComponents; } @@ -82,46 +88,64 @@ function vtkVariantArray(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - name: '', - numberOfComponents: 1, - size: 0, - // values: null, - dataType: 'JSON', -}; +function defaultValues(initialValues) { + return { + name: '', + numberOfComponents: 1, + size: 0, + // values: null, + dataType: 'JSON', + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); - - if (!model.empty && !model.values && !model.size) { + if (!initialValues.empty && !initialValues.values && !initialValues.size) { throw new TypeError( - 'Cannot create vtkVariantArray object without: size > 0, values' + 'Cannot create vtkDataArray object without: size > 0, values' ); } - - if (!model.values) { - model.values = []; - } else if (Array.isArray(model.values)) { - model.values = [...model.values]; - } - - if (model.values) { - model.size = model.values.length; - } + delete initialValues.empty; + Object.assign(initialValues, defaultValues(initialValues)); // Object methods macro.obj(publicAPI, model); - macro.set(publicAPI, model, ['name']); + macro.set(publicAPI, model, ['name', 'numberOfComponents']); + + model.dataType = initialValues.dataType ? initialValues.dataType : 'JSON'; + delete initialValues.dataType; + if (!initialValues.values) { + if (!initialValues.size) initialValues.values = null; + else + initialValues.values = macro.newTypedArray( + model.dataType, + initialValues.size + ); + } else if (Array.isArray(initialValues.values)) { + initialValues.values = macro.newTypedArrayFrom( + model.dataType, + initialValues.values + ); + } // Object specific methods vtkVariantArray(publicAPI, model); + + // We call customly setData here to keep coherence between parameters before + // the call of publicAPI.set(initialValues) in the constructor + // Warning : setData cannot be overwritten in a child class + publicAPI.setData(initialValues.values, initialValues.numberOfComponents); + delete initialValues.values; + delete initialValues.dataType; + delete initialValues.numberOfComponents; + delete initialValues.size; } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkVariantArray'); +export const newInstance = macro.newInstance(extend, 'vtkVariantArray', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/DataModel/Cell/index.js b/Sources/Common/DataModel/Cell/index.js index c1f537ff6e9..aa3a9b018f5 100644 --- a/Sources/Common/DataModel/Cell/index.js +++ b/Sources/Common/DataModel/Cell/index.js @@ -122,22 +122,22 @@ function vtkCell(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - bounds: [-1, -1, -1, -1, -1, -1], - pointsIds: [], -}; +function defaultValues(initialValues) { + return { + points: vtkPoints.newInstance(), + bounds: [-1, -1, -1, -1, -1, -1], + pointsIds: [], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); macro.obj(publicAPI, model); - if (!model.points) { - model.points = vtkPoints.newInstance(); - } - macro.get(publicAPI, model, ['points', 'pointsIds']); vtkCell(publicAPI, model); @@ -145,7 +145,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkCell'); +export const newInstance = macro.newInstance(extend, 'vtkCell', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/DataModel/Cell/test/testCell.js b/Sources/Common/DataModel/Cell/test/testCell.js index 751426f978e..97789f26bc2 100644 --- a/Sources/Common/DataModel/Cell/test/testCell.js +++ b/Sources/Common/DataModel/Cell/test/testCell.js @@ -53,7 +53,7 @@ test('Test vtkCell deepCopy', (t) => { cell.initialize(points); const cell2 = vtkCell.newInstance(); - cell2.deepCopy(cell); + cell.deepCopy(cell2); t.notEqual(cell2.getPoints(), points); t.deepEqual(cell2.getPoints().getData(), points.getData()); diff --git a/Sources/Common/DataModel/DataSetAttributes/FieldData.js b/Sources/Common/DataModel/DataSetAttributes/FieldData.js index 0658ef16ec8..4e4897acefc 100644 --- a/Sources/Common/DataModel/DataSetAttributes/FieldData.js +++ b/Sources/Common/DataModel/DataSetAttributes/FieldData.js @@ -10,11 +10,6 @@ function vtkFieldData(publicAPI, model) { model.classHierarchy.push('vtkFieldData'); const superGetState = publicAPI.getState; - // Decode serialized data if any - if (model.arrays) { - model.arrays = model.arrays.map((item) => ({ data: vtk(item.data) })); - } - publicAPI.initialize = () => { publicAPI.initializeFields(); publicAPI.copyAllOn(); @@ -57,12 +52,14 @@ function vtkFieldData(publicAPI, model) { ? publicAPI.getArrayByIndex(arraySpec) : publicAPI.getArrayByName(arraySpec); publicAPI.getArrayByName = (arrayName) => - model.arrays.reduce( - (a, b, i) => (b.data.getName() === arrayName ? b.data : a), - null - ); + model.arrays + ? model.arrays.reduce( + (a, b, i) => (b.data.getName() === arrayName ? b.data : a), + null + ) + : null; publicAPI.getArrayWithIndex = (arrayName) => - model.arrays.reduce( + (model.arrays || []).reduce( (a, b, i) => b.data && b.data.getName() === arrayName ? { array: b.data, index: i } @@ -71,8 +68,10 @@ function vtkFieldData(publicAPI, model) { ); publicAPI.getArrayByIndex = (idx) => idx >= 0 && idx < model.arrays.length ? model.arrays[idx].data : null; - publicAPI.hasArray = (arrayName) => - publicAPI.getArrayWithIndex(arrayName).index >= 0; + publicAPI.hasArray = (arrayName) => { + const array = publicAPI.getArrayWithIndex(arrayName); + return array && array.index >= 0; + }; publicAPI.getArrayName = (idx) => { const arr = model.arrays[idx]; return arr ? arr.data.getName() : ''; @@ -158,11 +157,13 @@ function vtkFieldData(publicAPI, model) { // TODO: publicAPI.squeeze = () => model.arrays.forEach(entry => entry.data.squeeze()); publicAPI.reset = () => model.arrays.forEach((entry) => entry.data.reset()); // TODO: getActualMemorySize - publicAPI.getMTime = () => - model.arrays.reduce( + publicAPI.getMTime = () => { + if (!model.arrays) return 0; + return model.arrays.reduce( (a, b) => (b.data.getMTime() > a ? b.data.getMTime() : a), model.mtime ); + }; // TODO: publicAPI.getField = (ids, other) => { copy ids from other into this model's arrays } // TODO: publicAPI.getArrayContainingComponent = (component) => ... publicAPI.getNumberOfComponents = () => @@ -181,24 +182,33 @@ function vtkFieldData(publicAPI, model) { }; } -const DEFAULT_VALUES = { - arrays: [], - copyFieldFlags: [], // fields not to copy - doCopyAllOn: true, - doCopyAllOff: false, -}; +function defaultValues(initialValues) { + return { + arrays: [], + copyFieldFlags: [], // fields not to copy + doCopyAllOn: true, + doCopyAllOff: false, + ...initialValues, + }; +} export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); macro.obj(publicAPI, model); + // Decode serialized data if any + if (initialValues.arrays) { + initialValues.arrays = initialValues.arrays.map((item) => ({ + data: vtk(item.data), + })); + } vtkFieldData(publicAPI, model); } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkFieldData'); +export const newInstance = macro.newInstance(extend, 'vtkFieldData', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/DataModel/DataSetAttributes/index.js b/Sources/Common/DataModel/DataSetAttributes/index.js index aceec25a9a2..41a19c878f1 100644 --- a/Sources/Common/DataModel/DataSetAttributes/index.js +++ b/Sources/Common/DataModel/DataSetAttributes/index.js @@ -161,11 +161,14 @@ function vtkDataSetAttributes(publicAPI, model) { publicAPI[`get${value}`] = () => publicAPI.getArrayByIndex(model[activeVal]); publicAPI[`set${value}`] = (da) => publicAPI.setAttribute(da, value); - publicAPI[`setActive${value}`] = (arrayName) => - publicAPI.setActiveAttributeByIndex( - publicAPI.getArrayWithIndex(arrayName).index, + publicAPI[`setActive${value}`] = (arrayNameOrIndex) => { + if (typeof arrayNameOrIndex === 'number') + return publicAPI.setActiveAttributeByIndex(arrayNameOrIndex, value); + return publicAPI.setActiveAttributeByIndex( + publicAPI.getArrayWithIndex(arrayNameOrIndex).index, value ); + }; publicAPI[`copy${value}Off`] = () => { const attType = value.toUpperCase(); model.copyAttributeFlags[AttributeCopyOperations.PASSDATA][ @@ -234,20 +237,23 @@ function vtkDataSetAttributes(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - activeScalars: -1, - activeVectors: -1, - activeTensors: -1, - activeNormals: -1, - activeTCoords: -1, - activeGlobalIds: -1, - activePedigreeIds: -1, -}; +function defaultValues(initialValues) { + return { + activeScalars: -1, + activeVectors: -1, + activeTensors: -1, + activeNormals: -1, + activeTCoords: -1, + activeGlobalIds: -1, + activePedigreeIds: -1, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Object methods vtkFieldData.extend(publicAPI, model, initialValues); @@ -261,9 +267,9 @@ export function extend(publicAPI, model, initialValues = {}) { 'activePedigreeIds', ]); - if (!model.arrays) { - model.arrays = {}; - } + // model.arrays must exist before calling setActiveAttribute + model.arrays = initialValues.arrays; + delete initialValues.arrays; // Object specific methods vtkDataSetAttributes(publicAPI, model); @@ -271,7 +277,11 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkDataSetAttributes'); +export const newInstance = macro.newInstance( + extend, + 'vtkDataSetAttributes', + true +); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/DataModel/Line/index.js b/Sources/Common/DataModel/Line/index.js index 54a1af94fbd..e9a233e6643 100644 --- a/Sources/Common/DataModel/Line/index.js +++ b/Sources/Common/DataModel/Line/index.js @@ -231,12 +231,16 @@ function vtkLine(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = {}; +function defaultValues(initialValues) { + return { + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); vtkCell.extend(publicAPI, model, initialValues); @@ -245,7 +249,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkLine'); +export const newInstance = macro.newInstance(extend, 'vtkLine', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/DataModel/SelectionNode/index.js b/Sources/Common/DataModel/SelectionNode/index.js index ed01bf26ddf..c6402405a58 100644 --- a/Sources/Common/DataModel/SelectionNode/index.js +++ b/Sources/Common/DataModel/SelectionNode/index.js @@ -16,21 +16,23 @@ function vtkSelectionNode(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - contentType: -1, - fieldType: -1, - properties: null, - selectionList: [], -}; +function defaultValues(initialValues) { + return { + contentType: -1, + fieldType: -1, + selectionList: [], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance macro.obj(publicAPI, model); - model.properties = {}; + initialValues.properties = {}; macro.setGet(publicAPI, model, [ 'contentType', 'fieldType', @@ -44,7 +46,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkSelectionNode'); +export const newInstance = macro.newInstance(extend, 'vtkSelectionNode', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Common/DataModel/Triangle/index.js b/Sources/Common/DataModel/Triangle/index.js index fb6e0b3080e..421af34237e 100644 --- a/Sources/Common/DataModel/Triangle/index.js +++ b/Sources/Common/DataModel/Triangle/index.js @@ -573,12 +573,15 @@ function vtkTriangle(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = {}; - +function defaultValues(initialValues) { + return { + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); vtkCell.extend(publicAPI, model, initialValues); @@ -587,7 +590,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkTriangle'); +export const newInstance = macro.newInstance(extend, 'vtkTriangle', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Core/Cutter/index.js b/Sources/Filters/Core/Cutter/index.js index ef1e2cc39e5..05d046b99a4 100644 --- a/Sources/Filters/Core/Cutter/index.js +++ b/Sources/Filters/Core/Cutter/index.js @@ -6,6 +6,7 @@ const { vtkErrorMacro } = macro; function initPolyIterator(pd) { const polys = pd.getPolys().getData(); const strips = pd.getStrips().getData(); + const stripsLength = strips ? strips.length : 0; const it = { done: false, polyIdx: 0, @@ -20,7 +21,7 @@ function initPolyIterator(pd) { const end = start + cellSize; it.polyIdx = end; ret = polys.subarray(start, end); - } else if (it.stripIdx < strips.length) { + } else if (it.stripIdx < stripsLength) { if (it.remainingStripLength === 0) { it.remainingStripLength = strips[it.stripIdx] - 2; // sliding window of 3 points // stripIdx points to the last point in a triangle 3-tuple @@ -35,7 +36,7 @@ function initPolyIterator(pd) { throw new Error('Iterator is done'); } - it.done = it.polyIdx >= polys.length && it.stripIdx >= strips.length; + it.done = it.polyIdx >= polys.length && it.stripIdx >= stripsLength; return ret; }, }; diff --git a/Sources/Filters/General/AppendPolyData/index.js b/Sources/Filters/General/AppendPolyData/index.js index d7cbb3de981..d5f34022534 100644 --- a/Sources/Filters/General/AppendPolyData/index.js +++ b/Sources/Filters/General/AppendPolyData/index.js @@ -10,6 +10,8 @@ const { vtkErrorMacro } = macro; function offsetCellArray(typedArray, offset) { let currentIdx = 0; + // No data to shift + if (!typedArray) return {}; return typedArray.map((value, index) => { if (index === currentIdx) { currentIdx += value + 1; diff --git a/Sources/Filters/Sources/Arrow2DSource/index.js b/Sources/Filters/Sources/Arrow2DSource/index.js index 4dc02785e17..7fc5879eb89 100644 --- a/Sources/Filters/Sources/Arrow2DSource/index.js +++ b/Sources/Filters/Sources/Arrow2DSource/index.js @@ -197,7 +197,7 @@ function defaultValues(initialValues) { // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, defaultValues(initialValues)); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -209,7 +209,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkArrow2DSource'); +export const newInstance = macro.newInstance(extend, 'vtkArrow2DSource', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/ArrowSource/index.js b/Sources/Filters/Sources/ArrowSource/index.js index 947c745dc56..0ae22bd90c2 100644 --- a/Sources/Filters/Sources/ArrowSource/index.js +++ b/Sources/Filters/Sources/ArrowSource/index.js @@ -90,21 +90,24 @@ function vtkArrowSource(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - tipResolution: 6, - tipRadius: 0.1, - tipLength: 0.35, - shaftResolution: 6, - shaftRadius: 0.03, - invert: false, - direction: [1.0, 0.0, 0.0], - pointType: 'Float64Array', -}; +function defaultValues(initialValues) { + return { + tipResolution: 6, + tipRadius: 0.1, + tipLength: 0.35, + shaftResolution: 6, + shaftRadius: 0.03, + invert: false, + direction: [1.0, 0.0, 0.0], + pointType: 'Float64Array', + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -123,7 +126,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkArrowSource'); +export const newInstance = macro.newInstance(extend, 'vtkArrowSource', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/CircleSource/index.js b/Sources/Filters/Sources/CircleSource/index.js index a42a33f25c9..55a85f2bbb7 100644 --- a/Sources/Filters/Sources/CircleSource/index.js +++ b/Sources/Filters/Sources/CircleSource/index.js @@ -84,7 +84,7 @@ function defaultValues(initialValues) { // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, defaultValues(initialValues)); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -96,7 +96,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkCircleSource'); +export const newInstance = macro.newInstance(extend, 'vtkCircleSource', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/ConcentricCylinderSource/index.js b/Sources/Filters/Sources/ConcentricCylinderSource/index.js index 153266e3c8a..a442c2a8d89 100644 --- a/Sources/Filters/Sources/ConcentricCylinderSource/index.js +++ b/Sources/Filters/Sources/ConcentricCylinderSource/index.js @@ -35,8 +35,16 @@ function vtkConcentricCylinderSource(publicAPI, model) { publicAPI.getNumberOfRadius = () => model.radius.length; publicAPI.getRadius = (index = 0) => model.radius[index]; - publicAPI.setRadius = (index, radius) => { - model.radius[index] = radius; + publicAPI.setRadius = (...args) => { + // Handle instanciation time + let n = []; + + if (args.length === 1 || Array.isArray(args[0])) { + n = [...args[0]]; + model.radius = n; + } else if (args.length === 2) { + model.radius[args[0]] = args[1]; + } publicAPI.modified(); }; publicAPI.setCellField = (index, field) => { @@ -403,24 +411,27 @@ function vtkConcentricCylinderSource(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - height: 1.0, - radius: [0.5], - cellFields: [1], - resolution: 6, - startTheta: 0.0, - endTheta: 360.0, - center: [0, 0, 0], - direction: [0.0, 0.0, 1.0], - skipInnerFaces: true, - mask: null, // If present, array to know if a layer should be skipped(=true) - pointType: 'Float64Array', -}; +function defaultValues(initialValues) { + return { + height: 1.0, + radius: [0.5], + cellFields: [1], + resolution: 6, + startTheta: 0.0, + endTheta: 360.0, + center: [0, 0, 0], + direction: [0.0, 0.0, 1.0], + skipInnerFaces: true, + mask: null, // If present, array to know if a layer should be skipped(=true) + pointType: 'Float64Array', + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -441,7 +452,8 @@ export function extend(publicAPI, model, initialValues = {}) { export const newInstance = macro.newInstance( extend, - 'vtkConcentricCylinderSource' + 'vtkConcentricCylinderSource', + true ); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/ConeSource/index.js b/Sources/Filters/Sources/ConeSource/index.js index c912b73c060..9cd626093a5 100644 --- a/Sources/Filters/Sources/ConeSource/index.js +++ b/Sources/Filters/Sources/ConeSource/index.js @@ -84,20 +84,23 @@ function vtkConeSource(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - height: 1.0, - radius: 0.5, - resolution: 6, - center: [0, 0, 0], - direction: [1.0, 0.0, 0.0], - capping: true, - pointType: 'Float64Array', -}; +function defaultValues(initialValues) { + return { + height: 1.0, + radius: 0.5, + resolution: 6, + center: [0, 0, 0], + direction: [1.0, 0.0, 0.0], + capping: true, + pointType: 'Float64Array', + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -109,7 +112,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkConeSource'); +export const newInstance = macro.newInstance(extend, 'vtkConeSource', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/CubeSource/index.js b/Sources/Filters/Sources/CubeSource/index.js index 331d866b427..2dd03b1018b 100644 --- a/Sources/Filters/Sources/CubeSource/index.js +++ b/Sources/Filters/Sources/CubeSource/index.js @@ -267,20 +267,23 @@ function vtkCubeSource(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - xLength: 1.0, - yLength: 1.0, - zLength: 1.0, - center: [0.0, 0.0, 0.0], - rotations: [0.0, 0.0, 0.0], - pointType: 'Float64Array', - generate3DTextureCoordinates: false, -}; +function defaultValues(initialValues) { + return { + xLength: 1.0, + yLength: 1.0, + zLength: 1.0, + center: [0.0, 0.0, 0.0], + rotations: [0.0, 0.0, 0.0], + pointType: 'Float64Array', + generate3DTextureCoordinates: false, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -298,7 +301,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkCubeSource'); +export const newInstance = macro.newInstance(extend, 'vtkCubeSource', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/Cursor3D/index.js b/Sources/Filters/Sources/Cursor3D/index.js index aefd608fb5d..a5472aa01c4 100644 --- a/Sources/Filters/Sources/Cursor3D/index.js +++ b/Sources/Filters/Sources/Cursor3D/index.js @@ -2,6 +2,27 @@ import macro from 'vtk.js/Sources/macros'; import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData'; import vtkCellArray from 'vtk.js/Sources/Common/Core/CellArray'; import vtkPoints from 'vtk.js/Sources/Common/Core/Points'; + +// ---------------------------------------------------------------------------- +// Object factory +// ---------------------------------------------------------------------------- + +function defaultValues(initialValues) { + return { + focus: null, + modelBounds: [-1.0, 1.0, -1.0, 1.0, -1.0, 1.0], + focalPoint: [0.0, 0.0, 0.0], + outline: true, + axes: true, + xShadows: true, + yShadows: true, + zShadows: true, + wrap: false, + translationMode: false, + ...initialValues, + }; +} + // ---------------------------------------------------------------------------- // vtkCursor3D methods // ---------------------------------------------------------------------------- @@ -15,6 +36,7 @@ function vtkCursor3D(publicAPI, model) { return; } if ( + model.modelBounds && model.modelBounds[0] === bounds[0] && model.modelBounds[1] === bounds[1] && model.modelBounds[2] === bounds[2] && @@ -41,6 +63,7 @@ function vtkCursor3D(publicAPI, model) { if (!Array.isArray(points) || points.length < 3) { return; } + if (!model.focalPoint) model.focalPoint = defaultValues().focalPoint; if ( points[0] === model.focalPoint[0] && points[1] === model.focalPoint[1] && @@ -388,27 +411,10 @@ function vtkCursor3D(publicAPI, model) { }; } -// ---------------------------------------------------------------------------- -// Object factory -// ---------------------------------------------------------------------------- - -const DEFAULT_VALUES = { - focus: null, - modelBounds: [-1.0, 1.0, -1.0, 1.0, -1.0, 1.0], - focalPoint: [0.0, 0.0, 0.0], - outline: true, - axes: true, - xShadows: true, - yShadows: true, - zShadows: true, - wrap: false, - translationMode: false, -}; - // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API // Cursor3D @@ -429,7 +435,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkCursor3D'); +export const newInstance = macro.newInstance(extend, 'vtkCursor3D', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/CylinderSource/index.js b/Sources/Filters/Sources/CylinderSource/index.js index 6139d13f6a5..14653e0177f 100644 --- a/Sources/Filters/Sources/CylinderSource/index.js +++ b/Sources/Filters/Sources/CylinderSource/index.js @@ -178,21 +178,24 @@ function vtkCylinderSource(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - height: 1.0, - initAngle: 0, - radius: 1.0, - resolution: 6, - center: [0, 0, 0], - direction: [0.0, 1.0, 0.0], - capping: true, - pointType: 'Float64Array', -}; +function defaultValues(initialValues) { + return { + height: 1.0, + initAngle: 0, + radius: 1.0, + resolution: 6, + center: [0, 0, 0], + direction: [0.0, 1.0, 0.0], + capping: true, + pointType: 'Float64Array', + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -211,7 +214,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkCylinderSource'); +export const newInstance = macro.newInstance(extend, 'vtkCylinderSource', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/ImageGridSource/index.js b/Sources/Filters/Sources/ImageGridSource/index.js index 27c9e3e785c..2a2da2574c4 100644 --- a/Sources/Filters/Sources/ImageGridSource/index.js +++ b/Sources/Filters/Sources/ImageGridSource/index.js @@ -96,21 +96,24 @@ function vtkImageGridSource(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - lineValue: 0, - fillValue: 255, - gridSpacing: [10, 10, 0], - gridOrigin: [0, 0, 0], - dataSpacing: [1.0, 1.0, 1.0], - dataOrigin: [0.0, 0.0, 0.0], - dataExtent: [0, 255, 0, 255, 0, 0], - dataDirection: [1, 0, 0, 0, 1, 0, 0, 0, 1], -}; +function defaultValues(initialValues) { + return { + lineValue: 0, + fillValue: 255, + gridSpacing: [10, 10, 0], + gridOrigin: [0, 0, 0], + dataSpacing: [1.0, 1.0, 1.0], + dataOrigin: [0.0, 0.0, 0.0], + dataExtent: [0, 255, 0, 255, 0, 0], + dataDirection: [1, 0, 0, 0, 1, 0, 0, 0, 1], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -134,7 +137,11 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkImageGridSource'); +export const newInstance = macro.newInstance( + extend, + 'vtkImageGridSource', + true +); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/LineSource/index.js b/Sources/Filters/Sources/LineSource/index.js index 614e0def70b..fa53084c76e 100644 --- a/Sources/Filters/Sources/LineSource/index.js +++ b/Sources/Filters/Sources/LineSource/index.js @@ -72,17 +72,20 @@ function vtkLineSource(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - resolution: 10, - point1: [-1, 0, 0], - point2: [1, 0, 0], - pointType: 'Float64Array', -}; +function defaultValues(initialValues) { + return { + resolution: 10, + point1: [-1, 0, 0], + point2: [1, 0, 0], + pointType: 'Float64Array', + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -94,7 +97,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkLineSource'); +export const newInstance = macro.newInstance(extend, 'vtkLineSource', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/PlaneSource/index.js b/Sources/Filters/Sources/PlaneSource/index.js index 45bf7efc4f5..fe5917bdd8c 100644 --- a/Sources/Filters/Sources/PlaneSource/index.js +++ b/Sources/Filters/Sources/PlaneSource/index.js @@ -122,7 +122,13 @@ function vtkPlaneSource(publicAPI, model) { n = [normal[0], normal[1], normal[2]]; } - if (vtkMath.normalize(n) !== 0) { + const normN = vtkMath.normalize(n); + // If we are at instanciation time + if (model.normal === undefined) { + model.normal = n; + } + + if (normN !== 0) { const dp = vtkMath.dot(model.normal, n); let theta = 0; @@ -180,6 +186,11 @@ function vtkPlaneSource(publicAPI, model) { c = [center[0], center[1], center[2]]; } + // If we are at instanciation time + if (model.center === undefined) { + model.center = c; + } + if (!vec3.exactEquals(c, model.center)) { const v1 = []; vtkMath.subtract(model.point1, model.origin, v1); @@ -206,6 +217,9 @@ function vtkPlaneSource(publicAPI, model) { point1 = [point[0], point[1], point[2]]; } + // If we are at instanciation time + if (model.point1 === undefined) model.point1 = point1; + if (!vec3.exactEquals(point1, model.point1)) { const v1 = []; const v2 = []; @@ -229,6 +243,9 @@ function vtkPlaneSource(publicAPI, model) { point2 = [point[0], point[1], point[2]]; } + // If we are at instanciation time + if (model.point2 === undefined) model.point2 = point2; + if (!vec3.exactEquals(point2, model.point2)) { const v1 = []; const v2 = []; @@ -260,22 +277,26 @@ function vtkPlaneSource(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - xResolution: 10, - yResolution: 10, - origin: [0, 0, 0], - point1: [1, 0, 0], - point2: [0, 1, 0], - pointType: 'Float64Array', -}; +function defaultValues(initialValues) { + return { + // Internal + normal: [0, 0, 1], + center: [0, 0, 0], + + xResolution: 10, + yResolution: 10, + origin: [0, 0, 0], + point1: [1, 0, 0], + point2: [0, 1, 0], + pointType: 'Float64Array', + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); - - model.normal = [0, 0, 1]; - model.center = [0, 0, 0]; + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -285,14 +306,11 @@ export function extend(publicAPI, model, initialValues = {}) { macro.algo(publicAPI, model, 0, 1); vtkPlaneSource(publicAPI, model); - - publicAPI.setPoint1(model.point1); - publicAPI.setPoint2(model.point2); } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkPlaneSource'); +export const newInstance = macro.newInstance(extend, 'vtkPlaneSource', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/PointSource/index.js b/Sources/Filters/Sources/PointSource/index.js index 85f22684316..a0131e2077c 100644 --- a/Sources/Filters/Sources/PointSource/index.js +++ b/Sources/Filters/Sources/PointSource/index.js @@ -66,17 +66,20 @@ function vtkPointSource(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - numberOfPoints: 10, - center: [0, 0, 0], - radius: 0.5, - pointType: 'Float64Array', -}; +function defaultValues(initialValues) { + return { + numberOfPoints: 10, + center: [0, 0, 0], + radius: 0.5, + pointType: 'Float64Array', + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -88,7 +91,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkPointSource'); +export const newInstance = macro.newInstance(extend, 'vtkPointSource', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/RTAnalyticSource/index.js b/Sources/Filters/Sources/RTAnalyticSource/index.js index 3ff7cbc0279..bb70cd90397 100644 --- a/Sources/Filters/Sources/RTAnalyticSource/index.js +++ b/Sources/Filters/Sources/RTAnalyticSource/index.js @@ -103,21 +103,24 @@ function vtkRTAnalyticSource(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - offset: 40, - maximum: 120, - center: [0, 0, 0], - frequency: [60, 30, 40], - magnitude: [10, 18, 5], - standardDeviation: 0.5, - wholeExtent: [-10, 10, -10, 10, -10, 10], - dataDirection: [1, 0, 0, 0, 1, 0, 0, 0, 1], -}; +function defaultValues(initialValues) { + return { + offset: 40, + maximum: 120, + center: [0, 0, 0], + frequency: [60, 30, 40], + magnitude: [10, 18, 5], + standardDeviation: 0.5, + wholeExtent: [-10, 10, -10, 10, -10, 10], + dataDirection: [1, 0, 0, 0, 1, 0, 0, 0, 1], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -136,7 +139,11 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkRTAnalyticSource'); +export const newInstance = macro.newInstance( + extend, + 'vtkRTAnalyticSource', + true +); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/SLICSource/index.js b/Sources/Filters/Sources/SLICSource/index.js index fbe88ed700c..532b79f0769 100644 --- a/Sources/Filters/Sources/SLICSource/index.js +++ b/Sources/Filters/Sources/SLICSource/index.js @@ -176,19 +176,22 @@ function vtkSLICSource(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - clusters: [], - spacing: [1.0, 1.0, 1.0], - origin: [0.0, 0.0, 0.0], - dimensions: [10, 10, 10], - clusterArrayName: 'cluster', - scalarArrayName: 'field', -}; +function defaultValues(initialValues) { + return { + clusters: [], + spacing: [1.0, 1.0, 1.0], + origin: [0.0, 0.0, 0.0], + dimensions: [10, 10, 10], + clusterArrayName: 'cluster', + scalarArrayName: 'field', + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -202,7 +205,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkSLICSource'); +export const newInstance = macro.newInstance(extend, 'vtkSLICSource', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/SphereSource/index.js b/Sources/Filters/Sources/SphereSource/index.js index d06008f16da..08559427b61 100644 --- a/Sources/Filters/Sources/SphereSource/index.js +++ b/Sources/Filters/Sources/SphereSource/index.js @@ -201,23 +201,26 @@ function vtkSphereSource(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - radius: 0.5, - latLongTessellation: false, - thetaResolution: 8, - startTheta: 0.0, - endTheta: 360.0, - phiResolution: 8, - startPhi: 0.0, - endPhi: 180.0, - center: [0, 0, 0], - pointType: 'Float64Array', -}; +function defaultValues(initialValues) { + return { + radius: 0.5, + latLongTessellation: false, + thetaResolution: 8, + startTheta: 0.0, + endTheta: 360.0, + phiResolution: 8, + startPhi: 0.0, + endPhi: 180.0, + center: [0, 0, 0], + pointType: 'Float64Array', + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -238,7 +241,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkSphereSource'); +export const newInstance = macro.newInstance(extend, 'vtkSphereSource', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Filters/Sources/ViewFinderSource/index.js b/Sources/Filters/Sources/ViewFinderSource/index.js index 873c238391a..01eb8e2940c 100644 --- a/Sources/Filters/Sources/ViewFinderSource/index.js +++ b/Sources/Filters/Sources/ViewFinderSource/index.js @@ -92,20 +92,24 @@ function vtkViewFinderSource(publicAPI, model) { // ---------------------------------------------------------------------------- // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - radius: 1, - spacing: 2, - width: 4, - pointType: 'Float64Array', -}; +function defaultValues(initialValues) { + return { + //Internal + center: [0, 0, 0], + orientation: [1, 0, 0], + + radius: 1, + spacing: 2, + width: 4, + pointType: 'Float64Array', + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - model.center = [0, 0, 0]; - model.orientation = [1, 0, 0]; - - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -117,7 +121,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkArrow2DSource'); +export const newInstance = macro.newInstance(extend, 'vtkArrow2DSource', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/AbstractMapper/index.js b/Sources/Rendering/Core/AbstractMapper/index.js index d3930694237..ca683775e3a 100644 --- a/Sources/Rendering/Core/AbstractMapper/index.js +++ b/Sources/Rendering/Core/AbstractMapper/index.js @@ -41,7 +41,9 @@ function vtkAbstractMapper(publicAPI, model) { publicAPI.getClippingPlanes = () => model.clippingPlanes; publicAPI.setClippingPlanes = (planes) => { - if (!planes) { + // Instanciation time + if (model.clippingPlanes === undefined) { + model.clippingPlanes = []; return; } if (!Array.isArray(planes)) { @@ -89,23 +91,22 @@ function vtkAbstractMapper(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - clippingPlanes: [], -}; +function defaultValues(initialValues) { + return { + clippingPlanes: [], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Object methods macro.obj(publicAPI, model); macro.algo(publicAPI, model, 1, 0); - if (!model.clippingPlanes) { - model.clippingPlanes = []; - } - vtkAbstractMapper(publicAPI, model); } diff --git a/Sources/Rendering/Core/AbstractMapper3D/index.js b/Sources/Rendering/Core/AbstractMapper3D/index.js index 7f0f71b99cd..10f1654547f 100644 --- a/Sources/Rendering/Core/AbstractMapper3D/index.js +++ b/Sources/Rendering/Core/AbstractMapper3D/index.js @@ -39,24 +39,23 @@ function vtkAbstractMapper3D(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - bounds: [1, -1, 1, -1, 1, -1], - center: [0, 0, 0], -}; +function defaultValues(initialValues) { + return { + bounds: [1, -1, 1, -1, 1, -1], + center: [0, 0, 0], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkAbstractMapper.extend(publicAPI, model, initialValues); - if (!model.bounds) { - vtkMath.uninitializeBounds(model.bounds); - } - - if (!model.center) { - model.center = [0.0, 0.0, 0.0]; + if (!initialValues.bounds) { + vtkMath.uninitializeBounds(initialValues.bounds); } vtkAbstractMapper3D(publicAPI, model); diff --git a/Sources/Rendering/Core/Actor/index.js b/Sources/Rendering/Core/Actor/index.js index 43f769187f6..1f5e1f4f49a 100644 --- a/Sources/Rendering/Core/Actor/index.js +++ b/Sources/Rendering/Core/Actor/index.js @@ -128,12 +128,12 @@ function vtkActor(publicAPI, model) { publicAPI.getMTime = () => { let mt = superClass.getMTime(); - if (model.property !== null) { + if (model.property) { const time = model.property.getMTime(); mt = time > mt ? time : mt; } - if (model.backfaceProperty !== null) { + if (model.backfaceProperty) { const time = model.backfaceProperty.getMTime(); mt = time > mt ? time : mt; } @@ -170,29 +170,32 @@ function vtkActor(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - mapper: null, - property: null, - backfaceProperty: null, +function defaultValues(initialValues) { + return { + // Internal objects + boundsMTime: macro.obj({}), - forceOpaque: false, - forceTranslucent: false, + mapper: null, + property: null, + backfaceProperty: null, - bounds: [1, -1, 1, -1, 1, -1], -}; + forceOpaque: false, + forceTranslucent: false, + + bounds: [1, -1, 1, -1, 1, -1], + + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkProp3D.extend(publicAPI, model, initialValues); - // vtkTimeStamp - model.boundsMTime = {}; - macro.obj(model.boundsMTime); - // Build VTK API macro.set(publicAPI, model, ['property']); macro.setGet(publicAPI, model, [ @@ -208,7 +211,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkActor'); +export const newInstance = macro.newInstance(extend, 'vtkActor', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/Actor2D/index.js b/Sources/Rendering/Core/Actor2D/index.js index d66fdedbd8b..19436acd25b 100644 --- a/Sources/Rendering/Core/Actor2D/index.js +++ b/Sources/Rendering/Core/Actor2D/index.js @@ -137,28 +137,31 @@ function vtkActor2D(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - mapper: null, - property: null, - layerNumber: 0, - positionCoordinate: null, - positionCoordinate2: null, -}; +function defaultValues(initialValues) { + return { + mapper: null, + property: null, + layerNumber: 0, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkProp.extend(publicAPI, model, initialValues); - model.positionCoordinate = vtkCoordinate.newInstance(); - model.positionCoordinate.setCoordinateSystemToViewport(); - model.positionCoordinate2 = vtkCoordinate.newInstance(); - model.positionCoordinate2.setCoordinateSystemToNormalizedViewport(); - model.positionCoordinate2.setValue(0.5, 0.5); - model.positionCoordinate2.setReferenceCoordinate(model.positionCoordinate); + initialValues.positionCoordinate = vtkCoordinate.newInstance(); + initialValues.positionCoordinate.setCoordinateSystemToViewport(); + initialValues.positionCoordinate2 = vtkCoordinate.newInstance(); + initialValues.positionCoordinate2.setCoordinateSystemToNormalizedViewport(); + initialValues.positionCoordinate2.setValue(0.5, 0.5); + initialValues.positionCoordinate2.setReferenceCoordinate( + initialValues.positionCoordinate + ); // Build VTK API macro.set(publicAPI, model, ['property']); @@ -170,7 +173,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkActor2D'); +export const newInstance = macro.newInstance(extend, 'vtkActor2D', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/Camera/index.js b/Sources/Rendering/Core/Camera/index.js index 0afae14541a..85cfae81f89 100644 --- a/Sources/Rendering/Core/Camera/index.js +++ b/Sources/Rendering/Core/Camera/index.js @@ -17,6 +17,41 @@ const { vtkDebugMacro } = macro; // return matrix[idx]; // } +// ---------------------------------------------------------------------------- +// Object factory +// ---------------------------------------------------------------------------- + +export function defaultValues(initialValues) { + return { + position: [0, 0, 1], + focalPoint: [0, 0, 0], + viewUp: [0, 1, 0], + directionOfProjection: [0, 0, -1], + parallelProjection: false, + useHorizontalViewAngle: false, + viewAngle: 30, + parallelScale: 1, + clippingRange: [0.01, 1000.01], + windowCenter: [0, 0], + viewPlaneNormal: [0, 0, 1], + useOffAxisProjection: false, + screenBottomLeft: [-0.5, -0.5, -0.5], + screenBottomRight: [0.5, -0.5, -0.5], + screenTopRight: [0.5, 0.5, -0.5], + freezeFocalPoint: false, + projectionMatrix: null, + viewMatrix: null, + cameraLightTransform: mat4.create(), + + // used for world to physical transformations + physicalTranslation: [0, 0, 0], + physicalScale: 1.0, + physicalViewUp: [0, 1, 0], + physicalViewNorth: [0, 0, -1], + ...initialValues, + }; +} + // ---------------------------------------------------------------------------- // vtkCamera methods // ---------------------------------------------------------------------------- @@ -42,6 +77,10 @@ function vtkCamera(publicAPI, model) { // Internal Functions that don't need to be public function computeViewPlaneNormal() { + // Instanciation time + if (model.viewPlaneNormal === undefined) { + model.viewPlaneNormal = defaultValues().viewPlaneNormal; + } // VPN is -DOP model.viewPlaneNormal[0] = -model.directionOfProjection[0]; model.viewPlaneNormal[1] = -model.directionOfProjection[1]; @@ -59,12 +98,17 @@ function vtkCamera(publicAPI, model) { publicAPI.setPosition = (x, y, z) => { if ( + model.position && x === model.position[0] && y === model.position[1] && z === model.position[2] ) { return; } + // Instanciation time + if (model.position === undefined) { + model.position = defaultValues().position; + } model.position[0] = x; model.position[1] = y; @@ -77,13 +121,17 @@ function vtkCamera(publicAPI, model) { publicAPI.setFocalPoint = (x, y, z) => { if ( + model.focalPoint && x === model.focalPoint[0] && y === model.focalPoint[1] && z === model.focalPoint[2] ) { return; } - + // Instanciation time + if (model.focalPoint === undefined) { + model.focalPoint = defaultValues().focalPoint; + } model.focalPoint[0] = x; model.focalPoint[1] = y; model.focalPoint[2] = z; @@ -118,12 +166,24 @@ function vtkCamera(publicAPI, model) { //---------------------------------------------------------------------------- // This method must be called when the focal point or camera position changes publicAPI.computeDistance = () => { + // Instanciation time : computeDistance can be called either when model.position + // or model.focalPoint are undefined + if (model.position === undefined) { + model.position = defaultValues().position; + } + if (model.focalPoint === undefined) { + model.focalPoint = defaultValues().focalPoint; + } const dx = model.focalPoint[0] - model.position[0]; const dy = model.focalPoint[1] - model.position[1]; const dz = model.focalPoint[2] - model.position[2]; model.distance = Math.sqrt(dx * dx + dy * dy + dz * dz); + if (model.directionOfProjection === undefined) { + model.directionOfProjection = defaultValues().directionOfProjection; + } + if (model.distance < 1e-20) { model.distance = 1e-20; vtkDebugMacro('Distance is set to minimum.'); @@ -709,42 +769,10 @@ function vtkCamera(publicAPI, model) { }; } -// ---------------------------------------------------------------------------- -// Object factory -// ---------------------------------------------------------------------------- - -export const DEFAULT_VALUES = { - position: [0, 0, 1], - focalPoint: [0, 0, 0], - viewUp: [0, 1, 0], - directionOfProjection: [0, 0, -1], - parallelProjection: false, - useHorizontalViewAngle: false, - viewAngle: 30, - parallelScale: 1, - clippingRange: [0.01, 1000.01], - windowCenter: [0, 0], - viewPlaneNormal: [0, 0, 1], - useOffAxisProjection: false, - screenBottomLeft: [-0.5, -0.5, -0.5], - screenBottomRight: [0.5, -0.5, -0.5], - screenTopRight: [0.5, 0.5, -0.5], - freezeFocalPoint: false, - projectionMatrix: null, - viewMatrix: null, - cameraLightTransform: mat4.create(), - - // used for world to physical transformations - physicalTranslation: [0, 0, 0], - physicalScale: 1.0, - physicalViewUp: [0, 1, 0], - physicalViewNorth: [0, 0, -1], -}; - // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -791,7 +819,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkCamera'); +export const newInstance = macro.newInstance(extend, 'vtkCamera', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/ColorTransferFunction/index.js b/Sources/Rendering/Core/ColorTransferFunction/index.js index 5287da2cc91..f3a1146c5fc 100644 --- a/Sources/Rendering/Core/ColorTransferFunction/index.js +++ b/Sources/Rendering/Core/ColorTransferFunction/index.js @@ -1197,48 +1197,45 @@ function vtkColorTransferFunction(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - clamping: true, - colorSpace: ColorSpace.RGB, - hSVWrap: true, - scale: Scale.LINEAR, - - nanColor: null, - belowRangeColor: null, - aboveRangeColor: null, - useAboveRangeColor: false, - useBelowRangeColor: false, - - allowDuplicateScalars: false, - - table: null, - tableSize: 0, - buildTime: null, - - nodes: null, - - discretize: false, - numberOfValues: 256, -}; +function defaultValues(initialValues) { + return { + // Internal objects + buildTime: macro.obj({}), + nodes: [], + table: [], + + clamping: true, + colorSpace: ColorSpace.RGB, + hSVWrap: true, + scale: Scale.LINEAR, + + nanColor: [0.5, 0.0, 0.0, 1.0], + belowRangeColor: [0.0, 0.0, 0.0, 1.0], + aboveRangeColor: [1.0, 1.0, 1.0, 1.0], + useAboveRangeColor: false, + useBelowRangeColor: false, + + allowDuplicateScalars: false, + + tableSize: 0, + + discretize: false, + numberOfValues: 256, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkScalarsToColors.extend(publicAPI, model, initialValues); - // Internal objects initialization - model.table = []; - model.nodes = []; - - model.nanColor = [0.5, 0.0, 0.0, 1.0]; - model.belowRangeColor = [0.0, 0.0, 0.0, 1.0]; - model.aboveRangeColor = [1.0, 1.0, 1.0, 1.0]; - - model.buildTime = {}; - macro.obj(model.buildTime); + // setMappingRange requires previous value for model.mappingRange + model.mappingRange = initialValues.mappingRange; + delete initialValues.mappingRange; // Create get-only macros macro.get(publicAPI, model, ['buildTime', 'mappingRange']); @@ -1276,7 +1273,8 @@ export function extend(publicAPI, model, initialValues = {}) { export const newInstance = macro.newInstance( extend, - 'vtkColorTransferFunction' + 'vtkColorTransferFunction', + true ); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/Coordinate/index.js b/Sources/Rendering/Core/Coordinate/index.js index 691c5189e30..164df3a97e3 100644 --- a/Sources/Rendering/Core/Coordinate/index.js +++ b/Sources/Rendering/Core/Coordinate/index.js @@ -33,14 +33,19 @@ function vtkCoordinate(publicAPI, model) { throw new RangeError('Invalid number of values for array setter'); } let changeDetected = false; - model.value.forEach((item, index) => { - if (item !== array[index]) { - if (changeDetected) { - return; + // Instanciation time : if model.value is undefined, change needs to be done + if (model.value) { + model.value.forEach((item, index) => { + if (item !== array[index]) { + if (changeDetected) { + return; + } + changeDetected = true; } - changeDetected = true; - } - }); + }); + } else { + changeDetected = true; + } if (changeDetected) { model.value = [].concat(array); @@ -581,20 +586,23 @@ function vtkCoordinate(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - coordinateSystem: Coordinate.WORLD, - value: [0.0, 0.0, 0.0], - renderer: null, - referenceCoordinate: null, - computing: 0, - computedWorldValue: [0.0, 0.0, 0.0], - computedDoubleDisplayValue: [0.0, 0.0], -}; +function defaultValues(initialValues) { + return { + coordinateSystem: Coordinate.WORLD, + value: [0.0, 0.0, 0.0], + renderer: null, + referenceCoordinate: null, + computing: 0, + computedWorldValue: [0.0, 0.0, 0.0], + computedDoubleDisplayValue: [0.0, 0.0], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); macro.obj(publicAPI, model); @@ -615,7 +623,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkCoordinate'); +export const newInstance = macro.newInstance(extend, 'vtkCoordinate', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/HardwareSelector/index.js b/Sources/Rendering/Core/HardwareSelector/index.js index 76abb383cc0..5326b8d8643 100644 --- a/Sources/Rendering/Core/HardwareSelector/index.js +++ b/Sources/Rendering/Core/HardwareSelector/index.js @@ -35,15 +35,18 @@ function vtkHardwareSelector(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - fieldAssociation: FieldAssociations.FIELD_ASSOCIATION_CELLS, - captureZValues: false, -}; +function defaultValues(initialValues) { + return { + fieldAssociation: FieldAssociations.FIELD_ASSOCIATION_CELLS, + captureZValues: false, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance macro.obj(publicAPI, model); @@ -56,7 +59,11 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkHardwareSelector'); +export const newInstance = macro.newInstance( + extend, + 'vtkHardwareSelector', + true +); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/Light/index.js b/Sources/Rendering/Core/Light/index.js index ac367f07f81..dd1a300fe6c 100644 --- a/Sources/Rendering/Core/Light/index.js +++ b/Sources/Rendering/Core/Light/index.js @@ -87,27 +87,31 @@ function vtkLight(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - switch: true, - intensity: 1, - color: [1, 1, 1], - position: [0, 0, 1], - focalPoint: [0, 0, 0], - positional: false, - exponent: 1, - coneAngle: 30, - attenuationValues: [1, 0, 0], - transformMatrix: null, - lightType: 'SceneLight', - shadowAttenuation: 1, - direction: [0, 0, 0], - directionMTime: 0, -}; +function defaultValues(initialValues) { + return { + switch: true, + intensity: 1, + color: [1, 1, 1], + position: [0, 0, 1], + focalPoint: [0, 0, 0], + positional: false, + exponent: 1, + coneAngle: 30, + attenuationValues: [1, 0, 0], + transformMatrix: null, + lightType: 'SceneLight', + shadowAttenuation: 1, + direction: [0, 0, 0], + directionMTime: 0, + + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -135,7 +139,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkLight'); +export const newInstance = macro.newInstance(extend, 'vtkLight', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/Mapper/index.js b/Sources/Rendering/Core/Mapper/index.js index 595b751c1aa..215abeb8288 100644 --- a/Sources/Rendering/Core/Mapper/index.js +++ b/Sources/Rendering/Core/Mapper/index.js @@ -567,47 +567,50 @@ function vtkMapper(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - colorMapColors: null, // Same as this->Colors +function defaultValues(initialValues) { + return { + colorMapColors: null, // Same as this->Colors - static: false, - lookupTable: null, + static: false, + lookupTable: null, - scalarVisibility: true, - scalarRange: [0, 1], - useLookupTableScalarRange: false, + scalarVisibility: true, + scalarRange: [0, 1], + useLookupTableScalarRange: false, - colorMode: 0, - scalarMode: 0, - arrayAccessMode: 1, // By_NAME + colorMode: 0, + scalarMode: 0, + arrayAccessMode: 1, // By_NAME - renderTime: 0, + renderTime: 0, - colorByArrayName: null, + colorByArrayName: null, - fieldDataTupleId: -1, + fieldDataTupleId: -1, - populateSelectionSettings: true, - selectionWebGLIdsToVTKIds: null, + populateSelectionSettings: true, + selectionWebGLIdsToVTKIds: null, - interpolateScalarsBeforeMapping: false, - colorCoordinates: null, - colorTextureMap: null, + interpolateScalarsBeforeMapping: false, + colorCoordinates: null, + colorTextureMap: null, - forceCompileOnly: 0, + forceCompileOnly: 0, - useInvertibleColors: false, - invertibleScalars: null, + useInvertibleColors: false, + invertibleScalars: null, - viewSpecificProperties: null, + viewSpecificProperties: {}, - customShaderAttributes: [], -}; + customShaderAttributes: [], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkAbstractMapper3D.extend(publicAPI, model, initialValues); @@ -636,10 +639,6 @@ export function extend(publicAPI, model, initialValues = {}) { ]); macro.setGetArray(publicAPI, model, ['scalarRange'], 2); - if (!model.viewSpecificProperties) { - model.viewSpecificProperties = {}; - } - CoincidentTopologyHelper.implementCoincidentTopologyMethods(publicAPI, model); // Object methods @@ -648,7 +647,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkMapper'); +export const newInstance = macro.newInstance(extend, 'vtkMapper', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/Mapper2D/index.js b/Sources/Rendering/Core/Mapper2D/index.js index 51b3b8e8817..897df3c5e07 100644 --- a/Sources/Rendering/Core/Mapper2D/index.js +++ b/Sources/Rendering/Core/Mapper2D/index.js @@ -147,31 +147,34 @@ function vtkMapper2D(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - static: false, - lookupTable: null, +function defaultValues(initialValues) { + return { + static: false, + lookupTable: null, - scalarVisibility: false, - scalarRange: [0, 1], - useLookupTableScalarRange: false, + scalarVisibility: false, + scalarRange: [0, 1], + useLookupTableScalarRange: false, - colorMode: 0, - scalarMode: 0, - arrayAccessMode: 1, // By_NAME + colorMode: 0, + scalarMode: 0, + arrayAccessMode: 1, // By_NAME - renderTime: 0, + renderTime: 0, - colorByArrayName: null, + colorByArrayName: null, - transformCoordinate: null, + transformCoordinate: null, - viewSpecificProperties: null, - customShaderAttributes: [], -}; + viewSpecificProperties: {}, + customShaderAttributes: [], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkAbstractMapper.extend(publicAPI, model, initialValues); @@ -193,17 +196,13 @@ export function extend(publicAPI, model, initialValues = {}) { ]); macro.setGetArray(publicAPI, model, ['scalarRange'], 2); - if (!model.viewSpecificProperties) { - model.viewSpecificProperties = {}; - } - // Object methods vtkMapper2D(publicAPI, model); } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkMapper2D'); +export const newInstance = macro.newInstance(extend, 'vtkMapper2D', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/Prop/index.js b/Sources/Rendering/Core/Prop/index.js index 41ccf2ca755..aad813b96f5 100644 --- a/Sources/Rendering/Core/Prop/index.js +++ b/Sources/Rendering/Core/Prop/index.js @@ -4,6 +4,27 @@ function notImplemented(method) { return () => macro.vtkErrorMacro(`vtkProp::${method} - NOT IMPLEMENTED`); } +// ---------------------------------------------------------------------------- +// Object factory +// ---------------------------------------------------------------------------- + +function defaultValues(initialValues) { + return { + // _parentProp: null, + visibility: true, + pickable: true, + dragable: true, + useBounds: true, + allocatedRenderTime: 10, + estimatedRenderTime: 0, + savedEstimatedRenderTime: 0, + renderTimeMultiplier: 1, + paths: null, + textures: [], + ...initialValues, + }; +} + // ---------------------------------------------------------------------------- // vtkProp methods // ---------------------------------------------------------------------------- @@ -13,9 +34,10 @@ function vtkProp(publicAPI, model) { model.classHierarchy.push('vtkProp'); publicAPI.getMTime = () => { + const textures = model.textures ? model.textures : defaultValues().textures; let m1 = model.mtime; - for (let index = 0; index < model.textures.length; ++index) { - const m2 = model.textures[index].getMTime(); + for (let index = 0; index < textures.length; ++index) { + const m2 = textures[index].getMTime(); if (m2 > m1) { m1 = m2; } @@ -89,28 +111,11 @@ function vtkProp(publicAPI, model) { }; } -// ---------------------------------------------------------------------------- -// Object factory -// ---------------------------------------------------------------------------- - -const DEFAULT_VALUES = { - // _parentProp: null, - visibility: true, - pickable: true, - dragable: true, - useBounds: true, - allocatedRenderTime: 10, - estimatedRenderTime: 0, - savedEstimatedRenderTime: 0, - renderTimeMultiplier: 1, - paths: null, - textures: [], -}; - // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + macro.moveToProtected(publicAPI, initialValues, ['parentProp']); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -123,7 +128,6 @@ export function extend(publicAPI, model, initialValues = {}) { 'renderTimeMultiplier', '_parentProp', ]); - macro.moveToProtected(publicAPI, model, ['parentProp']); // Object methods vtkProp(publicAPI, model); @@ -131,7 +135,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkProp'); +export const newInstance = macro.newInstance(extend, 'vtkProp', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/Prop3D/index.js b/Sources/Rendering/Core/Prop3D/index.js index adc7bd9ca1b..3c29d34654e 100644 --- a/Sources/Rendering/Core/Prop3D/index.js +++ b/Sources/Rendering/Core/Prop3D/index.js @@ -83,6 +83,7 @@ function vtkProp3D(publicAPI, model) { publicAPI.setOrientation = (x, y, z) => { if ( + model.orientation && x === model.orientation[0] && y === model.orientation[1] && z === model.orientation[2] @@ -90,6 +91,9 @@ function vtkProp3D(publicAPI, model) { return false; } model.orientation = [x, y, z]; + // Instanciation time + if (model.rotation === undefined) + model.rotation = mat4.identity(new Float64Array(16)); mat4.identity(model.rotation); publicAPI.rotateZ(z); publicAPI.rotateX(x); @@ -99,7 +103,10 @@ function vtkProp3D(publicAPI, model) { }; publicAPI.setUserMatrix = (matrix) => { + if (model.userMatrix === undefined) + model.userMatrix = mat4.identity(new Float64Array(16)); mat4.copy(model.userMatrix, matrix); + publicAPI.modified(); }; @@ -110,21 +117,26 @@ function vtkProp3D(publicAPI, model) { publicAPI.computeMatrix = () => { // check whether or not need to rebuild the matrix + if (model.matrixMTime === undefined) { + return; + } if (publicAPI.getMTime() > model.matrixMTime.getMTime()) { mat4.identity(model.matrix); if (model.userMatrix) { mat4.multiply(model.matrix, model.matrix, model.userMatrix); } - mat4.translate(model.matrix, model.matrix, model.origin); - mat4.translate(model.matrix, model.matrix, model.position); - mat4.multiply(model.matrix, model.matrix, model.rotation); - mat4.scale(model.matrix, model.matrix, model.scale); - mat4.translate(model.matrix, model.matrix, [ - -model.origin[0], - -model.origin[1], - -model.origin[2], - ]); - mat4.transpose(model.matrix, model.matrix); + if (model.position && model.origin && model.scale && model.rotation) { + mat4.translate(model.matrix, model.matrix, model.origin); + mat4.translate(model.matrix, model.matrix, model.position); + mat4.multiply(model.matrix, model.matrix, model.rotation); + mat4.scale(model.matrix, model.matrix, model.scale); + mat4.translate(model.matrix, model.matrix, [ + -model.origin[0], + -model.origin[1], + -model.origin[2], + ]); + mat4.transpose(model.matrix, model.matrix); + } // check for identity model.isIdentity = true; @@ -158,51 +170,48 @@ function vtkProp3D(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - origin: [0, 0, 0], - position: [0, 0, 0], - orientation: [0, 0, 0], - rotation: null, - scale: [1, 1, 1], - bounds: [1, -1, 1, -1, 1, -1], - - userMatrix: null, - userMatrixMTime: null, - - cachedProp3D: null, - isIdentity: true, - matrixMTime: null, -}; +function defaultValues(initialValues) { + return { + origin: [0, 0, 0], + position: [0, 0, 0], + orientation: [0, 0, 0], + scale: [1, 1, 1], + bounds: [1, -1, 1, -1, 1, -1], + + matrix: mat4.identity(new Float64Array(16)), + rotation: mat4.identity(new Float64Array(16)), + userMatrix: mat4.identity(new Float64Array(16)), + transform: null, + matrixMTime: macro.obj({}), + + userMatrixMTime: null, + + cachedProp3D: null, + isIdentity: true, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkProp.extend(publicAPI, model, initialValues); - model.matrixMTime = {}; - macro.obj(model.matrixMTime); - // Build VTK API macro.get(publicAPI, model, ['bounds', 'isIdentity']); macro.getArray(publicAPI, model, ['orientation']); macro.setGetArray(publicAPI, model, ['origin', 'position', 'scale'], 3); - // Object internal instance - model.matrix = mat4.identity(new Float64Array(16)); - model.rotation = mat4.identity(new Float64Array(16)); - model.userMatrix = mat4.identity(new Float64Array(16)); - model.transform = null; // FIXME - // Object methods vtkProp3D(publicAPI, model); } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkProp3D'); +export const newInstance = macro.newInstance(extend, 'vtkProp3D', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/Property/index.js b/Sources/Rendering/Core/Property/index.js index 5ca740532ea..197fecd7dae 100644 --- a/Sources/Rendering/Core/Property/index.js +++ b/Sources/Rendering/Core/Property/index.js @@ -16,6 +16,10 @@ function vtkProperty(publicAPI, model) { model.classHierarchy.push('vtkProperty'); publicAPI.setColor = (r, g, b) => { + // Instanciation time + if (model.color === undefined) { + model.color = [0, 0, 0]; + } if (Array.isArray(r)) { if ( model.color[0] !== r[0] || @@ -87,35 +91,38 @@ function vtkProperty(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - color: [1, 1, 1], - ambientColor: [1, 1, 1], - diffuseColor: [1, 1, 1], - specularColor: [1, 1, 1], - edgeColor: [0, 0, 0], - - ambient: 0, - diffuse: 1, - specular: 0, - specularPower: 1, - opacity: 1, - interpolation: Interpolation.GOURAUD, - representation: Representation.SURFACE, - edgeVisibility: false, - backfaceCulling: false, - frontfaceCulling: false, - pointSize: 1, - lineWidth: 1, - lighting: true, - - shading: false, - materialName: null, -}; +function defaultValues(initialValues) { + return { + color: [1, 1, 1], + ambientColor: [1, 1, 1], + diffuseColor: [1, 1, 1], + specularColor: [1, 1, 1], + edgeColor: [0, 0, 0], + + ambient: 0, + diffuse: 1, + specular: 0, + specularPower: 1, + opacity: 1, + interpolation: Interpolation.GOURAUD, + representation: Representation.SURFACE, + edgeVisibility: false, + backfaceCulling: false, + frontfaceCulling: false, + pointSize: 1, + lineWidth: 1, + lighting: true, + + shading: false, + materialName: null, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -148,7 +155,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkProperty'); +export const newInstance = macro.newInstance(extend, 'vtkProperty', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/Property2D/index.js b/Sources/Rendering/Core/Property2D/index.js index 6a62a8c247b..c3dcbda81c9 100644 --- a/Sources/Rendering/Core/Property2D/index.js +++ b/Sources/Rendering/Core/Property2D/index.js @@ -31,19 +31,22 @@ function vtkProperty2D(publicAPI, model) { // ---------------------------------------------------------------------------- // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - color: [1, 1, 1], - opacity: 1, - pointSize: 1, - lineWidth: 1, - representation: Representation.SURFACE, - displayLocation: DisplayLocation.FOREGROUND, -}; +function defaultValues(initialValues) { + return { + color: [1, 1, 1], + opacity: 1, + pointSize: 1, + lineWidth: 1, + representation: Representation.SURFACE, + displayLocation: DisplayLocation.FOREGROUND, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -62,7 +65,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkProperty2D'); +export const newInstance = macro.newInstance(extend, 'vtkProperty2D', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/RenderWindow/index.js b/Sources/Rendering/Core/RenderWindow/index.js index f922fe0d0c9..e974d8969fa 100644 --- a/Sources/Rendering/Core/RenderWindow/index.js +++ b/Sources/Rendering/Core/RenderWindow/index.js @@ -15,8 +15,10 @@ export function listViewAPIs() { return Object.keys(VIEW_CONSTRUCTORS); } -export function newAPISpecificView(name, initialValues = {}) { - return VIEW_CONSTRUCTORS[name] && VIEW_CONSTRUCTORS[name](initialValues); +export function newAPISpecificView(name, initialValues = {}, toSet = true) { + return ( + VIEW_CONSTRUCTORS[name] && VIEW_CONSTRUCTORS[name](initialValues, toSet) + ); } // ---------------------------------------------------------------------------- @@ -137,19 +139,22 @@ function vtkRenderWindow(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - defaultViewAPI: DEFAULT_VIEW_API, - renderers: [], - views: [], - interactor: null, - neverRendered: true, - numberOfLayers: 1, -}; +function defaultValues(initialValues) { + return { + defaultViewAPI: DEFAULT_VIEW_API, + renderers: [], + views: [], + interactor: null, + neverRendered: true, + numberOfLayers: 1, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -170,7 +175,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkRenderWindow'); +export const newInstance = macro.newInstance(extend, 'vtkRenderWindow', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/Renderer/index.js b/Sources/Rendering/Core/Renderer/index.js index 7ee31f160c3..7be064fc1a4 100644 --- a/Sources/Rendering/Core/Renderer/index.js +++ b/Sources/Rendering/Core/Renderer/index.js @@ -563,75 +563,80 @@ function vtkRenderer(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - pickedProp: null, - activeCamera: null, +function defaultValues(initialValues) { + return { + background: [0, 0, 0, 1], - allBounds: [], - ambient: [1, 1, 1], + pickedProp: null, + activeCamera: null, - allocatedRenderTime: 100, - timeFactor: 1, + allBounds: [], + ambient: [1, 1, 1], - automaticLightCreation: true, + allocatedRenderTime: 100, + timeFactor: 1, - twoSidedLighting: true, - lastRenderTimeInSeconds: -1, + automaticLightCreation: true, - renderWindow: null, - lights: [], - actors: [], - volumes: [], + twoSidedLighting: true, + lastRenderTimeInSeconds: -1, - lightFollowCamera: true, + _renderWindow: null, + lights: [], + actors: [], + volumes: [], - numberOfPropsRendered: 0, + lightFollowCamera: true, - propArray: null, + numberOfPropsRendered: 0, - pathArray: null, + propArray: null, - layer: 0, - preserveColorBuffer: false, - preserveDepthBuffer: false, + pathArray: null, - computeVisiblePropBounds: vtkMath.createUninitializedBounds(), + layer: 0, + preserveColorBuffer: false, + preserveDepthBuffer: false, - interactive: true, + computeVisiblePropBounds: vtkMath.createUninitializedBounds(), - nearClippingPlaneTolerance: 0, - clippingRangeExpansion: 0.05, + interactive: true, - erase: true, - draw: true, + nearClippingPlaneTolerance: 0, + clippingRangeExpansion: 0.05, - useShadows: false, + erase: true, + draw: true, - useDepthPeeling: false, - occlusionRatio: 0, - maximumNumberOfPeels: 4, + useShadows: false, - selector: null, - delegate: null, + useDepthPeeling: false, + occlusionRatio: 0, + maximumNumberOfPeels: 4, - texturedBackground: false, - backgroundTexture: null, + selector: null, + delegate: null, - pass: 0, -}; + texturedBackground: false, + backgroundTexture: null, + + pass: 0, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + macro.moveToProtected(publicAPI, initialValues, ['renderWindow']); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewport.extend(publicAPI, model, initialValues); // make sure background has 4 entries. Default to opaque black - if (!model.background) model.background = [0, 0, 0, 1]; - while (model.background.length < 3) model.background.push(0); - if (model.background.length === 3) model.background.push(1); + while (initialValues.background.length < 3) initialValues.background.push(0); + if (initialValues.background.length === 3) initialValues.background.push(1); // Build VTK API macro.get(publicAPI, model, [ @@ -670,7 +675,6 @@ export function extend(publicAPI, model, initialValues = {}) { ]); macro.getArray(publicAPI, model, ['actors', 'volumes', 'lights']); macro.setGetArray(publicAPI, model, ['background'], 4, 1.0); - macro.moveToProtected(publicAPI, model, ['renderWindow']); // Object methods vtkRenderer(publicAPI, model); @@ -678,7 +682,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkRenderer'); +export const newInstance = macro.newInstance(extend, 'vtkRenderer', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/Viewport/index.js b/Sources/Rendering/Core/Viewport/index.js index d8b1caf5f8a..238ac9c3f27 100644 --- a/Sources/Rendering/Core/Viewport/index.js +++ b/Sources/Rendering/Core/Viewport/index.js @@ -138,22 +138,25 @@ function vtkViewport(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - // _vtkWindow: null, - background: [0, 0, 0], - background2: [0.2, 0.2, 0.2], - gradientBackground: false, - viewport: [0, 0, 1, 1], - aspect: [1, 1], - pixelAspect: [1, 1], - props: [], - actors2D: [], -}; +function defaultValues(initialValues) { + return { + // _vtkWindow: null, + background: [0, 0, 0], + background2: [0.2, 0.2, 0.2], + gradientBackground: false, + viewport: [0, 0, 1, 1], + aspect: [1, 1], + pixelAspect: [1, 1], + props: [], + actors2D: [], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -168,7 +171,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkViewport'); +export const newInstance = macro.newInstance(extend, 'vtkViewport', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/Volume/index.js b/Sources/Rendering/Core/Volume/index.js index 420322b02b3..717ddb1dba2 100644 --- a/Sources/Rendering/Core/Volume/index.js +++ b/Sources/Rendering/Core/Volume/index.js @@ -110,24 +110,26 @@ function vtkVolume(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - mapper: null, - property: null, - bounds: [1, -1, 1, -1, 1, -1], -}; +function defaultValues(initialValues) { + return { + // Internal objects + boundsMTime: macro.obj({}), + + mapper: null, + property: null, + bounds: [1, -1, 1, -1, 1, -1], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkProp3D.extend(publicAPI, model, initialValues); - // vtkTimeStamp - model.boundsMTime = {}; - macro.obj(model.boundsMTime); - // Build VTK API macro.set(publicAPI, model, ['property']); macro.setGet(publicAPI, model, ['mapper']); @@ -139,7 +141,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkVolume'); +export const newInstance = macro.newInstance(extend, 'vtkVolume', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Core/VolumeMapper/index.js b/Sources/Rendering/Core/VolumeMapper/index.js index 900bec718c8..244b47777e2 100644 --- a/Sources/Rendering/Core/VolumeMapper/index.js +++ b/Sources/Rendering/Core/VolumeMapper/index.js @@ -100,28 +100,31 @@ function vtkVolumeMapper(publicAPI, model) { // ---------------------------------------------------------------------------- // TODO: what values to use for averageIPScalarRange to get GLSL to use max / min values like [-Math.inf, Math.inf]? -const DEFAULT_VALUES = { - bounds: [1, -1, 1, -1, 1, -1], - sampleDistance: 1.0, - imageSampleDistance: 1.0, - maximumSamplesPerRay: 1000, - autoAdjustSampleDistances: true, - blendMode: BlendMode.COMPOSITE_BLEND, - ipScalarRange: [-1000000.0, 1000000.0], - filterMode: FilterMode.OFF, // ignored by WebGL so no behavior change - preferSizeOverAccuracy: false, // Whether to use halfFloat representation of float, when it is inaccurate - computeNormalFromOpacity: false, - // volume shadow parameters - volumetricScatteringBlending: 0.0, - globalIlluminationReach: 0.0, - volumeShadowSamplingDistFactor: 5.0, - anisotropy: 0.0, -}; +function defaultValues(initialValues) { + return { + bounds: [1, -1, 1, -1, 1, -1], + sampleDistance: 1.0, + imageSampleDistance: 1.0, + maximumSamplesPerRay: 1000, + autoAdjustSampleDistances: true, + blendMode: BlendMode.COMPOSITE_BLEND, + ipScalarRange: [-1000000.0, 1000000.0], + filterMode: FilterMode.OFF, // ignored by WebGL so no behavior change + preferSizeOverAccuracy: false, // Whether to use halfFloat representation of float, when it is inaccurate + computeNormalFromOpacity: false, + // volume shadow parameters + volumetricScatteringBlending: 0.0, + globalIlluminationReach: 0.0, + volumeShadowSamplingDistFactor: 5.0, + anisotropy: 0.0, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); vtkAbstractMapper.extend(publicAPI, model, initialValues); @@ -150,7 +153,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkVolumeMapper'); +export const newInstance = macro.newInstance(extend, 'vtkVolumeMapper', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Misc/FullScreenRenderWindow/index.d.ts b/Sources/Rendering/Misc/FullScreenRenderWindow/index.d.ts index 1298a1576e7..dfedc2d0f0f 100644 --- a/Sources/Rendering/Misc/FullScreenRenderWindow/index.d.ts +++ b/Sources/Rendering/Misc/FullScreenRenderWindow/index.d.ts @@ -128,6 +128,7 @@ export function extend(publicAPI: object, model: object, initialValues?: IFullSc /** * Method used to create a new instance of vtkFullScreenRenderWindow * @param {IFullScreenRenderWindowInitialValues} [initialValues] for pre-setting some of its content + * a background property can be given in the initialValues to set the background of the renderer */ export function newInstance(initialValues?: IFullScreenRenderWindowInitialValues): vtkFullScreenRenderWindow; diff --git a/Sources/Rendering/Misc/FullScreenRenderWindow/index.js b/Sources/Rendering/Misc/FullScreenRenderWindow/index.js index 067c089b580..565b8de3aa9 100644 --- a/Sources/Rendering/Misc/FullScreenRenderWindow/index.js +++ b/Sources/Rendering/Misc/FullScreenRenderWindow/index.js @@ -94,8 +94,8 @@ function vtkFullScreenRenderWindow(publicAPI, model) { model.interactor.initialize(); model.interactor.bindEvents(model.container); - // Expose background publicAPI.setBackground = model.renderer.setBackground; + publicAPI.getBackground = model.renderer.getBackground; publicAPI.removeController = () => { const el = model.controlContainer; @@ -137,9 +137,6 @@ function vtkFullScreenRenderWindow(publicAPI, model) { }); }; - // Update BG color - publicAPI.setBackground(...model.background); - // Representation API publicAPI.addRepresentation = (representation) => { representation.getActors().forEach((actor) => { @@ -188,19 +185,22 @@ function vtkFullScreenRenderWindow(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - background: [0.32, 0.34, 0.43], - containerStyle: null, - controlPanelStyle: null, - listenWindowResize: true, - resizeCallback: null, - controllerVisibility: true, -}; +function defaultValues(initialValues) { + return { + background: [0.32, 0.34, 0.43], + containerStyle: null, + controlPanelStyle: null, + listenWindowResize: true, + resizeCallback: null, + controllerVisibility: true, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Object methods macro.obj(publicAPI, model); @@ -220,7 +220,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend); +export const newInstance = macro.newInstance(extend, undefined, true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/Misc/RemoteView/index.js b/Sources/Rendering/Misc/RemoteView/index.js index a60e8e96646..42db0be0c6c 100644 --- a/Sources/Rendering/Misc/RemoteView/index.js +++ b/Sources/Rendering/Misc/RemoteView/index.js @@ -181,32 +181,30 @@ function vtkRemoteView(publicAPI, model) { } return changeDetected; }; - - // Initialize viewStream if available - if (model.viewStream) { - publicAPI.setViewStream(model.viewStream); - } } // ---------------------------------------------------------------------------- // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - viewId: '-1', - interactiveQuality: 60, - interactiveRatio: 1 / window.devicePixelRatio, - stillQuality: 100, - stillRatio: 1, - rpcMouseEvent: 'viewport.mouse.interaction', - rpcGestureEvent: null, - rpcWheelEvent: null, -}; +function defaultValues(initialValues) { + return { + viewId: '-1', + interactiveQuality: 60, + interactiveRatio: 1 / window.devicePixelRatio, + stillQuality: 100, + stillRatio: 1, + rpcMouseEvent: 'viewport.mouse.interaction', + rpcGestureEvent: null, + rpcWheelEvent: null, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); macro.obj(publicAPI, model, initialValues); macro.get(publicAPI, model, [ diff --git a/Sources/Rendering/OpenGL/Actor/index.js b/Sources/Rendering/OpenGL/Actor/index.js index c62ee1ab395..05cfe25ff76 100644 --- a/Sources/Rendering/OpenGL/Actor/index.js +++ b/Sources/Rendering/OpenGL/Actor/index.js @@ -184,28 +184,30 @@ function vtkOpenGLActor(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - context: null, - keyMatrixTime: null, - keyMatrices: null, - activeTextures: null, -}; +function defaultValues(initialValues) { + return { + // Internal objects + keyMatrixTime: macro.obj({}, { mtime: 0 }), + keyMatrices: { + normalMatrix: mat3.identity(new Float64Array(9)), + mcwc: mat4.identity(new Float64Array(16)), + }, + + context: null, + activeTextures: null, + + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); - model.keyMatrixTime = {}; - macro.obj(model.keyMatrixTime, { mtime: 0 }); - model.keyMatrices = { - normalMatrix: mat3.identity(new Float64Array(9)), - mcwc: mat4.identity(new Float64Array(16)), - }; - // Build VTK API macro.setGet(publicAPI, model, ['context']); @@ -217,7 +219,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend); +export const newInstance = macro.newInstance(extend, undefined, true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/Actor2D/index.js b/Sources/Rendering/OpenGL/Actor2D/index.js index 849dc50a94b..78a81955dff 100644 --- a/Sources/Rendering/OpenGL/Actor2D/index.js +++ b/Sources/Rendering/OpenGL/Actor2D/index.js @@ -167,15 +167,18 @@ function vtkOpenGLActor2D(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - context: null, - activeTextures: null, -}; +function defaultValues(initialValues) { + return { + context: null, + activeTextures: null, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); @@ -191,7 +194,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend); +export const newInstance = macro.newInstance(extend, undefined, true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/Camera/index.js b/Sources/Rendering/OpenGL/Camera/index.js index 2a4283f1fc5..48c81b81068 100644 --- a/Sources/Rendering/OpenGL/Camera/index.js +++ b/Sources/Rendering/OpenGL/Camera/index.js @@ -88,32 +88,30 @@ function vtkOpenGLCamera(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - context: null, - lastRenderer: null, - keyMatrixTime: null, - keyMatrices: null, -}; +function defaultValues(initialValues) { + return { + context: null, + lastRenderer: null, + keyMatrixTime: macro.obj({}), + // values always get set by the get method + keyMatrices: { + normalMatrix: new Float64Array(9), + vcpc: new Float64Array(16), + wcvc: new Float64Array(16), + wcpc: new Float64Array(16), + }, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); - model.keyMatrixTime = {}; - macro.obj(model.keyMatrixTime); - - // values always get set by the get method - model.keyMatrices = { - normalMatrix: new Float64Array(9), - vcpc: new Float64Array(16), - wcvc: new Float64Array(16), - wcpc: new Float64Array(16), - }; - // Build VTK API macro.setGet(publicAPI, model, ['context', 'keyMatrixTime']); @@ -123,7 +121,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend); +export const newInstance = macro.newInstance(extend, undefined, true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/HardwareSelector/index.js b/Sources/Rendering/OpenGL/HardwareSelector/index.js index 27f52f627cd..93ed4687e02 100644 --- a/Sources/Rendering/OpenGL/HardwareSelector/index.js +++ b/Sources/Rendering/OpenGL/HardwareSelector/index.js @@ -876,6 +876,9 @@ function vtkOpenGLHardwareSelector(publicAPI, model) { // override const superSetArea = publicAPI.setArea; publicAPI.setArea = (...args) => { + if (!args[0]) return false; + // Instanciation time + if (model.area === undefined) model.area = [0, 0, 0, 0]; if (superSetArea(...args)) { model.area[0] = Math.floor(model.area[0]); model.area[1] = Math.floor(model.area[1]); @@ -891,34 +894,34 @@ function vtkOpenGLHardwareSelector(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - area: undefined, - // _renderer: null, - // _openGLRenderWindow: null, - // _openGLRenderer: null, - currentPass: -1, - propColorValue: null, - props: null, - maximumPointId: 0, - maximumCellId: 0, - idOffset: 1, -}; +function defaultValues(initialValues) { + return { + area: [0, 0, 0, 0], + // _renderer: null, + // _openGLRenderWindow: null, + // _openGLRenderer: null, + currentPass: -1, + propColorValue: [0, 0, 0], + props: [], + maximumPointId: 0, + maximumCellId: 0, + idOffset: 1, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + macro.moveToProtected(publicAPI, initialValues, [ + 'renderer', + 'openGLRenderWindow', + ]); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API vtkHardwareSelector.extend(publicAPI, model, initialValues); - model.propColorValue = [0, 0, 0]; - model.props = []; - - if (!model.area) { - model.area = [0, 0, 0, 0]; - } - macro.setGetArray(publicAPI, model, ['area'], 4); macro.setGet(publicAPI, model, [ '_renderer', @@ -929,7 +932,6 @@ export function extend(publicAPI, model, initialValues = {}) { ]); macro.setGetArray(publicAPI, model, ['propColorValue'], 3); - macro.moveToProtected(publicAPI, model, ['renderer', 'openGLRenderWindow']); macro.event(publicAPI, model, 'event'); // Object methods @@ -940,7 +942,8 @@ export function extend(publicAPI, model, initialValues = {}) { export const newInstance = macro.newInstance( extend, - 'vtkOpenGLHardwareSelector' + 'vtkOpenGLHardwareSelector', + true ); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/Helper/index.js b/Sources/Rendering/OpenGL/Helper/index.js index ec03a1ec322..1a202cc4cd0 100644 --- a/Sources/Rendering/OpenGL/Helper/index.js +++ b/Sources/Rendering/OpenGL/Helper/index.js @@ -259,31 +259,30 @@ function vtkOpenGLHelper(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - context: null, - program: null, - shaderSourceTime: null, - VAO: null, - attributeUpdateTime: null, - CABO: null, - primitiveType: 0, - pointPicking: false, -}; +function defaultValues(initialValues) { + return { + // Internal objects + shaderSourceTime: macro.obj({}), + attributeUpdateTime: macro.obj({}), + + context: null, + program: vtkShaderProgram.newInstance(), + VAO: vtkVertexArrayObject.newInstance(), + CABO: vtkCellArrayBufferObject.newInstance(), + primitiveType: 0, + pointPicking: false, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); - model.shaderSourceTime = {}; - macro.obj(model.shaderSourceTime); - - model.attributeUpdateTime = {}; - macro.obj(model.attributeUpdateTime); - macro.setGet(publicAPI, model, [ 'program', 'shaderSourceTime', @@ -294,17 +293,13 @@ export function extend(publicAPI, model, initialValues = {}) { 'pointPicking', ]); - model.program = vtkShaderProgram.newInstance(); - model.VAO = vtkVertexArrayObject.newInstance(); - model.CABO = vtkCellArrayBufferObject.newInstance(); - // Object methods vtkOpenGLHelper(publicAPI, model); } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend); +export const newInstance = macro.newInstance(extend, undefined, true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/PolyDataMapper/index.js b/Sources/Rendering/OpenGL/PolyDataMapper/index.js index 12859d1f2f2..48aa092df31 100755 --- a/Sources/Rendering/OpenGL/PolyDataMapper/index.js +++ b/Sources/Rendering/OpenGL/PolyDataMapper/index.js @@ -2010,32 +2010,49 @@ function vtkOpenGLPolyDataMapper(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - context: null, - VBOBuildTime: 0, - VBOBuildString: null, - primitives: null, - primTypes: null, - shaderRebuildString: null, - tmpMat4: null, - ambientColor: [], // used internally - diffuseColor: [], // used internally - specularColor: [], // used internally - lightColor: [], // used internally - lightHalfAngle: [], // used internally - lightDirection: [], // used internally - lastHaveSeenDepthRequest: false, - haveSeenDepthRequest: false, - lastSelectionState: PassTypes.MIN_KNOWN_PASS - 1, - selectionStateChanged: null, - selectionWebGLIdsToVTKIds: null, - pointPicking: false, -}; +function defaultValues(initialValues) { + return { + // Internal + tmpMat3: mat3.identity(new Float64Array(9)), + tmpMat4: mat4.identity(new Float64Array(16)), + + ambientColor: [], // used internally + diffuseColor: [], // used internally + specularColor: [], // used internally + lightColor: [], // used internally + lightHalfAngle: [], // used internally + lightDirection: [], // used internally + VBOBuildTime: macro.obj({}, { mtime: 0 }), + selectionStateChanged: macro.obj({}, { mtime: 0 }), + + context: null, + VBOBuildString: null, + primitives: Object.keys(primTypes) + .filter((primType) => primType !== 'Start' && primType !== 'End') + .map((primType) => + vtkHelper.newInstance({ + primitiveType: primTypes[primType], + lastLightComplexity: 0, + lastLightCount: 0, + lastSelectionPass: false, + }) + ), + primTypes, + shaderRebuildString: null, + + lastHaveSeenDepthRequest: false, + haveSeenDepthRequest: false, + lastSelectionState: PassTypes.MIN_KNOWN_PASS - 1, + selectionWebGLIdsToVTKIds: null, + pointPicking: false, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); @@ -2045,37 +2062,20 @@ export function extend(publicAPI, model, initialValues = {}) { initialValues ); - model.primitives = []; - model.primTypes = primTypes; - - model.tmpMat3 = mat3.identity(new Float64Array(9)); - model.tmpMat4 = mat4.identity(new Float64Array(16)); - - for (let i = primTypes.Start; i < primTypes.End; i++) { - model.primitives[i] = vtkHelper.newInstance(); - model.primitives[i].setPrimitiveType(i); - model.primitives[i].set( - { lastLightComplexity: 0, lastLightCount: 0, lastSelectionPass: false }, - true - ); - } - // Build VTK API macro.setGet(publicAPI, model, ['context']); - model.VBOBuildTime = {}; - macro.obj(model.VBOBuildTime, { mtime: 0 }); - - model.selectionStateChanged = {}; - macro.obj(model.selectionStateChanged, { mtime: 0 }); - // Object methods vtkOpenGLPolyDataMapper(publicAPI, model); } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkOpenGLPolyDataMapper'); +export const newInstance = macro.newInstance( + extend, + 'vtkOpenGLPolyDataMapper', + true +); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/PolyDataMapper2D/index.js b/Sources/Rendering/OpenGL/PolyDataMapper2D/index.js index 386e08f98cd..3bd42114664 100644 --- a/Sources/Rendering/OpenGL/PolyDataMapper2D/index.js +++ b/Sources/Rendering/OpenGL/PolyDataMapper2D/index.js @@ -23,6 +23,34 @@ const { vtkErrorMacro } = macro; const StartEvent = { type: 'StartEvent' }; const EndEvent = { type: 'EndEvent' }; +// ---------------------------------------------------------------------------- +// Object factory +// ---------------------------------------------------------------------------- + +function defaultValues(initialValues) { + return { + // Internal Objects + primitives: Object.keys(primTypes) + .filter((primType) => primType !== 'Start' && primType !== 'End') + .map((primType) => + vtkHelper.newInstance({ + primitiveType: primTypes[primType], + lastLightComplexity: 0, + lastLightCount: 0, + lastSelectionPass: false, + }) + ), + primTypes, + tmpMat4: mat4.identity(new Float64Array(16)), + VBOBuildTime: macro.obj({}, { mtime: 0 }), + VBOBuildString: null, + context: null, + + shaderRebuildString: null, + ...initialValues, + }; +} + // ---------------------------------------------------------------------------- // vtkOpenGLPolyDataMapper2D methods // ---------------------------------------------------------------------------- @@ -719,23 +747,10 @@ function vtkOpenGLPolyDataMapper2D(publicAPI, model) { }; } -// ---------------------------------------------------------------------------- -// Object factory -// ---------------------------------------------------------------------------- - -const DEFAULT_VALUES = { - context: null, - VBOBuildTime: 0, - VBOBuildString: null, - primitives: null, - primTypes: null, - shaderRebuildString: null, -}; - // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); @@ -745,26 +760,18 @@ export function extend(publicAPI, model, initialValues = {}) { initialValues ); - model.primitives = []; - model.primTypes = primTypes; - - model.tmpMat4 = mat4.identity(new Float64Array(16)); - for (let i = primTypes.Start; i < primTypes.End; i++) { - model.primitives[i] = vtkHelper.newInstance(); - model.primitives[i].setPrimitiveType(i); - model.primitives[i].set( - { lastLightComplexity: 0, lastLightCount: 0, lastSelectionPass: false }, - true - ); + initialValues.primitives[i] = vtkHelper.newInstance({ + primitiveType: i, + lastLightComplexity: 0, + lastLightCount: 0, + lastSelectionPass: false, + }); } // Build VTK API macro.setGet(publicAPI, model, ['context']); - model.VBOBuildTime = {}; - macro.obj(model.VBOBuildTime, { mtime: 0 }); - // Object methods vtkOpenGLPolyDataMapper2D(publicAPI, model); } @@ -773,7 +780,8 @@ export function extend(publicAPI, model, initialValues = {}) { export const newInstance = macro.newInstance( extend, - 'vtkOpenGLPolyDataMapper2D' + 'vtkOpenGLPolyDataMapper2D', + true ); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/RenderWindow/index.js b/Sources/Rendering/OpenGL/RenderWindow/index.js index 273b78e32f5..ebdcb65794c 100644 --- a/Sources/Rendering/OpenGL/RenderWindow/index.js +++ b/Sources/Rendering/OpenGL/RenderWindow/index.js @@ -118,8 +118,8 @@ function vtkOpenGLRenderWindow(publicAPI, model) { // Canvas size if (model.renderable) { if ( - model.size[0] !== previousSize[0] || - model.size[1] !== previousSize[1] + model.size && + (model.size[0] !== previousSize[0] || model.size[1] !== previousSize[1]) ) { previousSize[0] = model.size[0]; previousSize[1] = model.size[1]; @@ -573,7 +573,7 @@ function vtkOpenGLRenderWindow(publicAPI, model) { publicAPI.setUseBackgroundImage = (value) => { model.useBackgroundImage = value; - + if (!model.el) return; // Add or remove the background image from the // DOM as specified. if (model.useBackgroundImage && !model.el.contains(model.bgImage)) { @@ -1224,70 +1224,65 @@ function vtkOpenGLRenderWindow(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - cullFaceEnabled: false, - shaderCache: null, - initialized: false, - context: null, - canvas: null, - cursorVisibility: true, - cursor: 'pointer', - textureUnitManager: null, - textureResourceIds: null, - containerSize: null, - renderPasses: [], - notifyStartCaptureImage: false, - webgl2: false, - defaultToWebgl2: true, // attempt webgl2 on by default - activeFramebuffer: null, - xrSession: null, - xrSessionIsAR: false, - xrReferenceSpace: null, - xrSupported: true, - imageFormat: 'image/png', - useOffScreen: false, - useBackgroundImage: false, -}; +function defaultValues(initialValues) { + return { + // Internal + selector: vtkOpenGLHardwareSelector.newInstance(), + bgImage: new Image(), + _textureResourceIds: new Map(), + shaderCache: vtkShaderCache.newInstance(), + + cullFaceEnabled: false, + initialized: false, + context: null, + cursorVisibility: true, + cursor: 'pointer', + textureUnitManager: null, + containerSize: null, + renderPasses: [], + notifyStartCaptureImage: false, + webgl2: false, + defaultToWebgl2: true, // attempt webgl2 on by default + activeFramebuffer: null, + xrSession: null, + xrSessionIsAR: false, + xrReferenceSpace: null, + xrSupported: true, + imageFormat: 'image/png', + useOffScreen: false, + useBackgroundImage: false, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkRenderWindowViewNode.extend(publicAPI, model, initialValues); - // Create internal instances + // model.canvas needs to be set to model before calling other setters model.canvas = document.createElement('canvas'); model.canvas.style.width = '100%'; createGLContext(); - if (!model.selector) { - model.selector = vtkOpenGLHardwareSelector.newInstance(); - model.selector.setOpenGLRenderWindow(publicAPI); - } - // Create internal bgImage - model.bgImage = new Image(); - model.bgImage.style.position = 'absolute'; - model.bgImage.style.left = '0'; - model.bgImage.style.top = '0'; - model.bgImage.style.width = '100%'; - model.bgImage.style.height = '100%'; - model.bgImage.style.zIndex = '-1'; - - model._textureResourceIds = new Map(); - - model.myFactory = vtkOpenGLViewNodeFactory.newInstance(); + initialValues.bgImage.style.position = 'absolute'; + initialValues.bgImage.style.left = '0'; + initialValues.bgImage.style.top = '0'; + initialValues.bgImage.style.width = '100%'; + initialValues.bgImage.style.height = '100%'; + initialValues.bgImage.style.zIndex = '-1'; + + initialValues.myFactory = vtkOpenGLViewNodeFactory.newInstance(); /* eslint-disable no-use-before-define */ - model.myFactory.registerOverride('vtkRenderWindow', newInstance); + initialValues.myFactory.registerOverride('vtkRenderWindow', newInstance); /* eslint-enable no-use-before-define */ - model.shaderCache = vtkShaderCache.newInstance(); - model.shaderCache.setOpenGLRenderWindow(publicAPI); - // setup default forward pass rendering - model.renderPasses[0] = vtkForwardPass.newInstance(); + initialValues.renderPasses[0] = vtkForwardPass.newInstance(); macro.event(publicAPI, model, 'imageReady'); macro.event(publicAPI, model, 'haveVRDisplay'); @@ -1322,11 +1317,18 @@ export function extend(publicAPI, model, initialValues = {}) { // Object methods vtkOpenGLRenderWindow(publicAPI, model); + + initialValues.selector.setOpenGLRenderWindow(publicAPI); + initialValues.shaderCache.setOpenGLRenderWindow(publicAPI); } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkOpenGLRenderWindow'); +export const newInstance = macro.newInstance( + extend, + 'vtkOpenGLRenderWindow', + true +); // ---------------------------------------------------------------------------- // Register API specific RenderWindow implementation diff --git a/Sources/Rendering/OpenGL/Renderer/index.js b/Sources/Rendering/OpenGL/Renderer/index.js index 4e9e3b19999..b771e309676 100644 --- a/Sources/Rendering/OpenGL/Renderer/index.js +++ b/Sources/Rendering/OpenGL/Renderer/index.js @@ -188,16 +188,20 @@ function vtkOpenGLRenderer(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - context: null, - // _openGLRenderWindow: null, - selector: null, -}; +function defaultValues(initialValues) { + return { + context: null, + // _openGLRenderWindow: null, + selector: null, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + macro.moveToProtected(publicAPI, initialValues, ['openGLRenderWindow']); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); @@ -207,15 +211,13 @@ export function extend(publicAPI, model, initialValues = {}) { macro.setGet(publicAPI, model, ['selector']); - macro.moveToProtected(publicAPI, model, ['openGLRenderWindow']); - // Object methods vtkOpenGLRenderer(publicAPI, model); } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkOpenGLRenderer'); +export const newInstance = macro.newInstance(extend, 'vtkOpenGLRenderer', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/Shader/index.js b/Sources/Rendering/OpenGL/Shader/index.js index 21e132cefae..f517b1e9ec6 100644 --- a/Sources/Rendering/OpenGL/Shader/index.js +++ b/Sources/Rendering/OpenGL/Shader/index.js @@ -76,19 +76,22 @@ function vtkShader(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - shaderType: 'Unknown', - source: '', - error: '', - handle: 0, - dirty: false, - context: null, -}; +function defaultValues(initialValues) { + return { + shaderType: 'Unknown', + source: '', + error: '', + handle: 0, + dirty: false, + context: null, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -106,7 +109,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkShader'); +export const newInstance = macro.newInstance(extend, 'vtkShader', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/ShaderCache/index.js b/Sources/Rendering/OpenGL/ShaderCache/index.js index 938f96164a4..a4f41b3f752 100644 --- a/Sources/Rendering/OpenGL/ShaderCache/index.js +++ b/Sources/Rendering/OpenGL/ShaderCache/index.js @@ -234,25 +234,26 @@ function vtkShaderCache(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - lastShaderBound: null, - shaderPrograms: null, - context: null, - // _openGLRenderWindow: null, -}; +function defaultValues(initialValues) { + return { + // Internal objects + shaderPrograms: {}, + lastShaderBound: null, + context: null, + // _openGLRenderWindow: null, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); - - // Internal objects - model.shaderPrograms = {}; + macro.moveToProtected(publicAPI, initialValues, ['openGLRenderWindow']); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); macro.setGet(publicAPI, model, SET_GET_FIELDS); - macro.moveToProtected(publicAPI, model, ['openGLRenderWindow']); // Object methods vtkShaderCache(publicAPI, model); @@ -260,7 +261,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkShaderCache'); +export const newInstance = macro.newInstance(extend, 'vtkShaderCache', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/ShaderProgram/index.js b/Sources/Rendering/OpenGL/ShaderProgram/index.js index 734ee36d660..fdd1bff2888 100644 --- a/Sources/Rendering/OpenGL/ShaderProgram/index.js +++ b/Sources/Rendering/OpenGL/ShaderProgram/index.js @@ -97,12 +97,6 @@ function vtkShaderProgram(publicAPI, model) { publicAPI.setBound(false); }; - publicAPI.setContext = (ctx) => { - model.vertexShader.setContext(ctx); - model.fragmentShader.setContext(ctx); - model.geometryShader.setContext(ctx); - }; - publicAPI.link = () => { if (model.inked) { return true; @@ -516,6 +510,7 @@ function vtkShaderProgram(publicAPI, model) { publicAPI.setContext = (ctx) => { model.context = ctx; + if (!ctx) return; model.vertexShader.setContext(ctx); model.fragmentShader.setContext(ctx); model.geometryShader.setContext(ctx); @@ -550,41 +545,41 @@ function vtkShaderProgram(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - vertexShaderHandle: 0, - fragmentShaderHandle: 0, - geometryShaderHandle: 0, - vertexShader: null, - fragmentShader: null, - geometryShader: null, - - linked: false, - bound: false, - compiled: false, - error: '', - handle: 0, - numberOfOutputs: 0, - attributesLocs: null, - uniformLocs: null, - md5Hash: 0, - context: null, - lastCameraMTime: null, -}; +function defaultValues(initialValues) { + return { + vertexShaderHandle: 0, + fragmentShaderHandle: 0, + geometryShaderHandle: 0, + + // Internal objects + vertexShader: vtkShader.newInstance(), + fragmentShader: vtkShader.newInstance(), + geometryShader: vtkShader.newInstance(), + + linked: false, + bound: false, + compiled: false, + error: '', + handle: 0, + numberOfOutputs: 0, + attributesLocs: {}, + uniformLocs: {}, + md5Hash: 0, + context: null, + lastCameraMTime: null, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Instantiate internal objects - model.attributesLocs = {}; - model.uniformLocs = {}; - model.vertexShader = vtkShader.newInstance(); - model.vertexShader.setShaderType('Vertex'); - model.fragmentShader = vtkShader.newInstance(); - model.fragmentShader.setShaderType('Fragment'); - model.geometryShader = vtkShader.newInstance(); - model.geometryShader.setShaderType('Geometry'); + initialValues.vertexShader.setShaderType('Vertex'); + initialValues.fragmentShader.setShaderType('Fragment'); + initialValues.geometryShader.setShaderType('Geometry'); // Build VTK API macro.obj(publicAPI, model); @@ -607,7 +602,7 @@ function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -const newInstance = macro.newInstance(extend, 'vtkShaderProgram'); +const newInstance = macro.newInstance(extend, 'vtkShaderProgram', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/Texture/index.js b/Sources/Rendering/OpenGL/Texture/index.js index 3a21f0c05c8..7c3dedb5e85 100644 --- a/Sources/Rendering/OpenGL/Texture/index.js +++ b/Sources/Rendering/OpenGL/Texture/index.js @@ -1463,47 +1463,46 @@ function vtkOpenGLTexture(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - _openGLRenderWindow: null, - context: null, - handle: 0, - sendParametersTime: null, - textureBuildTime: null, - numberOfDimensions: 0, - target: 0, - format: 0, - openGLDataType: 0, - components: 0, - width: 0, - height: 0, - depth: 0, - autoParameters: true, - wrapS: Wrap.CLAMP_TO_EDGE, - wrapT: Wrap.CLAMP_TO_EDGE, - wrapR: Wrap.CLAMP_TO_EDGE, - minificationFilter: Filter.NEAREST, - magnificationFilter: Filter.NEAREST, - minLOD: -1000.0, - maxLOD: 1000.0, - baseLevel: 0, - maxLevel: 1000, - generateMipmap: false, -}; +function defaultValues(initialValues) { + return { + // Internal objects + sendParametersTime: macro.obj({}, { mtime: 0 }), + textureBuildTime: macro.obj({}, { mtime: 0 }), + + _openGLRenderWindow: null, + context: null, + handle: 0, + numberOfDimensions: 0, + target: 0, + format: 0, + openGLDataType: 0, + components: 0, + width: 0, + height: 0, + depth: 0, + autoParameters: true, + wrapS: Wrap.CLAMP_TO_EDGE, + wrapT: Wrap.CLAMP_TO_EDGE, + wrapR: Wrap.CLAMP_TO_EDGE, + minificationFilter: Filter.NEAREST, + magnificationFilter: Filter.NEAREST, + minLOD: -1000.0, + maxLOD: 1000.0, + baseLevel: 0, + maxLevel: 1000, + generateMipmap: false, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); - model.sendParametersTime = {}; - macro.obj(model.sendParametersTime, { mtime: 0 }); - - model.textureBuildTime = {}; - macro.obj(model.textureBuildTime, { mtime: 0 }); - // Build VTK API macro.set(publicAPI, model, ['format', 'openGLDataType']); @@ -1532,7 +1531,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkOpenGLTexture'); +export const newInstance = macro.newInstance(extend, 'vtkOpenGLTexture', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/ViewNodeFactory/index.js b/Sources/Rendering/OpenGL/ViewNodeFactory/index.js index c72cbc1b9eb..11141a20765 100644 --- a/Sources/Rendering/OpenGL/ViewNodeFactory/index.js +++ b/Sources/Rendering/OpenGL/ViewNodeFactory/index.js @@ -20,15 +20,18 @@ function vtkOpenGLViewNodeFactory(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = {}; +function defaultValues(initialValues) { + return { + // Static class mapping shared across instances + overrides: CLASS_MAPPING, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); - - // Static class mapping shared across instances - model.overrides = CLASS_MAPPING; + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNodeFactory.extend(publicAPI, model, initialValues); @@ -41,7 +44,8 @@ export function extend(publicAPI, model, initialValues = {}) { export const newInstance = macro.newInstance( extend, - 'vtkOpenGLViewNodeFactory' + 'vtkOpenGLViewNodeFactory', + true ); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/Volume/index.js b/Sources/Rendering/OpenGL/Volume/index.js index 83e6e0ea654..8a11ea722ad 100644 --- a/Sources/Rendering/OpenGL/Volume/index.js +++ b/Sources/Rendering/OpenGL/Volume/index.js @@ -89,28 +89,25 @@ function vtkOpenGLVolume(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - // context: null, - // keyMatrixTime: null, - // normalMatrix: null, - // MCWCMatrix: null, - // _openGLRenderWindow: null, -}; +function defaultValues(initialValues) { + return { + // context: null, + keyMatrixTime: macro.obj({}, { mtime: 0 }), + normalMatrix: new Float64Array(9), + MCWCMatrix: new Float64Array(16), + // _openGLRenderWindow: null, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); - model.keyMatrixTime = {}; - macro.obj(model.keyMatrixTime, { mtime: 0 }); - // always set by getter - model.normalMatrix = new Float64Array(9); - model.MCWCMatrix = new Float64Array(16); - // Build VTK API macro.setGet(publicAPI, model, ['context']); @@ -120,7 +117,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkOpenGLVolume'); +export const newInstance = macro.newInstance(extend, 'vtkOpenGLVolume', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/OpenGL/VolumeMapper/index.js b/Sources/Rendering/OpenGL/VolumeMapper/index.js index 2107ccb71f5..91c751f5a1a 100644 --- a/Sources/Rendering/OpenGL/VolumeMapper/index.js +++ b/Sources/Rendering/OpenGL/VolumeMapper/index.js @@ -1528,62 +1528,52 @@ function vtkOpenGLVolumeMapper(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - context: null, - VBOBuildTime: null, - scalarTexture: null, - scalarTextureString: null, - opacityTexture: null, - opacityTextureString: null, - colorTexture: null, - colorTextureString: null, - jitterTexture: null, - tris: null, - framebuffer: null, - copyShader: null, - copyVAO: null, - lastXYF: 1.0, - targetXYF: 1.0, - zBufferTexture: null, - lastZBufferTexture: null, - lastLightComplexity: 0, - fullViewportTime: 1.0, - idxToView: null, - idxNormalMatrix: null, - modelToView: null, - projectionToView: null, - avgWindowArea: 0.0, - avgFrameTime: 0.0, -}; +function defaultValues(initialValues) { + return { + // Internal Objects + VBOBuildTime: macro.obj({}, { mtime: 0 }), + tris: vtkHelper.newInstance(), + scalarTexture: vtkOpenGLTexture.newInstance(), + opacityTexture: vtkOpenGLTexture.newInstance(), + colorTexture: vtkOpenGLTexture.newInstance(), + jitterTexture: vtkOpenGLTexture.newInstance({ + wrapS: Wrap.REPEAT, + wrapT: Wrap.REPEAT, + }), + framebuffer: vtkOpenGLFramebuffer.newInstance(), + idxToView: mat4.identity(new Float64Array(16)), + idxNormalMatrix: mat3.identity(new Float64Array(9)), + modelToView: mat4.identity(new Float64Array(16)), + projectionToView: mat4.identity(new Float64Array(16)), + projectionToWorld: mat4.identity(new Float64Array(16)), + _lastScale: 1.0, + + context: null, + scalarTextureString: null, + opacityTextureString: null, + colorTextureString: null, + copyShader: null, + copyVAO: null, + lastXYF: 1.0, + targetXYF: 1.0, + zBufferTexture: null, + lastZBufferTexture: null, + lastLightComplexity: 0, + fullViewportTime: 1.0, + avgWindowArea: 0.0, + avgFrameTime: 0.0, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); - model.VBOBuildTime = {}; - macro.obj(model.VBOBuildTime, { mtime: 0 }); - - model.tris = vtkHelper.newInstance(); - model.scalarTexture = vtkOpenGLTexture.newInstance(); - model.opacityTexture = vtkOpenGLTexture.newInstance(); - model.colorTexture = vtkOpenGLTexture.newInstance(); - model.jitterTexture = vtkOpenGLTexture.newInstance(); - model.jitterTexture.setWrapS(Wrap.REPEAT); - model.jitterTexture.setWrapT(Wrap.REPEAT); - model.framebuffer = vtkOpenGLFramebuffer.newInstance(); - - model.idxToView = mat4.identity(new Float64Array(16)); - model.idxNormalMatrix = mat3.identity(new Float64Array(9)); - model.modelToView = mat4.identity(new Float64Array(16)); - model.projectionToView = mat4.identity(new Float64Array(16)); - model.projectionToWorld = mat4.identity(new Float64Array(16)); - - model._lastScale = 1.0; - // Build VTK API macro.setGet(publicAPI, model, ['context']); @@ -1593,7 +1583,11 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkOpenGLVolumeMapper'); +export const newInstance = macro.newInstance( + extend, + 'vtkOpenGLVolumeMapper', + true +); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/SceneGraph/RenderPass/index.js b/Sources/Rendering/SceneGraph/RenderPass/index.js index d8a41f89c93..6eae0809daf 100644 --- a/Sources/Rendering/SceneGraph/RenderPass/index.js +++ b/Sources/Rendering/SceneGraph/RenderPass/index.js @@ -10,9 +10,13 @@ function vtkRenderPass(publicAPI, model) { publicAPI.setCurrentOperation = (val) => { model.currentOperation = val; - model.currentTraverseOperation = `traverse${macro.capitalize( - model.currentOperation - )}`; + if (!val) { + model.currentTraverseOperation = undefined; + } else { + model.currentTraverseOperation = `traverse${macro.capitalize( + model.currentOperation + )}`; + } }; publicAPI.getTraverseOperation = () => model.currentTraverseOperation; @@ -47,18 +51,21 @@ function vtkRenderPass(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - delegates: [], - currentOperation: null, - preDelegateOperations: [], - postDelegateOperations: [], - currentParent: null, -}; +function defaultValues(initialValues) { + return { + delegates: [], + currentOperation: null, + preDelegateOperations: [], + postDelegateOperations: [], + currentParent: null, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -76,7 +83,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkRenderPass'); +export const newInstance = macro.newInstance(extend, 'vtkRenderPass', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/SceneGraph/RenderWindowViewNode/index.js b/Sources/Rendering/SceneGraph/RenderWindowViewNode/index.js index b415c147e33..52e96586aee 100644 --- a/Sources/Rendering/SceneGraph/RenderWindowViewNode/index.js +++ b/Sources/Rendering/SceneGraph/RenderWindowViewNode/index.js @@ -150,20 +150,18 @@ function vtkRenderWindowViewNode(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - size: undefined, - selector: undefined, -}; +function defaultValues(initialValues) { + return { + size: [300, 300], + selector: undefined, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); - - if (!model.size) { - model.size = [300, 300]; - } - + Object.assign(initialValues, defaultValues(initialValues)); macro.getArray(publicAPI, model, ['size'], 2); macro.get(publicAPI, model, ['selector']); @@ -176,7 +174,11 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkRenderWindowViewNode'); +export const newInstance = macro.newInstance( + extend, + 'vtkRenderWindowViewNode', + true +); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/SceneGraph/ViewNode/index.js b/Sources/Rendering/SceneGraph/ViewNode/index.js index 55847486b64..68e094daf3b 100644 --- a/Sources/Rendering/SceneGraph/ViewNode/index.js +++ b/Sources/Rendering/SceneGraph/ViewNode/index.js @@ -190,29 +190,31 @@ function vtkViewNode(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - // _parent: null, - renderable: null, - myFactory: null, - children: [], - visited: false, -}; +function defaultValues(initialValues) { + return { + // _parent: null, + renderable: null, + myFactory: null, + children: [], + visited: false, + _renderableChildMap: new Map(), + ...initialValues, + }; +} // ---------------------------------------------------------------------------- function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + macro.moveToProtected(publicAPI, initialValues, ['parent']); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); macro.event(publicAPI, model, 'event'); - model._renderableChildMap = new Map(); - macro.get(publicAPI, model, ['visited']); macro.setGet(publicAPI, model, ['_parent', 'renderable', 'myFactory']); macro.getArray(publicAPI, model, ['children']); - macro.moveToProtected(publicAPI, model, ['parent']); // Object methods vtkViewNode(publicAPI, model); @@ -220,7 +222,7 @@ function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -const newInstance = macro.newInstance(extend, 'vtkViewNode'); +const newInstance = macro.newInstance(extend, 'vtkViewNode', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/SceneGraph/ViewNodeFactory/index.js b/Sources/Rendering/SceneGraph/ViewNodeFactory/index.js index 06b1497e390..f4a222b51b9 100644 --- a/Sources/Rendering/SceneGraph/ViewNodeFactory/index.js +++ b/Sources/Rendering/SceneGraph/ViewNodeFactory/index.js @@ -47,14 +47,17 @@ function vtkViewNodeFactory(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - // overrides: {}, -}; +function defaultValues(initialValues) { + return { + // overrides: {}, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); @@ -65,7 +68,11 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkViewNodeFactory'); +export const newInstance = macro.newInstance( + extend, + 'vtkViewNodeFactory', + true +); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/WebGPU/Actor/index.js b/Sources/Rendering/WebGPU/Actor/index.js index 1acc67ad693..3b185b8cca0 100644 --- a/Sources/Rendering/WebGPU/Actor/index.js +++ b/Sources/Rendering/WebGPU/Actor/index.js @@ -152,40 +152,40 @@ function vtkWebGPUActor(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - keyMatricesTime: null, - keyMatrices: null, - propID: undefined, - bufferShift: undefined, -}; +function defaultValues(initialValues) { + return { + // Internal objects + keyMatricesTime: macro.obj({}, { mtime: 0 }), + keyMatrices: { + normalMatrix: new Float64Array(16), + bcwc: new Float64Array(16), + bcsc: new Float64Array(16), + }, + bufferShift: [0, 0, 0, 0], + + propID: undefined, + bufferShift: undefined, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); - model.keyMatricesTime = {}; - macro.obj(model.keyMatricesTime, { mtime: 0 }); - model.keyMatrices = { - normalMatrix: new Float64Array(16), - bcwc: new Float64Array(16), - bcsc: new Float64Array(16), - }; - macro.get(publicAPI, model, ['propID', 'keyMatricesTime']); - model.bufferShift = [0, 0, 0, 0]; - // Object methods vtkWebGPUActor(publicAPI, model); } // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend); +export const newInstance = macro.newInstance(extend, undefined, true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/WebGPU/Camera/index.js b/Sources/Rendering/WebGPU/Camera/index.js index d44af44a855..3dcfb0c055b 100644 --- a/Sources/Rendering/WebGPU/Camera/index.js +++ b/Sources/Rendering/WebGPU/Camera/index.js @@ -131,32 +131,30 @@ function vtkWebGPUCamera(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - keyMatrixTime: null, - keyMatrices: null, -}; +function defaultValues(initialValues) { + return { + keyMatrixTime: macro.obj({}), + // values always get set by the get method + keyMatrices: { + normalMatrix: new Float64Array(16), + vcpc: new Float64Array(16), + pcsc: new Float64Array(16), + wcvc: new Float64Array(16), + scpc: new Float64Array(16), + scvc: new Float64Array(16), + }, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); - model.keyMatrixTime = {}; - macro.obj(model.keyMatrixTime); - - // values always get set by the get method - model.keyMatrices = { - normalMatrix: new Float64Array(16), - vcpc: new Float64Array(16), - pcsc: new Float64Array(16), - wcvc: new Float64Array(16), - scpc: new Float64Array(16), - scvc: new Float64Array(16), - }; - // Build VTK API macro.setGet(publicAPI, model, ['keyMatrixTime']); @@ -166,7 +164,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend); +export const newInstance = macro.newInstance(extend, undefined, true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/WebGPU/HardwareSelectionPass/index.js b/Sources/Rendering/WebGPU/HardwareSelectionPass/index.js index eecc2dd21ad..5b1d81f1577 100644 --- a/Sources/Rendering/WebGPU/HardwareSelectionPass/index.js +++ b/Sources/Rendering/WebGPU/HardwareSelectionPass/index.js @@ -120,16 +120,19 @@ function vtkWebGPUHardwareSelectionPass(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - selectionRenderEncoder: null, - colorTexture: null, - depthTexture: null, -}; +function defaultValues(initialValues) { + return { + selectionRenderEncoder: null, + colorTexture: null, + depthTexture: null, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API vtkRenderPass.extend(publicAPI, model, initialValues); @@ -144,7 +147,8 @@ export function extend(publicAPI, model, initialValues = {}) { export const newInstance = macro.newInstance( extend, - 'vtkWebGPUHardwareSelectionPass' + 'vtkWebGPUHardwareSelectionPass', + true ); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/WebGPU/HardwareSelector/index.js b/Sources/Rendering/WebGPU/HardwareSelector/index.js index b4d7db83bd8..aa32b14e7c3 100644 --- a/Sources/Rendering/WebGPU/HardwareSelector/index.js +++ b/Sources/Rendering/WebGPU/HardwareSelector/index.js @@ -417,20 +417,22 @@ function vtkWebGPUHardwareSelector(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - WebGPURenderWindow: null, -}; +function defaultValues(initialValues) { + return { + WebGPURenderWindow: null, + _selectionPass: vtkWebGPUHardwareSelectionPass.newInstance(), + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API vtkHardwareSelector.extend(publicAPI, model, initialValues); - model._selectionPass = vtkWebGPUHardwareSelectionPass.newInstance(); - macro.setGet(publicAPI, model, ['WebGPURenderWindow']); // Object methods @@ -441,7 +443,8 @@ export function extend(publicAPI, model, initialValues = {}) { export const newInstance = macro.newInstance( extend, - 'vtkWebGPUHardwareSelector' + 'vtkWebGPUHardwareSelector', + true ); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/WebGPU/PolyDataMapper/index.js b/Sources/Rendering/WebGPU/PolyDataMapper/index.js index 2047f8ef190..75f6bc844d1 100644 --- a/Sources/Rendering/WebGPU/PolyDataMapper/index.js +++ b/Sources/Rendering/WebGPU/PolyDataMapper/index.js @@ -96,20 +96,21 @@ function vtkWebGPUPolyDataMapper(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - primitives: null, -}; +function defaultValues(initialValues) { + return { + primitives: [], + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); - model.primitives = []; - // Object methods vtkWebGPUPolyDataMapper(publicAPI, model); } diff --git a/Sources/Rendering/WebGPU/RenderWindow/index.js b/Sources/Rendering/WebGPU/RenderWindow/index.js index 97224805820..6c50df58ced 100644 --- a/Sources/Rendering/WebGPU/RenderWindow/index.js +++ b/Sources/Rendering/WebGPU/RenderWindow/index.js @@ -548,57 +548,59 @@ function vtkWebGPURenderWindow(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - initialized: false, - context: null, - adapter: null, - device: null, - canvas: null, - cursorVisibility: true, - cursor: 'pointer', - containerSize: null, - renderPasses: [], - notifyStartCaptureImage: false, - imageFormat: 'image/png', - useOffScreen: false, - useBackgroundImage: false, - nextPropID: 1, - xrSupported: false, -}; +function defaultValues(initialValues) { + return { + // Internal instances + bgImage: new Image(), + myFactory: vtkWebGPUViewNodeFactory.newInstance(), + selector: vtkWebGPUHardwareSelector.newInstance(), + + initialized: false, + context: null, + adapter: null, + device: null, + canvas: null, + cursorVisibility: true, + cursor: 'pointer', + containerSize: null, + renderPasses: [], + notifyStartCaptureImage: false, + imageFormat: 'image/png', + useOffScreen: false, + useBackgroundImage: false, + nextPropID: 1, + xrSupported: false, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); - // Create internal instances + // model.canvas needs to be set to model before calling other setters model.canvas = document.createElement('canvas'); model.canvas.style.width = '100%'; - // Create internal bgImage - model.bgImage = new Image(); - model.bgImage.style.position = 'absolute'; - model.bgImage.style.left = '0'; - model.bgImage.style.top = '0'; - model.bgImage.style.width = '100%'; - model.bgImage.style.height = '100%'; - model.bgImage.style.zIndex = '-1'; + initialValues.bgImage.style.position = 'absolute'; + initialValues.bgImage.style.left = '0'; + initialValues.bgImage.style.top = '0'; + initialValues.bgImage.style.width = '100%'; + initialValues.bgImage.style.height = '100%'; + initialValues.bgImage.style.zIndex = '-1'; // Inheritance vtkRenderWindowViewNode.extend(publicAPI, model, initialValues); - model.myFactory = vtkWebGPUViewNodeFactory.newInstance(); /* eslint-disable no-use-before-define */ - model.myFactory.registerOverride('vtkRenderWindow', newInstance); + initialValues.myFactory.registerOverride('vtkRenderWindow', newInstance); /* eslint-enable no-use-before-define */ // setup default forward pass rendering - model.renderPasses[0] = vtkForwardPass.newInstance(); + initialValues.renderPasses[0] = vtkForwardPass.newInstance(); - if (!model.selector) { - model.selector = vtkWebGPUHardwareSelector.newInstance(); - model.selector.setWebGPURenderWindow(publicAPI); - } + initialValues.selector.setWebGPURenderWindow(publicAPI); macro.event(publicAPI, model, 'imageReady'); macro.event(publicAPI, model, 'initialized'); @@ -630,7 +632,11 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkWebGPURenderWindow'); +export const newInstance = macro.newInstance( + extend, + 'vtkWebGPURenderWindow', + true +); // ---------------------------------------------------------------------------- // Register API specific RenderWindow implementation diff --git a/Sources/Rendering/WebGPU/Renderer/index.js b/Sources/Rendering/WebGPU/Renderer/index.js index cefca9a1c31..9bef9bd21a4 100644 --- a/Sources/Rendering/WebGPU/Renderer/index.js +++ b/Sources/Rendering/WebGPU/Renderer/index.js @@ -314,40 +314,42 @@ function vtkWebGPURenderer(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - bindGroup: null, - selector: null, - renderEncoder: null, - recenterThreshold: 20.0, - suppressClear: false, - stabilizedCenter: [0.0, 0.0, 0.0], -}; +function defaultValues(initialValues) { + return { + // Internal objects + UBO: vtkWebGPUUniformBuffer.newInstance({ label: 'rendererUBO' }), + bindGroup: vtkWebGPUBindGroup.newInstance({ label: 'rendererBG' }), + tmpMat4: mat4.identity(new Float64Array(16)), + stabilizedTime: macro.obj({}, { mtime: 0 }), + + selector: null, + renderEncoder: null, + recenterThreshold: 20.0, + suppressClear: false, + stabilizedCenter: [0.0, 0.0, 0.0], + + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); - model.UBO = vtkWebGPUUniformBuffer.newInstance({ label: 'rendererUBO' }); - model.UBO.addEntry('WCVCMatrix', 'mat4x4'); - model.UBO.addEntry('SCPCMatrix', 'mat4x4'); - model.UBO.addEntry('PCSCMatrix', 'mat4x4'); - model.UBO.addEntry('SCVCMatrix', 'mat4x4'); - model.UBO.addEntry('VCPCMatrix', 'mat4x4'); - model.UBO.addEntry('WCVCNormals', 'mat4x4'); - model.UBO.addEntry('viewportSize', 'vec2'); - model.UBO.addEntry('cameraParallel', 'u32'); - - model.bindGroup = vtkWebGPUBindGroup.newInstance({ label: 'rendererBG' }); - model.bindGroup.setBindables([model.UBO]); - - model.tmpMat4 = mat4.identity(new Float64Array(16)); + initialValues.UBO.addEntry('WCVCMatrix', 'mat4x4'); + initialValues.UBO.addEntry('SCPCMatrix', 'mat4x4'); + initialValues.UBO.addEntry('PCSCMatrix', 'mat4x4'); + initialValues.UBO.addEntry('SCVCMatrix', 'mat4x4'); + initialValues.UBO.addEntry('VCPCMatrix', 'mat4x4'); + initialValues.UBO.addEntry('WCVCNormals', 'mat4x4'); + initialValues.UBO.addEntry('viewportSize', 'vec2'); + initialValues.UBO.addEntry('cameraParallel', 'u32'); - model.stabilizedTime = {}; - macro.obj(model.stabilizedTime, { mtime: 0 }); + initialValues.bindGroup.setBindables([initialValues.UBO]); // Build VTK API macro.get(publicAPI, model, ['bindGroup', 'stabilizedTime']); @@ -365,7 +367,7 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkWebGPURenderer'); +export const newInstance = macro.newInstance(extend, 'vtkWebGPURenderer', true); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/WebGPU/ShaderCache/index.js b/Sources/Rendering/WebGPU/ShaderCache/index.js index ef19b82835b..85d1d9a19aa 100644 --- a/Sources/Rendering/WebGPU/ShaderCache/index.js +++ b/Sources/Rendering/WebGPU/ShaderCache/index.js @@ -52,20 +52,20 @@ function vtkWebGPUShaderCache(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - shaderModules: null, - device: null, - window: null, -}; +function defaultValues(initialValues) { + return { + _shaderModules: new Map(), + device: null, + window: null, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); - - // Internal objects - model._shaderModules = new Map(); - + macro.moveToProtected(publicAPI, initialValues, ['shaderModules']); + Object.assign(initialValues, defaultValues(initialValues)); // Build VTK API macro.obj(publicAPI, model); macro.setGet(publicAPI, model, ['device', 'window']); @@ -76,7 +76,11 @@ export function extend(publicAPI, model, initialValues = {}) { // ---------------------------------------------------------------------------- -export const newInstance = macro.newInstance(extend, 'vtkWebGPUShaderCache'); +export const newInstance = macro.newInstance( + extend, + 'vtkWebGPUShaderCache', + true +); // ---------------------------------------------------------------------------- diff --git a/Sources/Rendering/WebGPU/ViewNodeFactory/index.js b/Sources/Rendering/WebGPU/ViewNodeFactory/index.js index 0cb5dff6695..829fd28dee2 100644 --- a/Sources/Rendering/WebGPU/ViewNodeFactory/index.js +++ b/Sources/Rendering/WebGPU/ViewNodeFactory/index.js @@ -20,15 +20,18 @@ function vtkWebGPUViewNodeFactory(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = {}; +function defaultValues(initialValues) { + return { + // Static class mapping shared across instances + overrides: CLASS_MAPPING, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); - - // Static class mapping shared across instances - model.overrides = CLASS_MAPPING; + Object.assign(initialValues, defaultValues(initialValues)); // Inheritance vtkViewNodeFactory.extend(publicAPI, model, initialValues); @@ -41,7 +44,8 @@ export function extend(publicAPI, model, initialValues = {}) { export const newInstance = macro.newInstance( extend, - 'vtkWebGPUViewNodeFactory' + 'vtkWebGPUViewNodeFactory', + true ); // ---------------------------------------------------------------------------- diff --git a/Sources/Testing/testMacro.js b/Sources/Testing/testMacro.js index d1118059079..ced286fa72b 100644 --- a/Sources/Testing/testMacro.js +++ b/Sources/Testing/testMacro.js @@ -21,25 +21,35 @@ function myClass(publicAPI, model) { // ---------------------------------------------------------------------------- // Object factory // ---------------------------------------------------------------------------- -const DEFAULT_VALUES = { - myProp1: [0, 0, 0], - myProp2: null, // Do not initialize internal objects here - myProp3: true, - myProp4: 6, - myProp5: [1, 2, 3, 4], - myProp6: [0.1, 0.2, 0.3, 0.4, 0.5], - myProp7: MY_ENUM.FIRST, - myProp8: [1, 2, 3], - myProp9: null, - // myProp10: null, - myProp11: 11, - _myProp12: [12], - _myProp13: 13, -}; +function defaultValues(initialValues) { + return { + myProp1: [0, 0, 0], + myProp2: null, // Do not initialize internal objects here + myProp3: true, + myProp4: 6, + myProp5: [1, 2, 3, 4], + myProp6: [0.1, 0.2, 0.3, 0.4, 0.5], + myProp7: MY_ENUM.FIRST, + myProp8: [1, 2, 3], + myProp9: null, + // myProp10: null, + _myProp11: 11, + _myProp12: [12], + _myProp13: 13, + _myProp14: 14, + ...initialValues, + }; +} // ---------------------------------------------------------------------------- function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, DEFAULT_VALUES, initialValues); + macro.moveToProtected(publicAPI, initialValues, [ + 'myProp11', + 'myProp12', + 'myProp13', + 'myProp14', + ]); + Object.assign(initialValues, defaultValues(initialValues)); // Object methods macro.obj(publicAPI, model); @@ -72,7 +82,12 @@ function extend(publicAPI, model, initialValues = {}) { // Protected variables macro.setGet(publicAPI, model, ['_myProp11']); macro.setGetArray(publicAPI, model, ['_myProp12'], 1); - macro.moveToProtected(publicAPI, model, ['myProp11', 'myProp12', 'myProp13']); + macro.moveToProtected(publicAPI, initialValues, [ + 'myProp11', + 'myProp12', + 'myProp13', + 'myProp14', + ]); // Object specific methods myClass(publicAPI, model); @@ -93,7 +108,7 @@ test('Macro methods scalar tests', (t) => { t.equal( myTestClass.getMyProp3(), - DEFAULT_VALUES.myProp3, + defaultValues().myProp3, 'Initial gets should match defaults' ); myTestClass.setMyProp3(false); @@ -102,7 +117,7 @@ test('Macro methods scalar tests', (t) => { const mtime1 = myTestClass.getMTime(); t.equal( myTestClass.getMyProp4(), - DEFAULT_VALUES.myProp4, + defaultValues().myProp4, 'Initial gets should match defaults' ); myTestClass.setMyProp4(42); @@ -121,7 +136,7 @@ test('Macro methods array tests', (t) => { t.deepEqual( myTestClass.getMyProp1(), - DEFAULT_VALUES.myProp1, + defaultValues().myProp1, 'Initial gets should match defaults' ); // we must wrap the non-existent call inside another function to avoid test program exiting, and tape-catch generating error. @@ -155,7 +170,7 @@ test('Macro methods array tests', (t) => { ); t.deepEqual( myTestClass.getMyProp6(), - DEFAULT_VALUES.myProp6, + defaultValues().myProp6, 'Keep default value after illegal set' ); @@ -290,9 +305,9 @@ test('Macro methods array tests', (t) => { test('Macro protected variables tests', (t) => { const defaultInstance = newInstance(); t.deepEqual(defaultInstance.get('_myProp11', '_myProp12', '_myProp13'), { - _myProp11: DEFAULT_VALUES.myProp11, - _myProp12: DEFAULT_VALUES._myProp12, - _myProp13: DEFAULT_VALUES._myProp13, + _myProp11: defaultValues()._myProp11, + _myProp12: defaultValues()._myProp12, + _myProp13: defaultValues()._myProp13, }); // getter must have been renamed t.notOk(defaultInstance.get_myProp11); @@ -304,8 +319,8 @@ test('Macro protected variables tests', (t) => { t.notOk(defaultInstance.set_myProp12); t.notOk(defaultInstance.set_myProp13); - t.equal(defaultInstance.getMyProp11(), DEFAULT_VALUES.myProp11); - t.deepEqual(defaultInstance.getMyProp12(), DEFAULT_VALUES._myProp12); + t.equal(defaultInstance.getMyProp11(), defaultValues()._myProp11); + t.deepEqual(defaultInstance.getMyProp12(), defaultValues()._myProp12); t.notOk(defaultInstance.getMyProp13); t.notOk(defaultInstance.getMyProp11ByReference); @@ -327,22 +342,32 @@ test('Macro protected variables tests', (t) => { myProp11: 111, myProp12: [112], myProp13: 113, + myProp14: 114, }); - t.deepEqual(overridenInstance.get('_myProp11', '_myProp12', '_myProp13'), { - _myProp11: 111, - _myProp12: [112], - _myProp13: 113, - }); + t.deepEqual( + overridenInstance.get('_myProp11', '_myProp12', '_myProp13', '_myProp14'), + { + _myProp11: 111, + _myProp12: [112], + _myProp13: 113, + _myProp14: 114, + } + ); const overridenInstance2 = newInstance({ _myProp11: 111, _myProp12: [112], _myProp13: 113, + _myProp14: 114, }); - t.deepEqual(overridenInstance2.get('_myProp11', '_myProp12', '_myProp13'), { - _myProp11: DEFAULT_VALUES.myProp11, // TBD - _myProp12: [112], - _myProp13: 113, - }); + t.deepEqual( + overridenInstance2.get('_myProp11', '_myProp12', '_myProp13', '_myProp14'), + { + _myProp11: 111, + _myProp12: [112], + _myProp13: 113, + _myProp14: 114, + } + ); t.end(); }); @@ -351,7 +376,7 @@ test('Macro methods enum tests', (t) => { t.equal( myTestClass.getMyProp7(), - DEFAULT_VALUES.myProp7, + defaultValues().myProp7, 'Initial gets should match defaults' ); myTestClass.setMyProp7(MY_ENUM.THIRD); @@ -408,14 +433,16 @@ test('Macro methods enum tests', (t) => { test('Macro methods object tests', (t) => { const myTestClass = newInstance(); - const defaultValues = { ...DEFAULT_VALUES }; - defaultValues._myProp11 = defaultValues.myProp11; - delete defaultValues.myProp11; + const copyDefaultValues = { ...defaultValues() }; + copyDefaultValues._myProp11 = copyDefaultValues.myProp11; + copyDefaultValues._myProp14 = copyDefaultValues.myProp14; + delete copyDefaultValues.myProp11; + delete copyDefaultValues.myProp14; t.ok(myTestClass.get(), 'Get entire model'); t.deepEqual( - myTestClass.get(...Object.keys(defaultValues)), - defaultValues, + myTestClass.get(...Object.keys(copyDefaultValues)), + copyDefaultValues, 'Get defaults back test' ); diff --git a/Sources/macros.js b/Sources/macros.js index c7bc3cbaf74..656eb805de7 100644 --- a/Sources/macros.js +++ b/Sources/macros.js @@ -309,7 +309,7 @@ export function obj(publicAPI = {}, model = {}) { publicAPI.set = (map = {}, noWarning = false, noFunction = false) => { let ret = false; Object.keys(map).forEach((name) => { - const fn = noFunction ? null : publicAPI[`set${capitalize(name)}`]; + const fn = noFunction ? null : publicAPI[`set${_capitalize(name)}`]; if (fn && Array.isArray(map[name]) && fn.length > 1) { ret = fn(...map[name]) || ret; } else if (fn) { @@ -639,12 +639,14 @@ export function setGetArray( setArray(publicAPI, model, fieldNames, size, defaultVal); } -export function moveToProtected(publicAPI, model, fieldNames) { +export function moveToProtected(publicAPI, values, fieldNames) { for (let i = 0; i < fieldNames.length; i++) { const fieldName = fieldNames[i]; - if (model[fieldName] !== undefined) { - model[`_${fieldName}`] = model[fieldName]; - delete model[fieldName]; + if (values[fieldName] !== undefined) { + if (values[`_${fieldName}`] === undefined) { + values[`_${fieldName}`] = values[fieldName]; + } + delete values[fieldName]; } } } @@ -967,11 +969,12 @@ export function event(publicAPI, model, eventName) { // newInstance // ---------------------------------------------------------------------------- -export function newInstance(extend, className) { - const constructor = (initialValues = {}) => { +export function newInstance(extend, className, toSet = false) { + const constructor = (initialValues = {}, toSetWhenVtkCalled = false) => { const model = {}; const publicAPI = {}; extend(publicAPI, model, initialValues); + if (toSet || toSetWhenVtkCalled) publicAPI.set(initialValues, true); return Object.freeze(publicAPI); }; diff --git a/Sources/vtk.js b/Sources/vtk.js index e2499df60a5..cc3bbc80001 100644 --- a/Sources/vtk.js +++ b/Sources/vtk.js @@ -44,7 +44,7 @@ export default function vtk(obj) { }); // Return the root - const newInst = constructor(model); + const newInst = constructor(model, true); if (newInst && newInst.modified) { newInst.modified(); }