summaryrefslogtreecommitdiff
path: root/src/Cech_complex/include/gudhi/Cech_complex.h
blob: e847c97f1d8b27b67c592a59612ee05cde178f87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*    This file is part of the Gudhi Library. The Gudhi library
 *    (Geometric Understanding in Higher Dimensions) is a generic C++
 *    library for computational topology.
 *
 *    Author(s):       Vincent Rouvreau
 *
 *    Copyright (C) 2018 Inria
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef CECH_COMPLEX_H_
#define CECH_COMPLEX_H_

#include <gudhi/graph_simplicial_complex.h>  // for Gudhi::Proximity_graph
#include <gudhi/Debug_utils.h>               // for GUDHI_CHECK
#include <gudhi/Cech_complex_blocker.h>      // for Gudhi::cech_complex::Cech_blocker

#include <iostream>
#include <cstddef>    // for std::size_t
#include <stdexcept>  // for exception management

namespace Gudhi {

namespace cech_complex {

/**
 * \class Cech_complex
 * \brief Cech complex data structure.
 * 
 * \ingroup cech_complex
 * 
 * \details
 * The data structure is a proximity graph, containing edges when the edge length is less or equal
 * to a given threshold. Edge length is computed from a user given point cloud with a given distance function.
 *
 * \tparam SimplicialComplexForProximityGraph furnishes `Vertex_handle` and `Filtration_value` type definition required
 * by `Gudhi::Proximity_graph`.
 *
 * \tparam ForwardPointRange furnishes `.size()`, `.begin()` and `.end()` methods, and a `const_iterator` type
 * definition.
 */
template<typename SimplicialComplexForProximityGraph, typename ForwardPointRange>
class Cech_complex {
 private:
  using Vertex_handle = typename SimplicialComplexForProximityGraph::Vertex_handle;
  using Filtration_value = typename SimplicialComplexForProximityGraph::Filtration_value;
  using Proximity_graph = Gudhi::Proximity_graph<SimplicialComplexForProximityGraph>;

 public:
  /** \brief Cech_complex constructor from a list of points.
   *
   * @param[in] points Range of points.
   * @param[in] threshold Rips value.
   * @param[in] distance distance function that returns a `Filtration_value` from 2 given points.
   *
   * \tparam ForwardPointRange must be a range for which `.size()`, `.begin()` and `.end()` methods return input
   * iterators on a point. `.begin()` and `.end()` methods are required for a point.
   *
   * \tparam Distance furnishes `operator()(const Point& p1, const Point& p2)`, where
   * `Point` is a point from the `ForwardPointRange`, and that returns a `Filtration_value`.
   */
  template<typename Distance >
  Cech_complex(const ForwardPointRange& points, Filtration_value threshold, Distance distance)
    : threshold_(threshold),
      point_cloud_(points) {
    dimension_ = points.begin()->end() - points.begin()->begin();
    cech_skeleton_graph_ = Gudhi::compute_proximity_graph<SimplicialComplexForProximityGraph>(point_cloud_,
                                                                                              threshold_,
                                                                                              distance);
  }

  /** \brief Initializes the simplicial complex from the proximity graph and expands it until a given maximal
   * dimension, using the Cech blocker oracle.
   *
   * @param[in] complex SimplicialComplexForCech to be created.
   * @param[in] dim_max graph expansion until this given maximal dimension.
   * @exception std::invalid_argument In debug mode, if `complex.num_vertices()` does not return 0.
   *
   */
  template<typename SimplicialComplexForCechComplex >
  void create_complex(SimplicialComplexForCechComplex& complex, int dim_max) {
    GUDHI_CHECK(complex.num_vertices() == 0,
                std::invalid_argument("Cech_complex::create_complex - simplicial complex is not empty"));

    // insert the proximity graph in the simplicial complex
    complex.insert_graph(cech_skeleton_graph_);
    // expand the graph until dimension dim_max
    complex.expansion_with_blockers(dim_max,
                                    Cech_blocker<SimplicialComplexForCechComplex, ForwardPointRange>(complex, this));
  }

  /** @return Threshold value given at construction. */
  Filtration_value threshold() const {
    return threshold_;
  }

  /** @return Dimension value given at construction by the first point dimension. */
  std::size_t dimension() const {
    return dimension_;
  }

  /** @param[in] vertex Point position in the range.
   * @return A const iterator on the point.
   * @exception std::out_of_range In debug mode, if point position in the range is out.
   */
  typename ForwardPointRange::const_iterator point_iterator(std::size_t vertex) const {
    GUDHI_CHECK((point_cloud_.begin() + vertex) < point_cloud_.end(),
                std::out_of_range("Cech_complex::point - simplicial complex is not empty"));
    return (point_cloud_.begin() + vertex);
  }

 private:
  Proximity_graph cech_skeleton_graph_;
  Filtration_value threshold_;
  ForwardPointRange point_cloud_;
  std::size_t dimension_;
};

}  // namespace cech_complex

}  // namespace Gudhi

#endif  // CECH_COMPLEX_H_