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 <gudhi/Miniball.hpp>
29 
30 #include <boost/range/metafunctions.hpp>
31 #include <boost/range/size.hpp>
32 
33 #include <cmath> // for std::sqrt
34 #include <type_traits> // for std::decay
35 #include <iterator> // for std::begin, std::end
36 #include <utility>
37 
38 namespace Gudhi {
39 
47  public:
48  // boost::range_value is not SFINAE-friendly so we cannot use it in the return type
49  template< typename Point >
50  typename std::iterator_traits<typename boost::range_iterator<Point>::type>::value_type
51  operator()(const Point& p1, const Point& p2) const {
52  auto it1 = std::begin(p1);
53  auto it2 = std::begin(p2);
54  typedef typename boost::range_value<Point>::type NT;
55  NT dist = 0;
56  for (; it1 != std::end(p1); ++it1, ++it2) {
57  GUDHI_CHECK(it2 != std::end(p2), "inconsistent point dimensions");
58  NT tmp = *it1 - *it2;
59  dist += tmp*tmp;
60  }
61  GUDHI_CHECK(it2 == std::end(p2), "inconsistent point dimensions");
62  using std::sqrt;
63  return sqrt(dist);
64  }
65  template< typename T >
66  T operator() (const std::pair< T, T >& f, const std::pair< T, T >& s) const {
67  T dx = f.first - s.first;
68  T dy = f.second - s.second;
69  using std::sqrt;
70  return sqrt(dx*dx + dy*dy);
71  }
72 };
73 
77  public:
87  template< typename Point >
88  typename std::iterator_traits<typename boost::range_iterator<Point>::type>::value_type
89  operator()(const Point& point_1, const Point& point_2) const {
90  return Euclidean_distance()(point_1, point_2) / 2.;
91  }
101  template< typename Point_cloud,
102  typename Point_iterator = typename boost::range_const_iterator<Point_cloud>::type,
103  typename Point = typename std::iterator_traits<Point_iterator>::value_type,
104  typename Coordinate_iterator = typename boost::range_const_iterator<Point>::type,
105  typename Coordinate = typename std::iterator_traits<Coordinate_iterator>::value_type>
106  Coordinate
107  operator()(const Point_cloud& point_cloud) const {
108  using Min_sphere = Miniball::Miniball<Miniball::CoordAccessor<Point_iterator, Coordinate_iterator>>;
109 
110  Min_sphere ms(boost::size(*point_cloud.begin()), point_cloud.begin(), point_cloud.end());
111 #ifdef DEBUG_TRACES
112  std::cout << "Minimal_enclosing_ball_radius = " << std::sqrt(ms.squared_radius()) << " | nb points = "
113  << boost::size(point_cloud) << " | dimension = "
114  << boost::size(*point_cloud.begin()) << std::endl;
115 #endif // DEBUG_TRACES
116 
117  return std::sqrt(ms.squared_radius());
118  }
119 };
120 
121 } // namespace Gudhi
122 
123 #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:46
Definition: SimplicialComplexForAlpha.h:26
Compute the radius of the minimal enclosing ball between Points given by a range of coordinates...
Definition: distance_functions.h:76
Coordinate operator()(const Point_cloud &point_cloud) const
Minimal_enclosing_ball_radius from a point cloud.
Definition: distance_functions.h:107
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:89
GUDHI  Version 2.3.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Tue Sep 4 2018 14:32:59 for GUDHI by Doxygen 1.8.13