summaryrefslogtreecommitdiff
path: root/src/Tangential_complex/include/gudhi/Tangential_complex/Simplicial_complex.h
blob: 4881bdd5ffe5279008e4e9a6ee4a76de761d54a5 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*    This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
 *    See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
 *    Author(s):       Clement Jamin
 *
 *    Copyright (C) 2016 Inria
 *
 *    Modification(s):
 *      - YYYY/MM Author: Description of the modification
 */

#ifndef TANGENTIAL_COMPLEX_SIMPLICIAL_COMPLEX_H_
#define TANGENTIAL_COMPLEX_SIMPLICIAL_COMPLEX_H_

#include <gudhi/Tangential_complex/config.h>
#include <gudhi/Tangential_complex/utilities.h>
#include <gudhi/Debug_utils.h>
#include <gudhi/console_color.h>

#include <CGAL/iterator.h>

// For is_pure_pseudomanifold
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
#include <boost/container/flat_set.hpp>

#include <algorithm>
#include <string>
#include <fstream>
#include <map>  // for map<>
#include <vector>  // for vector<>
#include <set>  // for set<>

namespace Gudhi {
namespace tangential_complex {
namespace internal {

class Simplicial_complex {
 public:
  typedef boost::container::flat_set<std::size_t> Simplex;
  typedef std::set<Simplex> Simplex_set;

  // If perform_checks = true, the function:
  // - won't insert the simplex if it is already in a higher dim simplex
  // - will erase any lower-dim simplices that are faces of the new simplex
  // Returns true if the simplex was added
  bool add_simplex(
                   const Simplex &s, bool perform_checks = true) {
    if (perform_checks) {
      unsigned int num_pts = static_cast<int> (s.size());
      std::vector<Complex::iterator> to_erase;
      bool check_higher_dim_simpl = true;
      for (Complex::iterator it_simplex = m_complex.begin(),
           it_simplex_end = m_complex.end();
           it_simplex != it_simplex_end;
           ++it_simplex) {
        // Check if the simplex is not already in a higher dim simplex
        if (check_higher_dim_simpl
            && it_simplex->size() > num_pts
            && std::includes(it_simplex->begin(), it_simplex->end(),
                             s.begin(), s.end())) {
          // No need to insert it, then
          return false;
        }
        // Check if the simplex includes some lower-dim simplices
        if (it_simplex->size() < num_pts
            && std::includes(s.begin(), s.end(),
                             it_simplex->begin(), it_simplex->end())) {
          to_erase.push_back(it_simplex);
          // We don't need to check higher-sim simplices any more
          check_higher_dim_simpl = false;
        }
      }
      for (std::vector<Complex::iterator>::const_iterator it = to_erase.begin();
           it != to_erase.end(); ++it) {
        m_complex.erase(*it);
      }
    }
    return m_complex.insert(s).second;
  }

  const Simplex_set &simplex_range() const {
    return m_complex;
  }

  bool empty() {
    return m_complex.empty();
  }

  void clear() {
    m_complex.clear();
  }

  template <typename Test, typename Output_it>
  void get_simplices_matching_test(Test test, Output_it out) {
    for (Complex::const_iterator it_simplex = m_complex.begin(),
         it_simplex_end = m_complex.end();
         it_simplex != it_simplex_end;
         ++it_simplex) {
      if (test(*it_simplex))
        *out++ = *it_simplex;
    }
  }

  // When a simplex S has only one co-face C, we can remove S and C
  // without changing the topology

