UnilateralInteractionConstraint.h

Go to the documentation of this file.
00001 #ifndef SOFA_COMPONENT_CONSTRAINT_UNILATERALINTERACTIONCONSTRAINT_H
00002 #define SOFA_COMPONENT_CONSTRAINT_UNILATERALINTERACTIONCONSTRAINT_H
00003 
00004 #include <sofa/core/componentmodel/behavior/InteractionConstraint.h>
00005 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00006 #include <iostream>
00007 
00008 
00009 namespace sofa
00010 {
00011 
00012 namespace component
00013 {
00014 
00015 namespace constraint
00016 {
00017 template<class DataTypes>
00018 class UnilateralInteractionConstraint : public core::componentmodel::behavior::InteractionConstraint
00019 {
00020 public:
00021     typedef typename DataTypes::VecCoord VecCoord;
00022     typedef typename DataTypes::VecDeriv VecDeriv;
00023     typedef typename DataTypes::VecConst VecConst;
00024     typedef typename DataTypes::SparseVecDeriv SparseVecDeriv;
00025     typedef typename DataTypes::SparseDeriv SparseDeriv;
00026     typedef typename DataTypes::Coord Coord;
00027     typedef typename DataTypes::Deriv Deriv;
00028     typedef typename Coord::value_type Real;
00029     typedef typename core::componentmodel::behavior::MechanicalState<DataTypes> MechanicalState;
00030 
00031 protected:
00032     MechanicalState* object1;
00033     MechanicalState* object2;
00034 
00035     struct Contact
00036     {
00037         int m1, m2;     
00038         Deriv norm;     
00039         Deriv t;        
00040         Deriv s;        
00041         Real dt;
00042         Real delta;     
00043         Real dfree;     
00044         Real dfree_t;   
00045         Real dfree_s;   
00046         unsigned int id;
00047         long contactId;
00048         double mu;      
00049 
00050         // for visu
00051         Coord P, Q;
00052         Coord Pfree, Qfree;
00053     };
00054     
00055     sofa::helper::vector<Contact> contacts;
00056     Real epsilon;
00057     bool yetIntegrated;
00058 
00059 public:
00060 
00061     unsigned int constraintId;
00062 
00063     UnilateralInteractionConstraint(MechanicalState* object1, MechanicalState* object2)
00064     : object1(object1), object2(object2), epsilon(Real(0.001)),yetIntegrated(false)
00065     {
00066     }
00067     
00068     UnilateralInteractionConstraint(MechanicalState* object)
00069     : object1(object), object2(object), epsilon(Real(0.001)),yetIntegrated(false)
00070     {
00071     }
00072 
00073     UnilateralInteractionConstraint()
00074     : object1(NULL), object2(NULL), epsilon(Real(0.001)),yetIntegrated(false)
00075     {
00076     }
00077 
00078     virtual ~UnilateralInteractionConstraint()
00079     {
00080     }
00081     
00082     MechanicalState* getObject1() { return object1; }
00083     MechanicalState* getObject2() { return object2; }
00084     core::componentmodel::behavior::BaseMechanicalState* getMechModel1() { return object1; }
00085     core::componentmodel::behavior::BaseMechanicalState* getMechModel2() { return object2; }
00086 
00087     void clear(int reserve = 0)
00088     {
00089         contacts.clear();
00090         if (reserve)
00091         contacts.reserve(reserve);
00092     }
00093 
00094     virtual void applyConstraint(unsigned int & /*contactId*/, double & /*mu*/);
00095 
00096     virtual void addContact(double mu, Deriv norm, Coord P, Coord Q, Real contactDistance, int m1, int m2, Coord Pfree = Coord(), Coord Qfree = Coord(), long id=0);
00097 
00098     virtual void getConstraintValue(defaulttype::BaseVector *);
00099     virtual void getConstraintValue(double *);
00100 
00101     virtual void getConstraintId(long* id, unsigned int &offset);
00102     // Previous Constraint Interface
00103     virtual void projectResponse(){}
00104     virtual void projectVelocity(){}
00105     virtual void projectPosition(){}
00106     virtual void projectFreeVelocity(){}
00107     virtual void projectFreePosition(){}
00108 
00110     template<class T>
00111     static bool canCreate(T*& obj, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg)
00112     {
00113         if (arg->getAttribute("object1") || arg->getAttribute("object2"))
00114         {
00115             if (dynamic_cast<MechanicalState*>(arg->findObject(arg->getAttribute("object1",".."))) == NULL)
00116                 return false;
00117             if (dynamic_cast<MechanicalState*>(arg->findObject(arg->getAttribute("object2",".."))) == NULL)
00118                 return false;
00119         }
00120         else
00121         {
00122             if (dynamic_cast<MechanicalState*>(context->getMechanicalState()) == NULL)
00123                 return false;
00124         }
00125         return core::componentmodel::behavior::InteractionConstraint::canCreate(obj, context, arg);
00126     }
00127 
00129     template<class T>
00130     static void create(T*& obj, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg)
00131     {
00132         core::componentmodel::behavior::InteractionConstraint::create(obj, context, arg);
00133         if (arg && (arg->getAttribute("object1") || arg->getAttribute("object2")))
00134         {
00135             obj->object1 = dynamic_cast<MechanicalState*>(arg->findObject(arg->getAttribute("object1","..")));
00136             obj->object2 = dynamic_cast<MechanicalState*>(arg->findObject(arg->getAttribute("object2","..")));
00137         }
00138         else if (context)
00139         {
00140             obj->object1 =
00141             obj->object2 =
00142                 dynamic_cast<MechanicalState*>(context->getMechanicalState());
00143         }
00144     }
00145 
00146     virtual std::string getTemplateName() const
00147     {
00148       return templateName(this);
00149     }
00150 
00151     static std::string templateName(const UnilateralInteractionConstraint<DataTypes>* = NULL)
00152     {
00153       return DataTypes::Name();
00154     }
00155     void draw();
00156 
00158     bool isHolonomic() {return false;}
00159 };
00160 } // namespace constraint
00161 
00162 } // namespace component
00163 
00164 } // namespace sofa
00165 
00166 #endif // SOFA_COMPONENT_CONSTRAINT_UNILATERALINTERACTIONCONSTRAINT_H

Generated on Sun Nov 23 02:48:23 2008 for Sofa by  doxygen 1.5.7.1