]> Creatis software - cpPlugins.git/blob - plugins/cpPluginsWidgets/SeedWidget.cxx
0d4c57d4f3546e8cf4b8476f012dbb5ef03e629e
[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 {
80   this->_AddOutput< cpPlugins::DataObject >( "Output" );
81 }
82
83 // -------------------------------------------------------------------------
84 cpPluginsWidgets::SeedWidget::
85 ~SeedWidget( )
86 {
87 }
88
89 // -------------------------------------------------------------------------
90 void cpPluginsWidgets::SeedWidget::
91 _GenerateData( )
92 {
93   std::cout << "seeds" << std::endl;
94   if( this->m_Configured )
95   {
96     std::cout << "ok" << std::endl;
97     auto points = this->_CreateVTK< vtkPoints >( );
98     for(
99       auto wIt = this->m_Widgets.begin( );
100       wIt != this->m_Widgets.end( );
101       ++wIt
102       )
103     {
104       double pos[ 3 ];
105       for( unsigned int i = 0; i < wIt->Seed->GetNumberOfSeeds( ); ++i )
106       {
107         wIt->Seed->GetSeedWorldPosition( i, pos );
108         points->InsertNextPoint( pos );
109
110       } // rof
111
112     } // rof
113     this->GetOutput( "Output" )->SetVTK( points );
114   }
115   else
116   {
117     std::vector< vtkRenderWindowInteractor* > ints;
118     if( this->m_MPRViewer != NULL )
119     {
120       ints.push_back( this->m_MPRViewer->GetInteractor( 0 ) );
121       ints.push_back( this->m_MPRViewer->GetInteractor( 1 ) );
122       ints.push_back( this->m_MPRViewer->GetInteractor( 2 ) );
123
124     } // fi
125     if( this->m_SingleInteractor != NULL )
126       ints.push_back( this->m_SingleInteractor );
127
128     for( auto iIt = ints.begin( ); iIt !=  ints.end( ); ++iIt )
129     {
130       auto ren = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
131       if( ren != NULL )
132       {
133         auto props = ren->GetViewProps( );
134         if( props != NULL )
135         {
136           props->InitTraversal( );
137           vtkProp* prop;
138           while( ( prop = props->GetNextProp( ) ) != NULL )
139           {
140             auto actor = dynamic_cast< vtkImageActor* >( prop );
141             if( actor != NULL )
142             {
143               WidgetData d;
144               d.Configure( this, *iIt, actor );
145               d.On( );
146               this->m_Widgets.push_back( d );
147               this->m_Configured = true;
148
149             } // fi
150
151           } // elihw
152
153         } // fi
154
155       } // fi
156
157     } // rof
158
159   } // fi
160   if( this->m_Configured )
161     this->Modified( );
162   else
163     this->_Error(
164       "Could not create valid widget: are there any valid actors?"
165       );
166 }
167
168 // eof - $RCSfile$