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