]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/MPRActors.cxx
OpenGL with opaque binary image... Soon to be plural
[cpPlugins.git] / lib / cpExtensions / Visualization / MPRActors.cxx
1 #include <cpExtensions/Visualization/MPRActors.h>
2
3 #include <vtkAlgorithmOutput.h>
4 #include <vtkImageData.h>
5 #include <vtkOutlineSource.h>
6 #include <vtkProperty.h>
7 #include <vtkRenderer.h>
8 #include <vtkRendererCollection.h>
9 #include <vtkRenderWindow.h>
10
11 // -------------------------------------------------------------------------
12 cpExtensions::Visualization::MPRActors*
13 cpExtensions::Visualization::MPRActors::
14 New( )
15 {
16   return( new Self( ) );
17 }
18
19 // -------------------------------------------------------------------------
20 cpExtensions::Visualization::
21 ImageSliceActors* cpExtensions::Visualization::MPRActors::
22 GetSliceActors( const int& i ) const
23 {
24   if( i < 3 )
25     return( this->Slices[ 0 ][ i ] );
26   else
27     return( NULL );
28 }
29
30 // -------------------------------------------------------------------------
31 int cpExtensions::Visualization::MPRActors::
32 AddInputConnection( vtkAlgorithmOutput* aout )
33 {
34   for( unsigned int i = 0; i < 2; ++i )
35   {
36     for( unsigned int j = 0; j < 3; ++j )
37     {
38       this->Slices[ i ][ j ]->SetInputConnection( aout );
39       this->Slices[ i ][ j ]->SetAxis( j );
40
41     } // rof
42
43   } // rof
44   this->_CreateBoundingBox( );
45   return( 0 );
46 }
47
48 // -------------------------------------------------------------------------
49 int cpExtensions::Visualization::MPRActors::
50 AddInputData( vtkImageData* new_image )
51 {
52   for( unsigned int i = 0; i < 2; ++i )
53   {
54     for( unsigned int j = 0; j < 3; ++j )
55     {
56       this->Slices[ i ][ j ]->SetInputImage( new_image );
57       this->Slices[ i ][ j ]->SetAxis( j );
58
59     } // rof
60
61   } // rof
62   this->_CreateBoundingBox( );
63   return( 0 );
64 }
65
66 // -------------------------------------------------------------------------
67 unsigned int cpExtensions::Visualization::MPRActors::
68 GetNumberOfImages( ) const
69 {
70   /* TODO
71      ImageBlender* blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
72      if( blender != NULL )
73      return( blender->GetNumberOfImages( ) );
74      else
75   */
76   return( 0 );
77 }
78
79 // -------------------------------------------------------------------------
80 void cpExtensions::Visualization::MPRActors::
81 PushActorsInto(
82   vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
83   vtkRenderWindow* w
84   )
85 {
86   this->Slices[ 0 ][ 0 ]->PushActorsInto( x, true );
87   this->Slices[ 0 ][ 1 ]->PushActorsInto( y, true );
88   this->Slices[ 0 ][ 2 ]->PushActorsInto( z, true );
89   this->Slices[ 1 ][ 0 ]->PushActorsInto( w, false );
90   this->Slices[ 1 ][ 1 ]->PushActorsInto( w, false );
91   this->Slices[ 1 ][ 2 ]->PushActorsInto( w, false );
92
93   vtkRenderer* xren =
94     ( x != NULL )? x->GetRenderers( )->GetFirstRenderer( ): NULL;
95   vtkRenderer* yren =
96     ( y != NULL )? y->GetRenderers( )->GetFirstRenderer( ): NULL;
97   vtkRenderer* zren =
98     ( z != NULL )? z->GetRenderers( )->GetFirstRenderer( ): NULL;
99   vtkRenderer* wren =
100     ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
101
102   /* TODO
103      if( xren != NULL )
104      {
105      xren->AddActor( this->Slices[ 0 ][ 1 ]->GetPlaneActor( ) );
106      xren->AddActor( this->Slices[ 0 ][ 2 ]->GetPlaneActor( ) );
107
108      } // fi
109      if( yren != NULL )
110      {
111      yren->AddActor( this->Slices[ 0 ][ 0 ]->GetPlaneActor( ) );
112      yren->AddActor( this->Slices[ 0 ][ 2 ]->GetPlaneActor( ) );
113
114      } // fi
115      if( zren != NULL )
116      {
117      zren->AddActor( this->Slices[ 0 ][ 0 ]->GetPlaneActor( ) );
118      zren->AddActor( this->Slices[ 0 ][ 1 ]->GetPlaneActor( ) );
119
120      } // fi
121   */
122   if( wren != NULL )
123     wren->AddActor( this->ImageOutlineActor );
124 }
125
126 // -------------------------------------------------------------------------
127 void cpExtensions::Visualization::MPRActors::
128 PopActorsFrom(
129   vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
130   vtkRenderWindow* w
131   )
132 {
133   this->Slices[ 0 ][ 0 ]->PopActorsFrom( x );
134   this->Slices[ 0 ][ 1 ]->PopActorsFrom( y );
135   this->Slices[ 0 ][ 2 ]->PopActorsFrom( z );
136   this->Slices[ 1 ][ 0 ]->PopActorsFrom( w );
137   this->Slices[ 1 ][ 1 ]->PopActorsFrom( w );
138   this->Slices[ 1 ][ 2 ]->PopActorsFrom( w );
139
140   vtkRenderer* wren =
141     ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
142   if( wren != NULL )
143     wren->RemoveActor( this->ImageOutlineActor );
144
145   this->Slices[ 0 ][ 0 ]->Clear( );
146   this->Slices[ 0 ][ 1 ]->Clear( );
147   this->Slices[ 0 ][ 2 ]->Clear( );
148   this->Slices[ 1 ][ 0 ]->Clear( );
149   this->Slices[ 1 ][ 1 ]->Clear( );
150   this->Slices[ 1 ][ 2 ]->Clear( );
151 }
152
153 // -------------------------------------------------------------------------
154 double cpExtensions::Visualization::MPRActors::
155 GetWindow( )
156 {
157   return( this->Slices[ 0 ][ 0 ]->GetWindow( ) );
158 }
159
160 // -------------------------------------------------------------------------
161 double cpExtensions::Visualization::MPRActors::
162 GetLevel( )
163 {
164   return( this->Slices[ 0 ][ 0 ]->GetLevel( ) );
165 }
166
167 // -------------------------------------------------------------------------
168 int cpExtensions::Visualization::MPRActors::
169 GetSliceNumberMinValue( const int& axis ) const
170 {
171   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
172 }
173
174 // -------------------------------------------------------------------------
175 int cpExtensions::Visualization::MPRActors::
176 GetSliceNumberMaxValue( const int& axis ) const
177 {
178   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
179 }
180
181 // -------------------------------------------------------------------------
182 int cpExtensions::Visualization::MPRActors::
183 GetSlice( const int& axis ) const
184 {
185   return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
186 }
187
188 // -------------------------------------------------------------------------
189 void cpExtensions::Visualization::MPRActors::
190 SetSlice( const int& axis, const int& slice )
191 {
192   vtkImageData* image =
193     dynamic_cast< vtkImageData* >( this->Slices[ 0 ][ 0 ]->GetInputImage( ) );
194   if( image == NULL )
195     return;
196
197   // Get image data extent
198   int ext[ 6 ];
199   image->GetExtent( ext );
200
201   // Check if the slice is valid
202   int real = slice;
203   if( slice < ext[ axis << 1 ] )
204     real = ext[ axis << 1 ];
205   if( ext[ ( axis << 1 ) + 1 ] < slice )
206     real = ext[ ( axis << 1 ) + 1 ];
207
208   // Change slice
209   this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
210   this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
211   this->Modified( );
212 }
213
214 // -------------------------------------------------------------------------
215 void cpExtensions::Visualization::MPRActors::
216 SetSlice( const int& axis, const double& slice )
217 {
218   vtkImageData* image =
219     dynamic_cast< vtkImageData* >( this->Slices[ 0 ][ 0 ]->GetInputImage( ) );
220   if( image == NULL )
221     return;
222
223   double x[ 3 ] = { double( 0 ) };
224   double pcoords[ 3 ];
225   int ijk[ 3 ];
226
227   x[ axis ] = slice;
228   image->ComputeStructuredCoordinates( x, ijk, pcoords );
229   this->SetSlice( axis, ijk[ axis ] );
230 }
231
232 // -------------------------------------------------------------------------
233 void cpExtensions::Visualization::MPRActors::
234 ResetSlices( )
235 {
236   for( unsigned int i = 0; i < 2; ++i )
237     for( unsigned int j = 0; j < 3; ++j )
238       this->Slices[ i ][ j ]->SetSliceNumber(
239         this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
240         );
241 }
242
243 // -------------------------------------------------------------------------
244 cpExtensions::Visualization::MPRActors::
245 MPRActors( )
246   : Superclass( )
247 {
248   this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
249   for( unsigned int i = 0; i < 2; ++i )
250     for( unsigned int j = 0; j < 3; ++j )
251       this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
252
253   this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
254   this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
255   this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
256
257   for( unsigned int i = 0; i < 3; ++ i )
258   {
259     this->Slices[ 0 ][ i ]->AddRenderCommand(
260       Self::_RenderCommand, this
261       );
262     this->Slices[ 0 ][ i ]->AddWindowLevelCommand(
263       Self::_WindowLevelCommand, this
264       );
265     this->Slices[ 0 ][ i ]->AddSlicesCommand( Self::_SlicesCommand, this );
266
267   } // rof
268 }
269
270 // -------------------------------------------------------------------------
271 cpExtensions::Visualization::MPRActors::
272 ~MPRActors( )
273 {
274 }
275
276 // -------------------------------------------------------------------------
277 void cpExtensions::Visualization::MPRActors::
278 _CreateBoundingBox( )
279 {
280   vtkImageData* image =
281     dynamic_cast< vtkImageData* >( this->Slices[ 0 ][ 0 ]->GetInputImage( ) );
282   if( image == NULL )
283     return;
284
285   // Create 3D outline
286   double bb[ 6 ];
287   image->GetBounds( bb );
288
289   vtkSmartPointer< vtkOutlineSource > img_ol =
290     vtkSmartPointer< vtkOutlineSource >::New( );
291   img_ol->SetBounds( bb );
292
293   vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
294     vtkSmartPointer< vtkPolyDataMapper >::New( );
295   img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
296   this->ImageOutlineActor->SetMapper( img_ol_mapper );
297   this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
298   this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
299
300   this->AddItem( this->ImageOutlineActor );
301 }
302
303 // -------------------------------------------------------------------------
304 void cpExtensions::Visualization::MPRActors::
305 _SlicesCommand( double* pos, int axis, void* data )
306 {
307   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
308   if( actors == NULL )
309     return;
310   for( unsigned int j = 0; j < 3; ++j )
311   {
312     if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
313     {
314       actors->Slices[ 0 ][ j ]->SetSlice( pos );
315       actors->Slices[ 1 ][ j ]->SetSlice( pos );
316       actors->Slices[ 0 ][ j ]->Render( 1e-3 );
317       actors->Slices[ 1 ][ j ]->Render( 1e-3 );
318
319     } // fi
320
321   } // rof
322 }
323
324 // -------------------------------------------------------------------------
325 void cpExtensions::Visualization::MPRActors::
326 _WindowLevelCommand( double window, double level, void* data )
327 {
328   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
329   if( actors == NULL )
330     return;
331
332   actors->Slices[ 0 ][ 0 ]->SetWindowLevel( window, level );
333   actors->Slices[ 0 ][ 1 ]->SetWindowLevel( window, level );
334   actors->Slices[ 0 ][ 2 ]->SetWindowLevel( window, level );
335   actors->Slices[ 1 ][ 0 ]->SetWindowLevel( window, level );
336   actors->Slices[ 1 ][ 1 ]->SetWindowLevel( window, level );
337   actors->Slices[ 1 ][ 2 ]->SetWindowLevel( window, level );
338
339   actors->Slices[ 0 ][ 0 ]->Render( 1e-3 );
340   actors->Slices[ 0 ][ 1 ]->Render( 1e-3 );
341   actors->Slices[ 0 ][ 2 ]->Render( 1e-3 );
342   actors->Slices[ 1 ][ 0 ]->Render( 1e-3 );
343   actors->Slices[ 1 ][ 1 ]->Render( 1e-3 );
344   actors->Slices[ 1 ][ 2 ]->Render( 1e-3 );
345 }
346
347 // -------------------------------------------------------------------------
348 void cpExtensions::Visualization::MPRActors::
349 _RenderCommand( void* data )
350 {
351   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
352   if( actors == NULL )
353     return;
354   actors->Slices[ 0 ][ 0 ]->Render( 1e-3 );
355   actors->Slices[ 0 ][ 1 ]->Render( 1e-3 );
356   actors->Slices[ 0 ][ 2 ]->Render( 1e-3 );
357   actors->Slices[ 1 ][ 0 ]->Render( 1e-3 );
358   actors->Slices[ 1 ][ 1 ]->Render( 1e-3 );
359   actors->Slices[ 1 ][ 2 ]->Render( 1e-3 );
360 }
361
362 // eof - $RCSfile$