]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Extensions/Visualization/MPRActors.cxx
9808111ad8a380b9ac88ae96f8fa82ed8114add3
[cpPlugins.git] / lib / cpPlugins / Extensions / Visualization / MPRActors.cxx
1 #include <cpPlugins/Extensions/Visualization/MPRActors.h>
2
3 #include <vtkImageData.h>
4 #include <vtkLookupTable.h>
5 #include <vtkOutlineSource.h>
6 #include <vtkPolyDataMapper.h>
7 #include <vtkProperty.h>
8 #include <vtkRenderer.h>
9
10 // -------------------------------------------------------------------------
11 cpPlugins::Extensions::Visualization::MPRActors*
12 cpPlugins::Extensions::Visualization::MPRActors::
13 New( )
14 {
15   return( new Self( ) );
16 }
17
18 // -------------------------------------------------------------------------
19 cpPlugins::Extensions::Visualization::
20 ImageSliceActors* cpPlugins::Extensions::Visualization::MPRActors::
21 GetSliceActors( const int& i ) const
22 {
23   if( i < 3 )
24     return( this->Slices[ i ] );
25   else
26     return( NULL );
27 }
28
29 // -------------------------------------------------------------------------
30 void cpPlugins::Extensions::Visualization::MPRActors::
31 SetInputData( vtkImageData* image )
32 {
33   if( image == NULL )
34     return;
35
36   this->Image = image;
37
38   this->ImageToWindowLevel->SetInputData( this->Image );
39   this->ResetWindowLevel( );
40   this->ImageToWindowLevel->Update( );
41
42   for( int i = 0; i < 3; ++i )
43   {
44     this->Slices[ i ]->SetInputConnection(
45       this->ImageToWindowLevel->GetOutputPort( ), i
46       );
47     this->Slices[ i ]->UpdateText( this->GetWindow( ), this->GetLevel( ) );
48
49   } // rof
50
51   // Create 3D outline
52   vtkSmartPointer< vtkOutlineSource > img_ol =
53     vtkSmartPointer< vtkOutlineSource >::New( );
54   img_ol->SetBounds( this->Image->GetBounds( ) );
55
56   vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
57     vtkSmartPointer< vtkPolyDataMapper >::New( );
58   img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
59   this->ImageOutlineActor->SetMapper( img_ol_mapper );
60   this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
61   this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
62
63   this->ImageOutlineActorIndex = this->GetNumberOfItems( );
64   this->AddItem( this->ImageOutlineActor );
65
66   /*
67   // Cursor radius
68   double spac[ 3 ];
69   image->GetSpacing( spac );
70   double radius = spac[ 0 ];
71   radius = ( spac[ 1 ] < radius )? spac[ 1 ]: radius;
72   radius = ( spac[ 2 ] < radius )? spac[ 2 ]: radius;
73   radius *= double( 6 );
74   this->Cursor->SetRadius( radius );
75   this->CursorMapper->Modified( );
76   this->CursorActor->Modified( );
77
78   // Plane actors
79   for( int a = 0; a < 3; ++a )
80   {
81     // Configure actors
82     this->Planes[ a ].Configure( this->ImageToWindowLevel->GetOutputPort( ), a );
83     this->Planes[ a ].ConfigureRegion( this->Region->GetOutputPort( ) );
84     this->Planes[ a ].UpdateText( this->GetWindow( ), this->GetLevel( ) );
85
86     // Add them to renderer
87     vtkRenderer* ren = this->Interactors[ a ]->GetRenderWindow( )->
88       GetRenderers( )->GetFirstRenderer( );
89     if( ren == NULL )
90       vtkErrorMacro( "At least one interactor doesn't have a valid renderer" );
91     ren->AddActor( this->Planes[ a ].ImageActor );
92     ren->AddActor( this->Planes[ a ].TextActor );
93
94     for( int i = 0; i < 3; ++i )
95       this->Interactors[ a ]->GetRenderWindow( )->
96         GetRenderers( )->GetFirstRenderer( )->
97         AddActor( this->Planes[ i ].PlaneActor );
98
99   } // rof
100   */
101   // Keep track into collection
102 /*
103   this->XPlaneIndex = this->GetNumberOfItems( );
104   this->AddItem( this->Planes[ 0 ].ImageActor.GetPointer( ) );
105   this->XTextIndex = this->GetNumberOfItems( );
106   this->AddItem( this->Planes[ 0 ].TextActor.GetPointer( ) );
107   this->XBoundsIndex = this->GetNumberOfItems( );
108   this->AddItem( this->Planes[ 0 ].PlaneActor.GetPointer( ) );
109
110   this->YPlaneIndex = this->GetNumberOfItems( );
111   this->AddItem( this->Planes[ 1 ].ImageActor.GetPointer( ) );
112   this->YTextIndex = this->GetNumberOfItems( );
113   this->AddItem( this->Planes[ 1 ].TextActor.GetPointer( ) );
114   this->YBoundsIndex = this->GetNumberOfItems( );
115   this->AddItem( this->Planes[ 1 ].PlaneActor.GetPointer( ) );
116
117   this->ZPlaneIndex = this->GetNumberOfItems( );
118   this->AddItem( this->Planes[ 2 ].ImageActor.GetPointer( ) );
119   this->ZTextIndex = this->GetNumberOfItems( );
120   this->AddItem( this->Planes[ 2 ].TextActor.GetPointer( ) );
121   this->ZBoundsIndex = this->GetNumberOfItems( );
122   this->AddItem( this->Planes[ 2 ].PlaneActor.GetPointer( ) );
123 */
124   // Initialize slice visualization
125   this->ResetSlices( );
126
127   /*
128     #error CONTOUR_WIDGET <- ACA VOY
129     static vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep =
130     vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();
131     static vtkSmartPointer<vtkContourWidget> contourWidget =
132     vtkSmartPointer<vtkContourWidget>::New();
133     contourWidget->SetInteractor( zi );
134     contourWidget->SetRepresentation( contourRep );
135     contourWidget->On( );
136   */
137 }
138
139 // -------------------------------------------------------------------------
140 void cpPlugins::Extensions::Visualization::MPRActors::
141 SetSegmentationData( vtkImageData* segmentation )
142 {
143   double range[ 2 ];
144   this->Segmentation = segmentation;
145   this->Segmentation->GetScalarRange( range );
146
147   vtkSmartPointer< vtkLookupTable > lut =
148     vtkSmartPointer< vtkLookupTable >::New( );
149   lut->SetNumberOfTableValues( 2 );
150   lut->SetTableRange( range );
151   lut->SetTableValue( 0, 0, 0, 0, 0 );
152   lut->SetTableValue( 1, 1, 0, 0, 0.4 );
153
154   this->SegmentationToColors->SetInputData( this->Segmentation );
155   this->SegmentationToColors->SetLookupTable( lut );
156   this->SegmentationToColors->Update( );
157
158   for( int i = 0; i < 3; ++i )
159     this->Slices[ i ]->SetSegmentationConnection(
160       this->SegmentationToColors->GetOutputPort( )
161       );
162 }
163
164 // -------------------------------------------------------------------------
165 void cpPlugins::Extensions::Visualization::MPRActors::
166 PushDataInto( vtkRenderer* x, vtkRenderer* y, vtkRenderer* z, vtkRenderer* w )
167 {
168   vtkRenderer* rends[] = { x, y, z };
169   for( int i = 0; i < 3; ++i )
170   {
171     if( rends[ i ] != NULL )
172     {
173       rends[ i ]->AddActor( this->Slices[ i ]->GetImageActor( ) );
174       rends[ i ]->AddActor( this->Slices[ i ]->GetTextActor( ) );
175       for( int j = 0; j < 3; ++j )
176         rends[ i ]->AddActor( this->Slices[ j ]->GetPlaneActor( ) );
177       if( this->Segmentation != NULL )
178         rends[ i ]->AddActor( this->Slices[ i ]->GetSegmentationActor( ) );
179       if( w != NULL )
180         w->AddActor( this->Slices[ i ]->GetPlaneActor( ) );
181
182     } // fi
183
184   } // rof
185
186   if( w != NULL )
187   {
188     w->AddActor( this->ImageOutlineActor );
189
190   } // fi
191 }
192
193 // -------------------------------------------------------------------------
194 void cpPlugins::Extensions::Visualization::MPRActors::
195 PopDataFrom( vtkRenderer* x, vtkRenderer* y, vtkRenderer* z, vtkRenderer* w )
196 {
197 }
198
199 // -------------------------------------------------------------------------
200 double cpPlugins::Extensions::Visualization::MPRActors::
201 GetMinWindow( ) const
202 {
203   return( double( 0 ) );
204 }
205
206 // -------------------------------------------------------------------------
207 double cpPlugins::Extensions::Visualization::MPRActors::
208 GetMaxWindow( ) const
209 {
210   if( this->Image == NULL )
211     return( double( 0 ) );
212
213   double range[ 2 ];
214   this->Image->GetScalarRange( range );
215   return( range[ 1 ] - range[ 0 ] );
216 }
217
218 // -------------------------------------------------------------------------
219 double cpPlugins::Extensions::Visualization::MPRActors::
220 GetMinLevel( ) const
221 {
222   if( this->Image == NULL )
223     return( double( 0 ) );
224
225   double range[ 2 ];
226   this->Image->GetScalarRange( range );
227   return( range[ 0 ] );
228 }
229
230 // -------------------------------------------------------------------------
231 double cpPlugins::Extensions::Visualization::MPRActors::
232 GetMaxLevel( ) const
233 {
234   if( this->Image == NULL )
235     return( double( 0 ) );
236
237   double range[ 2 ];
238   this->Image->GetScalarRange( range );
239   return( range[ 1 ] );
240 }
241
242 // -------------------------------------------------------------------------
243 double cpPlugins::Extensions::Visualization::MPRActors::
244 GetWindow( ) const
245 {
246   if( this->Image != NULL )
247     return( this->ImageToWindowLevel->GetWindow( ) );
248   else
249     return( 0 );
250 }
251
252 // -------------------------------------------------------------------------
253 double cpPlugins::Extensions::Visualization::MPRActors::
254 GetLevel( ) const
255 {
256   if( this->Image != NULL )
257     return( this->ImageToWindowLevel->GetLevel( ) );
258   else
259     return( 0 );
260 }
261
262 // -------------------------------------------------------------------------
263 void cpPlugins::Extensions::Visualization::MPRActors::
264 SetWindow( const double& w )
265 {
266   if( this->Image != NULL )
267     this->ImageToWindowLevel->SetWindow( w );
268 }
269
270 // -------------------------------------------------------------------------
271 void cpPlugins::Extensions::Visualization::MPRActors::
272 SetLevel( const double& l )
273 {
274   if( this->Image != NULL )
275     this->ImageToWindowLevel->SetLevel( l );
276 }
277
278 // -------------------------------------------------------------------------
279 void cpPlugins::Extensions::Visualization::MPRActors::
280 SetWindowLevel( const double& w, const double& l )
281 {
282   this->ImageToWindowLevel->SetWindow( w );
283   this->ImageToWindowLevel->SetLevel( l );
284   for( int i = 0; i < 3; ++i )
285     this->Slices[ i ]->UpdateText( w, l );
286 }
287
288 // -------------------------------------------------------------------------
289 void cpPlugins::Extensions::Visualization::MPRActors::
290 ResetWindowLevel( )
291 {
292   double range[ 2 ];
293   this->Image->GetScalarRange( range );
294   this->SetWindowLevel(
295     range[ 1 ] - range[ 0 ],
296     ( ( range[ 1 ] + range[ 0 ] ) / double( 2 ) ) + range[ 0 ]
297     );
298 }
299
300 // -------------------------------------------------------------------------
301 vtkPlane* cpPlugins::Extensions::Visualization::MPRActors::
302 GetSlicePlane( const int& axis ) const
303 {
304   return( NULL );
305 }
306
307 // -------------------------------------------------------------------------
308 int cpPlugins::Extensions::Visualization::MPRActors::
309 GetSliceNumberMinValue( const int& axis ) const
310 {
311   return( this->Slices[ axis ]->GetSliceNumberMinValue( ) );
312 }
313
314 // -------------------------------------------------------------------------
315 int cpPlugins::Extensions::Visualization::MPRActors::
316 GetSliceNumberMaxValue( const int& axis ) const
317 {
318   return( this->Slices[ axis ]->GetSliceNumberMaxValue( ) );
319 }
320
321 // -------------------------------------------------------------------------
322 int cpPlugins::Extensions::Visualization::MPRActors::
323 GetSlice( const int& axis ) const
324 {
325   return( this->Slices[ axis ]->GetSliceNumber( ) );
326 }
327
328 // -------------------------------------------------------------------------
329 void cpPlugins::Extensions::Visualization::MPRActors::
330 SetSlice( const int& axis, const int& slice )
331 {
332   // Get image data extent
333   if( this->Image == NULL )
334     return;
335   int ext[ 6 ];
336   this->Image->GetExtent( ext );
337
338   // Check if the slice is valid
339   int real = slice;
340   if( slice < ext[ axis << 1 ] )
341     real = ext[ axis << 1 ];
342   if( ext[ ( axis << 1 ) + 1 ] < slice )
343     real = ext[ ( axis << 1 ) + 1 ];
344
345   // Change slice
346   this->Slices[ axis ]->SetSliceNumber( real );
347   this->Modified( );
348 }
349
350 // -------------------------------------------------------------------------
351 void cpPlugins::Extensions::Visualization::MPRActors::
352 SetSlice( const int& axis, const double& slice )
353 {
354   if( this->Image == NULL )
355     return;
356   
357   double x[ 3 ] = { double( 0 ) };
358   double pcoords[ 3 ];
359   int ijk[ 3 ];
360
361   x[ axis ] = slice;
362   this->Image->ComputeStructuredCoordinates( x, ijk, pcoords );
363   this->SetSlice( axis, ijk[ axis ] );
364 }
365
366 // -------------------------------------------------------------------------
367 void cpPlugins::Extensions::Visualization::MPRActors::
368 ResetSlices( )
369 {
370 }
371
372 // -------------------------------------------------------------------------
373 void cpPlugins::Extensions::Visualization::MPRActors::
374 GetImageBounds( double bounds[ 6 ] ) const
375 {
376 }
377
378 // -------------------------------------------------------------------------
379 cpPlugins::Extensions::Visualization::MPRActors::
380 MPRActors( )
381   : Superclass( ),
382     Image( NULL ),
383     Segmentation( NULL )
384 {
385   this->ImageToWindowLevel =
386     vtkSmartPointer< vtkImageMapToWindowLevelColors >::New( );
387   this->SegmentationToColors = vtkSmartPointer< vtkImageMapToColors >::New( );
388   this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
389   this->Slices[ 0 ] = vtkSmartPointer< TSlice >::New( );
390   this->Slices[ 1 ] = vtkSmartPointer< TSlice >::New( );
391   this->Slices[ 2 ] = vtkSmartPointer< TSlice >::New( );
392 }
393
394 // -------------------------------------------------------------------------
395 cpPlugins::Extensions::Visualization::MPRActors::
396 ~MPRActors( )
397 {
398 }
399
400 // eof - $RCSfile$