Reference documentation for deal.II version 9.0.0
kdtree.cc
1 #include <deal.II/numerics/kdtree.h>
2 
3 #ifdef DEAL_II_WITH_NANOFLANN
4 
5 #include<deal.II/base/std_cxx14/memory.h>
6 
7 DEAL_II_NAMESPACE_OPEN
8 
9 
10 template <int dim>
11 KDTree<dim>::KDTree(const unsigned int &max_leaf_size,
12  const std::vector<Point<dim> > &pts)
13  :
14  max_leaf_size(max_leaf_size)
15 {
16  if (pts.size() > 0)
17  set_points(pts);
18 }
19 
20 
21 
22 template <int dim>
23 std::vector<std::pair<unsigned int, double> >
25  const double &radius,
26  bool sorted) const
27 {
28  Assert(adaptor, ExcNotInitialized());
29  Assert(kdtree, ExcInternalError());
30 
31  Assert(radius > 0,
32  ExcMessage("Radius is expected to be positive."));
33 
34  nanoflann::SearchParams params;
35  params.sorted = sorted;
36 
37  std::vector<std::pair<unsigned int, double> > matches;
38  kdtree->radiusSearch(&center[0], radius, matches, params);
39 
40  return matches;
41 }
42 
43 
44 
45 template <int dim>
46 std::vector<std::pair<unsigned int, double> >
48  const unsigned int n_points) const
49 {
50  Assert(adaptor, ExcNotInitialized());
51  Assert(kdtree, ExcInternalError());
52 
53  // get the information out of nanoflann
54  std::vector<unsigned int> indices(n_points);
55  std::vector<double> distances(n_points);
56 
57  kdtree->knnSearch(&target[0], n_points, &indices[0], &distances[0]);
58 
59  // convert it to the format we want to return
60  std::vector<std::pair<unsigned int, double> > matches(n_points);
61  for (unsigned int i=0; i<n_points; ++i)
62  matches[i] = std::make_pair(indices[i], distances[i]);
63 
64  return matches;
65 }
66 
67 
68 
69 template <int dim>
70 void KDTree<dim>::set_points(const std::vector<Point<dim> > &pts)
71 {
72  Assert(pts.size() > 0, ExcMessage("Expecting a non zero set of points."));
73  adaptor = std_cxx14::make_unique<PointCloudAdaptor>(pts);
74  kdtree = std_cxx14::make_unique<NanoFlannKDTree>(dim,
75  *adaptor,
76  nanoflann::KDTreeSingleIndexAdaptorParams(max_leaf_size));
77  kdtree->buildIndex();
78 }
79 
80 
81 template class KDTree<1>;
82 template class KDTree<2>;
83 template class KDTree<3>;
84 
85 DEAL_II_NAMESPACE_CLOSE
86 
87 #endif
Definition: kdtree.h:63
void set_points(const std::vector< Point< dim > > &pts)
Definition: kdtree.cc:70
static ::ExceptionBase & ExcNotInitialized()
std::vector< std::pair< unsigned int, double > > get_closest_points(const Point< dim > &target, const unsigned int n_points) const
Definition: kdtree.cc:47
static ::ExceptionBase & ExcMessage(std::string arg1)
KDTree(const unsigned int &max_leaf_size=10, const std::vector< Point< dim > > &pts=std::vector< Point< dim > >())
Definition: kdtree.cc:11
#define Assert(cond, exc)
Definition: exceptions.h:1142
std::vector< std::pair< unsigned int, double > > get_points_within_ball(const Point< dim > &target, const double &radius, const bool sorted=false) const
Definition: kdtree.cc:24
static ::ExceptionBase & ExcInternalError()