KrisLibrary  1.0.0
BallTree.h
1 #ifndef GEOMETRY_BallTREE_H
2 #define GEOMETRY_BallTREE_H
3 
4 #include "KDTree.h"
5 #include <KrisLibrary/math/vector.h>
6 #include <vector>
7 #include <functional>
8 #include <memory>
9 using namespace Math;
10 
11 namespace Geometry {
12 
29 {
30 public:
31  typedef KDTree::Point Point;
32 
33  BallTreeNode();
34 
35  inline bool IsLeaf() const { return children.empty(); }
36  int MaxDepth() const;
37  int MinDepth() const;
38 
40  int MaxLeafSize() const;
41  int MinLeafSize() const;
42 
44  int TreeSize() const;
45 
46  Vector center;
47  Real radius;
48 
49  std::vector<Point> pts;
50 
51  BallTreeNode* parent;
52  std::vector<std::unique_ptr<BallTreeNode> > children;
53 };
54 
55 class BallTree
56 {
57 public:
58  typedef std::function<double(const Vector&,const Vector&)> Metric;
59  typedef std::function<void(const Vector&,const Vector&,Real,Vector&)> Interpolator;
60 
61  Metric metric;
62  BallTreeNode root;
63  Interpolator interpolator;
64  bool cartesian;
65  int splitsPerNode;
66 
67  BallTree(Metric metric,int numSplitsPerNode=2);
68  BallTree(Metric metric,Interpolator interpolator,int numSplitsPerNode=2);
69 
71  inline int MaxDepth() const { return root.MaxDepth(); }
72  inline int MinDepth() const { return root.MinDepth(); }
74  inline int MaxLeafSize() const { return root.MaxLeafSize(); }
75  inline int MinLeafSize() const { return root.MinLeafSize(); }
76 
78  inline int TreeSize() const { return root.TreeSize(); }
79 
81  void Build(const std::vector<Vector>& p,int maxDepth);
82 
86  BallTreeNode* Insert(const Vector& p,int id,int maxLeafPoints=-1);
87 
89  bool Split(BallTreeNode* node);
91  void Join(BallTreeNode* node);
93  void Fit(BallTreeNode* node,bool tight);
95  void Clear();
96 
98  int ClosestPoint(const Vector& pt,Real& dist) const;
99 
103  int PointWithin(const Vector& pt,Real& dist) const;
104 
106  void ClosePoints(const Vector& pt,Real radius,std::vector<Real>& distances,std::vector<int>& ids) const;
107 
110  void KClosestPoints(const Vector& pt,int k,Real* dist,int* idx) const;
111 
112 private:
113  void _ClosestPoint(const BallTreeNode* node,const Vector& pt,Real& dist,int& idx) const;
114  void _ClosePoints(const BallTreeNode* node,const Vector& pt,Real radius,std::vector<Real>& distances,std::vector<int>& ids) const;
115  void _KClosestPoints(const BallTreeNode* node,const Vector& pt,int k,Real* dist,int* idx,int& maxdist) const;
116  BallTreeNode* _LookupClosestLeaf(BallTreeNode* node,const Vector& pt,Real& dist);
117 };
118 
119 
120 } //namespace Geometry
121 
122 #endif
int MaxLeafSize() const
max/min number of points in leaves of tree
Definition: BallTree.cpp:43
A node of a ball-tree point location data structure.
Definition: BallTree.h:28
int TreeSize() const
Number of nodes in tree.
Definition: BallTree.cpp:60
A base class for all 1D interpolators.
Definition: Interpolator.h:10
Definition: KDTree.h:33
int ClosestPoint(const CollisionMesh &mesh, const Vector3 &p, Vector3 &cp, Real bound)
Definition: CollisionMesh.cpp:1309
int MaxDepth() const
depth statistics
Definition: BallTree.h:71
Contains all definitions in the Math package.
Definition: WorkspaceBound.h:12
int MaxLeafSize() const
max/min number of points in leaves of tree
Definition: BallTree.h:74
Definition: BallTree.h:55
Contains all definitions in the Geometry package.
Definition: AnyGeometry.cpp:26
int TreeSize() const
Number of nodes in tree.
Definition: BallTree.h:78