]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/QT/SimpleMPRWidget.cxx
...
[cpPlugins.git] / lib / cpExtensions / QT / SimpleMPRWidget.cxx
1 #include <cpExtensions/QT/SimpleMPRWidget.h>
2
3 #ifdef cpExtensions_QT4
4
5 #include <cpExtensions/ui_SimpleMPRWidget.h>
6 #include <vtkImageProperty.h>
7 #include <vtkRenderer.h>
8 #include <vtkRenderWindow.h>
9
10 // -------------------------------------------------------------------------
11 cpExtensions::QT::SimpleMPRWidget::_3DInteractorStyle*
12 cpExtensions::QT::SimpleMPRWidget::_3DInteractorStyle::
13 New( )
14 {
15   return( new _3DInteractorStyle( ) );
16 }
17
18 // -------------------------------------------------------------------------
19 void cpExtensions::QT::SimpleMPRWidget::_3DInteractorStyle::
20 OnKeyPress( )
21 {
22   vtkRenderWindowInteractor* i = this->Interactor;
23   std::string key = i->GetKeySym( );
24   if( key == "r" || key == "R" )
25   {
26     this->CurrentRenderer->ResetCamera( );
27     i->Render( );
28
29   } // fi
30 }
31
32 // -------------------------------------------------------------------------
33 cpExtensions::QT::SimpleMPRWidget::
34 SimpleMPRWidget( QWidget* parent )
35   : QWidget( parent ),
36     m_UI( new Ui::SimpleMPRWidget )
37 {
38   this->m_UI->setupUi( this );
39
40   // Configure VTK widgets
41   this->m_VTK[ 0 ] = this->m_UI->VTK_X;
42   this->m_VTK[ 1 ] = this->m_UI->VTK_Y;
43   this->m_VTK[ 2 ] = this->m_UI->VTK_Z;
44   this->m_VTK[ 3 ] = this->m_UI->VTK_3D;
45   for( unsigned int i = 0; i < 4; ++i )
46   {
47     this->m_Renderers[ i ] = vtkSmartPointer< vtkRenderer >::New( );
48     this->m_VTK[ i ]->GetRenderWindow( )->
49       AddRenderer( this->m_Renderers[ i ] );
50
51   } // rof
52   vtkSmartPointer< _3DInteractorStyle > style =
53     vtkSmartPointer< _3DInteractorStyle >::New( );
54   this->m_VTK[ 3 ]->GetRenderWindow( )->
55     GetInteractor( )->SetInteractorStyle( style );
56   style->SetCurrentRenderer( this->m_Renderers[ 3 ] );
57
58   // Connect slots
59   QObject::connect(
60     this->m_UI->TopSplitter, SIGNAL( splitterMoved( int, int ) ),
61     this, SLOT( _SyncBottom( int, int ) )
62     );
63   QObject::connect(
64     this->m_UI->BottomSplitter, SIGNAL( splitterMoved( int, int ) ),
65     this, SLOT( _SyncTop( int, int ) )
66     );
67 }
68
69 // -------------------------------------------------------------------------
70 cpExtensions::QT::SimpleMPRWidget::
71 ~SimpleMPRWidget( )
72 {
73   this->Clear( );
74   delete this->m_UI;
75 }
76
77 // -------------------------------------------------------------------------
78 void cpExtensions::QT::SimpleMPRWidget::
79 Clear( )
80 {
81   for( unsigned int i = 0; i < 4; ++i )
82     this->m_Renderers[ i ]->RemoveAllViewProps( );
83   for( unsigned int i = 0; i < 3; ++i )
84   {
85     if( this->m_2DSlices[ i ].GetPointer( ) != NULL )
86       this->m_2DSlices[ i ]->Clear( );
87     if( this->m_3DSlices[ i ].GetPointer( ) != NULL )
88       this->m_3DSlices[ i ]->Clear( );
89
90   } // rof
91   this->m_NamedActors.clear( );
92 }
93
94 // -------------------------------------------------------------------------
95 void cpExtensions::QT::SimpleMPRWidget::
96 SetMainImage( vtkImageData* image, const std::string& name )
97 {
98   this->Clear( );
99   for( unsigned int i = 0; i < 3; ++i )
100   {
101     this->m_2DSlices[ i ] = vtkSmartPointer< TSliceActors >::New( );
102     this->m_2DSlices[ i ]->SetInputData( image, i );
103     this->m_2DSlices[ i ]->PushInto( this->m_Renderers[ i ] );
104     this->m_VTK[ i ]->GetRenderWindow( )->GetInteractor( )->
105       SetInteractorStyle( this->m_2DSlices[ i ]->GetStyle( ) );
106
107     this->m_3DSlices[ i ] = vtkSmartPointer< TSliceActors >::New( );
108     this->m_3DSlices[ i ]->SetInputData( image, i );
109     this->m_3DSlices[ i ]->PushInto( this->m_Renderers[ 3 ] );
110     this->m_3DSlices[ i ]->SetStyle(
111       dynamic_cast< vtkInteractorStyle* >(
112         this->m_Renderers[ 3 ]->GetRenderWindow( )->
113         GetInteractor( )->GetInteractorStyle( )
114         )
115       );
116
117     this->m_NamedActors[ name ].insert(
118       this->m_2DSlices[ i ]->GetImageActor( )
119       );
120     this->m_NamedActors[ name ].insert(
121       this->m_3DSlices[ i ]->GetImageActor( )
122       );
123
124   } // rof
125   this->_AssociateSlices( );
126 }
127
128 // -------------------------------------------------------------------------
129 void cpExtensions::QT::SimpleMPRWidget::
130 SetMainActor( vtkImageActor* actor, const std::string& name )
131 {
132   this->Clear( );
133   vtkImageData* image = NULL;
134   int interp = actor->GetProperty( )->GetInterpolationType( );
135   for( unsigned int i = 0; i < 3; ++i )
136   {
137     this->m_2DSlices[ i ] = vtkSmartPointer< TSliceActors >::New( );
138     if( i == 0 )
139     {
140       this->m_2DSlices[ i ]->SetInputActor( actor, i );
141       image = this->m_2DSlices[ i ]->GetInputData( );
142     }
143     else
144       this->m_2DSlices[ i ]->SetInputData( image, i );
145     this->m_2DSlices[ i ]->PushInto( this->m_Renderers[ i ] );
146     this->m_VTK[ i ]->GetRenderWindow( )->GetInteractor( )->
147       SetInteractorStyle( this->m_2DSlices[ i ]->GetStyle( ) );
148
149     this->m_3DSlices[ i ] = vtkSmartPointer< TSliceActors >::New( );
150     this->m_3DSlices[ i ]->SetInputData( image, i );
151     this->m_3DSlices[ i ]->PushInto( this->m_Renderers[ 3 ] );
152     this->m_3DSlices[ i ]->SetStyle(
153       dynamic_cast< vtkInteractorStyle* >(
154         this->m_Renderers[ 3 ]->GetRenderWindow( )->
155         GetInteractor( )->GetInteractorStyle( )
156         )
157       );
158
159     this->m_NamedActors[ name ].insert(
160       this->m_2DSlices[ i ]->GetImageActor( )
161       );
162     this->m_NamedActors[ name ].insert(
163       this->m_3DSlices[ i ]->GetImageActor( )
164       );
165     this->m_2DSlices[ i ]->GetImageActor( )->
166       GetProperty( )->SetInterpolationType( interp );
167     this->m_3DSlices[ i ]->GetImageActor( )->
168       GetProperty( )->SetInterpolationType( interp );
169
170   } // rof
171   this->_AssociateSlices( );
172 }
173
174 // -------------------------------------------------------------------------
175 void cpExtensions::QT::SimpleMPRWidget::
176 AddImage( vtkImageData* image, const std::string& name )
177 {
178   std::cerr << "SimpleMPR: adding image" << std::endl;
179   std::exit( 1 );
180 }
181
182 // -------------------------------------------------------------------------
183 void cpExtensions::QT::SimpleMPRWidget::
184 AddImageActor( vtkImageActor* actor, const std::string& name )
185 {
186   unsigned int i = 0;
187   bool cont = true;
188   while( i < 3 && cont )
189   {
190     if( this->m_2DSlices[ i ].GetPointer( ) != NULL )
191     {
192       if( this->m_2DSlices[ i ]->GetImageActor( ) != NULL )
193       {
194         // TODO: add image actor
195       }
196       else
197         cont = false;
198
199     } // fi
200     i++;
201
202   } // rof
203
204   // Add if no actors were found
205   if( cont )
206     this->SetMainActor( actor, name );
207 }
208
209 // -------------------------------------------------------------------------
210 void cpExtensions::QT::SimpleMPRWidget::
211 AddMesh( vtkPolyData* mesh, const std::string& name )
212 {
213   std::cerr << "SimpleMPR: adding mesh" << std::endl;
214   std::exit( 1 );
215   /* TODO
216      PolyDataActor a;
217      a.Configure( mesh );
218      this->m_PolyDatas[ mesh ] = a;
219      this->m_Renderers[ 3 ]->AddViewProp( a.Actor );
220
221      bool has_main_image = false;
222      for( unsigned int i = 0; i < 3; ++i )
223      {
224      if( this->m_2DSlices[ i ].GetPointer( ) != NULL )
225      {
226      if( this->m_2DSlices[ i ]->GetInputData( ) != NULL )
227      {
228      has_main_image = true;
229      this->m_2DSlices[ i ]->AddMesh( mesh );
230      this->m_2DSlices[ i ]->Render( );
231
232      } // fi
233
234      } // fi
235
236      } // rof
237      if( !has_main_image )
238      this->m_Renderers[ 3 ]->ResetCamera( );
239      this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
240   */
241 }
242
243 // -------------------------------------------------------------------------
244 void cpExtensions::QT::SimpleMPRWidget::
245 AddMeshActor( vtkProp3D* actor, const std::string& name )
246 {
247   if( actor != NULL )
248   {
249     this->m_Renderers[ 3 ]->AddViewProp( actor );
250     this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
251     this->m_NamedActors[ name ].insert( actor );
252     if( this->m_Renderers[ 3 ]->GetViewProps( )->GetNumberOfItems( ) == 1 )
253     {
254       this->m_Renderers[ 3 ]->ResetCamera( );
255       this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
256
257     } // fi
258
259   } // fi
260 }
261
262 // -------------------------------------------------------------------------
263 void cpExtensions::QT::SimpleMPRWidget::
264 AddActor( vtkProp* actor, const std::string& name )
265 {
266   auto ia = dynamic_cast< vtkImageActor* >( actor );
267   if( ia == NULL )
268   {
269     auto ma = dynamic_cast< vtkProp3D* >( actor );
270     if( ma != NULL )
271       this->AddMeshActor( ma, name );
272   }
273   else
274     this->AddImageActor( ia, name );
275 }
276
277 // -------------------------------------------------------------------------
278 const cpExtensions::QT::SimpleMPRWidget::
279 TActors& cpExtensions::QT::SimpleMPRWidget::
280 GetActors( const std::string& name ) const
281 {
282   static const TActors empty;
283   auto i = this->m_NamedActors.find( name );
284   if( i != this->m_NamedActors.end( ) )
285     return( i->second );
286   else
287     return( empty );
288 }
289
290 // -------------------------------------------------------------------------
291 vtkRenderWindowInteractor* cpExtensions::QT::SimpleMPRWidget::
292 GetInteractor( unsigned int i )
293 {
294   if( i < 4 )
295     return( this->m_VTK[ i ]->GetRenderWindow( )->GetInteractor( ) );
296   else
297     return( NULL );
298 }
299
300 // -------------------------------------------------------------------------
301 vtkRenderer* cpExtensions::QT::SimpleMPRWidget::
302 GetRenderer( unsigned int i )
303 {
304   if( i < 4 )
305     return( this->m_Renderers[ i ] );
306   else
307     return( NULL );
308 }
309
310 // -------------------------------------------------------------------------
311 vtkRenderWindow* cpExtensions::QT::SimpleMPRWidget::
312 GetRenderWindow( unsigned int i )
313 {
314   if( i < 4 )
315   {
316     if( this->m_Renderers[ i ].GetPointer( ) != NULL )
317       return( this->m_Renderers[ i ]->GetRenderWindow( ) );
318     else
319       return( NULL );
320   }
321   else
322     return( NULL );
323 }
324
325 // -------------------------------------------------------------------------
326 /* TODO
327    std::vector< std::pair< vtkImageActor*, vtkRenderer* > >
328    cpExtensions::QT::SimpleMPRWidget::
329    GetMainImageActors( )
330    {
331    std::vector< std::pair< vtkImageActor*, vtkRenderer* > > actors;
332    for( unsigned int i = 0; i < 3; ++i )
333    {
334    actors.push_back(
335    std::pair< vtkImageActor*, vtkRenderer* >(
336    this->m_2DSlices[ i ]->GetImageActor( ),
337    this->m_Renderers[ i ]
338    )
339    );
340    actors.push_back(
341    std::pair< vtkImageActor*, vtkRenderer* >(
342    this->m_3DSlices[ i ]->GetImageActor( ),
343    this->m_Renderers[ 3 ]
344    )
345    );
346
347    } // rof
348    return( actors );
349    }
350 */
351
352 // -------------------------------------------------------------------------
353 /* TODO
354    vtkActor* cpExtensions::QT::SimpleMPRWidget::
355    GetActor( vtkPolyData* mesh )
356    {
357    auto i = this->m_PolyDatas.find( mesh );
358    if( i != this->m_PolyDatas.end( ) )
359    return( i->second.Actor.GetPointer( ) );
360    else
361    return( NULL );
362    }
363 */
364
365 // -------------------------------------------------------------------------
366 void cpExtensions::QT::SimpleMPRWidget::
367 _AssociateSlices( )
368 {
369   for( unsigned int i = 0; i < 3; ++i )
370   {
371     for( unsigned int j = 0; j < 3; ++j )
372     {
373       if( i != j )
374         this->m_2DSlices[ i ]->AssociateSlice( this->m_2DSlices[ j ] );
375       this->m_2DSlices[ i ]->AssociateSlice( this->m_3DSlices[ j ] );
376
377     } // rof
378
379   } // rof
380   for( unsigned int i = 0; i < 3; ++i )
381   {
382     this->m_2DSlices[ i ]->GetStyle( )->
383       SetCurrentRenderer( this->m_Renderers[ i ] );
384     this->m_Renderers[ i ]->ResetCamera( );
385     this->m_VTK[ i ]->GetRenderWindow( )->Render( );
386
387   } // rof
388   this->m_Renderers[ 3 ]->ResetCamera( );
389   this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
390 }
391
392 // -------------------------------------------------------------------------
393 void cpExtensions::QT::SimpleMPRWidget::
394 _SyncBottom( int a, int b )
395 {
396   this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
397 }
398
399 // -------------------------------------------------------------------------
400 void cpExtensions::QT::SimpleMPRWidget::
401 _SyncTop( int a, int b )
402 {
403   this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
404 }
405
406 #endif // cpExtensions_QT4
407
408 // eof - $RCSfile$