1 #include <cpExtensions/QT/SimpleMPRWidget.h>
3 #ifdef cpExtensions_QT4
5 #include <cpExtensions/QT/ui_SimpleMPRWidget.h>
6 #include <vtkImageProperty.h>
7 #include <vtkRenderer.h>
8 #include <vtkGenericOpenGLRenderWindow.h>
10 // -------------------------------------------------------------------------
11 cpExtensions::QT::SimpleMPRWidget::_3DInteractorStyle*
12 cpExtensions::QT::SimpleMPRWidget::_3DInteractorStyle::
15 return( new _3DInteractorStyle( ) );
18 // -------------------------------------------------------------------------
19 void cpExtensions::QT::SimpleMPRWidget::_3DInteractorStyle::
22 vtkRenderWindowInteractor* i = this->Interactor;
23 std::string key = i->GetKeySym( );
24 if( key == "r" || key == "R" )
26 this->CurrentRenderer->ResetCamera( );
32 // -------------------------------------------------------------------------
33 cpExtensions::QT::SimpleMPRWidget::
34 SimpleMPRWidget( QWidget* parent )
36 m_UI( new Ui::SimpleMPRWidget )
38 this->m_UI->setupUi( this );
40 // Configure VTK widgets
41 this->m_VTK[ 0 ] = this->m_UI->VTK_1;
42 this->m_VTK[ 1 ] = this->m_UI->VTK_2;
43 this->m_VTK[ 2 ] = this->m_UI->VTK_4;
44 this->m_VTK[ 3 ] = this->m_UI->VTK_3;
45 for( unsigned int i = 0; i < 4; ++i )
47 this->m_Renderers[ i ] = vtkSmartPointer< vtkRenderer >::New( );
48 this->m_VTK[ i ]->GetRenderWindow( )->
49 AddRenderer( this->m_Renderers[ i ] );
52 vtkSmartPointer< _3DInteractorStyle > style =
53 vtkSmartPointer< _3DInteractorStyle >::New( );
54 this->m_VTK[ 3 ]->GetRenderWindow( )->
55 GetInteractor( )->SetInteractorStyle( style );
56 style->SetCurrentRenderer( this->m_Renderers[ 3 ] );
60 this->m_UI->TopSplitter, SIGNAL( splitterMoved( int, int ) ),
61 this, SLOT( _SyncBottom( int, int ) )
64 this->m_UI->BottomSplitter, SIGNAL( splitterMoved( int, int ) ),
65 this, SLOT( _SyncTop( int, int ) )
69 // -------------------------------------------------------------------------
70 cpExtensions::QT::SimpleMPRWidget::
77 // -------------------------------------------------------------------------
78 void cpExtensions::QT::SimpleMPRWidget::
81 for( unsigned int i = 0; i < 4; ++i )
82 this->m_Renderers[ i ]->RemoveAllViewProps( );
83 for( unsigned int i = 0; i < 3; ++i )
85 if( this->m_2DSlices[ i ].GetPointer( ) != NULL )
86 this->m_2DSlices[ i ]->Clear( );
87 if( this->m_3DSlices[ i ].GetPointer( ) != NULL )
88 this->m_3DSlices[ i ]->Clear( );
91 this->m_NamedActors.clear( );
94 // -------------------------------------------------------------------------
95 void cpExtensions::QT::SimpleMPRWidget::
96 SetMainImage( vtkImageData* image, const std::string& name )
99 for( unsigned int i = 0; i < 3; ++i )
101 this->m_2DSlices[ i ] = vtkSmartPointer< TSliceActors >::New( );
102 this->m_2DSlices[ i ]->SetInputData( image, i );
103 this->m_2DSlices[ i ]->PushInto( this->m_Renderers[ i ] );
104 this->m_VTK[ i ]->GetRenderWindow( )->GetInteractor( )->
105 SetInteractorStyle( this->m_2DSlices[ i ]->GetStyle( ) );
107 this->m_3DSlices[ i ] = vtkSmartPointer< TSliceActors >::New( );
108 this->m_3DSlices[ i ]->SetInputData( image, i );
109 this->m_3DSlices[ i ]->PushInto( this->m_Renderers[ 3 ] );
110 this->m_3DSlices[ i ]->SetStyle(
111 dynamic_cast< vtkInteractorStyle* >(
112 this->m_Renderers[ 3 ]->GetRenderWindow( )->
113 GetInteractor( )->GetInteractorStyle( )
117 this->m_NamedActors[ name ].insert(
118 this->m_2DSlices[ i ]->GetImageActor( )
120 this->m_NamedActors[ name ].insert(
121 this->m_3DSlices[ i ]->GetImageActor( )
125 this->_AssociateSlices( );
128 // -------------------------------------------------------------------------
129 void cpExtensions::QT::SimpleMPRWidget::
130 SetMainActor( vtkImageActor* actor, const std::string& name )
133 vtkImageData* image = NULL;
134 int interp = actor->GetProperty( )->GetInterpolationType( );
135 for( unsigned int i = 0; i < 3; ++i )
137 this->m_2DSlices[ i ] = vtkSmartPointer< TSliceActors >::New( );
140 this->m_2DSlices[ i ]->SetInputActor( actor, i );
141 image = this->m_2DSlices[ i ]->GetInputData( );
144 this->m_2DSlices[ i ]->SetInputData( image, i );
145 this->m_2DSlices[ i ]->PushInto( this->m_Renderers[ i ] );
146 this->m_VTK[ i ]->GetRenderWindow( )->GetInteractor( )->
147 SetInteractorStyle( this->m_2DSlices[ i ]->GetStyle( ) );
149 this->m_3DSlices[ i ] = vtkSmartPointer< TSliceActors >::New( );
150 this->m_3DSlices[ i ]->SetInputData( image, i );
151 this->m_3DSlices[ i ]->PushInto( this->m_Renderers[ 3 ] );
152 this->m_3DSlices[ i ]->SetStyle(
153 dynamic_cast< vtkInteractorStyle* >(
154 this->m_Renderers[ 3 ]->GetRenderWindow( )->
155 GetInteractor( )->GetInteractorStyle( )
159 this->m_NamedActors[ name ].insert(
160 this->m_2DSlices[ i ]->GetImageActor( )
162 this->m_NamedActors[ name ].insert(
163 this->m_3DSlices[ i ]->GetImageActor( )
165 this->m_2DSlices[ i ]->GetImageActor( )->
166 GetProperty( )->SetInterpolationType( interp );
167 this->m_3DSlices[ i ]->GetImageActor( )->
168 GetProperty( )->SetInterpolationType( interp );
171 this->_AssociateSlices( );
174 // -------------------------------------------------------------------------
175 void cpExtensions::QT::SimpleMPRWidget::
176 AddImage( vtkImageData* image, const std::string& name )
178 std::cerr << "SimpleMPR: adding image" << std::endl;
182 // -------------------------------------------------------------------------
183 void cpExtensions::QT::SimpleMPRWidget::
184 AddImageActor( vtkImageActor* actor, const std::string& name )
188 while( i < 3 && cont )
190 if( this->m_2DSlices[ i ].GetPointer( ) != NULL )
192 if( this->m_2DSlices[ i ]->GetImageActor( ) != NULL )
194 // TODO: add image actor
204 // Add if no actors were found
206 this->SetMainActor( actor, name );
209 // -------------------------------------------------------------------------
210 void cpExtensions::QT::SimpleMPRWidget::
211 AddMesh( vtkPolyData* mesh, const std::string& name )
213 std::cerr << "SimpleMPR: adding mesh" << std::endl;
218 this->m_PolyDatas[ mesh ] = a;
219 this->m_Renderers[ 3 ]->AddViewProp( a.Actor );
221 bool has_main_image = false;
222 for( unsigned int i = 0; i < 3; ++i )
224 if( this->m_2DSlices[ i ].GetPointer( ) != NULL )
226 if( this->m_2DSlices[ i ]->GetInputData( ) != NULL )
228 has_main_image = true;
229 this->m_2DSlices[ i ]->AddMesh( mesh );
230 this->m_2DSlices[ i ]->Render( );
237 if( !has_main_image )
238 this->m_Renderers[ 3 ]->ResetCamera( );
239 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
243 // -------------------------------------------------------------------------
244 void cpExtensions::QT::SimpleMPRWidget::
245 AddMeshActor( vtkProp3D* actor, const std::string& name )
249 this->m_Renderers[ 3 ]->AddViewProp( actor );
250 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
251 this->m_NamedActors[ name ].insert( actor );
252 if( this->m_Renderers[ 3 ]->GetViewProps( )->GetNumberOfItems( ) == 1 )
254 this->m_Renderers[ 3 ]->ResetCamera( );
255 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
262 // -------------------------------------------------------------------------
263 void cpExtensions::QT::SimpleMPRWidget::
264 AddActor( vtkProp* actor, const std::string& name )
266 auto ia = dynamic_cast< vtkImageActor* >( actor );
269 auto ma = dynamic_cast< vtkProp3D* >( actor );
271 this->AddMeshActor( ma, name );
274 this->AddImageActor( ia, name );
277 // -------------------------------------------------------------------------
278 const cpExtensions::QT::SimpleMPRWidget::
279 TActors& cpExtensions::QT::SimpleMPRWidget::
280 GetActors( const std::string& name ) const
282 static const TActors empty;
283 auto i = this->m_NamedActors.find( name );
284 if( i != this->m_NamedActors.end( ) )
290 // -------------------------------------------------------------------------
291 vtkRenderWindowInteractor* cpExtensions::QT::SimpleMPRWidget::
292 GetInteractor( unsigned int i )
295 return( this->m_VTK[ i ]->GetRenderWindow( )->GetInteractor( ) );
300 // -------------------------------------------------------------------------
301 vtkRenderer* cpExtensions::QT::SimpleMPRWidget::
302 GetRenderer( unsigned int i )
305 return( this->m_Renderers[ i ] );
310 // -------------------------------------------------------------------------
311 vtkRenderWindow* cpExtensions::QT::SimpleMPRWidget::
312 GetRenderWindow( unsigned int i )
316 if( this->m_Renderers[ i ].GetPointer( ) != NULL )
317 return( this->m_Renderers[ i ]->GetRenderWindow( ) );
325 // -------------------------------------------------------------------------
327 std::vector< std::pair< vtkImageActor*, vtkRenderer* > >
328 cpExtensions::QT::SimpleMPRWidget::
329 GetMainImageActors( )
331 std::vector< std::pair< vtkImageActor*, vtkRenderer* > > actors;
332 for( unsigned int i = 0; i < 3; ++i )
335 std::pair< vtkImageActor*, vtkRenderer* >(
336 this->m_2DSlices[ i ]->GetImageActor( ),
337 this->m_Renderers[ i ]
341 std::pair< vtkImageActor*, vtkRenderer* >(
342 this->m_3DSlices[ i ]->GetImageActor( ),
343 this->m_Renderers[ 3 ]
352 // -------------------------------------------------------------------------
354 vtkActor* cpExtensions::QT::SimpleMPRWidget::
355 GetActor( vtkPolyData* mesh )
357 auto i = this->m_PolyDatas.find( mesh );
358 if( i != this->m_PolyDatas.end( ) )
359 return( i->second.Actor.GetPointer( ) );
365 // -------------------------------------------------------------------------
366 void cpExtensions::QT::SimpleMPRWidget::
369 for( unsigned int i = 0; i < 3; ++i )
371 for( unsigned int j = 0; j < 3; ++j )
374 this->m_2DSlices[ i ]->AssociateSlice( this->m_2DSlices[ j ] );
375 this->m_2DSlices[ i ]->AssociateSlice( this->m_3DSlices[ j ] );
380 for( unsigned int i = 0; i < 3; ++i )
382 this->m_2DSlices[ i ]->GetStyle( )->
383 SetCurrentRenderer( this->m_Renderers[ i ] );
384 this->m_Renderers[ i ]->ResetCamera( );
385 this->m_VTK[ i ]->GetRenderWindow( )->Render( );
388 this->m_Renderers[ 3 ]->ResetCamera( );
389 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
392 // -------------------------------------------------------------------------
393 void cpExtensions::QT::SimpleMPRWidget::
394 _SyncBottom( int a, int b )
396 this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
399 // -------------------------------------------------------------------------
400 void cpExtensions::QT::SimpleMPRWidget::
401 _SyncTop( int a, int b )
403 this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
406 #endif // cpExtensions_QT4