]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Extensions/Visualization/ImageSliceActors.cxx
ROI interaction added
[cpPlugins.git] / lib / cpPlugins / Extensions / Visualization / ImageSliceActors.cxx
1 #include <cpPlugins/Extensions/Visualization/ImageSliceActors.h>
2
3 #include <vtkAlgorithmOutput.h>
4 #include <vtkCellArray.h>
5 #include <vtkInformation.h>
6 #include <vtkPlane.h>
7 #include <vtkPoints.h>
8 #include <vtkProperty.h>
9 #include <vtkStreamingDemandDrivenPipeline.h>
10 #include <vtkTextProperty.h>
11
12 // -------------------------------------------------------------------------
13 cpPlugins::Extensions::Visualization::ImageSliceActors*
14 cpPlugins::Extensions::Visualization::ImageSliceActors::
15 New( )
16 {
17   return( new Self( ) );
18 }
19
20 // -------------------------------------------------------------------------
21 void cpPlugins::Extensions::Visualization::ImageSliceActors::
22 SetInputConnection( vtkAlgorithmOutput* aout, int axis )
23 {
24   this->InputAlgorithm = aout;
25   this->SliceMapper->SetInputConnection( aout );
26   this->SliceMapper->SetOrientation( axis );
27   this->SliceMapper->Update( );
28   this->SetSliceNumber( this->SliceMapper->GetSliceNumber( ) );
29   this->ImageActor->SetMapper( this->SliceMapper );
30   this->ImageActor->Modified( );
31   this->Modified( );
32 }
33
34 // -------------------------------------------------------------------------
35 void cpPlugins::Extensions::Visualization::ImageSliceActors::
36 SetSegmentationConnection( vtkAlgorithmOutput* aout )
37 {
38   this->SegmentationAlgorithm = aout;
39   this->SegmentationSliceMapper->SetInputConnection( aout );
40   this->SegmentationSliceMapper->
41     SetOrientation( this->SliceMapper->GetOrientation( ) );
42   this->SegmentationSliceMapper->
43     SetSliceNumber( this->SliceMapper->GetSliceNumber( ) );
44   this->SegmentationActor->SetMapper( this->SegmentationSliceMapper );
45   this->SegmentationActor->Modified( );
46   this->Modified( );
47 }
48
49 // -------------------------------------------------------------------------
50 double* cpPlugins::Extensions::Visualization::ImageSliceActors::
51 GetDisplayBounds( ) const
52 {
53   return( this->ImageActor->GetDisplayBounds( ) );
54 }
55
56 // -------------------------------------------------------------------------
57 void cpPlugins::Extensions::Visualization::ImageSliceActors::
58 GetDisplayBounds( double bounds[ 6 ] ) const
59 {
60   this->ImageActor->GetDisplayBounds( bounds );
61 }
62
63 // -------------------------------------------------------------------------
64 int cpPlugins::Extensions::Visualization::ImageSliceActors::
65 GetAxis( ) const
66 {
67   return( this->SliceMapper->GetOrientation( ) );
68 }
69
70 // -------------------------------------------------------------------------
71 int cpPlugins::Extensions::Visualization::ImageSliceActors::
72 GetSliceNumber( ) const
73 {
74   return( this->SliceMapper->GetSliceNumber( ) );
75 }
76
77 // -------------------------------------------------------------------------
78 int cpPlugins::Extensions::Visualization::ImageSliceActors::
79 GetSliceNumberMinValue( ) const
80 {
81   return( this->SliceMapper->GetSliceNumberMinValue( ) );
82 }
83
84 // -------------------------------------------------------------------------
85 int cpPlugins::Extensions::Visualization::ImageSliceActors::
86 GetSliceNumberMaxValue( ) const
87 {
88   return( this->SliceMapper->GetSliceNumberMaxValue( ) );
89 }
90
91 // -------------------------------------------------------------------------
92 void cpPlugins::Extensions::Visualization::ImageSliceActors::
93 SetSliceNumber( const int& slice )
94 {
95   if( this->InputAlgorithm == NULL )
96     return;
97   this->SliceMapper->SetSliceNumber( slice );
98   this->SliceMapper->Update( );
99
100   if( this->SegmentationAlgorithm != NULL )
101   {
102     this->SegmentationSliceMapper->SetSliceNumber( slice );
103     this->SegmentationSliceMapper->Update( );
104
105   } // fi
106
107   // Compute plane
108   vtkAlgorithm* algo = this->SliceMapper->GetInputAlgorithm( );
109   vtkInformation* info = algo->GetOutputInformation( 0 );
110   int ext[ 6 ];
111   double ori[ 3 ], spac[ 3 ], pos[ 3 ];
112   info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
113   info->Get( vtkDataObject::ORIGIN( ), ori );
114   info->Get( vtkDataObject::SPACING( ), spac );
115   this->SliceMapper->GetSlicePlane( )->GetOrigin( pos );
116
117   // Prevent obscuring voxels by offsetting the plane geometry
118   double xbnds[ ] =
119     {
120       ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
121       ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
122     };
123   double ybnds[ ] =
124     {
125       ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
126       ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
127     };
128   double zbnds[ ] =
129     {
130       ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
131       ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
132     };
133
134   if( spac[ 0 ] < double( 0 ) )
135   {
136     double t = xbnds[ 0 ];
137     xbnds[ 0 ] = xbnds[ 1 ];
138     xbnds[ 1 ] = t;
139
140   } // fi
141   if( spac[ 1 ] < double( 0 ) )
142   {
143     double t = ybnds[ 0 ];
144     ybnds[ 0 ] = ybnds[ 1 ];
145     ybnds[ 1 ] = t;
146
147   } // fi
148   if( spac[ 2 ] < double( 0 ) )
149   {
150     double t = zbnds[ 0 ];
151     zbnds[ 0 ] = zbnds[ 1 ];
152     zbnds[ 1 ] = t;
153
154   } // fi
155
156   int axis = this->SliceMapper->GetOrientation( );
157   this->PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
158   this->PlaneActor->GetProperty( )->SetLineWidth( 2 );
159   vtkPoints* plane_points = this->PlaneSource->GetPoints( );
160   if( axis == 0 ) // YZ, x-normal
161   {
162     plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
163     plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
164     plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
165     plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
166     this->PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
167   }
168   else if( axis == 1 ) // ZX, y-normal
169   {
170     plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
171     plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
172     plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
173     plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
174     this->PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
175   }
176   else // XY, z-normal
177   {
178     plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
179     plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
180     plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
181     plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
182     this->PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
183
184   } // fi
185   this->PlaneSource->Modified( );
186   this->PlaneMapper->Modified( );
187   this->PlaneActor->Modified( );
188   this->Modified( );
189 }
190
191 // -------------------------------------------------------------------------
192 void cpPlugins::Extensions::Visualization::ImageSliceActors::
193 UpdateText( const double& w, const double& l )
194 {
195   char axis;
196   int axId = this->SliceMapper->GetOrientation( );
197   if     ( axId == 0 ) axis = 'X';
198   else if( axId == 1 ) axis = 'Y';
199   else if( axId == 2 ) axis = 'Z';
200
201   std::sprintf(
202     this->TextBuffer, "Axis: %c (%d)\nWin/Lev: %.2f/%.2f",
203     axis, this->SliceMapper->GetSliceNumber( ), w, l
204     );
205   this->TextActor->SetInput( this->TextBuffer );
206   this->TextActor->Modified( );
207   this->Modified( );
208 }
209
210 // -------------------------------------------------------------------------
211 cpPlugins::Extensions::Visualization::ImageSliceActors::
212 ImageSliceActors( )
213   : Superclass( ),
214     InputAlgorithm( NULL ),
215     SegmentationAlgorithm( NULL )
216 {
217   this->SliceMapper = vtkSmartPointer< vtkImageSliceMapper >::New( );
218   this->SegmentationSliceMapper =
219     vtkSmartPointer< vtkImageSliceMapper >::New( );
220   this->PlaneSource = vtkSmartPointer< vtkPolyData >::New( );
221   this->PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
222   this->ImageActor = vtkSmartPointer< vtkImageActor >::New( );
223   this->SegmentationActor = vtkSmartPointer< vtkImageActor >::New( );
224   this->TextActor = vtkSmartPointer< vtkTextActor >::New( );
225   this->PlaneActor = vtkSmartPointer< vtkActor >::New( );
226
227   this->ImageActorIndex = this->GetNumberOfItems( );
228   this->SegmentationActorIndex = this->ImageActorIndex + 1;
229   this->TextActorIndex = this->ImageActorIndex + 2;
230   this->PlaneActorIndex = this->ImageActorIndex + 3;
231   this->AddItem( this->ImageActor );
232   this->AddItem( this->SegmentationActor );
233   this->AddItem( this->TextActor );
234   this->AddItem( this->PlaneActor );
235
236   // Configuration
237   vtkSmartPointer< vtkPoints > plane_points =
238     vtkSmartPointer< vtkPoints >::New( );
239   vtkSmartPointer< vtkCellArray > plane_lines =
240     vtkSmartPointer< vtkCellArray >::New( );
241
242   plane_points->InsertNextPoint( 0, 0, 0 );
243   plane_points->InsertNextPoint( 0, 1, 0 );
244   plane_points->InsertNextPoint( 1, 1, 0 );
245   plane_points->InsertNextPoint( 1, 0, 0 );
246   plane_lines->InsertNextCell( 5 );
247   plane_lines->InsertCellPoint( 0 );
248   plane_lines->InsertCellPoint( 1 );
249   plane_lines->InsertCellPoint( 2 );
250   plane_lines->InsertCellPoint( 3 );
251   plane_lines->InsertCellPoint( 0 );
252   this->PlaneSource->SetPoints( plane_points );
253   this->PlaneSource->SetLines( plane_lines );
254
255   this->PlaneMapper->SetInputData( this->PlaneSource );
256   this->PlaneActor->SetMapper( this->PlaneMapper );
257
258   this->TextActor->SetTextScaleModeToNone( );
259   vtkTextProperty* textprop = this->TextActor->GetTextProperty( );
260   textprop->SetColor( 1, 1, 1 );
261   textprop->SetFontFamilyToCourier( );
262   textprop->SetFontSize( 18 );
263   textprop->BoldOff( );
264   textprop->ItalicOff( );
265   textprop->ShadowOff( );
266   textprop->SetJustificationToLeft( );
267   textprop->SetVerticalJustificationToBottom( );
268   vtkCoordinate* coord = this->TextActor->GetPositionCoordinate( );
269   coord->SetCoordinateSystemToNormalizedViewport( );
270   coord->SetValue( 0.01, 0.01 );
271
272 }
273
274 // -------------------------------------------------------------------------
275 cpPlugins::Extensions::Visualization::ImageSliceActors::
276 ~ImageSliceActors( )
277 {
278 }
279
280 // eof - $RCSfile$