]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/DataStructures/PolyLineParametricPath.hxx
5e26816e47876a55d8501f8aad605a0b6381bdeb
[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 GetVertex( 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 TPoint cpExtensions::DataStructures::PolyLineParametricPath< _VDim >::
41 GetPoint( unsigned long i ) const
42 {
43   typedef typename TPoint::CoordRepType _TCoordRep;
44   TPoint pnt;
45   TContinuousIndex idx = this->GetVertex( i );
46   for( unsigned int r = 0; r < _VDim; ++r )
47   {
48     _TCoordRep sum = itk::NumericTraits< _TCoordRep >::ZeroValue( );
49     for( unsigned int c = 0; c < _VDim; ++c )
50       sum += this->m_IndexToPhysicalPoint( r, c ) * idx[ c ];
51     pnt[ r ] = sum + this->m_Origin[ r ];
52
53   } // rof
54   return( pnt );
55 }
56
57 // -------------------------------------------------------------------------
58 template< unsigned int _VDim >
59 typename cpExtensions::DataStructures::PolyLineParametricPath< _VDim >::
60 TPoint cpExtensions::DataStructures::PolyLineParametricPath< _VDim >::
61 GetSmoothPoint( double u ) const
62 {
63   TPoint p;
64   p.Fill( 0 );
65   p += this->m_Bezier->Evaluate( u );
66   return( p );
67 }
68
69 // -------------------------------------------------------------------------
70 template< unsigned int _VDim >
71 void cpExtensions::DataStructures::PolyLineParametricPath< _VDim >::
72 SetSpacing( const TSpacing& spac )
73 {
74   if( this->m_Spacing != spac )
75   {
76     this->m_Spacing = spac;
77     this->_ComputeIndexToPhysicalPointMatrices( );
78     this->Modified( );
79
80   } // fi
81 }
82
83 // -------------------------------------------------------------------------
84 template< unsigned int _VDim >
85 void cpExtensions::DataStructures::PolyLineParametricPath< _VDim >::
86 SetSpacing( const double spac[ _VDim ] )
87 {
88   this->SetSpacing( TSpacing( spac ) );
89 }
90
91 // -------------------------------------------------------------------------
92 template< unsigned int _VDim >
93 void cpExtensions::DataStructures::PolyLineParametricPath< _VDim >::
94 SetSpacing( const float spac[ _VDim ] )
95 {
96   TSpacing s;
97   for( unsigned int d = 0; d < _VDim; ++d )
98     s[ d ] = spac[ d ];
99   this->SetSpacing( s );
100 }
101
102 // -------------------------------------------------------------------------
103 template< unsigned int _VDim >
104 void cpExtensions::DataStructures::PolyLineParametricPath< _VDim >::
105 SetOrigin( const double ori[ _VDim ] )
106 {
107   this->SetOrigin( TPoint( ori ) );
108 }
109
110 // -------------------------------------------------------------------------
111 template< unsigned int _VDim >
112 void cpExtensions::DataStructures::PolyLineParametricPath< _VDim >::
113 SetOrigin( const float ori[ _VDim ] )
114 {
115   this->SetOrigin( TPoint( ori ) );
116 }
117
118 // -------------------------------------------------------------------------
119 template< unsigned int _VDim >
120 void cpExtensions::DataStructures::PolyLineParametricPath< _VDim >::
121 SetDirection( const TDirection& dir )
122 {
123   bool modified = false;
124   for( unsigned int r = 0; r < _VDim; r++ )
125   {
126     for( unsigned int c = 0; c < _VDim; c++ )
127     {
128       if(
129         itk::Math::NotExactlyEquals(
130           this->m_Direction[ r ][ c ], dir[ r ][ c ]
131           )
132         )
133       {
134         this->m_Direction[ r ][ c ] = dir[ r ][ c ];
135         modified = true;
136       } // fi
137
138     } // rof
139
140   } // rof
141   if( modified )
142   {
143     this->_ComputeIndexToPhysicalPointMatrices( );
144     this->m_InverseDirection = this->m_Direction.GetInverse( );
145     this->Modified( );
146
147   } // fi
148 }
149
150 // -------------------------------------------------------------------------
151 template< unsigned int _VDim >
152 cpExtensions::DataStructures::PolyLineParametricPath< _VDim >::
153 PolyLineParametricPath( )
154   : Superclass( )
155 {
156   this->m_Bezier = TBezier::New( );
157
158   this->m_Spacing.Fill( 1.0 );
159   this->m_Origin.Fill( 0.0 );
160   this->m_Direction.SetIdentity( );
161   this->m_InverseDirection.SetIdentity( );
162   this->m_IndexToPhysicalPoint.SetIdentity( );
163   this->m_PhysicalPointToIndex.SetIdentity( );
164 }
165
166 // -------------------------------------------------------------------------
167 template< unsigned int _VDim >
168 cpExtensions::DataStructures::PolyLineParametricPath< _VDim >::
169 ~PolyLineParametricPath( )
170 {
171 }
172
173 // -------------------------------------------------------------------------
174 template< unsigned int _VDim >
175 void cpExtensions::DataStructures::PolyLineParametricPath< _VDim >::
176 _ComputeIndexToPhysicalPointMatrices( )
177 {
178   TDirection scale;
179   scale.Fill( 0.0 );
180   for( unsigned int i = 0; i < _VDim; i++ )
181   {
182     if( this->m_Spacing[ i ] == 0.0 )
183       itkExceptionMacro(
184         "A spacing of 0 is not allowed: Spacing is " << this->m_Spacing
185         );
186     scale[ i ][ i ] = this->m_Spacing[ i ];
187
188   } // rof
189
190   if( vnl_determinant( this->m_Direction.GetVnlMatrix( ) ) == 0.0 )
191     itkExceptionMacro(
192       << "Bad direction, determinant is 0. Direction is "
193       << this->m_Direction
194       );
195   this->m_IndexToPhysicalPoint = this->m_Direction * scale;
196   this->m_PhysicalPointToIndex = this->m_IndexToPhysicalPoint.GetInverse( );
197   this->Modified( );
198 }
199
200 #endif // __cpExtensions__DataStructures__PolyLineParametricPath__hxx__
201
202 // eof - $RCSfile$