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 
30 class KDTree
31 {
32  public:
33  struct Point {
34  Point();
35  Point(const Point& pt);
36  const Point& operator = (const Point&);
37  Vector pt;
38  int id;
39  };
40 
42  static KDTree* Create(const std::vector<Vector>& p, int k, int maxDepth);
43 
44  KDTree();
45  KDTree(const std::vector<Point>& pts, int k, int depth=0, int maxDepth=100);
46  ~KDTree();
47 
49  KDTree(const KDTree&) = delete;
50  const KDTree& operator=(const KDTree&) const = delete;
51 
52  inline bool IsLeaf() const { return splitDim==-1; }
53  int MaxDepth() const;
54  int MinDepth() const;
55 
57  int MaxLeafSize() const;
58  int MinLeafSize() const;
59 
61  int TreeSize() const;
62 
64  KDTree* Insert(const Vector& p,int id,int maxLeafPoints=2);
66  KDTree* Locate(const Vector& p);
67 
70  bool Split(int dimension=-1);
72  void Join();
74  void Clear();
75 
77  int ClosestPoint(const Vector& pt,Real& dist) const;
79  int ClosestPoint(const Vector& pt,Real n,const Vector& w,Real& dist) const;
80 
84  int PointWithin(const Vector& pt,Real& dist) const;
86  void ClosePoints(const Vector& pt,Real radius,std::vector<Real>& distances,std::vector<int>& ids) const;
88  void ClosePoints(const Vector& pt,Real radius,Real n,const Vector& w,std::vector<Real>& distances,std::vector<int>& ids) const;
89 
92  void KClosestPoints(const Vector& pt,int k,Real* dist,int* idx) const;
94  void KClosestPoints(const Vector& pt,int k,Real n,const Vector& w,Real* dist,int* idx) const;
95 
97  static Real Select(const std::vector<Point>& S, int d, int k);
98 
99 private:
100  void _ClosestPoint(const Vector& pt,Real& dist,int& idx) const;
101  void _KClosestPoints(const Vector& pt,int k,Real* dist,int* idx,int& maxdist) const;
102  void _ClosestPoint2(const Vector& pt,Real& dist,int& idx,Real norm,const Vector& weights) const;
103  void _KClosestPoints2(const Vector& pt,int k,Real* dist,int* idx,int& maxdist,Real norm,const Vector& weights) const;
104 
105  int depth;
106  int splitDim;
107  Real splitVal;
108  KDTree *pos,*neg;
109  std::vector<Point> pts;
110 
111  //temporary -- used for performance testing
112  int visits;
113 };
114 
115 } //namespace Geometry
116 
117 #endif
A kd-tree or a node of one.
Definition: KDTree.h:30
Definition: KDTree.h:33
int ClosestPoint(const CollisionMesh &mesh, const Vector3 &p, Vector3 &cp, Real bound)
Definition: CollisionMesh.cpp:1309
Contains all definitions in the Math package.
Definition: WorkspaceBound.h:12
Contains all definitions in the Geometry package.
Definition: AnyGeometry.cpp:26