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