forked from nightn/CloudViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudviewer.cpp
1478 lines (1292 loc) · 47.4 KB
/
cloudviewer.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
#include "cloudviewer.h"
CloudViewer::CloudViewer(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
/***** Slots connection of QMenuBar and QToolBar *****/
// File (connect)
QObject::connect(ui.openAction, &QAction::triggered, this, &CloudViewer::open);
QObject::connect(ui.addAction, &QAction::triggered, this, &CloudViewer::add);
QObject::connect(ui.clearAction, &QAction::triggered, this, &CloudViewer::clear);
QObject::connect(ui.saveAction, &QAction::triggered, this, &CloudViewer::save);
QObject::connect(ui.saveBinaryAction, &QAction::triggered, this, &CloudViewer::saveBinary);
QObject::connect(ui.changeAction, &QAction::triggered, this, &CloudViewer::change);
QObject::connect(ui.exitAction, &QAction::triggered, this, &CloudViewer::exit);
// Display (connect)
QObject::connect(ui.pointcolorAction, &QAction::triggered, this, &CloudViewer::pointcolorChanged);
QObject::connect(ui.bgcolorAction, &QAction::triggered, this, &CloudViewer::bgcolorChanged);
QObject::connect(ui.mainviewAction, &QAction::triggered, this, &CloudViewer::mainview);
QObject::connect(ui.leftviewAction, &QAction::triggered, this, &CloudViewer::leftview);
QObject::connect(ui.topviewAction, &QAction::triggered, this, &CloudViewer::topview);
// View (connect)
QObject::connect(ui.dataAction, &QAction::triggered, this, &CloudViewer::data);
QObject::connect(ui.propertyAction, &QAction::triggered, this, &CloudViewer::properties);
QObject::connect(ui.consoleAction, &QAction::triggered, this, &CloudViewer::console);
QObject::connect(ui.RGBAction, &QAction::triggered, this, &CloudViewer::rgbDock);
// Generate (connect)
QObject::connect(ui.cubeAction, &QAction::triggered, this, &CloudViewer::cube);
QObject::connect(ui.sphereAction, &QAction::triggered, this, &CloudViewer::createSphere);
QObject::connect(ui.cylinderAction, &QAction::triggered, this, &CloudViewer::createCylinder);
// Process (connect)
QObject::connect(ui.meshsurfaceAction, &QAction::triggered, this, &CloudViewer::convertSurface);
QObject::connect(ui.wireframeAction, &QAction::triggered, this, &CloudViewer::convertWireframe);
// Option (connect)
QObject::connect(ui.windowsThemeAction, &QAction::triggered, this, &CloudViewer::windowsTheme);
QObject::connect(ui.darculaThemeAction, &QAction::triggered, this, &CloudViewer::darculaTheme);
QObject::connect(ui.englishAction, &QAction::triggered, this, &CloudViewer::langEnglish);
QObject::connect(ui.chineseAction, &QAction::triggered, this, &CloudViewer::langChinese);
// About (connect)
QObject::connect(ui.aboutAction, &QAction::triggered, this, &CloudViewer::about);
QObject::connect(ui.helpAction, &QAction::triggered, this, &CloudViewer::help);
/***** Slots connection of RGB widget *****/
// Random color (connect)
connect(ui.colorBtn, SIGNAL(clicked()), this, SLOT(colorBtnPressed()));
// Connection between RGB slider and RGB value (connect)
connect(ui.rSlider, SIGNAL(valueChanged(int)), this, SLOT(rSliderChanged(int)));
connect(ui.gSlider, SIGNAL(valueChanged(int)), this, SLOT(gSliderChanged(int)));
connect(ui.bSlider, SIGNAL(valueChanged(int)), this, SLOT(bSliderChanged(int)));
// RGB slider released (connect)
connect(ui.rSlider, SIGNAL(sliderReleased()), this, SLOT(RGBsliderReleased()));
connect(ui.gSlider, SIGNAL(sliderReleased()), this, SLOT(RGBsliderReleased()));
connect(ui.bSlider, SIGNAL(sliderReleased()), this, SLOT(RGBsliderReleased()));
// Change size of cloud (connect)
connect(ui.pSlider, SIGNAL(valueChanged(int)), this, SLOT(pSliderChanged(int)));
connect(ui.pSlider, SIGNAL(sliderReleased()), this, SLOT(psliderReleased()));
// Checkbox for coordinate and background color (connect)
connect(ui.cooCbx, SIGNAL(stateChanged(int)), this, SLOT(cooCbxChecked(int)));
connect(ui.bgcCbx, SIGNAL(stateChanged(int)), this, SLOT(bgcCbxChecked(int)));
/***** Slots connection of dataTree(QTreeWidget) widget *****/
// Item in dataTree is left-clicked (connect)
connect(ui.dataTree, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(itemSelected(QTreeWidgetItem*, int)));
// Item in dataTree is right-clicked
connect(ui.dataTree, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(popMenu(const QPoint&)));
connect(ui.consoleTable, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(popMenuInConsole(const QPoint&)));
// Initialization
initial();
}
CloudViewer::~CloudViewer()
{
}
// Open point cloud
void CloudViewer::open()
{
QStringList filenames = QFileDialog::getOpenFileNames(this, tr("Open point cloud file"), QString::fromLocal8Bit(mycloud.dirname.c_str()), tr("Point cloud data(*.pcd *.ply *.obj);;All file(*.*)"));
//Return if filenames is empty
if (filenames.isEmpty())
return;
// Clear cache
mycloud_vec.clear();
total_points = 0;
ui.dataTree->clear();
viewer->removeAllPointClouds();
// Open point cloud one by one
for (int i = 0; i != filenames.size(); i++){
// time start
timeStart();
mycloud.cloud.reset(new PointCloudT); // Reset cloud
QString filename = filenames[i];
std::string file_name = filename.toStdString();
std::string subname = getFileName(file_name); //提取全路径中的文件名(带后缀)
//更新状态栏
ui.statusBar->showMessage(QString::fromLocal8Bit(subname.c_str()) + ": " + QString::number(i) + "/" + QString::number(filenames.size()) + " point cloud loading...");
int status = -1;
if (filename.endsWith(".pcd", Qt::CaseInsensitive))
{
status = pcl::io::loadPCDFile(file_name, *(mycloud.cloud));
if (mycloud.cloud->points[0].r == 0 && mycloud.cloud->points[0].g == 0 && mycloud.cloud->points[0].b == 0)
{
setCloudColor(255, 255, 255);
}
}
else if (filename.endsWith(".ply", Qt::CaseInsensitive))
{
status = pcl::io::loadPLYFile(file_name, *(mycloud.cloud));
if (mycloud.cloud->points[0].r == 0 && mycloud.cloud->points[0].g == 0 && mycloud.cloud->points[0].b == 0)
{
setCloudColor(255, 255, 255);
}
}
else if (filename.endsWith(".obj", Qt::CaseInsensitive))
{
status = pcl::io::loadOBJFile(file_name, *(mycloud.cloud));
if (mycloud.cloud->points[0].r == 0 && mycloud.cloud->points[0].g == 0 && mycloud.cloud->points[0].b == 0)
{
setCloudColor(255, 255, 255);
}
}
else
{
//提示:无法读取除了.ply .pcd .obj以外的文件
QMessageBox::information(this, tr("File format error"),
tr("Can't open files except .ply .pcd .obj"));
return;
}
//提示:后缀没问题,但文件内容无法读取
if (status != 0)
{
QMessageBox::critical(this, tr("Reading file error"), tr("We can not open the file"));
return;
}
setA(255); //设置点云为不透明
// 最后导入的点云的信息
mycloud.filename = file_name;
mycloud.subname = subname;
mycloud.dirname = file_name.substr(0, file_name.size() - subname.size());
mycloud_vec.push_back(mycloud); //将点云导入点云容器
// time off
time_cost = timeOff();
// 输出窗口
consoleLog("Open", QString::fromLocal8Bit(mycloud.subname.c_str()), QString::fromLocal8Bit(mycloud.filename.c_str()), "Time cost: " + time_cost + " s, Points: " + QString::number(mycloud.cloud->points.size()));
//更新资源管理树
QTreeWidgetItem *cloudName = new QTreeWidgetItem(QStringList()
<< QString::fromLocal8Bit(subname.c_str()));
cloudName->setIcon(0, QIcon(":/Resources/images/icon.png"));
ui.dataTree->addTopLevelItem(cloudName);
//setWindowTitle(filename + " - CloudViewer"); //更新标题
total_points += mycloud.cloud->points.size();
}
ui.statusBar->showMessage("");
showPointcloudAdd(); //更新视图窗口
setPropertyTable();
}
// Add Point Cloud
void CloudViewer::add()
{
QStringList filenames = QFileDialog::getOpenFileNames(this, tr("Open point cloud file"), QString::fromLocal8Bit(mycloud.dirname.c_str()), tr("Point cloud data(*.pcd *.ply *.obj);;All file(*.*)"));
if (filenames.isEmpty())
return;
for (int i = 0; i != filenames.size(); i++){
// time start
timeStart();
mycloud.cloud.reset(new PointCloudT);
QString filename = filenames[i];
std::string file_name = filename.toStdString();
std::string subname = getFileName(file_name);
// 更新状态栏
ui.statusBar->showMessage(QString::fromLocal8Bit(subname.c_str()) + ": " + QString::number(i) + "/" + QString::number(filenames.size()) + " point cloud loading...");
int status = -1;
if (filename.endsWith(".pcd", Qt::CaseInsensitive))
{
status = pcl::io::loadPCDFile(file_name, *(mycloud.cloud));
if (mycloud.cloud->points[0].r == 0 && mycloud.cloud->points[0].g == 0 && mycloud.cloud->points[0].b == 0)
{
setCloudColor(255, 255, 255);
}
}
else if (filename.endsWith(".ply", Qt::CaseInsensitive))
{
status = pcl::io::loadPLYFile(file_name, *(mycloud.cloud));
if (mycloud.cloud->points[0].r == 0 && mycloud.cloud->points[0].g == 0 && mycloud.cloud->points[0].b == 0)
{
setCloudColor(255, 255, 255);
}
}
else if (filename.endsWith(".obj", Qt::CaseInsensitive))
{
status = pcl::io::loadOBJFile(file_name, *(mycloud.cloud));
if (mycloud.cloud->points[0].r == 0 && mycloud.cloud->points[0].g == 0 && mycloud.cloud->points[0].b == 0)
{
setCloudColor(255, 255, 255);
}
}
else
{
//提示:无法读取除了.ply .pcd .obj以外的文件
QMessageBox::information(this, tr("File format error"), tr("Can't open files except .ply .pcd .obj"));
return;
}
//提示:后缀没问题,但文件内容无法读取
if (status != 0)
{
QMessageBox::critical(this, tr("Reading file error"), tr("We can not open the file"));
return;
}
setA(255); //设置点云为不透明
mycloud.filename = file_name;
mycloud.subname = subname;
mycloud.dirname = file_name.substr(0, file_name.size() - subname.size());
mycloud_vec.push_back(mycloud); //将点云导入点云容器
// time of
time_cost = timeOff();
//输出窗口
consoleLog("Add", QString::fromLocal8Bit(mycloud.subname.c_str()), QString::fromLocal8Bit(mycloud.filename.c_str()), "Time cost: " + time_cost + " s, Points: " + QString::number(mycloud.cloud->points.size()));
//设置资源管理器
QTreeWidgetItem *cloudName = new QTreeWidgetItem(QStringList() << QString::fromLocal8Bit(subname.c_str()));
cloudName->setIcon(0, QIcon(":/Resources/images/icon.png"));
ui.dataTree->addTopLevelItem(cloudName);
//setWindowTitle("CloudViewer");
total_points += mycloud.cloud->points.size();
}
ui.statusBar->showMessage("");
showPointcloudAdd();
setPropertyTable();
}
// Clear all point clouds
void CloudViewer::clear()
{
mycloud_vec.clear(); //从点云容器中移除所有点云
viewer->removeAllPointClouds(); //从viewer中移除所有点云
viewer->removeAllShapes(); //这个remove更彻底
ui.dataTree->clear(); //将dataTree清空
ui.propertyTable->clear(); //清空属性窗口propertyTable
QStringList header;
header << "Property" << "Value";
ui.propertyTable->setHorizontalHeaderLabels(header);
//输出窗口
consoleLog("Clear", "All point clouds", "", "");
setWindowTitle("CloudViewer"); //更新窗口标题
showPointcloud(); //更新显示
}
// Save point cloud
void CloudViewer::save()
{
save_filename = QFileDialog::getSaveFileName(this, tr("Save point cloud"),
QString::fromLocal8Bit(mycloud.dirname.c_str()), tr("Point cloud data(*.pcd *.ply);;Allfile(*.*)"));
std::string file_name = save_filename.toStdString();
std::string subname = getFileName(file_name);
//文件名为空直接返回
if (save_filename.isEmpty())
return;
if (mycloud_vec.size() > 1)
{
savemulti();
return;
}
int status = -1;
if (save_filename.endsWith(".pcd", Qt::CaseInsensitive))
{
status = pcl::io::savePCDFile(file_name, *(mycloud.cloud));
}
else if (save_filename.endsWith(".ply", Qt::CaseInsensitive))
{
status = pcl::io::savePLYFile(file_name, *(mycloud.cloud));
}
else //提示:无法保存为除了.ply .pcd以外的文件
{
QMessageBox::information(this, tr("File format error"),
tr("Can't save files except .ply .pcd"));
return;
}
//提示:后缀没问题,但是无法保存
if (status != 0)
{
QMessageBox::critical(this, tr("Saving file error"),
tr("We can not save the file"));
return;
}
//输出窗口
consoleLog("Save", QString::fromLocal8Bit(subname.c_str()), save_filename, "Single save");
setWindowTitle(save_filename + " - CloudViewer");
QMessageBox::information(this, tr("save point cloud file"),
QString::fromLocal8Bit(("Save " + subname + " successfully!").c_str()));
}
// Save point cloud as binary file
void CloudViewer::saveBinary()
{
save_filename = QFileDialog::getSaveFileName(this, tr("Save point cloud as binary file"),
QString::fromLocal8Bit(mycloud.dirname.c_str()), tr("Point cloud data(*.pcd *.ply);;Allfile(*.*)"));
std::string file_name = save_filename.toStdString();
std::string subname = getFileName(file_name);
//文件名为空直接返回
if (save_filename.isEmpty())
return;
if (mycloud_vec.size() > 1)
{
savemulti();
return;
}
int status = -1;
if (save_filename.endsWith(".pcd", Qt::CaseInsensitive))
{
status = pcl::io::savePCDFileBinary(file_name, *(mycloud.cloud));
}
else if (save_filename.endsWith(".ply", Qt::CaseInsensitive))
{
status = pcl::io::savePLYFileBinary(file_name, *(mycloud.cloud));
}
else //提示:无法保存为除了.ply .pcd以外的文件
{
QMessageBox::information(this, tr("File format error"),
tr("Can't save files except .ply .pcd"));
return;
}
//提示:后缀没问题,但是无法保存
if (status != 0)
{
QMessageBox::critical(this, tr("Saving file error"),
tr("We can not save the file"));
return;
}
//输出窗口
consoleLog("Save as binary", QString::fromLocal8Bit(subname.c_str()), save_filename, "Single save (binary)");
setWindowTitle(save_filename + " - CloudViewer");
QMessageBox::information(this, tr("save point cloud file"),
QString::fromLocal8Bit(("Save " + subname + " successfully!").c_str()));
}
// Save multi point cloud
void CloudViewer::savemulti()
{
std::string subname = getFileName(save_filename.toStdString());
PointCloudT::Ptr multi_cloud;
multi_cloud.reset(new PointCloudT);
multi_cloud->height = 1;
int sum = 0;
for (auto c : mycloud_vec)
{
sum += c.cloud->points.size();
}
multi_cloud->width = sum;
multi_cloud->resize(multi_cloud->height * multi_cloud->width);
int k = 0;
for (int i = 0; i != mycloud_vec.size(); i++)
{
for (int j = 0; j != mycloud_vec[i].cloud->points.size(); j++) //注意cloudvec[i]->points.size()和cloudvec[i]->size()的区别
{
multi_cloud->points[k].x = mycloud_vec[i].cloud->points[j].x;
multi_cloud->points[k].y = mycloud_vec[i].cloud->points[j].y;
multi_cloud->points[k].z = mycloud_vec[i].cloud->points[j].z;
multi_cloud->points[k].r = mycloud_vec[i].cloud->points[j].r;
multi_cloud->points[k].g = mycloud_vec[i].cloud->points[j].g;
multi_cloud->points[k].b = mycloud_vec[i].cloud->points[j].b;
k++;
}
}
//保存multi_cloud
int status = -1;
if (save_filename.endsWith(".pcd", Qt::CaseInsensitive))
{
if (save_as_binary){
status = pcl::io::savePCDFileBinary(save_filename.toStdString(), *multi_cloud);
}
else{
status = pcl::io::savePCDFile(save_filename.toStdString(), *multi_cloud);
}
}
else if (save_filename.endsWith(".ply", Qt::CaseInsensitive))
{
if (save_as_binary){
status = pcl::io::savePLYFileBinary(save_filename.toStdString(), *multi_cloud);
}
else{
status = pcl::io::savePLYFile(save_filename.toStdString(), *multi_cloud);
}
}
else //提示:无法保存为除了.ply .pcd以外的文件
{
QMessageBox::information(this, tr("File format error"), tr("Can't save files except .ply .pcd"));
return;
}
//提示:后缀没问题,但是无法保存
if (status != 0)
{
QMessageBox::critical(this, tr("Saving file error"), tr("We can not save the file"));
return;
}
// 输出窗口
if (save_as_binary){
consoleLog("Save as binary", QString::fromLocal8Bit(subname.c_str()), save_filename, "Multi save (binary)");
}
else{
consoleLog("Save", QString::fromLocal8Bit(subname.c_str()), save_filename, "Multi save");
}
save_as_binary = false;
//将保存后的 multi_cloud 设置为当前 mycloud,以便保存之后直接进行操作
mycloud.cloud = multi_cloud;
mycloud.filename = save_filename.toStdString();
mycloud.subname = subname;
setWindowTitle(save_filename + " - CloudViewer");
QMessageBox::information(this, tr("save point cloud file"), QString::fromLocal8Bit(("Save " + subname + " successfully!").c_str()));
}
//格式转换
void CloudViewer::change()
{
}
//退出程序
void CloudViewer::exit()
{
this->close();
}
// Generate cube
void CloudViewer::cube()
{
mycloud.cloud.reset(new PointCloudT);
total_points = 0;
ui.dataTree->clear(); //清空资源管理器的item
viewer->removeAllPointClouds(); //从viewer中移除所有点云
mycloud_vec.clear(); //清空点云容器
mycloud.cloud->width = 50000; // 设置点云宽
mycloud.cloud->height = 1; // 设置点云高,高为1,说明为无组织点云
mycloud.cloud->is_dense = false;
mycloud.cloud->resize(mycloud.cloud->width * mycloud.cloud->height); // 重置点云大小
for (size_t i = 0; i != mycloud.cloud->size(); ++i)
{
mycloud.cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
mycloud.cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
mycloud.cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
mycloud.cloud->points[i].r = red;
mycloud.cloud->points[i].g = green;
mycloud.cloud->points[i].b = blue;
}
//设置资源管理器
QTreeWidgetItem *cloudName = new QTreeWidgetItem(QStringList() << QString::fromLocal8Bit("cube"));
cloudName->setIcon(0, QIcon(":/Resources/images/icon.png"));
ui.dataTree->addTopLevelItem(cloudName);
// 输出窗口
consoleLog("Generate cube", "cube", "cube", "");
mycloud_vec.push_back(mycloud);
showPointcloudAdd();
}
//初始化
void CloudViewer::initial()
{
//界面初始化
setWindowIcon(QIcon(tr(":/Resources/images/icon.png")));
setWindowTitle(tr("CloudViewer"));
//点云初始化
mycloud.cloud.reset(new PointCloudT);
mycloud.cloud->resize(1);
viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));
//viewer->addPointCloud(cloud, "cloud");
ui.screen->SetRenderWindow(viewer->getRenderWindow());
viewer->setupInteractor(ui.screen->GetInteractor(), ui.screen->GetRenderWindow());
ui.screen->update();
ui.propertyTable->setSelectionMode(QAbstractItemView::NoSelection); // 禁止点击属性管理器的 item
ui.consoleTable->setSelectionMode(QAbstractItemView::NoSelection); // 禁止点击输出窗口的 item
ui.dataTree->setSelectionMode(QAbstractItemView::ExtendedSelection); // 允许 dataTree 进行多选
// 设置默认主题
QString qss = darcula_qss;
qApp->setStyleSheet(qss);
setPropertyTable();
setConsoleTable();
// 输出窗口
consoleLog("Software start", "CloudViewer", "Welcome to use CloudViewer", "Nightn");
// 设置背景颜色为 dark
viewer->setBackgroundColor(30 / 255.0, 30 / 255.0, 30 / 255.0);
}
//显示点云,不重置相机角度
void CloudViewer::showPointcloud()
{
for (int i = 0; i != mycloud_vec.size(); i++)
{
viewer->updatePointCloud(mycloud_vec[i].cloud, "cloud" + QString::number(i).toStdString());
}
//viewer->resetCamera();
ui.screen->update();
}
//添加点云到viewer,并显示点云
void CloudViewer::showPointcloudAdd()
{
for (int i = 0; i != mycloud_vec.size(); i++)
{
viewer->addPointCloud(mycloud_vec[i].cloud, "cloud" + QString::number(i).toStdString());
viewer->updatePointCloud(mycloud_vec[i].cloud, "cloud" + QString::number(i).toStdString());
}
viewer->resetCamera();
ui.screen->update();
}
void CloudViewer::setCloudColor(unsigned int r, unsigned int g, unsigned int b)
{
// Set the new color
for (size_t i = 0; i < mycloud.cloud->size(); i++)
{
mycloud.cloud->points[i].r = r;
mycloud.cloud->points[i].g = g;
mycloud.cloud->points[i].b = b;
mycloud.cloud->points[i].a = 255;
}
}
void CloudViewer::setA(unsigned int a)
{
for (size_t i = 0; i < mycloud.cloud->size(); i++)
{
mycloud.cloud->points[i].a = a;
}
}
//关于
void CloudViewer::about()
{
AboutWin *aboutwin = new AboutWin(this);
aboutwin->setModal(true);
aboutwin->show();
// 输出窗口
consoleLog("About", "Nightn", "http://nightn.com", "Welcome to my blog!");
}
//帮助
void CloudViewer::help()
{
QDesktopServices::openUrl(QUrl(QLatin1String("http://nightn.com/cloudviewer")));
// 输出窗口
consoleLog("Help", "Cloudviewer help", "http://nightn.com/cloudviewer", "");
//QMessageBox::information(this, "Help", "we are building help widget...");
}
//设置停靠窗口的显示与隐藏
void CloudViewer::data()
{
if (ui.dataAction->isChecked())
{
ui.dataDock->setVisible(true);
}
else
{
ui.dataDock->setVisible(false);
}
}
void CloudViewer::properties()
{
if (ui.propertyAction->isChecked())
{
ui.propertyDock->setVisible(true);
}
else
{
ui.propertyDock->setVisible(false);
}
}
void CloudViewer::console()
{
if (ui.consoleAction->isChecked())
{
ui.consoleDock->setVisible(true);
}
else
{
ui.consoleDock->setVisible(false);
}
}
void CloudViewer::rgbDock()
{
if (ui.RGBAction->isChecked())
{
ui.RGBDock->setVisible(true);
}
else
{
ui.RGBDock->setVisible(false);
}
}
//绘制基本图形
void CloudViewer::createSphere()
{
mycloud.cloud.reset(new PointCloudT);
ui.dataTree->clear(); //清空资源管理器的item
viewer->removeAllShapes();
mycloud_vec.clear(); //清空点云容器
pcl::PointXYZ p;
p.x = 0; p.y = 0; p.z = 0;
viewer->addSphere(p, 100, "sphere1");
viewer->resetCamera();
ui.screen->update();
// 输出窗口
consoleLog("Create sphere", "Sphere", "", "Succeeded");
}
void CloudViewer::createCylinder()
{
mycloud.cloud.reset(new PointCloudT);
ui.dataTree->clear(); //清空资源管理器的item
viewer->removeAllShapes();
mycloud_vec.clear(); //清空点云容器
viewer->addCylinder(*(new pcl::ModelCoefficients()), "cylinder");
viewer->resetCamera();
ui.screen->update();
// 输出窗口
consoleLog("Create cylinder", "Cylinder", "", "Failed");
}
// Change theme: Windows/Darcula
void CloudViewer::windowsTheme(){
/*
QFile qssFile("Resources/qss/Windows.qss"); //资源文件":/Darcula.qss"
qssFile.open(QFile::ReadOnly);
if (qssFile.isOpen())
{
QString qss = QLatin1String(qssFile.readAll());
//consoleLog("", "", "", qss);
qApp->setStyleSheet(qss);
qssFile.close();
}
*/
QString qss = windows_qss;
qApp->setStyleSheet(qss);
//改变 dataTree 字体颜色,以适应主题的笨办法
QColor light_color(241, 241, 241, 255);
QColor dark_color(0, 0, 0, 255);
for (int i = 0; i != mycloud_vec.size(); i++){
if (ui.dataTree->topLevelItem(i)->textColor(0) == light_color){
ui.dataTree->topLevelItem(i)->setTextColor(0, dark_color);
}
}
// 输出窗口
consoleLog("Change theme", "Windows theme", "", "");
theme_id = 0;
}
void CloudViewer::darculaTheme(){
/*
QFile qssFile("Resources/qss/Darcula.qss"); //资源文件":/Darcula.qss"
qssFile.open(QFile::ReadOnly);
if (qssFile.isOpen())
{
QString qss = QLatin1String(qssFile.readAll());
//cout << qss.toStdString();
consoleLog("", "", "", qss);
qApp->setStyleSheet(qss);
qssFile.close();
}
*/
QString qss = darcula_qss;
qApp->setStyleSheet(qss);
//改变 dataTree 字体颜色,以适应主题的笨办法
QColor light_color(241, 241, 241, 255);
QColor dark_color(0, 0, 0, 255);
for (int i = 0; i != mycloud_vec.size(); i++){
if (ui.dataTree->topLevelItem(i)->textColor(0) == dark_color){
ui.dataTree->topLevelItem(i)->setTextColor(0, light_color);
}
}
// 输出窗口
consoleLog("Change theme", "Darcula theme", "", "");
theme_id = 1;
}
// Change language: English/Chinese
void CloudViewer::langEnglish(){
// 输出窗口
consoleLog("Change language", "English", "", "");
}
void CloudViewer::langChinese(){
// 输出窗口
consoleLog("Change language", "Chinese", "Doesn't support Chinese temporarily", "");
}
/*********************************************/
/*****************界面槽函数*****************/
/********************************************/
void CloudViewer::colorBtnPressed()
{
QList<QTreeWidgetItem*> itemList = ui.dataTree->selectedItems();
int selected_item_count = ui.dataTree->selectedItems().size();
// 如果未选中任何点云,则对视图窗口中的所有点云进行着色
if (selected_item_count == 0){
for (int i = 0; i != mycloud_vec.size(); i++){
for (int j = 0; j != mycloud_vec[i].cloud->points.size(); j++){
mycloud_vec[i].cloud->points[j].r = 255 * (1024 * rand() / (RAND_MAX + 1.0f));
mycloud_vec[i].cloud->points[j].g = 255 * (1024 * rand() / (RAND_MAX + 1.0f));
mycloud_vec[i].cloud->points[j].b = 255 * (1024 * rand() / (RAND_MAX + 1.0f));
}
}
// 输出窗口
consoleLog("Random color", "All point clous", "", "");
}
else{
for (int i = 0; i != selected_item_count; i++){
int cloud_id = ui.dataTree->indexOfTopLevelItem(itemList[i]);
for (int j = 0; j != mycloud_vec[cloud_id].cloud->size(); j++){
mycloud_vec[cloud_id].cloud->points[j].r = red;
mycloud_vec[cloud_id].cloud->points[j].g = 255 * (1024 * rand() / (RAND_MAX + 1.0f));
mycloud_vec[cloud_id].cloud->points[j].b = 255 * (1024 * rand() / (RAND_MAX + 1.0f));
}
}
// 输出窗口
consoleLog("Random color", "Point clouds selected", "", "");
}
showPointcloud();
}
void CloudViewer::RGBsliderReleased()
{
QList<QTreeWidgetItem*> itemList = ui.dataTree->selectedItems();
int selected_item_count = ui.dataTree->selectedItems().size();
// 如果未选中任何点云,则对视图窗口中的所有点云进行着色
if (selected_item_count == 0){
for (int i = 0; i != mycloud_vec.size(); i++){
for (int j = 0; j != mycloud_vec[i].cloud->points.size(); j++){
mycloud_vec[i].cloud->points[j].r = red;
mycloud_vec[i].cloud->points[j].g = green;
mycloud_vec[i].cloud->points[j].b = blue;
}
}
// 输出窗口
consoleLog("Change cloud color", "All point clouds", QString::number(red) + " " + QString::number(green) + " " + QString::number(blue), "");
}
else{
for (int i = 0; i != selected_item_count; i++){
int cloud_id = ui.dataTree->indexOfTopLevelItem(itemList[i]);
for (int j = 0; j != mycloud_vec[cloud_id].cloud->size(); j++){
mycloud_vec[cloud_id].cloud->points[j].r = red;
mycloud_vec[cloud_id].cloud->points[j].g = green;
mycloud_vec[cloud_id].cloud->points[j].b = blue;
}
}
// 输出窗口
consoleLog("Change cloud color", "Point clouds selected", QString::number(red) + " " + QString::number(green) + " " + QString::number(blue), "");
}
showPointcloud();
}
//设置所有点云的尺寸
void CloudViewer::psliderReleased()
{
QList<QTreeWidgetItem*> itemList = ui.dataTree->selectedItems();
int selected_item_count = ui.dataTree->selectedItems().size();
if (selected_item_count == 0){
for (int i = 0; i != mycloud_vec.size(); i++){
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,
p, "cloud" + QString::number(i).toStdString());
}
// 输出窗口
consoleLog("Change cloud size", "All point clouds", "Size: " + QString::number(p), "");
}
else{
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,
p, "cloud" + QString::number(cloud_id).toStdString());
}
// 输出窗口
consoleLog("Change cloud size", "Point clouds selected", "Size: " + QString::number(p), "");
}
ui.screen->update();
}
void CloudViewer::pSliderChanged(int value)
{
p = value;
ui.sizeLCD->display(value);
}
void CloudViewer::rSliderChanged(int value)
{
red = value;
ui.rLCD->display(value);
}
void CloudViewer::gSliderChanged(int value)
{
green = value;
ui.gLCD->display(value);
}
void CloudViewer::bSliderChanged(int value)
{
blue = value;
ui.bLCD->display(value);
}
void CloudViewer::cooCbxChecked(int value)
{
switch (value)
{
case 0:
viewer->removeCoordinateSystem(); //移除坐标系
// 输出窗口
consoleLog("Remove coordinate system", "Remove", "", "");
break;
case 2:
viewer->addCoordinateSystem(); //添加坐标系
// 输出窗口
consoleLog("Add coordinate system", "Add", "", "");
break;
}
//viewer->updatePointCloud(cloud, "cloud");
ui.screen->update();
}
void CloudViewer::bgcCbxChecked(int value)
{
switch (value)
{
case 0:
viewer->setBackgroundColor(30 / 255.0, 30 / 255.0, 30 / 255.0);
// 输出窗口
consoleLog("Change bg color", "Background", "30 30 30", "");
break;
case 2:
//!注意:setBackgroundColor()接收的是0-1的double型参数
viewer->setBackgroundColor(240 / 255.0, 240 / 255.0, 240 / 255.0);
// 输出窗口
consoleLog("Change bg color", "Background", "240 240 240", "");
break;
}
//viewer->updatePointCloud(cloud, "cloud");
ui.screen->update();
}
//通过颜色对话框改变点云颜色
void CloudViewer::pointcolorChanged()
{
QColor color = QColorDialog::getColor(Qt::white, this, "Select color for point cloud");
if (color.isValid()) //判断所选的颜色是否有效
{
//QAction* action = dynamic_cast<QAction*>(sender());
//if (action != ui.pointcolorAction) //改变颜色的信号来自于 dataTree
QList<QTreeWidgetItem*> itemList = ui.dataTree->selectedItems();
int selected_item_count = ui.dataTree->selectedItems().size();
if (selected_item_count == 0){
for (int i = 0; i != mycloud_vec.size(); i++){
for (int j = 0; j != mycloud_vec[i].cloud->points.size(); j++){
mycloud_vec[i].cloud->points[j].r = color.red();
mycloud_vec[i].cloud->points[j].g = color.green();
mycloud_vec[i].cloud->points[j].b = color.blue();
}
}
// 输出窗口
consoleLog("Change cloud color", "All point clouds", QString::number(color.red()) + " " + QString::number(color.green()) + " " + QString::number(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 != mycloud_vec[cloud_id].cloud->size(); j++){
mycloud_vec[cloud_id].cloud->points[j].r = color.red();
mycloud_vec[cloud_id].cloud->points[j].g = color.green();
mycloud_vec[cloud_id].cloud->points[j].b = color.blue();
}
}
// 输出窗口
consoleLog("Change cloud color", "Point clouds selected", QString::number(color.red()) + " " + QString::number(color.green()) + " " + QString::number(color.blue()), "");
}
//颜色的改变同步至RGB停靠窗口
ui.rSlider->setValue(color.red());
ui.gSlider->setValue(color.green());
ui.bSlider->setValue(color.blue());
showPointcloud();
}
}
//通过颜色对话框改变背景颜色
void CloudViewer::bgcolorChanged()
{
QColor color = QColorDialog::getColor(Qt::white, this,
"Select color for point cloud");
if (color.isValid())
{
viewer->setBackgroundColor(color.red() / 255.0,
color.green() / 255.0, color.blue() / 255.0);
// 输出窗口
consoleLog("Change bg color", "Background", QString::number(color.red()) + " " + QString::number(color.green()) + " " + QString::number(color.blue()), "");
showPointcloud();
}
}
//三视图
void CloudViewer::mainview()
{
viewer->setCameraPosition(0, -1, 0, 0.5, 0.5, 0.5, 0, 0, 1);
ui.screen->update();
}
void CloudViewer::leftview()
{
viewer->setCameraPosition(-1, 0, 0, 0, 0, 0, 0, 0, 1);
ui.screen->update();
}
void CloudViewer::topview()
{
viewer->setCameraPosition(0, 0, 1, 0, 0, 0, 0, 1, 0);
ui.screen->update();
}
//设置属性管理窗口
void CloudViewer::setPropertyTable(){
QStringList header;
header << "Property" << "Value";
ui.propertyTable->setHorizontalHeaderLabels(header);
ui.propertyTable->setItem(0, 0, new QTableWidgetItem("Clouds"));
ui.propertyTable->setItem(1, 0, new QTableWidgetItem("Points"));
ui.propertyTable->setItem(2, 0, new QTableWidgetItem("Total points"));
ui.propertyTable->setItem(3, 0, new QTableWidgetItem("RGB"));
ui.propertyTable->setItem(0, 1, new QTableWidgetItem(QString::number(mycloud_vec.size())));
ui.propertyTable->setItem(1, 1, new QTableWidgetItem(""));
ui.propertyTable->setItem(2, 1, new QTableWidgetItem(QString::number(total_points)));
ui.propertyTable->setItem(4, 1, new QTableWidgetItem(""));