File: InteractionEllipsoidForceField.h

package info (click to toggle)
sofa-framework 1.0~beta4-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 88,688 kB
  • ctags: 27,205
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 67
file content (173 lines) | stat: -rw-r--r-- 6,388 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
/******************************************************************************
*       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_INTERACTION_ELLIPSOIDFORCEFIELD_H
#define SOFA_COMPONENT_FORCEFIELD_INTERACTION_ELLIPSOIDFORCEFIELD_H

#include <sofa/core/componentmodel/behavior/MixedInteractionForceField.h>
#include <sofa/core/componentmodel/behavior/MechanicalState.h>
#include <sofa/core/objectmodel/Data.h>
#include <sofa/defaulttype/RigidTypes.h>



namespace sofa
{

namespace component
{

namespace interactionforcefield
{
using namespace sofa::defaulttype;

/// This class can be overridden if needed for additionnal storage within template specializations.
template<class DataTypes1, class DataTypes2>
class InteractionEllipsoidForceFieldInternalData
{
public:
};

template<class DataTypes1, class DataTypes2>
class InteractionEllipsoidForceField : public core::componentmodel::behavior::MixedInteractionForceField<DataTypes1, DataTypes2>, public virtual core::objectmodel::BaseObject
{
public:
	typedef core::componentmodel::behavior::MixedInteractionForceField<DataTypes1, DataTypes2> Inherit;
	typedef typename DataTypes1::VecCoord VecCoord1;
	typedef typename DataTypes1::VecDeriv VecDeriv1;
	typedef typename DataTypes1::Coord Coord1;
	typedef typename DataTypes1::Deriv Deriv1;
	typedef typename Coord1::value_type Real1;
	typedef typename DataTypes2::VecCoord VecCoord2;
	typedef typename DataTypes2::VecDeriv VecDeriv2;
	typedef typename DataTypes2::Coord Coord2;
	typedef typename DataTypes2::Deriv Deriv2;
	typedef typename Coord2::value_type Real2;

	enum { N=Coord1::static_size };
	typedef defaulttype::Mat<N,N,Real1> Mat;
protected:
    class Contact
    {
    public:
        int index;
		Deriv1 pos;
		Vec<3,SReal> bras_levier;
        Mat m;
	Contact( int index=0, const Mat& m=Mat())
	  : index(index), m(m)
	  {
	  }

        inline friend std::istream& operator >> ( std::istream& in, Contact& c ){
	  in>>c.index>>c.m;
            return in;
        }

        inline friend std::ostream& operator << ( std::ostream& out, const Contact& c ){
	  out << c.index << " " << c.m ;
	  return out;
        }

    };

    Data<sofa::helper::vector<Contact> > contacts;

    InteractionEllipsoidForceFieldInternalData<DataTypes1, DataTypes2> data;
	
	bool calcF(const Coord1& p1, const Deriv1 &v1, Deriv1& f1,Mat& dfdx);
	void initCalcF();

public:

    Data<Coord1> center;
    Data<Coord1> vradius;
    Data<Real1> stiffness;
    Data<Real1> damping;
    Data<defaulttype::Vec3f> color;
    Data<bool> bDraw;
	Data<int> object2_dof_index;

    InteractionEllipsoidForceField()
      : contacts(initData(&contacts,"contacts", "Contacts"))
      , center(initData(&center, "center", "ellipsoid center"))
      , vradius(initData(&vradius, "vradius", "ellipsoid radius"))
      , stiffness(initData(&stiffness, (Real1)500, "stiffness", "force stiffness (positive to repulse outward, negative inward)"))
      , damping(initData(&damping, (Real1)5, "damping", "force damping"))
      , color(initData(&color, defaulttype::Vec3f(0.0f,0.5f,1.0f), "color", "ellipsoid color"))
      , bDraw(initData(&bDraw, true, "draw", "enable/disable drawing of the ellipsoid"))
	  , object2_dof_index(initData(&object2_dof_index, (int)0, "object2_dof_index", "Dof index of object 2 where the forcefield is attached"))
    {
		_update_pos_relative = true;
		vars.center =center.getValue();
// 		printf("\n vars.center : %f %f %f",vars.center.x(),vars.center.y(),vars.center.z());
		_orientation.clear();
    }

    void setStiffness(Real1 stiff)
    {
        stiffness.setValue( stiff );
    }

    void setDamping(Real1 damp)
    {
        damping.setValue( damp );
    }

    virtual void addForce(VecDeriv1& f1, VecDeriv2& f2, const VecCoord1& p1, const VecCoord2& p2, const VecDeriv1& v1, const VecDeriv2& v2);

    virtual void addForce2(VecDeriv1& f1, VecDeriv2& f2, const VecCoord1& p1, const VecCoord2& p2, const VecDeriv1& v1, const VecDeriv2& v2);
	
    virtual void addDForce(VecDeriv1& df1, VecDeriv2& df2, const VecDeriv1& dx1, const VecDeriv2& dx2);
	
    virtual double getPotentialEnergy(const VecCoord1& x1, const VecCoord2& x2);

	void reinit() {_update_pos_relative = true;}

    void draw();

protected:
	struct TempVars {
	    Coord1 center; // center in the local frame ;
		Coord1 r;
		Real1 stiffness;
		Real1 stiffabs;
		Coord1 inv_r2;
		Coord2 pos6D;
	} vars;

	bool _update_pos_relative;
	VecCoord1 X1;
	VecCoord2 X2;
	Quat _orientation;
};

} // namespace interactionforcefield

} // namespace component

} // namespace sofa


#endif