distance_functions.h
Go to the documentation of this file.
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): ClĂ©ment Maria
6  *
7  * Copyright (C) 2014 INRIA
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef DISTANCE_FUNCTIONS_H_
24 #define DISTANCE_FUNCTIONS_H_
25 
26 #include <gudhi/Debug_utils.h>
27 
28 #include <boost/range/metafunctions.hpp>
29 
30 #include <cmath> // for std::sqrt
31 #include <type_traits> // for std::decay
32 #include <iterator> // for std::begin, std::end
33 #include <utility>
34 
35 namespace Gudhi {
36 
44  public:
45  // boost::range_value is not SFINAE-friendly so we cannot use it in the return type
46  template< typename Point >
47  typename std::iterator_traits<typename boost::range_iterator<Point>::type>::value_type
48  operator()(const Point& p1, const Point& p2) const {
49  auto it1 = std::begin(p1);
50  auto it2 = std::begin(p2);
51  typedef typename boost::range_value<Point>::type NT;
52  NT dist = 0;
53  for (; it1 != std::end(p1); ++it1, ++it2) {
54  GUDHI_CHECK(it2 != std::end(p2), "inconsistent point dimensions");
55  NT tmp = *it1 - *it2;
56  dist += tmp*tmp;
57  }
58  GUDHI_CHECK(it2 == std::end(p2), "inconsistent point dimensions");
59  using std::sqrt;
60  return sqrt(dist);
61  }
62  template< typename T >
63  T operator() (const std::pair< T, T >& f, const std::pair< T, T >& s) const {
64  T dx = f.first - s.first;
65  T dy = f.second - s.second;
66  using std::sqrt;
67  return sqrt(dx*dx + dy*dy);
68  }
69 };
70 
71 } // namespace Gudhi
72 
73 #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:43
Definition: SimplicialComplexForAlpha.h:26
GUDHI  Version 2.1.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Wed Jan 31 2018 09:40:55 for GUDHI by Doxygen 1.8.11