forked from cp-algorithms/cp-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_planar_faces.cpp
More file actions
126 lines (111 loc) · 2.63 KB
/
test_planar_faces.cpp
File metadata and controls
126 lines (111 loc) · 2.63 KB
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
#include <algorithm>
#include <assert.h>
#include <vector>
#include "planar.h"
bool equal_cycles(const std::vector<size_t> & a, const std::vector<size_t> & b) {
size_t n = a.size();
if (n != b.size()) {
return false;
}
for (size_t begin = 0; begin < n; begin++) {
bool ok = true;
for (size_t i = 0; i < n; i++) {
if (a[(begin + i) % n] != b[i]) {
ok = false;
break;
}
}
if (ok) {
return true;
}
}
return false;
}
void test_simple() {
std::vector<Point> p = {
Point(0, 0),
Point(1, 0),
Point(1, 1),
Point(0, 1)
};
std::vector<std::vector<size_t>> adj = {
{1, 2, 3},
{0, 2},
{0, 1, 3},
{0, 2}
};
auto faces = find_faces(p, adj);
assert(faces.size() == 3u);
assert(equal_cycles(faces[0], {3, 2, 1, 0}));
bool eq11 = equal_cycles(faces[1], {0, 1, 2});
bool eq12 = equal_cycles(faces[1], {0, 2, 3});
bool eq21 = equal_cycles(faces[2], {0, 1, 2});
bool eq22 = equal_cycles(faces[2], {0, 2, 3});
assert(eq11^eq21);
assert(eq12^eq22);
}
void test_degenerate() {
std::vector<Point> p = {
Point(0, 0),
Point(1, 1),
Point(2, 2)
};
std::vector<std::vector<size_t>> adj = {
{1},
{0, 2},
{1}
};
auto faces = find_faces(p, adj);
assert(faces.size() == 1u);
assert(equal_cycles(faces[0], {0, 1, 2, 1}));
}
void test_cycle_with_chain() {
std::vector<Point> p = {
Point(0, 0),
Point(0, 3),
Point(3, 3),
Point(3, 0),
Point(1, 1),
Point(2, 2)
};
std::vector<std::vector<size_t>> adj = {
{1, 3},
{0, 2},
{1, 3, 5},
{0, 2},
{5},
{2, 4}
};
auto faces = find_faces(p, adj);
assert(faces.size() == 2u);
assert(equal_cycles(faces[0], {0, 1, 2, 3}));
assert(equal_cycles(faces[1], {2, 5, 4, 5, 2, 1, 0, 3}));
}
void test_ccw_angle() {
std::vector<Point> p = {
Point(0, 2),
Point(1, 3),
Point(0, 1),
Point(1, 0),
Point(-1, 0),
Point(-1, 3)
};
std::vector<std::vector<size_t>> adj = {
{1, 5},
{0, 2},
{1, 3},
{2, 4},
{3, 5},
{0, 4}
};
auto faces = find_faces(p, adj);
assert(faces.size() == 2u);
assert(equal_cycles(faces[0], {0, 1, 2, 3, 4, 5}));
assert(equal_cycles(faces[1], {5, 4, 3, 2, 1, 0}));
}
int main() {
test_simple();
test_degenerate();
test_cycle_with_chain();
test_ccw_angle();
}