]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/QT/SimpleMPRWidget.cxx
f838a2ea704c118576580bd39897728d1f5277f1
[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( vtkActor* actor )
198 {
199   std::cerr << "SimpleMPR: adding mesh actor" << std::endl;
200   std::exit( 1 );
201 }
202
203 // -------------------------------------------------------------------------
204 void cpExtensions::QT::SimpleMPRWidget::
205 AddActor( vtkProp* actor )
206 {
207   auto ia = dynamic_cast< vtkImageActor* >( actor );
208   if( ia == NULL )
209   {
210     auto ma = dynamic_cast< vtkActor* >( actor );
211     if( ma != NULL )
212       this->AddMeshActor( ma );
213   }
214   else
215     this->AddImageActor( ia );
216 }
217
218 // -------------------------------------------------------------------------
219 vtkRenderWindowInteractor* cpExtensions::QT::SimpleMPRWidget::
220 GetInteractor( unsigned int i )
221 {
222   if( i < 4 )
223     return( this->m_VTK[ i ]->GetRenderWindow( )->GetInteractor( ) );
224   else
225     return( NULL );
226 }
227
228 // -------------------------------------------------------------------------
229 vtkRenderer* cpExtensions::QT::SimpleMPRWidget::
230 GetRenderer( unsigned int i )
231 {
232   if( i < 4 )
233     return( this->m_Renderers[ i ] );
234   else
235     return( NULL );
236 }
237
238 // -------------------------------------------------------------------------
239 vtkRenderWindow* cpExtensions::QT::SimpleMPRWidget::
240 GetRenderWindow( unsigned int i )
241 {
242   if( i < 4 )
243   {
244     if( this->m_Renderers[ i ].GetPointer( ) != NULL )
245       return( this->m_Renderers[ i ]->GetRenderWindow( ) );
246     else
247       return( NULL );
248   }
249   else
250     return( NULL );
251 }
252
253 // -------------------------------------------------------------------------
254 /* TODO
255    std::vector< std::pair< vtkImageActor*, vtkRenderer* > >
256    cpExtensions::QT::SimpleMPRWidget::
257    GetMainImageActors( )
258    {
259    std::vector< std::pair< vtkImageActor*, vtkRenderer* > > actors;
260    for( unsigned int i = 0; i < 3; ++i )
261    {
262    actors.push_back(
263    std::pair< vtkImageActor*, vtkRenderer* >(
264    this->m_2DSlices[ i ]->GetImageActor( ),
265    this->m_Renderers[ i ]
266    )
267    );
268    actors.push_back(
269    std::pair< vtkImageActor*, vtkRenderer* >(
270    this->m_3DSlices[ i ]->GetImageActor( ),
271    this->m_Renderers[ 3 ]
272    )
273    );
274
275    } // rof
276    return( actors );
277    }
278 */
279
280 // -------------------------------------------------------------------------
281 /* TODO
282    vtkActor* cpExtensions::QT::SimpleMPRWidget::
283    GetActor( vtkPolyData* mesh )
284    {
285    auto i = this->m_PolyDatas.find( mesh );
286    if( i != this->m_PolyDatas.end( ) )
287    return( i->second.Actor.GetPointer( ) );
288    else
289    return( NULL );
290    }
291 */
292
293 // -------------------------------------------------------------------------
294 void cpExtensions::QT::SimpleMPRWidget::
295 _AssociateSlices( )
296 {
297   for( unsigned int i = 0; i < 3; ++i )
298   {
299     for( unsigned int j = 0; j < 3; ++j )
300     {
301       if( i != j )
302         this->m_2DSlices[ i ]->AssociateSlice( this->m_2DSlices[ j ] );
303       this->m_2DSlices[ i ]->AssociateSlice( this->m_3DSlices[ j ] );
304
305     } // rof
306
307   } // rof
308   for( unsigned int i = 0; i < 3; ++i )
309   {
310     this->m_2DSlices[ i ]->GetStyle( )->
311       SetCurrentRenderer( this->m_Renderers[ i ] );
312     this->m_Renderers[ i ]->ResetCamera( );
313     this->m_VTK[ i ]->GetRenderWindow( )->Render( );
314
315   } // rof
316   this->m_Renderers[ 3 ]->ResetCamera( );
317   this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
318 }
319
320 // -------------------------------------------------------------------------
321 void cpExtensions::QT::SimpleMPRWidget::
322 _SyncBottom( int a, int b )
323 {
324   this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
325 }
326
327 // -------------------------------------------------------------------------
328 void cpExtensions::QT::SimpleMPRWidget::
329 _SyncTop( int a, int b )
330 {
331   this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
332 }
333
334 #endif // cpExtensions_QT4
335
336 // eof - $RCSfile$