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