Reference documentation for deal.II version 9.1.0-pre
kdtree.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2017 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #include <deal.II/numerics/kdtree.h>
17 
18 #ifdef DEAL_II_WITH_NANOFLANN
19 
20 # include <deal.II/base/std_cxx14/memory.h>
21 
22 DEAL_II_NAMESPACE_OPEN
23 
24 
25 template <int dim>
26 KDTree<dim>::KDTree(const unsigned int max_leaf_size,
27  const std::vector<Point<dim>> &pts)
28  : max_leaf_size(max_leaf_size)
29 {
30  if (pts.size() > 0)
31  set_points(pts);
32 }
33 
34 
35 
36 template <int dim>
37 std::vector<std::pair<unsigned int, double>>
39  const double & radius,
40  bool sorted) const
41 {
44 
45  Assert(radius > 0, ExcMessage("Radius is expected to be positive."));
46 
47  nanoflann::SearchParams params;
48  params.sorted = sorted;
49 
50  std::vector<std::pair<unsigned int, double>> matches;
51  kdtree->radiusSearch(&center[0], radius, matches, params);
52 
53  return matches;
54 }
55 
56 
57 
58 template <int dim>
59 std::vector<std::pair<unsigned int, double>>
61  const unsigned int n_points) const
62 {
65 
66  // get the information out of nanoflann
67  std::vector<unsigned int> indices(n_points);
68  std::vector<double> distances(n_points);
69 
70  kdtree->knnSearch(&target[0], n_points, &indices[0], &distances[0]);
71 
72  // convert it to the format we want to return
73  std::vector<std::pair<unsigned int, double>> matches(n_points);
74  for (unsigned int i = 0; i < n_points; ++i)
75  matches[i] = std::make_pair(indices[i], distances[i]);
76 
77  return matches;
78 }
79 
80 
81 
82 template <int dim>
83 void
84 KDTree<dim>::set_points(const std::vector<Point<dim>> &pts)
85 {
86  Assert(pts.size() > 0, ExcMessage("Expecting a non zero set of points."));
87  adaptor = std_cxx14::make_unique<PointCloudAdaptor>(pts);
88  kdtree = std_cxx14::make_unique<NanoFlannKDTree>(
89  dim, *adaptor, nanoflann::KDTreeSingleIndexAdaptorParams(max_leaf_size));
90  kdtree->buildIndex();
91 }
92 
93 
94 template class KDTree<1>;
95 template class KDTree<2>;
96 template class KDTree<3>;
97 
98 DEAL_II_NAMESPACE_CLOSE
99 
100 #endif
Definition: kdtree.h:62
std::unique_ptr< NanoFlannKDTree > kdtree
Definition: kdtree.h:245
static::ExceptionBase & ExcNotInitialized()
KDTree(const unsigned int max_leaf_size=10, const std::vector< Point< dim >> &pts=std::vector< Point< dim >>())
Definition: kdtree.cc:26
std::vector< std::pair< unsigned int, double > > get_closest_points(const Point< dim > &target, const unsigned int n_points) const
Definition: kdtree.cc:60
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1227
void set_points(const std::vector< Point< dim >> &pts)
Definition: kdtree.cc:84
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:38
std::unique_ptr< PointCloudAdaptor > adaptor
Definition: kdtree.h:239
const unsigned int max_leaf_size
Definition: kdtree.h:233
static::ExceptionBase & ExcInternalError()