-
Notifications
You must be signed in to change notification settings - Fork 13
/
polyve.cpp
executable file
·261 lines (225 loc) · 8.35 KB
/
polyve.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
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
#include "polyve/polyve.h"
#include <cstdlib>
#include <climits>
#include <random>
using namespace std;
using namespace ros;
using namespace Eigen;
PolyVe::PolyVe(Config &conf, NodeHandle &nh_)
: config(conf), nh(nh_), visualization(config, nh)
{
triggerSub = nh.subscribe(config.triggerTopic, 1, &PolyVe::triggerCallBack,
this, TransportHints().tcpNoDelay());
}
PolyVe::~PolyVe() {}
void PolyVe::triggerCallBack(const std_msgs::Empty::ConstPtr &msg)
{
conductVE();
return;
}
void PolyVe::conductVE(void)
{
std::cout << "------------------------------------" << std::endl;
// ---------------------------- Test Data Generation ----------------------------
Eigen::Matrix3Xd mesh, recoveredV;
Eigen::Matrix<double, 3, -1, Eigen::ColMajor> vertices;
Eigen::Vector3d inner;
// Randomly generate a set of points
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(-0.5, 0.5);
vertices.resize(3, config.randomTries);
for (int i = 0; i < config.randomTries; i++)
{
vertices.col(i) << dis(gen), dis(gen), dis(gen);
}
vertices.array() *= config.randomScale;
vertices.row(0).array() += dis(gen) * 2.0 * config.randomScale;
vertices.row(1).array() += dis(gen) * 2.0 * config.randomScale;
vertices.row(2).array() += dis(gen) * 2.0 * config.randomScale;
// Get the convex hull in mesh
quickhull::QuickHull<double> qh;
const auto cvxHull = qh.getConvexHull(vertices.data(),
vertices.cols(),
false, false);
const auto &idBuffer = cvxHull.getIndexBuffer();
const auto &vtBuffer = cvxHull.getVertexBuffer();
int ids = idBuffer.size();
mesh.resize(3, ids);
quickhull::Vector3<double> v;
for (int i = 0; i < ids; i++)
{
v = vtBuffer[idBuffer[i]];
mesh(0, i) = v.x;
mesh(1, i) = v.y;
mesh(2, i) = v.z;
}
// Obtain the half space intersection form from the mesh
Eigen::MatrixX4d hPoly(ids / 3, 4);
Eigen::Vector3d normal, point, edge0, edge1;
for (int i = 0; i < ids / 3; i++)
{
point = mesh.col(3 * i + 1);
edge0 = point - mesh.col(3 * i);
edge1 = mesh.col(3 * i + 2) - point;
normal = edge0.cross(edge1).normalized();
hPoly(i, 0) = normal(0);
hPoly(i, 1) = normal(1);
hPoly(i, 2) = normal(2);
hPoly(i, 3) = -normal.dot(point);
}
// Generate some redundant half spaces
Eigen::Array4d hParamRange;
hParamRange.head<3>() = hPoly.leftCols<3>().cwiseAbs().colwise().maxCoeff().transpose();
hParamRange(3) = hPoly.rightCols<1>().cwiseAbs().maxCoeff();
Eigen::Vector4d halfSpace;
Eigen::MatrixX4d redundantHs(config.redundantTryH, 4);
int validNum = 0;
for (int i = 0; i < config.redundantTryH; i++)
{
halfSpace(0) = dis(gen);
halfSpace(1) = dis(gen);
halfSpace(2) = dis(gen);
halfSpace(3) = dis(gen);
halfSpace.array() *= 2.0 * hParamRange;
if (halfSpace.head<3>().squaredNorm() > 0 &&
(halfSpace.head<3>().transpose() * vertices).maxCoeff() < halfSpace(3))
{
redundantHs(validNum, 0) = halfSpace(0);
redundantHs(validNum, 1) = halfSpace(1);
redundantHs(validNum, 2) = halfSpace(2);
redundantHs(validNum, 3) = -halfSpace(3);
validNum++;
}
}
std::cout << "Number of Redundant Half Space: " << validNum << std::endl;
Eigen::MatrixX4d mergedHs(validNum + hPoly.rows(), 4);
mergedHs.topRows(hPoly.rows()) = hPoly;
mergedHs.bottomRows(validNum) = redundantHs.topRows(validNum);
// ---------------------------- Test Vertex Enumeration ----------------------------
if (!geo_utils::enumerateVs(mergedHs, recoveredV))
{
std::cout << "Vertex Enumeration Fails Once !!!" << std::endl;
return;
}
// ---------------------------- Visualize All Results ----------------------------
std::cout << "Number of Enumerated Vertices: " << recoveredV.cols() << std::endl;
geo_utils::findInterior(hPoly, inner); //recalculate iterior just for visualization
visualization.visualizeVertices(recoveredV);
visualization.visualizeInterior(inner);
visualization.visualizeMesh(mesh);
return;
}
Visualization::Visualization(Config &conf, NodeHandle &nh_)
: config(conf), nh(nh_)
{
meshPub = nh.advertise<visualization_msgs::Marker>(config.meshTopic, 1000);
edgePub = nh.advertise<visualization_msgs::Marker>(config.edgeTopic, 1000);
verticesPub = nh.advertise<visualization_msgs::Marker>(config.vertexTopic, 1000);
interiorPub = nh.advertise<visualization_msgs::Marker>(config.interiorTopic, 1000);
}
void Visualization::visualizeMesh(const Eigen::Matrix3Xd &mesh)
{
visualization_msgs::Marker meshMarker, edgeMarker;
meshMarker.id = 0;
meshMarker.header.stamp = ros::Time::now();
meshMarker.header.frame_id = "map";
meshMarker.pose.orientation.w = 1.00;
meshMarker.action = visualization_msgs::Marker::ADD;
meshMarker.type = visualization_msgs::Marker::TRIANGLE_LIST;
meshMarker.ns = "mesh";
meshMarker.color.r = 0.00;
meshMarker.color.g = 0.00;
meshMarker.color.b = 1.00;
meshMarker.color.a = 0.5;
meshMarker.scale.x = 1.00;
meshMarker.scale.y = 1.00;
meshMarker.scale.z = 1.00;
edgeMarker = meshMarker;
edgeMarker.type = visualization_msgs::Marker::LINE_LIST;
edgeMarker.ns = "edge";
edgeMarker.color.r = 0.00;
edgeMarker.color.g = 1.00;
edgeMarker.color.b = 0.00;
edgeMarker.color.a = 1.00;
edgeMarker.scale.x = 0.005 * config.randomScale;
geometry_msgs::Point point;
int ptnum = mesh.cols();
for (int i = 0; i < ptnum; i++)
{
point.x = mesh(0, i);
point.y = mesh(1, i);
point.z = mesh(2, i);
meshMarker.points.push_back(point);
}
for (int i = 0; i < ptnum / 3; i++)
{
for (int j = 0; j < 3; j++)
{
point.x = mesh(0, 3 * i + j);
point.y = mesh(1, 3 * i + j);
point.z = mesh(2, 3 * i + j);
edgeMarker.points.push_back(point);
point.x = mesh(0, 3 * i + (j + 1) % 3);
point.y = mesh(1, 3 * i + (j + 1) % 3);
point.z = mesh(2, 3 * i + (j + 1) % 3);
edgeMarker.points.push_back(point);
}
}
meshPub.publish(meshMarker);
edgePub.publish(edgeMarker);
return;
}
void Visualization::visualizeVertices(const Eigen::Matrix3Xd &vertices)
{
visualization_msgs::Marker pointsMarker;
pointsMarker.id = 0;
pointsMarker.header.stamp = ros::Time::now();
pointsMarker.header.frame_id = "map";
pointsMarker.pose.orientation.w = 1.00;
pointsMarker.action = visualization_msgs::Marker::ADD;
pointsMarker.type = visualization_msgs::Marker::SPHERE_LIST;
pointsMarker.ns = "vertices";
pointsMarker.color.r = 1.00;
pointsMarker.color.g = 0.00;
pointsMarker.color.b = 0.00;
pointsMarker.color.a = 1.00;
pointsMarker.scale.x = 0.02 * config.randomScale;
pointsMarker.scale.y = pointsMarker.scale.x;
pointsMarker.scale.z = pointsMarker.scale.x;
for (int i = 0; i < vertices.cols(); i++)
{
geometry_msgs::Point point;
point.x = vertices.col(i)(0);
point.y = vertices.col(i)(1);
point.z = vertices.col(i)(2);
pointsMarker.points.push_back(point);
}
verticesPub.publish(pointsMarker);
return;
}
void Visualization::visualizeInterior(const Eigen::Vector3d &interior)
{
visualization_msgs::Marker pointsMarker;
pointsMarker.id = 0;
pointsMarker.header.stamp = ros::Time::now();
pointsMarker.header.frame_id = "map";
pointsMarker.pose.orientation.w = 1.00;
pointsMarker.action = visualization_msgs::Marker::ADD;
pointsMarker.type = visualization_msgs::Marker::SPHERE_LIST;
pointsMarker.ns = "interior";
pointsMarker.color.r = 1.00;
pointsMarker.color.g = 1.00;
pointsMarker.color.b = 1.00;
pointsMarker.color.a = 1.00;
pointsMarker.scale.x = 0.02 * config.randomScale;
pointsMarker.scale.y = pointsMarker.scale.x;
pointsMarker.scale.z = pointsMarker.scale.x;
geometry_msgs::Point point;
point.x = interior(0);
point.y = interior(1);
point.z = interior(2);
pointsMarker.points.push_back(point);
interiorPub.publish(pointsMarker);
return;
}