]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/IsoImageSlicer.hxx
...
[cpPlugins.git] / lib / cpExtensions / Algorithms / IsoImageSlicer.hxx
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __CPEXTENSIONS__ALGORITHMS__ISOIMAGESLICER__HXX__
6 #define __CPEXTENSIONS__ALGORITHMS__ISOIMAGESLICER__HXX__
7
8 // -------------------------------------------------------------------------
9 template< class R, class I >
10 unsigned long cpExtensions::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 cpExtensions::Algorithms::BaseImageSlicer< R, I >::
26 TInterpolateFunction*
27 cpExtensions::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 cpExtensions::Algorithms::BaseImageSlicer< R, I >::
36 TMatrix& cpExtensions::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 cpExtensions::Algorithms::BaseImageSlicer< R, I >::
45 TVector& cpExtensions::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 cpExtensions::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 cpExtensions::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 cpExtensions::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 cpExtensions::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 cpExtensions::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   this->m_Collapsor->SetDirectionCollapseToIdentity( );
120
121   this->m_Transform = TTransform::New( );
122   this->m_Transform->SetIdentity( );
123 }
124
125 // -------------------------------------------------------------------------
126 template< class R, class I >
127 cpExtensions::Algorithms::BaseImageSlicer< R, I >::
128 ~BaseImageSlicer( )
129 {
130 }
131
132 // -------------------------------------------------------------------------
133 template< class R, class I >
134 void cpExtensions::Algorithms::BaseImageSlicer< R, I >::
135 GenerateOutputInformation( )
136 {
137 }
138
139 // -------------------------------------------------------------------------
140 template< class R, class I >
141 void cpExtensions::Algorithms::BaseImageSlicer< R, I >::
142 GenerateInputRequestedRegion( )
143 {
144   TImage* input = const_cast< TImage* >( this->GetInput( ) );
145   if( input != NULL )
146     input->SetRequestedRegionToLargestPossibleRegion( );
147 }
148
149 // -------------------------------------------------------------------------
150 template< class R, class I >
151 void cpExtensions::Algorithms::BaseImageSlicer< R, I >::
152 GenerateData( )
153 {
154   const TImage* input = this->GetInput( );
155
156   // Spacing
157   TSpacing spac;
158   if( this->m_SpacingFromMaximum || this->m_SpacingFromMinimum )
159   {
160     spac = input->GetSpacing( );
161     TSpacingValue minIso = spac[ 0 ];
162     TSpacingValue maxIso = spac[ 0 ];
163     for( unsigned int i = 1; i < Self::Dim; i++ )
164     {
165       minIso = ( spac[ i ] < minIso )? spac[ i ]: minIso;
166       maxIso = ( spac[ i ] > maxIso )? spac[ i ]: maxIso;
167
168     } // rof
169     this->m_Spacing = ( this->m_SpacingFromMinimum )? minIso: maxIso;
170
171   } // fi
172   spac.Fill( this->m_Spacing );
173
174   // Size and origin
175   if( this->m_SizeFromMaximum || this->m_SizeFromMinimum )
176   {
177     TSize iSize = input->GetRequestedRegion( ).GetSize( );
178     TSpacing iSpac = input->GetSpacing( );
179     TScalar minSize = TScalar( iSize[ 0 ] ) * TScalar( iSpac[ 0 ] );
180     TScalar maxSize = minSize;
181     for( unsigned int i = 1; i < Self::Dim; i++ )
182     {
183       TScalar v = TScalar( iSize[ i ] ) * TScalar( iSpac[ i ] );
184       minSize = ( v < minSize )? v: minSize;
185       maxSize = ( v > maxSize )? v: maxSize;
186
187     } // rof
188     if( this->m_SizeFromMaximum )
189       this->m_Size.Fill( maxSize );
190     else
191       this->m_Size.Fill( minSize );
192
193   } // fi
194
195   TSize size;
196   typename TSlicer::OriginPointType origin;
197   size[ 0 ] = 1;
198   origin[ 0 ] = 0;
199   for( unsigned int i = 1; i < Self::Dim; i++ )
200   {
201     double s = double( this->m_Size[ i ] ) / double( spac[ i ] );
202     size[ i ] = ( unsigned int )( s );
203     origin[ i ] = -( 0.5 * this->m_Size[ i ] );
204
205   } // rof
206
207   // Prepare slicer
208   this->m_Slicer->SetInput( input );
209   this->m_Slicer->SetTransform( this->m_Transform );
210   this->m_Slicer->SetOutputSpacing( spac );
211   this->m_Slicer->SetOutputOrigin( origin );
212   this->m_Slicer->SetSize( size );
213   this->m_Slicer->SetDefaultPixelValue( this->m_DefaultValue );
214
215   // Slice!
216   // Note: UpdateLargestPossibleRegion( ) is used since we need the
217   // output regions to be updated at each filter call.
218   this->m_Slicer->UpdateLargestPossibleRegion( );
219
220   // Collapse result
221   TRegion region = this->m_Slicer->GetOutput( )->GetRequestedRegion( );
222   TSize regionSize = region.GetSize( );
223   regionSize[ 0 ] = 0;
224   region.SetSize( regionSize );
225   this->m_Collapsor->SetExtractionRegion( region );
226
227   this->m_Collapsor->GraftOutput( this->GetOutput( ) );
228   this->m_Collapsor->UpdateLargestPossibleRegion( );
229   this->GraftOutput( this->m_Collapsor->GetOutput( ) );
230 }
231
232 #endif // __CPEXTENSIONS__ALGORITHMS__ISOIMAGESLICER__HXX__
233
234 // eof - $RCSfile$