deal2lkit: A ToolKit library for Deal.II
parsed_kdtree_distance.cc
Go to the documentation of this file.
2 
3 using namespace dealii;
4 
5 D2K_NAMESPACE_OPEN
6 
7 
8 template<int dim>
10  const unsigned int &max_leaf_size,
11  const std::vector<Point<dim> > &pts):
12  ParameterAcceptor(name),
13  max_leaf_size(max_leaf_size)
14 {
15  if (pts.size() > 0)
16  set_points(pts);
17 }
18 
19 
20 
21 template<int dim>
23 {
24 
25  add_parameter(prm, &max_leaf_size, "Max number of points per leaf",
26  std::to_string(max_leaf_size), Patterns::Double(),
27  "While building the tree, nodes are recursively divided until the number "
28  "of points inside is equal or below this value. While doing queries, the "
29  "tree algorithm ends by selecting leaf nodes, then performing linear search "
30  "(one-by-one) for the closest point to the query within all those in the leaf.");
31 
32 }
33 
34 
35 
36 template<int dim>
37 double ParsedKDTreeDistance<dim>::value(const Point<dim> &p, const unsigned int component) const
38 {
39  // Only 1 component in this function
40  AssertDimension(component, 0);
42  Assert(kdtree, ExcInternalError("Adaptor is initialized, but kdtree is not. This should not happen."));
43 
44  // do a knn search
45  const size_t num_results = 1;
46  size_t ret_index;
47  double dist;
48  nanoflann::KNNResultSet<double> resultSet(num_results);
49 
50  resultSet.init(&ret_index, &dist);
51  kdtree->findNeighbors(resultSet, &p[0], nanoflann::SearchParams());
52  //index.knnSearch(query, indices, dists, num_results, mrpt_flann::SearchParams(10));
53 
54  // silence a warning about unused variable
55  // when compiling in release mode
56  (void)component;
57  return dist;
58 }
59 
60 
61 
62 template<int dim>
63 unsigned int ParsedKDTreeDistance<dim>::get_points_within_ball(const Point<dim> &center, const double &radius,
64  std::vector<std::pair<unsigned int, double> > &matches,
65  bool sorted) const
66 {
69 
70  Assert(radius > 0,
71  ExcMessage("Radius is expected to be positive."));
72 
73  nanoflann::SearchParams params;
74  params.sorted = sorted;
75  return kdtree->radiusSearch(&center[0], radius, matches, params);
76 }
77 
78 template<int dim>
80  std::vector<unsigned int> &indices,
81  std::vector<double> &distances) const
82 {
85  AssertDimension(indices.size(), distances.size());
86 
87  kdtree->knnSearch(&target[0], indices.size(), &indices[0], &distances[0]);
88 }
89 
90 template<int dim>
91 void ParsedKDTreeDistance<dim>::set_points(const std::vector<Point<dim> > &pts)
92 {
93  Assert(pts.size() > 0, ExcMessage("Expecting a non zero set of points."));
94  adaptor = SP(new PointCloudAdaptor(pts));
95  kdtree = SP(new KDTree(dim, *adaptor, nanoflann::KDTreeSingleIndexAdaptorParams(max_leaf_size)));
96  kdtree->buildIndex();
97 }
98 
99 
100 template class ParsedKDTreeDistance<1>;
101 template class ParsedKDTreeDistance<2>;
102 template class ParsedKDTreeDistance<3>;
103 
104 D2K_NAMESPACE_CLOSE
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.
#define AssertDimension(dim1, dim2)
A parameter acceptor base class.
Adaptor class used internally by nanoflann.
static ParameterHandler prm
Static parameter.
shared_ptr< KDTree > kdtree
The actual kdtree.
unsigned int get_points_within_ball(const Point< dim > &target, const double &radius, std::vector< std::pair< unsigned int, double > > &matches, bool sorted=false) const
Fill a vector with the indices and the distance of the points that are at distance less than or equal...
static::ExceptionBase & ExcNotInitialized()
shared_ptr< T > SP(T *t)
Construct a shared pointer to a non const class T.
Definition: utilities.h:483
ParsedKDTreeDistance(const std::string &name="", const unsigned int &max_leaf_size=10, const std::vector< Point< dim > > &pts=std::vector< Point< dim > >())
Constructor: takes an optional name for the section.
unsigned int max_leaf_size
Max number of points per leaf.
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
void get_closest_points(const Point< dim > &target, std::vector< unsigned int > &indices, std::vector< double > &distances) const
Fill two vectors with the indices and distances of the closest points to the given target point...
shared_ptr< PointCloudAdaptor > adaptor
A point cloud adaptor, to be filled when set points is called.
virtual void declare_parameters(ParameterHandler &prm)
Calls the underlying function of ParsedFunction.
virtual double value(const Point< dim > &p, const unsigned int component=0) const
Compute the distance between the given point and the cloud of points in the currently stored adaptor...
void add_parameter(ParameterHandler &prm, T *parameter, const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())
Add a parameter the given parameter list.
void set_points(const std::vector< Point< dim > > &pts)
Store a reference to the passed points.
static::ExceptionBase & ExcInternalError()