Reference documentation for deal.II version 9.0.0
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 at
12 // the top level of the deal.II distribution.
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 <memory>
26 
27 #include <nanoflann.hpp>
28 
29 
30 DEAL_II_NAMESPACE_OPEN
31 
62 template <int dim>
63 class KDTree
64 {
65 public:
92  KDTree(const unsigned int &max_leaf_size = 10,
93  const std::vector<Point<dim> > &pts = std::vector<Point<dim> >());
94 
95 
102  {
106  typedef double coord_t;
107 
108 
113  const std::vector<Point<dim> > &points;
114 
115 
120  PointCloudAdaptor (const std::vector<Point<dim> > &_points);
121 
122 
126  size_t kdtree_get_point_count() const;
127 
128 
132  coord_t kdtree_distance (const coord_t *p1,
133  const size_t idx_p2,
134  const size_t size) const;
135 
136 
140  coord_t kdtree_get_pt (const size_t idx,
141  const int d) const;
142 
143 
151  template <class BBOX>
152  bool kdtree_get_bbox (BBOX &) const;
153  };
154 
155 
159  typedef
160  typename nanoflann::KDTreeSingleIndexAdaptor<nanoflann::L2_Simple_Adaptor<double, PointCloudAdaptor> ,
161  PointCloudAdaptor, dim, unsigned int>
163 
164 
183  void set_points (const std::vector<Point<dim> > &pts);
184 
185 
189  const Point<dim> &operator[] (const unsigned int i) const;
190 
191 
195  unsigned int size() const;
196 
197 
209  std::vector<std::pair<unsigned int, double> >
210  get_points_within_ball (const Point<dim> &target,
211  const double &radius,
212  const bool sorted=false) const;
213 
223  std::vector<std::pair<unsigned int, double> >
224  get_closest_points (const Point<dim> &target,
225  const unsigned int n_points) const;
226 
227 private:
231  const unsigned int max_leaf_size;
232 
233 
237  std::unique_ptr<PointCloudAdaptor> adaptor;
238 
239 
243  std::unique_ptr<NanoFlannKDTree> kdtree;
244 };
245 
246 
247 //------------ inline functions -------------
248 #ifndef DOXYGEN
249 
250 template <int dim>
251 inline
252 unsigned int KDTree<dim>::size() const
253 {
254  if (adaptor)
255  return adaptor->points.size();
256  else
257  return 0;
258 }
259 
260 
261 
262 template <int dim>
263 inline const Point<dim> &
264 KDTree<dim>::operator[] (const unsigned int i) const
265 {
266  AssertIndexRange(i, size());
267  return adaptor->points[i];
268 }
269 
270 
271 
272 template <int dim>
274  :
275  points(_points)
276 {}
277 
278 
279 
280 template <int dim>
281 inline size_t
283 {
284  return points.size();
285 }
286 
287 
288 
289 template <int dim>
290 inline double
291 KDTree<dim>::PointCloudAdaptor::kdtree_get_pt(const size_t idx, int d) const
292 {
293  AssertIndexRange(d,dim);
294  return points[idx][d];
295 }
296 
297 
298 
299 template <int dim>
300 template <class BBOX>
301 inline bool
303 {
304  return false;
305 }
306 
307 
308 
309 template <int dim>
310 inline double
312  const size_t idx_p2,
313  const size_t size) const
314 {
315  AssertDimension(size, dim);
316  double res=0.0;
317  for (size_t d=0; d<size; ++d)
318  res += (p1[d]-points[idx_p2][d]) * (p1[d]-points[idx_p2][d]);
319  return std::sqrt(res);
320 }
321 #endif
322 
323 DEAL_II_NAMESPACE_CLOSE
324 
325 # endif // DEAL_II_WITH_NANO_FLANN
326 #endif
PointCloudAdaptor(const std::vector< Point< dim > > &_points)
const std::vector< Point< dim > > & points
Definition: kdtree.h:113
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1248
Definition: kdtree.h:63
#define AssertIndexRange(index, range)
Definition: exceptions.h:1284
std::unique_ptr< NanoFlannKDTree > kdtree
Definition: kdtree.h:243
void set_points(const std::vector< Point< dim > > &pts)
Definition: kdtree.cc:70
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_closest_points(const Point< dim > &target, const unsigned int n_points) const
Definition: kdtree.cc:47
KDTree(const unsigned int &max_leaf_size=10, const std::vector< Point< dim > > &pts=std::vector< Point< dim > >())
Definition: kdtree.cc:11
coord_t kdtree_get_pt(const size_t idx, const int d) const
size_t kdtree_get_point_count() const
const Point< dim > & operator[](const unsigned int i) const
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
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
unsigned int size() const
bool kdtree_get_bbox(BBOX &) const
std::unique_ptr< PointCloudAdaptor > adaptor
Definition: kdtree.h:237
nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< double, PointCloudAdaptor >, PointCloudAdaptor, dim, unsigned int > NanoFlannKDTree
Definition: kdtree.h:162
const unsigned int max_leaf_size
Definition: kdtree.h:231