1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
5 #ifndef __CPPLUGINS__EXTENSIONS__ALGORITHMS__BEZIERCURVEFUNCTION__HXX__
6 #define __CPPLUGINS__EXTENSIONS__ALGORITHMS__BEZIERCURVEFUNCTION__HXX__
8 // -------------------------------------------------------------------------
10 void cpPlugins::Extensions::Algorithms::BezierCurveFunction< V >::
11 AddPoint( const TVector& v )
13 this->m_Vectors.push_back( v );
14 this->m_DerivativeUpdated = false;
18 // -------------------------------------------------------------------------
20 unsigned int cpPlugins::Extensions::Algorithms::BezierCurveFunction< V >::
21 GetNumberOfPoints( ) const
23 return( this->m_Vectors.size( ) );
26 // -------------------------------------------------------------------------
28 typename cpPlugins::Extensions::Algorithms::BezierCurveFunction< V >::
29 TVector cpPlugins::Extensions::Algorithms::BezierCurveFunction< V >::
30 Evaluate( const TScalar& u ) const
32 TVectorsContainer Q = this->m_Vectors;
33 unsigned int n = Q.size( );
34 TScalar _1u = TScalar( 1 ) - u;
36 for( unsigned int k = 1; k < n; k++ )
38 // CM Fixed a bug appearing under Windows : changed the stopping
39 // condition from <= to <. Otherwise, on the last step, an element out
40 // of the range of vector Q is accessed (Q[ i + 1 ])...
41 for( unsigned int i = 0; i < n - k; i++ )
42 Q[ i ] = ( Q[ i ] * _1u ) + ( Q[ i + 1 ] * u );
48 // -------------------------------------------------------------------------
50 typename cpPlugins::Extensions::Algorithms::BezierCurveFunction< V >::
51 TFrame cpPlugins::Extensions::Algorithms::BezierCurveFunction< V >::
52 EvaluateFrenetFrame( const TScalar& u ) const
55 fr.Fill( TScalar( 0 ) );
56 if( TVector::Dimension == 2 )
58 this->_UpdateDerivative( );
59 this->m_Derivative->_UpdateDerivative( );
61 TVector vT = this->m_Derivative->Evaluate( u );
62 TScalar nvT = vT.GetNorm( );
63 if( TScalar( 0 ) < nvT )
66 fr[ 0 ][ 0 ] = vT[ 0 ];
67 fr[ 1 ][ 0 ] = vT[ 1 ];
69 fr[ 0 ][ 1 ] = -vT[ 1 ];
70 fr[ 1 ][ 1 ] = vT[ 0 ];
72 else if( TVector::Dimension == 3 )
74 this->_UpdateDerivative( );
75 this->m_Derivative->_UpdateDerivative( );
76 TVector vT = this->m_Derivative->Evaluate( u );
77 TScalar nvT = vT.GetNorm( );
78 if( nvT > TScalar( 0 ) )
81 TVector vN = this->m_Derivative->m_Derivative->Evaluate( u );
82 TScalar nvN = vN.GetNorm( );
83 if( nvT > TScalar( 0 ) )
87 vB[ 0 ] = ( vT[ 1 ] * vN[ 2 ] ) - ( vT[ 2 ] * vN[ 1 ] );
88 vB[ 1 ] = ( vT[ 2 ] * vN[ 0 ] ) - ( vT[ 0 ] * vN[ 2 ] );
89 vB[ 2 ] = ( vT[ 0 ] * vN[ 1 ] ) - ( vT[ 1 ] * vN[ 0 ] );
91 for( unsigned int d = 0; d < 3; d++ )
93 fr[ d ][ 0 ] = vT[ d ];
94 fr[ d ][ 1 ] = vN[ d ];
95 fr[ d ][ 2 ] = vB[ d ];
100 std::cerr << "ERROR normal" << std::endl;
103 std::cerr << "ERROR tangent" << std::endl;
109 // -------------------------------------------------------------------------
111 typename cpPlugins::Extensions::Algorithms::BezierCurveFunction< V >::
112 TScalar cpPlugins::Extensions::Algorithms::BezierCurveFunction< V >::
113 EvaluateLength( ) const
115 unsigned int n = this->GetNumberOfPoints( ) << 1;
116 TScalar d = TScalar( 0 );
117 TVector v0 = this->Evaluate( 0 );
118 for( unsigned int i = 1; i < n; i++ )
120 TVector v1 = this->Evaluate( TScalar( i ) / TScalar( n - 1 ) );
121 d += ( v1 - v0 ).GetNorm( );
128 // -------------------------------------------------------------------------
130 cpPlugins::Extensions::Algorithms::BezierCurveFunction< V >::
131 BezierCurveFunction( )
133 m_DerivativeUpdated( false )
137 // -------------------------------------------------------------------------
139 void cpPlugins::Extensions::Algorithms::BezierCurveFunction< V >::
140 _UpdateDerivative( ) const
142 if( this->m_DerivativeUpdated )
145 this->m_Derivative = Self::New( );
146 unsigned int n = this->m_Vectors.size( ) - 1;
147 for( unsigned int i = 0; i < n; i++ )
148 this->m_Derivative->AddPoint(
149 TScalar( n ) * ( this->m_Vectors[ i + 1 ] - this->m_Vectors[ i ] )
153 #endif // __CPPLUGINS__EXTENSIONS__ALGORITHMS__BEZIERCURVEFUNCTION__HXX__