Skip to content

Commit 5ec65e9

Browse files
committed
fix(ImageDataOutlineFilter): refactor with vtkCubeSource
Refactor vtkImageDataOutlineFilter and vtkCubeSource to move generation of line cells to vtkCubeSource. Also add a matrix parameter to cubesource that will be applied after rotations and center.
1 parent edc49ec commit 5ec65e9

File tree

5 files changed

+158
-145
lines changed

5 files changed

+158
-145
lines changed

Sources/Filters/General/ImageDataOutlineFilter/index.d.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,30 @@ export interface vtkImageDataOutlineFilter extends vtkImageDataOutlineFilterBase
1717
*/
1818
requestData(inData: vtkImageData, outData: vtkPolyData): void;
1919

20-
/**
21-
* Flag to indicate that the output should generate triangulated faces of the outline.
22-
* @param {boolean} generateFaces
23-
*/
24-
setGenerateFaces(generateFaces: boolean): boolean;
25-
26-
/**
27-
* Flag that indicates whether the output will generate triangulated faces of the outline.
28-
* @returns {boolean}
29-
*/
20+
/**
21+
* Flag that indicates whether the output will generate faces of the outline.
22+
* @returns {boolean}
23+
*/
3024
getGenerateFaces(): boolean;
3125

32-
/**
26+
/**
27+
* Flag that indicates whether the output will generate wireframe lines of the outline.
28+
* @returns {boolean}
29+
*/
30+
getGenerateLines(): boolean;
31+
32+
/**
3333
* Flag to indicate that the output should generate wireframe of the outline.
34-
* @param {boolean} generateLines
35-
*/
34+
* @param {boolean} generateLines
35+
*/
3636
setGenerateLines(generateLines: boolean): boolean;
3737

38-
/**
39-
* Flag that indicates whether the output will generate wireframe lines of the outline.
40-
* @returns {boolean}
41-
*/
42-
getGenerateLines(): boolean;
38+
/**
39+
* Flag to indicate that the output should generate triangulated faces of the outline.
40+
* @param {boolean} generateFaces
41+
*/
42+
setGenerateFaces(generateFaces: boolean): boolean;
43+
4344
}
4445

