Line data Source code
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 18 : namespace nanoflann 19 : { 20 : typedef SearchParams SearchParameters; 21 : 22 : template <typename T, typename U> 23 : using ResultItem = std::pair<T, U>; 24 : } 25 : #endif 26 : 27 68674 : KDTree::KDTree(const std::vector<Point> & master_points, unsigned int max_leaf_size) 28 68674 : : _point_list_adaptor(master_points.begin(), master_points.end()), 29 68674 : _kd_tree(std::make_unique<KdTreeT>( 30 137348 : LIBMESH_DIM, _point_list_adaptor, nanoflann::KDTreeSingleIndexAdaptorParams(max_leaf_size))) 31 : { 32 : mooseAssert(_kd_tree != nullptr, "KDTree was not properly initialized."); 33 : 34 68674 : _kd_tree->buildIndex(); 35 68674 : } 36 : 37 : void 38 414220 : KDTree::neighborSearch(const Point & query_point, 39 : unsigned int patch_size, 40 : std::vector<std::size_t> & return_index) 41 : { 42 414220 : std::vector<Real> return_dist_sqr(patch_size); 43 414220 : neighborSearch(query_point, patch_size, return_index, return_dist_sqr); 44 414220 : } 45 : 46 : void 47 6641816 : KDTree::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 6641816 : return_index.resize(patch_size); 53 : 54 : std::size_t n_result = 55 6641816 : _kd_tree->knnSearch(&query_point(0), patch_size, return_index.data(), return_dist_sqr.data()); 56 : 57 6641816 : if (n_result == 0) 58 0 : mooseError("Unable to find closest node!"); 59 : 60 6641816 : return_index.resize(n_result); 61 6641816 : return_dist_sqr.resize(n_result); 62 6641816 : } 63 : 64 : void 65 3 : KDTree::radiusSearch(const Point & query_point, 66 : Real radius, 67 : std::vector<nanoflann::ResultItem<std::size_t, Real>> & indices_dist) 68 : { 69 3 : nanoflann::SearchParameters sp; 70 3 : _kd_tree->radiusSearch(&query_point(0), radius * radius, indices_dist, sp); 71 3 : } 72 : 73 : std::size_t 74 6242997 : KDTree::numberCandidatePoints() 75 : { 76 6242997 : return _point_list_adaptor.kdtree_get_point_count(); 77 : }