File: FrameSpringForceField.h

package info (click to toggle)
sofa-framework 1.0~beta4-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 88,624 kB
  • sloc: cpp: 151,120; ansic: 2,387; xml: 581; sh: 436; makefile: 67
file content (215 lines) | stat: -rw-r--r-- 8,943 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/

#ifndef SOFA_COMPONENT_FORCEFIELD_FRAMESPRINGFORCEFIELD_H
#define SOFA_COMPONENT_FORCEFIELD_FRAMESPRINGFORCEFIELD_H

#include <sofa/core/componentmodel/behavior/PairInteractionForceField.h>
#include <sofa/core/componentmodel/behavior/MechanicalState.h>
#include <sofa/defaulttype/Vec.h>
#include <vector>
#include <sofa/defaulttype/Mat.h>
using namespace sofa::defaulttype;

namespace sofa
{

  namespace component
  {

    namespace forcefield
    {

      template<class DataTypes>
      class FrameSpringForceFieldInternalData
      {
        public:
      };

      /** FrameSpringForceField simulates 6D springs between moving frames
        Use stiffnessTrans vector to specify the directionnal stiffnesses (on each local axis)
        Use stiffnessRot vector to specify the rotational stiffnesses (on each local axis)
      */
      template<class DataTypes>
      class FrameSpringForceField : public core::componentmodel::behavior::PairInteractionForceField<DataTypes>, public virtual core::objectmodel::BaseObject
      {
        public:
          typedef typename core::componentmodel::behavior::PairInteractionForceField<DataTypes> Inherit;
          typedef typename DataTypes::VecCoord VecCoord;
          typedef typename DataTypes::VecDeriv VecDeriv;
          typedef typename DataTypes::Coord Coord;
          typedef typename DataTypes::Deriv Deriv;
          typedef typename Coord::value_type Real;
          typedef core::componentmodel::behavior::MechanicalState<DataTypes> MechanicalState;
          enum { N=Coord::static_size };
          typedef defaulttype::Mat<N,N,Real> Mat;
          typedef Vec<N,Real> VecN;


          class Spring
          {
            public:
              int  m1, m2;             /// the two extremities of the spring: masses m1 and m2 ( indexes of the DOFs)
              VecN vec1, vec2;
              Real kd;                 /// damping factor

              Real stiffnessTrans;  ///stiffness to apply on axis where the translations are free (default 0.0)
              Real stiffnessRot;    ///stiffness to apply on axis where the rotations are free (default 0.0)

              ///constructors
              Spring ( int m1=0, int m2=0, Real softKst=0, Real softKsr=0, Real kd=0 )
                  : m1 ( m1 ), m2 ( m2 ), kd ( kd ), stiffnessTrans ( softKst ), stiffnessRot ( softKsr )
              {
              }

              //accessors
              Real getStiffnessRotation() {return stiffnessRot;}
              Real getStiffnessTranslation() {return stiffnessTrans;}
              VecN getInitVec1() {return vec1;}

              //affectors
              void setStiffnessRotation ( Real ksr ) {    stiffnessRot = ksr;  }
              void setStiffnessTranslation ( Real kst ) { stiffnessTrans = kst;  }
              void setInitVec1 ( const VecN& l ) { vec1=l; }
              void setInitVec2 ( const VecN& l ) { vec2=l; }
              void setDamping ( Real _kd ) {  kd = _kd;   }


              inline friend std::istream& operator >> ( std::istream& in, Spring& s )
              {
                //default joint is a free rotation joint --> translation is bloqued, rotation is free

                std::string str;
                in>>str;
                if ( str == "BEGIN_SPRING" )
                {
                  in>>s.m1>>s.m2; //read references
                  in>>str;
                  while ( str != "END_SPRING" )
                  {
                    if ( str == "KS_T" )
                      in>>s.stiffnessTrans;
                    else if ( str == "KS_R" )
                      in>>s.stiffnessRot;
                    else if ( str == "KD" )
                      in>>s.kd;
                    else if ( str == "VEC1" )
                      in>>s.vec1;
                    else if ( str == "VEC2" )
                      in>>s.vec2;
                    else
                    {
		      std::cerr<<"Error parsing Spring : Unknown Attribute "<<str<<std::endl;
                      return in;
                    }

                    in>>str;
                  }
                }

                return in;
              }

              friend std::ostream& operator << ( std::ostream& out, const Spring& s )
              {
                out<<"BEGIN_SPRING  "<<s.m1<<" "<<s.m2<<"  ";

                if ( s.stiffnessTrans != 0.0 )
                  out<<"KS_T "<<s.stiffnessTrans<<"  ";
                if ( s.stiffnessRot != 0.0 )
                  out<<"KS_R "<<s.stiffnessRot<<"  ";
                if ( s.kd != 0.0 )
                  out<<"KD "<<s.kd<<"  ";
                if ( s.vec1!= VecN ( 0, 0, 0 ) )
                  out<<"VEC1 "<<s.vec1<<"  ";
                if ( s.vec2!= VecN ( 0, 0, 0 ) )
                  out<<"VEC2 "<<s.vec2<<"  ";

                out<<"END_SPRING"<<std::endl;
                return out;
              }

          };
// end inner class spring



        protected:

          double m_potentialEnergy;
          /// the list of the springs
          Data<sofa::helper::vector<Spring> > springs;
          /// the list of the local referentials of the springs
          VecCoord springRef;
          /// bool to allow the display of the 2 parts of springs torsions
          Data<bool> showLawfulTorsion;
          Data<bool> showExtraTorsion;

          FrameSpringForceFieldInternalData<DataTypes> data;


          /// Accumulate the spring force and compute and store its stiffness
          void addSpringForce ( double& potentialEnergy, VecDeriv& f1, const VecCoord& p1, const VecDeriv& v1, VecDeriv& f2, const VecCoord& p2, const VecDeriv& v2, int i, /*const*/ Spring& spring );
          /// Apply the stiffness, i.e. accumulate df given dx
          void addSpringDForce ( VecDeriv& df1, const VecDeriv& dx1, VecDeriv& df2, const VecDeriv& dx2, int i, /*const*/ Spring& spring );



        public:
          FrameSpringForceField ( MechanicalState* object1, MechanicalState* object2 );
          FrameSpringForceField();

          core::componentmodel::behavior::MechanicalState<DataTypes>* getObject1() { return this->mstate1; }
          core::componentmodel::behavior::MechanicalState<DataTypes>* getObject2() { return this->mstate2; }

          virtual void init();

          virtual void addForce ( VecDeriv& f1, VecDeriv& f2, const VecCoord& x1, const VecCoord& x2, const VecDeriv& v1, const VecDeriv& v2 );

          virtual void addDForce ( VecDeriv& df1, VecDeriv& df2, const VecDeriv& dx1, const VecDeriv& dx2 );

          virtual double getPotentialEnergy ( const VecCoord&, const VecCoord& ) { return m_potentialEnergy; }

          sofa::helper::vector<Spring> * getSprings() { return springs.beginEdit(); }

          void draw();

          // -- Modifiers

          void clear ( int reserve=0 );

          void addSpring ( const Spring& s );

          void addSpring ( int m1, int m2, Real softKst, Real softKsr, Real kd );

      };

    } // namespace forcefield

  } // namespace component

} // namespace sofa

#endif