]> Creatis software - cpPlugins.git/blob - plugins/Widgets/SeedWidget.cxx
Code cleaning
[cpPlugins.git] / plugins / Widgets / SeedWidget.cxx
1 #include <plugins/Widgets/SeedWidget.h>
2
3 #include <vtkCommand.h>
4 #include <vtkProperty.h>
5 #include <vtkRenderer.h>
6 #include <vtkRenderWindowInteractor.h>
7 #include <cpExtensions/QT/SimpleMPRWidget.h>
8
9 // -------------------------------------------------------------------------
10 // This callback is responsible for changing update time
11 namespace cpPluginsWidgets
12 {
13   /**
14    */
15   class SeedWidgetCallback
16     : public vtkCommand
17   {
18   public:
19     static SeedWidgetCallback* New( )
20       { return( new SeedWidgetCallback ); }
21     virtual void Execute( vtkObject* caller, unsigned long id, void* data )
22       {
23         if(
24           id == vtkCommand::CursorChangedEvent ||
25           id == vtkCommand::PlacePointEvent
26           )
27           this->Widget->Modified( );
28       }
29     SeedWidget* Widget;
30   };
31
32 } // ecapseman
33
34 // -------------------------------------------------------------------------
35 void cpPluginsWidgets::SeedWidget::WidgetData::
36 Configure(
37   cpPluginsWidgets::SeedWidget* parent,
38   vtkRenderWindowInteractor* interactor, vtkImageActor* actor
39   )
40 {
41   this->Placer = vtkSmartPointer< vtkImageActorPointPlacer >::New( );
42   this->Handle = vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
43   this->Seed = vtkSmartPointer< vtkSeedRepresentation >::New( );
44   this->Widget =
45     vtkSmartPointer< cpExtensions::Interaction::SeedWidget >::New( );
46
47   this->Placer->SetImageActor( actor );
48   this->Handle->GetProperty( )->SetColor( 1, 0, 0 );
49   this->Handle->SetPointPlacer( this->Placer );
50   this->Seed->SetHandleRepresentation( this->Handle );
51   this->Widget->SetRepresentation( this->Seed );
52   this->Widget->SetInteractor( interactor );
53
54   vtkSmartPointer< SeedWidgetCallback > cb =
55     vtkSmartPointer< SeedWidgetCallback >::New( );
56   cb->Widget = parent;
57   this->Widget->AddObserver( vtkCommand::PlacePointEvent, cb );
58   this->Widget->AddObserver( vtkCommand::CursorChangedEvent, cb );
59 }
60
61 // -------------------------------------------------------------------------
62 void cpPluginsWidgets::SeedWidget::WidgetData::
63 On( )
64 {
65   this->Widget->On( );
66 }
67
68 // -------------------------------------------------------------------------
69 void cpPluginsWidgets::SeedWidget::WidgetData::
70 Off( )
71 {
72   this->Widget->Off( );
73 }
74
75 // -------------------------------------------------------------------------
76 cpPluginsWidgets::SeedWidget::
77 SeedWidget( )
78   : Superclass( ),
79     m_Configured( false ),
80     m_InitialNumberOfSeeds( 0 )
81 {
82   this->_AddOutput< cpPlugins::DataObject >( "Output" );
83 }
84
85 // -------------------------------------------------------------------------
86 cpPluginsWidgets::SeedWidget::
87 ~SeedWidget( )
88 {
89 }
90
91 // -------------------------------------------------------------------------
92 void cpPluginsWidgets::SeedWidget::
93 _GenerateData( )
94 {
95   auto points = this->_CreateVTK< vtkPoints >( );
96   if( this->m_Configured )
97   {
98     std::stringstream text;
99     points->Resize( this->m_InitialNumberOfSeeds );
100     for(
101       auto wIt = this->m_Widgets.begin( );
102       wIt != this->m_Widgets.end( );
103       ++wIt
104       )
105     {
106       double pos[ 3 ];
107       for( unsigned int i = 0; i < wIt->Seed->GetNumberOfSeeds( ); ++i )
108       {
109         wIt->Seed->GetSeedWorldPosition( i, pos );
110         if( i > 0 )
111           text << "#";
112         text << pos[ 0 ] << " " << pos[ 1 ] << " " << pos[ 2 ];
113         points->InsertNextPoint( pos );
114
115       } // rof
116
117     } // rof
118     this->m_Parameters.SetString( "Text", text.str( ) );
119   }
120   else
121   {
122     std::vector< std::string > tokens;
123     cpPlugins::TokenizeString(
124       tokens, this->m_Parameters.GetString( "Text" ), "#"
125       );
126     this->m_InitialNumberOfSeeds = tokens.size( );
127     points->SetNumberOfPoints( 0 );
128     for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
129     {
130       std::vector< std::string > coords;
131       cpPlugins::TokenizeString( coords, *tIt, " \t" );
132       int dim = ( coords.size( ) < 3 )? coords.size( ): 3;
133       double pos[ 3 ];
134       for( unsigned int d = 0; d < 3; ++d )
135       {
136         pos[ d ] = double( 0 );
137         if( d < dim )
138         {
139           std::istringstream value( coords[ d ] );
140           value >> pos[ d ];
141
142         } // fi
143
144       } // rof
145       points->InsertNextPoint( pos );
146       this->m_Configured = true;
147
148     } // rof
149
150     std::vector< vtkRenderWindowInteractor* > ints;
151     if( this->m_MPRViewer != NULL )
152     {
153       ints.push_back( this->m_MPRViewer->GetInteractor( 0 ) );
154       ints.push_back( this->m_MPRViewer->GetInteractor( 1 ) );
155       ints.push_back( this->m_MPRViewer->GetInteractor( 2 ) );
156
157     } // fi
158     if( this->m_SingleInteractor != NULL )
159       ints.push_back( this->m_SingleInteractor );
160
161     for( auto iIt = ints.begin( ); iIt !=  ints.end( ); ++iIt )
162     {
163       auto ren = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
164       if( ren != NULL )
165       {
166         auto props = ren->GetViewProps( );
167         if( props != NULL )
168         {
169           props->InitTraversal( );
170           vtkProp* prop;
171           while( ( prop = props->GetNextProp( ) ) != NULL )
172           {
173             auto actor = dynamic_cast< vtkImageActor* >( prop );
174             if( actor != NULL )
175             {
176               WidgetData d;
177               d.Configure( this, *iIt, actor );
178               d.On( );
179               this->m_Widgets.push_back( d );
180               this->m_Configured = true;
181
182             } // fi
183
184           } // elihw
185
186         } // fi
187
188       } // fi
189
190     } // rof
191
192   } // fi
193   if( this->m_Configured )
194     this->Modified( );
195   else
196     this->_Error(
197       "Could not create valid widget: are there any valid actors?"
198       );
199   this->GetOutput( "Output" )->SetVTK( points );
200 }
201
202 // eof - $RCSfile$