]> Creatis software - cpPlugins.git/blob - plugins/cpPluginsWidgets/SeedWidget.cxx
03e721572851e1d1dbbd80bff1153df355af75c8
[cpPlugins.git] / plugins / cpPluginsWidgets / SeedWidget.cxx
1 #include "SeedWidget.h"
2
3 #include <cpPlugins/Image.h>
4 #include <cpExtensions/QT/SimpleMPRWidget.h>
5 #include <cpExtensions/Interaction/ImageInteractorStyle.h>
6 #include <vtkRenderWindowInteractor.h>
7 #include <itkSimpleDataObjectDecorator.h>
8 #include <itkSimpleDataObjectDecorator.hxx>
9
10 // -------------------------------------------------------------------------
11 itk::ModifiedTimeType cpPluginsWidgets::SeedWidget::
12 GetMTime( ) const
13 {
14   // std::cout << "Seed GetMTime" << std::endl;
15   return( 0 /*this->Superclass::GetMTime( )*/ );
16 }
17
18 // -------------------------------------------------------------------------
19 cpPluginsWidgets::SeedWidget::
20 SeedWidget( )
21   : Superclass( ),
22     m_Configured( false )
23 {
24   this->_AddInput( "ReferenceImage" );
25   this->_AddOutput< cpPlugins::DataObject >( "Output" );
26
27   this->m_Parameters.ConfigureAsBool( "SeedsAreInRealSpace" );
28   this->m_Parameters.SetBool( "SeedsAreInRealSpace", false );
29 }
30
31 // -------------------------------------------------------------------------
32 cpPluginsWidgets::SeedWidget::
33 ~SeedWidget( )
34 {
35 }
36
37 // -------------------------------------------------------------------------
38 std::string cpPluginsWidgets::SeedWidget::
39 _GenerateData( )
40 {
41   auto image = this->GetInputData( "ReferenceImage" );
42   std::string   r = cpPlugin_Image_Demangle_Dim( _GD0, image, 3 );
43   if( r != "" ) r = cpPlugin_Image_Demangle_Dim( _GD0, image, 2 );
44   return( r );
45 }
46
47 // -------------------------------------------------------------------------
48 template< class _TImage >
49 std::string cpPluginsWidgets::SeedWidget::
50 _GD0( _TImage* image )
51 {
52   if( image != NULL )
53   {
54     if( this->m_Parameters.GetBool( "SeedsAreInRealSpace" ) )
55       return( this->_GD1_Points( image ) );
56     else
57       return( this->_GD1_Vertices( image ) );
58   }
59   else
60     return( "Widgets::SeedWidget: Input image dimension not supported." );
61 }
62
63 // -------------------------------------------------------------------------
64 template< class _TImage >
65 std::string cpPluginsWidgets::SeedWidget::
66 _GD1_Points( _TImage* image )
67 {
68   typedef cpExtensions::Interaction::ImageInteractorStyle _S;
69   typedef itk::Point< double, _TImage::ImageDimension > _P;
70   typedef itk::SimpleDataObjectDecorator< std::vector< _P > > _Container;
71
72   auto container = this->_CreateITK< _Container >( );
73
74   double aux_pnt[ 3 ];
75   unsigned int dim = ( _TImage::ImageDimension < 3 )? _TImage::ImageDimension: 3;
76
77   container->Get( ).clear( );
78
79   // MPR
80   if( this->m_MPRViewer != NULL )
81   {
82     for( unsigned int i = 0; i < 4; ++i )
83     {
84       _S* s =
85         dynamic_cast< _S* >(
86           this->m_MPRViewer->GetInteractor( i )->GetInteractorStyle( )
87           );
88       if( s != NULL )
89       {
90         if( this->m_Configured )
91         {
92           for( unsigned int i = 0; i < s->GetNumberOfSeeds( ); ++i )
93           {
94             s->GetSeedAsPoint( i, aux_pnt );
95             _P seed;
96             for( unsigned int d = 0; d < dim; ++d )
97               seed[ d ] = aux_pnt[ d ];
98             container->Get( ).push_back( seed );
99
100           } // rof
101         }
102         else
103           s->SeedWidgetOn( );
104
105       } // fi
106
107     } // rof
108
109   } // fi
110
111   // Single interactor
112   _S* s = dynamic_cast< _S* >( this->m_SingleInteractor );
113   if( s != NULL )
114   {
115     if( this->m_Configured )
116     {
117       for( unsigned int i = 0; i < s->GetNumberOfSeeds( ); ++i )
118       {
119         s->GetSeedAsPoint( i, aux_pnt );
120         _P seed;
121         for( unsigned int d = 0; d < dim; ++d )
122           seed[ d ] = aux_pnt[ d ];
123         container->Get( ).push_back( seed );
124
125       } // rof
126     }
127     else
128       s->SeedWidgetOn( );
129
130   } // fi
131   this->m_Configured = true;
132
133   this->GetOutputData( "Output" )->SetITK( container );
134   return( "" );
135 }
136
137 // -------------------------------------------------------------------------
138 template< class _TImage >
139 std::string cpPluginsWidgets::SeedWidget::
140 _GD1_Vertices( _TImage* image )
141 {
142   typedef cpExtensions::Interaction::ImageInteractorStyle _S;
143   typedef
144     itk::SimpleDataObjectDecorator< std::vector< typename _TImage::IndexType > >
145     _Container;
146   auto container = this->_CreateITK< _Container >( );
147
148   double aux_pnt[ 3 ];
149   unsigned int dim = ( _TImage::ImageDimension < 3 )? _TImage::ImageDimension: 3;
150
151   container->Get( ).clear( );
152
153   // MPR
154   if( this->m_MPRViewer != NULL )
155   {
156     for( unsigned int i = 0; i < 4; ++i )
157     {
158       _S* s =
159         dynamic_cast< _S* >(
160           this->m_MPRViewer->GetInteractor( i )->GetInteractorStyle( )
161           );
162       if( s != NULL )
163       {
164         if( this->m_Configured )
165         {
166           for( unsigned int i = 0; i < s->GetNumberOfSeeds( ); ++i )
167           {
168             s->GetSeedAsPoint( i, aux_pnt );
169             typename _TImage::PointType seed;
170             for( unsigned int d = 0; d < dim; ++d )
171               seed[ d ] = aux_pnt[ d ];
172             typename _TImage::IndexType idx;
173             if( image->TransformPhysicalPointToIndex( seed, idx ) )
174               container->Get( ).push_back( idx );
175
176           } // rof
177         }
178         else
179           s->SeedWidgetOn( );
180
181       } // fi
182
183     } // rof
184
185   } // fi
186
187   // Single interactor
188   _S* s = dynamic_cast< _S* >( this->m_SingleInteractor );
189   if( s != NULL )
190   {
191     if( this->m_Configured )
192     {
193       for( unsigned int i = 0; i < s->GetNumberOfSeeds( ); ++i )
194       {
195         s->GetSeedAsPoint( i, aux_pnt );
196         typename _TImage::PointType seed;
197         for( unsigned int d = 0; d < dim; ++d )
198           seed[ d ] = aux_pnt[ d ];
199         typename _TImage::IndexType idx;
200         if( image->TransformPhysicalPointToIndex( seed, idx ) )
201           container->Get( ).push_back( idx );
202
203       } // rof
204     }
205     else
206       s->SeedWidgetOn( );
207
208   } // fi
209   this->m_Configured = true;
210
211   this->GetOutputData( "Output" )->SetITK( container );
212   return( "" );
213 }
214
215 // eof - $RCSfile$