VoxelGrid.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_VOXELGRID_H
00026 #define SOFA_COMPONENT_COLLISION_VOXELGRID_H
00027 
00028 #include <sofa/core/componentmodel/collision/BroadPhaseDetection.h>
00029 #include <sofa/component/collision/NarrowPhaseDetection.h>
00030 #include <sofa/defaulttype/Vec.h>
00031 #include <sofa/simulation/tree/GNode.h>
00032 #include <vector>
00033 
00034 
00035 namespace sofa
00036 {
00037 
00038 namespace component
00039 {
00040 
00041 namespace collision
00042 {
00043 
00044 using namespace sofa::defaulttype;
00045 
00046 class VoxelGrid;
00047 
00048 class GridCell
00049 {
00050 private:
00051     //Vector3 minVect, maxVect; // minx, miny, minz; maxx, maxy, maxz
00052     sofa::helper::vector<core::CollisionElementIterator> collisElems; // elements wich are added at each iteration
00053     sofa::helper::vector<core::CollisionElementIterator> collisElemsImmobile[2]; // elements which are added only once
00054 
00055     Vector3 minCell, maxCell;
00056     int timeStamp;
00057 public:
00058     // Adding a sphere in a cell of the voxel grid.
00059     // When adding a sphere, we test if there are collision with the sphere in the cell
00060     // then we add it in the vector sphere
00061     void add(VoxelGrid* grid, core::CollisionElementIterator collElem, sofa::helper::vector<core::CollisionElementIterator> &vectCollis, int phase);
00062     void eraseAll(int timeStampMethod);
00063     GridCell();
00064     
00065     void draw(int timeStampMethod);
00066     void setMinMax(const Vector3 &minimum, const Vector3& maximum);
00067 };
00068 
00069 class VoxelGrid : public BroadPhaseDetection, public NarrowPhaseDetection
00070 {
00071 private:
00072     Vector3 nbSubDiv;
00073     GridCell ***grid;
00074     bool bDraw;
00075     Vector3 minVect, maxVect, step;
00076     void posToIdx (const Vector3& pos, Vector3 &indices);
00077     simulation::tree::GNode* timeLogger;
00078     simulation::tree::GNode::ctime_t timeInter;
00079     friend class GridCell;
00080 public:
00081     VoxelGrid (Vector3 minVect = Vector3(-20.0, -20.0, -20.0), Vector3 maxVect = Vector3(-20.0, -20.0, -20.0), Vector3 nbSubdivision = Vector3(5.0, 5.0, 5.0), bool draw=false)
00082     {
00083         createVoxelGrid (minVect, maxVect, nbSubdivision);
00084         timeStamp = 0;
00085         bDraw = draw;
00086         timeLogger = NULL;
00087         timeInter = 0;
00088     }
00089     
00090     ~VoxelGrid () {}
00091     
00092      // Create a voxel grid define by minx, miny, minz, maxx, maxy, maxz and the number of subdivision on x, y, z
00093     void createVoxelGrid (const Vector3 &min, const Vector3 &max, const Vector3 &nbSubdivision);
00094     
00095     void draw();
00096     
00097     void add(core::CollisionModel *cm, int phase);
00098 
00099     void addCollisionModel(core::CollisionModel *cm);
00100     void addCollisionPair(const std::pair<core::CollisionModel*, core::CollisionModel*>& cmPair);
00101     void add (core::CollisionModel *cm);
00102 
00103     void clearBroadPhase()
00104     { 
00105         BroadPhaseDetection::clearBroadPhase();
00106         timeStamp++;
00107     }
00108     void clearNarrowPhase()
00109     { 
00110         NarrowPhaseDetection::clearNarrowPhase();
00111         timeStamp++;
00112     }
00113 
00114 };
00115 
00116 } // namespace collision
00117 
00118 } // namespace component
00119 
00120 } // namespace sofa
00121 
00122 #endif

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