diff --git a/.eslintrc.js b/.eslintrc.js index 3a6ce10158c..37af905b2b7 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -18,6 +18,7 @@ module.exports = { 'jsx-a11y/label-has-for': 0, 'no-console': 0, 'no-plusplus': 0, + 'no-underscore-dangle': 0, 'import/no-named-as-default': 0, 'import/no-named-as-default-member': 0, diff --git a/Sources/Common/Core/DataArray/index.js b/Sources/Common/Core/DataArray/index.js index 04d7920e86f..2338f04ec7d 100644 --- a/Sources/Common/Core/DataArray/index.js +++ b/Sources/Common/Core/DataArray/index.js @@ -307,9 +307,9 @@ export function extend(publicAPI, model, initialValues = {}) { } if (!model.values) { - model.values = new window[model.dataType](model.size); + model.values = macro.newTypedArray(model.dataType, model.size); } else if (Array.isArray(model.values)) { - model.values = window[model.dataType].from(model.values); + model.values = macro.newTypedArrayFrom(model.dataType, model.values); } if (model.values) { diff --git a/Sources/Common/Core/Points/index.js b/Sources/Common/Core/Points/index.js index 4cf4e36a3b5..10a9645ea7b 100644 --- a/Sources/Common/Core/Points/index.js +++ b/Sources/Common/Core/Points/index.js @@ -20,7 +20,7 @@ function vtkPoints(publicAPI, model) { publicAPI.setNumberOfPoints = (nbPoints, dimension = 3) => { if (publicAPI.getNumberOfPoints() !== nbPoints) { model.size = nbPoints * dimension; - model.values = new window[model.dataType](model.size); + model.values = macro.newTypedArray(model.dataType, model.size); publicAPI.setNumberOfComponents(dimension); publicAPI.modified(); } diff --git a/Sources/Common/Core/ScalarsToColors/index.js b/Sources/Common/Core/ScalarsToColors/index.js index dee8ae019df..4722bad525d 100644 --- a/Sources/Common/Core/ScalarsToColors/index.js +++ b/Sources/Common/Core/ScalarsToColors/index.js @@ -222,7 +222,8 @@ function vtkScalarsToColors(publicAPI, model) { dataType: VtkDataTypes.UNSIGNED_CHAR, }; - const s = new window[newscalars.dataType]( + const s = macro.newTypedArray( + newscalars.dataType, 4 * scalars.getNumberOfTuples() ); newscalars.values = s; diff --git a/Sources/Common/DataModel/Cell/index.js b/Sources/Common/DataModel/Cell/index.js index a3ce1c5c354..cd47b1127bd 100644 --- a/Sources/Common/DataModel/Cell/index.js +++ b/Sources/Common/DataModel/Cell/index.js @@ -21,7 +21,8 @@ function vtkCell(publicAPI, model) { model.pointsIds = pointIdsList; let triangleData = model.points.getData(); if (triangleData.length !== 3 * model.pointsIds.length) { - triangleData = new window[points.getDataType()]( + triangleData = macro.newTypedArray( + points.getDataType(), 3 * model.pointsIds.length ); } diff --git a/Sources/Common/DataModel/Cell/test/testCell.js b/Sources/Common/DataModel/Cell/test/testCell.js index f4d4d3b0604..751426f978e 100644 --- a/Sources/Common/DataModel/Cell/test/testCell.js +++ b/Sources/Common/DataModel/Cell/test/testCell.js @@ -27,7 +27,7 @@ test('Test vtkCell initialize without pointsIds', (t) => { test('Test vtkCell initialize with pointsIds', (t) => { const points = vtkPoints.newInstance(); - points.setData([0, 0, 0, 2, 0, 0, 2, 2, 0]); + points.setData(Float64Array.from([0, 0, 0, 2, 0, 0, 2, 2, 0])); const cell = vtkCell.newInstance(); diff --git a/Sources/Filters/Core/Cutter/index.js b/Sources/Filters/Core/Cutter/index.js index 3e664867e1f..440d098e1b4 100644 --- a/Sources/Filters/Core/Cutter/index.js +++ b/Sources/Filters/Core/Cutter/index.js @@ -1,7 +1,7 @@ import * as macro from 'vtk.js/Sources/macro'; import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData'; -const { vtkErrorMacro, TYPED_ARRAYS } = macro; +const { vtkErrorMacro } = macro; function initPolyIterator(pd) { const polys = pd.getPolys().getData(); @@ -235,7 +235,7 @@ function vtkCutter(publicAPI, model) { // Set points const outputPoints = output.getPoints(); outputPoints.setData( - TYPED_ARRAYS[points.getDataType()].from(newPointsData), + macro.newTypedArrayFrom(points.getDataType(), newPointsData), 3 ); diff --git a/Sources/Filters/Cornerstone/ImageDataToCornerstoneImage/index.js b/Sources/Filters/Cornerstone/ImageDataToCornerstoneImage/index.js index 3f9a5692c58..96adf97d583 100644 --- a/Sources/Filters/Cornerstone/ImageDataToCornerstoneImage/index.js +++ b/Sources/Filters/Cornerstone/ImageDataToCornerstoneImage/index.js @@ -41,7 +41,8 @@ function vtkImageDataToCornerstoneImage(publicAPI, model) { } else { const offset = model.sliceIndex * dims[0] * dims[1] * rawData.BYTES_PER_ELEMENT; - pixelData = new macro.TYPED_ARRAYS[scalars.getDataType()]( + pixelData = macro.newTypedArray( + scalars.getDataType(), rawData.buffer, offset, dims[0] * dims[1] diff --git a/Sources/Filters/General/TriangleFilter/index.js b/Sources/Filters/General/TriangleFilter/index.js index 6920cc60946..7f9e6181b12 100644 --- a/Sources/Filters/General/TriangleFilter/index.js +++ b/Sources/Filters/General/TriangleFilter/index.js @@ -73,10 +73,10 @@ function vtkTriangleFilter(publicAPI, model) { const dataset = vtkPolyData.newInstance(); dataset .getPoints() - .setData(macro.TYPED_ARRAYS[pointsDataType].from(newPoints)); + .setData(macro.newTypedArrayFrom(pointsDataType, newPoints)); dataset .getPolys() - .setData(macro.TYPED_ARRAYS[cellsDataType].from(newCells)); + .setData(macro.newTypedArrayFrom(cellsDataType, newCells)); outData[0] = dataset; }; diff --git a/Sources/Filters/General/WindowedSincPolyDataFilter/index.js b/Sources/Filters/General/WindowedSincPolyDataFilter/index.js index 1154211fcd1..3783827a992 100644 --- a/Sources/Filters/General/WindowedSincPolyDataFilter/index.js +++ b/Sources/Filters/General/WindowedSincPolyDataFilter/index.js @@ -334,7 +334,7 @@ function vtkWindowedSincPolyDataFilter(publicAPI, model) { // for (let i = 0; i < numPts; ++i) { // newPts[zero].setPoint(i, inPts.subarray(i)); // } - const copy = new window[newPts[zero].getDataType()](inPtsData); + const copy = macro.newTypedArray(newPts[zero].getDataType(), inPtsData); newPts[zero].setData(copy, 3); } else { // center the data and scale to be within unit cube [-1, 1] diff --git a/Sources/Filters/Sources/Arrow2DSource/index.js b/Sources/Filters/Sources/Arrow2DSource/index.js index b67c591526b..8b205094c60 100644 --- a/Sources/Filters/Sources/Arrow2DSource/index.js +++ b/Sources/Filters/Sources/Arrow2DSource/index.js @@ -12,7 +12,7 @@ const { ShapeType } = Constants; function vtkStarSource(publicAPI, model) { const dataset = vtkPolyData.newInstance(); - const points = new macro.TYPED_ARRAYS[model.pointType](10 * 3); + const points = macro.newTypedArray(model.pointType, 10 * 3); const edges = new Uint32Array(11); edges[0] = 10; for (let i = 0; i < 10; i++) { @@ -33,7 +33,7 @@ function vtkStarSource(publicAPI, model) { function vtk6PointsArrow(publicAPI, model) { const dataset = vtkPolyData.newInstance(); - const points = new macro.TYPED_ARRAYS[model.pointType](6 * 3); + const points = macro.newTypedArray(model.pointType, 6 * 3); const thickOp = model.height * 0.5 * model.thickness; @@ -87,7 +87,7 @@ function vtk6PointsArrow(publicAPI, model) { function vtk4PointsArrow(publicAPI, model) { const dataset = vtkPolyData.newInstance(); - const points = new macro.TYPED_ARRAYS[model.pointType](4 * 3); + const points = macro.newTypedArray(model.pointType, 4 * 3); const thickOp = (model.height / 3) * model.thickness; @@ -124,7 +124,7 @@ function vtk4PointsArrow(publicAPI, model) { function vtkTriangleSource(publicAPI, model) { const dataset = vtkPolyData.newInstance(); - const points = new macro.TYPED_ARRAYS[model.pointType](3 * 3); + const points = macro.newTypedArray(model.pointType, 3 * 3); const baseOffsetOp = model.height * (1 - model.base); @@ -174,10 +174,10 @@ function vtkArrow2DSource(publicAPI, model) { /** * height modifies the size of the source along x axis * width modifies the size of the source along y axis - + * thickness modifies the shape of the source, which becomes wider on - * y axis - + * y axis + * base modifies the position which lowers the source on x axis for 0 and moves * the source up on x axis for 1 */ diff --git a/Sources/Filters/Sources/CircleSource/index.js b/Sources/Filters/Sources/CircleSource/index.js index c90d47279aa..fa693133d47 100644 --- a/Sources/Filters/Sources/CircleSource/index.js +++ b/Sources/Filters/Sources/CircleSource/index.js @@ -19,7 +19,7 @@ function vtkCircleSource(publicAPI, model) { let dataset = outData[0]; // Points - const points = new window[model.pointType](model.resolution * 3); + const points = macro.newTypedArray(model.pointType, model.resolution * 3); // Lines/cells // [# of points in line, vert_index_0, vert_index_1, ..., vert_index_0] diff --git a/Sources/Filters/Sources/ConcentricCylinderSource/index.js b/Sources/Filters/Sources/ConcentricCylinderSource/index.js index 81c2713abca..8ed21992712 100644 --- a/Sources/Filters/Sources/ConcentricCylinderSource/index.js +++ b/Sources/Filters/Sources/ConcentricCylinderSource/index.js @@ -188,7 +188,7 @@ function vtkConcentricCylinderSource(publicAPI, model) { // Points let pointIdx = 0; - const points = new window[model.pointType](numberOfPoints * 3); + const points = macro.newTypedArray(model.pointType, numberOfPoints * 3); // Cells let cellLocation = 0; diff --git a/Sources/Filters/Sources/ConeSource/index.js b/Sources/Filters/Sources/ConeSource/index.js index 6b63b601307..6b672af1eec 100644 --- a/Sources/Filters/Sources/ConeSource/index.js +++ b/Sources/Filters/Sources/ConeSource/index.js @@ -24,7 +24,7 @@ function vtkConeSource(publicAPI, model) { // Points let pointIdx = 0; - const points = new window[model.pointType](numberOfPoints * 3); + const points = macro.newTypedArray(model.pointType, numberOfPoints * 3); // Cells let cellLocation = 0; diff --git a/Sources/Filters/Sources/CubeSource/index.js b/Sources/Filters/Sources/CubeSource/index.js index 93cc5b5bb41..9b13227e63c 100644 --- a/Sources/Filters/Sources/CubeSource/index.js +++ b/Sources/Filters/Sources/CubeSource/index.js @@ -23,10 +23,10 @@ function vtkCubeSource(publicAPI, model) { const numberOfPoints = 24; // Define points - const points = new window[model.pointType](numberOfPoints * 3); + const points = macro.newTypedArray(model.pointType, numberOfPoints * 3); polyData.getPoints().setData(points, 3); - const normals = new window[model.pointType](numberOfPoints * 3); + const normals = macro.newTypedArray(model.pointType, numberOfPoints * 3); const normalArray = vtkDataArray.newInstance({ name: 'Normals', values: normals, @@ -39,7 +39,10 @@ function vtkCubeSource(publicAPI, model) { tcdim = 3; } - const textureCoords = new window[model.pointType](numberOfPoints * tcdim); + const textureCoords = macro.newTypedArray( + model.pointType, + numberOfPoints * tcdim + ); const tcoords = vtkDataArray.newInstance({ name: 'TextureCoordinates', values: textureCoords, @@ -189,7 +192,7 @@ function vtkCubeSource(publicAPI, model) { .apply(points); // Define quads - const polys = new window[model.pointType](numberOfPolys * 5); + const polys = macro.newTypedArray(model.pointType, numberOfPolys * 5); polyData.getPolys().setData(polys, 1); let polyIndex = 0; diff --git a/Sources/Filters/Sources/CylinderSource/index.js b/Sources/Filters/Sources/CylinderSource/index.js index 39673e02ddf..765c9aaa8f2 100644 --- a/Sources/Filters/Sources/CylinderSource/index.js +++ b/Sources/Filters/Sources/CylinderSource/index.js @@ -28,7 +28,7 @@ function vtkCylinderSource(publicAPI, model) { } // Points - const points = new window[model.pointType](numberOfPoints * 3); + const points = macro.newTypedArray(model.pointType, numberOfPoints * 3); // Cells let cellLocation = 0; diff --git a/Sources/Filters/Sources/LineSource/index.js b/Sources/Filters/Sources/LineSource/index.js index 37b325a2ce4..25027b316f2 100644 --- a/Sources/Filters/Sources/LineSource/index.js +++ b/Sources/Filters/Sources/LineSource/index.js @@ -36,7 +36,7 @@ function vtkLineSource(publicAPI, model) { const numPts = xres + 1; // Points - const points = new window[pointDataType](numPts * 3); + const points = macro.newTypedArray(pointDataType, numPts * 3); pd.getPoints().setData(points, 3); // Cells diff --git a/Sources/Filters/Sources/PlaneSource/index.js b/Sources/Filters/Sources/PlaneSource/index.js index cd8909b0121..588da26c4e8 100644 --- a/Sources/Filters/Sources/PlaneSource/index.js +++ b/Sources/Filters/Sources/PlaneSource/index.js @@ -47,7 +47,7 @@ function vtkPlaneSource(publicAPI, model) { const numPolys = xres * yres; // Points - const points = new window[pointDataType](numPts * 3); + const points = macro.newTypedArray(pointDataType, numPts * 3); pd.getPoints().setData(points, 3); // Cells diff --git a/Sources/Filters/Sources/PointSource/index.js b/Sources/Filters/Sources/PointSource/index.js index bc102c93064..35d422b11ed 100644 --- a/Sources/Filters/Sources/PointSource/index.js +++ b/Sources/Filters/Sources/PointSource/index.js @@ -27,7 +27,7 @@ function vtkPointSource(publicAPI, model) { const numPts = model.numberOfPoints; // Points - const points = new window[pointDataType](numPts * 3); + const points = macro.newTypedArray(pointDataType, numPts * 3); pd.getPoints().setData(points, 3); // Cells diff --git a/Sources/Filters/Sources/SLICSource/index.js b/Sources/Filters/Sources/SLICSource/index.js index 84adb713bce..13e36b3ba92 100644 --- a/Sources/Filters/Sources/SLICSource/index.js +++ b/Sources/Filters/Sources/SLICSource/index.js @@ -125,7 +125,10 @@ function vtkSLICSource(publicAPI, model) { const nbBytes = (model.clusters.length < 256 ? 8 : 0) || (model.clusters.length < 65536 ? 16 : 32); - const clusterIdxValues = new window[`Uint${nbBytes}Array`](dataSize); + const clusterIdxValues = macro.newTypedArray( + `Uint${nbBytes}Array`, + dataSize + ); for (let i = 0; i < dataSize; i++) { let clusterDistance = Number.MAX_VALUE; model.clusters.forEach((cluster, idx) => { diff --git a/Sources/Filters/Sources/SphereSource/index.js b/Sources/Filters/Sources/SphereSource/index.js index 6bfbdbe616d..a91dacabf35 100644 --- a/Sources/Filters/Sources/SphereSource/index.js +++ b/Sources/Filters/Sources/SphereSource/index.js @@ -52,7 +52,7 @@ function vtkSphereSource(publicAPI, model) { // Points let pointIdx = 0; - let points = new window[pointDataType](numPts * 3); + let points = macro.newTypedArray(pointDataType, numPts * 3); // Normals let normals = new Float32Array(numPts * 3); diff --git a/Sources/Filters/Sources/ViewFinderSource/index.js b/Sources/Filters/Sources/ViewFinderSource/index.js index e756b05a6e5..909be279992 100644 --- a/Sources/Filters/Sources/ViewFinderSource/index.js +++ b/Sources/Filters/Sources/ViewFinderSource/index.js @@ -6,7 +6,7 @@ function vtkViewFinderSource(publicAPI, model) { publicAPI.requestData = (inData, outData) => { const dataset = vtkPolyData.newInstance(); - const points = new macro.TYPED_ARRAYS[model.pointType](3 * 16); + const points = macro.newTypedArray(model.pointType, 3 * 16); points[0] = model.radius; points[1] = model.radius / model.width; @@ -71,7 +71,7 @@ function vtkViewFinderSource(publicAPI, model) { 3, 4, 6, 5, 3, 6, 5, 7, 3, 8, 11, 9, - 3, 8, 10, 11, + 3, 8, 10, 11, 3, 12, 13, 15, 3, 12, 15, 14, ]); diff --git a/Sources/IO/Core/DataAccessHelper/HtmlDataAccessHelper.js b/Sources/IO/Core/DataAccessHelper/HtmlDataAccessHelper.js index 65ded1a2f51..f7ff576a796 100644 --- a/Sources/IO/Core/DataAccessHelper/HtmlDataAccessHelper.js +++ b/Sources/IO/Core/DataAccessHelper/HtmlDataAccessHelper.js @@ -93,7 +93,7 @@ function fetchArray(instance = {}, baseURL, array, options = {}) { Endian.swapBytes(array.buffer, DataTypeByteSize[array.dataType]); } - array.values = new window[array.dataType](array.buffer); + array.values = macro.newTypedArray(array.dataType, array.buffer); } if (array.values.length !== array.size) { diff --git a/Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper.js b/Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper.js index 2fd7704e0a3..a863df50ffa 100644 --- a/Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper.js +++ b/Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper.js @@ -94,7 +94,7 @@ function fetchArray(instance = {}, baseURL, array, options = {}) { ); } - array.values = new window[array.dataType](array.buffer); + array.values = macro.newTypedArray(array.dataType, array.buffer); } if (array.values.length !== array.size) { diff --git a/Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper.js b/Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper.js index 355a117ff79..83309837e7d 100644 --- a/Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper.js +++ b/Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper.js @@ -43,7 +43,7 @@ function handleUint8Array(array, compression, done) { Endian.swapBytes(array.buffer, DataTypeByteSize[array.dataType]); } - array.values = new window[array.dataType](array.buffer); + array.values = macro.newTypedArray(array.dataType, array.buffer); } if (array.values.length !== array.size) { diff --git a/Sources/IO/Core/DataAccessHelper/LiteHttpDataAccessHelper.js b/Sources/IO/Core/DataAccessHelper/LiteHttpDataAccessHelper.js index 4c38dca19f2..c5c3d45d923 100644 --- a/Sources/IO/Core/DataAccessHelper/LiteHttpDataAccessHelper.js +++ b/Sources/IO/Core/DataAccessHelper/LiteHttpDataAccessHelper.js @@ -90,7 +90,7 @@ function fetchArray(instance = {}, baseURL, array, options = {}) { ); } - array.values = new window[array.dataType](array.buffer); + array.values = macro.newTypedArray(array.dataType, array.buffer); } if (array.values.length !== array.size) { diff --git a/Sources/IO/Core/ZipMultiDataSetReader/index.js b/Sources/IO/Core/ZipMultiDataSetReader/index.js index 6b795eb2d21..7c92884fcb6 100644 --- a/Sources/IO/Core/ZipMultiDataSetReader/index.js +++ b/Sources/IO/Core/ZipMultiDataSetReader/index.js @@ -75,7 +75,7 @@ function vtkZipMultiDataSetReader(publicAPI, model) { zipEntry.async('arraybuffer').then((arraybuffer) => { processing--; const [type, id] = relativePath.split('_').slice(-2); - model.arrays[id] = new macro.TYPED_ARRAYS[type](arraybuffer); + model.arrays[id] = macro.newTypedArray(type, arraybuffer); if (!processing) { resolve(); } diff --git a/Sources/Imaging/Core/ImageReslice/index.js b/Sources/Imaging/Core/ImageReslice/index.js index 4fd2dfe717e..88f3343afa9 100644 --- a/Sources/Imaging/Core/ImageReslice/index.js +++ b/Sources/Imaging/Core/ImageReslice/index.js @@ -20,7 +20,7 @@ import Constants from 'vtk.js/Sources/Imaging/Core/ImageReslice/Constants'; const { SlabMode } = Constants; -const { TYPED_ARRAYS, capitalize, vtkErrorMacro } = macro; +const { capitalize, vtkErrorMacro } = macro; // ---------------------------------------------------------------------------- // vtkImageReslice methods @@ -235,7 +235,8 @@ function vtkImageReslice(publicAPI, model) { .getScalars() .getNumberOfComponents(); // or s.numberOfComponents; - const outScalarsData = new TYPED_ARRAYS[dataType]( + const outScalarsData = macro.newTypedArray( + dataType, outDims[0] * outDims[1] * outDims[2] * numComponents ); const outScalars = vtkDataArray.newInstance({ @@ -387,7 +388,10 @@ function vtkImageReslice(publicAPI, model) { floatPtr = new Float64Array(inComponents * (outExt[1] - outExt[0])); } - const background = new TYPED_ARRAYS[inputScalarType](model.backgroundColor); + const background = macro.newTypedArray( + inputScalarType, + model.backgroundColor + ); // set color for area outside of input volume extent // void *background; @@ -427,7 +431,8 @@ function vtkImageReslice(publicAPI, model) { iter.initialize(output, outExt, model.stencil, null); const outPtr0 = iter.getScalars(output, 0); let outPtrIndex = 0; - const outTmp = new TYPED_ARRAYS[scalarType]( + const outTmp = macro.newTypedArray( + scalarType, vtkBoundingBox.getDiagonalLength(outExt) * outComponents * 2 ); diff --git a/Sources/Imaging/Hybrid/SampleFunction/index.js b/Sources/Imaging/Hybrid/SampleFunction/index.js index 85cbb1b0f8f..4771d5efa56 100644 --- a/Sources/Imaging/Hybrid/SampleFunction/index.js +++ b/Sources/Imaging/Hybrid/SampleFunction/index.js @@ -58,7 +58,7 @@ function vtkSampleFunction(publicAPI, model) { volume.setSpacing(spacing); // Create scalars array - const s = new window[model.pointType](numScalars); + const s = macro.newTypedArray(model.pointType, numScalars); const scalars = vtkDataArray.newInstance({ name: 'Scalars', values: s, diff --git a/Sources/Rendering/Core/CubeAxesActor/index.js b/Sources/Rendering/Core/CubeAxesActor/index.js index e51ba3eeaab..aa355419d18 100644 --- a/Sources/Rendering/Core/CubeAxesActor/index.js +++ b/Sources/Rendering/Core/CubeAxesActor/index.js @@ -75,10 +75,6 @@ function vtkCubeAxesActor(publicAPI, model) { // Set our className model.classHierarchy.push('vtkCubeAxesActor'); - // map used to store the string to texture values - // done as a const to not prevent serialization - const tmAtlas = new Map(); - publicAPI.setCamera = (cam) => { if (model.camera === cam) { return; @@ -404,11 +400,11 @@ function vtkCubeAxesActor(publicAPI, model) { model.tmContext.textAlign = 'left'; // first the three labels - tmAtlas.clear(); + model._tmAtlas.clear(); let maxWidth = 0; let totalHeight = 1; // start one pixel in so we have a border for (let i = 0; i < 3; i++) { - if (!tmAtlas.has(model.axisLabels[i])) { + if (!model._tmAtlas.has(model.axisLabels[i])) { applyTextStyle(model.tmContext, model.axisTextStyle); const metrics = model.tmContext.measureText(model.axisLabels[i]); const entry = { @@ -417,7 +413,7 @@ function vtkCubeAxesActor(publicAPI, model) { width: metrics.width + 2, textStyle: model.axisTextStyle, }; - tmAtlas.set(model.axisLabels[i], entry); + model._tmAtlas.set(model.axisLabels[i], entry); totalHeight += entry.height; if (maxWidth < entry.width) { maxWidth = entry.width; @@ -426,7 +422,7 @@ function vtkCubeAxesActor(publicAPI, model) { // and the ticks applyTextStyle(model.tmContext, model.tickTextStyle); for (let t = 0; t < tickStrings[i].length; t++) { - if (!tmAtlas.has(tickStrings[i][t])) { + if (!model._tmAtlas.has(tickStrings[i][t])) { const metrics = model.tmContext.measureText(tickStrings[i][t]); const entry = { height: metrics.actualBoundingBoxAscent + 2, @@ -434,7 +430,7 @@ function vtkCubeAxesActor(publicAPI, model) { width: metrics.width + 2, textStyle: model.tickTextStyle, }; - tmAtlas.set(tickStrings[i][t], entry); + model._tmAtlas.set(tickStrings[i][t], entry); totalHeight += entry.height; if (maxWidth < entry.width) { maxWidth = entry.width; @@ -449,7 +445,7 @@ function vtkCubeAxesActor(publicAPI, model) { totalHeight = vtkMath.nearestPowerOfTwo(totalHeight); // set the tcoord values - tmAtlas.forEach((value) => { + model._tmAtlas.forEach((value) => { value.tcoords = [ 0.0, (totalHeight - value.startingHeight - value.height) / totalHeight, @@ -470,7 +466,7 @@ function vtkCubeAxesActor(publicAPI, model) { model.tmContext.clearRect(0, 0, maxWidth, totalHeight); // draw the text onto the texture - tmAtlas.forEach((value, key) => { + model._tmAtlas.forEach((value, key) => { applyTextStyle(model.tmContext, value.textStyle); model.tmContext.fillText(key, 1, value.startingHeight + value.height - 1); }); @@ -491,7 +487,7 @@ function vtkCubeAxesActor(publicAPI, model) { offset, results ) => { - const value = tmAtlas.get(text); + const value = model._tmAtlas.get(text); if (!value) { return; } @@ -681,6 +677,12 @@ function vtkCubeAxesActor(publicAPI, model) { publicAPI.update(); }); + publicAPI.setVisibility = macro.chain( + publicAPI.setVisibility, + model.pixelActor.setVisibility, + model.tmActor.setVisibility + ); + publicAPI.setTickTextStyle = (tickStyle) => { model.tickTextStyle = { ...model.tickTextStyle, ...tickStyle }; publicAPI.modified(); @@ -738,6 +740,8 @@ export function extend(publicAPI, model, initialValues = {}) { model.textValues = []; model.lastTickBounds = []; + model._tmAtlas = new Map(); + model.mapper = vtkMapper.newInstance(); model.polyData = vtkPolyData.newInstance(); model.mapper.setInputData(model.polyData); diff --git a/Sources/Rendering/Core/ScalarBarActor/index.js b/Sources/Rendering/Core/ScalarBarActor/index.js index d73a1b10f55..eda9e16dc2e 100644 --- a/Sources/Rendering/Core/ScalarBarActor/index.js +++ b/Sources/Rendering/Core/ScalarBarActor/index.js @@ -38,10 +38,6 @@ function vtkScalarBarActor(publicAPI, model) { // Set our className model.classHierarchy.push('vtkScalarBarActor'); - // map used to store the string to texture values - // done as a const to not prevent serialization - let tmAtlas = new Map(); - // compute good values to use based on window size etc // a bunch of heuristics here with hand tuned constants // These values worked for me but really this method @@ -187,8 +183,8 @@ function vtkScalarBarActor(publicAPI, model) { if (model.nextImage && model.nextImage.complete) { model.tmTexture.setImage(model.nextImage); model.nextImage = null; - tmAtlas = model.nextAtlas; - model.nextAtlas = null; + model._tmAtlas = model._nextAtlas; + model._nextAtlas = null; if (doUpdate) { model.forceViewUpdate = true; publicAPI.update(); @@ -288,7 +284,7 @@ function vtkScalarBarActor(publicAPI, model) { const image = new Image(); image.src = model.tmCanvas.toDataURL('image/png'); model.nextImage = image; - model.nextAtlas = newTmAtlas; + model._nextAtlas = newTmAtlas; if (image.complete) { publicAPI.completedImage(false); } else { @@ -427,7 +423,7 @@ function vtkScalarBarActor(publicAPI, model) { offset, results ) => { - const value = tmAtlas.get(text); + const value = model._tmAtlas.get(text); if (!value) { return; } @@ -756,15 +752,11 @@ function vtkScalarBarActor(publicAPI, model) { publicAPI.modified(); }; - publicAPI.setVisibility = (val) => { - if (model.visibility === val) { - return; - } - model.visibility = val; - model.barActor.setVisibility(val); - model.tmActor.setVisibility(val); - publicAPI.modified(); - }; + publicAPI.setVisibility = macro.chain( + publicAPI.setVisibility, + model.barActor.setVisibility, + model.tmActor.setVisibility + ); } // ---------------------------------------------------------------------------- @@ -809,6 +801,8 @@ export function extend(publicAPI, model, initialValues = {}) { publicAPI.getProperty().setDiffuse(0.0); publicAPI.getProperty().setAmbient(1.0); + model._tmAtlas = new Map(); + // internal variables model.lastSize = [800, 800]; model.lastAspectRatio = 1.0; diff --git a/Sources/Rendering/Misc/SynchronizableRenderWindow/index.js b/Sources/Rendering/Misc/SynchronizableRenderWindow/index.js index 955992d2e03..8a01d30aa05 100644 --- a/Sources/Rendering/Misc/SynchronizableRenderWindow/index.js +++ b/Sources/Rendering/Misc/SynchronizableRenderWindow/index.js @@ -44,7 +44,7 @@ function createArrayHandler() { if (buffer instanceof Blob) { const fileReader = new FileReader(); fileReader.onload = () => { - const array = new window[dataType](fileReader.result); + const array = macro.newTypedArray(dataType, fileReader.result); const mtimes = { [context.getActiveViewId()]: context.getMTime(), }; @@ -53,7 +53,7 @@ function createArrayHandler() { }; fileReader.readAsArrayBuffer(buffer); } else { - const array = new window[dataType](buffer); + const array = macro.newTypedArray(dataType, buffer); const mtimes = { [context.getActiveViewId()]: context.getMTime() }; dataArrayCache[sha] = { mtimes, array }; resolve(array); diff --git a/Sources/Rendering/OpenGL/RenderWindow/index.js b/Sources/Rendering/OpenGL/RenderWindow/index.js index 9836679c64e..5e58d951ee2 100644 --- a/Sources/Rendering/OpenGL/RenderWindow/index.js +++ b/Sources/Rendering/OpenGL/RenderWindow/index.js @@ -486,7 +486,7 @@ function vtkOpenGLRenderWindow(publicAPI, model) { publicAPI.activateTexture = (texture) => { // Only add if it isn't already there - const result = model.textureResourceIds.get(texture); + const result = model._textureResourceIds.get(texture); if (result !== undefined) { model.context.activeTexture(model.context.TEXTURE0 + result); return; @@ -500,21 +500,21 @@ function vtkOpenGLRenderWindow(publicAPI, model) { return; } - model.textureResourceIds.set(texture, activeUnit); + model._textureResourceIds.set(texture, activeUnit); model.context.activeTexture(model.context.TEXTURE0 + activeUnit); }; publicAPI.deactivateTexture = (texture) => { // Only deactivate if it isn't already there - const result = model.textureResourceIds.get(texture); + const result = model._textureResourceIds.get(texture); if (result !== undefined) { publicAPI.getTextureUnitManager().free(result); - delete model.textureResourceIds.delete(texture); + delete model._textureResourceIds.delete(texture); } }; publicAPI.getTextureUnitForTexture = (texture) => { - const result = model.textureResourceIds.get(texture); + const result = model._textureResourceIds.get(texture); if (result !== undefined) { return result; } @@ -1173,7 +1173,7 @@ export function extend(publicAPI, model, initialValues = {}) { model.bgImage.style.height = '100%'; model.bgImage.style.zIndex = '-1'; - model.textureResourceIds = new Map(); + model._textureResourceIds = new Map(); // Inheritance vtkViewNode.extend(publicAPI, model, initialValues); diff --git a/Sources/Rendering/OpenGL/Texture/index.js b/Sources/Rendering/OpenGL/Texture/index.js index 44df7d32b12..69494c29dc6 100644 --- a/Sources/Rendering/OpenGL/Texture/index.js +++ b/Sources/Rendering/OpenGL/Texture/index.js @@ -753,7 +753,8 @@ function vtkOpenGLTexture(publicAPI, model) { widthLevel /= 2; heightLevel /= 2; } - invertedData[i] = new window[dataType]( + invertedData[i] = macro.newTypedArray( + dataType, heightLevel * widthLevel * model.components ); for (let y = 0; y < heightLevel; ++y) { diff --git a/Sources/Rendering/SceneGraph/ViewNode/index.js b/Sources/Rendering/SceneGraph/ViewNode/index.js index 1888e7c967a..7e197495b49 100644 --- a/Sources/Rendering/SceneGraph/ViewNode/index.js +++ b/Sources/Rendering/SceneGraph/ViewNode/index.js @@ -74,7 +74,7 @@ function vtkViewNode(publicAPI, model) { if (!dobj) { return; } - const result = model.renderableChildMap.get(dobj); + const result = model._renderableChildMap.get(dobj); // if found just mark as visited if (result !== undefined) { result.setVisited(true); @@ -84,7 +84,7 @@ function vtkViewNode(publicAPI, model) { if (newNode) { newNode.setParent(publicAPI); newNode.setVisited(true); - model.renderableChildMap.set(dobj, newNode); + model._renderableChildMap.set(dobj, newNode); model.children.push(newNode); } } @@ -97,7 +97,7 @@ function vtkViewNode(publicAPI, model) { for (let index = 0; index < dataObjs.length; ++index) { const dobj = dataObjs[index]; - const result = model.renderableChildMap.get(dobj); + const result = model._renderableChildMap.get(dobj); // if found just mark as visited if (result !== undefined) { result.setVisited(true); @@ -107,7 +107,7 @@ function vtkViewNode(publicAPI, model) { if (newNode) { newNode.setParent(publicAPI); newNode.setVisited(true); - model.renderableChildMap.set(dobj, newNode); + model._renderableChildMap.set(dobj, newNode); model.children.push(newNode); } } @@ -132,7 +132,7 @@ function vtkViewNode(publicAPI, model) { if (!visited) { const renderable = child.getRenderable(); if (renderable) { - model.renderableChildMap.delete(renderable); + model._renderableChildMap.delete(renderable); } if (!deleted) { deleted = []; @@ -183,7 +183,7 @@ function extend(publicAPI, model, initialValues = {}) { macro.obj(publicAPI, model); macro.event(publicAPI, model, 'event'); - model.renderableChildMap = new Map(); + model._renderableChildMap = new Map(); macro.get(publicAPI, model, ['visited']); macro.setGet(publicAPI, model, ['parent', 'renderable', 'myFactory']); diff --git a/Sources/Rendering/WebGPU/BufferManager/index.js b/Sources/Rendering/WebGPU/BufferManager/index.js index c667abaea95..15b67d7de41 100644 --- a/Sources/Rendering/WebGPU/BufferManager/index.js +++ b/Sources/Rendering/WebGPU/BufferManager/index.js @@ -13,12 +13,6 @@ const { vtkDebugMacro } = macro; // the webgpu constants all show up as undefined /* eslint-disable no-undef */ -// const { ObjectType } = Constants; - -// ---------------------------------------------------------------------------- -// Global methods -// ---------------------------------------------------------------------------- - // ---------------------------------------------------------------------------- // Static API // ---------------------------------------------------------------------------- @@ -205,7 +199,8 @@ function packArray( let vboidx = 0; const numComp = inArray.getNumberOfComponents(); - const packedVBO = new window[outputType]( + const packedVBO = macro.newTypedArray( + outputType, caboCount * (numComp + (packExtra ? 1 : 0)) ); diff --git a/Sources/Rendering/WebGPU/ShaderCache/index.js b/Sources/Rendering/WebGPU/ShaderCache/index.js index 9612ec7bbeb..eafa947d818 100644 --- a/Sources/Rendering/WebGPU/ShaderCache/index.js +++ b/Sources/Rendering/WebGPU/ShaderCache/index.js @@ -31,11 +31,11 @@ function vtkWebGPUShaderCache(publicAPI, model) { // has it already been created? const sType = shaderDesc.getType(); const sHash = shaderDesc.getHash(); - const keys = model.shaderModules.keys(); + const keys = model._shaderModules.keys(); for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (key.getHash() === sHash && key.getType() === sType) { - return model.shaderModules.get(key); + return model._shaderModules.get(key); } } @@ -43,7 +43,7 @@ function vtkWebGPUShaderCache(publicAPI, model) { const sm = vtkWebGPUShaderModule.newInstance(); sm.initialize(model.device, shaderDesc); - model.shaderModules.set(shaderDesc, sm); + model._shaderModules.set(shaderDesc, sm); return sm; }; } @@ -64,7 +64,7 @@ export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues); // Internal objects - model.shaderModules = new Map(); + model._shaderModules = new Map(); // Build VTK API macro.obj(publicAPI, model); diff --git a/Sources/Rendering/WebGPU/Types/index.js b/Sources/Rendering/WebGPU/Types/index.js index 22716124d7a..c58b8be65b6 100644 --- a/Sources/Rendering/WebGPU/Types/index.js +++ b/Sources/Rendering/WebGPU/Types/index.js @@ -1,3 +1,5 @@ +import { vtkErrorMacro } from 'vtk.js/Sources/macro'; + // ---------------------------------------------------------------------------- // vtkWebGPUDevice static functions // @@ -10,34 +12,59 @@ // - texture types such as rgba32float // ---------------------------------------------------------------------------- +// see https://gpuweb.github.io/gpuweb/#enumdef-gpuvertexformat +// for possible formats function getByteStrideFromBufferFormat(format) { - if (!format) return 0; + if (!format || format.length < 5) return 0; + + // options are x2, x3, x4 or nothing let numComp = 1; - if (format.substring(format.length - 2) === 'x4') numComp = 4; - if (format.substring(format.length - 2) === 'x3') numComp = 3; - if (format.substring(format.length - 2) === 'x2') numComp = 2; + if (format[format.length - 2] === 'x') { + numComp = format[format.length - 1]; + } - let typeSize = 4; - if (numComp > 1) { - if (format.substring(format.length - 3, format.length - 1) === '8x') { - typeSize = 1; - } - if (format.substring(format.length - 4, format.length - 1) === '16x') { - typeSize = 2; - } - } else { - if (format.substring(format.length - 1) === '8') typeSize = 1; - if (format.substring(format.length - 2) === '16') typeSize = 2; + const sizeStart = numComp === 1 ? format.length - 1 : format.length - 3; + // options are 8, 16, 32 resulting in 8, 6, 2 as the last char + // plugged into the formula below gives 1, 2, 4 respectively + const num = Number(format[sizeStart]); + if (Number.isNaN(num)) { + vtkErrorMacro(`unknown format ${format}`); + return 0; } + const typeSize = 5 - num / 2; return numComp * typeSize; } +// see https://gpuweb.github.io/gpuweb/#enumdef-gpuvertexformat +// for possible formats function getNativeTypeFromBufferFormat(format) { - if (!format) return null; - if (format.substring(0, 7) === 'float32') return 'Float32Array'; - if (format.substring(0, 6) === 'snorm8') return 'Int8Array'; - if (format.substring(0, 6) === 'unorm8') return 'Uint8Array'; - return ''; + if (!format || format.length < 5) return 0; + + // raw types are Uint Int or Float as follows + let result; + if (format[0] === 'f') { + result = 'Float'; + } else if (format[0] === 's') { + result = 'Int'; + } else if (format[0] === 'u') { + result = 'Uint'; + } else { + vtkErrorMacro(`unknown format ${format}`); + return undefined; + } + + // options are 8, 16, 32 resulting in 8, 6, 2 as the last char + // plugged into the formula below gives 1, 2, 4 respectively + const base = format.split('x')[0]; + const num = Number(base[base.length - 1]); + if (Number.isNaN(num)) { + vtkErrorMacro(`unknown format ${format}`); + return undefined; + } + result += 8 * (5 - num / 2); + result += 'Array'; + + return result; } function getByteStrideFromShaderFormat(format) { @@ -55,15 +82,12 @@ function getByteStrideFromShaderFormat(format) { } function getNativeTypeFromShaderFormat(format) { - if (!format) return null; - let type = format.substring(format.length - 3, format.length); - if (format[format.length - 1] === '>') { - type = format.substring(format.length - 4, format.length - 1); - } - if (type === 'f32') return 'Float32Array'; - if (type === 'i32') return 'Int32Array'; - if (type === 'u32') return 'Uint32Array'; - return ''; + if (!format) return undefined; + if (format.includes('f32')) return 'Float32Array'; + if (format.includes('i32')) return 'Int32Array'; + if (format.includes('u32')) return 'Uint32Array'; + vtkErrorMacro(`unknown format ${format}`); + return undefined; } export default { diff --git a/Sources/Rendering/WebGPU/UniformBuffer/index.js b/Sources/Rendering/WebGPU/UniformBuffer/index.js index a7a6d2df195..478bde20d32 100644 --- a/Sources/Rendering/WebGPU/UniformBuffer/index.js +++ b/Sources/Rendering/WebGPU/UniformBuffer/index.js @@ -15,12 +15,12 @@ function vtkWebGPUUniformBuffer(publicAPI, model) { model.classHierarchy.push('vtkWebGPUUniformBuffer'); publicAPI.addEntry = (name, type) => { - if (model.bufferEntryNames.has(name)) { + if (model._bufferEntryNames.has(name)) { vtkErrorMacro(`entry named ${name} already exists`); return; } model.sortDirty = true; - model.bufferEntryNames.set(name, model.bufferEntries.length); + model._bufferEntryNames.set(name, model.bufferEntries.length); model.bufferEntries.push({ name, type, @@ -162,9 +162,9 @@ function vtkWebGPUUniformBuffer(publicAPI, model) { // update entries and entryNames model.bufferEntries = newEntries; - model.bufferEntryNames.clear(); + model._bufferEntryNames.clear(); for (let i = 0; i < model.bufferEntries.length; i++) { - model.bufferEntryNames.set(model.bufferEntries[i].name, i); + model._bufferEntryNames.set(model.bufferEntries[i].name, i); } model.sizeInBytes = currOffset; model.sortDirty = false; @@ -214,13 +214,13 @@ function vtkWebGPUUniformBuffer(publicAPI, model) { if (!model.arrayBuffer) { model.arrayBuffer = new ArrayBuffer(model.sizeInBytes); } - model[type] = new window[type](model.arrayBuffer); + model[type] = macro.newTypedArray(type, model.arrayBuffer); } }; publicAPI.setValue = (name, val) => { publicAPI.sortBufferEntries(); - const idx = model.bufferEntryNames.get(name); + const idx = model._bufferEntryNames.get(name); if (idx === undefined) { vtkErrorMacro(`entry named ${name} not found in UBO`); return; @@ -236,7 +236,7 @@ function vtkWebGPUUniformBuffer(publicAPI, model) { publicAPI.setArray = (name, arr) => { publicAPI.sortBufferEntries(); - const idx = model.bufferEntryNames.get(name); + const idx = model._bufferEntryNames.get(name); if (idx === undefined) { vtkErrorMacro(`entry named ${name} not found in UBO`); return; @@ -254,7 +254,7 @@ function vtkWebGPUUniformBuffer(publicAPI, model) { publicAPI.getNativeView = (name) => { publicAPI.sortBufferEntries(); - const idx = model.bufferEntryNames.get(name); + const idx = model._bufferEntryNames.get(name); if (idx === undefined) { vtkErrorMacro(`entry named ${name} not found in UBO`); return undefined; @@ -263,7 +263,8 @@ function vtkWebGPUUniformBuffer(publicAPI, model) { publicAPI.createView(entry.nativeType); const bpe = model[entry.nativeType].BYTES_PER_ELEMENT; model.sendDirty = true; - return new window[entry.nativeType]( + return macro.newTypedArray( + entry.nativeType, model.arrayBuffer, entry.offset, entry.sizeInBytes / bpe @@ -275,13 +276,15 @@ function vtkWebGPUUniformBuffer(publicAPI, model) { // sort the entries publicAPI.sortBufferEntries(); - let result = `[[block]] struct ${model.name}Struct\n{\n`; + const lines = [`[[block]] struct ${model.name}Struct\n{`]; for (let i = 0; i < model.bufferEntries.length; i++) { const entry = model.bufferEntries[i]; - result += `[[offset(${entry.offset})]] ${entry.name}: ${entry.type};\n`; + lines.push(`[[offset(${entry.offset})]] ${entry.name}: ${entry.type};`); } - result += `};\n[[binding(${model.binding}), group(${model.group})]] var ${model.name}: ${model.name}Struct;`; - return result; + lines.push( + `};\n[[binding(${model.binding}), group(${model.group})]] var ${model.name}: ${model.name}Struct;` + ); + return lines.join('\n'); }; } @@ -307,7 +310,7 @@ export function extend(publicAPI, model, initialValues = {}) { macro.obj(publicAPI, model); // Internal objects - model.bufferEntryNames = new Map(); + model._bufferEntryNames = new Map(); model.bufferEntries = []; model.sendTime = {}; diff --git a/Sources/macro.js b/Sources/macro.js index 71c89169ee8..222f3b9ab74 100644 --- a/Sources/macro.js +++ b/Sources/macro.js @@ -80,16 +80,26 @@ export function vtkOnceErrorMacro(str) { // TypedArray // ---------------------------------------------------------------------------- -export const TYPED_ARRAYS = { - Float32Array, - Float64Array, - Uint8Array, - Int8Array, - Uint16Array, - Int16Array, - Uint32Array, - Int32Array, -}; +export const TYPED_ARRAYS = Object.create(null); +TYPED_ARRAYS.Float32Array = Float32Array; +TYPED_ARRAYS.Float64Array = Float64Array; +TYPED_ARRAYS.Uint8Array = Uint8Array; +TYPED_ARRAYS.Int8Array = Int8Array; +TYPED_ARRAYS.Uint16Array = Uint16Array; +TYPED_ARRAYS.Int16Array = Int16Array; +TYPED_ARRAYS.Uint32Array = Uint32Array; +TYPED_ARRAYS.Int32Array = Int32Array; +TYPED_ARRAYS.Uint8ClampedArray = Uint8ClampedArray; +// TYPED_ARRAYS.BigInt64Array = BigInt64Array; +// TYPED_ARRAYS.BigUint64Array = BigUint64Array; + +export function newTypedArray(type, ...args) { + return new (TYPED_ARRAYS[type] || Float64Array)(...args); +} + +export function newTypedArrayFrom(type, ...args) { + return (TYPED_ARRAYS[type] || Float64Array).from(...args); +} // ---------------------------------------------------------------------------- // capitilze provided string @@ -312,7 +322,11 @@ export function obj(publicAPI = {}, model = {}) { // Convert every vtkObject to its serializable form Object.keys(jsonArchive).forEach((keyName) => { - if (jsonArchive[keyName] === null || jsonArchive[keyName] === undefined) { + if ( + jsonArchive[keyName] === null || + jsonArchive[keyName] === undefined || + keyName[0] === '_' // protected members start with _ + ) { delete jsonArchive[keyName]; } else if (jsonArchive[keyName].isA) { jsonArchive[keyName] = jsonArchive[keyName].getState(); @@ -1622,6 +1636,8 @@ export default { isVtkObject, keystore, newInstance, + newTypedArray, + newTypedArrayFrom, normalizeWheel, obj, proxy, @@ -1636,7 +1652,7 @@ export default { setLoggerFunction, throttle, traverseInstanceTree, - TYPED_ARRAYS, + TYPED_ARRAYS, // deprecated todo remove on breaking API revision uncapitalize, VOID, vtkDebugMacro,