Persistence_graph.h
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: Francois Godi
4  *
5  * Copyright (C) 2015 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
11 #ifndef PERSISTENCE_GRAPH_H_
12 #define PERSISTENCE_GRAPH_H_
13 
14 #include <gudhi/Internal_point.h>
15 
16 #ifdef GUDHI_USE_TBB
17 #include <tbb/parallel_sort.h>
18 #endif
19 
20 #include <vector>
21 #include <algorithm>
22 #include <limits> // for numeric_limits
23 
24 namespace Gudhi {
25 
26 namespace persistence_diagram {
27 
33 class Persistence_graph {
34  public:
36  template<typename Persistence_diagram1, typename Persistence_diagram2>
37  Persistence_graph(const Persistence_diagram1& diag1, const Persistence_diagram2& diag2, double e);
39  bool on_the_u_diagonal(int u_point_index) const;
41  bool on_the_v_diagonal(int v_point_index) const;
43  int corresponding_point_in_u(int v_point_index) const;
45  int corresponding_point_in_v(int u_point_index) const;
47  double distance(int u_point_index, int v_point_index) const;
49  int size() const;
51  double bottleneck_alive() const;
53  std::vector<double> sorted_distances() const;
55  double diameter_bound() const;
57  Internal_point get_u_point(int u_point_index) const;
59  Internal_point get_v_point(int v_point_index) const;
60 
61  private:
62  std::vector<Internal_point> u;
63  std::vector<Internal_point> v;
64  double b_alive;
65 };
66 
67 template<typename Persistence_diagram1, typename Persistence_diagram2>
68 Persistence_graph::Persistence_graph(const Persistence_diagram1 &diag1,
69  const Persistence_diagram2 &diag2, double e)
70  : u(), v(), b_alive(0.) {
71  std::vector<double> u_alive;
72  std::vector<double> v_alive;
73  for (auto it = std::begin(diag1); it != std::end(diag1); ++it) {
74  if (std::get<1>(*it) == std::numeric_limits<double>::infinity())
75  u_alive.push_back(std::get<0>(*it));
76  else if (std::get<1>(*it) - std::get<0>(*it) > e)
77  u.push_back(Internal_point(std::get<0>(*it), std::get<1>(*it), u.size()));
78  }
79  for (auto it = std::begin(diag2); it != std::end(diag2); ++it) {
80  if (std::get<1>(*it) == std::numeric_limits<double>::infinity())
81  v_alive.push_back(std::get<0>(*it));
82  else if (std::get<1>(*it) - std::get<0>(*it) > e)
83  v.push_back(Internal_point(std::get<0>(*it), std::get<1>(*it), v.size()));
84  }
85  if (u.size() < v.size())
86  swap(u, v);
87  std::sort(u_alive.begin(), u_alive.end());
88  std::sort(v_alive.begin(), v_alive.end());
89  if (u_alive.size() != v_alive.size()) {
90  b_alive = std::numeric_limits<double>::infinity();
91  } else {
92  for (auto it_u = u_alive.cbegin(), it_v = v_alive.cbegin(); it_u != u_alive.cend(); ++it_u, ++it_v)
93  b_alive = (std::max)(b_alive, std::fabs(*it_u - *it_v));
94  }
95 }
96 
97 inline bool Persistence_graph::on_the_u_diagonal(int u_point_index) const {
98  return u_point_index >= static_cast<int> (u.size());
99 }
100 
101 inline bool Persistence_graph::on_the_v_diagonal(int v_point_index) const {
102  return v_point_index >= static_cast<int> (v.size());
103 }
104 
105 inline int Persistence_graph::corresponding_point_in_u(int v_point_index) const {
106  return on_the_v_diagonal(v_point_index) ?
107  v_point_index - static_cast<int> (v.size()) : v_point_index + static_cast<int> (u.size());
108 }
109 
110 inline int Persistence_graph::corresponding_point_in_v(int u_point_index) const {
111  return on_the_u_diagonal(u_point_index) ?
112  u_point_index - static_cast<int> (u.size()) : u_point_index + static_cast<int> (v.size());
113 }
114 
115 inline double Persistence_graph::distance(int u_point_index, int v_point_index) const {
116  if (on_the_u_diagonal(u_point_index) && on_the_v_diagonal(v_point_index))
117  return 0.;
118  Internal_point p_u = get_u_point(u_point_index);
119  Internal_point p_v = get_v_point(v_point_index);
120  return (std::max)(std::fabs(p_u.x() - p_v.x()), std::fabs(p_u.y() - p_v.y()));
121 }
122 
123 inline int Persistence_graph::size() const {
124  return static_cast<int> (u.size() + v.size());
125 }
126 
127 inline double Persistence_graph::bottleneck_alive() const {
128  return b_alive;
129 }
130 
131 inline std::vector<double> Persistence_graph::sorted_distances() const {
132  std::vector<double> distances;
133  distances.push_back(0.); // for empty diagrams
134  for (int u_point_index = 0; u_point_index < size(); ++u_point_index) {
135  distances.push_back(distance(u_point_index, corresponding_point_in_v(u_point_index)));
136  for (int v_point_index = 0; v_point_index < size(); ++v_point_index)
137  distances.push_back(distance(u_point_index, v_point_index));
138  }
139 #ifdef GUDHI_USE_TBB
140  tbb::parallel_sort(distances.begin(), distances.end());
141 #else
142  std::sort(distances.begin(), distances.end());
143 #endif
144  return distances;
145 }
146 
147 inline Internal_point Persistence_graph::get_u_point(int u_point_index) const {
148  if (!on_the_u_diagonal(u_point_index))
149  return u.at(u_point_index);
150  Internal_point projector = v.at(corresponding_point_in_v(u_point_index));
151  double m = (projector.x() + projector.y()) / 2.;
152  return Internal_point(m, m, u_point_index);
153 }
154 
155 inline Internal_point Persistence_graph::get_v_point(int v_point_index) const {
156  if (!on_the_v_diagonal(v_point_index))
157  return v.at(v_point_index);
158  Internal_point projector = u.at(corresponding_point_in_u(v_point_index));
159  double m = (projector.x() + projector.y()) / 2.;
160  return Internal_point(m, m, v_point_index);
161 }
162 
163 inline double Persistence_graph::diameter_bound() const {
164  double max = 0.;
165  for (auto it = u.cbegin(); it != u.cend(); it++)
166  max = (std::max)(max, it->y());
167  for (auto it = v.cbegin(); it != v.cend(); it++)
168  max = (std::max)(max, it->y());
169  return max;
170 }
171 
172 } // namespace persistence_diagram
173 
174 } // namespace Gudhi
175 
176 #endif // PERSISTENCE_GRAPH_H_
GUDHI  Version 3.1.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Mon Feb 3 2020 20:32:13 for GUDHI by Doxygen 1.8.16