Medusa  1.1
Coordinate Free Mehless Method implementation
KDTreeMutable.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_SPATIAL_SEARCH_KDTREEMUTABLE_HPP_
2 #define MEDUSA_BITS_SPATIAL_SEARCH_KDTREEMUTABLE_HPP_
3 
9 #include "KDTreeMutable_fwd.hpp"
11 #include <cmath>
12 
13 namespace mm {
14 
16 template <class V>
17 std::ostream& operator<<(std::ostream& os, const KDTreeMutable<V>& tree) {
18  return os << "KDTreeMutable:\n"
19  << " dimension: " << tree.dim << '\n'
20  << " num of points: " << tree.size() << std::endl;
21 }
22 
23 template <class vec_t>
24 void KDTreeMutable<vec_t>::insert(const vec_t& point) {
25  assert_msg(point.array().isFinite().prod() == 1, "Invalid point.");
26  auto n = points_.kdtree_get_point_count();
27  points_.add(point);
28  tree.addPoints(n, n);
29  ++size_;
30 }
31 
32 template <class vec_t>
34  auto n = points_.kdtree_get_point_count();
35  for (const auto& p : points) {
36  assert_msg(p.array().isFinite().prod() == 1, "One of the points is invalid.");
37  points_.add(p);
38  }
39  size_ += points.size();
40  tree.addPoints(n, n + points.size() - 1);
41 }
42 
43 template <class vec_t>
44 std::pair<mm::Range<int>, mm::Range<double>>
45 KDTreeMutable<vec_t>::query(const vec_t& point, int k) {
46  assert_msg(point.array().isFinite().prod() == 1, "Invalid query point.");
47  nanoflann::KNNResultSet<scalar_t, int> resultSet(k);
48  Range<int> ret_index(k);
49  Range<scalar_t> out_dist_sqr(k);
50  resultSet.init(&ret_index[0], &out_dist_sqr[0]);
51  tree.findNeighbors(resultSet, point.data(), nanoflann::SearchParams(k));
52  assert_msg(resultSet.full(), "Not enough points in the tree, you requested %d points, "
53  "but the tree contains only %d points.", k, size());
54  return {ret_index, out_dist_sqr};
55 }
56 
57 } // namespace mm
58 
59 #endif // MEDUSA_BITS_SPATIAL_SEARCH_KDTREEMUTABLE_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
A k-d tree data structure that supports dynamic insertions and lazy-removal.
Definition: HalfLinksRefine_fwd.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::operator<<
std::ostream & operator<<(std::ostream &os, const Gaussian< S > &b)
Output basic information about given Gaussian RBF.
Definition: Gaussian.hpp:37
assert_msg
#define assert_msg(cond,...)
Assert with better error reporting.
Definition: assert.hpp:75
mm::KDTreeMutable::dim
@ dim
Dimensionality of the space.
Definition: KDTreeMutable_fwd.hpp:41
assert.hpp
mm::KDTreeMutable::size
int size() const
Returns number of points in the tree.
Definition: KDTreeMutable_fwd.hpp:103
mm::Range< vec_t >
KDTreeMutable_fwd.hpp