![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsSpringRef.cpp 00003 00004 SpringRef class is a class for a spring connecting between two ParticleRef. 00005 It needs to know (ref) the address of the particles. 00006 00007 SUKITTI PUNAK (02/24/2006) 00008 UPDATE (03/01/2006) 00009 ******************************************************************************/ 00010 #include "TAPsSpringRef.hpp" 00011 // Using Inclusion Model (i.e. definitions are included in declarations) 00012 // (this name.cpp is included in name.hpp) 00013 // Each friend is defined directly inside its declaration. 00014 00015 BEGIN_NAMESPACE_TAPs 00016 //============================================================================= 00017 // Constructors and Destructor 00018 //----------------------------------------------------------------------------- 00019 // SpringRef Constructor 00020 template <typename T> 00021 SpringRef<T>::SpringRef 00022 ( 00023 ParticleRef<T> & P1, 00024 ParticleRef<T> & P2, 00025 T k, T d, T l, 00026 int hops 00027 ) : 00028 m_refP1( &P1 ), 00029 m_refP2( &P2 ), 00030 m_tK( k ), 00031 m_tD( d ), 00032 m_tL( l ), 00033 m_iNumHops( hops ) 00034 {} 00035 //----------------------------------------------------------------------------- 00036 // SpringRef Constructor 00037 template <typename T> 00038 SpringRef<T>::SpringRef 00039 ( 00040 ParticleRef<T> & P1, 00041 ParticleRef<T> & P2, 00042 T k, T d, 00043 int hops 00044 ) : 00045 m_refP1( &P1 ), 00046 m_refP2( &P2 ), 00047 m_tK( k ), 00048 m_tD( d ), 00049 m_iNumHops( hops ) 00050 { 00051 T x = P2.GetPosition().GetX() - P1.GetPosition().GetX(); 00052 T y = P2.GetPosition().GetY() - P1.GetPosition().GetY(); 00053 T z = P2.GetPosition().GetZ() - P1.GetPosition().GetZ(); 00054 m_tL = sqrt( x*x + y*y + z*z ); 00055 } 00056 //----------------------------------------------------------------------------- 00057 // SpringRef Destructor 00058 template <typename T> 00059 SpringRef<T>::~SpringRef() 00060 {} 00061 //----------------------------------------------------------------------------- 00062 //============================================================================= 00063 // Calculate and Return the (Internal) Force of a Spring 00064 //----------------------------------------------------------------------------- 00065 template <typename T> 00066 Vector3<T> SpringRef<T>::GetForce1() const 00067 { 00068 00069 std::cout << "OBSOLETED: GetForce1() should not be called" << std::endl; 00070 exit( 1 ); 00071 00072 // Not Home Spring 00073 if ( m_iNumHops > 0 ) { 00074 // Local Variables ------------------------------------------ 00075 // spring current length vector 00076 Vector3<T> cL = ( m_refP1->GetPosition() - m_refP2->GetPosition() ); 00077 // spring current length scalar 00078 T scl = cL.Length(); 00079 cL /= scl; // normalize the length vector 00080 // velocity difference of the two particles linked by the spring 00081 Vector3<T> V = ( m_refP1->GetVelocity() - m_refP2->GetVelocity() ); 00082 //----------------------------------------------------------- 00083 // Formula from Physically Based Modeling (Siggraph 2001 Course Note) 00084 // F1 = -{Ks(l - r) + Kd[(V1-V2)*L]/l}*L/l 00085 // F2 = -F1 00086 // where Ks = spring stiffness, Kd = spring damping, 00087 // V1 and V2 are velocity of particles linked by the spring 00088 // r = spring rest length 00089 // L = vector of difference of positions of particle#1 and #2 00090 // l = magnitude of L 00091 return ( ( m_tK * (scl - m_tL) ) + ( m_tD * V * cL ) ) * cL; 00092 } 00093 // Home Spring 00094 else { 00095 // Local Variables ------------------------------------------ 00096 // spring current length vector 00097 Vector3<T> cL = ( m_refP1->GetPosition() - m_refP2->GetPosition() ); 00098 // spring current length scalar 00099 T scl = cL.Length(); 00100 if ( scl == TAPs::Math<T>::ZERO ) return TAPs::CGMath<T>::ZeroVector; 00101 cL /= scl; // normalize the length vector 00102 // velocity difference of the two particles linked by the spring 00103 Vector3<T> V = ( m_refP1->GetVelocity() - m_refP2->GetVelocity() ); 00104 //----------------------------------------------------------- 00105 // Formula from Physically Based Modeling (Siggraph 2001 Course Note) 00106 // F1 = -{Ks(l - r) + Kd[(V1-V2)*L]/l}*L/l 00107 // F2 = -F1 00108 // where Ks = spring stiffness, Kd = spring damping, 00109 // V1 and V2 are velocity of particles linked by the spring 00110 // r = spring rest length 00111 // L = vector of difference of positions of particle#1 and #2 00112 // l = magnitude of L 00113 //return ( ( m_tK * (scl - m_tL) ) + ( m_tD * V * cL ) ) * cL; 00114 return ( ( m_tK * scl ) + ( m_tD * V * cL ) ) * cL; // since m_tL is zero 00115 } 00116 } 00117 //----------------------------------------------------------------------------- 00118 //============================================================================= 00119 // Calculate and Return the (Internal) Force of a Spring 00120 //----------------------------------------------------------------------------- 00121 template <typename T> 00122 Vector3<T> SpringRef<T>::GetForce2() const 00123 { 00124 // Not Home Spring 00125 if ( m_iNumHops > 0 ) { 00126 // Local Variables ------------------------------------------ 00127 // spring current length vector 00128 Vector3<T> cL = ( m_refP1->GetPosition() - m_refP2->GetPosition() ); 00129 // spring current length scalar 00130 T scl = cL.Length(); 00131 cL /= scl; // normalize the length vector 00132 // velocity difference of the two particles linked by the spring 00133 Vector3<T> V = ( m_refP1->GetVelocity() - m_refP2->GetVelocity() ); 00134 //----------------------------------------------------------- 00135 // Formula from Physically Based Modeling (Siggraph 2001 Course Note) 00136 // F1 = -{Ks(l - r) + Kd[(V1-V2)*L]/l}*L/l 00137 // F2 = -F1 00138 // where Ks = spring stiffness, Kd = spring damping, 00139 // V1 and V2 are velocity of particles linked by the spring 00140 // r = spring rest length 00141 // L = vector of difference of positions of particle#1 and #2 00142 // l = magnitude of L 00143 return ( ( m_tK * (scl - m_tL) ) + ( m_tD * V * cL ) ) * cL; 00144 } 00145 // Home Spring 00146 else { 00147 // Local Variables ------------------------------------------ 00148 // spring current length vector 00149 Vector3<T> cL = ( m_refP1->GetPosition() - m_refP2->GetPosition() ); 00150 // spring current length scalar 00151 T scl = cL.Length(); 00152 if ( scl == TAPs::Math<T>::ZERO ) return TAPs::CGMath<T>::ZeroVector; 00153 cL /= scl; // normalize the length vector 00154 // velocity difference of the two particles linked by the spring 00155 Vector3<T> V = ( m_refP1->GetVelocity() - m_refP2->GetVelocity() ); 00156 //----------------------------------------------------------- 00157 // Formula from Physically Based Modeling (Siggraph 2001 Course Note) 00158 // F1 = -{Ks(l - r) + Kd[(V1-V2)*L]/l}*L/l 00159 // F2 = -F1 00160 // where Ks = spring stiffness, Kd = spring damping, 00161 // V1 and V2 are velocity of particles linked by the spring 00162 // r = spring rest length 00163 // L = vector of difference of positions of particle#1 and #2 00164 // l = magnitude of L 00165 //return ( ( m_tK * (scl - m_tL) ) + ( m_tD * V * cL ) ) * cL; 00166 return ( ( m_tK * scl ) + ( m_tD * V * cL ) ) * cL; // since m_tL is zero 00167 } 00168 } 00169 //----------------------------------------------------------------------------- 00170 //============================================================================= 00171 // Calculate and Return the (Internal) Force of a Spring 00172 //----------------------------------------------------------------------------- 00173 template <typename T> 00174 void SpringRef<T>::GetForce( Vector3<T> & F1, Vector3<T> & F2 ) const 00175 { 00176 F2 = GetForce2(); 00177 F1 = -F2; 00178 } 00179 //============================================================================= 00180 // Calculate and Return the (Internal) Force of a Spring 00181 //----------------------------------------------------------------------------- 00182 template <typename T> 00183 void SpringRef<T>::CalAndSetForce() 00184 { 00185 Vector3<T> F2 = GetForce2(); 00186 if ( m_refP2->GetFixStatus() ) { 00187 m_refP2->SetVelocity( 0, 0, 0 ); 00188 m_refP2->SetForce( 0, 0 , 0 ); 00189 } 00190 else { 00191 m_refP2->SetForce( m_refP2->GetForce() + F2 ); 00192 } 00193 if ( m_refP1->GetFixStatus() ) { 00194 m_refP1->SetVelocity( 0, 0, 0 ); 00195 m_refP1->SetForce( 0, 0 , 0 ); 00196 } 00197 else { 00198 m_refP1->SetForce( m_refP1->GetForce() - F2 ); 00199 } 00200 } 00201 //----------------------------------------------------------------------------- 00202 //============================================================================= 00203 // Get/Set Functions 00204 //----------------------------------------------------------------------------- 00205 // Get Particle #1 00206 template <typename T> 00207 inline ParticleRef<T> & SpringRef<T>::GetParticleOne () 00208 { return *m_refP1; } 00209 //----------------------------------------------------------------------------- 00210 // Get Particle #2 00211 template <typename T> 00212 inline ParticleRef<T> & SpringRef<T>::GetParticleTwo () 00213 { return *m_refP2; } 00214 //----------------------------------------------------------------------------- 00215 // Get Particle #1 00216 template <typename T> 00217 inline ParticleRef<T> const & SpringRef<T>::GetParticleOne () const 00218 { return *m_refP1; } 00219 //----------------------------------------------------------------------------- 00220 // Get Particle #2 00221 template <typename T> 00222 inline ParticleRef<T> const & SpringRef<T>::GetParticleTwo () const 00223 { return *m_refP2; } 00224 //----------------------------------------------------------------------------- 00225 // Get Constant K 00226 template <typename T> 00227 inline T SpringRef<T>::GetConstantK () const 00228 { return m_tK; } 00229 //----------------------------------------------------------------------------- 00230 // Get Damping D 00231 template <typename T> 00232 inline T SpringRef<T>::GetDampingD () const 00233 { return m_tD; } 00234 //----------------------------------------------------------------------------- 00235 // Get Rest Length L 00236 template <typename T> 00237 inline T SpringRef<T>::GetRestLengthL () const 00238 { return m_tL; } 00239 //----------------------------------------------------------------------------- 00240 // Get Current Length 00241 template <typename T> 00242 inline T SpringRef<T>::GetCurrentLength () const 00243 { return ( m_refP1->GetPosition() - m_refP2->GetPosition() ).Length(); } 00244 //----------------------------------------------------------------------------- 00245 // Set Constant K 00246 template <typename T> 00247 inline void SpringRef<T>::SetConstantK ( T Ks ) 00248 { m_tK = Ks; } 00249 //----------------------------------------------------------------------------- 00250 // Set Damping D 00251 template <typename T> 00252 inline void SpringRef<T>::SetDampingD ( T Kd ) 00253 { m_tD = Kd; } 00254 //----------------------------------------------------------------------------- 00255 // Set Rest Length L 00256 template <typename T> 00257 inline void SpringRef<T>::SetRestLengthL ( T length ) 00258 { m_tL = length; } 00259 //----------------------------------------------------------------------------- 00260 //============================================================================= 00261 // Spring Creations (Static Functions) 00262 //----------------------------------------------------------------------------- 00263 template <typename T> 00264 void SpringRef<T>::SpringsCreation ( 00265 HEVertexList<T> * meshVertex, // I/P: Mesh XVertex 00266 std::list<int> & vertexNo, // O/P: vertex# associates Mesh XVertex w/ Spring# 00267 std::list< ParticleRef<T> > & particleList, // O/P: Particle List 00268 T mass, // I/P: mass for each particle 00269 std::list< SpringRef<T> > & springNo, // O/P: Spring List 00270 T Ks, T Kd, // I/P: Spring Stiffness and Damping Constant 00271 int hopDistance // I/P: Number of distance/rigidity for spring connection 00272 ) 00273 { 00274 if ( !meshVertex ) return; 00275 //---------------------------------------------------------------- 00276 HEVertex<T> * vertex = meshVertex.Head(); 00277 //---------------------------------------------------------------- 00278 vertexNo.clear(); 00279 for ( int i = 0; i < meshVertex.Size(); ++i ) { 00280 // vertex = 00281 } 00282 //---------------------------------------------------------------- 00283 //---------------------------------------------------------------- 00284 //---------------------------------------------------------------- 00285 } 00286 //----------------------------------------------------------------------------- 00287 //============================================================================= 00288 00289 //----------------------------------------------------------------------------- 00290 #if defined(__gl_h_) || defined(__GL_H__) 00291 //============================================================================= 00292 // DrawByOpenGL 00293 //----------------------------------------------------------------------------- 00294 template <typename T> 00295 void SpringRef<T>::DrawByOpenGL () const 00296 { 00297 glPushAttrib( GL_CURRENT_BIT | GL_LIGHTING_BIT ); 00298 glPushMatrix(); 00299 //---------------------------------------------------------------- 00300 glDisable( GL_LIGHTING ); 00301 glPointSize( 3 ); 00302 glLineWidth( 1 ); 00303 //---------------------------------------------------------- 00304 // Draw Points 00305 glBegin( GL_POINTS ); 00306 glColor3f( 1, 1, 0 ); 00307 glVertex3f( static_cast<GLfloat>(GetParticleOne().GetPosition()[0]), 00308 static_cast<GLfloat>(GetParticleOne().GetPosition()[1]), 00309 static_cast<GLfloat>(GetParticleOne().GetPosition()[2]) ); 00310 glColor3f( 0, 1, 1 ); 00311 glVertex3f( static_cast<GLfloat>(GetParticleTwo().GetPosition()[0]), 00312 static_cast<GLfloat>(GetParticleTwo().GetPosition()[1]), 00313 static_cast<GLfloat>(GetParticleTwo().GetPosition()[2]) ); 00314 glEnd(); 00315 //---------------------------------------------------------- 00316 // Draw Lines 00317 glBegin( GL_LINES ); 00318 if ( m_iNumHops > 0 ) 00319 glColor3f( 1, 0.5f/m_iNumHops, 1 ); 00320 else 00321 glColor3f( 1, 1, 1 ); 00322 glVertex3f( static_cast<GLfloat>(GetParticleOne().GetPosition()[0]), 00323 static_cast<GLfloat>(GetParticleOne().GetPosition()[1]), 00324 static_cast<GLfloat>(GetParticleOne().GetPosition()[2]) ); 00325 glVertex3f( static_cast<GLfloat>(GetParticleTwo().GetPosition()[0]), 00326 static_cast<GLfloat>(GetParticleTwo().GetPosition()[1]), 00327 static_cast<GLfloat>(GetParticleTwo().GetPosition()[2]) ); 00328 glEnd(); 00329 //---------------------------------------------------------------- 00330 glPopMatrix(); 00331 glPopAttrib(); 00332 } 00333 //----------------------------------------------------------------------------- 00334 template <typename T> 00335 void SpringRef<T>::DrawByOpenGL ( int hopNumber ) const 00336 { 00337 if ( hopNumber == m_iNumHops ) { 00338 DrawByOpenGL(); 00339 } 00340 } 00341 //----------------------------------------------------------------------------- 00342 template <typename T> 00343 void SpringRef<T>::DrawByOpenGL ( int startHopNumber, int endHopNumber ) const 00344 { 00345 if ( startHopNumber <= m_iNumHops && m_iNumHops <= endHopNumber ) { 00346 DrawByOpenGL(); 00347 } 00348 } 00349 //----------------------------------------------------------------------------- 00350 #endif 00351 //============================================================================= 00352 //----------------------------------------------------------------------------- 00353 //============================================================================= 00354 END_NAMESPACE_TAPs 00355 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00356 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8