libMesh
point_locator_nanoflann.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_POINT_LOCATOR_NANOFLANN_H
21 #define LIBMESH_POINT_LOCATOR_NANOFLANN_H
22 
23 #include "libmesh/libmesh_config.h"
24 
25 // This class is not defined unless libmesh is built with Nanoflann support
26 #ifdef LIBMESH_HAVE_NANOFLANN
27 
28 // libmesh includes
29 #include "libmesh/point_locator_base.h"
30 #include "libmesh/point.h"
31 #include "libmesh/bounding_box.h"
32 
33 // contrib includes
34 #include "libmesh/nanoflann.hpp"
35 
36 // C++ includes
37 #include <vector>
38 #include <memory>
39 
40 namespace libMesh
41 {
42 
43 // Forward Declarations
44 class MeshBase;
45 class Point;
46 class Elem;
47 
72 {
73 public:
82  const PointLocatorBase * master = nullptr);
83 
87  virtual ~PointLocatorNanoflann ();
88 
92  virtual void clear() override final;
93 
98  virtual void init() override final;
99 
105  virtual const Elem * operator() (const Point & p,
106  const std::set<subdomain_id_type> * allowed_subdomains = nullptr) const override final;
107 
117  virtual void operator() (const Point & p,
118  std::set<const Elem *> & candidate_elements,
119  const std::set<subdomain_id_type> * allowed_subdomains = nullptr) const override final;
120 
126  virtual void enable_out_of_mesh_mode () override final;
127 
131  virtual void disable_out_of_mesh_mode () override final;
132 
157  std::size_t get_num_results() const;
158  void set_num_results(std::size_t val);
159 
160  //
161  // Required Nanoflann typedefs and APIs
162  //
163 
167  typedef Real coord_t;
168 
172  std::size_t kdtree_get_point_count() const;
173 
178  coord_t kdtree_distance(const coord_t * p1,
179  const std::size_t idx_p2,
180  std::size_t size) const;
181 
185  coord_t kdtree_get_pt(const std::size_t idx, int dim) const;
186 
195  template <class BBOX>
196  bool kdtree_get_bbox(BBOX & /*bb*/) const { return false; }
197 
198 protected:
199 
205 
210  std::size_t _num_results;
211 
222  std::shared_ptr<std::vector<const Elem *>> _elems;
223  std::shared_ptr<std::vector<Point>> _point_cloud;
224 
225  // kd_tree will be initialized during init() and then automatically
226  // cleaned up by the destructor. We always create a LIBMESH_DIM
227  // dimensional Nanoflann object.
228  typedef nanoflann::L2_Simple_Adaptor<Real, PointLocatorNanoflann> adapter_t;
229  typedef nanoflann::KDTreeSingleIndexAdaptor<adapter_t, PointLocatorNanoflann, LIBMESH_DIM> kd_tree_t;
230  std::shared_ptr<kd_tree_t> _kd_tree;
231 
237  mutable std::vector<std::size_t> _ret_index;
238  mutable std::vector<Real> _out_dist_sqr;
239 
244  mutable std::vector<std::size_t> _b;
245 
254  nanoflann::KNNResultSet<Real>
255  kd_tree_find_neighbors(const Point & p,
256  std::size_t num_results) const;
257 };
258 
259 } // namespace libMesh
260 
261 #endif // LIBMESH_HAVE_NANOFLANN
262 #endif // LIBMESH_POINT_LOCATOR_NANOFLANN_H
nanoflann::KNNResultSet< Real > kd_tree_find_neighbors(const Point &p, std::size_t num_results) const
Helper function that wraps the call to the KDTree&#39;s findNeighbors() routine.
nanoflann::KDTreeSingleIndexAdaptor< adapter_t, PointLocatorNanoflann, LIBMESH_DIM > kd_tree_t
std::vector< std::size_t > _b
Vector of indices used by indirect sort.
bool kdtree_get_bbox(BBOX &) const
Optional bounding-box computation: return false to default to a standard bbox computation loop...
coord_t kdtree_distance(const coord_t *p1, const std::size_t idx_p2, std::size_t size) const
Returns the squared distance between the vector (p1[0], p1[1], p1[2]) and the vertex average of Elem ...
bool _out_of_mesh_mode
true if out-of-mesh mode is enabled.
unsigned int dim
This is the base class from which all geometric element types are derived.
Definition: elem.h:94
MeshBase & mesh
virtual void init() override final
Initializes the locator, so that the operator() methods can be used.
std::size_t get_num_results() const
Set/get the number of results returned by each Nanoflann findNeighbors() search.
virtual const Elem * operator()(const Point &p, const std::set< subdomain_id_type > *allowed_subdomains=nullptr) const override final
Locates the element in which the point with global coordinates p is located, optionally restricted to...
The libMesh namespace provides an interface to certain functionality in the library.
This is the MeshBase class.
Definition: mesh_base.h:75
std::size_t _num_results
The number of results returned by Nanoflann when operator() is called.
Real coord_t
Floating point type used for storing coordinates.
virtual ~PointLocatorNanoflann()
Destructor.
This is the base class for point locators.
std::size_t kdtree_get_point_count() const
Must return the number of data points.
std::vector< std::size_t > _ret_index
The operator() functions on PointLocator-derived classes are const, so to avoid re-allocating these r...
PointLocatorNanoflann(const MeshBase &mesh, const PointLocatorBase *master=nullptr)
Constructor.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::shared_ptr< kd_tree_t > _kd_tree
nanoflann::L2_Simple_Adaptor< Real, PointLocatorNanoflann > adapter_t
virtual void disable_out_of_mesh_mode() override final
Disables out-of-mesh mode (default).
coord_t kdtree_get_pt(const std::size_t idx, int dim) const
Returns the dim&#39;th component of the idx&#39;th vertex average.
std::shared_ptr< std::vector< const Elem * > > _elems
Lists of Points and ids which make up the "point cloud" that is to be searched via Nanoflann...
std::shared_ptr< std::vector< Point > > _point_cloud
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39
This is a PointLocator that uses Nanoflann for its implementation.
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)
A useful inline function which replaces the macros used previously.
virtual void enable_out_of_mesh_mode() override final
Enables out-of-mesh mode.
virtual void clear() override final
Restore to PointLocator to a just-constructed state.