www.mooseframework.org
KDTree.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "KDTree.h"
11 #include "MooseError.h"
12 
13 #include "libmesh/nanoflann.hpp"
14 #include "libmesh/point.h"
15 
16 KDTree::KDTree(std::vector<Point> & master_points, unsigned int max_leaf_size)
17  : _point_list_adaptor(master_points.begin(), master_points.end()),
18  _kd_tree(libmesh_make_unique<KdTreeT>(
19  LIBMESH_DIM, _point_list_adaptor, nanoflann::KDTreeSingleIndexAdaptorParams(max_leaf_size)))
20 {
21  mooseAssert(_kd_tree != nullptr, "KDTree was not properly initalized.");
22 
23  _kd_tree->buildIndex();
24 }
25 
26 void
27 KDTree::neighborSearch(Point & query_point,
28  unsigned int patch_size,
29  std::vector<std::size_t> & return_index)
30 {
31  std::vector<Real> return_dist_sqr(patch_size);
32  neighborSearch(query_point, patch_size, return_index, return_dist_sqr);
33 }
34 
35 void
36 KDTree::neighborSearch(Point & query_point,
37  unsigned int patch_size,
38  std::vector<std::size_t> & return_index,
39  std::vector<Real> & return_dist_sqr)
40 {
41  return_index.resize(patch_size);
42 
43  std::size_t n_result =
44  _kd_tree->knnSearch(&query_point(0), patch_size, return_index.data(), return_dist_sqr.data());
45 
46  if (n_result == 0)
47  mooseError("Unable to find closest node!");
48 
49  return_index.resize(n_result);
50  return_dist_sqr.resize(n_result);
51 }
52 
53 void
54 KDTree::radiusSearch(Point & query_point,
55  Real radius,
56  std::vector<std::pair<std::size_t, Real>> & indices_dist)
57 {
58  nanoflann::SearchParams sp;
59  _kd_tree->radiusSearch(&query_point(0), radius * radius, indices_dist, sp);
60 }
KDTree(std::vector< Point > &master_points, unsigned int max_leaf_size)
Definition: KDTree.C:16
std::unique_ptr< KdTreeT > _kd_tree
Definition: KDTree.h:46
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
void neighborSearch(Point &query_point, unsigned int patch_size, std::vector< std::size_t > &return_index)
Definition: KDTree.C:27
void radiusSearch(Point &query_point, Real radius, std::vector< std::pair< std::size_t, Real >> &indices_dist)
Definition: KDTree.C:54
nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< Real, PointListAdaptor< Point > >, PointListAdaptor< Point >, LIBMESH_DIM > KdTreeT
Definition: KDTree.h:42