  void collapse(int max_simplex_dim, bool quiet = false) {
#ifdef DEBUG_TRACES
    if (!quiet)
      std::cerr << "Collapsing... ";
#endif
    // We note k = max_simplex_dim - 1
    int k = max_simplex_dim - 1;

    typedef Complex::iterator Simplex_iterator;
    typedef std::vector<Simplex_iterator> Simplex_iterator_list;
    typedef std::map<Simplex, Simplex_iterator_list> Cofaces_map;

    std::size_t num_collapsed_maximal_simplices = 0;
    do {
      num_collapsed_maximal_simplices = 0;
      // Create a map associating each non-maximal k-faces to the list of its
      // maximal cofaces
      Cofaces_map cofaces_map;
      for (Complex::const_iterator it_simplex = m_complex.begin(),
           it_simplex_end = m_complex.end();
           it_simplex != it_simplex_end;
           ++it_simplex) {
        if (static_cast<int> (it_simplex->size()) > k + 1) {
          std::vector<Simplex> k_faces;
          // Get the k-faces composing the simplex
          combinations(*it_simplex, k + 1, std::back_inserter(k_faces));
          for (const auto &comb : k_faces)
            cofaces_map[comb].push_back(it_simplex);
        }
      }

      // For each non-maximal k-face F, if F has only one maximal coface Cf:
      // - Look for the other k-faces F2, F3... of Cf in the map and:
      //    * if the list contains only Cf, clear the list (we don't remove the
      //      list since it creates troubles with the iterators) and add the F2,
      //      F3... to the complex
      //    * otherwise, remove Cf from the associated list
      // - Remove Cf from the complex
      for (Cofaces_map::const_iterator it_map_elt = cofaces_map.begin(),
           it_map_end = cofaces_map.end();
           it_map_elt != it_map_end;
           ++it_map_elt) {
        if (it_map_elt->second.size() == 1) {
          std::vector<Simplex> k_faces;
          const Simplex_iterator_list::value_type &it_Cf =
              *it_map_elt->second.begin();
          GUDHI_CHECK(it_Cf->size() == max_simplex_dim + 1,
                      std::logic_error("Wrong dimension"));
          // Get the k-faces composing the simplex
          combinations(*it_Cf, k + 1, std::back_inserter(k_faces));
          for (const auto &f2 : k_faces) {
            // Skip F
            if (f2 != it_map_elt->first) {
              Cofaces_map::iterator it_comb_in_map = cofaces_map.find(f2);
              if (it_comb_in_map->second.size() == 1) {
                it_comb_in_map->second.clear();
                m_complex.insert(f2);
              } else {  // it_comb_in_map->second.size() > 1
                Simplex_iterator_list::iterator it = std::find(it_comb_in_map->second.begin(),
                                                               it_comb_in_map->second.end(),
                                                               it_Cf);
                GUDHI_CHECK(it != it_comb_in_map->second.end(),
                            std::logic_error("Error: it == it_comb_in_map->second.end()"));
                it_comb_in_map->second.erase(it);
              }
            }
          }
          m_complex.erase(it_Cf);
          ++num_collapsed_maximal_simplices;
        }
      }
      // Repeat until no maximal simplex got removed
    } while (num_collapsed_maximal_simplices > 0);

    // Collapse the lower dimension simplices
    if (k > 0)
      collapse(max_simplex_dim - 1, true);

#ifdef DEBUG_TRACES
    if (!quiet)
      std::cerr << "done.\n";
#endif
  }

  void display_stats() const {
    std::cerr << yellow << "Complex stats:\n" << white;

    if (m_complex.empty()) {
      std::cerr << "  * No simplices.\n";
    } else {
      // Number of simplex for each dimension
      std::map<int, std::size_t> simplex_stats;

      for (Complex::const_iterator it_simplex = m_complex.begin(),
           it_simplex_end = m_complex.end();
           it_simplex != it_simplex_end;
           ++it_simplex) {
        ++simplex_stats[static_cast<int> (it_simplex->size()) - 1];
      }

      for (std::map<int, std::size_t>::const_iterator it_map = simplex_stats.begin();
           it_map != simplex_stats.end(); ++it_map) {
        std::cerr << "  * " << it_map->first << "-simplices: "
            << it_map->second << "\n";
      }
    }
  }

