https://mooseframework.inl.gov
Loading...
Searching...
No Matches
KDTree.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
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// Make newer nanoflann API compatible with older nanoflann versions
17#if NANOFLANN_VERSION < 0x150
18namespace nanoflann
19{
20typedef SearchParams SearchParameters;
21
22template <typename T, typename U>
23using ResultItem = std::pair<T, U>;
24}
25#endif
26
27KDTree::KDTree(const std::vector<Point> & master_points, unsigned int max_leaf_size)
28 : _point_list_adaptor(master_points.begin(), master_points.end()),
29 _kd_tree(std::make_unique<KdTreeT>(
30 LIBMESH_DIM, _point_list_adaptor, nanoflann::KDTreeSingleIndexAdaptorParams(max_leaf_size)))
31{
32 mooseAssert(_kd_tree != nullptr, "KDTree was not properly initialized.");
33
34 _kd_tree->buildIndex();
35}
36
37void
38KDTree::neighborSearch(const Point & query_point,
39 unsigned int patch_size,
40 std::vector<std::size_t> & return_index)
41{
42 std::vector<Real> return_dist_sqr(patch_size);
43 neighborSearch(query_point, patch_size, return_index, return_dist_sqr);
44}
45
46void
47KDTree::neighborSearch(const Point & query_point,
48 unsigned int patch_size,
49 std::vector<std::size_t> & return_index,
50 std::vector<Real> & return_dist_sqr)
51{
52 return_index.resize(patch_size);
53
54 std::size_t n_result =
55 _kd_tree->knnSearch(&query_point(0), patch_size, return_index.data(), return_dist_sqr.data());
56
57 if (n_result == 0)
58 mooseError("Unable to find closest node!");
59
60 return_index.resize(n_result);
61 return_dist_sqr.resize(n_result);
62}
63
64void
65KDTree::radiusSearch(const Point & query_point,
66 Real radius,
67 std::vector<nanoflann::ResultItem<std::size_t, Real>> & indices_dist)
68{
70 _kd_tree->radiusSearch(&query_point(0), radius * radius, indices_dist, sp);
71}
72
73std::size_t
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< Real, PointListAdaptor< Point >, Real, std::size_t >, PointListAdaptor< Point >, LIBMESH_DIM, std::size_t > KdTreeT
Definition KDTree.h:57
KDTree(const std::vector< Point > &master_points, unsigned int max_leaf_size)
Definition KDTree.C:27
std::size_t numberCandidatePoints()
Definition KDTree.C:74
std::unique_ptr< KdTreeT > _kd_tree
Definition KDTree.h:61
void radiusSearch(const Point &query_point, Real radius, std::vector< nanoflann::ResultItem< std::size_t, Real > > &indices_dist)
Definition KDTree.C:65
void neighborSearch(const Point &query_point, unsigned int patch_size, std::vector< std::size_t > &return_index)
Definition KDTree.C:38
PointListAdaptor< Point > _point_list_adaptor
Definition KDTree.h:60
size_t kdtree_get_point_count() const
Must return the number of data points.
std::pair< T, U > ResultItem
Definition KDTree.h:24
SearchParams SearchParameters
const Real radius