-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicppcl.cpp
471 lines (403 loc) · 16.3 KB
/
icppcl.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include "icppcl.h"
#include "ui_icppcl.h"
ICPPCL::ICPPCL(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::ICPPCL)
{
ui->setupUi(this);
QObject::connect(ui->actionOpen, &QAction::triggered, this, &ICPPCL::open);
//Sampling
QObject::connect(ui->actionUniform_Sampling, &QAction::triggered, this, &ICPPCL::uniformSampling);
//features
QObject::connect(ui->actionNormalVector, &QAction::triggered, this, &ICPPCL::normalVector);
QObject::connect(ui->actionPFH, &QAction::triggered, this, &ICPPCL::pfh);
QObject::connect(ui->dataTree, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(itemSelected(QTreeWidgetItem*, int)));
QObject::connect(ui->dataTree, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(itemPopmenu(const QPoint&)));
//point cloud initialization
cloud_now.reset(new PointCloudT);
ui->dataTree->setContextMenuPolicy(Qt::CustomContextMenu);
vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
vtkNew<vtkRenderer> renderer;
renderWindow->AddRenderer(renderer);
//visualization
//viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));
viewer.reset(new pcl::visualization::PCLVisualizer(renderer, renderWindow, "viewer", false));
ui->openGLWidget->setRenderWindow(viewer->getRenderWindow());
viewer->setupInteractor(ui->openGLWidget->GetInteractor(), ui->openGLWidget->GetRenderWindow());
ui->openGLWidget->update();
//ui->qvtkWidget->SetRenderWindow(viewer->getRenderWindow());
//viewer->setupInteractor(ui->qvtkWidget->GetInteractor(), ui->qvtkWidget->GetRenderWindow());
//ui->qvtkWidget->update();
}
void ICPPCL::open()
{
QStringList files = QFileDialog::getOpenFileNames(this, tr("Open point cloud file"), QString::fromLocal8Bit(model_dirname.c_str()), tr("Point cloud data(*.pcd *.ply *.obj *.txt);;All file(*.*)"));
if (files.isEmpty()) return;
viewer->removeAllPointClouds();
PointCloudT::Ptr c_temp;
for (int i = 0; i != files.size(); ++i) {
c_temp.reset(new PointCloudT);
QString file = files[i];
std::string file_s = file.toStdString();
std::string subname = getFileName(file_s);
ui->statusbar->showMessage(QString::fromLocal8Bit(subname.c_str()) + ": " + QString::number(i) + "/" + QString::number(files.size()) + " point cloud loading...");
int status = -1;
if (file.endsWith(".pcd", Qt::CaseInsensitive))
{
status = pcl::io::loadPCDFile(file_s, *c_temp);
}
else if (file.endsWith(".ply", Qt::CaseInsensitive))
{
status = pcl::io::loadPLYFile(file_s, *c_temp);
}
else if (file.endsWith(".obj", Qt::CaseInsensitive))
{
status = pcl::io::loadOBJFile(file_s, *c_temp);
}
else if(file.endsWith(".txt", Qt::CaseInsensitive))
{
std::ifstream fs;
pcl::PointXYZRGBA p;
std::string line;
fs.open(file_s.c_str(), std::ios::binary);
if (!fs.is_open() || fs.fail())
{
PCL_ERROR("could not open file '%s'! Error : '%s' \n", file_s.c_str(), strerror(errno));
fs.close();
return ;
}
std::vector<std::string> st;
while (!fs.eof())
{
std::getline(fs, line);
if (line.empty())
continue;
boost::trim(line);
boost::split(st, line, boost::is_any_of(",\r"), boost::token_compress_on);
if (st.size() != 6)
continue;
p.x = float(atof(st[0].c_str()));
p.y = float(atof(st[1].c_str()));
p.z = float(atof(st[2].c_str()));
c_temp->push_back(p);
}
fs.close();
status = 0;
}
else
{
QMessageBox::information(this, tr("File form error"), tr("Can't open files except .ply .pcd .obj"));
}
if (status != 0)
{
QMessageBox::critical(this, tr("Reading file error"), tr("We can not open the file"));
return ;
}
if (c_temp->points[0].r == 0 && c_temp->points[0].g == 0 && c_temp->points[0].b == 0)
{
setCloudRGB(c_temp, 255, 255, 255);
}
setCloudAlpha(c_temp, 255);
cloud_show.push_back(c_temp);
total_points += c_temp->points.size();
}
setConsole("Load Cloud(s)", "We now have " + QString::number(total_points) + " Points.");
ui->statusbar->showMessage("Load Point Cloud Done.");
updatePointcloud();
}
void ICPPCL::itemSelected(QTreeWidgetItem* item, int count)
{
count = ui->dataTree->indexOfTopLevelItem(item);
for (int i = 0; i != cloud_show.size(); ++i)
{
viewer->updatePointCloud(cloud_show[i], "cloud" + QString::number(i).toStdString());
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud" + QString::number(i).toStdString());
}
*cloud_now = *cloud_show[count];
//The size of the point cloud corresponding to the selected tiem becomes larger
QList<QTreeWidgetItem*> itemList = ui->dataTree->selectedItems();
int selected_item_count = ui->dataTree->selectedItems().size();
for (int i = 0; i != selected_item_count; ++i) {
int cloud_id = ui->dataTree->indexOfTopLevelItem(itemList[i]);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "cloud" + QString::number(cloud_id).toStdString());
}
ui->openGLWidget->update();
setConsole("itemSelected", QString::number(selected_item_count) + " item(s) selected.");
}
void ICPPCL::itemPopmenu(const QPoint &)
{
QTreeWidgetItem* curItem = ui->dataTree->currentItem(); //Get the currently clicked node
if (curItem == NULL) return ; //In this case, the position of the right button is not within the scope of the treeItem, that is, right-clicking in an empty position
QString name = curItem->text(0);
int id = ui->dataTree->indexOfTopLevelItem(curItem);
std::string cloud_id = "cloud " + QString::number(id).toStdString();
QAction hideItemAction("Hide", this);
QAction showItemAction("show", this);
QAction changeColorAction("Change color", this);
QAction randomColorAction("Random color", this);
connect(&hideItemAction, &QAction::triggered, this, &ICPPCL::pointHide);
connect(&showItemAction, &QAction::triggered, this, &ICPPCL::pointShow);
connect(&changeColorAction, &QAction::triggered, this, &ICPPCL::pointcolorChanged);
connect(&randomColorAction, &QAction::triggered, this, &ICPPCL::pointcolorRandom);
QMenu menu(ui->dataTree);
menu.addAction(&hideItemAction);
menu.addAction(&showItemAction);
menu.addAction(&changeColorAction);
menu.addAction(&randomColorAction);
menu.exec(QCursor::pos()); //Show at current mouse position
setConsole("popMenu", "popMenu");
}
void ICPPCL::pointcolorChanged()
{
QColor color = QColorDialog::getColor(Qt::white, this, "Select color for point cloud");
if (color.isValid()) //Determine if the selected color is valid
{
QList<QTreeWidgetItem*> itemList = ui->dataTree->selectedItems();
int selected_item_count = ui->dataTree->selectedItems().size();
if (selected_item_count == 0) {
for (int i = 0; i != cloud_show.size(); ++i) {
for (int j = 0; j != cloud_show[i]->points.size(); ++j) {
cloud_show[i]->points[j].r = color.red();
cloud_show[i]->points[j].g = color.green();
cloud_show[i]->points[j].b = color.blue();
}
}
}
else {
for (int i = 0; i != selected_item_count; ++i) {
int cloud_id = ui->dataTree->indexOfTopLevelItem(itemList[i]);
for (int j = 0; j != cloud_show[cloud_id]->size(); ++j) {
cloud_show[cloud_id]->points[j].r = color.red();
cloud_show[cloud_id]->points[j].g = color.green();
cloud_show[cloud_id]->points[j].b = color.blue();
}
}
// output window
setConsole("change cloud color", "to " + QString::number(color.red()) + " " + QString::number(color.green()) + " " + QString::number(color.blue()));
}
}
updatePointcloud();
}
void ICPPCL::pointcolorRandom()
{
unsigned int r = 255 * (rand() / (RAND_MAX + 1.0f));
unsigned int g = 255 * (rand() / (RAND_MAX + 1.0f));
unsigned int b = 255 * (rand() / (RAND_MAX + 1.0f));
QList<QTreeWidgetItem*> itemList = ui->dataTree->selectedItems();
int selected_item_count = ui->dataTree->selectedItems().size();
if (selected_item_count == 0) {
for (int i = 0; i != cloud_show.size(); ++i) {
for (int j = 0; j != cloud_show[i]->points.size(); ++j) {
cloud_show[i]->points[j].r = r;
cloud_show[i]->points[j].g = g;
cloud_show[i]->points[j].b = b;
}
}
}
else {
for (int i = 0; i != selected_item_count; ++i) {
int cloud_id = ui->dataTree->indexOfTopLevelItem(itemList[i]);
for (int j = 0; j != cloud_show[cloud_id]->size(); ++j) {
cloud_show[cloud_id]->points[j].r = r;
cloud_show[cloud_id]->points[j].g = g;
cloud_show[cloud_id]->points[j].b = b;
}
}
// output window
setConsole("Change cloud color", "to random color." );
}
updatePointcloud();
ui->openGLWidget->update();
}
void ICPPCL::pointShow()
{
QList<QTreeWidgetItem*> itemList = ui->dataTree->selectedItems();
int selected_item_count = ui->dataTree->selectedItems().size();
if (selected_item_count == 0) {
for (int i = 0; i != cloud_show.size(); ++i) {
setCloudAlpha(cloud_show[i], 255);
}
}
else {
for (int i = 0; i != selected_item_count; ++i) {
int cloud_id = ui->dataTree->indexOfTopLevelItem(itemList[i]);
setCloudAlpha(cloud_show[cloud_id], 255);
}
// output window
setConsole("Hide", "");
}
updatePointcloud();
ui->openGLWidget->update();
}
void ICPPCL::pointHide()
{
QList<QTreeWidgetItem*> itemList = ui->dataTree->selectedItems();
int selected_item_count = ui->dataTree->selectedItems().size();
if (selected_item_count == 0)
{
for (int i = 0; i != cloud_show.size(); ++i)
{
setCloudAlpha(cloud_show[i], 0);
}
}
else
{
for (int i = 0; i != selected_item_count; ++i)
{
int cloud_id = ui->dataTree->indexOfTopLevelItem(itemList[i]);
setCloudAlpha(cloud_show[cloud_id], 0);
}
setConsole("Hide", "");
}
updatePointcloud();
ui->openGLWidget->update();
}
std::string ICPPCL::getFileName(std::string file_name)
{
std::string subname;
for (auto i = file_name.end() - 1; *i != '/'; --i)
{
subname.insert(subname.begin(), *i);
}
return subname;
}
void ICPPCL::print4x4MatrixT(const Eigen::Matrix4d &matrix)
{
printf("Rotation matrix : \n");
printf(" | %6.3f %6.3f %6.3f | \n", matrix(0, 0), matrix(0, 1), matrix(0, 2));
printf("R = | %6.3f %6.3f %6.3f | \n", matrix(1, 0), matrix(1, 1), matrix(1, 2));
printf(" | %6.3f %6.3f %6.3f | \n", matrix(2, 0), matrix(2, 1), matrix(2, 2));
printf("Translation vector :\n");
printf("t = < %6.3f, %6.3f, %6.3f >\n\n", matrix(0, 3), matrix(1, 3), matrix(2, 3));
}
void ICPPCL::setConsole(QString operation, QString detail)
{
int rows = ui->consoleTable->rowCount();
ui->consoleTable->setRowCount(++rows);
QDateTime time = QDateTime::currentDateTime(); //Get the current time of the system
QString t_str = time.toString("MM-dd hh:mm:ss"); // set display format
ui->consoleTable->setItem(rows - 1, 0, new QTableWidgetItem(t_str));
ui->consoleTable->setItem(rows - 1, 1, new QTableWidgetItem(operation));
ui->consoleTable->setItem(rows - 1, 2, new QTableWidgetItem(detail));
ui->consoleTable->scrollToBottom();
}
void ICPPCL::keyboardEventOccured(const pcl::visualization::KeyboardEvent &event, void *non)
{
if (event.getKeySym() == "space" && event.keyDown())
next = true;
}
void ICPPCL::updatePointcloud()
{
viewer->removeAllPointClouds();
for (int i = 0; i != cloud_show.size(); ++i)
{
viewer->addPointCloud(cloud_show[i], "cloud " + QString::number(i).toStdString());
if (nor_show.find(i) != nor_show.end()) {
viewer->addPointCloudNormals<PointT, pcl::Normal>(cloud_show[i], nor_show[i], 10, 0.02, "normals");
}
viewer->updatePointCloud(cloud_show[i], "cloud " + QString::number(i).toStdString());
}
updateDataTree();
}
void ICPPCL::updateDataTree()
{
ui->dataTree->clear();
//Update resource management tree
for (int i = 0; i != cloud_show.size(); ++i)
{
QTreeWidgetItem *cloudName = new QTreeWidgetItem(QStringList() << QString::fromLocal8Bit(std::to_string(i).c_str()));
cloudName->setIcon(0, QIcon(":/Resources/images/icon.png"));
ui->dataTree->addTopLevelItem(cloudName);
}
}
void ICPPCL::setCloudAlpha(PointCloudT::Ptr &cloud, unsigned int a)
{
for (size_t i = 0; i < cloud->size(); ++i)
{
cloud->points[i].a = a;
}
}
void ICPPCL::setCloudRGB(PointCloudT::Ptr &cloud, unsigned int r, unsigned int g, unsigned int b)
{
for (size_t i = 0; i < cloud->size(); ++i)
{
cloud->points[i].r = r;
cloud->points[i].g = g;
cloud->points[i].b = b;
cloud->points[i].a = 255;
}
}
// Sampling
// uniform sampling
void ICPPCL::uniformSampling()
{
if (cloud_show.size() < 1)
{
setConsole("transformLastCloud", " insufficient point cloud.");
return ;
}
PointCloudT::Ptr cloud_in(cloud_show.back());
PointCloudT::Ptr us_cloud(new PointCloudT);
pcl::UniformSampling<pcl::PointXYZRGBA> us;
us.setInputCloud(cloud_in);
us.setRadiusSearch(0.005f);
us.filter(*us_cloud);
Eigen::Affine3f transformation = Eigen::Affine3f::Identity(); //创建平移旋转变换矩阵。本例只演示平移点云。
transformation.translation() << 0.2, 0.0, 0.0; //沿x轴方向平移10m
pcl::transformPointCloud(*us_cloud, *us_cloud, transformation); //平移点云
cloud_show.push_back(us_cloud);
updatePointcloud();
ui->openGLWidget->update();
}
//Features
void ICPPCL::normalVector()
{
QTreeWidgetItem* curItem = ui->dataTree->currentItem();
if (curItem == NULL) return;
int id = ui->dataTree->indexOfTopLevelItem(curItem);
pcl::NormalEstimationOMP<PointT, pcl::Normal> n;
pcl::PointCloud<pcl::Normal>::Ptr normal(new pcl::PointCloud<pcl::Normal>);
pcl::search::KdTree<PointT>::Ptr tree(new pcl::search::KdTree<PointT>());
n.setNumberOfThreads(10);
n.setInputCloud(cloud_show[id]);
n.setSearchMethod(tree);
n.setKSearch(10);
n.compute(*normal);
nor_show[id] = normal;
viewer->addPointCloudNormals<PointT, pcl::Normal>(cloud_show[id], normal, 20, 0.02, "normals");
//updatePointcloud();
//updateNormals();
ui->openGLWidget->update();
}
//pfh
void ICPPCL::pfh()
{
QTreeWidgetItem* curItem = ui->dataTree->currentItem();
if (curItem == NULL) return;
int id = ui->dataTree->indexOfTopLevelItem(curItem);
pcl::NormalEstimationOMP<PointT, pcl::Normal> n;
pcl::PointCloud<pcl::Normal>::Ptr normal(new pcl::PointCloud<pcl::Normal>);
pcl::search::KdTree<PointT>::Ptr tree(new pcl::search::KdTree<PointT>());
n.setNumberOfThreads(10);
n.setInputCloud(cloud_show[id]);
n.setSearchMethod(tree);
n.setKSearch(10);
n.compute(*normal);
pcl::PFHEstimation<pcl::PointXYZRGBA, pcl::Normal, pcl::PFHSignature125> pfh;
pfh.setInputCloud(cloud_show[id]);
pfh.setInputNormals(normal);
pcl::search::KdTree<pcl::PointXYZRGBA>::Ptr tree2(new pcl::search::KdTree<pcl::PointXYZRGBA>());
pfh.setSearchMethod(tree2);
pcl::PointCloud<pcl::PFHSignature125>::Ptr pfh_fe_ptr(new pcl::PointCloud<pcl::PFHSignature125>());
pfh.setKSearch(5);
pfh.compute(*pfh_fe_ptr);
cout << "pfh feature size : " << pfh_fe_ptr->points.size() << endl;
pcl::visualization::PCLPlotter plotter;
plotter.addFeatureHistogram(*pfh_fe_ptr, 300);
plotter.plot();
}
//fpfh
ICPPCL::~ICPPCL()
{
delete ui;
}