www.mooseframework.org
PointListAdaptor.h
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 #pragma once
11 
12 #include "libmesh/nanoflann.hpp"
13 #include "libmesh/utility.h"
14 #include "libmesh/point.h"
15 
16 #include <iterator>
17 
18 template <typename PointObject>
20 {
21 public:
22  using Iterator = typename std::vector<PointObject>::const_iterator;
23 
25  : _begin(begin), _end(end), _size(std::distance(begin, end))
26  {
27  }
28 
29 private:
32 
34  const Iterator _end;
35 
37  std::size_t _size;
38 
39 public:
43  using coord_t = Real;
44 
48  inline size_t kdtree_get_point_count() const { return _size; }
49 
53  const Point & getPoint(const PointObject & item) const;
54 
59  inline coord_t kdtree_distance(const coord_t * p1, const size_t idx_p2, size_t /*size*/) const
60  {
61  mooseAssert(idx_p2 < _size,
62  "The point index should be less than"
63  "total number of points used to build"
64  "the KDTree.");
65 
66  auto it = _begin;
67  std::advance(it, idx_p2);
68  const Point & p2 = getPoint(*it);
69 
70  coord_t dist = 0.0;
71 
72  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
73  dist += Utility::pow<2>(p1[i] - p2(i));
74 
75  return dist;
76  }
77 
84  inline coord_t kdtree_get_pt(const size_t idx, int dim) const
85  {
86  mooseAssert(dim < (int)LIBMESH_DIM,
87  "The required component number should be less than the LIBMESH_DIM.");
88  mooseAssert(idx < _size,
89  "The index of the point should be less"
90  "than total number of points used to"
91  "construct the KDTree.");
92 
93  auto it = _begin;
94  std::advance(it, idx);
95  const Point & p = getPoint(*it);
96 
97  return p(dim);
98  }
99 
105  template <class BBOX>
106  bool kdtree_get_bbox(BBOX & /* bb */) const
107  {
108  return false;
109  }
110 };
111 
112 // Specialization for PointListAdaptor<Point> (provide your own for custom types)
113 template <>
114 inline const Point &
115 PointListAdaptor<Point>::getPoint(const Point & item) const
116 {
117  return item;
118 }
119 
bool kdtree_get_bbox(BBOX &) const
Optional bounding-box computation.
Real coord_t
libMesh Point coordinate type
const Point & getPoint(const PointObject &item) const
get a Point reference from the PointData object at index idx in the list
const Iterator _end
end iterator of the underlying point type vector
coord_t kdtree_get_pt(const size_t idx, int dim) const
Returns the dim&#39;th component of the idx&#39;th point in the class: Since this is inlined and the "dim" ar...
PointListAdaptor(Iterator begin, Iterator end)
const Iterator _begin
begin iterator of the underlying point type vector
std::size_t _size
number of elements pointed to
size_t kdtree_get_point_count() const
Must return the number of data points.
typename std::vector< PointObject >::const_iterator Iterator
coord_t kdtree_distance(const coord_t *p1, const size_t idx_p2, size_t) const
Returns the distance between the vector "p1[0:size-1]" and the data point with index "idx_p2" stored ...