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