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 void cpExtensions::QT::SimpleMPRWidget::
292 SetWindowLevel( double win, double lev )
294 for( unsigned int i = 0; i < 3; ++i )
296 if( this->m_2DSlices[ i ].GetPointer( ) != NULL )
298 this->m_2DSlices[ i ]->SetWindowLevel( win, lev );
299 this->m_2DSlices[ i ]->Render( );
302 if( this->m_3DSlices[ i ].GetPointer( ) != NULL )
304 this->m_3DSlices[ i ]->SetWindowLevel( win, lev );
305 this->m_3DSlices[ i ]->Render( );
312 // -------------------------------------------------------------------------
313 void cpExtensions::QT::SimpleMPRWidget::
318 // -------------------------------------------------------------------------
319 vtkRenderWindowInteractor* cpExtensions::QT::SimpleMPRWidget::
320 GetInteractor( unsigned int i )
323 return( this->m_VTK[ i ]->GetRenderWindow( )->GetInteractor( ) );
328 // -------------------------------------------------------------------------
329 vtkRenderer* cpExtensions::QT::SimpleMPRWidget::
330 GetRenderer( unsigned int i )
333 return( this->m_Renderers[ i ] );
338 // -------------------------------------------------------------------------
339 vtkRenderWindow* cpExtensions::QT::SimpleMPRWidget::
340 GetRenderWindow( unsigned int i )
344 if( this->m_Renderers[ i ].GetPointer( ) != NULL )
345 return( this->m_Renderers[ i ]->GetRenderWindow( ) );
353 // -------------------------------------------------------------------------
355 std::vector< std::pair< vtkImageActor*, vtkRenderer* > >
356 cpExtensions::QT::SimpleMPRWidget::
357 GetMainImageActors( )
359 std::vector< std::pair< vtkImageActor*, vtkRenderer* > > actors;
360 for( unsigned int i = 0; i < 3; ++i )
363 std::pair< vtkImageActor*, vtkRenderer* >(
364 this->m_2DSlices[ i ]->GetImageActor( ),
365 this->m_Renderers[ i ]
369 std::pair< vtkImageActor*, vtkRenderer* >(
370 this->m_3DSlices[ i ]->GetImageActor( ),
371 this->m_Renderers[ 3 ]
380 // -------------------------------------------------------------------------
382 vtkActor* cpExtensions::QT::SimpleMPRWidget::
383 GetActor( vtkPolyData* mesh )
385 auto i = this->m_PolyDatas.find( mesh );
386 if( i != this->m_PolyDatas.end( ) )
387 return( i->second.Actor.GetPointer( ) );
393 // -------------------------------------------------------------------------
394 void cpExtensions::QT::SimpleMPRWidget::
397 for( unsigned int i = 0; i < 3; ++i )
399 for( unsigned int j = 0; j < 3; ++j )
402 this->m_2DSlices[ i ]->AssociateSlice( this->m_2DSlices[ j ] );
403 this->m_2DSlices[ i ]->AssociateSlice( this->m_3DSlices[ j ] );
408 for( unsigned int i = 0; i < 3; ++i )
410 this->m_2DSlices[ i ]->GetStyle( )->
411 SetCurrentRenderer( this->m_Renderers[ i ] );
412 this->m_Renderers[ i ]->ResetCamera( );
413 this->m_VTK[ i ]->GetRenderWindow( )->Render( );
416 this->m_Renderers[ 3 ]->ResetCamera( );
417 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
420 // -------------------------------------------------------------------------
421 void cpExtensions::QT::SimpleMPRWidget::
422 _SyncBottom( int a, int b )
424 this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
427 // -------------------------------------------------------------------------
428 void cpExtensions::QT::SimpleMPRWidget::
429 _SyncTop( int a, int b )
431 this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
434 #endif // cpExtensions_QT4