00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef MASSSPRINGSOLVER_H
00019 #define MASSSPRINGSOLVER_H
00020
00021 #include <utility>
00022 #include <animal/vector.h>
00023 #include <animal/physicalSolver.h>
00024 #include <animal/odeImplicitSolver.h>
00025 #include<iostream>
00026 using std::cerr; using std::endl;
00027
00028 namespace animal
00029 {
00039 template<class t_Positions,
00040 class t_Vector,
00041 class t_InvMasses,
00042 class t_VecReal,
00043 class t_VecIndex
00044 >
00045 class MassSpringSolver
00046 : public OdeImplicitSolver<t_Positions,t_Vector,typename value_type<t_VecReal>::type>
00047 , public PhysicalSolver<t_Positions,t_Vector,t_InvMasses,typename value_type<t_VecReal>::type>
00048 {
00049 public:
00050
00051 typedef t_Positions Positions;
00052 typedef typename container_traits<Positions>::value_type Position;
00053 typedef t_Vector Vector;
00054 typedef typename container_traits<Vector>::value_type Vec;
00055 typedef t_VecReal VecReal;
00056 typedef typename value_type<VecReal>::type Real;
00057 typedef t_VecIndex VecIndex;
00058 typedef typename value_type<VecIndex>::type Index;
00059
00060
00061
00062 typedef PhysicalSolver<t_Positions,t_Vector,t_InvMasses,typename value_type<t_VecReal>::type> _PhysicalSolver;
00063 typedef OdeImplicitSolver<t_Positions,t_Vector,typename value_type<t_VecReal>::type> _OdeImplicitSolver;
00064
00065 MassSpringSolver();
00066 ~MassSpringSolver();
00070 virtual void setMethod( int );
00072 virtual int getMethod() const;
00074 void setIsotropy( Real iso );
00076 Real getIsotropy() const;
00078
00082 virtual void set_common_damping_ratio( const Real& _newVal);
00084 virtual const Real& get_common_damping_ratio();
00086
00090 virtual void set_spring_indices( VecIndex* _newVal);
00092 virtual void set_stiffnesses( VecReal* _newVal);
00094
00098 inline void create_rest_lengths( const Positions& p )
00099 {
00100 assert( size(*_links)%2==0 );
00101 if(!_restLengths) _restLengths = new VecReal;
00102 _own_restLengths = true;
00103 for( unsigned int i=0; i<_links->size(); i+=2 )
00104 {
00105 Position x = p[(*_links)[i]];
00106 cerr<<"p0 = " << x <<", p1 = "<< p[(*_links)[i+1]]
00107 << endl;
00108 v_meq( x, p[(*_links)[i+1]] );
00109 _restLengths->push_back( v_norm( x ) );
00110 }
00111 cerr<<"MassSpringSolver<P,V,Im,R,I>::create_rest_lengths: "
00112 << *_restLengths
00113 << endl;
00114 }
00115
00119 inline void set_rest_lengths( VecReal* r )
00120 {
00121 assert( r );
00122 assert(_links);
00123 cerr<<"size(*_links)== "<<size(*_links)<<endl;
00124 cerr<<"size(*r)== "<<size(*r)<<endl;
00125 assert( size(*_links)/2==size(*r) );
00126 this->_restLengths = r;
00127 _own_restLengths = false;
00128 }
00130 VecReal* get_rest_lengths(){ return _restLengths;}
00131
00133 static Real get_thresholdDistance(){
00134 return _thresholdDistance;
00135 }
00137 static void set_thresholdDistance(Real threshold){
00138 _thresholdDistance=threshold;
00139 }
00140
00142 bool get_debugInfo() const{return _debug;}
00143
00145 void set_debugInfo(bool b) {_debug=b;}
00146
00147
00148
00149 VecIndex* _links;
00150 VecReal* _stiffnesses;
00151 VecReal* _restLengths;
00152 bool _own_restLengths;
00153
00154 animal::vector<Vec> _directions;
00155 Real _isotropy;
00156
00162 virtual void solveODE( Positions& pos, Vector& vel, Real dt);
00163
00165 static void accumulateSpringForce(
00166 Vec& fa, Vec& fb,
00167 const Vec& pa, const Vec& pb,
00168 const Vec& va, const Vec& vb,
00169 const Real& stiffness, const Real& dampingRatio, const Real& restLength
00170 );
00175 static void accumulateSpringForceAndGradientMatrix(
00176 Vec& fa, Vec& fb, Vec& grad,
00177 const Vec& pa, const Vec& pb,
00178 const Vec& va, const Vec& vb,
00179 const Real& stiffness, const Real& dampingRatio, const Real& restLength
00180 );
00182 static bool computeDistanceAndDirection(Real& d, Vec& u, const Vec& pa, const Vec& pb);
00184 virtual void computeForces( Vector& f, const Positions& p, const Vector& v);
00185
00187 virtual void computeAccelerations (Vector & acc, const Positions & pos, const Vector & vel);
00188
00190 virtual void computeAccelerationsAndStiffness (Vector & acc, const Positions & pos, const Vector & vel);
00191
00192
00193
00194
00195
00196
00197
00198
00203 virtual void applyForces( Vector& a, const Vector& f);
00204
00210 virtual void v_eq_h_dfdx_x( Vector& v, Real h, const Vector& x );
00211
00217 virtual void v_peq_h_dfdx_x( Vector& v, Real h, const Vector& x );
00218
00219
00220
00221 static Real _thresholdDistance;
00222 static bool _debug;
00223
00224 };
00225
00226 }
00227 #endif