www.mooseframework.org
KDTree.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #ifndef KDTREE_H
16 #define KDTREE_H
17 
18 // Moose includes
19 #include "MooseMesh.h"
20 
21 #include "libmesh/nanoflann.hpp"
22 
23 class KDTree
24 {
25 public:
26  KDTree(std::vector<Point> & master_points, unsigned int max_leaf_size);
27 
28  virtual ~KDTree() = default;
29 
30  void neighborSearch(Point & query_point,
31  unsigned int patch_size,
32  std::vector<std::size_t> & return_index);
33 
39  template <unsigned int KDDim>
41  {
42  private:
43  const std::vector<Point> & _pts;
44 
45  public:
46  PointListAdaptor(const std::vector<Point> & pts) : _pts(pts) {}
47 
51  using coord_t = Real;
55  inline size_t kdtree_get_point_count() const { return _pts.size(); }
56 
61  inline coord_t kdtree_distance(const coord_t * p1, const size_t idx_p2, size_t /*size*/) const
62  {
63  mooseAssert(idx_p2 <= _pts.size(),
64  "The point index should be less than"
65  "total number of points used to build"
66  "the KDTree.");
67 
68  const Point & p2(_pts[idx_p2]);
69 
70  coord_t dist = 0.0;
71 
72  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
73  dist += std::pow(p1[i] - p2(i), 2.0);
74 
75  return dist;
76  }
77 
84  inline coord_t kdtree_get_pt(const size_t idx, int dim) const
85  {
86  mooseAssert(dim < (int)LIBMESH_DIM,
87  "The required component number should be less than the LIBMESH_DIM.");
88  mooseAssert(idx < _pts.size(),
89  "The index of the point should be less"
90  "than total number of points used to"
91  "construct the KDTree.");
92 
93  const Point & p(_pts[idx]);
94 
95  return p(dim);
96  }
97 
103  template <class BBOX>
104  bool kdtree_get_bbox(BBOX & /* bb */) const
105  {
106  return false;
107  }
108  };
109 
110  using KdTreeT = nanoflann::KDTreeSingleIndexAdaptor<
111  nanoflann::L2_Simple_Adaptor<Real, PointListAdaptor<LIBMESH_DIM>>,
113  LIBMESH_DIM>;
114 
115 protected:
116  PointListAdaptor<LIBMESH_DIM> _point_list_adaptor;
117  UniquePtr<KdTreeT> _kd_tree;
118 };
119 
120 #endif // KDTREE_H
KDTree(std::vector< Point > &master_points, unsigned int max_leaf_size)
Definition: KDTree.C:20
Definition: KDTree.h:23
PointListAdaptor is required to use libMesh Point coordinate type with nanoflann KDTree library...
Definition: KDTree.h:40
void neighborSearch(Point &query_point, unsigned int patch_size, std::vector< std::size_t > &return_index)
Definition: KDTree.C:31
virtual ~KDTree()=default
UniquePtr< KdTreeT > _kd_tree
Definition: KDTree.h:117
nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< Real, PointListAdaptor< LIBMESH_DIM >>, PointListAdaptor< LIBMESH_DIM >, LIBMESH_DIM > KdTreeT
Definition: KDTree.h:113
PointListAdaptor(const std::vector< Point > &pts)
Definition: KDTree.h:46
coord_t kdtree_get_pt(const size_t idx, int dim) const
Returns the dim&#39;th component of the idx&#39;th point in the class: Since this is inlined and the "dim" ar...
Definition: KDTree.h:84
coord_t kdtree_distance(const coord_t *p1, const size_t idx_p2, size_t) const
Returns the distance between the vector "p1[0:size-1]" and the data point with index "idx_p2" stored ...
Definition: KDTree.h:61
Real coord_t
libMesh Point coordinate type
Definition: KDTree.h:51
const std::vector< Point > & _pts
Definition: KDTree.h:43
bool kdtree_get_bbox(BBOX &) const
Optional bounding-box computation.
Definition: KDTree.h:104
size_t kdtree_get_point_count() const
Must return the number of data points.
Definition: KDTree.h:55
PointListAdaptor< LIBMESH_DIM > _point_list_adaptor
Definition: KDTree.h:116