-
Notifications
You must be signed in to change notification settings - Fork 2
/
triangulation.cpp
176 lines (166 loc) · 6.07 KB
/
triangulation.cpp
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
#include "triangulation.hpp"
void Triangulation::run() {
init();
for (int i = 0; i < points.size(); ++i) {
insertPoint(i);
}
}
void Triangulation::init() {
// calculate bounding triangle
double lx = points[0].x, ly = points[0].y, hx = lx, hy = ly;
for (const auto& pt : points) {
if (pt.x < lx) lx = pt.x;
else if (pt.x > hx) hx = pt.x;
if (pt.y < ly) ly = pt.y;
else if (pt.y > hy) hy = pt.y;
}
double height = hy - ly, width = hx - lx;
constexpr double margin = 10;
vertices.push_back(std::make_shared<Vertex>(lx - height - margin, ly - margin));
vertices.push_back(std::make_shared<Vertex>(hx + height + margin, ly - margin));
vertices.push_back(std::make_shared<Vertex>((lx + hx) / 2, hy + width / 2 + margin)); // CCW
// init DCEL
for (int i = 0; i < 3; ++i) {
int j = (i==2) ? 0 : i + 1;
auto& edge = vertices[i]->edge;
edge = HalfEdge::newEdge(vertices[i], vertices[j]);
}
for (int i = 0; i < 3; ++i) {
int j = (i==2) ? 0 : i + 1;
auto edge = vertices[i]->edge;
auto nextEdge = vertices[j]->edge;
HalfEdge::setNext(edge, nextEdge);
HalfEdge::setNext(nextEdge->twin, edge->twin);
}
auto face = Face::newFace(vertices[0]->edge);
outerFace = Face::newFace(vertices[0]->edge->twin);
// point - face mapping
auto& facePtIdxes = facePts[face];
ptFaces.resize(points.size());
for (int i = 0; i < points.size(); ++i) {
facePtIdxes.push_back(i);
ptFaces[i] = face;
}
}
void Triangulation::insertPoint(int ptIdx) {
auto face = ptFaces[ptIdx];
auto centerVertex = std::make_shared<Vertex>(points[ptIdx]);
centerVertex->idx = ptIdx;
vertices.push_back(centerVertex);
// store inner edges
std::vector<std::shared_ptr<HalfEdge>> innerEdges;
auto edge = face->edge;
do {
innerEdges.push_back(edge);
edge = edge->next;
}
while (edge != face->edge);
// init
for (auto edge : innerEdges) {
HalfEdge::setNext(edge, HalfEdge::newEdge(edge->twin->org, centerVertex));
}
centerVertex->edge = face->edge->next->twin;
// connect
std::vector<std::shared_ptr<Face>> faces;
for (auto edge : innerEdges) {
HalfEdge::setNext(edge->next, edge->prev->next->twin);
HalfEdge::setNext(edge->prev->next->twin, edge);
faces.push_back(Face::newFace(edge));
}
// divide points into sub faces (triangles)
std::vector<std::vector<int>> pts(faces.size());
rebucket(faces, facePts[face], pts);
facePts.erase(face);
for (int i = 0; i < faces.size(); ++i) {
facePts[faces[i]] = pts[i];
}
// swap test
for (auto edge : innerEdges) {
swapTest(edge);
}
}
void Triangulation::rebucket(const std::vector<std::shared_ptr<Face>>& faces, const std::vector<int>& inputPts, std::vector<std::vector<int>>& pts) {
for (int pt : inputPts) {
for (int i = 0; i < faces.size(); ++i) {
if (faces[i]->inFace(points[pt])) {
ptFaces[pt] = faces[i];
pts[i].push_back(pt);
}
}
}
}
void Triangulation::swapTest(std::shared_ptr<HalfEdge> edge) {
if (edge->twin->face == outerFace) {
return;
}
// edge is AB
auto edgePA = edge->prev;
auto edgeDB = edge->twin->prev;
if (inCircle( edgePA->org->loc, // P
edge->org->loc, // A
edge->twin->org->loc, // B
edgeDB->org->loc)) { // D
// add PD
auto edgePD = HalfEdge::newEdge(edgePA->org, edgeDB->org); // PD
HalfEdge::setNext(edgePA->prev, edgePD); // B-P-D
HalfEdge::setNext(edgePD->twin, edgePA); // D-P-A
HalfEdge::setNext(edgeDB->prev, edgePD->twin); // A-D-P
HalfEdge::setNext(edgePD, edgeDB); // P-D-B
// remove AB
for (auto halfEdge : {edge, edge->twin}) {
if (halfEdge->org->edge == halfEdge) {
halfEdge->org->edge = halfEdge->twin->next;
}
}
HalfEdge::setNext(edge->prev, edge->twin->next); // P-A-D
HalfEdge::setNext(edge->twin->prev, edge->next); // D-A-P
// overwrite faces
std::vector<std::shared_ptr<Face>> oldFaces = {edge->face, edge->twin->face};
std::vector<std::shared_ptr<Face>> faces;
faces.push_back(Face::newFace(edgePD));
faces.push_back(Face::newFace(edgePD->twin));
// rebucket
std::vector<std::vector<int>> pts(faces.size());
for (auto oldFace : oldFaces) {
rebucket(faces, facePts[oldFace], pts);
facePts.erase(oldFace);
}
for (int i = 0; i < faces.size(); ++i) {
facePts[faces[i]] = pts[i];
}
swapTest(edgePD->next); // DB
swapTest(edgePD->twin->prev); // DA
}
}
void Triangulation::write(const std::string& fileName) {
auto iterateAllEdges = [&](std::function<void(std::shared_ptr<HalfEdge>)> handle) {
for (int i = 3; i < vertices.size(); ++i) { // the first three are dummy vertices
auto vertex = vertices[i];
auto edge = vertex->edge;
do {
if (edge->twin->org->idx >= 0 && // dst vertex should be an input point
edge->org->idx < edge->twin->org->idx) { // visit once only
handle(edge);
}
edge = edge->twin->next;
}
while (edge != vertex->edge);
}
};
// count
numEdges = 0;
iterateAllEdges([&](std::shared_ptr<HalfEdge> edge){
++numEdges;
});
// write
std::ofstream ofs(fileName);
if (ofs.fail()) {
std::cout << "Cannot open output file " << fileName << ", skip..." << std::endl;
}
ofs << numEdges << std::endl;
iterateAllEdges([&](std::shared_ptr<HalfEdge> edge){
ofs << edge->org->loc.x << " " << edge->org->loc.y << " "
<< edge->twin->org->loc.x << " " << edge->twin->org->loc.y << std::endl;
});
ofs.close();
}