-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.cpp
178 lines (143 loc) · 4.9 KB
/
main.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
#include <map>
#include <set>
#include "codelibrary/base/time_counter.h"
#include "codelibrary/geometry/kernel/transform_3d.h"
#include "codelibrary/geometry/pca_estimate_normals_3d.h"
#include "codelibrary/point_cloud/global_l0_extractor.h"
#include "codelibrary/point_cloud/io/xyz_io.h"
#include "codelibrary/point_cloud/region_growing.h"
#include "codelibrary/statistics/kernel/mean.h"
#include "codelibrary/visualization/plot.h"
using namespace cl;
void DrawMappedNormals(const Array<RPlane3D>& planes) {
std::set<RVector3D> normals;
for (const RPlane3D& plane : planes) {
if (plane.normal().z < 0.0)
normals.insert(-plane.normal());
else
normals.insert(plane.normal());
}
plot::PointPlot point_plot;
for (const RVector3D& normal : normals) {
double u = 0.5 + std::atan2(normal.y, normal.x) / (2.0 * M_PI);
double v = 1.0 - 2.0 * std::asin(normal.z) / M_PI;
u = Clamp(u, 0.0, 1.0);
v = Clamp(v, 0.0, 1.0);
point_plot.Draw(RPoint2D(u, v), RGB32Color(0, 0, 255, 128), 10);
}
point_plot.set_axes_ticks_visibility(false);
point_plot.set_axes_fixed(true);
point_plot.Save("test.svg", 512, 512);
system("test.svg");
}
/**
* Assign each plane a color.
*/
void AssignColor(const Array<RPoint3D>& points,
const Array<int>& labels,
const Array<RPlane3D>& planes,
Array<RGB32Color>* colors) {
assert(points.size() == labels.size());
assert(colors);
Array<RGB32Color> colors1 = cl::ColorMap::Set1().colors();
std::map<RVector3D, int> hash;
Array<int> labels2(points.size(), -1);
int id = 0;
for (int i = 0; i < points.size(); ++i) {
if (labels[i] == -1) continue;
const RPlane3D& plane = planes[labels[i]];
RVector3D v = plane.normal();
if (v.z < 0.0) v = -v;
if (hash.find(v) == hash.end()) {
hash[v] = id++;
}
labels2[i] = hash[v];
}
Array<int> cnt(id, 0);
for (int i = 0; i < labels.size(); ++i) {
if (labels2[i] == -1) continue;
++cnt[labels2[i]];
}
Array<int> index;
IndexSort(cnt.begin(), cnt.end(), &index);
std::reverse(index.begin(), index.end());
Array<int> rank(planes.size());
for (int i = 0; i < index.size(); ++i) {
rank[index[i]] = i;
}
std::mt19937 random(32);
for (int i = colors1.size(); i < planes.size(); ++i) {
colors1.push_back(cl::RGB32Color(random()));
}
colors->resize(points.size());
for (int i = 0; i < points.size(); ++i) {
if (labels2[i] == -1) continue;
int id = labels2[i];
(*colors)[i] = colors1[rank[id]];
}
}
void Metric(const Array<RPoint3D>& points,
const Array<int>& labels,
const Array<RPlane3D>& planes) {
Array<double> dis1;
for (int i = 0; i < points.size(); ++i) {
double dis = DBL_MAX;
for (const RPlane3D& plane : planes) {
dis = std::min(dis, Distance(points[i], plane));
}
dis1.push_back(dis);
}
LOG(INFO) << "RMS of distances1:"
<< RootMeanSquare(dis1.begin(), dis1.end());
Array<double> dis2;
KDTree<RPoint3D> kd_tree(points.begin(), points.end());
for (int i = 0; i < points.size(); ++i) {
int l = labels[i];
if (l == -1) continue;
RPoint3D p1 = Project(points[i], planes[l]);
RPoint3D p;
kd_tree.FindNearestPoint(p1, &p);
dis2.push_back(Distance(p, p1));
}
LOG(INFO) << "RMS of distances2:"
<< RootMeanSquare(dis2.begin(), dis2.end());
int cnt = 0;
for (int i = 0; i < points.size(); ++i) {
if (labels[i] != -1) ++cnt;
}
LOG(INFO) << "Coverage: " << static_cast<double>(cnt) / points.size();
}
int main() {
LOG_ON(INFO);
Array<RPoint3D> points;
point_cloud::ReadXYZPoints("foam_box.xyz", &points);
int n_points = points.size();
LOG(INFO) << "Import points: " << n_points;
LOG(INFO) << "Estimate normals...";
KDTree<RPoint3D> kd_tree;
kd_tree.SwapPoints(&points);
Array<RVector3D> normals;
geometry::PCAEstimateNormals(kd_tree, 30, &normals);
LOG(INFO) << "Start plane extracting.";
TimeCounter timer;
timer.Start();
Array<RPlane3D> planes;
Array<int> labels;
point_cloud::GlobalL0Extractor extractor(15, 50, 3, 1.0);
extractor.ExtractPlanes(kd_tree, normals, &planes, &labels);
timer.Stop();
LOG(INFO) << "Time: " << timer.elapsed_time();
LOG(INFO) << "Extracted " << planes.size() << " planes.";
kd_tree.SwapPoints(&points);
kd_tree.clear();
// Print metrics.
Metric(points, labels, planes);
DrawMappedNormals(planes);
// Output points.
Array<RGB32Color> colors;
AssignColor(points, labels, planes, &colors);
cl::point_cloud::WriteXYZPoints("out.xyz", points, colors);
LOG(INFO) << "Done";
system("out.xyz");
return 0;
}