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