  // verbose_level = 0, 1 or 2
  bool is_pure_pseudomanifold__do_not_check_if_stars_are_connected(int simplex_dim,
                                                                   bool allow_borders = false,
                                                                   bool exit_at_the_first_problem = false,
                                                                   int verbose_level = 0,
                                                                   std::size_t *p_num_wrong_dim_simplices = NULL,
                                                                   std::size_t *p_num_wrong_number_of_cofaces = NULL) const {
    typedef Simplex K_1_face;
    typedef std::map<K_1_face, std::size_t> Cofaces_map;

    std::size_t num_wrong_dim_simplices = 0;
    std::size_t num_wrong_number_of_cofaces = 0;

    // Counts the number of cofaces of each K_1_face

    // Create a map associating each non-maximal k-faces to the list of its
    // maximal cofaces
    Cofaces_map cofaces_map;
    for (Complex::const_iterator it_simplex = m_complex.begin(),
         it_simplex_end = m_complex.end();
         it_simplex != it_simplex_end;
         ++it_simplex) {
      if (static_cast<int> (it_simplex->size()) != simplex_dim + 1) {
        if (verbose_level >= 2)
          std::cerr << "Found a simplex with dim = "
            << it_simplex->size() - 1 << "\n";
        ++num_wrong_dim_simplices;
      } else {
        std::vector<K_1_face> k_1_faces;
        // Get the facets composing the simplex
        combinations(
                     *it_simplex, simplex_dim, std::back_inserter(k_1_faces));
        for (const auto &k_1_face : k_1_faces) {
          ++cofaces_map[k_1_face];
        }
      }
    }

    for (Cofaces_map::const_iterator it_map_elt = cofaces_map.begin(),
         it_map_end = cofaces_map.end();
         it_map_elt != it_map_end;
         ++it_map_elt) {
      if (it_map_elt->second != 2
          && (!allow_borders || it_map_elt->second != 1)) {
        if (verbose_level >= 2)
          std::cerr << "Found a k-1-face with "
            << it_map_elt->second << " cofaces\n";

        if (exit_at_the_first_problem)
          return false;
        else
          ++num_wrong_number_of_cofaces;
      }
    }

    bool ret = num_wrong_dim_simplices == 0 && num_wrong_number_of_cofaces == 0;

    if (verbose_level >= 1) {
      std::cerr << "Pure pseudo-manifold: ";
      if (ret) {
        std::cerr << green << "YES" << white << "\n";
      } else {
        std::cerr << red << "NO" << white << "\n"
            << "  * Number of wrong dimension simplices: "
            << num_wrong_dim_simplices << "\n"
            << "  * Number of wrong number of cofaces: "
            << num_wrong_number_of_cofaces << "\n";
      }
    }

    if (p_num_wrong_dim_simplices)
      *p_num_wrong_dim_simplices = num_wrong_dim_simplices;
    if (p_num_wrong_number_of_cofaces)
      *p_num_wrong_number_of_cofaces = num_wrong_number_of_cofaces;

    return ret;
  }

  template <int K>
  std::size_t num_K_simplices() const {
    Simplex_set k_simplices;

    for (Complex::const_iterator it_simplex = m_complex.begin(),
         it_simplex_end = m_complex.end();
         it_simplex != it_simplex_end;
         ++it_simplex) {
      if (it_simplex->size() == K + 1) {
        k_simplices.insert(*it_simplex);
      } else if (it_simplex->size() > K + 1) {
        // Get the k-faces composing the simplex
        combinations(
                     *it_simplex, K + 1, std::inserter(k_simplices, k_simplices.begin()));
      }
    }

    return k_simplices.size();
  }

  std::ptrdiff_t euler_characteristic(bool verbose = false) const {
    if (verbose)
      std::cerr << "\nComputing Euler characteristic of the complex...\n";

    std::size_t num_vertices = num_K_simplices<0>();
    std::size_t num_edges = num_K_simplices<1>();
    std::size_t num_triangles = num_K_simplices<2>();

    std::ptrdiff_t ec =
        (std::ptrdiff_t) num_vertices
        - (std::ptrdiff_t) num_edges
        + (std::ptrdiff_t) num_triangles;

    if (verbose)
      std::cerr << "Euler characteristic: V - E + F = "
        << num_vertices << " - " << num_edges << " + " << num_triangles << " = "
        << blue
        << ec
        << white << "\n";

    return ec;
  }

