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 <boost/range/metafunctions.hpp>
17 #include <boost/range/size.hpp>
18 
19 #include <cmath> // for std::sqrt
20 #include <type_traits> // for std::decay
21 #include <iterator> // for std::begin, std::end
22 #include <utility>
23 
24 namespace Gudhi {
25 
33  public:
34  // boost::range_value is not SFINAE-friendly so we cannot use it in the return type
35  template< typename Point >
36  typename std::iterator_traits<typename boost::range_iterator<Point>::type>::value_type
37  operator()(const Point& p1, const Point& p2) const {
38  auto it1 = std::begin(p1);
39  auto it2 = std::begin(p2);
40  typedef typename boost::range_value<Point>::type NT;
41  NT dist = 0;
42  for (; it1 != std::end(p1); ++it1, ++it2) {
43  GUDHI_CHECK(it2 != std::end(p2), "inconsistent point dimensions");
44  NT tmp = *it1 - *it2;
45  dist += tmp*tmp;
46  }
47  GUDHI_CHECK(it2 == std::end(p2), "inconsistent point dimensions");
48  using std::sqrt;
49  return sqrt(dist);
50  }
51  template< typename T >
52  T operator() (const std::pair< T, T >& f, const std::pair< T, T >& s) const {
53  T dx = f.first - s.first;
54  T dy = f.second - s.second;
55  using std::sqrt;
56  return sqrt(dx*dx + dy*dy);
57  }
58 };
59 
60 } // namespace Gudhi
61 
62 #endif // DISTANCE_FUNCTIONS_H_
Compute the Euclidean distance between two Points given by a range of coordinates....
Definition: distance_functions.h:32
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14