summaryrefslogtreecommitdiff
path: root/src/GudhUI/model/Model.h
blob: dd9bdaab3df54e4e639b1fcf3f845278190618d3 (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
/*    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):       David Salinas
 *
 *    Copyright (C) 2014 Inria
 *
 *    Modification(s):
 *      - YYYY/MM Author: Description of the modification
 *      - 2019/07 Vincent Rouvreau: homsimpl (from CHOMP) is no more delivered.
 *                                  Error message when not previously installed.
 */

#ifndef MODEL_MODEL_H_
#define MODEL_MODEL_H_

#include <gudhi/Clock.h>
#include <gudhi/Skeleton_blocker/Skeleton_blocker_simple_geometric_traits.h>
#include <gudhi/Skeleton_blocker_geometric_complex.h>
#include <gudhi/Off_reader.h>

#include <CGAL/Euclidean_distance.h>

#include <fstream>
#include <limits>
#include <string>
#include <vector>

#include "utils/UI_utils.h"
#include "utils/Lloyd_builder.h"
#include "utils/Rips_builder.h"
#include "utils/K_nearest_builder.h"
#include "utils/Vertex_collapsor.h"
#include "utils/Edge_collapsor.h"
#include "utils/Edge_contractor.h"
#include "utils/Persistence_compute.h"
#include "utils/Critical_points.h"
#include "utils/Is_manifold.h"

#include "Complex_typedefs.h"

template<typename Complex>
class CGAL_geometric_flag_complex_wrapper {
  Complex& complex_;
  typedef typename Complex::Vertex_handle Vertex_handle;
  typedef typename Complex::Point Point;

  const bool load_only_points_;

 public:
  CGAL_geometric_flag_complex_wrapper(Complex& complex, bool load_only_points = false) :
      complex_(complex),
      load_only_points_(load_only_points) { }

  void init(int dim, int num_vertices, int num_max_faces, int num_edges) const { }

  void point(const std::vector<double>& coords) {
    Point p(coords.size(), coords.begin(), coords.end());
    complex_.add_vertex(p);
  }

  void maximal_face(std::vector<int> vertices) {
    if (!load_only_points_) {
      // std::cout << "size:" << vertices.size() << std::endl;
      for (std::size_t i = 0; i < vertices.size(); ++i)
        for (std::size_t j = i + 1; j < vertices.size(); ++j)
          complex_.add_edge_without_blockers(Vertex_handle(vertices[i]), Vertex_handle(vertices[j]));
    }
  }

  void done() const { }
};

class Model {
 public:
  Complex complex_;
  typedef Complex::Vertex_handle Vertex_handle;

  Model() : complex_() { }

 public:
  void off_file_open(const std::string& name_file) {
    UIDBGMSG("load off file", name_file);
    complex_.clear();
    CGAL_geometric_flag_complex_wrapper<Complex> read_wraper(complex_, false);
    Gudhi::read_off(name_file, read_wraper);
  }

  void off_points_open(const std::string& name_file) {
    UIDBGMSG("load off points", name_file);
    complex_.clear();
    CGAL_geometric_flag_complex_wrapper<Complex> read_wraper(complex_, true);
    Gudhi::read_off(name_file, read_wraper);
  }

  void off_file_save(const std::string& name_file) {
    UIDBG("save off file");
    UIDBG("save off off_points_save");
    std::ofstream file(name_file);
    if (file.is_open()) {
      file << "OFF\n";
      file << complex_.num_vertices() << " " << complex_.num_edges() << " 0\n";
      for (auto v : complex_.vertex_range()) {
        const auto& pt(complex_.point(v));
        for (auto it = pt.cartesian_begin(); it != pt.cartesian_end(); ++it)
          file << *it << " ";
        file << std::endl;
      }
      for (auto e : complex_.edge_range())
        file << "2 " << complex_.first_vertex(e) << " " << complex_.second_vertex(e) << "\n";
      file.close();
    } else {
      std::cerr << "Could not open file " << name_file << std::endl;
    }
  }

  void off_points_save(const std::string& name_file) {
    UIDBG("save off off_points_save");
    std::ofstream file(name_file);
    if (file.is_open()) {
      file << "OFF\n";
      file << complex_.num_vertices() << " 0 0\n";
      for (auto v : complex_.vertex_range()) {
        const auto& pt(complex_.point(v));
        for (auto it = pt.cartesian_begin(); it != pt.cartesian_end(); ++it)
          file << *it << " ";
        file << std::endl;
      }
      file.close();
    } else {
      std::cerr << "Could not open file " << name_file << std::endl;
    }
  }

  // point sets operations
  void uniform_noise(double amplitude) {
    UIDBG("unif noise");
    for (auto v : complex_.vertex_range())
      complex_.point(v) = add_uniform_noise(complex_.point(v), amplitude);
  }

 private:
  Point add_uniform_noise(const Point& point, double amplitude) {
    std::vector<double> new_point(point.dimension());
    for (int i = 0; i < point.dimension(); ++i) {
      new_point[i] = point[i] + (rand() % 2 - .5) * amplitude;
    }
    return Point(point.dimension(), new_point.begin(), new_point.end());
  }

 public:
  void lloyd(int num_iterations, int num_closest_neighbors) {
    UIDBG("lloyd");
    Lloyd_builder<Complex> lloyd_builder(complex_, 1);
  }

  double squared_eucl_distance(const Point& p1, const Point& p2) const {
    return Geometry_trait::Squared_distance_d()(p1, p2);
  }

  // complex operations from points

  void build_rips(double alpha) {
    UIDBG("build_rips");
    Rips_builder<Complex> rips_builder(complex_, alpha);
  }

  void build_k_nearest_neighbors(unsigned k) {
    UIDBG("build_k_nearest");
    complex_.keep_only_vertices();
    K_nearest_builder<Complex> k_nearest_builder(complex_, k);
  }

  void build_delaunay() {
    UIDBG("build_delaunay");
    complex_.keep_only_vertices();
  }

  void contract_edges(unsigned num_contractions) {
    Gudhi::Clock c;
    Edge_contractor<Complex> contractor(complex_, num_contractions);
    std::cout << "Time to simplify: " << c.num_seconds() << "s" << std::endl;
  }

  void collapse_vertices(unsigned num_collapses) {
    auto old_num_vertices = complex_.num_vertices();
    Vertex_collapsor<Complex> collapsor(complex_, complex_.num_vertices());
    UIDBGMSG("num vertices collapsed:", old_num_vertices - complex_.num_vertices());
  }

  void collapse_edges(unsigned num_collapses) {
    Edge_collapsor<Complex> collapsor(complex_, num_collapses);
  }

  void show_graph_stats() {
    std::cout << "++++++ Graph stats +++++++" << std::endl;
    std::cout << "Num vertices : " << complex_.num_vertices() << std::endl;
    std::cout << "Num edges : " << complex_.num_edges() << std::endl;
    std::cout << "Num connected components : " << complex_.num_connected_components() << std::endl;
    std::cout << "Min/avg/max degree : " << min_degree() << "/" << avg_degree() << "/" << max_degree() << std::endl;
    std::cout << "Num connected components : " << complex_.num_connected_components() << std::endl;
    std::cout << "Num connected components : " << complex_.num_connected_components() << std::endl;
    std::cout << "+++++++++++++++++++++++++" << std::endl;
  }

 private:
  int min_degree() const {
    int res = (std::numeric_limits<int>::max)();
    for (auto v : complex_.vertex_range())
      res = (std::min)(res, complex_.degree(v));
    return res;
  }

  int max_degree() const {
    int res = 0;
    for (auto v : complex_.vertex_range())
      res = (std::max)(res, complex_.degree(v));
    return res;
  }

  int avg_degree() const {
    int res = 0;
    for (auto v : complex_.vertex_range())
      res += complex_.degree(v);
    return res / complex_.num_vertices();
  }

 public:
  void show_complex_stats() {
    std::cout << "++++++ Mesh stats +++++++" << std::endl;
    std::cout << "Num vertices : " << complex_.num_vertices() << std::endl;
    std::cout << "Num edges : " << complex_.num_edges() << std::endl;
    std::cout << "Num connected components : " << complex_.num_connected_components() << std::endl;
    std::cout << "+++++++++++++++++++++++++" << std::endl;
  }

  void show_complex_dimension() {
    unsigned num_simplices = 0;
    int euler = 0;
    int dimension = 0;
    Gudhi::Clock clock;
    for (const auto &s : complex_.complex_simplex_range()) {
      num_simplices++;
      dimension = (std::max)(s.dimension(), dimension);
      if (s.dimension() % 2 == 0)
        euler += 1;
      else
        euler -= 1;
    }
    clock.end();
    std::cout << "++++++ Mesh dimension +++++++" << std::endl;
    std::cout << "Dimension : " << dimension << std::endl;
    std::cout << "Euler characteristic : " << euler << std::endl;
    std::cout << "Num simplices : " << num_simplices << std::endl;
    std::cout << "Total time: " << clock << std::endl;
    std::cout << "Time per simplex: " << clock.num_seconds() / num_simplices << " s" << std::endl;
    std::cout << "+++++++++++++++++++++++++" << std::endl;
  }

  void show_homology_group() {
#ifdef _WIN32
    std::cout << "Works only on linux x64 for the moment\n";
#else
    Gudhi::Clock clock;
    run_chomp();
    clock.end();
#endif
  }

  void show_euler_characteristic() {
    unsigned num_simplices = 0;
    int euler = 0;
    int dimension = 0;
    for (const auto &s : complex_.complex_simplex_range()) {
      num_simplices++;
      dimension = (std::max)(s.dimension(), dimension);
      if (s.dimension() % 2 == 0)
        euler += 1;
      else
        euler -= 1;
    }
    std::cout << "Saw " << num_simplices << " simplices with maximum dimension " << dimension << std::endl;
    std::cout << "The euler characteristic is : " << euler << std::endl;
  }

  void show_persistence(int p, double threshold, int max_dim, double min_pers) {
    Persistence_compute<Complex> persistence(complex_, std::cout, Persistence_params(p, threshold, max_dim, min_pers));
  }

  void show_critical_points(double max_distance) {
    Critical_points<Complex> critical_points(complex_, std::cout, max_distance);
  }

  void show_is_manifold() {
    unsigned dim;
    bool is_manifold;
    Is_manifold<Complex> test_manifold(complex_, dim, is_manifold);

    if (is_manifold) {
      std::cout << "The complex is a " << dim << "-manifold\n";
    } else {
      if (dim < 4) {
        std::cout << "The complex has dimension greater than " << dim << " and is not a manifold\n";
      } else {
        std::cout << "The complex has dimension>=4 and may or may not be a manifold\n";
      }
    }
  }

 private:
  void run_chomp() {
    save_complex_in_file_for_chomp();
    std::cout << "Call CHOMP library\n";
    int returnValue = system("homsimpl chomp.sim");
    if (returnValue != 0) {
        std::cout << "homsimpl (from CHOMP) failed. Please check it is installed or available in the PATH."
                  << std::endl;
    }
  }

  void save_complex_in_file_for_chomp() {
    std::ofstream file;
    file.open("chomp.sim");
    for (const auto &s : complex_.complex_simplex_range()) {
      bool first = true;
      file << "(";
      for (auto x : s) {
        if (first)
          first = false;
        else
          file << ",";
        file << x;
      }
      file << ")\n";
    }
  }

 public:
  unsigned num_vertices() const {
    return complex_.num_vertices();
  }

  unsigned num_edges() const {
    return complex_.num_edges();
  }
};

#endif  // MODEL_MODEL_H_