  // TODO(CJ): ADD COMMENTS

  bool is_pure_pseudomanifold(
                              int simplex_dim,
                              std::size_t num_vertices,
                              bool allow_borders = false,
                              bool exit_at_the_first_problem = false,
                              int verbose_level = 0,
                              std::size_t *p_num_wrong_dim_simplices = NULL,
                              std::size_t *p_num_wrong_number_of_cofaces = NULL,
                              std::size_t *p_num_unconnected_stars = NULL,
                              Simplex_set *p_wrong_dim_simplices = NULL,
                              Simplex_set *p_wrong_number_of_cofaces_simplices = NULL,
                              Simplex_set *p_unconnected_stars_simplices = NULL) const {
    // If simplex_dim == 1, we do not need to check if stars are connected
    if (simplex_dim == 1) {
      if (p_num_unconnected_stars)
        *p_num_unconnected_stars = 0;
      return is_pure_pseudomanifold__do_not_check_if_stars_are_connected(simplex_dim,
                                                                         allow_borders,
                                                                         exit_at_the_first_problem,
                                                                         verbose_level,
                                                                         p_num_wrong_dim_simplices,
                                                                         p_num_wrong_number_of_cofaces);
    }
    // Associates each vertex (= the index in the vector)
    // to its star (list of simplices)
    typedef std::vector<std::vector<Complex::const_iterator> > Stars;
    std::size_t num_wrong_dim_simplices = 0;
    std::size_t num_wrong_number_of_cofaces = 0;
    std::size_t num_unconnected_stars = 0;

    // Fills a Stars data structure
    Stars stars;
    stars.resize(num_vertices);
    for (Complex::const_iterator it_simplex = m_complex.begin(),
         it_simplex_end = m_complex.end();
         it_simplex != it_simplex_end;
         ++it_simplex) {
      if (static_cast<int> (it_simplex->size()) != simplex_dim + 1) {
        if (verbose_level >= 2)
          std::cerr << "Found a simplex with dim = "
            << it_simplex->size() - 1 << "\n";
        ++num_wrong_dim_simplices;
        if (p_wrong_dim_simplices)
          p_wrong_dim_simplices->insert(*it_simplex);
      } else {
        for (Simplex::const_iterator it_point_idx = it_simplex->begin();
             it_point_idx != it_simplex->end();
             ++it_point_idx) {
          stars[*it_point_idx].push_back(it_simplex);
        }
      }
    }

    // Now, for each star, we have a vector of its d-simplices
    // i.e. one index for each d-simplex
    // Boost Graph only deals with indexes, so we also need indexes for the
    // (d-1)-simplices
    std::size_t center_vertex_index = 0;
    for (Stars::const_iterator it_star = stars.begin();
         it_star != stars.end();
         ++it_star, ++center_vertex_index) {
      typedef std::map<Simplex, std::vector<std::size_t> >
          Dm1_faces_to_adj_D_faces;
      Dm1_faces_to_adj_D_faces dm1_faces_to_adj_d_faces;

      for (std::size_t i_dsimpl = 0; i_dsimpl < it_star->size(); ++i_dsimpl) {
        Simplex dm1_simpl_of_link = *((*it_star)[i_dsimpl]);
        dm1_simpl_of_link.erase(center_vertex_index);
        // Copy it to a vector so that we can use operator[] on it
        std::vector<std::size_t> dm1_simpl_of_link_vec(
                                                       dm1_simpl_of_link.begin(), dm1_simpl_of_link.end());

        CGAL::Combination_enumerator<int> dm2_simplices(
                                                        simplex_dim - 1, 0, simplex_dim);
        for (; !dm2_simplices.finished(); ++dm2_simplices) {
          Simplex dm2_simpl;
          for (int j = 0; j < simplex_dim - 1; ++j)
            dm2_simpl.insert(dm1_simpl_of_link_vec[dm2_simplices[j]]);
          dm1_faces_to_adj_d_faces[dm2_simpl].push_back(i_dsimpl);
        }
      }

      Adj_graph adj_graph;
      std::vector<Graph_vertex> d_faces_descriptors;
      d_faces_descriptors.resize(it_star->size());
      for (std::size_t j = 0; j < it_star->size(); ++j)
        d_faces_descriptors[j] = boost::add_vertex(adj_graph);

      Dm1_faces_to_adj_D_faces::const_iterator dm1_to_d_it =
          dm1_faces_to_adj_d_faces.begin();
      Dm1_faces_to_adj_D_faces::const_iterator dm1_to_d_it_end =
          dm1_faces_to_adj_d_faces.end();
      for (std::size_t i_km1_face = 0;
           dm1_to_d_it != dm1_to_d_it_end;
           ++dm1_to_d_it, ++i_km1_face) {
        Graph_vertex km1_gv = boost::add_vertex(adj_graph);

        for (std::vector<std::size_t>::const_iterator kface_it =
             dm1_to_d_it->second.begin();
             kface_it != dm1_to_d_it->second.end();
             ++kface_it) {
          boost::add_edge(km1_gv, *kface_it, adj_graph);
        }

        if (dm1_to_d_it->second.size() != 2
            && (!allow_borders || dm1_to_d_it->second.size() != 1)) {
          ++num_wrong_number_of_cofaces;
          if (p_wrong_number_of_cofaces_simplices) {
            for (auto idx : dm1_to_d_it->second)
              p_wrong_number_of_cofaces_simplices->insert(*((*it_star)[idx]));
          }
        }
      }

      // What is left is to check the connexity
      bool is_connected = true;
      if (boost::num_vertices(adj_graph) > 0) {
        std::vector<int> components(boost::num_vertices(adj_graph));
        is_connected =
            (boost::connected_components(adj_graph, &components[0]) == 1);
      }

      if (!is_connected) {
        if (verbose_level >= 2)
          std::cerr << "Error: star #" << center_vertex_index
            << " is not connected\n";
        ++num_unconnected_stars;
        if (p_unconnected_stars_simplices) {
          for (std::vector<Complex::const_iterator>::const_iterator
               it_simpl = it_star->begin(),
               it_simpl_end = it_star->end();
               it_simpl != it_simpl_end;
               ++it_simpl) {
            p_unconnected_stars_simplices->insert(**it_simpl);
          }
        }
      }
    }

    // Each one has been counted several times ("simplex_dim" times)
    num_wrong_number_of_cofaces /= simplex_dim;

    bool ret =
        num_wrong_dim_simplices == 0
        && num_wrong_number_of_cofaces == 0
        && num_unconnected_stars == 0;

    if (verbose_level >= 1) {
      std::cerr << "Pure pseudo-manifold: ";
      if (ret) {
        std::cerr << green << "YES" << white << "\n";
      } else {
        std::cerr << red << "NO" << white << "\n"
            << "  * Number of wrong dimension simplices: "
            << num_wrong_dim_simplices << "\n"
            << "  * Number of wrong number of cofaces: "
            << num_wrong_number_of_cofaces << "\n"
            << "  * Number of not-connected stars: "
            << num_unconnected_stars << "\n";
      }
    }

    if (p_num_wrong_dim_simplices)
      *p_num_wrong_dim_simplices = num_wrong_dim_simplices;
    if (p_num_wrong_number_of_cofaces)
      *p_num_wrong_number_of_cofaces = num_wrong_number_of_cofaces;
    if (p_num_unconnected_stars)
      *p_num_unconnected_stars = num_unconnected_stars;

    return ret;
  }

 private:
  typedef Simplex_set Complex;

  // graph is an adjacency list
  typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Adj_graph;
  // map that gives to a certain simplex its node in graph and its dimension
  typedef boost::graph_traits<Adj_graph>::vertex_descriptor Graph_vertex;
  typedef boost::graph_traits<Adj_graph>::edge_descriptor Graph_edge;

  Complex m_complex;
};  // class Simplicial_complex

}  // namespace internal
}  // namespace tangential_complex
}  // namespace Gudhi

#endif  // TANGENTIAL_COMPLEX_SIMPLICIAL_COMPLEX_H_