MechanicalObject.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_MECHANICALOBJECT_H
00026 #define SOFA_COMPONENT_MECHANICALOBJECT_H
00027 
00028 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00029 #include <sofa/core/componentmodel/topology/BaseMeshTopology.h>
00030 #include <sofa/core/objectmodel/XDataPtr.h>
00031 #include <sofa/core/objectmodel/VDataPtr.h>
00032 #include <sofa/defaulttype/BaseVector.h>
00033 #include <sofa/defaulttype/Quat.h>
00034 #include <vector>
00035 #include <assert.h>
00036 #include <fstream>
00037 
00038 namespace sofa
00039 {
00040 
00041 namespace component
00042 {
00043 
00044 
00045 using namespace core::componentmodel::behavior;
00046 using namespace core::objectmodel;
00047 using sofa::defaulttype::Vector3;
00048 
00050 template<class DataTypes>
00051 class MechanicalObjectInternalData
00052 {
00053 public:
00054 };
00055 
00056 template <class DataTypes>
00057 class MechanicalObject : public MechanicalState<DataTypes>
00058 {
00059 public:
00060     typedef MechanicalState<DataTypes> Inherited;
00061     typedef typename Inherited::VecId VecId;
00062     typedef typename Inherited::VMultiOp VMultiOp;
00063     typedef typename DataTypes::VecCoord VecCoord;
00064     typedef typename DataTypes::VecDeriv VecDeriv;
00065     typedef typename DataTypes::Coord Coord;
00066     typedef typename DataTypes::Deriv Deriv;
00067     typedef typename DataTypes::Real Real;
00068     typedef typename DataTypes::SparseDeriv SparseDeriv;
00069     typedef typename DataTypes::SparseVecDeriv SparseVecDeriv;
00070     typedef typename DataTypes::VecConst VecConst;
00071     
00072 protected:
00073     VecCoord* x;
00074     VecDeriv* v;
00075     VecDeriv* f;
00076     VecDeriv* dx;
00077     VecCoord* x0;
00078     VecCoord* reset_position;
00079 
00080     VecDeriv* v0;
00081     VecDeriv* internalForces;
00082     VecDeriv* externalForces;
00083     VecCoord* xfree; // stores the position of the mechanical objet after a free movement (p.e. gravity action)
00084     VecDeriv* vfree; // stores the velocity of the mechanical objet after a free movement (p.e. gravity action)
00085 
00086     // Constraints stored in the Mechanical State
00087     // The storage is a SparseMatrix
00088     // Each constraint (Type TConst) contains the index of the related DOF
00089     VecConst *c;
00090     sofa::helper::vector<unsigned int> constraintId;
00091 
00092     bool initialized;
00093     Data< Vector3 > translation;
00094     Data< Vector3> rotation;
00095     Data< SReal > scale;
00096     Data< std::string > filename;
00097 
00100 
00101     sofa::helper::vector< VecCoord * > vectorsCoord;
00102     sofa::helper::vector< VecDeriv * > vectorsDeriv;
00103     sofa::helper::vector< VecConst * > vectorsConst;
00104     int vsize; 
00105 
00106     void setVecCoord(unsigned int index, VecCoord* v);
00107     void setVecDeriv(unsigned int index, VecDeriv* v);
00108     void setVecConst(unsigned int index, VecConst* v);
00109 
00111 
00112     MechanicalObjectInternalData<DataTypes> data;
00113 
00114     friend class MechanicalObjectInternalData<DataTypes>;
00115         
00116         std::ofstream* m_gnuplotFileX;
00117         std::ofstream* m_gnuplotFileV;
00118 
00119     class Loader;
00120 
00121 public:
00122 
00123     MechanicalObject();
00124     MechanicalObject& operator = ( const MechanicalObject& );
00125 
00126     virtual ~MechanicalObject();
00127     
00128     virtual bool load(const char* filename);
00129 
00130     virtual void parse ( BaseObjectDescription* arg );
00131 
00132     XDataPtr<DataTypes>* const f_X;
00133     VDataPtr<DataTypes>* const f_V;
00134     VDataPtr<DataTypes>* const f_F;
00135     VDataPtr<DataTypes>* const f_Dx;
00136     XDataPtr<DataTypes>* const f_Xfree;
00137     VDataPtr<DataTypes>* const f_Vfree;
00138 
00139     XDataPtr<DataTypes>* const f_X0;
00140 
00141     Data<SReal> restScale;
00142     
00143     Data<bool> debugViewIndices;
00144     Data<float> debugViewIndicesScale;
00145 
00146     virtual VecCoord* getX()  { f_X->beginEdit(); return x;  }
00147     virtual VecDeriv* getV()  { f_V->beginEdit(); return v;  }
00148     virtual VecDeriv* getF()  { f_F->beginEdit(); return f;  }
00149     virtual VecDeriv* getExternalForces()  { return externalForces;  }
00150     virtual VecDeriv* getDx() { f_Dx->beginEdit(); return dx; }
00151     virtual VecConst* getC() { return c;}
00152     virtual VecCoord* getXfree() { f_Xfree->beginEdit(); return xfree; }
00153     virtual VecDeriv* getVfree() { f_Vfree->beginEdit(); return vfree;  }
00154     VecCoord* getX0() { f_X0->beginEdit(); return x0;}
00155 
00156     virtual const VecCoord* getX()  const { return x;  }
00157     virtual const VecCoord* getX0()  const { return x0;  }
00158     virtual const VecDeriv* getV()  const { return v;  }
00159     virtual const VecDeriv* getV0()  const { return v0;  }
00160     virtual const VecDeriv* getF()  const { return f;  }
00161     virtual const VecDeriv* getExternalForces()  const { return externalForces;  }
00162     virtual const VecDeriv* getDx() const { return dx; }
00163     virtual const VecConst* getC() const { return c; }
00164     virtual const VecCoord* getXfree() const { return xfree; }
00165     virtual const VecDeriv* getVfree()  const { return vfree;  }
00166     
00167 //  SReal getScale(){return scale.getValue();};
00168 
00169     virtual void init();
00170     virtual void reinit();
00171     
00172     virtual void storeResetState();
00173 
00174     virtual void reset();
00175     
00176     virtual void writeX(std::ostream& out);
00177     virtual void readX(std::istream& in);
00178     virtual double compareX(std::istream& in);
00179     virtual void writeV(std::ostream& out);
00180     virtual void readV(std::istream& in);
00181     virtual double compareV(std::istream& in);
00182 
00183     virtual void writeState( std::ostream& out );
00184 
00185         virtual void initGnuplot(const std::string path);
00186         virtual void exportGnuplot(Real time);
00187 
00188     virtual void resize( int vsize);
00189     
00190     virtual bool addBBox(double* minBBox, double* maxBBox);
00191 
00192     int getSize() const {
00193         return vsize;
00194     }
00195     double getPX(int i) const { Real x=0.0,y=0.0,z=0.0; DataTypes::get(x,y,z,(*getX())[i]); return (SReal)x; }
00196     double getPY(int i) const { Real x=0.0,y=0.0,z=0.0; DataTypes::get(x,y,z,(*getX())[i]); return (SReal)y; }
00197     double getPZ(int i) const { Real x=0.0,y=0.0,z=0.0; DataTypes::get(x,y,z,(*getX())[i]); return (SReal)z; }
00198 
00202     void replaceValue (const int inputIndex, const int outputIndex);
00203 
00204 
00205 
00209     void swapValues (const int idx1, const int idx2);
00210 
00211 
00212 
00218     void renumberValues( const sofa::helper::vector<unsigned int> &index );
00219 
00220 
00221 
00226     void computeWeightedValue( const unsigned int i, const sofa::helper::vector< unsigned int >& ancestors, const sofa::helper::vector< double >& coefs);
00227 
00231     void computeNewPoint( const unsigned int i, const sofa::helper::vector< double >& m_x);
00232 
00233     // Force the position of a point (and force its velocity to zero value)
00234     void forcePointPosition( const unsigned int i, const sofa::helper::vector< double >& m_x);
00235 
00236     virtual void applyTranslation (const double dx,const double dy,const double dz);
00237     
00238         // rotation using Euler Angles in degree
00239         virtual void applyRotation (const double rx, const double ry, const double rz);
00240             
00241     virtual void applyRotation (const defaulttype::Quat q);
00242     
00243     virtual void applyScale (const double s);
00244     
00246     void getIndicesInSpace(sofa::helper::vector<unsigned>& indices, Real xmin, Real xmax, Real ymin, Real ymax, Real zmin, Real zmax) const;
00247 
00248 
00251 
00253     virtual void contributeToMatrixDimension(unsigned int * const, unsigned int * const);
00254 
00256     virtual void loadInBaseVector(defaulttype::BaseVector *, VecId , unsigned int &);
00257 
00259     virtual void addBaseVectorToState(VecId , defaulttype::BaseVector *, unsigned int &);
00260 
00262     virtual void setOffset(unsigned int &);
00263 
00265 
00266         void setFilename(std::string s){filename.setValue(s);};
00267         std::string getFilename(){return filename.getValue();};      
00268         
00269     virtual void addDxToCollisionModel(void);
00270 
00271     void setConstraintId(unsigned int);
00272     sofa::helper::vector<unsigned int>& getConstraintId();
00273 
00274 
00277 
00278     virtual void beginIntegration(Real dt);
00279 
00280     virtual void endIntegration(Real dt);
00281 
00282     virtual void accumulateForce();
00283 
00284     VecCoord* getVecCoord(unsigned int index);
00285     const VecCoord* getVecCoord(unsigned int index) const;
00286 
00287     VecDeriv* getVecDeriv(unsigned int index);
00288     const VecDeriv* getVecDeriv(unsigned int index) const;
00289 
00290     VecConst* getVecConst(unsigned int index);
00291     const VecConst* getVecConst(unsigned int index) const;
00292 
00293     virtual void vAvail(VecId& v);
00294 
00295     virtual void vAlloc(VecId v);
00296 
00297     virtual void vFree(VecId v);
00298 
00299     virtual void vOp(VecId v, VecId a = VecId::null(), VecId b = VecId::null(), double f=1.0);
00300 
00301     virtual void vMultiOp(const VMultiOp& ops);
00302 
00303         virtual void vThreshold( VecId a, double threshold );
00304 
00305         virtual double vDot(VecId a, VecId b);
00306 
00307     virtual void setX(VecId v);
00308 
00309     virtual void setXfree(VecId v);
00310 
00311     virtual void setVfree(VecId v);
00312 
00313     virtual void setV(VecId v);
00314 
00315     virtual void setF(VecId v);
00316 
00317     virtual void setDx(VecId v);
00318 
00319     virtual void setC(VecId v);
00320 
00321     virtual void resetForce();
00322 
00323     virtual void resetConstraint();
00324 
00326 
00329     virtual void printDOF( VecId, std::ostream& =std::cerr );
00330     virtual unsigned printDOFWithElapsedTime(VecId, unsigned =0, unsigned =0, std::ostream& =std::cerr );
00331     //
00332     void draw();
00334 
00335     // handle state changes
00336     virtual void handleStateChange();
00337 
00338 protected:
00339     sofa::core::componentmodel::topology::BaseMeshTopology* _topology;
00340     
00341     
00342 };
00343 
00344 } // namespace component
00345 
00346 } // namespace sofa
00347 
00348 #endif

Generated on Sun Nov 23 03:41:20 2008 for Sofa by  doxygen 1.5.7.1