1 #include <cpExtensions/Interaction/ImageInteractorStyle.h>
4 #include <vtkCommand.h>
5 #include <vtkImageActor.h>
6 #include <vtkImageMapper3D.h>
7 #include <vtkImageSliceMapper.h>
8 #include <vtkPropCollection.h>
9 #include <vtkProperty.h>
10 #include <vtkRenderWindowInteractor.h>
11 #include <vtkRenderer.h>
13 // -------------------------------------------------------------------------
15 cpExtensions::Interaction::ImageInteractorStyle::TSeedWidget::
16 TSeedWidget( vtkRenderWindowInteractor* interactor, vtkImageActor* actor )
18 this->Placer = vtkSmartPointer< vtkImageActorPointPlacer >::New( );
19 this->Handle = vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
20 this->Representation = vtkSmartPointer< vtkSeedRepresentation >::New( );
21 this->Widget = vtkSmartPointer< SeedWidget >::New( );
23 this->Placer->SetImageActor( actor );
24 this->Handle->GetProperty( )->SetColor( 1, 0, 0 );
25 this->Handle->SetPointPlacer( this->Placer );
26 this->Representation->SetHandleRepresentation( this->Handle );
27 this->Widget->SetRepresentation( this->Representation );
28 this->Widget->SetInteractor( interactor );
31 // -------------------------------------------------------------------------
32 void cpExtensions::Interaction::ImageInteractorStyle::TSeedWidget::
38 // -------------------------------------------------------------------------
39 void cpExtensions::Interaction::ImageInteractorStyle::TSeedWidget::
46 // -------------------------------------------------------------------------
47 cpExtensions::Interaction::ImageInteractorStyle::
48 Self* cpExtensions::Interaction::ImageInteractorStyle::
54 // -------------------------------------------------------------------------
55 void cpExtensions::Interaction::ImageInteractorStyle::
56 SetCurrentRenderer( vtkRenderer* r )
58 this->Superclass::SetCurrentRenderer( r );
60 if( !( this->m_Configured ) )
62 auto lst = this->m_PropPicker->GetPickList( );
65 if( lst->GetNumberOfItems( ) == 1 )
67 auto actor = dynamic_cast< vtkImageActor* >( lst->GetItemAsObject( 0 ) );
70 this->_ConfigureCamera( actor );
80 // -------------------------------------------------------------------------
81 void cpExtensions::Interaction::ImageInteractorStyle::
82 AssociateImageActor( vtkImageActor* actor )
87 this->m_PropPicker->GetPickList( )->RemoveAllItems( );
88 this->m_PropPicker->AddPickList( actor );
89 this->m_Configured = false;
90 this->_ConfigureCamera( actor );
98 // -------------------------------------------------------------------------
100 void cpExtensions::Interaction::ImageInteractorStyle::
103 if( this->m_SeedWidget != NULL ) delete this->m_SeedWidget;
105 this->m_PropPicker->GetPickList( )->InitTraversal( );
106 this->m_SeedWidget = new TSeedWidget(
108 dynamic_cast< vtkImageActor* >(
109 this->m_PropPicker->GetPickList( )->GetNextProp( )
112 this->m_SeedWidget->On( );
115 // -------------------------------------------------------------------------
116 void cpExtensions::Interaction::ImageInteractorStyle::
119 if( this->m_SeedWidget != NULL )
121 this->m_SeedWidget->Off( );
122 delete this->m_SeedWidget;
123 this->m_SeedWidget = NULL;
129 // -------------------------------------------------------------------------
131 void cpExtensions::Interaction::ImageInteractorStyle::
132 SetSeedWidgetCommand( vtkCommand* cmd )
134 if( cmd == NULL || this->m_SeedWidget == NULL )
136 this->m_SeedWidget->Widget->
137 AddObserver( vtkCommand::PlacePointEvent, cmd );
140 // -------------------------------------------------------------------------
141 unsigned int cpExtensions::Interaction::ImageInteractorStyle::
142 GetNumberOfSeeds( ) const
144 if( this->m_SeedWidget != NULL )
145 return( this->m_SeedWidget->Representation->GetNumberOfSeeds( ) );
150 // -------------------------------------------------------------------------
151 void cpExtensions::Interaction::ImageInteractorStyle::
152 GetSeedAsPoint( unsigned int id, double pos[ 3 ] ) const
154 if( this->m_SeedWidget != NULL )
155 this->m_SeedWidget->Representation->GetSeedWorldPosition( id, pos );
158 // -------------------------------------------------------------------------
159 void cpExtensions::Interaction::ImageInteractorStyle::
160 GetSeedAsIndex( unsigned int id, int idx[ 3 ] ) const
162 if( this->m_SeedWidget != NULL )
163 this->m_SeedWidget->Representation->GetSeedWorldPosition( id, pos );
167 // -------------------------------------------------------------------------
168 cpExtensions::Interaction::ImageInteractorStyle::
169 ImageInteractorStyle( )
171 m_Configured( false )
177 this->m_PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
178 this->m_PropPicker->PickFromListOn( );
181 // -------------------------------------------------------------------------
182 cpExtensions::Interaction::ImageInteractorStyle::
183 ~ImageInteractorStyle( )
185 // if( this->m_SeedWidget != NULL ) delete this->m_SeedWidget;
188 // -------------------------------------------------------------------------
189 void cpExtensions::Interaction::ImageInteractorStyle::
190 _ConfigureCamera( vtkImageActor* actor )
192 if( this->CurrentRenderer != NULL )
195 auto camera = this->CurrentRenderer->GetActiveCamera( );
196 auto mapper = dynamic_cast< vtkImageSliceMapper* >( actor->GetMapper( ) );
197 if( mapper != NULL && camera != NULL )
200 actor->GetBounds( bounds );
201 camera->ParallelProjectionOn( );
202 camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
203 int orientation = mapper->GetOrientation( );
204 switch( orientation )
208 camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
209 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
214 camera->SetPosition( double( 0 ), double( 1 ), double( 0 ) );
215 camera->SetViewUp ( double( 0 ), double( 0 ), double( -1 ) );
220 camera->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
221 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
227 this->CurrentRenderer->ResetCamera( );
228 this->m_Configured = true;
235 // -------------------------------------------------------------------------
236 bool cpExtensions::Interaction::ImageInteractorStyle::
237 _PickPosition( int idx[ 2 ], double pos[ 3 ] )
239 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
243 // Find the renderer where the event has been raised
244 idx[ 0 ] = rwi->GetEventPosition( )[ 0 ];
245 idx[ 1 ] = rwi->GetEventPosition( )[ 1 ];
246 this->FindPokedRenderer( double( idx[ 0 ] ), double( idx[ 1 ] ) );
248 // Pick a 3D position
249 int r = this->m_PropPicker->Pick(
250 double( idx[ 0 ] ), double( idx[ 1 ] ), double( 0 ),
251 this->CurrentRenderer
255 this->m_PropPicker->GetPickPosition( pos );