forked from luolaihua/point-cloud-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pclvisualizer.cpp
1852 lines (1619 loc) · 59.6 KB
/
pclvisualizer.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma execution_character_set("utf-8")
#include "pclvisualizer.h"
#include "./ui_pclvisualizer.h"
#include <QColor>
#include <QColorDialog>
#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QPainter>
#include <QTextList>
#include <QTextStream>
PCLVisualizer::PCLVisualizer(QWidget* parent)
: QMainWindow(parent)
, isCloud2(true) // 设置初始点大小
, point_size(5) // 设置初始背景颜色
, ui(new Ui::PCLVisualizer) //是否默认开启RGBA显示点云
, isRBGA(true) // 是否显示第二个点云
, bgColor(0, 0, 50) // 初始化顺序要和声明的顺序一致
{
ui->setupUi(this);
// cout << "INIT" << endl;
initPCV();
// //创建动作,工具栏以及菜单栏
createActions();
// createMenus();
// 创建工具栏
createToolBars();
//初始化点云数据
initPointCloud();
// 给QVTK配置PCLViewer显示
viewer_.reset(new pcl::visualization::PCLVisualizer("viewer", false));
//设置背景颜色
viewer_->setBackgroundColor(double(bgColor.red()) / 255,
double(bgColor.green()) / 255,
double(bgColor.blue()) / 255);
ui->qvtkWidget->SetRenderWindow(viewer_->getRenderWindow());
viewer_->setupInteractor(ui->qvtkWidget->GetInteractor(),
ui->qvtkWidget->GetRenderWindow());
ui->qvtkWidget->update();
connectSS();
// Color the randomly generated cloud
// 以随机颜色填充点云
colorCloudDistances();
if (isRBGA) {
viewer_->addPointCloud(cloudRGBA_, "cloud");
} else {
viewer_->addPointCloud(cloud_, "cloud");
}
////viewer_->addPointCloud(cloud_, "cloud");
// viewer_->addPointCloud(cloudRGBA_, "cloud");
viewer_->setPointCloudRenderingProperties(
pcl::visualization::PCL_VISUALIZER_POINT_SIZE, point_size);
// viewer_->addCoordinateSystem(1);
viewer_->resetCamera();
viewer_->setLookUpTableID("cloud");
ui->qvtkWidget->update();
showLogItem("PCV 系统", "系统初始化完成。");
}
PCLVisualizer::~PCLVisualizer()
{
delete ui;
}
void
PCLVisualizer::initPCV()
{
//设置窗口名称
QString str = "PointCloudViewer";
this->setWindowTitle(str);
showLogItem("PCV 系统", "系统正在初始化...");
logStr = "主窗口位置已还原: " + QString("X-Y-Width-Height(%1,%2,%3,%4)")
.arg(this->x())
.arg(this->y())
.arg(this->width())
.arg(this->height());
showLogItem("PCV 窗口", logStr);
//写ini文件,记录当前窗口位置和大小:
QString wstrFilePath =
qApp->applicationDirPath() + "/setting.ini"; // .ini放在工程源文件目录下
settings = new QSettings(
wstrFilePath, QSettings::IniFormat); //用QSetting获取ini文件中的数据
// settings->clear(); //清空当前配置文件中的内容
}
void
PCLVisualizer::showLogItem(QString item, QString info)
{
//--------------------LOG--------------------------
logStr = "[" + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") +
"] " + " [ " + item + " ] " + info;
logList.push_back(logStr);
ui->logList->addItem(logStr);
//--------------------LOG--------------------------
}
void
PCLVisualizer::saveSetting(QString key, QString value)
{
//写ini文件,记录当前窗口位置和大小:
QString wstrFilePath =
qApp->applicationDirPath() + "/setting.ini"; // .ini放在工程源文件目录下
QSettings* settings = new QSettings(
wstrFilePath, QSettings::IniFormat); //用QSetting获取ini文件中的数据
// settings->clear(); //清空当前配置文件中的内容
settings->setValue(key, value);
settings->sync();
}
QVariant
PCLVisualizer::getSetting(QString name)
{
return settings->value(name);
}
void
PCLVisualizer::createActions()
{
//添加点云
// addCloudAction =
// new QAction(QIcon(":/images/files/cloud.png"), "添加点云", this);
// addCloudAction->setShortcut(tr("Ctrl+O")); //(b)
// addCloudAction->setStatusTip(tr("添加点云")); //(c)
//新建工作台
newWorkStationAction =
new QAction(QIcon(":/images/files/add.png"), tr("new"), this);
newWorkStationAction->setShortcut(tr("Ctrl+N"));
newWorkStationAction->setStatusTip(tr("new"));
//打开点云文件
newCloudAction =
new QAction(QIcon(":/images/files/new2.png"), tr("打开点云文件"), this);
// exitAction->setShortcut(tr("Ctrl+Q"));
newCloudAction->setStatusTip(tr("打开点云文件"));
//复制点云
copyCloudAction =
new QAction(QIcon(":/images/files/copy.png"), tr("复制点云"), this);
copyCloudAction->setShortcut(tr("Ctrl+C"));
copyCloudAction->setStatusTip(tr("复制点云"));
//剪切点云
cutCloudAction =
new QAction(QIcon(":/images/files/cut.png"), tr("剪切点云"), this);
cutCloudAction->setShortcut(tr("Ctrl+X"));
cutCloudAction->setStatusTip(tr("剪切点云"));
//粘贴点云
pasteCloudAction =
new QAction(QIcon(":/images/files/paste.png"), tr("粘贴点云"), this);
pasteCloudAction->setShortcut(tr("Ctrl+V"));
pasteCloudAction->setStatusTip(tr("粘贴点云"));
//查找点云文件
searchCloudAction =
new QAction(QIcon(":/images/files/search.png"), tr("查找点云文件"), this);
searchCloudAction->setShortcut(tr("Ctrl+F"));
searchCloudAction->setStatusTip(tr("查找点云文件"));
//导出点云至PCD文件
exportCloud2PCDAction = new QAction(
QIcon(":/images/files/pointCloud.png"), tr("导出点云至PCD文件"), this);
exportCloud2PCDAction->setStatusTip(tr("导出点云至PCD文件"));
//导出点云至PLY文件
exportCloud2PLYAction = new QAction(
QIcon(":/images/files/cloud2.png"), tr("导出点云至PLY文件"), this);
exportCloud2PLYAction->setStatusTip(tr("导出点云至PLY文件"));
//导出点云至CSV文件
export2CSVAction =
new QAction(QIcon(":/images/files/CSV.png"), tr("导出点云至CSV文件"), this);
export2CSVAction->setStatusTip(tr("导出点云至CSV文件"));
//导出点云至TXT文件
export2TXTAction =
new QAction(QIcon(":/images/files/txt.png"), tr("导出点云至TXT文件"), this);
export2TXTAction->setStatusTip(tr("导出点云至TXT文件"));
//收藏点云文件
starCloudAction =
new QAction(QIcon(":/images/files/star.png"), tr("收藏点云文件"), this);
starCloudAction->setStatusTip(tr("收藏点云文件"));
//导出屏幕截图
snapShotAction =
new QAction(QIcon(":/images/files/snapshot.png"), tr("导出屏幕截图"), this);
snapShotAction->setStatusTip(tr("导出屏幕截图"));
//离群点移除
outliersRemoveAction = new QAction(
QIcon(":/images/algorithm/KMeans.png"), tr("outliersRemove"), this);
outliersRemoveAction->setStatusTip(tr("outliersRemove"));
//滤波平滑
filterAction =
new QAction(QIcon(":/images/algorithm/filter.png"), tr("滤波平滑"), this);
filterAction->setStatusTip(tr("滤波平滑"));
//点云下采样
downSampleAction =
new QAction(QIcon(":/images/algorithm/density.png"), "downSampling", this);
downSampleAction->setStatusTip(tr("downSampling"));
//点云拼接
cloudSpliceAction =
new QAction(QIcon(":/images/algorithm/pingjie.png"), "点云拼接", this);
cloudSpliceAction->setStatusTip(tr("点云拼接"));
//点云直方图
HistogramAction =
new QAction(QIcon(":/images/algorithm/Histogram.png"), "Histogram", this);
HistogramAction->setStatusTip(tr("Histogram"));
//表面重建
surfaceAction =
new QAction(QIcon(":/images/algorithm/matrix.png"), "surface", this);
surfaceAction->setStatusTip(tr("surface"));
//点云配准
alignAction =
new QAction(QIcon(":/images/algorithm/DBSCAN.png"), "点云配准", this);
alignAction->setStatusTip(tr("点云配准"));
// MLS细化
MLSAction =
new QAction(QIcon(":/images/algorithm/nihe.png"), "MLS细化", this);
MLSAction->setStatusTip(tr("MLS细化"));
}
void
PCLVisualizer::createMenus()
{}
void
PCLVisualizer::createToolBars()
{
//点云文件工具栏
fileTool = addToolBar("cloudFile");
fileTool->addAction(newWorkStationAction);
// fileTool->addAction(addCloudAction);
fileTool->addAction(ui->actionload_point_cloud);
fileTool->addAction(newCloudAction);
fileTool->addAction(copyCloudAction);
fileTool->addAction(cutCloudAction);
fileTool->addAction(pasteCloudAction);
fileTool->addSeparator();
fileTool->addAction(exportCloud2PCDAction);
fileTool->addAction(exportCloud2PLYAction);
fileTool->addAction(export2CSVAction);
fileTool->addAction(export2TXTAction);
fileTool->addAction(ui->actionExportLog);
fileTool->addAction(snapShotAction);
fileTool->addSeparator();
fileTool->addAction(starCloudAction);
fileTool->addAction(searchCloudAction);
fileTool->addAction(ui->actionBGColor);
//算法工具栏
algorithmTool = addToolBar("algorithm");
algorithmTool->addAction(ui->actionbestRemoval);
algorithmTool->addAction(ui->actionbestFiltering);
algorithmTool->addAction(ui->actionbestKeypoint);
algorithmTool->addAction(ui->actionbestRegistration);
algorithmTool->addAction(ui->actionbestSurface);
algorithmTool->addAction(MLSAction);
algorithmTool->addAction(HistogramAction);
}
void
PCLVisualizer::test()
{
qDebug() << "Hello World!";
}
void
PCLVisualizer::initPointCloud()
{
// Setup the cloud pointer
cloud_.reset(new PointCloudT);
cloud.reset(new PointCloudT);
cloud_noise.reset(new PointCloudT);
cloud_filtered.reset(new PointCloudT);
cloud_filtered_guass.reset(new PointCloudT);
cloud_filtered_guass_down.reset(new PointCloudT);
cloud_filtered_out.reset(new PointCloudT);
cloud_in.reset(new PointCloudT);
cloud_tr.reset(new PointCloudT);
cloud_RE.reset(new PointCloudT);
cloudRGBA_.reset(new PointCloudTRGBA);
// The number of points in the cloud
cloud_->resize(1000);
cloudRGBA_->resize(1000);
// Fill the cloud with random points
for (size_t i = 0; i < cloud_->points.size(); ++i) {
cloud_->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud_->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud_->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
}
// 获取点云内的最大点和最小点
pcl::getMinMax3D(*cloud_, p_min, p_max);
maxLen = getMaxValue(p_max, p_min);
//拷贝一份给RGBA点云
pcl::copyPointCloud(*cloud_, *cloudRGBA_);
showLogItem("PCV 主窗口", "点云初始化完成");
}
//连接信号槽
void
PCLVisualizer::connectSS()
{
connect(ui->pushButton_inc,
&QPushButton::clicked,
this,
&PCLVisualizer::IncPointSize);
connect(ui->actionload_point_cloud,
&QAction::triggered,
this,
&PCLVisualizer::loadPCDFile);
connect(ui->actionsave_point_cloud,
&QAction::triggered,
this,
&PCLVisualizer::savePCDFile);
connect(ui->actionCoordinateSystem,
&QAction::triggered,
this,
&PCLVisualizer::AddCoordinateSystem);
// Connect "Load" and "Save" buttons and their functions
connect(ui->pushButton_dec,
&QPushButton::clicked,
this,
&PCLVisualizer::DecPointSize);
// Connect X,Y,Z radio buttons and their functions
connect(ui->radioButton_x,
&QRadioButton::clicked,
this,
&PCLVisualizer::chooseAxis);
connect(ui->radioButton_y,
&QRadioButton::clicked,
this,
&PCLVisualizer::chooseAxis);
connect(ui->radioButton_z,
&QRadioButton::clicked,
this,
&PCLVisualizer::chooseAxis);
connect(ui->radioButton_BlueRed,
&QRadioButton::clicked,
this,
&PCLVisualizer::chooseColorMode);
connect(ui->radioButton_GreenMagenta,
&QRadioButton::clicked,
this,
&PCLVisualizer::chooseColorMode);
connect(ui->radioButton_WhiteRed,
&QRadioButton::clicked,
this,
&PCLVisualizer::chooseColorMode);
connect(ui->radioButton_GreyRed,
&QRadioButton::clicked,
this,
&PCLVisualizer::chooseColorMode);
connect(ui->radioButton_Rainbow,
&QRadioButton::clicked,
this,
&PCLVisualizer::chooseColorMode);
connect(ui->radioButton_others,
&QRadioButton::clicked,
this,
&PCLVisualizer::chooseColorMode);
//增加新工作台功能
connect(newWorkStationAction,
&QAction::triggered,
this,
&PCLVisualizer::newWorkStation);
}
void
PCLVisualizer::savePCDFile()
{
QString filename =
QFileDialog::getSaveFileName(this,
tr("Open point cloud"),
"/home/",
tr("Point cloud data(*.pcd *.ply)"));
PCL_INFO("File chosen: %s\n", filename.toStdString().c_str());
if (filename.isEmpty())
return;
int return_status;
if (filename.endsWith(".pcd", Qt::CaseInsensitive))
return_status = pcl::io::savePCDFileBinary(filename.toStdString(), *cloud_);
else if (filename.endsWith(".ply", Qt::CaseInsensitive))
return_status = pcl::io::savePLYFileBinary(filename.toStdString(), *cloud_);
else {
filename.append(".ply");
return_status = pcl::io::savePLYFileBinary(filename.toStdString(), *cloud_);
}
if (return_status != 0) {
PCL_ERROR("Error writing point cloud %s\n", filename.toStdString().c_str());
return;
}
}
void
PCLVisualizer::loadPCDFile()
{
QString fileFormat, fileName, fileBaseName, pointCount, filePath, fileSuffix,
lastPath;
//读取文件名
//记住上一次加载的路径
lastPath = getSetting("FilePath/lastPath").toString();
QString filePathWithName =
QFileDialog::getOpenFileName(this,
tr("Open point cloud"),
lastPath,
tr("Point cloud data (*.pcd *.ply)"));
QFileInfo fileInfo;
fileInfo = QFileInfo(filePathWithName);
//文件名
fileName = fileInfo.fileName();
//文件后缀
fileSuffix = fileInfo.suffix();
//绝对路径
filePath = fileInfo.absolutePath();
fileBaseName = fileInfo.baseName();
//qDebug() << fileName << endl
// << fileSuffix << endl
// << filePath << endl
// << fileInfo.baseName() << endl
// << fileInfo.completeBaseName();
showLogItem("点云文件选择", filePath);
// PCL_INFO("File chosen: %s\n", filePathWithName.toStdString().c_str());
PointCloudT::Ptr cloud_tmp(new PointCloudT);
if (filePathWithName.isEmpty()) {
showLogItem("文件加载失败", filePathWithName);
return;
}
showLogItem("文件加载成功", filePathWithName);
// 将当前文件路径保存,下次使用
saveSetting("FilePath/lastPath", filePathWithName);
//判断文件类型然后加载点云
int return_status;
if (filePathWithName.endsWith(".pcd", Qt::CaseInsensitive)) {
return_status =
pcl::io::loadPCDFile(filePathWithName.toStdString(), *cloud_tmp);
fileFormat = "PCD";
} else {
return_status =
pcl::io::loadPLYFile(filePathWithName.toStdString(), *cloud_tmp);
fileFormat = "PLY";
}
showLogItem("点云加载中", "文件格式为:" + fileFormat);
//判断是否加载成功
if (return_status != 0) {
PCL_ERROR("Error reading point cloud %s\n",
filePathWithName.toStdString().c_str());
showLogItem("点云加载中", fileName+" 文件读取失败。");
return;
}
PCL_INFO("file has loaded\n");
showLogItem("点云加载完成", fileFormat);
// If point cloud contains NaN values, remove them before updating the
// visualizer point cloud
// True if no points are invalid (e.g., have NaN or Inf values in any of
// their floating point fields).
if (cloud_tmp->is_dense) {
pcl::copyPointCloud(*cloud_tmp, *cloud_);
} else {
PCL_WARN("Cloud is not dense! Non finite points will be removed\n");
std::vector<int> vec;
pcl::removeNaNFromPointCloud(*cloud_tmp, *cloud_, vec);
}
//将当前点云拷贝给RGBA点云
pcl::copyPointCloud(*cloud_, *cloudRGBA_);
showLogItem("点云信息", fileName + QString(" 点云数量: %1").arg(cloud_->points.size()));
qDebug() << "The number of points :" << cloud_->points.size();
//更新点云属性信息
ui->fileFormatEdt->setText(fileFormat);
ui->fileNameEdt->setText(fileInfo.baseName());
ui->pointCountEdt->setText(QString("%1").arg(cloud_->points.size()));
QString cloudFile = fileName + " [" + filePath + "]";
QListWidgetItem* item = new QListWidgetItem;
// item->setBackgroundColor(QColor(220, 230, 250));
item->setBackground(QBrush(QColor(220, 230, 250)));
item->setData(Qt::DisplayRole, cloudFile);
item->setData(Qt::CheckStateRole, Qt::Checked);
ui->filesList->addItem(item);
//初始化 A
for (PointCloudTRGBA::iterator cloud_it = cloudRGBA_->begin();
cloud_it != cloudRGBA_->end();
++cloud_it) {
// qDebug() << cloud_it->_PointXYZRGBA::r << " " <<
// cloud_it->_PointXYZRGBA::g
// << " " << cloud_it->_PointXYZRGBA::b << " "
// << cloud_it->_PointXYZRGBA::a;
cloud_it->_PointXYZRGBA::a = 255;
}
pcl::getMinMax3D(*cloud_, p_min, p_max);
maxLen = getMaxValue(p_max, p_min);
colorCloudDistances();
if (isRBGA) {
viewer_->updatePointCloud(cloudRGBA_, "cloud");
} else {
viewer_->updatePointCloud(cloud_, "cloud");
}
viewer_->resetCamera();
viewer_->setPointCloudRenderingProperties(
pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1);
ui->qvtkWidget->update();
}
void
PCLVisualizer::loadPLYFile()
{
QString fileFormat, fileName, fileBaseName, pointCount, filePath, fileSuffix;
//读取文件名
filePathWithName =
QFileDialog::getOpenFileName(this,
tr("Open point cloud"),
"E:/BaiduNetdiskWorkspace/Paper-of-Luo/PCD",
tr("Point cloud data (*.pcd *.ply)"));
QFileInfo fileInfo;
fileInfo = QFileInfo(filePathWithName);
//文件名
fileName = fileInfo.fileName();
//文件后缀
fileSuffix = fileInfo.suffix();
//绝对路径
filePath = fileInfo.absolutePath();
fileBaseName = fileInfo.baseName();
QString cloudFile = fileName + " [" + filePath + "]";
QListWidgetItem* item = new QListWidgetItem;
// item->setBackgroundColor(QColor(220, 230, 250));
item->setBackground(QBrush(QColor(220, 230, 250)));
item->setData(Qt::DisplayRole, cloudFile);
item->setData(Qt::CheckStateRole, Qt::Checked);
ui->filesList->addItem(item);
}
void
PCLVisualizer::chooseAxis()
{
if (color_mode_ == 5)
return;
// Only 1 of the button can be checked at the time (mutual exclusivity) in a
// group of radio buttons
if (ui->radioButton_x->isChecked()) {
PCL_INFO("x filtering chosen\n");
filtering_axis_ = 0;
} else if (ui->radioButton_y->isChecked()) {
PCL_INFO("y filtering chosen\n");
filtering_axis_ = 1;
} else {
PCL_INFO("z filtering chosen\n");
filtering_axis_ = 2;
}
colorCloudDistances();
if (isRBGA) {
viewer_->updatePointCloud(cloudRGBA_, "cloud");
} else {
viewer_->updatePointCloud(cloud_, "cloud");
}
ui->qvtkWidget->update();
}
void
PCLVisualizer::chooseColorMode()
{
// Only 1 of the button can be checked at the time (mutual exclusivity) in a
// group of radio buttons
if (ui->radioButton_BlueRed->isChecked()) {
PCL_INFO("Blue -> Red LUT chosen\n");
color_mode_ = 0;
} else if (ui->radioButton_GreenMagenta->isChecked()) {
PCL_INFO("Green -> Magenta LUT chosen\n");
color_mode_ = 1;
} else if (ui->radioButton_WhiteRed->isChecked()) {
PCL_INFO("White -> Red LUT chosen\n");
color_mode_ = 2;
} else if (ui->radioButton_GreyRed->isChecked()) {
PCL_INFO("Grey / Red LUT chosen\n");
color_mode_ = 3;
} else if (ui->radioButton_Rainbow->isChecked()) {
PCL_INFO("Rainbow LUT chosen\n");
color_mode_ = 4;
} else {
PCL_INFO("Full color chosen\n");
color_mode_ = 5;
QColor c = QColorDialog::getColor(Qt::red);
if (c.isValid()) {
point_color = c;
qDebug() << "RBG: " << c.red() << " " << c.green() << " " << c.blue();
}
}
colorCloudDistances();
if (isRBGA) {
viewer_->updatePointCloud(cloudRGBA_, "cloud");
} else {
viewer_->updatePointCloud(cloud_, "cloud");
}
ui->qvtkWidget->update();
}
void
PCLVisualizer::IncPointSize()
{
// setValue函数会自动调用 SpinBox的触发动作
ui->pointSizeEdt->setValue(++point_size);
}
void
PCLVisualizer::DecPointSize()
{
if (point_size == 1)
return;
ui->pointSizeEdt->setValue(--point_size);
}
void
PCLVisualizer::AddCoordinateSystem()
{
QString str = ui->actionCoordinateSystem->text();
if (str.compare("CoordinateSystem [OFF]") == 0) {
qDebug() << str;
ui->actionCoordinateSystem->setText("CoordinateSystem [ON]");
viewer_->addCoordinateSystem();
} else {
ui->actionCoordinateSystem->setText("CoordinateSystem [OFF]");
viewer_->removeCoordinateSystem();
}
ui->qvtkWidget->update();
}
//软件关闭时动作
void
PCLVisualizer::closeEvent(QCloseEvent* event)
{
//写ini文件,记录当前窗口位置和大小:
QString wstrFilePath =
qApp->applicationDirPath() + "/setting.ini"; // .ini放在工程源文件目录下
QSettings* settings = new QSettings(
wstrFilePath, QSettings::IniFormat); //用QSetting获取ini文件中的数据
// settings->clear(); //清空当前配置文件中的内容
settings->setValue("WindowGeometry/x", this->x());
settings->setValue("WindowGeometry/y", this->y());
settings->setValue("WindowGeometry/width", this->width());
settings->setValue("WindowGeometry/height", this->height());
qDebug() << "Position is right:" << this->x() << " " << this->y() << " "
<< this->width() << " " << this->height();
}
void
PCLVisualizer::colorCloudDistances()
{
double min, max;
switch (filtering_axis_) {
case 0: // x
min = cloud_->points[0].x;
max = cloud_->points[0].x;
break;
case 1: // y
min = cloud_->points[0].y;
max = cloud_->points[0].y;
break;
default: // z
min = cloud_->points[0].z;
max = cloud_->points[0].z;
break;
}
// Search for the minimum/maximum
for (PointCloudTRGBA::iterator cloud_it = cloudRGBA_->begin();
cloud_it != cloudRGBA_->end();
++cloud_it) {
switch (filtering_axis_) {
case 0: // x
if (min > cloud_it->x)
min = cloud_it->x;
if (max < cloud_it->x)
max = cloud_it->x;
break;
case 1: // y
if (min > cloud_it->y)
min = cloud_it->y;
if (max < cloud_it->y)
max = cloud_it->y;
break;
default: // z
if (min > cloud_it->z)
min = cloud_it->z;
if (max < cloud_it->z)
max = cloud_it->z;
break;
}
}
// Compute LUT scaling to fit the full histogram spectrum
double lut_scale = 255.0 / (max - min); // max is 255, min is 0
if (min ==
max) // In case the cloud is flat on the chosen direction (x,y or z)
lut_scale = 1.0; // Avoid rounding error in boost
for (PointCloudTRGBA::iterator cloud_it = cloudRGBA_->begin();
cloud_it != cloudRGBA_->end();
++cloud_it) {
int value;
switch (filtering_axis_) {
case 0: // x
value = boost::math::iround(
(cloud_it->x - min) *
lut_scale); // Round the number to the closest integer
break;
case 1: // y
value = boost::math::iround((cloud_it->y - min) * lut_scale);
break;
default: // z
value = boost::math::iround((cloud_it->z - min) * lut_scale);
break;
}
// Apply color to the cloud
switch (color_mode_) {
case 0:
// Blue (= min) -> Red (= max)
cloud_it->r = value;
cloud_it->g = 0;
cloud_it->b = 255 - value;
break;
case 1:
// Green (= min) -> Magenta (= max)
cloud_it->r = value;
cloud_it->g = 255 - value;
cloud_it->b = value;
break;
case 2:
// White (= min) -> Red (= max)
cloud_it->r = 255;
cloud_it->g = 255 - value;
cloud_it->b = 255 - value;
break;
case 3:
// Grey (< 128) / Red (> 128)
if (value > 128) {
cloud_it->r = 255;
cloud_it->g = 0;
cloud_it->b = 0;
} else {
cloud_it->r = 128;
cloud_it->g = 128;
cloud_it->b = 128;
}
break;
case 5:
cloud_it->r = point_color.red();
cloud_it->g = point_color.green();
cloud_it->b = point_color.blue();
break;
default:
// Blue -> Green -> Red (~ rainbow)
cloud_it->r =
value > 128 ? (value - 128) * 2 : 0; // r[128] = 0, r[255] = 255
cloud_it->g =
value < 128
? 2 * value
: 255 - ((value - 128) * 2); // g[0] = 0, g[128] = 255, g[255] = 0
cloud_it->b =
value < 128 ? 255 - (2 * value) : 0; // b[0] = 255, b[128] = 0
}
}
}
void
PCLVisualizer::on_actionUp_triggered()
{
if (!cloud_->empty()) {
viewer_->setCameraPosition(0.5 * (p_min.x + p_max.x),
0.5 * (p_min.y + p_max.y),
p_max.z + 2 * maxLen,
0.5 * (p_min.x + p_max.x),
0.5 * (p_min.y + p_max.y),
p_max.z,
0,
1,
0);
ui->qvtkWidget->update();
}
}
void
PCLVisualizer::on_actionBottom_triggered()
{
if (!cloud_->empty()) {
viewer_->setCameraPosition(0.5 * (p_min.x + p_max.x),
0.5 * (p_min.y + p_max.y),
p_min.z - 2 * maxLen,
0.5 * (p_min.x + p_max.x),
0.5 * (p_min.y + p_max.y),
p_min.z,
0,
1,
0);
ui->qvtkWidget->update();
}
}
void
PCLVisualizer::on_actionFront_triggered()
{
if (!cloud_->empty()) {
viewer_->setCameraPosition(0.5 * (p_min.x + p_max.x),
p_min.y - 2 * maxLen,
0.5 * (p_min.z + p_max.z),
0.5 * (p_min.x + p_max.x),
p_min.y,
0.5 * (p_min.z + p_max.z),
0,
0,
1);
ui->qvtkWidget->update();
}
}
void
PCLVisualizer::on_actionBack_triggered()
{
if (!cloud_->empty()) {
viewer_->setCameraPosition(0.5 * (p_min.x + p_max.x),
p_max.y + 2 * maxLen,
0.5 * (p_min.z + p_max.z),
0.5 * (p_min.x + p_max.x),
p_min.y,
0.5 * (p_min.z + p_max.z),
0,
0,
1);
ui->qvtkWidget->update();
}
}
void
PCLVisualizer::on_actionLeft_triggered()
{
if (!cloud_->empty()) {
viewer_->setCameraPosition(p_min.x - 2 * maxLen,
0.5 * (p_min.y + p_max.y),
0.5 * (p_min.z + p_max.z),
p_max.x,
0.5 * (p_min.y + p_max.y),
0.5 * (p_min.z + p_max.z),
0,
0,
1);
ui->qvtkWidget->update();
}
}
void
PCLVisualizer::on_actionRight_triggered()
{
if (!cloud_->empty()) {
viewer_->setCameraPosition(p_max.x + 2 * maxLen,
0.5 * (p_min.y + p_max.y),
0.5 * (p_min.z + p_max.z),
p_max.x,
0.5 * (p_min.y + p_max.y),
0.5 * (p_min.z + p_max.z),
0,
0,
1);
ui->qvtkWidget->update();
}
}
double
PCLVisualizer::getMinValue(PointT p1, PointT p2)
{
double min = 0;
if (p1.x - p2.x > p1.y - p2.y) {
min = p1.y - p2.y;
} else {
min = p1.x - p2.x;
}
if (min > p1.z - p2.z) {
min = p1.z - p2.z;
}
return min;
}
double
PCLVisualizer::getMaxValue(PointT p1, PointT p2)
{
double max = 0;
if (p1.x - p2.x > p1.y - p2.y) {
max = p1.x - p2.x;
} else {
max = p1.y - p2.y;
}
if (max < p1.z - p2.z) {
max = p1.z - p2.z;
}
return max;
}
void
PCLVisualizer::openProgressDlg(int num = 500)
{
//创建一个进度对话框
QProgressDialog* progressDialog = new QProgressDialog(this);
QFont font("ZYSong18030", 12);
progressDialog->setFont(font);
progressDialog->setWindowModality(Qt::WindowModal); //(d)
progressDialog->setMinimumDuration(5); //(e)
progressDialog->setWindowTitle(tr("Please Wait")); //(f)
progressDialog->setLabelText(tr("Processing...")); //(g)
progressDialog->setCancelButtonText(tr("Cancel")); //(h)
progressDialog->setRange(0, num); //设置进度对话框的步进范围
for (int i = 1; i < num + 1; i++) {
Sleep(10);
progressDialog->setValue(i); //(i)
if (progressDialog->wasCanceled()) //(j)
return;
}
}
void
PCLVisualizer::updateCloudInfo()
{
// TODO 暂时只更新点云数量
ui->pointCountEdt->setText(QString::number(cloud_->points.size()));
}
void
PCLVisualizer::best_filter()
{
pcl::console::TicToc time;
//创建一个进度对话框
QProgressDialog* progressDialog = new QProgressDialog(this);
QFont font("ZYSong18030", 12);
progressDialog->setFont(font);