Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix calculator for image data #2928

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions Sources/Filters/General/Calculator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -191,7 +194,7 @@ function vtkCalculator(publicAPI, model) {
[
FieldDataTypes.POINT,
(x) => x.getPointData(),
(x) => x.getPoints().getNumberOfPoints(),
(x) => x.getNumberOfPoints(),
],
[
FieldDataTypes.CELL,
Expand Down
40 changes: 40 additions & 0 deletions Sources/Filters/General/Calculator/test/testCalculator.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
bourdaisj marked this conversation as resolved.
Show resolved Hide resolved

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();
});