KrisLibrary  1.0.0
Octree.h
1 #ifndef GEOMETRY_OCTREE_H
2 #define GEOMETRY_OCTREE_H
3 
4 #include <KrisLibrary/math3d/AABB3D.h>
5 #include <KrisLibrary/math3d/Box3D.h>
6 #include <KrisLibrary/math3d/Ray3D.h>
7 #include <vector>
8 #include <list>
9 
10 namespace Geometry {
11  using namespace std;
12  using namespace Math3D;
13 
14 
15 struct OctreeNode
16 {
17  //child indices
18  enum {LLL,LLU,LUL,LUU,ULL,ULU,UUL,UUU};
19 
20  AABB3D bb;
21  int parentIndex;
22  int childIndices[8];
23 };
24 
25 class Octree
26 {
27  public:
29  Octree(const AABB3D& bb);
30  virtual ~Octree() {}
32  int NumNodes() const { return (int)nodes.size()-(int)freeNodes.size(); }
34  int Size() const { return NumNodes(); }
36  bool IsLeaf(const OctreeNode& n) const { return n.childIndices[0] < 0; }
38  int Depth(const OctreeNode& n) const { if(n.parentIndex<0) return 0; return 1+Depth(nodes[n.parentIndex]); }
40  int Index(const OctreeNode& n) const { return int(&n - &nodes[0]); }
42  const OctreeNode& Node(int index) const { return nodes[index]; }
44  OctreeNode& Node(int index) { return nodes[index]; }
46  int MaxDepth() const;
48  void SplitToDepth(int d);
51  void SplitToDepth(OctreeNode& n,int d);
55  OctreeNode* SplitToDepth(OctreeNode& n,const Vector3& point,int d);
57  void SplitToResolution(const Vector3& res);
60  void SplitToResolution(OctreeNode& n,const Vector3& res);
64  OctreeNode* SplitToResolution(OctreeNode& n,const Vector3& point,const Vector3& res);
66  int Child(const OctreeNode& n,const Vector3& pt) const;
68  void Range(const OctreeNode& n,int child,AABB3D& bbchild) const;
70  OctreeNode* Lookup(const Vector3& point);
72  OctreeNode* Lookup(OctreeNode& root,const Vector3& point);
75  OctreeNode* Lookup(OctreeNode& root,const Vector3& point,int depthMax);
78  void BoxLookup(const Vector3& bmin,const Vector3& bmax,vector<int>& nodeIndices) const;
80  void BoxLookup(const Box3D& b,vector<int>& nodeIndices) const;
83  void BallLookup(const Vector3& c,Real r,vector<int>& nodeIndices) const;
85  void RayLookup(const Ray3D& ray,vector<int>& nodeindices) const;
88  void FattenedRayLookup(const Ray3D& ray,Real radius,vector<int>& nodeindices) const;
90  virtual void Split(int nodeindex);
92  virtual void Join(int nodeindex);
93 
94  protected:
95  virtual int AddNode(int parent=-1);
96  virtual void DeleteNode(int);
97  void _RayLookup(int nodeindex,const Ray3D& ray,vector<int>& nodeindices) const;
98  void _FattenedRayLookup(int nodeindex,const Ray3D& ray,Real radius,vector<int>& nodeindices) const;
99 
100  vector<OctreeNode> nodes;
101  list<int> freeNodes;
102 };
103 
107 class OctreePointSet : public Octree
108 {
109  public:
110  OctreePointSet(const AABB3D& bbox,int maxPointsPerCell=1,Real minCellSize=0);
111  virtual ~OctreePointSet() {}
112  void GetPoints(int node,vector<Vector3>& pts) const;
113  void GetPoints(const OctreeNode& node,vector<Vector3>& pts) const { GetPoints(Index(node),pts); }
114  void GetPointIDs(int node,vector<int>& ids) const;
115  void GetPointIDs(const OctreeNode& node,vector<int>& ids) const { GetPointIDs(Index(node),ids); }
116  void Add(const Vector3& pt,int id=-1);
117  void BoxQuery(const Vector3& bmin,const Vector3& bmax,vector<Vector3>& points,vector<int>& ids) const;
118  void BoxQuery(const Box3D& b,vector<Vector3>& points,vector<int>& ids) const;
119  void BallQuery(const Vector3& c,Real r,vector<Vector3>& points,vector<int>& ids) const;
122  void RayQuery(const Ray3D& r,Real radius,vector<Vector3>& points,vector<int>& ids) const;
125  int RayCast(const Ray3D& r,Real radius) const;
126  bool NearestNeighbor(const Vector3& c,Vector3& closest,int& id) const;
127  void KNearestNeighbors(const Vector3& c,int k,vector<Vector3>& closest,vector<int>& ids) const;
130  void Collapse(int maxSize=0);
134  void FitToPoints();
135  const Sphere3D& Ball(int index) const { return balls[index]; }
136 
137  protected:
138  Real _NearestNeighbor(const OctreeNode& n,const Vector3& c,Vector3& closest,int& id,Real minDist) const;
139  int _KNearestNeighbors(const OctreeNode& n,const Vector3& c,vector<Vector3>& closest,vector<int>& ids,vector<Real>& distances,int kmin) const;
140  virtual int AddNode(int parent=-1);
141  virtual void DeleteNode(int id);
142  virtual void Split(int nodeindex);
143  virtual void Join(int nodeindex);
144 
145  int maxPointsPerCell;
146  Real minCellSize;
147  vector<vector<int> > indexLists;
148  vector<Vector3> points;
149  vector<int> ids;
150  vector<Sphere3D> balls;
151  bool fit;
152 };
153 
158 class OctreeScalarField : public Octree
159 {
160  public:
161  OctreeScalarField(const AABB3D& bb,Real defaultValue = -Inf);
162  virtual ~OctreeScalarField() {}
166  void Set(const Vector3& pt,Real value,int id=-1);
168  Real Value(const Vector3& pt) const;
171  bool ValueIn(const Vector3& pt,Real valueMin,Real valueMax) const;
174  bool ValueGreater(const Vector3& pt,Real bound) const;
176  bool ValueLess(const Vector3& pt,Real bound) const;
180  void BoxLookupRange(const Vector3& bmin,const Vector3& bmax,Real valueMin,Real valueMax,vector<int>& nodeIndices,bool inclusive=true) const;
183  void BoxLookupGreater(const Vector3& bmin,const Vector3& bmax,Real bound,vector<int>& nodeIndices) const;
186  void BoxLookupLess(const Vector3& bmin,const Vector3& bmax,Real bound,vector<int>& nodeIndices) const;
189  void Collapse(Real tolerance=0);
190 
191  protected:
192  struct Data {
194  Real value;
196  Real valueMin,valueMax;
198  int id;
199  };
200 
201  virtual int AddNode(int parent=-1);
202  virtual void DeleteNode(int id);
203  virtual void Split(int nodeindex);
204  virtual void Join(int nodeindex);
205 
206  Real defaultValue;
207  vector<Data> data;
208 };
209 
210 } //namespcae Geometry
211 
212 #endif
int NumNodes() const
Returns the number of nodes in the octree.
Definition: Octree.h:32
A 3D vector class.
Definition: math3d/primitives.h:136
int Depth(const OctreeNode &n) const
Returns the depth of the node in the tree (root is depth 0)
Definition: Octree.h:38
int id
An ID.
Definition: Octree.h:198
Definition: rayprimitives.h:132
A 3D axis-aligned bounding box.
Definition: AABB3D.h:13
Definition: Octree.h:192
int Index(const OctreeNode &n) const
Returns the index of the node.
Definition: Octree.h:40
OctreeNode & Node(int index)
Returns the node for a given index.
Definition: Octree.h:44
An integer tuple class.
Definition: IntTuple.h:14
bool IsLeaf(const OctreeNode &n) const
Tests whether a node is a leaf.
Definition: Octree.h:36
Stores a function f(x) on an octree grid. Allows for O(d) setting, sub O(d) testing of f(x) in range ...
Definition: Octree.h:158
A 3D sphere class.
Definition: Sphere3D.h:21
Real valueMin
The min / max value.
Definition: Octree.h:196
Contains all the definitions in the Math3D package.
Definition: AnyGeometry.h:12
Definition: Octree.h:15
int RayCast(const CollisionMesh &mesh, const Ray3D &r, Vector3 &pt)
Definition: CollisionMesh.cpp:1749
Definition: Octree.h:25
int Size() const
Returns the number of nodes in the octree.
Definition: Octree.h:34
Real value
The value, at a leaf, or the average value of leaves, if it&#39;s non-leaf.
Definition: Octree.h:194
Stores a point set P on an octree grid. Allows for O(d) adding, O(d h) range queries and pseudo-O(d) ...
Definition: Octree.h:107
A 3D boxThe box is the unit cube [0,1]^3 set in the scaled local coordinate system. That is, one corner is at the origin, and it has dimensions [dims.x,dims.y,dims.z] in the coordinates given by {xbasis,ybasis,zbasis}.
Definition: Box3D.h:21
Contains all definitions in the Geometry package.
Definition: AnyGeometry.h:11
const OctreeNode & Node(int index) const
Returns the node for a given index.
Definition: Octree.h:42
Definition: Ray3D.h:8