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:
70  template<typename ForwardPointRange, typename Distance >
71  Rips_complex(const ForwardPointRange& points, Filtration_value threshold, Distance distance) {
72  compute_proximity_graph(points, threshold, distance);
73  }
74 
84  template<typename DistanceMatrix>
85  Rips_complex(const DistanceMatrix& distance_matrix, Filtration_value threshold) {
86  compute_proximity_graph(boost::irange((size_t)0, distance_matrix.size()), threshold,
87  [&](size_t i, size_t j){return distance_matrix[j][i];});
88  }
89 
100  template <typename SimplicialComplexForRips>
101  void create_complex(SimplicialComplexForRips& complex, int dim_max) {
102  GUDHI_CHECK(complex.num_vertices() == 0,
103  std::invalid_argument("Rips_complex::create_complex - simplicial complex is not empty"));
104 
105  // insert the proximity graph in the simplicial complex
106  complex.insert_graph(rips_skeleton_graph_);
107  // expand the graph until dimension dim_max
108  complex.expansion(dim_max);
109  }
110 
111  private:
123  template< typename ForwardPointRange, typename Distance >
124  void compute_proximity_graph(const ForwardPointRange& points, Filtration_value threshold,
125  Distance distance) {
126  std::vector< std::pair< Vertex_handle, Vertex_handle > > edges;
127  std::vector< Filtration_value > edges_fil;
128 
129  // Compute the proximity graph of the points.
130  // If points contains n elements, the proximity graph is the graph with n vertices, and an edge [u,v] iff the
131  // distance function between points u and v is smaller than threshold.
132  // --------------------------------------------------------------------------------------------
133  // Creates the vector of edges and its filtration values (returned by distance function)
134  Vertex_handle idx_u = 0;
135  for (auto it_u = std::begin(points); it_u != std::end(points); ++it_u, ++idx_u) {
136  Vertex_handle idx_v = idx_u + 1;
137  for (auto it_v = it_u + 1; it_v != std::end(points); ++it_v, ++idx_v) {
138  Filtration_value fil = distance(*it_u, *it_v);
139  if (fil <= threshold) {
140  edges.emplace_back(idx_u, idx_v);
141  edges_fil.push_back(fil);
142  }
143  }
144  }
145 
146  // --------------------------------------------------------------------------------------------
147  // Creates the proximity graph from edges and sets the property with the filtration value.
148  // Number of points is labeled from 0 to idx_u-1
149  // --------------------------------------------------------------------------------------------
150  // Do not use : rips_skeleton_graph_ = OneSkeletonGraph(...) -> deep copy of the graph (boost graph is not
151  // move-enabled)
152  rips_skeleton_graph_.~OneSkeletonGraph();
153  new(&rips_skeleton_graph_)OneSkeletonGraph(edges.begin(), edges.end(), edges_fil.begin(), idx_u);
154 
155  auto vertex_prop = boost::get(vertex_filtration_t(), rips_skeleton_graph_);
156 
157  using vertex_iterator = typename boost::graph_traits<OneSkeletonGraph>::vertex_iterator;
158  vertex_iterator vi, vi_end;
159  for (std::tie(vi, vi_end) = boost::vertices(rips_skeleton_graph_);
160  vi != vi_end; ++vi) {
161  boost::put(vertex_prop, *vi, 0.);
162  }
163  }
164 
165  private:
166  OneSkeletonGraph rips_skeleton_graph_;
167 };
168 
169 } // namespace rips_complex
170 
171 } // namespace Gudhi
172 
173 #endif // RIPS_COMPLEX_H_
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:101
Rips_complex(const DistanceMatrix &distance_matrix, Filtration_value threshold)
Rips_complex constructor from a distance matrix.
Definition: Rips_complex.h:85
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...
The concept SimplicialComplexForRips describes the requirements for a type to implement a simplicial ...
Definition: SimplicialComplexForRips.h:21
Definition: SimplicialComplexForAlpha.h:14
Rips complex data structure.
Definition: Rips_complex.h:45
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:20
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
std::size_t num_vertices()
Returns the number of vertices in the simplicial complex.
Rips_complex(const ForwardPointRange &points, Filtration_value threshold, Distance distance)
Rips_complex constructor from a list of points.
Definition: Rips_complex.h:71
Graph simplicial complex methods.
GUDHI  Version 3.3.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Tue Aug 11 2020 11:09:13 for GUDHI by Doxygen 1.8.13