SphereForceField.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 #ifndef SOFA_COMPONENT_FORCEFIELD_SPHEREFORCEFIELD_H
00026 #define SOFA_COMPONENT_FORCEFIELD_SPHEREFORCEFIELD_H
00027 
00028 #include <sofa/core/componentmodel/behavior/ForceField.h>
00029 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00030 #include <sofa/core/objectmodel/Data.h>
00031 
00032 namespace sofa
00033 {
00034 
00035 namespace component
00036 {
00037 
00038 namespace forcefield
00039 {
00040 
00042 template<class DataTypes>
00043 class SphereForceFieldInternalData
00044 {
00045 public:
00046 };
00047 
00048 template<class DataTypes>
00049 class SphereForceField : public core::componentmodel::behavior::ForceField<DataTypes>, public virtual core::objectmodel::BaseObject
00050 {
00051 public:
00052     typedef core::componentmodel::behavior::ForceField<DataTypes> Inherit;
00053     typedef typename DataTypes::VecCoord VecCoord;
00054     typedef typename DataTypes::VecDeriv VecDeriv;
00055     typedef typename DataTypes::Coord Coord;
00056     typedef typename DataTypes::Deriv Deriv;
00057     typedef typename Coord::value_type Real;
00058     
00059 protected:
00060     class Contact
00061     {
00062     public:
00063         int index;
00064         Coord normal;
00065         Real fact;
00066     Contact( int index=0, Coord normal=Coord(),Real fact=Real(0))
00067       : index(index),normal(normal),fact(fact)
00068       {
00069       }
00070 
00071         inline friend std::istream& operator >> ( std::istream& in, Contact& c ){
00072       in>>c.index>>c.normal>>c.fact;
00073             return in;
00074         }
00075 
00076         inline friend std::ostream& operator << ( std::ostream& out, const Contact& c ){
00077       out << c.index << " " << c.normal << " " << c.fact ;
00078       return out;
00079         }
00080 
00081     };
00082 
00083     Data<sofa::helper::vector<Contact> > contacts;
00084 
00085     SphereForceFieldInternalData<DataTypes> data;
00086     
00087 public:
00088 
00089     Data<Coord> sphereCenter;
00090     Data<Real> sphereRadius;
00091     Data<Real> stiffness;
00092     Data<Real> damping;
00093     Data<defaulttype::Vec3f> color;
00094     Data<bool> bDraw;
00095 
00096     SphereForceField()
00097       : contacts(initData(&contacts,"contacts", "Contacts"))
00098       , sphereCenter(initData(&sphereCenter, "center", "sphere center"))
00099       , sphereRadius(initData(&sphereRadius, (Real)1, "radius", "sphere radius"))
00100       , stiffness(initData(&stiffness, (Real)500, "stiffness", "force stiffness"))
00101       , damping(initData(&damping, (Real)5, "damping", "force damping"))
00102       , color(initData(&color, defaulttype::Vec3f(0.0f,0.0f,1.0f), "color", "sphere color"))
00103       , bDraw(initData(&bDraw, true, "draw", "enable/disable drawing of the sphere"))
00104     {
00105     }
00106 
00107     void setSphere(const Coord& center, Real radius)
00108     {
00109         sphereCenter.setValue( center );
00110         sphereRadius.setValue( radius );
00111     }
00112 
00113     void setStiffness(Real stiff)
00114     {
00115         stiffness.setValue( stiff );
00116     }
00117 
00118     void setDamping(Real damp)
00119     {
00120         damping.setValue( damp );
00121     }
00122 
00123     virtual void addForce (VecDeriv& f, const VecCoord& x, const VecDeriv& v);
00124 
00125     virtual void addDForce (VecDeriv& df, const VecDeriv& dx, double kFactor, double bFactor);
00126     
00127     virtual double getPotentialEnergy(const VecCoord& x);
00128 
00129     virtual void updateStiffness( const VecCoord& x );
00130 
00131     void draw();
00132 };
00133 
00134 } // namespace forcefield
00135 
00136 } // namespace component
00137 
00138 } // namespace sofa
00139 
00140 #endif

Generated on Sun Nov 23 03:05:45 2008 for Sofa by  doxygen 1.5.7.1