deal2lkit: A ToolKit library for Deal.II
parsed_kdtree_distance.h
Go to the documentation of this file.
1 //-----------------------------------------------------------
2 //
3 // Copyright (C) 2016 by the deal2lkit authors
4 //
5 // This file is part of the deal2lkit library.
6 //
7 // The deal2lkit 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 deal2lkit distribution.
13 //
14 //-----------------------------------------------------------
15 
16 #ifndef _d2k_parsed_kdtree_distance_h
17 #define _d2k_parsed_kdtree_distance_h
18 
19 #include <deal2lkit/config.h>
20 #include <deal2lkit/utilities.h>
21 #include <deal.II/base/function.h>
23 
24 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
25 #include <external/nanoflann.h>
26 DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
27 
28 using namespace dealii;
29 
30 D2K_NAMESPACE_OPEN
31 
44 template<int dim>
45 class ParsedKDTreeDistance : public ParameterAcceptor, public Function<dim>
46 {
47 public:
73  ParsedKDTreeDistance(const std::string &name="",
74  const unsigned int &max_leaf_size=10,
75  const std::vector<Point<dim> > &pts=std::vector<Point<dim> >());
76 
77 
84  {
88  typedef double coord_t;
89 
90 
95  const std::vector<Point<dim> > &points;
96 
97 
101  PointCloudAdaptor(const std::vector<Point<dim> > &_points) : points(_points) { }
102 
103 
107  inline size_t kdtree_get_point_count() const
108  {
109  return points.size();
110  }
111 
112 
116  inline coord_t kdtree_distance(const coord_t *p1, const size_t idx_p2,size_t size) const
117  {
118  AssertDimension(size, dim);
119  coord_t res=0.0;
120  for (size_t d=0; d<size; ++d)
121  res += (p1[d]-points[idx_p2][d])*(p1[d]-points[idx_p2][d]);
122  return std::sqrt(res);
123  }
124 
125 
129  inline coord_t kdtree_get_pt(const size_t idx, int d) const
130  {
131  AssertIndexRange(d,dim);
132  return points[idx][d];
133  }
134 
135 
143  template <class BBOX>
144  bool kdtree_get_bbox(BBOX &) const
145  {
146  return false;
147  }
148  };
149 
150 
154  typedef typename nanoflann::KDTreeSingleIndexAdaptor<nanoflann::L2_Simple_Adaptor<double, PointCloudAdaptor> ,
155  PointCloudAdaptor, dim, unsigned int> KDTree;
156 
160  virtual void declare_parameters(ParameterHandler &prm);
161 
162 
180  virtual double value(const Point<dim> &p, const unsigned int component=0) const;
181 
182 
201  void set_points(const std::vector<Point<dim> > &pts);
202 
203 
207  inline const Point<dim> &operator[](unsigned int i) const
208  {
209  AssertIndexRange(i, size());
210  return adaptor->points[i];
211  }
212 
213 
217  inline unsigned int size() const
218  {
219  if (adaptor)
220  return adaptor->points.size();
221  else
222  return 0;
223  };
224 
225 
239  unsigned int get_points_within_ball(const Point<dim> &target, const double &radius,
240  std::vector<std::pair<unsigned int, double> > &matches,
241  bool sorted=false) const;
242 
254  void get_closest_points(const Point<dim> &target,
255  std::vector<unsigned int> &indices,
256  std::vector<double> &distances) const;
257 
258 private:
262  unsigned int max_leaf_size;
263 
264 
268  shared_ptr<PointCloudAdaptor> adaptor;
269 
270 
274  shared_ptr<KDTree> kdtree;
275 };
276 
277 D2K_NAMESPACE_CLOSE
278 
279 #endif
A deal2lkit wrapper for the nanoflann library, used to compute the distance from a collection of poin...
nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< double, PointCloudAdaptor >, PointCloudAdaptor, dim, unsigned int > KDTree
A typedef for the actual KDTree object.
const Point< dim > & operator[](unsigned int i) const
A const accessor to the underlying points.
#define AssertDimension(dim1, dim2)
A parameter acceptor base class.
Adaptor class used internally by nanoflann.
double coord_t
A typedef used by nanoflann.
unsigned int size() const
The size of the vector stored by this class.
shared_ptr< KDTree > kdtree
The actual kdtree.
#define AssertIndexRange(index, range)
unsigned int max_leaf_size
Max number of points per leaf.
const std::vector< Point< dim > > & points
Reference to the vector of points from which we want to compute the distance.
bool kdtree_get_bbox(BBOX &) const
Optional bounding-box computation: return false to default to a standard bbox computation loop...
coord_t kdtree_get_pt(const size_t idx, int d) const
Return the dim&#39;th component of the idx&#39;th point in the class.
shared_ptr< PointCloudAdaptor > adaptor
A point cloud adaptor, to be filled when set points is called.
static const bool value
PointCloudAdaptor(const std::vector< Point< dim > > &_points)
The constrcutor needs the data set source.
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
coord_t kdtree_distance(const coord_t *p1, const size_t idx_p2, size_t size) const
Return the L2 distance between points.
size_t kdtree_get_point_count() const
Return number of points in the data set (required by nanoflann).