00001
00002
00003
00004
00005
00006
00007 #ifndef _PHYSSIM_OCTREE_H_
00008 #define _PHYSSIM_OCTREE_H_
00009
00010 #include <boost/shared_ptr.hpp>
00011 #include <boost/enable_shared_from_this.hpp>
00012 #include <list>
00013 #include <Physsim/Types.h>
00014 #include <Physsim/Vector3.h>
00015
00016 namespace Physsim {
00017
00019 class Octree : public boost::enable_shared_from_this<Octree>
00020 {
00021 public:
00022 Octree();
00023 Octree(Real minres);
00024 Octree(Real minres, const Vector3& lo_bounds, const Vector3& hi_bounds);
00025 Octree(boost::shared_ptr<Octree> parent, Real minres, const Vector3& lo_bounds, const Vector3& hi_bounds);
00026 void insert(const Vector3& point);
00027 bool clear_cell(const Vector3& point);
00028 OctreePtr is_cell_occupied(const Vector3& point) const;
00029 bool is_occupied(const Vector3& lo_bounds, const Vector3& hi_bounds) const;
00030 void set_bounds(const Vector3& lo_bounds, const Vector3& hi_bounds);
00031 void get_bounds(Vector3& lo_bounds, Vector3& hi_bounds) const;
00032 void reset();
00033
00035 const std::vector<OctreePtr>& get_children() const { return _children; }
00036
00038 void set_parent(OctreePtr parent) { _parent = parent; }
00039
00041 OctreePtr get_parent() const { return (_parent.expired()) ? OctreePtr() : OctreePtr(_parent); }
00042
00044 unsigned get_num_points() const { return _num_points; }
00045
00047 Real get_min_res() const { return _minres; }
00048
00050 void set_min_res(Real minres) { _minres = minres; }
00051
00053 bool is_leaf() const { return _children.empty(); }
00054
00055 private:
00056 enum BBQueryType { eOutside, ePartiallyInside, eFullyInside };
00057 static const unsigned OCT_CHILDREN = 8;
00058 std::vector<OctreePtr> _children;
00059 boost::weak_ptr<Octree> _parent;
00060 Vector3 _bounds_lo, _bounds_hi;
00061 unsigned _num_points;
00062 Real _minres;
00063
00064 static BBQueryType within_bounding_box(const std::pair<Vector3, Vector3>& query, const std::pair<Vector3, Vector3>& reference);
00065 static bool within_bounding_box(const Vector3& query, const std::pair<Vector3, Vector3>& reference);
00066 unsigned get_sub_volume_idx(const Vector3& point) const;
00067 bool subdivide();
00068 };
00069
00070 }
00071
00072 #endif
00073