1
1
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' ;
4
2
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' ;
7
3
8
4
const { vtkErrorMacro } = macro ;
9
5
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
-
26
6
// ----------------------------------------------------------------------------
27
7
// vtkImageDataOutlineFilter methods
28
8
// ----------------------------------------------------------------------------
@@ -40,62 +20,45 @@ function vtkImageDataOutlineFilter(publicAPI, model) {
40
20
return ;
41
21
}
42
22
43
- if ( ! model . _tmpOut ) {
44
- // allocate output object if not done previously.
45
- model . _tmpOut = vtkPolyData . newInstance ( ) ;
46
- }
47
-
48
23
// 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.
49
26
const spatialExt = input . getSpatialExtent ( ) ;
50
27
if ( ! spatialExt ) {
51
28
vtkErrorMacro ( 'Unable to fetch spatial extents of input image.' ) ;
52
29
return ;
53
30
}
54
31
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 ( ) ;
84
43
}
44
+ } ;
85
45
86
- model . _tmpOut . modified ( ) ;
87
- outData [ 0 ] = model . _tmpOut ;
46
+ publicAPI . setGenerateLines = ( generateLines ) => {
47
+ if ( model . _cubeSource . setGenerateLines ( generateLines ) ) {
48
+ publicAPI . modified ( ) ;
49
+ }
88
50
} ;
51
+
52
+ publicAPI . getGenerateFaces = model . _cubeSource . getGenerateFaces ;
53
+
54
+ publicAPI . getGenerateLines = model . _cubeSource . getGenerateLines ;
89
55
}
90
56
91
57
// ----------------------------------------------------------------------------
92
58
// Object factory
93
59
// ----------------------------------------------------------------------------
94
60
95
- const DEFAULT_VALUES = {
96
- generateFaces : false ,
97
- generateLines : true ,
98
- } ;
61
+ const DEFAULT_VALUES = { } ;
99
62
100
63
// ----------------------------------------------------------------------------
101
64
@@ -105,27 +68,13 @@ export function extend(publicAPI, model, initialValues = {}) {
105
68
// Make this a VTK object
106
69
macro . obj ( publicAPI , model ) ;
107
70
108
- macro . setGet ( publicAPI , model , [ 'generateFaces' , 'generateLines' ] ) ;
109
-
110
71
// Also make it an algorithm with one input and one output
111
72
macro . algo ( publicAPI , model , 1 , 1 ) ;
112
73
113
74
// Internal persistent objects
114
- model . _boundingBox = vtkBoundingBox . newInstance ( ) ;
115
75
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' ] ) ;
129
78
130
79
// Object specific methods
131
80
vtkImageDataOutlineFilter ( publicAPI , model ) ;
0 commit comments