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