From f0e1a263ac445ade89b67676cdddbb433caa73d7 Mon Sep 17 00:00:00 2001 From: Jules BOURDAIS Date: Fri, 29 Sep 2023 16:44:52 +0200 Subject: [PATCH 1/2] fix(calculator): make it work with vtkImageData input do not call getPoints on the input dataset if it not necessary fix #2257 --- Sources/Filters/General/Calculator/index.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Sources/Filters/General/Calculator/index.js b/Sources/Filters/General/Calculator/index.js index 74a67beae15..bcf96b456ae 100644 --- a/Sources/Filters/General/Calculator/index.js +++ b/Sources/Filters/General/Calculator/index.js @@ -45,11 +45,14 @@ function vtkCalculator(publicAPI, model) { singleValueFormula, options = {} ) => ({ - getArrays: () => ({ - input: publicAPI.augmentInputArrays( - locn, - arrNames.map((x) => ({ location: locn, name: x })) - ), + getArrays: (inData) => ({ + // don't augment input data array in case of structured input dataset + input: inData[0].isA('vtkImageData') + ? arrNames.map((x) => ({ location: locn, name: x })) + : publicAPI.augmentInputArrays( + locn, + arrNames.map((x) => ({ location: locn, name: x })) + ), output: [ { location: locn, @@ -191,7 +194,7 @@ function vtkCalculator(publicAPI, model) { [ FieldDataTypes.POINT, (x) => x.getPointData(), - (x) => x.getPoints().getNumberOfPoints(), + (x) => x.getNumberOfPoints(), ], [ FieldDataTypes.CELL, From 1c12d595fc9972594dec5f4e179ad16f13a02a10 Mon Sep 17 00:00:00 2001 From: Jules BOURDAIS Date: Fri, 29 Sep 2023 17:07:38 +0200 Subject: [PATCH 2/2] test(calculator): add a test to make sure vtkCalculator does not crash with a vtkImageData input --- .../General/Calculator/test/testCalculator.js | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Sources/Filters/General/Calculator/test/testCalculator.js b/Sources/Filters/General/Calculator/test/testCalculator.js index 210ca566154..e7569cf120c 100644 --- a/Sources/Filters/General/Calculator/test/testCalculator.js +++ b/Sources/Filters/General/Calculator/test/testCalculator.js @@ -1,6 +1,7 @@ import test from 'tape-catch'; import vtkCalculator from 'vtk.js/Sources/Filters/General/Calculator'; +import vtkImageGridSource from 'vtk.js/Sources/Filters/Sources/ImageGridSource'; import vtkPlaneSource from 'vtk.js/Sources/Filters/Sources/PlaneSource'; import { AttributeTypes } from 'vtk.js/Sources/Common/DataModel/DataSetAttributes/Constants'; import { FieldDataTypes } from 'vtk.js/Sources/Common/DataModel/DataSet/Constants'; @@ -93,3 +94,42 @@ test('Test vtkCalculator execution', (t) => { t.end(); }); + +test('make sure vtkCalculator does not crash with a vtkImageData input', (t) => { + const source = vtkImageGridSource.newInstance(); + const filter = vtkCalculator.newInstance(); + + filter.setInputConnection(source.getOutputPort()); + filter.setFormulaSimple(FieldDataTypes.POINT, ['scalars'], 'mask', (value) => + value > 10 ? 1 : 0 + ); + + source.update(); + filter.update(); + + const input = source.getOutputData(); + const output = filter.getOutputData(); + + t.ok(output, 'Output dataset exists'); + t.equal( + output.isA('vtkImageData'), + true, + 'The output dataset should be a vtkImagedata' + ); + t.equal( + input.getNumberOfPoints(), + output.getNumberOfPoints(), + `The number of points did not change between input ${input.getNumberOfPoints()} and output ${output.getNumberOfPoints()}` + ); + t.ok( + output.getPointData().getScalars(), + 'Output point-scalars array exists.' + ); + t.equal( + output.getPointData().getScalars().getName(), + 'mask', + 'Output point-scalars is "mask".' + ); + + t.end(); +});