TetrahedronModel.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_COLLISION_TETRAHEDRONMODEL_H
00026 #define SOFA_COMPONENT_COLLISION_TETRAHEDRONMODEL_H
00027 
00028 #include <sofa/core/CollisionModel.h>
00029 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00030 #include <sofa/core/componentmodel/topology/BaseMeshTopology.h>
00031 #include <sofa/defaulttype/Vec3Types.h>
00032 
00033 #include <map>
00034 
00035 namespace sofa
00036 {
00037 
00038 namespace component
00039 {
00040 
00041 namespace collision
00042 {
00043 
00044 using namespace sofa::defaulttype;
00045 
00046 class TetrahedronModel;
00047 
00048 class Tetrahedron : public core::TCollisionElementIterator<TetrahedronModel>
00049 {
00050 public:
00051     Tetrahedron(TetrahedronModel* model, int index);
00052     Tetrahedron(){};
00053     explicit Tetrahedron(core::CollisionElementIterator& i);
00054 
00055     const Vector3& p1() const;
00056     const Vector3& p2() const;
00057     const Vector3& p3() const;
00058     const Vector3& p4() const;
00059     int p1Index() const;
00060     int p2Index() const;
00061     int p3Index() const;
00062     int p4Index() const;
00063 
00064     const Vector3& p1Free() const;
00065     const Vector3& p2Free() const;
00066     const Vector3& p3Free() const;
00067     const Vector3& p4Free() const;
00068 
00069     const Vector3& v1() const;
00070     const Vector3& v2() const;
00071     const Vector3& v3() const;
00072     const Vector3& v4() const;
00073 
00074     Vector3 getBary(const Vector3& p) const;
00075     Vector3 getDBary(const Vector3& v) const;
00076 
00077     Vector3 getCoord(const Vector3& b) const;
00078     Vector3 getDCoord(const Vector3& b) const;
00079 
00080 };
00081 
00082 class TetrahedronModel : public core::CollisionModel
00083 {
00084 public:
00085     typedef Vec3Types InDataTypes;
00086     typedef Vec3Types DataTypes;
00087     typedef DataTypes::VecCoord VecCoord;
00088     typedef DataTypes::VecDeriv VecDeriv;
00089     typedef DataTypes::Coord Coord;
00090     typedef DataTypes::Deriv Deriv;
00091     typedef Tetrahedron Element;
00092     friend class Tetrahedron;
00093 
00094 protected:
00095     struct TetrahedronInfo
00096     {
00097     Vector3 coord0;
00098         Matrix3 coord2bary;
00099     Matrix3 bary2coord;
00100     };
00101 
00102     sofa::helper::vector<TetrahedronInfo> elems;
00103     const sofa::core::componentmodel::topology::BaseMeshTopology::SeqTetras* tetra;
00104 
00105     core::componentmodel::behavior::MechanicalState<Vec3Types>* mstate;
00106 
00107     sofa::core::componentmodel::topology::BaseMeshTopology* _topology;
00108 
00109 public:
00110 
00111     TetrahedronModel();
00112 
00113     virtual void init();
00114 
00115     // -- CollisionModel interface
00116 
00117     virtual void resize(int size);
00118 
00119     virtual void computeBoundingTree(int maxDepth=0);
00120 
00121     //virtual void computeContinuousBoundingTree(double dt, int maxDepth=0);
00122 
00123     void draw(int index);
00124 
00125     void draw();
00126 
00127     virtual void handleTopologyChange();
00128 
00129     core::componentmodel::behavior::MechanicalState<Vec3Types>* getMechanicalState() { return mstate; }
00130 
00131 };
00132 
00133 inline Tetrahedron::Tetrahedron(TetrahedronModel* model, int index)
00134 : core::TCollisionElementIterator<TetrahedronModel>(model, index)
00135 {}
00136 
00137 inline Tetrahedron::Tetrahedron(core::CollisionElementIterator& i)
00138 : core::TCollisionElementIterator<TetrahedronModel>(static_cast<TetrahedronModel*>(i.getCollisionModel()), i.getIndex())
00139 {}
00140 
00141 inline const Vector3& Tetrahedron::p1() const { return (*model->mstate->getX())[(*(model->tetra))[index][0]]; }
00142 inline const Vector3& Tetrahedron::p2() const { return (*model->mstate->getX())[(*(model->tetra))[index][1]]; }
00143 inline const Vector3& Tetrahedron::p3() const { return (*model->mstate->getX())[(*(model->tetra))[index][2]]; }
00144 inline const Vector3& Tetrahedron::p4() const { return (*model->mstate->getX())[(*(model->tetra))[index][3]]; }
00145 
00146 inline const Vector3& Tetrahedron::p1Free() const { return (*model->mstate->getXfree())[(*(model->tetra))[index][0]]; }
00147 inline const Vector3& Tetrahedron::p2Free() const { return (*model->mstate->getXfree())[(*(model->tetra))[index][1]]; }
00148 inline const Vector3& Tetrahedron::p3Free() const { return (*model->mstate->getXfree())[(*(model->tetra))[index][2]]; }
00149 inline const Vector3& Tetrahedron::p4Free() const { return (*model->mstate->getXfree())[(*(model->tetra))[index][3]]; }
00150 
00151 inline int Tetrahedron::p1Index() const { return (*(model->tetra))[index][0]; }
00152 inline int Tetrahedron::p2Index() const { return (*(model->tetra))[index][1]; }
00153 inline int Tetrahedron::p3Index() const { return (*(model->tetra))[index][2]; }
00154 inline int Tetrahedron::p4Index() const { return (*(model->tetra))[index][3]; }
00155 
00156 inline const Vector3& Tetrahedron::v1() const { return (*model->mstate->getV())[(*(model->tetra))[index][0]]; }
00157 inline const Vector3& Tetrahedron::v2() const { return (*model->mstate->getV())[(*(model->tetra))[index][1]]; }
00158 inline const Vector3& Tetrahedron::v3() const { return (*model->mstate->getV())[(*(model->tetra))[index][2]]; }
00159 inline const Vector3& Tetrahedron::v4() const { return (*model->mstate->getV())[(*(model->tetra))[index][3]]; }
00160 
00161 inline Vector3 Tetrahedron::getBary(const Vector3& p) const { return model->elems[index].coord2bary*(p-model->elems[index].coord0); }
00162 inline Vector3 Tetrahedron::getDBary(const Vector3& v) const { return model->elems[index].coord2bary*(v); }
00163 inline Vector3 Tetrahedron::getCoord(const Vector3& b) const { return model->elems[index].bary2coord*b + model->elems[index].coord0; }
00164 inline Vector3 Tetrahedron::getDCoord(const Vector3& b) const { return model->elems[index].bary2coord*b; }
00165     
00166 } // namespace collision
00167 
00168 } // namespace component
00169 
00170 } // namespace sofa
00171 
00172 #endif

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