Reference documentation for deal.II version 9.1.0-pre
kdtree.h
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 #ifndef dealii_numerics_kdtree_h
17 #define dealii_numerics_kdtree_h
18 
19 #include <deal.II/base/config.h>
20 
21 #ifdef DEAL_II_WITH_NANOFLANN
22 
23 # include <deal.II/base/point.h>
24 
25 # include <nanoflann.hpp>
26 
27 # include <memory>
28 
29 
30 DEAL_II_NAMESPACE_OPEN
31 
61 template <int dim>
62 class KDTree
63 {
64 public:
88  KDTree(const unsigned int max_leaf_size = 10,
89  const std::vector<Point<dim>> &pts = std::vector<Point<dim>>());
90 
91 
97  {
101  using coord_t = double;
102 
103 
108  const std::vector<Point<dim>> &points;
109 
110 
115  PointCloudAdaptor(const std::vector<Point<dim>> &_points);
116 
117 
121  size_t
122  kdtree_get_point_count() const;
123 
124 
128  coord_t
129  kdtree_distance(const coord_t *p1,
130  const size_t idx_p2,
131  const size_t size) const;
132 
133 
137  coord_t
138  kdtree_get_pt(const size_t idx, const int d) const;
139 
140 
148  template <class BBOX>
149  bool
150  kdtree_get_bbox(BBOX &) const;
151  };
152 
153 
157  using NanoFlannKDTree = typename nanoflann::KDTreeSingleIndexAdaptor<
158  nanoflann::L2_Simple_Adaptor<double, PointCloudAdaptor>,
160  dim,
161  unsigned int>;
162 
163 
181  void
182  set_points(const std::vector<Point<dim>> &pts);
183 
184 
188  const Point<dim> &operator[](const unsigned int i) const;
189 
190 
194  unsigned int
195  size() const;
196 
197 
211  std::vector<std::pair<unsigned int, double>>
212  get_points_within_ball(const Point<dim> &target,
213  const double & radius,
214  const bool sorted = false) const;
215 
225  std::vector<std::pair<unsigned int, double>>
226  get_closest_points(const Point<dim> & target,
227  const unsigned int n_points) const;
228 
229 private:
233  const unsigned int max_leaf_size;
234 
235 
239  std::unique_ptr<PointCloudAdaptor> adaptor;
240 
241 
245  std::unique_ptr<NanoFlannKDTree> kdtree;
246 };
247 
248 
249 //------------ inline functions -------------
250 # ifndef DOXYGEN
251 
252 template <int dim>
253 inline unsigned int
254 KDTree<dim>::size() const
255 {
256  if (adaptor)
257  return adaptor->points.size();
258  else
259  return 0;
260 }
261 
262 
263 
264 template <int dim>
265 inline const Point<dim> &KDTree<dim>::operator[](const unsigned int i) const
266 {
267  AssertIndexRange(i, size());
268  return adaptor->points[i];
269 }
270 
271 
272 
273 template <int dim>
275  const std::vector<Point<dim>> &_points)
276  : points(_points)
277 {}
278 
279 
280 
281 template <int dim>
282 inline size_t
284 {
285  return points.size();
286 }
287 
288 
289 
290 template <int dim>
291 inline double
292 KDTree<dim>::PointCloudAdaptor::kdtree_get_pt(const size_t idx, int d) const
293 {
294  AssertIndexRange(d, dim);
295  return points[idx][d];
296 }
297 
298 
299 
300 template <int dim>
301 template <class BBOX>
302 inline bool
304 {
305  return false;
306 }
307 
308 
309 
310 template <int dim>
311 inline double
313  const size_t idx_p2,
314  const size_t size) const
315 {
316  AssertDimension(size, dim);
317  double res = 0.0;
318  for (size_t d = 0; d < size; ++d)
319  res += (p1[d] - points[idx_p2][d]) * (p1[d] - points[idx_p2][d]);
320  return std::sqrt(res);
321 }
322 # endif
323 
324 DEAL_II_NAMESPACE_CLOSE
325 
326 #endif // DEAL_II_WITH_NANO_FLANN
327 #endif
const std::vector< Point< dim > > & points
Definition: kdtree.h:108
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1366
Definition: kdtree.h:62
#define AssertIndexRange(index, range)
Definition: exceptions.h:1407
std::unique_ptr< NanoFlannKDTree > kdtree
Definition: kdtree.h:245
typename nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< double, PointCloudAdaptor >, PointCloudAdaptor, dim, unsigned int > NanoFlannKDTree
Definition: kdtree.h:161
unsigned int size() const
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
coord_t kdtree_get_pt(const size_t idx, const int d) const
PointCloudAdaptor(const std::vector< Point< dim >> &_points)
const Point< dim > & operator[](const unsigned int i) const
void set_points(const std::vector< Point< dim >> &pts)
Definition: kdtree.cc:84
bool kdtree_get_bbox(BBOX &) const
coord_t kdtree_distance(const coord_t *p1, const size_t idx_p2, const size_t size) const
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
size_t kdtree_get_point_count() const
const unsigned int max_leaf_size
Definition: kdtree.h:233