Medusa  1.1
Coordinate Free Mehless Method implementation
KDTree_fwd.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_SPATIAL_SEARCH_KDTREE_FWD_HPP_
2 #define MEDUSA_BITS_SPATIAL_SEARCH_KDTREE_FWD_HPP_
3 
11 #include <medusa/Config.hpp>
12 #include <nanoflann/nanoflann.hpp>
14 #include <iosfwd>
16 #include "PointCloud.hpp"
17 
18 namespace mm {
19 
35 template <class vec_t>
36 class KDTree {
37  public:
38  typedef vec_t vector_t;
39  typedef typename vector_t::scalar_t scalar_t;
40  enum { dim = vec_t::dim };
42 
43  private:
44  typedef nanoflann::KDTreeSingleIndexAdaptor<
45  nanoflann::L2_Simple_Adaptor<scalar_t, kdtree_internal::PointCloud<vec_t>>,
47 
50 
51  public:
56  explicit KDTree(const Range<vec_t>& points) :
57  points_(points), tree(dim, points_, nanoflann::KDTreeSingleIndexAdaptorParams(20)) {
58  tree.buildIndex();
59  }
60 
62  KDTree() : points_(), tree(dim, points_, nanoflann::KDTreeSingleIndexAdaptorParams(20)) {}
63 
71  void reset(const Range<vec_t>& points) {
72  points_.setPts(points);
73  tree.buildIndex();
74  }
75 
85  std::pair<Range<int>, Range<scalar_t>> query(const vec_t& point, int k = 1) const;
86 
96  std::pair<Range<int>, Range<scalar_t>> query(
97  const vec_t& point, const scalar_t& radius_squared) const;
98 
113  vec_t get(int index) const { return points_.get(index); }
114 
116  Range<vec_t> get(const Range<int>& indexes) const {
117  const int n = indexes.size();
118  Range<vec_t> result(n);
119  for (int i = 0; i < n; ++i) {
120  result[i] = points_.get(indexes[i]);
121  }
122  return result;
123  }
124 
126  int size() const { return points_.kdtree_get_point_count(); }
127 
129  template <class V>
130  friend std::ostream& operator<<(std::ostream& os, const KDTree<V>& tree);
131 };
132 
133 } // namespace mm
134 
135 #endif // MEDUSA_BITS_SPATIAL_SEARCH_KDTREE_FWD_HPP_
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
scalar_t
Scalar scalar_t
Type of the elements, alias of Scalar.
Definition: MatrixBaseAddons.hpp:16
mm::KDTree
Class representing a static k-d tree data structure.
Definition: KDTree_fwd.hpp:36
mm::kdtree_internal::PointCloud
Helper class for KDTree with appropriate accessors containing a set of points.
Definition: PointCloud.hpp:18
mm::KDTree::points_
kdtree_internal::PointCloud< vec_t > points_
Points, contained in the tree.
Definition: KDTree_fwd.hpp:48
dim
@ dim
Number of elements of this matrix.
Definition: MatrixBaseAddons.hpp:14
mm::KDTree::dim
@ dim
Dimensionality of the space.
Definition: KDTree_fwd.hpp:41
mm::KDTree::kd_tree_t
nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< scalar_t, kdtree_internal::PointCloud< vec_t > >, kdtree_internal::PointCloud< vec_t >, dim, int > kd_tree_t
k-d tree type.
Definition: KDTree_fwd.hpp:46
mm::KDTree::KDTree
KDTree()
Creates an empty KDTree. The tree may later be filled using KDTree::resetTree.
Definition: KDTree_fwd.hpp:62
mm::KDTree::vector_t
vec_t vector_t
Vector type used.
Definition: KDTree_fwd.hpp:38
mm::KDTree::query
std::pair< Range< int >, Range< scalar_t > > query(const vec_t &point, int k=1) const
Find k nearest neighbors to given point.
Definition: KDTree.hpp:16
mm::KDTree::KDTree
KDTree(const Range< vec_t > &points)
Constructor that builds the search tree for the given points.
Definition: KDTree_fwd.hpp:56
Config.hpp
mm::KDTree::tree
kd_tree_t tree
Actual tree build over points.
Definition: KDTree_fwd.hpp:49
Range_fwd.hpp
mm::KDTree::operator<<
friend std::ostream & operator<<(std::ostream &os, const KDTree< V > &tree)
Output basic info about given tree.
Definition: KDTree.hpp:42
mm::KDTree::size
int size() const
Returns number of points in this tree.
Definition: KDTree_fwd.hpp:126
mm::Range::size
int size() const
Returns number of elements.
Definition: Range_fwd.hpp:185
mm::KDTree::get
Range< vec_t > get(const Range< int > &indexes) const
Vectorized version of KDTree::get.
Definition: KDTree_fwd.hpp:116
memutils.hpp
mm::KDTree::reset
void reset(const Range< vec_t > &points)
Grows a new tree with new points.
Definition: KDTree_fwd.hpp:71
mm::KDTree::scalar_t
vector_t::scalar_t scalar_t
Scalar type used.
Definition: KDTree_fwd.hpp:39
mm::KDTree::get
vec_t get(int index) const
Get the coordinates of a point in the tree.
Definition: KDTree_fwd.hpp:113
mm::Range< vec_t >
PointCloud.hpp