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