]> 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     this->m_Renderers[ 3 ]->AddViewProp( actor );
244     this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
245     this->m_NamedActors[ name ].insert( actor );
246     if( this->m_Renderers[ 3 ]->GetViewProps( )->GetNumberOfItems( ) == 1 )
247     {
248       this->m_Renderers[ 3 ]->ResetCamera( );
249       this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
250
251     } // fi
252
253   } // fi
254 }
255
256 // -------------------------------------------------------------------------
257 void cpExtensions::QT::SimpleMPRWidget::
258 AddActor( vtkProp* actor, const std::string& name )
259 {
260   auto ia = dynamic_cast< vtkImageActor* >( actor );
261   if( ia == NULL )
262   {
263     auto ma = dynamic_cast< vtkProp3D* >( actor );
264     if( ma != NULL )
265       this->AddMeshActor( ma, name );
266   }
267   else
268     this->AddImageActor( ia, name );
269 }
270
271 // -------------------------------------------------------------------------
272 const cpExtensions::QT::SimpleMPRWidget::
273 TActors& cpExtensions::QT::SimpleMPRWidget::
274 GetActors( const std::string& name ) const
275 {
276   static const TActors empty;
277   auto i = this->m_NamedActors.find( name );
278   if( i != this->m_NamedActors.end( ) )
279     return( i->second );
280   else
281     return( empty );
282 }
283
284 // -------------------------------------------------------------------------
285 vtkRenderWindowInteractor* cpExtensions::QT::SimpleMPRWidget::
286 GetInteractor( unsigned int i )
287 {
288   if( i < 4 )
289     return( this->m_VTK[ i ]->GetRenderWindow( )->GetInteractor( ) );
290   else
291     return( NULL );
292 }
293
294 // -------------------------------------------------------------------------
295 vtkRenderer* cpExtensions::QT::SimpleMPRWidget::
296 GetRenderer( unsigned int i )
297 {
298   if( i < 4 )
299     return( this->m_Renderers[ i ] );
300   else
301     return( NULL );
302 }
303
304 // -------------------------------------------------------------------------
305 vtkRenderWindow* cpExtensions::QT::SimpleMPRWidget::
306 GetRenderWindow( unsigned int i )
307 {
308   if( i < 4 )
309   {
310     if( this->m_Renderers[ i ].GetPointer( ) != NULL )
311       return( this->m_Renderers[ i ]->GetRenderWindow( ) );
312     else
313       return( NULL );
314   }
315   else
316     return( NULL );
317 }
318
319 // -------------------------------------------------------------------------
320 /* TODO
321    std::vector< std::pair< vtkImageActor*, vtkRenderer* > >
322    cpExtensions::QT::SimpleMPRWidget::
323    GetMainImageActors( )
324    {
325    std::vector< std::pair< vtkImageActor*, vtkRenderer* > > actors;
326    for( unsigned int i = 0; i < 3; ++i )
327    {
328    actors.push_back(
329    std::pair< vtkImageActor*, vtkRenderer* >(
330    this->m_2DSlices[ i ]->GetImageActor( ),
331    this->m_Renderers[ i ]
332    )
333    );
334    actors.push_back(
335    std::pair< vtkImageActor*, vtkRenderer* >(
336    this->m_3DSlices[ i ]->GetImageActor( ),
337    this->m_Renderers[ 3 ]
338    )
339    );
340
341    } // rof
342    return( actors );
343    }
344 */
345
346 // -------------------------------------------------------------------------
347 /* TODO
348    vtkActor* cpExtensions::QT::SimpleMPRWidget::
349    GetActor( vtkPolyData* mesh )
350    {
351    auto i = this->m_PolyDatas.find( mesh );
352    if( i != this->m_PolyDatas.end( ) )
353    return( i->second.Actor.GetPointer( ) );
354    else
355    return( NULL );
356    }
357 */
358
359 // -------------------------------------------------------------------------
360 void cpExtensions::QT::SimpleMPRWidget::
361 _AssociateSlices( )
362 {
363   for( unsigned int i = 0; i < 3; ++i )
364   {
365     for( unsigned int j = 0; j < 3; ++j )
366     {
367       if( i != j )
368         this->m_2DSlices[ i ]->AssociateSlice( this->m_2DSlices[ j ] );
369       this->m_2DSlices[ i ]->AssociateSlice( this->m_3DSlices[ j ] );
370
371     } // rof
372
373   } // rof
374   for( unsigned int i = 0; i < 3; ++i )
375   {
376     this->m_2DSlices[ i ]->GetStyle( )->
377       SetCurrentRenderer( this->m_Renderers[ i ] );
378     this->m_Renderers[ i ]->ResetCamera( );
379     this->m_VTK[ i ]->GetRenderWindow( )->Render( );
380
381   } // rof
382   this->m_Renderers[ 3 ]->ResetCamera( );
383   this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
384 }
385
386 // -------------------------------------------------------------------------
387 void cpExtensions::QT::SimpleMPRWidget::
388 _SyncBottom( int a, int b )
389 {
390   this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
391 }
392
393 // -------------------------------------------------------------------------
394 void cpExtensions::QT::SimpleMPRWidget::
395 _SyncTop( int a, int b )
396 {
397   this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
398 }
399
400 #endif // cpExtensions_QT4
401
402 // eof - $RCSfile$