KrisLibrary  1.0.0
KDTree.h
1 #ifndef GEOMETRY_KDTREE_H
2 #define GEOMETRY_KDTREE_H
3 
4 #include <KrisLibrary/math/vector.h>
5 #include <vector>
6 using namespace Math;
7 
8 namespace Geometry {
9 
27 class KDTree
28 {
29  public:
30  struct Point {
31  Point();
32  Point(const Point& pt);
33  const Point& operator = (const Point&);
34  Vector pt;
35  int id;
36  };
37 
39  static KDTree* Create(const std::vector<Vector>& p, int k, int maxDepth);
40 
41  KDTree();
42  KDTree(const std::vector<Point>& pts, int k, int depth=0, int maxDepth=100);
43  ~KDTree();
44 
45  inline bool IsLeaf() const { return splitDim==-1; }
46  int MaxDepth() const;
47  int MinDepth() const;
48 
50  int MaxLeafSize() const;
51  int MinLeafSize() const;
52 
54  int TreeSize() const;
55 
57  KDTree* Insert(const Vector& p,int id,int maxLeafPoints=2);
59  KDTree* Locate(const Vector& p);
60 
63  bool Split(int dimension=-1);
65  void Join();
67  void Clear();
68 
70  int ClosestPoint(const Vector& pt,Real& dist) const;
72  int ClosestPoint(const Vector& pt,Real n,const Vector& w,Real& dist) const;
73 
77  int PointWithin(const Vector& pt,Real& dist) const;
79  void ClosePoints(const Vector& pt,Real radius,std::vector<Real>& distances,std::vector<int>& ids) const;
81  void ClosePoints(const Vector& pt,Real radius,Real n,const Vector& w,std::vector<Real>& distances,std::vector<int>& ids) const;
82 
85  void KClosestPoints(const Vector& pt,int k,Real* dist,int* idx) const;
87  void KClosestPoints(const Vector& pt,int k,Real n,const Vector& w,Real* dist,int* idx) const;
88 
90  static Real Select(const std::vector<Point>& S, int d, int k);
91 
92 private:
93  void _ClosestPoint(const Vector& pt,Real& dist,int& idx) const;
94  void _KClosestPoints(const Vector& pt,int k,Real* dist,int* idx,int& maxdist) const;
95  void _ClosestPoint2(const Vector& pt,Real& dist,int& idx,Real norm,const Vector& weights) const;
96  void _KClosestPoints2(const Vector& pt,int k,Real* dist,int* idx,int& maxdist,Real norm,const Vector& weights) const;
97 
99  KDTree(const KDTree&) {}
100  const KDTree& operator=(const KDTree&) const { return *this; }
101 
102  int depth;
103  int splitDim;
104  Real splitVal;
105  KDTree *pos,*neg;
106  std::vector<Point> pts;
107 
108  //temporary -- used for performance testing
109  int visits;
110 };
111 
112 } //namespace Geometry
113 
114 #endif
A kd-tree or a node of one.
Definition: KDTree.h:27
Definition: KDTree.h:30
Contains all definitions in the Math package.
Definition: WorkspaceBound.h:12
Contains all definitions in the Geometry package.
Definition: AnyGeometry.h:11
int ClosestPoint(const CollisionMesh &mesh, const Vector3 &p, Vector3 &cp)
Finds the closest point pt to p on m and returns the triangle index. cp is given in the mesh&#39;s local ...
Definition: CollisionMesh.cpp:1304