LCOV - code coverage report
Current view: top level - include/utils - PointListAdaptor.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 13 13 100.0 %
Date: 2025-07-17 01:28:37 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

          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             : #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>
      19             : class PointListAdaptor
      20             : {
      21             : public:
      22             :   using Iterator = typename std::vector<PointObject>::const_iterator;
      23             : 
      24       68839 :   PointListAdaptor(Iterator begin, Iterator end)
      25       68839 :     : _begin(begin), _end(end), _size(std::distance(begin, end))
      26             :   {
      27       68839 :   }
      28             : 
      29             : private:
      30             :   /// begin iterator of the underlying point type vector
      31             :   const Iterator _begin;
      32             : 
      33             :   /// end iterator of the underlying point type vector
      34             :   const Iterator _end;
      35             : 
      36             :   /// number of elements pointed to
      37             :   std::size_t _size;
      38             : 
      39             : public:
      40             :   /**
      41             :    * libMesh \p Point coordinate type
      42             :    */
      43             :   using coord_t = Real;
      44             : 
      45             :   /**
      46             :    * Must return the number of data points
      47             :    */
      48     6722088 :   inline size_t kdtree_get_point_count() const { return _size; }
      49             : 
      50             :   /**
      51             :    * get a Point reference from the PointData object at index idx in the list
      52             :    */
      53             :   const Point & getPoint(const PointObject & item) const;
      54             : 
      55             :   /**
      56             :    * Returns the distance between the vector "p1[0:size-1]"
      57             :    * and the data point with index "idx_p2" stored in the class
      58             :    */
      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 (const auto i : make_range(Moose::dim))
      73             :       dist += Utility::pow<2>(p1[i] - p2(i));
      74             : 
      75             :     return dist;
      76             :   }
      77             : 
      78             :   /**
      79             :    * Returns the dim'th component of the idx'th point in the class
      80             :    */
      81   426140600 :   inline coord_t kdtree_get_pt(const size_t idx, int dim) const
      82             :   {
      83             :     mooseAssert(dim < (int)Moose::dim,
      84             :                 "The required component number should be less than the LIBMESH_DIM.");
      85             :     mooseAssert(idx < _size,
      86             :                 "The index of the point should be less"
      87             :                 "than total number of points used to"
      88             :                 "construct the KDTree.");
      89             : 
      90   426140600 :     auto it = _begin;
      91   426140600 :     std::advance(it, idx);
      92   426140600 :     const Point & p = getPoint(*it);
      93             : 
      94   426140600 :     return p(dim);
      95             :   }
      96             : 
      97             :   /**
      98             :    * Optional bounding-box computation. This function is called by the nanoflann library.
      99             :    * If the return value is false, the standard bbox computation loop in the nanoflann
     100             :    * library is activated.
     101             :    */
     102             :   template <class BBOX>
     103      134896 :   bool kdtree_get_bbox(BBOX & /* bb */) const
     104             :   {
     105      134896 :     return false;
     106             :   }
     107             : };
     108             : 
     109             : // Specialization for PointListAdaptor<Point> (provide your own for custom types)
     110             : template <>
     111             : inline const Point &
     112   397497392 : PointListAdaptor<Point>::getPoint(const Point & item) const
     113             : {
     114   397497392 :   return item;
     115             : }

Generated by: LCOV version 1.14