]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/DataStructures/Image/Path.hxx
...
[FrontAlgorithms.git] / lib / fpa / DataStructures / Image / Path.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 #ifndef __fpa__DataStructures__Image__Path__hxx__
7 #define __fpa__DataStructures__Image__Path__hxx__
8
9 #include <itkMath.h>
10 #include <itkNumericTraits.h>
11
12 // -------------------------------------------------------------------------
13 template< unsigned int _VDim >
14 unsigned long fpa::DataStructures::Image::Path< _VDim >::
15 GetSize( ) const
16 {
17   return( this->GetVertexList( )->Size( ) );
18 }
19
20 // -------------------------------------------------------------------------
21 template< unsigned int _VDim >
22 typename fpa::DataStructures::Image::Path< _VDim >::
23 TContinuousIndex fpa::DataStructures::Image::Path< _VDim >::
24 GetContinuousVertex( unsigned long i ) const
25 {
26   return( this->GetVertexList( )->GetElement( i ) );
27 }
28
29 // -------------------------------------------------------------------------
30 template< unsigned int _VDim >
31 typename fpa::DataStructures::Image::Path< _VDim >::
32 TIndex fpa::DataStructures::Image::Path< _VDim >::
33 GetVertex( unsigned long i ) const
34 {
35   TContinuousIndex cidx = this->GetContinuousVertex( i );
36   TIndex idx;
37   for( unsigned int d = 0; d < _VDim; ++d )
38     idx[ d ] = cidx[ d ];
39   return( idx );
40 }
41
42 // -------------------------------------------------------------------------
43 template< unsigned int _VDim >
44 typename fpa::DataStructures::Image::Path< _VDim >::
45 TPoint fpa::DataStructures::Image::Path< _VDim >::
46 GetPoint( unsigned long i ) const
47 {
48   typedef typename TPoint::CoordRepType _TCoordRep;
49   TPoint pnt;
50   TContinuousIndex idx = this->GetVertex( i );
51   for( unsigned int r = 0; r < _VDim; ++r )
52   {
53     _TCoordRep sum = itk::NumericTraits< _TCoordRep >::ZeroValue( );
54     for( unsigned int c = 0; c < _VDim; ++c )
55       sum += this->m_IndexToPhysicalPoint( r, c ) * idx[ c ];
56     pnt[ r ] = sum + this->m_Origin[ r ];
57
58   } // rof
59   return( pnt );
60 }
61
62 // -------------------------------------------------------------------------
63 template< unsigned int _VDim >
64 void fpa::DataStructures::Image::Path< _VDim >::
65 SetSpacing( const TSpacing& spac )
66 {
67   if( this->m_Spacing != spac )
68   {
69     this->m_Spacing = spac;
70     this->_ComputeIndexToPhysicalPointMatrices( );
71     this->Modified( );
72
73   } // fi
74 }
75
76 // -------------------------------------------------------------------------
77 template< unsigned int _VDim >
78 void fpa::DataStructures::Image::Path< _VDim >::
79 SetSpacing( const double spac[ _VDim ] )
80 {
81   this->SetSpacing( TSpacing( spac ) );
82 }
83
84 // -------------------------------------------------------------------------
85 template< unsigned int _VDim >
86 void fpa::DataStructures::Image::Path< _VDim >::
87 SetSpacing( const float spac[ _VDim ] )
88 {
89   TSpacing s;
90   for( unsigned int d = 0; d < _VDim; ++d )
91     s[ d ] = spac[ d ];
92   this->SetSpacing( s );
93 }
94
95 // -------------------------------------------------------------------------
96 template< unsigned int _VDim >
97 void fpa::DataStructures::Image::Path< _VDim >::
98 SetOrigin( const double ori[ _VDim ] )
99 {
100   this->SetOrigin( TPoint( ori ) );
101 }
102
103 // -------------------------------------------------------------------------
104 template< unsigned int _VDim >
105 void fpa::DataStructures::Image::Path< _VDim >::
106 SetOrigin( const float ori[ _VDim ] )
107 {
108   this->SetOrigin( TPoint( ori ) );
109 }
110
111 // -------------------------------------------------------------------------
112 template< unsigned int _VDim >
113 void fpa::DataStructures::Image::Path< _VDim >::
114 SetDirection( const TDirection& dir )
115 {
116   bool modified = false;
117   for( unsigned int r = 0; r < _VDim; r++ )
118   {
119     for( unsigned int c = 0; c < _VDim; c++ )
120     {
121       if(
122         itk::Math::NotExactlyEquals(
123           this->m_Direction[ r ][ c ], dir[ r ][ c ]
124           )
125         )
126       {
127         this->m_Direction[ r ][ c ] = dir[ r ][ c ];
128         modified = true;
129       } // fi
130
131     } // rof
132
133   } // rof
134   if( modified )
135   {
136     this->_ComputeIndexToPhysicalPointMatrices( );
137     this->m_InverseDirection = this->m_Direction.GetInverse( );
138     this->Modified( );
139
140   } // fi
141 }
142
143 // -------------------------------------------------------------------------
144 template< unsigned int _VDim >
145 void fpa::DataStructures::Image::Path< _VDim >::
146 Graft( itk::DataObject* o )
147 {
148   this->Superclass::Graft( o );
149   Self* other = dynamic_cast< Self* >( o );
150   if( other != NULL )
151   {
152     this->m_DefaultInputStepSize = other->m_DefaultInputStepSize;
153     this->Initialize( );
154     for( unsigned long i = 0; i < other->GetSize( ); ++i )
155       this->AddVertex( other->GetContinuousVertex( i ) );
156     this->m_Spacing = other->m_Spacing;
157     this->m_Origin = other->m_Origin;
158     this->m_Direction = other->m_Direction;
159     this->m_InverseDirection = other->m_InverseDirection;
160     this->m_IndexToPhysicalPoint = other->m_IndexToPhysicalPoint;
161     this->m_PhysicalPointToIndex = other->m_PhysicalPointToIndex;
162
163   } // fi
164 }
165
166 // -------------------------------------------------------------------------
167 template< unsigned int _VDim >
168 fpa::DataStructures::Image::Path< _VDim >::
169 Path( )
170   : Superclass( )
171 {
172   this->m_Spacing.Fill( 1.0 );
173   this->m_Origin.Fill( 0.0 );
174   this->m_Direction.SetIdentity( );
175   this->m_InverseDirection.SetIdentity( );
176   this->m_IndexToPhysicalPoint.SetIdentity( );
177   this->m_PhysicalPointToIndex.SetIdentity( );
178 }
179
180 // -------------------------------------------------------------------------
181 template< unsigned int _VDim >
182 fpa::DataStructures::Image::Path< _VDim >::
183 ~Path( )
184 {
185 }
186
187 // -------------------------------------------------------------------------
188 template< unsigned int _VDim >
189 void fpa::DataStructures::Image::Path< _VDim >::
190 _ComputeIndexToPhysicalPointMatrices( )
191 {
192   TDirection scale;
193   scale.Fill( 0.0 );
194   for( unsigned int i = 0; i < _VDim; i++ )
195   {
196     if( this->m_Spacing[ i ] == 0.0 )
197       itkExceptionMacro(
198         "A spacing of 0 is not allowed: Spacing is " << this->m_Spacing
199         );
200     scale[ i ][ i ] = this->m_Spacing[ i ];
201
202   } // rof
203
204   if( vnl_determinant( this->m_Direction.GetVnlMatrix( ) ) == 0.0 )
205     itkExceptionMacro(
206       << "Bad direction, determinant is 0. Direction is "
207       << this->m_Direction
208       );
209   this->m_IndexToPhysicalPoint = this->m_Direction * scale;
210   this->m_PhysicalPointToIndex = this->m_IndexToPhysicalPoint.GetInverse( );
211   this->Modified( );
212 }
213
214 #endif // __fpa__DataStructures__Image__Path__hxx__
215 // eof - $RCSfile$