www.mooseframework.org
KDTree.C
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 #include "KDTree.h"
16 #include "MooseError.h"
17 
18 #include "libmesh/nanoflann.hpp"
19 
20 KDTree::KDTree(std::vector<Point> & master_points, unsigned int max_leaf_size)
21  : _point_list_adaptor(master_points),
22  _kd_tree(libmesh_make_unique<KdTreeT>(
23  LIBMESH_DIM, _point_list_adaptor, nanoflann::KDTreeSingleIndexAdaptorParams(max_leaf_size)))
24 {
25  mooseAssert(_kd_tree != nullptr, "KDTree was not properly initalized.");
26 
27  _kd_tree->buildIndex();
28 }
29 
30 void
31 KDTree::neighborSearch(Point & query_point,
32  unsigned int patch_size,
33  std::vector<std::size_t> & return_index)
34 {
35  // The query point has to be converted from a C++ array to a C array because nanoflann library
36  // expects C arrays.
37  const Real query_pt[] = {query_point(0), query_point(1), query_point(2)};
38 
39  return_index.resize(patch_size, std::numeric_limits<std::size_t>::max());
40  std::vector<Real> return_dist_sqr(patch_size, std::numeric_limits<Real>::max());
41 
42  _kd_tree->knnSearch(&query_pt[0], patch_size, &return_index[0], &return_dist_sqr[0]);
43 
44  if (return_dist_sqr[0] == std::numeric_limits<Real>::max() ||
45  return_index[0] == std::numeric_limits<std::size_t>::max())
46  mooseError("Unable to find closest node!");
47 }
KDTree(std::vector< Point > &master_points, unsigned int max_leaf_size)
Definition: KDTree.C:20
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
void neighborSearch(Point &query_point, unsigned int patch_size, std::vector< std::size_t > &return_index)
Definition: KDTree.C:31
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