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