]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Interaction/ImageInteractorStyle.cxx
First dump for version 0.1.0
[cpPlugins.git] / lib / cpExtensions / Interaction / ImageInteractorStyle.cxx
1 #include <cpExtensions/Interaction/ImageInteractorStyle.h>
2
3 #include <vtkCommand.h>
4 #include <vtkImageActor.h>
5 #include <vtkPropCollection.h>
6 #include <vtkProperty.h>
7 #include <vtkRenderWindowInteractor.h>
8
9 // -------------------------------------------------------------------------
10 cpExtensions::Interaction::ImageInteractorStyle::TSeedWidget::
11 TSeedWidget( vtkRenderWindowInteractor* interactor, vtkImageActor* actor )
12 {
13   this->Placer = vtkSmartPointer< vtkImageActorPointPlacer >::New( );
14   this->Handle = vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
15   this->Representation = vtkSmartPointer< vtkSeedRepresentation >::New( );
16   this->Widget = vtkSmartPointer< SeedWidget >::New( );
17
18   this->Placer->SetImageActor( actor );
19   this->Handle->GetProperty( )->SetColor( 1, 0, 0 );
20   this->Handle->SetPointPlacer( this->Placer );
21   this->Representation->SetHandleRepresentation( this->Handle );
22   this->Widget->SetRepresentation( this->Representation );
23   this->Widget->SetInteractor( interactor );
24 }
25
26 // -------------------------------------------------------------------------
27 void cpExtensions::Interaction::ImageInteractorStyle::TSeedWidget::
28 On( )
29 {
30   this->Widget->On( );
31 }
32
33 // -------------------------------------------------------------------------
34 void cpExtensions::Interaction::ImageInteractorStyle::TSeedWidget::
35 Off( )
36 {
37   this->Widget->Off( );
38 }
39
40 // -------------------------------------------------------------------------
41 cpExtensions::Interaction::ImageInteractorStyle::
42 Self* cpExtensions::Interaction::ImageInteractorStyle::
43 New( )
44 {
45   return( new Self );
46 }
47
48 // -------------------------------------------------------------------------
49 void cpExtensions::Interaction::ImageInteractorStyle::
50 AssociateImageActor( vtkImageActor* actor )
51 {
52   if( actor != NULL )
53   {
54     this->m_PropPicker->AddPickList( actor );
55     this->Modified( );
56
57   } // fi
58 }
59
60 // -------------------------------------------------------------------------
61 void cpExtensions::Interaction::ImageInteractorStyle::
62 SeedWidgetOn( )
63 {
64   if( this->m_SeedWidget != NULL ) delete this->m_SeedWidget;
65
66   this->m_PropPicker->GetPickList( )->InitTraversal( );
67   this->m_SeedWidget = new TSeedWidget(
68     this->Interactor,
69     dynamic_cast< vtkImageActor* >(
70       this->m_PropPicker->GetPickList( )->GetNextProp( )
71       )
72     );
73   this->m_SeedWidget->On( );
74 }
75
76 // -------------------------------------------------------------------------
77 void cpExtensions::Interaction::ImageInteractorStyle::
78 SeedWidgetOff( )
79 {
80   if( this->m_SeedWidget != NULL )
81   {
82     this->m_SeedWidget->Off( );
83     delete this->m_SeedWidget;
84     this->m_SeedWidget = NULL;
85
86   } // fi
87 }
88
89 // -------------------------------------------------------------------------
90 void cpExtensions::Interaction::ImageInteractorStyle::
91 SetSeedWidgetCommand( vtkCommand* cmd )
92 {
93   if( cmd == NULL || this->m_SeedWidget == NULL )
94     return;
95   this->m_SeedWidget->Widget->
96     AddObserver( vtkCommand::PlacePointEvent, cmd );
97 }
98
99 // -------------------------------------------------------------------------
100 unsigned int cpExtensions::Interaction::ImageInteractorStyle::
101 GetNumberOfSeeds( ) const
102 {
103   if( this->m_SeedWidget != NULL )
104     return( this->m_SeedWidget->Representation->GetNumberOfSeeds( ) );
105   else
106     return( 0 );
107 }
108
109 // -------------------------------------------------------------------------
110 void cpExtensions::Interaction::ImageInteractorStyle::
111 GetSeedAsPoint( unsigned int id, double pos[ 3 ] ) const
112 {
113   if( this->m_SeedWidget != NULL )
114     this->m_SeedWidget->Representation->GetSeedWorldPosition( id, pos );
115 }
116
117 // -------------------------------------------------------------------------
118 void cpExtensions::Interaction::ImageInteractorStyle::
119 GetSeedAsIndex( unsigned int id, int idx[ 3 ] ) const
120 {
121   /* TODO
122      if( this->m_SeedWidget != NULL )
123      this->m_SeedWidget->Representation->GetSeedWorldPosition( id, pos );
124   */
125 }
126
127 // -------------------------------------------------------------------------
128 cpExtensions::Interaction::ImageInteractorStyle::
129 ImageInteractorStyle( )
130   : Superclass( ),
131     m_SeedWidget( NULL )
132 {
133   this->m_PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
134   this->m_PropPicker->PickFromListOn( );
135 }
136
137 // -------------------------------------------------------------------------
138 cpExtensions::Interaction::ImageInteractorStyle::
139 ~ImageInteractorStyle( )
140 {
141   if( this->m_SeedWidget != NULL ) delete this->m_SeedWidget;
142 }
143
144 // -------------------------------------------------------------------------
145 bool cpExtensions::Interaction::ImageInteractorStyle::
146 _PickPosition( int idx[ 2 ], double pos[ 3 ] )
147 {
148   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
149   if( rwi == NULL )
150     return( false );
151
152   // Find the renderer where the event has been raised
153   idx[ 0 ] = rwi->GetEventPosition( )[ 0 ];
154   idx[ 1 ] = rwi->GetEventPosition( )[ 1 ];
155   this->FindPokedRenderer( double( idx[ 0 ] ), double( idx[ 1 ] ) );
156
157   // Pick a 3D position
158   int r = this->m_PropPicker->Pick(
159     double( idx[ 0 ] ), double( idx[ 1 ] ), double( 0 ),
160     this->CurrentRenderer
161     );
162   if( r == 0 )
163     return( false );
164   this->m_PropPicker->GetPickPosition( pos );
165   return( true );
166 }
167
168 // eof - $RCSfile$