Skip to content

Commit

Permalink
chore(WebGPU): remove use of window
Browse files Browse the repository at this point in the history
Remove the use of window to create native arrays throughout vtk.js. Also
check for cases where a dataArray is created with a type of Array which
is invalid and promote to Float64Array instead.

Make the use of Map in models to use _ as a prefix
to indicate it is protected and not to be serailized.
  • Loading branch information
martinken committed Apr 13, 2021
1 parent 4c9bcee commit 848e770
Show file tree
Hide file tree
Showing 40 changed files with 206 additions and 154 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
4 changes: 2 additions & 2 deletions Sources/Common/Core/DataArray/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Common/Core/Points/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/Common/Core/ScalarsToColors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion Sources/Common/DataModel/Cell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Common/DataModel/Cell/test/testCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions Sources/Filters/Core/Cutter/index.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions Sources/Filters/General/TriangleFilter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
14 changes: 7 additions & 7 deletions Sources/Filters/Sources/Arrow2DSource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion Sources/Filters/Sources/CircleSource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion Sources/Filters/Sources/ConcentricCylinderSource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Filters/Sources/ConeSource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 7 additions & 4 deletions Sources/Filters/Sources/CubeSource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Filters/Sources/CylinderSource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Filters/Sources/LineSource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/Filters/Sources/PlaneSource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/Filters/Sources/PointSource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion Sources/Filters/Sources/SLICSource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Filters/Sources/SphereSource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions Sources/Filters/Sources/ViewFinderSource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
]);
Expand Down
2 changes: 1 addition & 1 deletion Sources/IO/Core/DataAccessHelper/HtmlDataAccessHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/IO/Core/ZipMultiDataSetReader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
13 changes: 9 additions & 4 deletions Sources/Imaging/Core/ImageReslice/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
);

Expand Down
2 changes: 1 addition & 1 deletion Sources/Imaging/Hybrid/SampleFunction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 848e770

Please sign in to comment.