4546
/**

Sources/Filters/General/ImageDataOutlineFilter/index.js

Lines changed: 25 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,8 @@
11
import macro from 'vtk.js/Sources/macros';
2-
import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox';
3-
import vtkCellArray from 'vtk.js/Sources/Common/Core/CellArray';
42
import vtkCubeSource from 'vtk.js/Sources/Filters/Sources/CubeSource';
5-
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
6-
import vtkTransform from 'vtk.js/Sources/Common/Transform/Transform';
73

84
const { vtkErrorMacro } = macro;
95

10-
// prettier-ignore
11-
export const LINE_ARRAY = [
12-
2, 0, 1,
13-
2, 2, 3,
14-
2, 4, 5,
15-
2, 6, 7,
16-
2, 0, 2,
17-
2, 1, 3,
18-
2, 4, 6,
19-
2, 5, 7,
20-
2, 0, 4,
21-
2, 1, 5,
22-
2, 2, 6,
23-
2, 3, 7,
24-
];
25-
266
// ----------------------------------------------------------------------------
277
// vtkImageDataOutlineFilter methods
288
// ----------------------------------------------------------------------------
@@ -40,62 +20,45 @@ function vtkImageDataOutlineFilter(publicAPI, model) {
4020
return;
4121
}
4222

43-
if (!model._tmpOut) {
44-
// allocate output object if not done previously.
45-
model._tmpOut = vtkPolyData.newInstance();
46-
}
47-
4823
// First create a cube polydata in the index-space of the image.
24+
// The benefit of using `getSpatialExtent` call is that it automatically
25+
// takes care of 0.5 voxel padding as required by an vtkImageData representation.
4926
const spatialExt = input.getSpatialExtent();
5027
if (!spatialExt) {
5128
vtkErrorMacro('Unable to fetch spatial extents of input image.');
5229
return;
5330
}
5431

55-
model._boundingBox.setBounds(spatialExt);
56-
const lengths = model._boundingBox.getLengths();
57-
model._cubeSource.setXLength(lengths[0]);
58-
model._cubeSource.setYLength(lengths[1]);
59-
model._cubeSource.setZLength(lengths[2]);
60-
model._cubeSource.setCenter(model._boundingBox.getCenter());
61-
model._cubeSource.update();
62-
63-
const out = model._cubeSource.getOutputData();
64-
model._tmpOut.getPoints().deepCopy(out.getPoints());
65-
66-
// Now, transform the cube polydata points in-place
67-
// using the image's indexToWorld transformation.
68-
model._transform.setMatrix(input.getIndexToWorld());
69-
model._transform.transformPoints(
70-
model._tmpOut.getPoints().getData(),
71-
model._tmpOut.getPoints().getData()
72-
);
73-
74-
// Lastly, generate the necessary cell arrays.
75-
if (model.generateFaces) {
76-
model._tmpOut.getPolys().deepCopy(out.getPolys());
77-
} else {
78-
model._tmpOut.getPolys().initialize();
79-
}
80-
if (model.generateLines) {
81-
model._tmpOut.getLines().deepCopy(model._lineCells);
82-
} else {
83-
model._tmpOut.getLines().initialize();
32+
model._cubeSource.setBounds(spatialExt);
33+
34+
// Then apply index-to-world transform to the cube to create the outline.
35+
model._cubeSource.setMatrix(input.getIndexToWorld());
36+
outData[0] = model._cubeSource.getOutputData();
37+
};
38+
39+
// Forward calls for [set/get]Generate[Faces/Lines] functions to cubeSource:
40+
publicAPI.setGenerateFaces = (generateFaces) => {
41+
if (model._cubeSource.setGenerateFaces(generateFaces)) {
42+
publicAPI.modified();
8443
}
44+
};
8545

86-
model._tmpOut.modified();
87-
outData[0] = model._tmpOut;
46+
publicAPI.setGenerateLines = (generateLines) => {
47+
if (model._cubeSource.setGenerateLines(generateLines)) {
48+
publicAPI.modified();
49+
}
8850
};
51+
52+
publicAPI.getGenerateFaces = model._cubeSource.getGenerateFaces;
53+
54+
publicAPI.getGenerateLines = model._cubeSource.getGenerateLines;
8955
}
9056

9157
// ----------------------------------------------------------------------------
9258
// Object factory
9359
// ----------------------------------------------------------------------------
9460

95-
const DEFAULT_VALUES = {
96-
generateFaces: false,
97-
generateLines: true,
98-
};
61+
const DEFAULT_VALUES = {};
9962

10063
// ----------------------------------------------------------------------------
10164

@@ -105,27 +68,13 @@ export function extend(publicAPI, model, initialValues = {}) {
10568
// Make this a VTK object
10669
macro.obj(publicAPI, model);
10770

108-
macro.setGet(publicAPI, model, ['generateFaces', 'generateLines']);
109-
11071
// Also make it an algorithm with one input and one output
11172
macro.algo(publicAPI, model, 1, 1);
11273

11374
// Internal persistent objects
114-
model._boundingBox = vtkBoundingBox.newInstance();
11575
model._cubeSource = vtkCubeSource.newInstance();
116-
model._tmpOut = vtkPolyData.newInstance();
117-
model._lineCells = vtkCellArray.newInstance({
118-
values: Uint16Array.from(LINE_ARRAY),
119-
});
120-
model._transform = vtkTransform.newInstance();
121-
122-
macro.moveToProtected(publicAPI, model, [
123-
'boundingBox',
124-
'cubeSource',
125-
'tmpOut',
126-
'lineCells',
127-
'transform',
128-
]);
76+
77+
macro.moveToProtected(publicAPI, model, ['cubeSource', 'tmpOut']);
12978

13079
// Object specific methods
13180
vtkImageDataOutlineFilter(publicAPI, model);

Sources/Filters/Sources/CubeSource/index.d.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { mat4 } from 'gl-matrix';
12
import { vtkAlgorithm, vtkObject } from "../../../interfaces";
23
import { Bounds, Vector3 } from "../../../types";
34

@@ -41,6 +42,24 @@ export interface vtkCubeSource extends vtkCubeSourceBase {
4142
*/
4243
getGenerate3DTextureCoordinates(): boolean;
4344

45+
/**
46+
* Flag that indicates whether the output will generate faces of the outline.
47+
* @returns {boolean}
48+
*/
49+
getGenerateFaces(): boolean;
50+
51+
/**
52+
* Flag that indicates whether the output will generate wireframe lines of the outline.
53+
* @returns {boolean}
54+
*/
55+
getGenerateLines(): boolean;
56+
57+
/**
58+
* Get the 4x4 transformation set to apply as a final trasformation to the output.
59+
* @param matrix
60+
*/
61+
getMatrix(): mat4;
62+
4463
/**
4564
*
4665
* @default [0.0, 0.0, 0.0]
@@ -117,6 +136,24 @@ export interface vtkCubeSource extends vtkCubeSourceBase {
117136
*/
118137
setGenerate3DTextureCoordinates(generate3DTextureCoordinates: boolean): boolean;
119138

139+
/**
140+
* Flag to indicate that the output should generate wireframe of the outline.
141+
* @param {boolean} generateLines
142+
*/
143+
setGenerateLines(generateLines: boolean): boolean;
144+
145+
/**
146+
* Flag to indicate that the output should generate triangulated faces of the outline.
147+
* @param {boolean} generateFaces
148+
*/
149+
setGenerateFaces(generateFaces: boolean): boolean;
150+
151+
/**
152+
* Set a 4x4 transformation that will be applied as a final trasformation to the output.
153+
* @param matrix
154+
*/
155+
setMatrix(matrix: mat4): boolean;
156+
120157
/**
121158
* Float array of size 3 representing the angles, in degrees, of rotation for the cube.
122159
* @param xAngle

0 commit comments

Comments
 (0)