-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.cpp
executable file
·184 lines (145 loc) · 5.48 KB
/
test1.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
177
178
179
180
181
182
183
184
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Delaunay_triangulation_3.h>
#include <CGAL/Union_find.h>
#include <CGAL/IO/read_xyz_points.h>
#include <iostream>
#include <fstream>
#include <limits>
#include <map>
#include <chrono>
using namespace std;
#define val(x) cout << #x "=" << x << "\n";
const double inf = std::numeric_limits<double>::infinity();
typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_3 Point3D;
typedef Kernel::Segment_3 Segment3D;
typedef CGAL::Delaunay_triangulation_3<Kernel> DT3;
template <typename T>
size_t getIndex(const vector<T> &v, const T &elem) {
return lower_bound(v.begin(), v.end(), elem) - v.begin();
}
vector<Segment3D> get_All_Edges(const DT3 &dt) {
vector<Segment3D> allEdges;
for (auto edgeItr = dt.finite_edges_begin(); edgeItr != dt.finite_edges_end(); edgeItr++)
allEdges.push_back(dt.segment(*edgeItr));
return allEdges;
}
vector<Segment3D> get_Mst_Edges_Kruskal(const vector<Point3D> &points, const DT3 &dt) {
vector<CGAL::Union_find<size_t>::handle> handle;
CGAL::Union_find<size_t> uf;
handle.reserve(points.size());
for (size_t i = 0; i < points.size(); i++)
handle.push_back(uf.make_set(i));
vector<tuple<double, size_t, size_t>> allEdges;
for (auto edgeItr = dt.finite_edges_begin(); edgeItr != dt.finite_edges_end(); edgeItr++) {
auto edge = dt.segment(*edgeItr);
auto u = getIndex(points, edge.start()),
v = getIndex(points, edge.end());
allEdges.push_back(make_tuple(edge.squared_length(), u, v));
}
sort(allEdges.begin(),
allEdges.end(),
[](tuple<double, size_t, size_t> &a, tuple<double, size_t, size_t> &b) -> bool { return get<0>(a) < get<0>(b); });
vector<Segment3D> mst;
for (auto edge : allEdges) {
auto u = get<1>(edge), v = get<2>(edge);
if (!uf.same_set(handle[u], handle[v])) {
mst.push_back(Segment3D(points[u], points[v]));
uf.unify_sets(handle[u], handle[v]);
}
}
return mst;
}
vector<Segment3D> get_Mst_Edges_Prim(const vector<Point3D> &points, const DT3 &dt) {
vector<vector<pair<size_t, double>>> adjList;
vector<Segment3D> mst;
adjList.resize(points.size());
for (auto edgeItr = dt.finite_edges_begin(); edgeItr != dt.finite_edges_end(); edgeItr++) {
auto edge = dt.segment(*edgeItr);
auto u = getIndex(points, edge.start()),
v = getIndex(points, edge.end());
adjList[u].push_back({v, edge.squared_length()});
adjList[v].push_back({u, edge.squared_length()});
}
vector<double> weight(points.size(), inf);
vector<bool> inPQ(points.size(), true);
vector<size_t> parent(points.size(), SIZE_MAX);
set<pair<double, size_t>> pq;
weight[0] = 0;
for (size_t i = 0; i < points.size(); i++)
pq.insert({weight[i], i});
while (!pq.empty()) {
size_t u = pq.begin()->second;
pq.erase(pq.begin());
inPQ[u] = false;
if (parent[u] != SIZE_MAX)
mst.push_back(Segment3D(points[u], points[parent[u]]));
for (auto elem : adjList[u]) {
size_t v = elem.first;
double w = elem.second;
if (inPQ[v] && weight[v] > w) {
pq.erase(pq.find({weight[v], v}));
weight[v] = w;
pq.insert({w, v});
parent[v] = u;
}
}
}
return mst;
}
int main(int argc, char *argv[]) {
if (argc != 3) {
cerr << "Invalid Argument\n";
return 1;
}
ifstream inputFile(argv[1]);
ofstream outputFile(argv[2]);
DT3 dt;
vector<Point3D> points;
auto start = chrono::high_resolution_clock::now();
if (!CGAL::read_xyz_points(inputFile, back_inserter(points))) { // output iterator over points
cerr << "Error: cannot read file.";
return 1;
}
sort(points.begin(), points.end()); // vector may have repeated elements like 1 1 2 2 3 3 3 4 4 5 5 6 7
auto last = unique(points.begin(), points.end()); // vector now holds {1 2 3 4 5 6 7 x x x x x x}, where 'x' is indeterminate
points.erase(last, points.end());
auto finish = chrono::high_resolution_clock::now();
cout << points.size() << " points read in " << std::chrono::duration<double>(finish - start).count() << " secs\n";
start = chrono::high_resolution_clock::now();
dt.insert(points.begin(), points.end());
if (!dt.is_valid(true)) {
cerr << "Error: fail to build a Delaunay triangulation.\n";
return 1;
}
if (dt.dimension() != 3) {
cerr << "Error: cannot built a 3D triangulation.\n Current dimension = " << dt.dimension() << "\n";
return 1;
}
finish = chrono::high_resolution_clock::now();
cout << "Delaunay Triangulation created in " << std::chrono::duration<double>(finish - start).count() << " secs\n";
start = chrono::high_resolution_clock::now();
auto mst1 = get_Mst_Edges_Kruskal(points, dt);
finish = chrono::high_resolution_clock::now();
cout << "Kruskal MST created in " << std::chrono::duration<double>(finish - start).count() << " secs\n";
//start = chrono::high_resolution_clock::now();
//auto mst2 = get_Mst_Edges_Prim(points, dt);
//finish = chrono::high_resolution_clock::now();
//cout << "Prim MST created in " << std::chrono::duration<double>(finish - start).count() << " secs\n";
//if (mst1.size() != mst2.size()) {
// cerr << "Size Different : Kruskal = " << mst1.size() << ", Prim = " << mst2.size() << "\n";
// return 1;
//}
start = chrono::high_resolution_clock::now();
outputFile << points.size() << "\n";
for (auto point : points) {
outputFile << point << "\n";
}
outputFile << mst1.size() << "\n";
for (auto edge : mst1) {
outputFile << getIndex(points, edge.start()) << " " << getIndex(points, edge.end()) << "\n";
}
finish = chrono::high_resolution_clock::now();
cout << "Output created in " << std::chrono::duration<double>(finish - start).count() << " secs\n";
return 0;
}