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