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