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 70898 : KDTree::KDTree(const std::vector<Point> & master_points, unsigned int max_leaf_size) 28 70898 : : _point_list_adaptor(master_points.begin(), master_points.end()), 29 70898 : _kd_tree(std::make_unique<KdTreeT>( 30 141796 : LIBMESH_DIM, _point_list_adaptor, nanoflann::KDTreeSingleIndexAdaptorParams(max_leaf_size))) 31 : { 32 : mooseAssert(_kd_tree != nullptr, "KDTree was not properly initialized."); 33 : 34 70898 : _kd_tree->buildIndex(); 35 70898 : } 36 : 37 : void 38 294848 : KDTree::neighborSearch(const Point & query_point, 39 : unsigned int patch_size, 40 : std::vector<std::size_t> & return_index) 41 : { 42 294848 : std::vector<Real> return_dist_sqr(patch_size); 43 294848 : neighborSearch(query_point, patch_size, return_index, return_dist_sqr); 44 294848 : } 45 : 46 : void 47 6859725 : 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 6859725 : return_index.resize(patch_size); 53 : 54 : std::size_t n_result = 55 6859725 : _kd_tree->knnSearch(&query_point(0), patch_size, return_index.data(), return_dist_sqr.data()); 56 : 57 6859725 : if (n_result == 0) 58 0 : mooseError("Unable to find closest node!"); 59 : 60 6859725 : return_index.resize(n_result); 61 6859725 : return_dist_sqr.resize(n_result); 62 6859725 : } 63 : 64 : void 65 287 : KDTree::radiusSearch(const Point & query_point, 66 : Real radius, 67 : std::vector<nanoflann::ResultItem<std::size_t, Real>> & indices_dist) 68 : { 69 287 : nanoflann::SearchParameters sp; 70 287 : _kd_tree->radiusSearch(&query_point(0), radius * radius, indices_dist, sp); 71 287 : } 72 : 73 : std::size_t 74 6574437 : KDTree::numberCandidatePoints() 75 : { 76 6574437 : return _point_list_adaptor.kdtree_get_point_count(); 77 : }