Strong_witness_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): Siargey Kachanovich
6  *
7  * Copyright (C) 2015 INRIA (France)
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 STRONG_WITNESS_COMPLEX_H_
24 #define STRONG_WITNESS_COMPLEX_H_
25 
26 #include <gudhi/Active_witness/Active_witness.h>
27 
28 #include <utility>
29 #include <vector>
30 #include <list>
31 #include <limits>
32 
33 namespace Gudhi {
34 
35 namespace witness_complex {
36 
48 template< class Nearest_landmark_table_ >
50  private:
51  typedef typename Nearest_landmark_table_::value_type Nearest_landmark_range;
52  typedef std::size_t Witness_id;
53  typedef std::size_t Landmark_id;
54  typedef std::pair<Landmark_id, double> Id_distance_pair;
55  typedef Active_witness<Id_distance_pair, Nearest_landmark_range> ActiveWitness;
56  typedef std::list< ActiveWitness > ActiveWitnessList;
57  typedef std::vector< Landmark_id > typeVectorVertex;
58  typedef std::vector<Nearest_landmark_range> Nearest_landmark_table_internal;
59  typedef Landmark_id Vertex_handle;
60 
61  protected:
62  Nearest_landmark_table_internal nearest_landmark_table_;
63 
64  public:
66  /* @name Constructor
67  */
68 
70 
72  }
73 
82  Strong_witness_complex(Nearest_landmark_table_ const & nearest_landmark_table)
83  : nearest_landmark_table_(std::begin(nearest_landmark_table), std::end(nearest_landmark_table)) {
84  }
85 
95  template < typename SimplicialComplexForWitness >
97  double max_alpha_square,
98  Landmark_id limit_dimension = std::numeric_limits<Landmark_id>::max()) const {
99  Landmark_id complex_dim = 0;
100  if (complex.num_vertices() > 0) {
101  std::cerr << "Strong witness complex cannot create complex - complex is not empty.\n";
102  return false;
103  }
104  if (max_alpha_square < 0) {
105  std::cerr << "Strong witness complex cannot create complex - squared relaxation parameter must be "
106  << "non-negative.\n";
107  return false;
108  }
109  for (auto w : nearest_landmark_table_) {
110  ActiveWitness aw(w);
111  typeVectorVertex simplex;
112  typename ActiveWitness::iterator aw_it = aw.begin();
113  float lim_dist2 = aw.begin()->second + max_alpha_square;
114  while ((Landmark_id)simplex.size() <= limit_dimension && aw_it != aw.end() && aw_it->second < lim_dist2) {
115  simplex.push_back(aw_it->first);
116  complex.insert_simplex_and_subfaces(simplex, aw_it->second - aw.begin()->second);
117  aw_it++;
118  }
119  // continue inserting limD-faces of the following simplices
120  typeVectorVertex& vertices = simplex; // 'simplex' now will be called vertices
121  while (aw_it != aw.end() && aw_it->second < lim_dist2) {
122  typeVectorVertex facet = {};
123  add_all_faces_of_dimension(limit_dimension, vertices, vertices.begin(), aw_it,
124  aw_it->second - aw.begin()->second, facet, complex);
125  vertices.push_back(aw_it->first);
126  aw_it++;
127  }
128  if ((Landmark_id)simplex.size() - 1 > complex_dim)
129  complex_dim = simplex.size() - 1;
130  }
131  return true;
132  }
133 
135 
136  private:
137  /* \brief Adds recursively all the faces of a certain dimension dim-1 witnessed by the same witness.
138  * Iterator is needed to know until how far we can take landmarks to form simplexes.
139  * simplex is the prefix of the simplexes to insert.
140  * The landmark pointed by aw_it is added to all formed simplices.
141  */
142  template < typename SimplicialComplexForWitness >
143  void add_all_faces_of_dimension(Landmark_id dim,
144  typeVectorVertex& vertices,
145  typename typeVectorVertex::iterator curr_it,
146  typename ActiveWitness::iterator aw_it,
147  double filtration_value,
148  typeVectorVertex& simplex,
149  SimplicialComplexForWitness& sc) const {
150  if (dim > 0) {
151  while (curr_it != vertices.end()) {
152  simplex.push_back(*curr_it);
153  ++curr_it;
154  add_all_faces_of_dimension(dim-1,
155  vertices,
156  curr_it,
157  aw_it,
158  filtration_value,
159  simplex,
160  sc);
161  simplex.pop_back();
162  add_all_faces_of_dimension(dim,
163  vertices,
164  curr_it,
165  aw_it,
166  filtration_value,
167  simplex,
168  sc);
169  }
170  } else if (dim == 0) {
171  simplex.push_back(aw_it->first);
172  sc.insert_simplex_and_subfaces(simplex, filtration_value);
173  simplex.pop_back();
174  }
175  }
176 };
177 
178 } // namespace witness_complex
179 
180 } // namespace Gudhi
181 
182 #endif // STRONG_WITNESS_COMPLEX_H_
The concept SimplicialComplexForWitness describes the requirements for a type to implement a simplici...
Definition: SimplicialComplexForWitness.h:34
Definition: SimplicialComplexForAlpha.h:26
Strong_witness_complex(Nearest_landmark_table_ const &nearest_landmark_table)
Initializes member variables before constructing simplicial complex.
Definition: Strong_witness_complex.h:82
Insertion_result_type insert_simplex_and_subfaces(Input_vertex_range const &vertex_range, Filtration_value filtration)
Inserts a simplex and all its faces with vertices from a given range &#39;vertex_range&#39; in the simplicial...
bool create_complex(SimplicialComplexForWitness &complex, double max_alpha_square, Landmark_id limit_dimension=std::numeric_limits< Landmark_id >::max()) const
Outputs the strong witness complex of relaxation &#39;max_alpha_square&#39; in a simplicial complex data stru...
Definition: Strong_witness_complex.h:96
Constructs strong witness complex for a given table of nearest landmarks with respect to witnesses...
Definition: Strong_witness_complex.h:49
GUDHI  Version 2.1.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Wed Jan 31 2018 09:40:55 for GUDHI by Doxygen 1.8.11