TriangleModel.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_TRIANGLEMODEL_H
00026 #define SOFA_COMPONENT_COLLISION_TRIANGLEMODEL_H
00027 
00028 #include <sofa/core/CollisionModel.h>
00029 #include <sofa/component/MechanicalObject.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 TriangleModel;
00047 
00048 class Triangle : public core::TCollisionElementIterator<TriangleModel>
00049 {
00050 public:
00051     Triangle(TriangleModel* model, int index);
00052     Triangle(){};
00053     explicit Triangle(core::CollisionElementIterator& i);
00054 
00055     const Vector3& p1() const;
00056     const Vector3& p2() const;
00057     const Vector3& p3() const;
00058     int p1Index() const;
00059     int p2Index() const;
00060     int p3Index() const;
00061 
00062     const Vector3& p1Free() const;
00063     const Vector3& p2Free() const;
00064     const Vector3& p3Free() const;
00065 
00066     const Vector3& v1() const;
00067     const Vector3& v2() const;
00068     const Vector3& v3() const;
00069 
00070     const Vector3& n() const;
00071     Vector3& n();
00072 
00073     int flags() const;
00074 };
00075 
00076 class TriangleModel : public core::CollisionModel
00077 {
00078 public:
00079     typedef Vec3Types InDataTypes;
00080     typedef Vec3Types DataTypes;
00081     typedef DataTypes::VecCoord VecCoord;
00082     typedef DataTypes::VecDeriv VecDeriv;
00083     typedef DataTypes::Coord Coord;
00084     typedef DataTypes::Deriv Deriv;
00085     typedef Triangle Element;   
00086     friend class Triangle;
00087     
00088     enum TriangleFlag {
00089       FLAG_P1  = 1<<0, 
00090       FLAG_P2  = 1<<1, 
00091       FLAG_P3  = 1<<2, 
00092       FLAG_E12 = 1<<3, 
00093       FLAG_E23 = 1<<4, 
00094       FLAG_E31 = 1<<5, 
00095       FLAG_POINTS  = FLAG_P1|FLAG_P2|FLAG_P3,
00096       FLAG_EDGES   = FLAG_E12|FLAG_E23|FLAG_E31,
00097     };
00098 
00099 protected:
00100     struct TriangleInfo
00101     {
00102         //int i1,i2,i3;
00103         int flags;
00104         Vector3 normal;
00105     };
00106 
00107     sofa::helper::vector<TriangleInfo> elems;
00108     const sofa::core::componentmodel::topology::BaseMeshTopology::SeqTriangles* triangles;
00109 
00110     sofa::core::componentmodel::topology::BaseMeshTopology::SeqTriangles mytriangles;
00111     
00112     bool needsUpdate;
00113     virtual void updateFromTopology();
00114     virtual void updateFlags(int ntri=-1);
00115     virtual void updateNormals();
00116 
00117     core::componentmodel::behavior::MechanicalState<Vec3Types>* mstate;
00118     Data<bool> computeNormals;
00119     int meshRevision;
00120 
00121     sofa::core::componentmodel::topology::BaseMeshTopology* _topology;
00122 
00123 public:
00124 
00125     TriangleModel();
00126 
00127     virtual void init();
00128 
00129     // -- CollisionModel interface
00130 
00131     virtual void resize(int size);
00132 
00133     virtual void computeBoundingTree(int maxDepth=0);
00134 
00135     virtual void computeContinuousBoundingTree(double dt, int maxDepth=0);
00136 
00137     void draw(int index);
00138 
00139     void draw();
00140 
00141     virtual void handleTopologyChange();
00142 
00143     core::componentmodel::behavior::MechanicalState<Vec3Types>* getMechanicalState() { return mstate; }
00144     
00145     void buildOctree(); 
00146 };
00147 
00148 inline Triangle::Triangle(TriangleModel* model, int index)
00149 : core::TCollisionElementIterator<TriangleModel>(model, index)
00150 {}
00151 
00152 inline Triangle::Triangle(core::CollisionElementIterator& i)
00153 : core::TCollisionElementIterator<TriangleModel>(static_cast<TriangleModel*>(i.getCollisionModel()), i.getIndex())
00154 {}
00155 
00156 inline const Vector3& Triangle::p1() const { return (*model->mstate->getX())[(*(model->triangles))[index][0]]; }
00157 inline const Vector3& Triangle::p2() const { return (*model->mstate->getX())[(*(model->triangles))[index][1]]; }
00158 inline const Vector3& Triangle::p3() const { return (*model->mstate->getX())[(*(model->triangles))[index][2]]; }
00159 
00160 inline const Vector3& Triangle::p1Free() const { return (*model->mstate->getXfree())[(*(model->triangles))[index][0]]; }
00161 inline const Vector3& Triangle::p2Free() const { return (*model->mstate->getXfree())[(*(model->triangles))[index][1]]; }
00162 inline const Vector3& Triangle::p3Free() const { return (*model->mstate->getXfree())[(*(model->triangles))[index][2]]; }
00163 
00164 inline int Triangle::p1Index() const { return (*(model->triangles))[index][0]; }
00165 inline int Triangle::p2Index() const { return (*(model->triangles))[index][1]; }
00166 inline int Triangle::p3Index() const { return (*(model->triangles))[index][2]; }
00167 
00168 inline const Vector3& Triangle::v1() const { return (*model->mstate->getV())[(*(model->triangles))[index][0]]; }
00169 inline const Vector3& Triangle::v2() const { return (*model->mstate->getV())[(*(model->triangles))[index][1]]; }
00170 inline const Vector3& Triangle::v3() const { return (*model->mstate->getV())[(*(model->triangles))[index][2]]; }
00171 
00172 inline const Vector3& Triangle::n() const { return model->elems[index].normal; }
00173 inline       Vector3& Triangle::n()       { return model->elems[index].normal; }
00174 
00175 inline int            Triangle::flags() const { return model->elems[index].flags; }
00176     
00177 } // namespace collision
00178 
00179 } // namespace component
00180 
00181 } // namespace sofa
00182 
00183 #endif

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