]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Extensions/Algorithms/IsoImageSlicer.hxx
...
[cpPlugins.git] / lib / cpPlugins / Extensions / Algorithms / IsoImageSlicer.hxx
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __CPPLUGINS__EXTENSIONS__ALGORITHMS__ISOIMAGESLICER__HXX__
6 #define __CPPLUGINS__EXTENSIONS__ALGORITHMS__ISOIMAGESLICER__HXX__
7
8 // -------------------------------------------------------------------------
9 template< class R, class I >
10 unsigned long cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
11 GetMTime( ) const
12 {
13   unsigned long t = this->Superclass::GetMTime( );
14   unsigned long sT = this->m_Slicer->GetMTime( );
15   unsigned long cT = this->m_Collapsor->GetMTime( );
16   unsigned long tT = this->m_Transform->GetMTime( );
17   t = ( sT > t )? sT: t;
18   t = ( cT > t )? cT: t;
19   t = ( tT > t )? tT: t;
20   return( t );
21 }
22
23 // -------------------------------------------------------------------------
24 template< class R, class I >
25 const typename cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
26 TInterpolateFunction*
27 cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
28 GetInterpolator( ) const
29 {
30   return( this->m_Slicer->GetInterpolator( ) );
31 }
32
33 // -------------------------------------------------------------------------
34 template< class R, class I >
35 const typename cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
36 TMatrix& cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
37 GetRotation( ) const
38 {
39   return( this->m_Transform->GetMatrix( ) );
40 }
41
42 // -------------------------------------------------------------------------
43 template< class R, class I >
44 const typename cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
45 TVector& cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
46 GetTranslation( ) const
47 {
48   return( this->m_Transform->GetOffset( ) );
49 }
50
51 // -------------------------------------------------------------------------
52 template< class R, class I >
53 void cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
54 SetInterpolator( TInterpolateFunction* f )
55 {
56   this->m_Slicer->SetInterpolator( f );
57   this->Modified( );
58 }
59
60 // -------------------------------------------------------------------------
61 template< class R, class I >
62 template< class M >
63 void cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
64 SetRotation( const M& r )
65 {
66   TMatrix rotation;
67   for( unsigned int i = 0; i < Self::Dim; ++i )
68     for( unsigned int j = 0; j < Self::Dim; ++j )
69       rotation[ i ][ j ] = r[ i ][ j ];
70   this->m_Transform->SetMatrix( rotation );
71   this->Modified( );
72 }
73
74 // -------------------------------------------------------------------------
75 template< class R, class I >
76 template< class V >
77 void cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
78 SetTranslation( const V& t )
79 {
80   TVector off;
81   for( unsigned int i = 0; i < Self::Dim; ++i )
82     off[ i ] = t[ i ];
83   this->m_Transform->SetOffset( off );
84   this->Modified( );
85 }
86
87 // -------------------------------------------------------------------------
88 template< class R, class I >
89 void cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
90 SetSize( TScalar s )
91 {
92   this->m_Size.Fill( s );
93   this->Modified( );
94 }
95
96 // -------------------------------------------------------------------------
97 template< class R, class I >
98 cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
99 BaseImageSlicer( )
100   : Superclass( ),
101     m_SizeFromMaximum( false ),
102     m_SizeFromMinimum( false ),
103     m_Spacing( TSpacingValue( 1 ) ),
104     m_SpacingFromMaximum( false ),
105     m_SpacingFromMinimum( false )
106 {
107   this->m_Size.Fill( TScalar( 1 ) );
108
109   // Slicer
110   this->m_Slicer = TSlicer::New( );
111
112   TIndex idx;
113   idx.Fill( 0 );
114   this->m_Slicer->SetOutputStartIndex( idx );
115
116   // Dimension collapsor
117   this->m_Collapsor = TCollapsor::New( );
118   this->m_Collapsor->SetInput( this->m_Slicer->GetOutput( ) );
119
120   this->m_Transform = TTransform::New( );
121   this->m_Transform->SetIdentity( );
122 }
123
124 // -------------------------------------------------------------------------
125 template< class R, class I >
126 cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
127 ~BaseImageSlicer( )
128 {
129 }
130
131 // -------------------------------------------------------------------------
132 template< class R, class I >
133 void cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
134 GenerateOutputInformation( )
135 {
136 }
137
138 // -------------------------------------------------------------------------
139 template< class R, class I >
140 void cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
141 GenerateInputRequestedRegion( )
142 {
143   TImage* input = const_cast< TImage* >( this->GetInput( ) );
144   if( input != NULL )
145     input->SetRequestedRegionToLargestPossibleRegion( );
146 }
147
148 // -------------------------------------------------------------------------
149 template< class R, class I >
150 void cpPlugins::Extensions::Algorithms::BaseImageSlicer< R, I >::
151 GenerateData( )
152 {
153   const TImage* input = this->GetInput( );
154
155   // Spacing
156   TSpacing spac;
157   if( this->m_SpacingFromMaximum || this->m_SpacingFromMinimum )
158   {
159     spac = input->GetSpacing( );
160     TSpacingValue minIso = spac[ 0 ];
161     TSpacingValue maxIso = spac[ 0 ];
162     for( unsigned int i = 1; i < Self::Dim; i++ )
163     {
164       minIso = ( spac[ i ] < minIso )? spac[ i ]: minIso;
165       maxIso = ( spac[ i ] > maxIso )? spac[ i ]: maxIso;
166
167     } // rof
168     this->m_Spacing = ( this->m_SpacingFromMinimum )? minIso: maxIso;
169
170   } // fi
171   spac.Fill( this->m_Spacing );
172
173   // Size and origin
174   if( this->m_SizeFromMaximum || this->m_SizeFromMinimum )
175   {
176     TSize iSize = input->GetRequestedRegion( ).GetSize( );
177     TSpacing iSpac = input->GetSpacing( );
178     TScalar minSize = TScalar( iSize[ 0 ] ) * TScalar( iSpac[ 0 ] );
179     TScalar maxSize = minSize;
180     for( unsigned int i = 1; i < Self::Dim; i++ )
181     {
182       TScalar v = TScalar( iSize[ i ] ) * TScalar( iSpac[ i ] );
183       minSize = ( v < minSize )? v: minSize;
184       maxSize = ( v > maxSize )? v: maxSize;
185
186     } // rof
187     if( this->m_SizeFromMaximum )
188       this->m_Size.Fill( maxSize );
189     else
190       this->m_Size.Fill( minSize );
191
192   } // fi
193
194   TSize size;
195   typename TSlicer::OriginPointType origin;
196   size[ 0 ] = 1;
197   origin[ 0 ] = 0;
198   for( unsigned int i = 1; i < Self::Dim; i++ )
199   {
200     double s = double( this->m_Size[ i ] ) / double( spac[ i ] );
201     size[ i ] = ( unsigned int )( s );
202     origin[ i ] = -( 0.5 * this->m_Size[ i ] );
203
204   } // rof
205
206   // Prepare slicer
207   this->m_Slicer->SetInput( input );
208   this->m_Slicer->SetTransform( this->m_Transform );
209   this->m_Slicer->SetOutputSpacing( spac );
210   this->m_Slicer->SetOutputOrigin( origin );
211   this->m_Slicer->SetSize( size );
212   this->m_Slicer->SetDefaultPixelValue( this->m_DefaultValue );
213
214   // Slice!
215   // Note: UpdateLargestPossibleRegion( ) is used since we need the
216   // output regions to be updated at each filter call.
217   this->m_Slicer->UpdateLargestPossibleRegion( );
218
219   // Collapse result
220   TRegion region = this->m_Slicer->GetOutput( )->GetRequestedRegion( );
221   TSize regionSize = region.GetSize( );
222   regionSize[ 0 ] = 0;
223   region.SetSize( regionSize );
224   this->m_Collapsor->SetExtractionRegion( region );
225
226   this->m_Collapsor->GraftOutput( this->GetOutput( ) );
227   this->m_Collapsor->UpdateLargestPossibleRegion( );
228   this->GraftOutput( this->m_Collapsor->GetOutput( ) );
229 }
230
231 #endif // __CPPLUGINS__EXTENSIONS__ALGORITHMS__ISOIMAGESLICER__HXX__
232
233 // eof - $RCSfile$