Medusa  1.1
Coordinate Free Mehless Method implementation
KDTreeMutable_fwd.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_SPATIAL_SEARCH_KDTREEMUTABLE_FWD_HPP_
2 #define MEDUSA_BITS_SPATIAL_SEARCH_KDTREEMUTABLE_FWD_HPP_
3 
11 #include <medusa/Config.hpp>
13 #include <nanoflann/nanoflann.hpp>
14 #include <array>
15 #include <iosfwd>
16 #include "PointCloud.hpp"
17 
18 namespace mm {
19 
35 template <class vec_t>
36 class KDTreeMutable {
37  public:
38  typedef vec_t vector_t;
39  typedef typename vector_t::scalar_t scalar_t;
40  enum { dim = vec_t::dim };
42  static_assert(std::is_same<double, scalar_t>::value,
43  "Not Implemented Error: only implemented for double type!");
44  private:
45  typedef nanoflann::KDTreeSingleIndexDynamicAdaptor<
46  nanoflann::L2_Simple_Adaptor<scalar_t, kdtree_internal::PointCloud<vec_t>>,
48 
50  int size_;
52 
53  public:
58  explicit KDTreeMutable(const Range<vec_t>& points) :
59  points_(points), size_(points.size()),
60  tree(dim, points_, nanoflann::KDTreeSingleIndexAdaptorParams(20)) {}
61 
64  tree(dim, points_, nanoflann::KDTreeSingleIndexAdaptorParams(20)) {}
65 
73  void reset(const Range<vec_t>& points) {
74  points_.setPts(points);
75  size_ = points.size();
76  tree.reset();
77  }
82  void insert(const vec_t& point);
83 
85  void insert(const Range<vec_t>& points);
86 
88  bool existsPointInSphere(const vec_t& p, scalar_t r) {
89  if (size_ == 0) return false;
90  return query(p).second[0] <= r*r;
91  }
92 
98  void remove(int index) {
99  size_ -= tree.removePoint(index);
100  }
101 
103  int size() const { return size_; }
104 
114  std::pair<mm::Range<int>, mm::Range<double>> query(const vec_t& point, int k = 1);
115 
117  template <class V>
118  friend std::ostream& operator<<(std::ostream& os, const KDTreeMutable<V>& tree);
119 };
120 
121 } // namespace mm
122 
123 #endif // MEDUSA_BITS_SPATIAL_SEARCH_KDTREEMUTABLE_FWD_HPP_
mm::KDTreeMutable::insert
void insert(const vec_t &point)
Insert a point into the tree.
Definition: KDTreeMutable.hpp:24
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
mm::KDTreeMutable::kd_tree_t
nanoflann::KDTreeSingleIndexDynamicAdaptor< nanoflann::L2_Simple_Adaptor< scalar_t, kdtree_internal::PointCloud< vec_t > >, kdtree_internal::PointCloud< vec_t >, dim > kd_tree_t
Tree type.
Definition: KDTreeMutable_fwd.hpp:43
scalar_t
Scalar scalar_t
Type of the elements, alias of Scalar.
Definition: MatrixBaseAddons.hpp:16
mm::KDTreeMutable
A k-d tree data structure that supports dynamic insertions and lazy-removal.
Definition: HalfLinksRefine_fwd.hpp:18
mm::kdtree_internal::PointCloud
Helper class for KDTree with appropriate accessors containing a set of points.
Definition: PointCloud.hpp:18
mm::KDTreeMutable::query
std::pair< mm::Range< int >, mm::Range< double > > query(const vec_t &point, int k=1)
Find k nearest neighbors to given point.
Definition: KDTreeMutable.hpp:45
mm::KDTreeMutable::operator<<
friend std::ostream & operator<<(std::ostream &os, const KDTreeMutable< V > &tree)
Output basic info about given tree.
Definition: KDTreeMutable.hpp:17
dim
@ dim
Number of elements of this matrix.
Definition: MatrixBaseAddons.hpp:14
mm::KDTreeMutable::remove
void remove(int index)
Removes a point with given index from the tree.
Definition: KDTreeMutable_fwd.hpp:98
mm::KDTreeMutable::scalar_t
vector_t::scalar_t scalar_t
Scalar type used.
Definition: KDTreeMutable_fwd.hpp:39
mm::KDTreeMutable::vector_t
vec_t vector_t
Vector type used.
Definition: KDTreeMutable_fwd.hpp:38
Config.hpp
mm::KDTreeMutable::dim
@ dim
Dimensionality of the space.
Definition: KDTreeMutable_fwd.hpp:41
Range_fwd.hpp
mm::KDTreeMutable::existsPointInSphere
bool existsPointInSphere(const vec_t &p, scalar_t r)
Check if any point exists in sphere centered at p with radius r.
Definition: KDTreeMutable_fwd.hpp:88
mm::KDTreeMutable::reset
void reset(const Range< vec_t > &points)
Grows a new tree with new points.
Definition: KDTreeMutable_fwd.hpp:73
mm::KDTreeMutable::size_
int size_
Number of points in the tree.
Definition: KDTreeMutable_fwd.hpp:50
mm::Range::size
int size() const
Returns number of elements.
Definition: Range_fwd.hpp:185
mm::KDTreeMutable::tree
kd_tree_t tree
Actual tree build over points.
Definition: KDTreeMutable_fwd.hpp:51
mm::KDTreeMutable::size
int size() const
Returns number of points in the tree.
Definition: KDTreeMutable_fwd.hpp:103
mm::KDTreeMutable::points_
kdtree_internal::PointCloud< vec_t > points_
Points, contained in the tree.
Definition: KDTreeMutable_fwd.hpp:49
mm::Range< vec_t >
mm::KDTreeMutable::KDTreeMutable
KDTreeMutable()
Creates an empty k-d tree.
Definition: KDTreeMutable_fwd.hpp:63
PointCloud.hpp
mm::KDTreeMutable::KDTreeMutable
KDTreeMutable(const Range< vec_t > &points)
Constructor that builds the search tree for the given points.
Definition: KDTreeMutable_fwd.hpp:58