]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/MPRActors.cxx
OpenGL binary images visualization finished.
[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   return( this->Slices[ 0 ][ 0 ]->GetNumberOfImages( ) );
71 }
72
73 // -------------------------------------------------------------------------
74 void cpExtensions::Visualization::MPRActors::
75 PushActorsInto(
76   vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
77   vtkRenderWindow* w
78   )
79 {
80   this->Slices[ 0 ][ 0 ]->PushActorsInto( x, true );
81   this->Slices[ 0 ][ 1 ]->PushActorsInto( y, true );
82   this->Slices[ 0 ][ 2 ]->PushActorsInto( z, true );
83   this->Slices[ 1 ][ 0 ]->PushActorsInto( w, false );
84   this->Slices[ 1 ][ 1 ]->PushActorsInto( w, false );
85   this->Slices[ 1 ][ 2 ]->PushActorsInto( w, false );
86
87   vtkRenderer* wren =
88     ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
89   if( wren != NULL )
90     wren->AddActor( this->ImageOutlineActor );
91 }
92
93 // -------------------------------------------------------------------------
94 void cpExtensions::Visualization::MPRActors::
95 PopActorsFrom(
96   vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
97   vtkRenderWindow* w
98   )
99 {
100   this->Slices[ 0 ][ 0 ]->PopActorsFrom( x );
101   this->Slices[ 0 ][ 1 ]->PopActorsFrom( y );
102   this->Slices[ 0 ][ 2 ]->PopActorsFrom( z );
103   this->Slices[ 1 ][ 0 ]->PopActorsFrom( w );
104   this->Slices[ 1 ][ 1 ]->PopActorsFrom( w );
105   this->Slices[ 1 ][ 2 ]->PopActorsFrom( w );
106
107   vtkRenderer* wren =
108     ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
109   if( wren != NULL )
110     wren->RemoveActor( this->ImageOutlineActor );
111
112   this->Slices[ 0 ][ 0 ]->Clear( );
113   this->Slices[ 0 ][ 1 ]->Clear( );
114   this->Slices[ 0 ][ 2 ]->Clear( );
115   this->Slices[ 1 ][ 0 ]->Clear( );
116   this->Slices[ 1 ][ 1 ]->Clear( );
117   this->Slices[ 1 ][ 2 ]->Clear( );
118 }
119
120 // -------------------------------------------------------------------------
121 double cpExtensions::Visualization::MPRActors::
122 GetWindow( )
123 {
124   return( this->Slices[ 0 ][ 0 ]->GetWindow( ) );
125 }
126
127 // -------------------------------------------------------------------------
128 double cpExtensions::Visualization::MPRActors::
129 GetLevel( )
130 {
131   return( this->Slices[ 0 ][ 0 ]->GetLevel( ) );
132 }
133
134 // -------------------------------------------------------------------------
135 int cpExtensions::Visualization::MPRActors::
136 GetSliceNumberMinValue( const int& axis ) const
137 {
138   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
139 }
140
141 // -------------------------------------------------------------------------
142 int cpExtensions::Visualization::MPRActors::
143 GetSliceNumberMaxValue( const int& axis ) const
144 {
145   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
146 }
147
148 // -------------------------------------------------------------------------
149 int cpExtensions::Visualization::MPRActors::
150 GetSlice( const int& axis ) const
151 {
152   return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
153 }
154
155 // -------------------------------------------------------------------------
156 void cpExtensions::Visualization::MPRActors::
157 SetSlice( const int& axis, const int& slice )
158 {
159   vtkImageData* image =
160     dynamic_cast< vtkImageData* >( this->Slices[ 0 ][ 0 ]->GetInputImage( ) );
161   if( image == NULL )
162     return;
163
164   // Get image data extent
165   int ext[ 6 ];
166   image->GetExtent( ext );
167
168   // Check if the slice is valid
169   int real = slice;
170   if( slice < ext[ axis << 1 ] )
171     real = ext[ axis << 1 ];
172   if( ext[ ( axis << 1 ) + 1 ] < slice )
173     real = ext[ ( axis << 1 ) + 1 ];
174
175   // Change slice
176   this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
177   this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
178   this->Modified( );
179 }
180
181 // -------------------------------------------------------------------------
182 void cpExtensions::Visualization::MPRActors::
183 SetSlice( const int& axis, const double& slice )
184 {
185   vtkImageData* image =
186     dynamic_cast< vtkImageData* >( this->Slices[ 0 ][ 0 ]->GetInputImage( ) );
187   if( image == NULL )
188     return;
189
190   double x[ 3 ] = { double( 0 ) };
191   double pcoords[ 3 ];
192   int ijk[ 3 ];
193
194   x[ axis ] = slice;
195   image->ComputeStructuredCoordinates( x, ijk, pcoords );
196   this->SetSlice( axis, ijk[ axis ] );
197 }
198
199 // -------------------------------------------------------------------------
200 void cpExtensions::Visualization::MPRActors::
201 ResetSlices( )
202 {
203   for( unsigned int i = 0; i < 2; ++i )
204     for( unsigned int j = 0; j < 3; ++j )
205       this->Slices[ i ][ j ]->SetSliceNumber(
206         this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
207         );
208 }
209
210 // -------------------------------------------------------------------------
211 cpExtensions::Visualization::MPRActors::
212 MPRActors( )
213   : Superclass( )
214 {
215   this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
216   for( unsigned int i = 0; i < 2; ++i )
217     for( unsigned int j = 0; j < 3; ++j )
218       this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
219
220   this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
221   this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
222   this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
223
224   for( unsigned int i = 0; i < 3; ++ i )
225   {
226     this->Slices[ 0 ][ i ]->AddRenderCommand(
227       Self::_RenderCommand, this
228       );
229     this->Slices[ 0 ][ i ]->AddWindowLevelCommand(
230       Self::_WindowLevelCommand, this
231       );
232     this->Slices[ 0 ][ i ]->AddSlicesCommand( Self::_SlicesCommand, this );
233
234   } // rof
235 }
236
237 // -------------------------------------------------------------------------
238 cpExtensions::Visualization::MPRActors::
239 ~MPRActors( )
240 {
241 }
242
243 // -------------------------------------------------------------------------
244 void cpExtensions::Visualization::MPRActors::
245 _CreateBoundingBox( )
246 {
247   vtkImageData* image =
248     dynamic_cast< vtkImageData* >( this->Slices[ 0 ][ 0 ]->GetInputImage( ) );
249   if( image == NULL )
250     return;
251
252   // Create 3D outline
253   double bb[ 6 ];
254   image->GetBounds( bb );
255
256   vtkSmartPointer< vtkOutlineSource > img_ol =
257     vtkSmartPointer< vtkOutlineSource >::New( );
258   img_ol->SetBounds( bb );
259
260   vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
261     vtkSmartPointer< vtkPolyDataMapper >::New( );
262   img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
263   this->ImageOutlineActor->SetMapper( img_ol_mapper );
264   this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
265   this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
266
267   this->AddItem( this->ImageOutlineActor );
268 }
269
270 // -------------------------------------------------------------------------
271 void cpExtensions::Visualization::MPRActors::
272 _SlicesCommand( double* pos, int axis, void* data )
273 {
274   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
275   if( actors == NULL )
276     return;
277   for( unsigned int j = 0; j < 3; ++j )
278   {
279     if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
280     {
281       actors->Slices[ 0 ][ j ]->SetSlice( pos );
282       actors->Slices[ 1 ][ j ]->SetSlice( pos );
283       actors->Slices[ 0 ][ j ]->Render( 1e-3 );
284       actors->Slices[ 1 ][ j ]->Render( 1e-3 );
285
286     } // fi
287
288   } // rof
289 }
290
291 // -------------------------------------------------------------------------
292 void cpExtensions::Visualization::MPRActors::
293 _WindowLevelCommand( double window, double level, void* data )
294 {
295   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
296   if( actors == NULL )
297     return;
298
299   actors->Slices[ 0 ][ 0 ]->SetWindowLevel( window, level );
300   actors->Slices[ 0 ][ 1 ]->SetWindowLevel( window, level );
301   actors->Slices[ 0 ][ 2 ]->SetWindowLevel( window, level );
302   actors->Slices[ 1 ][ 0 ]->SetWindowLevel( window, level );
303   actors->Slices[ 1 ][ 1 ]->SetWindowLevel( window, level );
304   actors->Slices[ 1 ][ 2 ]->SetWindowLevel( window, level );
305
306   actors->Slices[ 0 ][ 0 ]->Render( 1e-3 );
307   actors->Slices[ 0 ][ 1 ]->Render( 1e-3 );
308   actors->Slices[ 0 ][ 2 ]->Render( 1e-3 );
309   actors->Slices[ 1 ][ 0 ]->Render( 1e-3 );
310   actors->Slices[ 1 ][ 1 ]->Render( 1e-3 );
311   actors->Slices[ 1 ][ 2 ]->Render( 1e-3 );
312 }
313
314 // -------------------------------------------------------------------------
315 void cpExtensions::Visualization::MPRActors::
316 _RenderCommand( void* data )
317 {
318   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
319   if( actors == NULL )
320     return;
321   actors->Slices[ 0 ][ 0 ]->Render( 1e-3 );
322   actors->Slices[ 0 ][ 1 ]->Render( 1e-3 );
323   actors->Slices[ 0 ][ 2 ]->Render( 1e-3 );
324   actors->Slices[ 1 ][ 0 ]->Render( 1e-3 );
325   actors->Slices[ 1 ][ 1 ]->Render( 1e-3 );
326   actors->Slices[ 1 ][ 2 ]->Render( 1e-3 );
327 }
328
329 // eof - $RCSfile$