00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef SOFA_CORE_COMPONENTMODEL_BEHAVIOR_BASEMECHANICALSTATE_H
00028 #define SOFA_CORE_COMPONENTMODEL_BEHAVIOR_BASEMECHANICALSTATE_H
00029
00030 #include <sofa/defaulttype/Quat.h>
00031 #include <sofa/core/objectmodel/BaseObject.h>
00032 #include <sofa/defaulttype/BaseVector.h>
00033 #include <sofa/defaulttype/Vec.h>
00034 #include <sstream>
00035 #include <iostream>
00036
00037
00038 namespace sofa
00039 {
00040
00041 namespace core
00042 {
00043
00044 namespace componentmodel
00045 {
00046
00047 namespace behavior
00048 {
00049
00050 class BaseMechanicalMapping;
00051
00079 class BaseMechanicalState : public virtual objectmodel::BaseObject
00080 {
00081 public:
00082 BaseMechanicalState()
00083 {}
00084 virtual ~BaseMechanicalState()
00085 { }
00086
00088 virtual void resize(int vsize) = 0;
00089
00091 virtual int getSize() const { return 0; }
00092 virtual double getPX(int ) const { return 0.0; }
00093 virtual double getPY(int ) const { return 0.0; }
00094 virtual double getPZ(int ) const { return 0.0; }
00095
00098
00100 virtual void beginIntegration(double ) { }
00101
00103 virtual void endIntegration(double ) { }
00104
00106 virtual void resetForce() =0;
00107
00109 virtual void resetConstraint() =0;
00110
00112 virtual void accumulateForce() { }
00113
00115 virtual void accumulateDf() { }
00116
00118 virtual void applyTranslation(const double dx, const double dy, const double dz)=0;
00119
00120
00123 virtual void applyRotation (const double , const double , const double ){};
00124
00126 virtual void applyRotation(const defaulttype::Quat q)=0;
00127
00129 virtual void applyScale(const double s)=0;
00130
00131 virtual void writeX(std::ostream &out)=0;
00132 virtual void readX(std::istream &in)=0;
00133 virtual double compareX(std::istream &in)=0;
00134
00135 virtual void writeV(std::ostream &out)=0;
00136 virtual void readV(std::istream &in)=0;
00137 virtual double compareV(std::istream &in)=0;
00138
00139
00140 virtual bool addBBox(double* , double* )
00141 {
00142 return false;
00143 }
00144
00146 class VecId
00147 {
00148 public:
00149 enum { V_FIRST_DYNAMIC_INDEX = 8 };
00150 enum Type
00151 {
00152 V_NULL=0,
00153 V_COORD,
00154 V_DERIV,
00155 V_CONST
00156 };
00157 Type type;
00158 unsigned int index;
00159 VecId(Type t, unsigned int i) : type(t), index(i) { }
00160 VecId() : type(V_NULL), index(0) { }
00161 bool isNull() const { return type==V_NULL; }
00162 static VecId null() { return VecId(V_NULL,0); }
00163 static VecId position() { return VecId(V_COORD,0); }
00164 static VecId restPosition() { return VecId(V_COORD,1); }
00165 static VecId velocity() { return VecId(V_DERIV,0); }
00166 static VecId restVelocity() { return VecId(V_DERIV,1); }
00167 static VecId force() { return VecId(V_DERIV,3); }
00168 static VecId dx() { return VecId(V_DERIV,4); }
00169 static VecId freePosition() { return VecId(V_COORD,2); }
00170 static VecId freeVelocity() { return VecId(V_DERIV,2); }
00171 static VecId holonomicC() {return VecId(V_CONST,0);}
00172 static VecId nonHolonomicC() {return VecId(V_CONST,1);}
00173
00175 bool operator==(const VecId& v) const
00176 {
00177 return type == v.type && index == v.index;
00178 }
00179 };
00180
00182 virtual void vAvail(VecId& v) = 0;
00183
00185 virtual void vAlloc(VecId v) = 0;
00186
00188 virtual void vFree(VecId v) = 0;
00189
00197 virtual void vOp(VecId v, VecId a = VecId::null(), VecId b = VecId::null(), double f=1.0) = 0;
00198
00201 typedef helper::vector< std::pair< VecId, helper::vector< std::pair< VecId, double > > > > VMultiOp;
00202
00208 virtual void vMultiOp(const VMultiOp& ops)
00209 {
00210 for(VMultiOp::const_iterator it = ops.begin(), itend = ops.end(); it != itend; ++it)
00211 {
00212 VecId r = it->first;
00213 const helper::vector< std::pair< VecId, double > >& operands = it->second;
00214 int nop = operands.size();
00215 if (nop==0)
00216 {
00217 vOp(r);
00218 }
00219 else if (nop==1)
00220 {
00221 if (operands[0].second == 1.0)
00222 vOp(r, operands[0].first);
00223 else
00224 vOp(r, VecId::null(), operands[0].first, operands[0].second);
00225 }
00226 else
00227 {
00228 int i;
00229 if (operands[0].second == 1.0)
00230 {
00231 vOp(r, operands[0].first, operands[1].first, operands[1].second);
00232 i = 2;
00233 }
00234 else
00235 {
00236 vOp(r, VecId::null(), operands[0].first, operands[0].second);
00237 i = 1;
00238 }
00239 for (;i<nop;++i)
00240 vOp(r, r, operands[i].first, operands[i].second);
00241 }
00242 }
00243 }
00244
00246 virtual double vDot(VecId a, VecId b) = 0;
00247
00249 virtual void vThreshold( VecId a, double threshold )=0;
00250
00254 virtual void setX(VecId v) = 0;
00255
00259 virtual void setXfree(VecId v) = 0;
00260
00264 virtual void setVfree(VecId v) = 0;
00265
00269 virtual void setV(VecId v) = 0;
00270
00274 virtual void setF(VecId v) = 0;
00275
00279 virtual void setDx(VecId v) = 0;
00280
00285 virtual void setC(VecId v) = 0;
00286
00287
00288
00290 virtual void getCompliance(double ** ){ }
00292 virtual void applyContactForce(double * ){ }
00293
00294 virtual void resetContactForce(void) {}
00295
00296 virtual void addDxToCollisionModel(void) = 0;
00297
00299 virtual void contributeToMatrixDimension(unsigned int * const, unsigned int * const) = 0;
00300
00302 virtual void loadInBaseVector(defaulttype::BaseVector *, VecId , unsigned int &) = 0;
00303
00305 virtual void addBaseVectorToState(VecId , defaulttype::BaseVector *, unsigned int &) = 0;
00306
00308 virtual void setOffset(unsigned int &) = 0;
00309
00311
00314 virtual void printDOF( VecId, std::ostream& =std::cerr ) = 0;
00315 virtual void initGnuplot(const std::string){}
00316 virtual void exportGnuplot(double){}
00317 virtual unsigned printDOFWithElapsedTime(VecId, unsigned =0, unsigned =0, std::ostream& =std::cerr ){return 0;};
00319
00320 };
00321
00322 inline std::ostream& operator<<(std::ostream& o, const BaseMechanicalState::VecId& v)
00323 {
00324 switch (v.type)
00325 {
00326 case BaseMechanicalState::VecId::V_NULL: o << "vNull"; break;
00327 case BaseMechanicalState::VecId::V_COORD: o << "vCoord"; break;
00328 case BaseMechanicalState::VecId::V_DERIV: o << "vDeriv"; break;
00329 case BaseMechanicalState::VecId::V_CONST: o << "vConst"; break;
00330 default: o << "vUNKNOWN"; break;
00331 }
00332 o << '[' << v.index << ']';
00333 return o;
00334 }
00335
00336 }
00337
00338 }
00339
00340 }
00341
00342 }
00343
00344 #endif