Rips_complex.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(s): ClĂ©ment Maria, Pawel Dlotko, Vincent Rouvreau
4  *
5  * Copyright (C) 2016 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
11 #ifndef RIPS_COMPLEX_H_
12 #define RIPS_COMPLEX_H_
13 
14 #include <gudhi/Debug_utils.h>
16 
17 #include <boost/graph/adjacency_list.hpp>
18 
19 #include <iostream>
20 #include <vector>
21 #include <map>
22 #include <string>
23 #include <limits> // for numeric_limits
24 #include <utility> // for pair<>
25 
26 
27 namespace Gudhi {
28 
29 namespace rips_complex {
30 
44 template<typename Filtration_value>
45 class Rips_complex {
46  public:
50  typedef typename boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS
51  , boost::property < vertex_filtration_t, Filtration_value >
52  , boost::property < edge_filtration_t, Filtration_value >> OneSkeletonGraph;
53 
54  private:
55  typedef int Vertex_handle;
56 
57  public:
72  template<typename ForwardPointRange, typename Distance >
73  Rips_complex(const ForwardPointRange& points, Filtration_value threshold, Distance distance) {
74  compute_proximity_graph(points, threshold, distance);
75  }
76 
87  template<typename DistanceMatrix>
88  Rips_complex(const DistanceMatrix& distance_matrix, Filtration_value threshold) {
89  compute_proximity_graph(boost::irange((size_t)0, distance_matrix.size()), threshold,
90  [&](size_t i, size_t j){return distance_matrix[j][i];});
91  }
92 
103  template <typename SimplicialComplexForRips>
104  void create_complex(SimplicialComplexForRips& complex, int dim_max) {
105  GUDHI_CHECK(complex.num_vertices() == 0,
106  std::invalid_argument("Rips_complex::create_complex - simplicial complex is not empty"));
107 
108  // insert the proximity graph in the simplicial complex
109  complex.insert_graph(rips_skeleton_graph_);
110  // expand the graph until dimension dim_max
111  complex.expansion(dim_max);
112  }
113 
114  private:
126  template< typename ForwardPointRange, typename Distance >
127  void compute_proximity_graph(const ForwardPointRange& points, Filtration_value threshold,
128  Distance distance) {
129  std::vector< std::pair< Vertex_handle, Vertex_handle > > edges;
130  std::vector< Filtration_value > edges_fil;
131 
132  // Compute the proximity graph of the points.
133  // If points contains n elements, the proximity graph is the graph with n vertices, and an edge [u,v] iff the
134  // distance function between points u and v is smaller than threshold.
135  // --------------------------------------------------------------------------------------------
136  // Creates the vector of edges and its filtration values (returned by distance function)
137  Vertex_handle idx_u = 0;
138  for (auto it_u = std::begin(points); it_u != std::end(points); ++it_u, ++idx_u) {
139  Vertex_handle idx_v = idx_u + 1;
140  for (auto it_v = it_u + 1; it_v != std::end(points); ++it_v, ++idx_v) {
141  Filtration_value fil = distance(*it_u, *it_v);
142  if (fil <= threshold) {
143  edges.emplace_back(idx_u, idx_v);
144  edges_fil.push_back(fil);
145  }
146  }
147  }
148 
149  // --------------------------------------------------------------------------------------------
150  // Creates the proximity graph from edges and sets the property with the filtration value.
151  // Number of points is labeled from 0 to idx_u-1
152  // --------------------------------------------------------------------------------------------
153  // Do not use : rips_skeleton_graph_ = OneSkeletonGraph(...) -> deep copy of the graph (boost graph is not
154  // move-enabled)
155  rips_skeleton_graph_.~OneSkeletonGraph();
156  new(&rips_skeleton_graph_)OneSkeletonGraph(edges.begin(), edges.end(), edges_fil.begin(), idx_u);
157 
158  auto vertex_prop = boost::get(vertex_filtration_t(), rips_skeleton_graph_);
159 
160  using vertex_iterator = typename boost::graph_traits<OneSkeletonGraph>::vertex_iterator;
161  vertex_iterator vi, vi_end;
162  for (std::tie(vi, vi_end) = boost::vertices(rips_skeleton_graph_);
163  vi != vi_end; ++vi) {
164  boost::put(vertex_prop, *vi, 0.);
165  }
166  }
167 
168  private:
169  OneSkeletonGraph rips_skeleton_graph_;
170 };
171 
172 } // namespace rips_complex
173 
174 } // namespace Gudhi
175 
176 #endif // RIPS_COMPLEX_H_
Rips complex data structure.
Definition: Rips_complex.h:45
Rips_complex(const ForwardPointRange &points, Filtration_value threshold, Distance distance)
Rips_complex constructor from a list of points.
Definition: Rips_complex.h:73
boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, boost::property< vertex_filtration_t, Filtration_value >, boost::property< edge_filtration_t, Filtration_value > > OneSkeletonGraph
Type of the one skeleton graph stored inside the Rips complex structure.
Definition: Rips_complex.h:52
void create_complex(SimplicialComplexForRips &complex, int dim_max)
Initializes the simplicial complex from the Rips graph and expands it until a given maximal dimension...
Definition: Rips_complex.h:104
Rips_complex(const DistanceMatrix &distance_matrix, Filtration_value threshold)
Rips_complex constructor from a distance matrix.
Definition: Rips_complex.h:88
Graph simplicial complex methods.
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:20
The concept SimplicialComplexForRips describes the requirements for a type to implement a simplicial ...
Definition: SimplicialComplexForRips.h:21
std::size_t num_vertices()
Returns the number of vertices in the simplicial complex.
void expansion(int max_dim)
Expands the simplicial complex containing only its one skeleton until a given maximal dimension as ex...
void insert_graph(const OneSkeletonGraph &skel_graph)
Inserts a given Gudhi::rips_complex::Rips_complex::OneSkeletonGraph in the simplicial complex.
Handle type for the vertices of a cell complex.
Definition: VertexHandle.h:15