Rips_complex.h
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, Pawel Dlotko, Vincent Rouvreau
6  *
7  * Copyright (C) 2016 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 RIPS_COMPLEX_H_
24 #define RIPS_COMPLEX_H_
25 
26 #include <gudhi/Debug_utils.h>
27 #include <gudhi/graph_simplicial_complex.h>
28 
29 #include <boost/graph/adjacency_list.hpp>
30 
31 #include <iostream>
32 #include <vector>
33 #include <map>
34 #include <string>
35 #include <limits> // for numeric_limits
36 #include <utility> // for pair<>
37 
38 
39 namespace Gudhi {
40 
41 namespace rips_complex {
42 
56 template<typename Filtration_value>
57 class Rips_complex {
58  public:
62  typedef typename boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS
63  , boost::property < vertex_filtration_t, Filtration_value >
64  , boost::property < edge_filtration_t, Filtration_value >> OneSkeletonGraph;
65 
66  private:
67  typedef int Vertex_handle;
68 
69  public:
82  template<typename ForwardPointRange, typename Distance >
83  Rips_complex(const ForwardPointRange& points, Filtration_value threshold, Distance distance) {
84  compute_proximity_graph(points, threshold, distance);
85  }
86 
96  template<typename DistanceMatrix>
97  Rips_complex(const DistanceMatrix& distance_matrix, Filtration_value threshold) {
98  compute_proximity_graph(boost::irange((size_t)0, distance_matrix.size()), threshold,
99  [&](size_t i, size_t j){return distance_matrix[j][i];});
100  }
101 
112  template <typename SimplicialComplexForRips>
113  void create_complex(SimplicialComplexForRips& complex, int dim_max) {
114  GUDHI_CHECK(complex.num_vertices() == 0,
115  std::invalid_argument("Rips_complex::create_complex - simplicial complex is not empty"));
116 
117  // insert the proximity graph in the simplicial complex
118  complex.insert_graph(rips_skeleton_graph_);
119  // expand the graph until dimension dim_max
120  complex.expansion(dim_max);
121  }
122 
123  private:
135  template< typename ForwardPointRange, typename Distance >
136  void compute_proximity_graph(const ForwardPointRange& points, Filtration_value threshold,
137  Distance distance) {
138  std::vector< std::pair< Vertex_handle, Vertex_handle > > edges;
139  std::vector< Filtration_value > edges_fil;
140 
141  // Compute the proximity graph of the points.
142  // If points contains n elements, the proximity graph is the graph with n vertices, and an edge [u,v] iff the
143  // distance function between points u and v is smaller than threshold.
144  // --------------------------------------------------------------------------------------------
145  // Creates the vector of edges and its filtration values (returned by distance function)
146  Vertex_handle idx_u = 0;
147  for (auto it_u = std::begin(points); it_u != std::end(points); ++it_u, ++idx_u) {
148  Vertex_handle idx_v = idx_u + 1;
149  for (auto it_v = it_u + 1; it_v != std::end(points); ++it_v, ++idx_v) {
150  Filtration_value fil = distance(*it_u, *it_v);
151  if (fil <= threshold) {
152  edges.emplace_back(idx_u, idx_v);
153  edges_fil.push_back(fil);
154  }
155  }
156  }
157 
158  // --------------------------------------------------------------------------------------------
159  // Creates the proximity graph from edges and sets the property with the filtration value.
160  // Number of points is labeled from 0 to idx_u-1
161  // --------------------------------------------------------------------------------------------
162  // Do not use : rips_skeleton_graph_ = OneSkeletonGraph(...) -> deep copy of the graph (boost graph is not
163  // move-enabled)
164  rips_skeleton_graph_.~OneSkeletonGraph();
165  new(&rips_skeleton_graph_)OneSkeletonGraph(edges.begin(), edges.end(), edges_fil.begin(), idx_u);
166 
167  auto vertex_prop = boost::get(vertex_filtration_t(), rips_skeleton_graph_);
168 
169  using vertex_iterator = typename boost::graph_traits<OneSkeletonGraph>::vertex_iterator;
170  vertex_iterator vi, vi_end;
171  for (std::tie(vi, vi_end) = boost::vertices(rips_skeleton_graph_);
172  vi != vi_end; ++vi) {
173  boost::put(vertex_prop, *vi, 0.);
174  }
175  }
176 
177  private:
178  OneSkeletonGraph rips_skeleton_graph_;
179 };
180 
181 } // namespace rips_complex
182 
183 } // namespace Gudhi
184 
185 #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:113
Rips_complex(const DistanceMatrix &distance_matrix, Filtration_value threshold)
Rips_complex constructor from a distance matrix.
Definition: Rips_complex.h:97
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:33
Definition: SimplicialComplexForAlpha.h:26
Rips complex data structure.
Definition: Rips_complex.h:57
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:32
boost::adjacency_list< boost::vecS, boost::vecS, boost::undirectedS, 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:64
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:83
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