summaryrefslogtreecommitdiff
path: root/src/Skeleton_blocker/test/TestSimplifiable.cpp
blob: 76736240db6b2c805820a19526fcfdde1322173c (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
/*    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):       David Salinas
 *
 *    Copyright (C) 2014  INRIA Sophia Antipolis-Mediterranee (France)
 *
 *    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/>.
 */


#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <sstream>
#include "gudhi/Test.h"
//#include "Skeleton_blocker/Simplex.h"
#include "gudhi/Skeleton_blocker.h"


using namespace std;

using namespace Gudhi;

using namespace skeleton_blocker;

template<typename ComplexType> class Skeleton_blocker_sub_complex;
typedef Skeleton_blocker_complex<Skeleton_blocker_simple_traits> Complex;
typedef Complex::Vertex_handle Vertex_handle;
typedef Complex::Root_vertex_handle Root_vertex_handle;
typedef Skeleton_blocker_simplex<Vertex_handle> Simplex;
// true iff v \in complex

bool assert_vertex(Complex &complex, Vertex_handle v) {
  Simplex simplex(v);
  bool test = complex.contains(simplex);
  assert(test);
  return test;
}

// true iff the blocker (a,b,c) is in complex

bool assert_blocker(Complex &complex, Root_vertex_handle a, Root_vertex_handle b, Root_vertex_handle c) {

  return complex.contains_blocker(Simplex(*complex.get_address(a), *complex.get_address(b), *complex.get_address(c)));
  //return complex.contains_blocker((a),(b),(c));
}

void build_complete(int n, Complex& complex) {
  complex.clear();
  for (int i = 0; i < n; i++)
    complex.add_vertex();
  for (int i = 0; i < n; i++)
    for (int j = 0; j < i; j++)
      complex.add_edge_without_blockers(Vertex_handle(i), Vertex_handle(j));
}

bool test_contraction1() {

  enum {
    a, b, x, y, z, n
  };
  Complex complex(n);
  build_complete(n, complex);
  complex.remove_edge(static_cast<Vertex_handle> (b), static_cast<Vertex_handle> (z));
  complex.add_blocker(Simplex(static_cast<Vertex_handle> (a), static_cast<Vertex_handle> (x),
                              static_cast<Vertex_handle> (y)));
  complex.add_blocker(Simplex(static_cast<Vertex_handle> (b), static_cast<Vertex_handle> (x),
                              static_cast<Vertex_handle> (y)));

  // Print result
  cerr << "complex before complex" << complex.to_string() << endl;

  cerr << endl << endl;
  complex.contract_edge(static_cast<Vertex_handle> (a), static_cast<Vertex_handle> (b));
  // Print result
  cerr << "ContractEdge(0,1)\n";
  PRINT(complex.to_string());

  // verification
  for (int i = 0; i < 5; i++)
    if (i != 1) assert_vertex(complex, static_cast<Vertex_handle> (i));
  bool test1 = !complex.contains_edge(static_cast<Vertex_handle> (a), static_cast<Vertex_handle> (b));
  bool test2 = assert_blocker(complex, Root_vertex_handle(a), Root_vertex_handle(x), Root_vertex_handle(y));
  bool test3 = complex.num_edges() == 6;
  bool test4 = complex.num_blockers() == 1;
  Simplex sigma;
  sigma.add_vertex(static_cast<Vertex_handle> (a));
  sigma.add_vertex(static_cast<Vertex_handle> (x));
  sigma.add_vertex(static_cast<Vertex_handle> (y));
  sigma.add_vertex(static_cast<Vertex_handle> (z));
  bool test5 = !(complex.contains(sigma));
  return test1 && test2 && test3 && test4&&test5;
}

bool test_contraction2() {

  enum {
    a, b, x, y, z, n
  };
  Complex complex(n);
  build_complete(n, complex);
  complex.remove_edge(static_cast<Vertex_handle> (b), static_cast<Vertex_handle> (x));
  Simplex blocker;
  blocker.add_vertex(static_cast<Vertex_handle> (a));
  blocker.add_vertex(static_cast<Vertex_handle> (y));
  blocker.add_vertex(static_cast<Vertex_handle> (z));

  complex.add_blocker(blocker);

  // Print result
  cerr << "complex complex" << complex.to_string();
  cerr << endl << endl;
  complex.contract_edge(static_cast<Vertex_handle> (a), static_cast<Vertex_handle> (b));

  cerr << "complex.ContractEdge(a,b)" << complex.to_string();

  cerr << endl << endl;

  // there should be one blocker (a,c,d,e) in the complex
  bool test;
  test = complex.contains_blocker(Simplex(static_cast<Vertex_handle> (a), static_cast<Vertex_handle> (x),
                                          static_cast<Vertex_handle> (y), static_cast<Vertex_handle> (z)));
  test = test && complex.num_blockers() == 1;
  return test;
}

bool test_link_condition1() {

  Complex complex(0);
  // Build the complexes
  build_complete(4, complex);
  complex.add_blocker(Simplex(static_cast<Vertex_handle> (0), static_cast<Vertex_handle> (1), static_cast<Vertex_handle> (2)));


  // Print result
  cerr << "complex complex" << complex.to_string();
  cerr << endl << endl;

  bool weak_link_condition = complex.link_condition(Vertex_handle(1), Vertex_handle(2), true);

  bool strong_link_condition = complex.link_condition(Vertex_handle(1), Vertex_handle(2), false);

  return weak_link_condition && !strong_link_condition;
}

bool test_collapse0() {
  Complex complex(5);
  build_complete(4, complex);
  complex.add_vertex();
  complex.add_edge_without_blockers(static_cast<Vertex_handle> (2), static_cast<Vertex_handle> (4));
  complex.add_edge_without_blockers(static_cast<Vertex_handle> (3), static_cast<Vertex_handle> (4));
  // Print result
  cerr << "initial complex :\n" << complex.to_string();
  cerr << endl << endl;

  Simplex simplex_123(static_cast<Vertex_handle> (1), static_cast<Vertex_handle> (2), static_cast<Vertex_handle> (3));
  complex.remove_star(simplex_123);
  cerr << "complex.remove_star(1,2,3):\n" << complex.to_string();
  cerr << endl << endl;

  // verification
  bool blocker123_here = complex.contains_blocker(simplex_123);
  cerr << "----> Ocomplex \n";
  return blocker123_here;
}

bool test_collapse1() {
  Complex complex(5);
  build_complete(4, complex);
  complex.add_blocker(Simplex(Vertex_handle(0), Vertex_handle(1), Vertex_handle(2), Vertex_handle(3)));
  // Print result
  cerr << "initial complex :\n" << complex.to_string();
  cerr << endl << endl;

  Simplex simplex_123(Vertex_handle(1), Vertex_handle(2), Vertex_handle(3));
  complex.remove_star(simplex_123);
  cerr << "complex.remove_star(1,2,3):\n" << complex.to_string();
  cerr << endl << endl;

  // verification
  bool res = complex.contains_blocker(simplex_123);
  res = res && complex.num_blockers() == 1;
  cerr << "----> Ocomplex \n";
  return res;
}

bool test_collapse2() {
  Complex complex(5);
  build_complete(4, complex);
  complex.add_vertex();
  complex.add_edge_without_blockers(Vertex_handle(1), Vertex_handle(4));
  complex.add_edge_without_blockers(Vertex_handle(2), Vertex_handle(4));
  complex.add_edge_without_blockers(Vertex_handle(3), Vertex_handle(4));
  complex.add_blocker(Simplex(Vertex_handle(1), Vertex_handle(2), Vertex_handle(3), Vertex_handle(4)));
  // Print result
  cerr << "initial complex :\n" << complex.to_string();
  cerr << endl << endl;

  Simplex sigma(Vertex_handle(1), Vertex_handle(2), Vertex_handle(3));
  complex.remove_star(sigma);
  cerr << "complex.remove_star(1,2,3):\n" << complex.to_string();
  cerr << endl << endl;

  // verification
  bool blocker_removed = !complex.contains_blocker(Simplex(Vertex_handle(1), Vertex_handle(2), Vertex_handle(3), Vertex_handle(4)));
  bool blocker123_here = complex.contains_blocker(sigma);
  return blocker_removed && blocker123_here;
}

bool test_collapse3() {
  Complex complex(5);
  build_complete(4, complex);
  complex.add_vertex();
  complex.add_edge_without_blockers(Vertex_handle(1), Vertex_handle(4));
  complex.add_edge_without_blockers(Vertex_handle(2), Vertex_handle(4));
  complex.add_edge_without_blockers(Vertex_handle(3), Vertex_handle(4));
  complex.add_blocker(Simplex(Vertex_handle(1), Vertex_handle(2), Vertex_handle(3), Vertex_handle(4)));
  // Print result
  cerr << "initial complex:\n" << complex.to_string();
  cerr << endl << endl;

  complex.remove_star(static_cast<Vertex_handle> (2));
  cerr << "complex after remove star of 2:\n" << complex.to_string();

  bool blocker134_here = complex.contains_blocker(Simplex(Vertex_handle(1), Vertex_handle(3), Vertex_handle(4)));
  bool blocker1234_here = complex.contains_blocker(Simplex(Vertex_handle(1), Vertex_handle(2), Vertex_handle(3), Vertex_handle(4)));
  return blocker134_here && !blocker1234_here;
}

bool test_add_simplex() {
  Complex complex(4);
  build_complete(4, complex);
  complex.add_blocker(Simplex(Vertex_handle(0), Vertex_handle(1), Vertex_handle(3)));
  cerr << "initial complex:\n" << complex.to_string();
  cerr << endl << endl;

  complex.add_simplex(Simplex(Vertex_handle(0), Vertex_handle(1), Vertex_handle(3)));
  cerr << "complex after add_simplex:\n" << complex.to_string();
  return complex.num_blockers() == 1
      && complex.contains_blocker(Simplex(Vertex_handle(0), Vertex_handle(1), Vertex_handle(2), Vertex_handle(3)));
}

bool test_add_simplex2() {
  Complex complex;
  build_complete(4, complex);
  // Print result
  cerr << "initial complex:\n" << complex.to_string();
  cerr << endl << endl;

  Complex copy(complex.num_vertices());

  std::vector<Simplex> simplices(complex.complex_simplex_range().begin(), complex.complex_simplex_range().end());
  sort(simplices.begin(), simplices.end(), [&](const Simplex& s1, const Simplex & s2) {
    return s1.dimension() < s2.dimension();
  });
  for (const auto & simplex : simplices) {
    if (!copy.contains(simplex) && simplex.dimension() == 1)
      copy.add_edge_without_blockers(simplex.first_vertex(), simplex.last_vertex());
    if (!copy.contains(simplex) && simplex.dimension() > 1)
      copy.add_simplex(simplex);
  }


  cerr << "complex after add_simplex:\n" << copy.to_string();


  return complex.num_blockers() == copy.num_blockers() &&
      complex.num_edges() == copy.num_edges() &&
      complex.num_vertices() == copy.num_vertices();
}

bool test_add_simplex3() {
  Complex complex(5);
  build_complete(5, complex);
  complex.remove_edge(Vertex_handle(3), Vertex_handle(4));
  Simplex sigma(Vertex_handle(0), Vertex_handle(1), Vertex_handle(2));
  complex.add_blocker(sigma);
  // Print result
  cerr << "initial complex:\n" << complex.to_string();
  cerr << endl << endl;
  complex.add_simplex(sigma);
  //should create two blockers 0123 and 0124
  cerr << "complex after adding simplex 012:\n" << complex.to_string();
  return complex.num_blockers() == 2
      && complex.contains_blocker(Simplex(Vertex_handle(0), Vertex_handle(1), Vertex_handle(2), Vertex_handle(3)))
      && complex.contains_blocker(Simplex(Vertex_handle(0), Vertex_handle(1), Vertex_handle(2), Vertex_handle(4)));
}

bool test_add_simplex4() {
  int n = 6;
  Complex complex(n);

  // add all simplex 0..n without i
  for (int i = 0; i < n; i++) {
    Simplex s;
    for (int k = 0; k < n; k++)
      s.add_vertex(Vertex_handle(k));
    s.remove_vertex(Vertex_handle(i));
    complex.add_simplex(s);

    //at step i there is only blocker 0..i
    if (i < 2 && complex.num_blockers() > 0)
      return false;
    if (i >= 2 && complex.num_blockers() != 1) {
      Simplex b;
      for (int k = 0; k < i; k++)
        b.add_vertex(Vertex_handle(i));
      if (!complex.contains_blocker(b))
        return false;
    }
    TESTVALUE(complex.blockers_to_string());
  }
  Simplex s;
  for (int k = 0; k < n; k++)
    s.add_vertex(Vertex_handle(k));
  return complex.num_blockers() == 1 && complex.contains_blocker(s);
}

bool test_add_edge() {
  Complex complex(4);
  for (unsigned i = 0u; i < 4; i++)
    complex.add_edge(Vertex_handle(i), Vertex_handle((i + 1) % 4));

  // Print result
  cerr << "initial complex:\n" << complex.to_string();
  cerr << endl << endl;
  complex.add_edge(Vertex_handle(1), Vertex_handle(3));
  //should create two blockers 013 and 012
  cerr << "complex after adding edge 13:\n" << complex.to_string();
  return complex.num_blockers() == 2
      && complex.contains_blocker(Simplex(Vertex_handle(0), Vertex_handle(1), Vertex_handle(3)))
      && complex.contains_blocker(Simplex(Vertex_handle(1), Vertex_handle(2), Vertex_handle(3)));
}

bool test_remove_popable_blockers() {
  Complex complex;
  build_complete(4, complex);
  complex.add_vertex();
  complex.add_edge_without_blockers(Vertex_handle(3), Vertex_handle(4));
  complex.add_edge_without_blockers(Vertex_handle(2), Vertex_handle(4));
  Simplex sigma1 = Simplex(Vertex_handle(1), Vertex_handle(2), Vertex_handle(3));
  Simplex sigma2 = Simplex(Vertex_handle(2), Vertex_handle(3), Vertex_handle(4));

  complex.add_blocker(sigma1);
  complex.add_blocker(sigma2);
  cerr << "complex complex" << complex.to_string();
  cerr << endl << endl;
  cerr << "complex.RemovePopableBlockers();" << endl;
  complex.remove_popable_blockers();
  cerr << "complex complex" << complex.to_string();
  cerr << endl << endl;

  bool test1 = (complex.num_blockers() == 1);


  // test 2
  complex.clear();
  build_complete(4, complex);
  complex.add_vertex();
  complex.add_vertex();
  complex.add_edge_without_blockers(Vertex_handle(3), Vertex_handle(4));
  complex.add_edge_without_blockers(Vertex_handle(2), Vertex_handle(4));
  complex.add_edge_without_blockers(Vertex_handle(2), Vertex_handle(5));
  complex.add_edge_without_blockers(Vertex_handle(3), Vertex_handle(5));
  complex.add_edge_without_blockers(Vertex_handle(4), Vertex_handle(5));
  sigma1 = Simplex(Vertex_handle(1), Vertex_handle(2), Vertex_handle(3));
  sigma2 = Simplex(Vertex_handle(2), Vertex_handle(3), Vertex_handle(4));

  complex.add_blocker(sigma1);
  complex.add_blocker(sigma2);
  cerr << "complex complex" << complex.to_string();
  cerr << endl << endl;
  cerr << "complex.RemovePopableBlockers();" << endl;
  complex.remove_popable_blockers();
  cerr << "complex complex" << complex.to_string();

  cerr << endl << endl;
  bool test2 = (complex.num_blockers() == 0);
  return test1&&test2;
}

int main(int argc, char *argv[]) {
  Tests tests_simplifiable_complex;
  tests_simplifiable_complex.add("Test contraction 1", test_contraction1);
  tests_simplifiable_complex.add("Test contraction 2", test_contraction2);
  tests_simplifiable_complex.add("Test Link condition 1", test_link_condition1);
  tests_simplifiable_complex.add("Test remove popable blockers", test_remove_popable_blockers);


  tests_simplifiable_complex.add("Test collapse 0", test_collapse0);
  tests_simplifiable_complex.add("Test collapse 1", test_collapse1);
  tests_simplifiable_complex.add("Test collapse 2", test_collapse2);
  tests_simplifiable_complex.add("Test collapse 3", test_collapse3);

  tests_simplifiable_complex.add("Test add edge",test_add_edge);
  tests_simplifiable_complex.add("Test add simplex", test_add_simplex);
  tests_simplifiable_complex.add("Test add simplex2", test_add_simplex2);
  tests_simplifiable_complex.add("Test add simplex3",test_add_simplex3);
  tests_simplifiable_complex.add("Test add simplex4",test_add_simplex4);


  tests_simplifiable_complex.run();

  if (tests_simplifiable_complex.run())
    return EXIT_SUCCESS;
  else
    return EXIT_FAILURE;
}