BaseMechanicalState.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 :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
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 /*i*/) const { return 0.0; }
00093     virtual double getPY(int /*i*/) const { return 0.0; }
00094     virtual double getPZ(int /*i*/) const { return 0.0; }
00095 
00098 
00100     virtual void beginIntegration(double /*dt*/) { }
00101 
00103     virtual void endIntegration(double /*dt*/) { }
00104 
00106     virtual void resetForce() =0;//{ vOp( VecId::force() ); }
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 /*rx*/, const double /*ry*/, const double /*rz*/){};
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* /*minBBox*/, double* /*maxBBox*/)
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; //{ return 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 ** /*w*/){ }
00292     virtual void applyContactForce(double * /*f*/){ }
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 } // namespace behavior
00337 
00338 } // namespace componentmodel
00339 
00340 } // namespace core
00341 
00342 } // namespace sofa
00343 
00344 #endif

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