CubeModel.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_CUBEMODEL_H
00026 #define SOFA_COMPONENT_COLLISION_CUBEMODEL_H
00027 
00028 #include <sofa/core/CollisionModel.h>
00029 #include <sofa/component/MechanicalObject.h>
00030 #include <sofa/defaulttype/Vec3Types.h>
00031 
00032 namespace sofa
00033 {
00034 
00035 namespace component
00036 {
00037 
00038 namespace collision
00039 {
00040 
00041 using namespace sofa::defaulttype;
00042 
00043 class CubeModel;
00044 
00045 class Cube : public core::TCollisionElementIterator<CubeModel>
00046 {
00047 public:
00048     Cube(CubeModel* model=NULL, int index=0);
00049 
00050     explicit Cube(const core::CollisionElementIterator& i);
00051 
00052     const Vector3& minVect() const;
00053 
00054     const Vector3& maxVect() const;
00055 
00056     const std::pair<Cube,Cube>& subcells() const;
00057 };
00058 
00059 class CubeModel : public core::CollisionModel
00060 {
00061 protected:
00062     
00063     struct CubeData
00064     {
00065         Vector3 minBBox, maxBBox;
00066         std::pair<Cube,Cube> subcells;
00067         std::pair<core::CollisionElementIterator,core::CollisionElementIterator> children; 
00068     };
00069     
00070     class CubeSortPredicate;
00071     
00072     sofa::helper::vector<CubeData> elems;
00073     sofa::helper::vector<int> parentOf; 
00074     
00075 public:
00076     typedef core::CollisionElementIterator ChildIterator;
00077     typedef Vec3Types DataTypes;
00078     typedef Cube Element;
00079     friend class Cube;
00080     
00081     CubeModel();
00082 
00083     virtual void resize(int size);
00084 
00085     void setParentOf(int childIndex, const Vector3& min, const Vector3& max);
00086     void setLeafCube(int cubeIndex, std::pair<core::CollisionElementIterator,core::CollisionElementIterator> children, const Vector3& min, const Vector3& max);
00087 
00088     
00089     unsigned int getNumberCells(){ return elems.size();};
00090 
00091     void getBoundingTree ( sofa::helper::vector< std::pair< Vector3, Vector3> > &bounding )
00092     {
00093         bounding.resize(elems.size());
00094         for (unsigned int index=0; index<elems.size(); index++)
00095         {
00096             bounding[index] = std::make_pair( elems[index].minBBox, elems[index].maxBBox);
00097         }
00098     }
00099 
00100     int getLeafIndex(int index) const
00101     {
00102     return elems[index].children.first.getIndex();
00103     }
00104 
00105     int getLeafEndIndex(int index) const
00106     {
00107     return elems[index].children.second.getIndex();
00108     }
00109 
00110     // -- CollisionModel interface
00111 
00112     virtual void computeBoundingTree(int maxDepth=0);
00113     
00114     virtual std::pair<core::CollisionElementIterator,core::CollisionElementIterator> getInternalChildren(int index) const;
00115 
00116     virtual std::pair<core::CollisionElementIterator,core::CollisionElementIterator> getExternalChildren(int index) const;
00117 
00118     virtual bool isLeaf( int index ) const;
00119 
00120     void draw(int index);
00121 
00122     void draw();
00123 
00124     int addCube(Cube subcellsBegin, Cube subcellsEnd);
00125     void updateCube(int index);
00126     void updateCubes();
00127 };
00128 
00129 inline Cube::Cube(CubeModel* model, int index)
00130 : core::TCollisionElementIterator<CubeModel>(model, index)
00131 {}
00132 
00133 inline Cube::Cube(const core::CollisionElementIterator& i)
00134 : core::TCollisionElementIterator<CubeModel>(static_cast<CubeModel*>(i.getCollisionModel()), i.getIndex())
00135 {
00136 }
00137 
00138 inline const Vector3& Cube::minVect() const
00139 {
00140     return model->elems[index].minBBox;
00141 }
00142 
00143 inline const Vector3& Cube::maxVect() const
00144 {
00145     return model->elems[index].maxBBox;
00146 }
00147 
00148 
00149 inline const std::pair<Cube,Cube>& Cube::subcells() const
00150 {
00151     return model->elems[index].subcells;
00152 }
00153 
00154 } // namespace collision
00155 
00156 } // namespace component
00157 
00158 } // namespace sofa
00159 
00160 #endif

Generated on Sun Nov 23 02:33:11 2008 for Sofa by  doxygen 1.5.7.1