distance_functions.h
Go to the documentation of this file.
1 /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2  * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3  * Author(s): ClĂ©ment Maria
4  *
5  * Copyright (C) 2014 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
11 #ifndef DISTANCE_FUNCTIONS_H_
12 #define DISTANCE_FUNCTIONS_H_
13 
14 #include <gudhi/Debug_utils.h>
15 
16 #include <gudhi/Miniball.hpp>
17 
18 #include <boost/range/metafunctions.hpp>
19 #include <boost/range/size.hpp>
20 
21 #include <cmath> // for std::sqrt
22 #include <type_traits> // for std::decay
23 #include <iterator> // for std::begin, std::end
24 #include <utility>
25 
26 namespace Gudhi {
27 
35  public:
36  // boost::range_value is not SFINAE-friendly so we cannot use it in the return type
37  template< typename Point >
38  typename std::iterator_traits<typename boost::range_iterator<Point>::type>::value_type
39  operator()(const Point& p1, const Point& p2) const {
40  auto it1 = std::begin(p1);
41  auto it2 = std::begin(p2);
42  typedef typename boost::range_value<Point>::type NT;
43  NT dist = 0;
44  for (; it1 != std::end(p1); ++it1, ++it2) {
45  GUDHI_CHECK(it2 != std::end(p2), "inconsistent point dimensions");
46  NT tmp = *it1 - *it2;
47  dist += tmp*tmp;
48  }
49  GUDHI_CHECK(it2 == std::end(p2), "inconsistent point dimensions");
50  using std::sqrt;
51  return sqrt(dist);
52  }
53  template< typename T >
54  T operator() (const std::pair< T, T >& f, const std::pair< T, T >& s) const {
55  T dx = f.first - s.first;
56  T dy = f.second - s.second;
57  using std::sqrt;
58  return sqrt(dx*dx + dy*dy);
59  }
60 };
61 
65  public:
75  template< typename Point >
76  typename std::iterator_traits<typename boost::range_iterator<Point>::type>::value_type
77  operator()(const Point& point_1, const Point& point_2) const {
78  return Euclidean_distance()(point_1, point_2) / 2.;
79  }
89  template< typename Point_cloud,
90  typename Point_iterator = typename boost::range_const_iterator<Point_cloud>::type,
91  typename Point = typename std::iterator_traits<Point_iterator>::value_type,
92  typename Coordinate_iterator = typename boost::range_const_iterator<Point>::type,
93  typename Coordinate = typename std::iterator_traits<Coordinate_iterator>::value_type>
94  Coordinate
95  operator()(const Point_cloud& point_cloud) const {
96  using Min_sphere = Miniball::Miniball<Miniball::CoordAccessor<Point_iterator, Coordinate_iterator>>;
97 
98  Min_sphere ms(boost::size(*point_cloud.begin()), point_cloud.begin(), point_cloud.end());
99 #ifdef DEBUG_TRACES
100  std::cout << "Minimal_enclosing_ball_radius = " << std::sqrt(ms.squared_radius()) << " | nb points = "
101  << boost::size(point_cloud) << " | dimension = "
102  << boost::size(*point_cloud.begin()) << std::endl;
103 #endif // DEBUG_TRACES
104 
105  return std::sqrt(ms.squared_radius());
106  }
107 };
108 
109 } // namespace Gudhi
110 
111 #endif // DISTANCE_FUNCTIONS_H_
Compute the Euclidean distance between two Points given by a range of coordinates. The points are assumed to have the same dimension.
Definition: distance_functions.h:34
Definition: SimplicialComplexForAlpha.h:14
Compute the radius of the minimal enclosing ball between Points given by a range of coordinates...
Definition: distance_functions.h:64
Coordinate operator()(const Point_cloud &point_cloud) const
Minimal_enclosing_ball_radius from a point cloud.
Definition: distance_functions.h:95
std::iterator_traits< typename boost::range_iterator< Point >::type >::value_type operator()(const Point &point_1, const Point &point_2) const
Minimal_enclosing_ball_radius from two points.
Definition: distance_functions.h:77
GUDHI  Version 3.1.1  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Fri Feb 7 2020 16:35:36 for GUDHI by Doxygen 1.8.13