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