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