1 #include <cpExtensions/QT/SimpleMPRWidget.h>
3 #ifdef cpExtensions_QT4
5 #include <cpExtensions/ui_SimpleMPRWidget.h>
6 #include <vtkRenderer.h>
7 #include <vtkRenderWindow.h>
9 // -------------------------------------------------------------------------
10 cpExtensions::QT::SimpleMPRWidget::_3DInteractorStyle*
11 cpExtensions::QT::SimpleMPRWidget::_3DInteractorStyle::
14 return( new _3DInteractorStyle( ) );
17 // -------------------------------------------------------------------------
18 void cpExtensions::QT::SimpleMPRWidget::_3DInteractorStyle::
21 vtkRenderWindowInteractor* i = this->Interactor;
22 std::string key = i->GetKeySym( );
23 if( key == "r" || key == "R" )
25 this->CurrentRenderer->ResetCamera( );
31 // -------------------------------------------------------------------------
32 cpExtensions::QT::SimpleMPRWidget::
33 SimpleMPRWidget( QWidget* parent )
35 m_UI( new Ui::SimpleMPRWidget )
37 this->m_UI->setupUi( this );
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 )
46 this->m_Renderers[ i ] = vtkSmartPointer< vtkRenderer >::New( );
47 this->m_VTK[ i ]->GetRenderWindow( )->
48 AddRenderer( this->m_Renderers[ i ] );
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 ] );
59 this->m_UI->TopSplitter, SIGNAL( splitterMoved( int, int ) ),
60 this, SLOT( _SyncBottom( int, int ) )
63 this->m_UI->BottomSplitter, SIGNAL( splitterMoved( int, int ) ),
64 this, SLOT( _SyncTop( int, int ) )
68 // -------------------------------------------------------------------------
69 cpExtensions::QT::SimpleMPRWidget::
76 // -------------------------------------------------------------------------
77 void cpExtensions::QT::SimpleMPRWidget::
80 for( unsigned int i = 0; i < 4; ++i )
81 this->m_Renderers[ i ]->RemoveAllViewProps( );
82 for( unsigned int i = 0; i < 3; ++i )
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( );
90 this->m_NamedActors.clear( );
93 // -------------------------------------------------------------------------
94 void cpExtensions::QT::SimpleMPRWidget::
95 SetMainImage( vtkImageData* image, const std::string& name )
98 for( unsigned int i = 0; i < 3; ++i )
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( ) );
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( )
116 this->m_NamedActors[ name ].insert(
117 this->m_2DSlices[ i ]->GetImageActor( )
119 this->m_NamedActors[ name ].insert(
120 this->m_3DSlices[ i ]->GetImageActor( )
124 this->_AssociateSlices( );
127 // -------------------------------------------------------------------------
128 void cpExtensions::QT::SimpleMPRWidget::
129 SetMainActor( vtkImageActor* actor, const std::string& name )
132 vtkImageData* image = NULL;
133 for( unsigned int i = 0; i < 3; ++i )
135 this->m_2DSlices[ i ] = vtkSmartPointer< TSliceActors >::New( );
138 this->m_2DSlices[ i ]->SetInputActor( actor, i );
139 image = this->m_2DSlices[ i ]->GetInputData( );
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( ) );
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( )
157 this->m_NamedActors[ name ].insert(
158 this->m_2DSlices[ i ]->GetImageActor( )
160 this->m_NamedActors[ name ].insert(
161 this->m_3DSlices[ i ]->GetImageActor( )
165 this->_AssociateSlices( );
168 // -------------------------------------------------------------------------
169 void cpExtensions::QT::SimpleMPRWidget::
170 AddImage( vtkImageData* image, const std::string& name )
172 std::cerr << "SimpleMPR: adding image" << std::endl;
176 // -------------------------------------------------------------------------
177 void cpExtensions::QT::SimpleMPRWidget::
178 AddImageActor( vtkImageActor* actor, const std::string& name )
182 while( i < 3 && cont )
184 if( this->m_2DSlices[ i ].GetPointer( ) != NULL )
186 if( this->m_2DSlices[ i ]->GetImageActor( ) != NULL )
188 // TODO: add image actor
198 // Add if no actors were found
200 this->SetMainActor( actor, name );
203 // -------------------------------------------------------------------------
204 void cpExtensions::QT::SimpleMPRWidget::
205 AddMesh( vtkPolyData* mesh, const std::string& name )
207 std::cerr << "SimpleMPR: adding mesh" << std::endl;
212 this->m_PolyDatas[ mesh ] = a;
213 this->m_Renderers[ 3 ]->AddViewProp( a.Actor );
215 bool has_main_image = false;
216 for( unsigned int i = 0; i < 3; ++i )
218 if( this->m_2DSlices[ i ].GetPointer( ) != NULL )
220 if( this->m_2DSlices[ i ]->GetInputData( ) != NULL )
222 has_main_image = true;
223 this->m_2DSlices[ i ]->AddMesh( mesh );
224 this->m_2DSlices[ i ]->Render( );
231 if( !has_main_image )
232 this->m_Renderers[ 3 ]->ResetCamera( );
233 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
237 // -------------------------------------------------------------------------
238 void cpExtensions::QT::SimpleMPRWidget::
239 AddMeshActor( vtkProp3D* actor, const std::string& name )
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 )
248 this->m_Renderers[ 3 ]->ResetCamera( );
249 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
256 // -------------------------------------------------------------------------
257 void cpExtensions::QT::SimpleMPRWidget::
258 AddActor( vtkProp* actor, const std::string& name )
260 auto ia = dynamic_cast< vtkImageActor* >( actor );
263 auto ma = dynamic_cast< vtkProp3D* >( actor );
265 this->AddMeshActor( ma, name );
268 this->AddImageActor( ia, name );
271 // -------------------------------------------------------------------------
272 const cpExtensions::QT::SimpleMPRWidget::
273 TActors& cpExtensions::QT::SimpleMPRWidget::
274 GetActors( const std::string& name ) const
276 static const TActors empty;
277 auto i = this->m_NamedActors.find( name );
278 if( i != this->m_NamedActors.end( ) )
284 // -------------------------------------------------------------------------
285 vtkRenderWindowInteractor* cpExtensions::QT::SimpleMPRWidget::
286 GetInteractor( unsigned int i )
289 return( this->m_VTK[ i ]->GetRenderWindow( )->GetInteractor( ) );
294 // -------------------------------------------------------------------------
295 vtkRenderer* cpExtensions::QT::SimpleMPRWidget::
296 GetRenderer( unsigned int i )
299 return( this->m_Renderers[ i ] );
304 // -------------------------------------------------------------------------
305 vtkRenderWindow* cpExtensions::QT::SimpleMPRWidget::
306 GetRenderWindow( unsigned int i )
310 if( this->m_Renderers[ i ].GetPointer( ) != NULL )
311 return( this->m_Renderers[ i ]->GetRenderWindow( ) );
319 // -------------------------------------------------------------------------
321 std::vector< std::pair< vtkImageActor*, vtkRenderer* > >
322 cpExtensions::QT::SimpleMPRWidget::
323 GetMainImageActors( )
325 std::vector< std::pair< vtkImageActor*, vtkRenderer* > > actors;
326 for( unsigned int i = 0; i < 3; ++i )
329 std::pair< vtkImageActor*, vtkRenderer* >(
330 this->m_2DSlices[ i ]->GetImageActor( ),
331 this->m_Renderers[ i ]
335 std::pair< vtkImageActor*, vtkRenderer* >(
336 this->m_3DSlices[ i ]->GetImageActor( ),
337 this->m_Renderers[ 3 ]
346 // -------------------------------------------------------------------------
348 vtkActor* cpExtensions::QT::SimpleMPRWidget::
349 GetActor( vtkPolyData* mesh )
351 auto i = this->m_PolyDatas.find( mesh );
352 if( i != this->m_PolyDatas.end( ) )
353 return( i->second.Actor.GetPointer( ) );
359 // -------------------------------------------------------------------------
360 void cpExtensions::QT::SimpleMPRWidget::
363 for( unsigned int i = 0; i < 3; ++i )
365 for( unsigned int j = 0; j < 3; ++j )
368 this->m_2DSlices[ i ]->AssociateSlice( this->m_2DSlices[ j ] );
369 this->m_2DSlices[ i ]->AssociateSlice( this->m_3DSlices[ j ] );
374 for( unsigned int i = 0; i < 3; ++i )
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( );
382 this->m_Renderers[ 3 ]->ResetCamera( );
383 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
386 // -------------------------------------------------------------------------
387 void cpExtensions::QT::SimpleMPRWidget::
388 _SyncBottom( int a, int b )
390 this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
393 // -------------------------------------------------------------------------
394 void cpExtensions::QT::SimpleMPRWidget::
395 _SyncTop( int a, int b )
397 this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
400 #endif // cpExtensions_QT4