1 Star 0 Fork 11

lxj362343/CloudViewer

forked from jerry.xu/CloudViewer 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cloudviewer.cpp 47.36 KB
一键复制 编辑 原始数据 按行查看 历史
wwq 提交于 2017-04-17 23:33 . CloudViewer 1.0.1
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
#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(""));
}
void CloudViewer::setConsoleTable(){
// 设置输出窗口
QStringList header2;
header2 << "Time" << "Operation" << "Operation obeject" << "Details" << "Note";
ui.consoleTable->setHorizontalHeaderLabels(header2);
ui.consoleTable->setColumnWidth(0, 150);
ui.consoleTable->setColumnWidth(1, 200);
ui.consoleTable->setColumnWidth(2, 200);
ui.consoleTable->setColumnWidth(3, 300);
//ui.consoleTable->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不可编辑
ui.consoleTable->verticalHeader()->setDefaultSectionSize(22); //设置行距
ui.consoleTable->setContextMenuPolicy(Qt::CustomContextMenu);
}
//
void CloudViewer::consoleLog(QString operation, QString subname, QString filename, QString note)
{
if (enable_console == false){
return;
}
int rows = ui.consoleTable->rowCount();
ui.consoleTable->setRowCount(++rows);
QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
QString time_str = time.toString("MM-dd hh:mm:ss"); //设置显示格式
ui.consoleTable->setItem(rows - 1, 0, new QTableWidgetItem(time_str));
ui.consoleTable->setItem(rows - 1, 1, new QTableWidgetItem(operation));
ui.consoleTable->setItem(rows - 1, 2, new QTableWidgetItem(subname));
ui.consoleTable->setItem(rows - 1, 3, new QTableWidgetItem(filename));
ui.consoleTable->setItem(rows - 1, 4, new QTableWidgetItem(note));
ui.consoleTable->scrollToBottom(); // 滑动自动滚到最底部
}
//QTreeWidget的item的点击相应函数
void CloudViewer::itemSelected(QTreeWidgetItem* item, int count)
{
count = ui.dataTree->indexOfTopLevelItem(item); //获取item的行号
for (int i = 0; i != mycloud_vec.size(); i++)
{
viewer->updatePointCloud(mycloud_vec[i].cloud, "cloud" + QString::number(i).toStdString());
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud" + QString::number(i).toStdString());
}
//提取当前点云的RGB,点云数量等信息
int cloud_size = mycloud_vec[count].cloud->points.size();
unsigned int cloud_r = mycloud_vec[count].cloud->points[0].r;
unsigned int cloud_g = mycloud_vec[count].cloud->points[0].g;
unsigned int cloud_b = mycloud_vec[count].cloud->points[0].b;
bool multi_color = true;
if (mycloud_vec[count].cloud->points.begin()->r == (mycloud_vec[count].cloud->points.end() - 1)->r) //判断点云单色多色的条件(不是很严谨)
multi_color = false;
ui.propertyTable->setItem(0, 1, new QTableWidgetItem(QString::number(mycloud_vec.size())));
ui.propertyTable->setItem(1, 1, new QTableWidgetItem(QString::number(cloud_size)));
ui.propertyTable->setItem(2, 1, new QTableWidgetItem(QString::number(total_points)));
ui.propertyTable->setItem(3, 1, new QTableWidgetItem(multi_color ? "Multi Color" : (QString::number(cloud_r) + " " + QString::number(cloud_g) + " " + QString::number(cloud_b))));
//选中item所对应的点云尺寸变大
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());
}
//mycloud = mycloud_vec[count];
ui.screen->update();
}
// consoleTable 右击响应事件
void CloudViewer::popMenuInConsole(const QPoint&){
QAction clearConsoleAction("Clear console", this);
QAction enableConsoleAction("Enable console", this);
QAction disableConsoleAction("Disable console", this);
connect(&clearConsoleAction, &QAction::triggered, this, &CloudViewer::clearConsole);
connect(&enableConsoleAction, &QAction::triggered, this, &CloudViewer::enableConsole);
connect(&disableConsoleAction, &QAction::triggered, this, &CloudViewer::disableConsole);
QPoint pos;
QMenu menu(ui.dataTree);
menu.addAction(&clearConsoleAction);
menu.addAction(&enableConsoleAction);
menu.addAction(&disableConsoleAction);
if (enable_console == true){
menu.actions()[1]->setVisible(false);
menu.actions()[2]->setVisible(true);
}
else{
menu.actions()[1]->setVisible(true);
menu.actions()[2]->setVisible(false);
}
menu.exec(QCursor::pos()); //在当前鼠标位置显示
}
// 清空 consoleTable
void CloudViewer::clearConsole(){
ui.consoleTable->clearContents();
ui.consoleTable->setRowCount(0);
}
// 允许使用 consoleTable
void CloudViewer::enableConsole(){
enable_console = true;
}
// 禁用 consoleTable
void CloudViewer::disableConsole(){
clearConsole();
enable_console = false;
}
//QTreeWidget的item的右击响应函数
void CloudViewer::popMenu(const QPoint&)
{
QTreeWidgetItem* curItem = ui.dataTree->currentItem(); //获取当前被点击的节点
if (curItem == NULL)return; //这种情况是右键的位置不在treeItem的范围内,即在空白位置右击
QString name = curItem->text(0);
int id = ui.dataTree->indexOfTopLevelItem(curItem);
string cloud_id = "cloud" + QString::number(id).toStdString();
QAction hideItemAction("Hide", this);
QAction showItemAction("Show", this);
QAction deleteItemAction("Delete", this);
QAction changeColorAction("Change color", this);
connect(&hideItemAction, &QAction::triggered, this, &CloudViewer::hideItem);
connect(&showItemAction, &QAction::triggered, this, &CloudViewer::showItem);
connect(&deleteItemAction, &QAction::triggered, this, &CloudViewer::deleteItem);
connect(&changeColorAction, &QAction::triggered, this, &CloudViewer::pointcolorChanged);
QPoint pos;
QMenu menu(ui.dataTree);
menu.addAction(&hideItemAction);
menu.addAction(&showItemAction);
menu.addAction(&deleteItemAction);
menu.addAction(&changeColorAction);
if (mycloud_vec[id].visible == true){
menu.actions()[1]->setVisible(false);
menu.actions()[0]->setVisible(true);
}
else{
menu.actions()[1]->setVisible(true);
menu.actions()[0]->setVisible(false);
}
menu.exec(QCursor::pos()); //在当前鼠标位置显示
}
void CloudViewer::hideItem()
{
QList<QTreeWidgetItem*> itemList = ui.dataTree->selectedItems();
for (int i = 0; i != ui.dataTree->selectedItems().size(); i++){
//TODO hide之后,item变成灰色,再次右击item时,“hideItem” 选项变成 “showItem”
//QTreeWidgetItem* curItem = ui.dataTree->currentItem();
QTreeWidgetItem* curItem = itemList[i];
QString name = curItem->text(0);
int id = ui.dataTree->indexOfTopLevelItem(curItem);
string cloud_id = "cloud" + QString::number(id).toStdString();
//QMessageBox::information(this, "cloud_id", QString::fromLocal8Bit(cloud_id.c_str()));
// 将cloud_id所对应的点云设置成透明
viewer->setPointCloudRenderingProperties(pcl::visualization::RenderingProperties::PCL_VISUALIZER_OPACITY, 0.0, cloud_id, 0);
QColor item_color = QColor(112, 122, 132, 255);
curItem->setTextColor(0, item_color);
mycloud_vec[id].visible = false;
}
// 输出窗口
consoleLog("Hide point clouds", "Point clouds selected", "", "");
ui.screen->update(); //刷新视图窗口,不能省略
}
void CloudViewer::showItem()
{
QList<QTreeWidgetItem*> itemList = ui.dataTree->selectedItems();
for (int i = 0; i != ui.dataTree->selectedItems().size(); i++){
//QTreeWidgetItem* curItem = ui.dataTree->currentItem();
QTreeWidgetItem* curItem = itemList[i];
QString name = curItem->text(0);
int id = ui.dataTree->indexOfTopLevelItem(curItem);
string cloud_id = "cloud" + QString::number(id).toStdString();
// 将cloud_id所对应的点云设置成透明
viewer->setPointCloudRenderingProperties(pcl::visualization::RenderingProperties::PCL_VISUALIZER_OPACITY, 1.0, cloud_id, 0);
QColor item_color;
if (theme_id == 0){
item_color = QColor(0, 0, 0, 255);
}
else{
item_color = QColor(241, 241, 241, 255);
}
curItem->setTextColor(0, item_color);
mycloud_vec[id].visible = true;
}
// 输出窗口
consoleLog("Show point clouds", "Point clouds selected", "", "");
ui.screen->update(); //刷新视图窗口,不能省略
}
void CloudViewer::deleteItem()
{
QList<QTreeWidgetItem*> itemList = ui.dataTree->selectedItems();
// ui.dataTree->selectedItems().size() 随着迭代次数而改变,因此循环条件要设置为固定大小的 selected_item_count
int selected_item_count = ui.dataTree->selectedItems().size();
for (int i = 0; i != selected_item_count; i++){
//QTreeWidgetItem* curItem = ui.dataTree->currentItem();
//QMessageBox::information(this, "itemList's size", QString::number(ui.dataTree->selectedItems().size()));
QTreeWidgetItem* curItem = itemList[i];
QString name = curItem->text(0);
int id = ui.dataTree->indexOfTopLevelItem(curItem);
//QMessageBox::information(this, "information", "curItem: " + name + " " + QString::number(id));
auto it = mycloud_vec.begin() + ui.dataTree->indexOfTopLevelItem(curItem);
// 删除点云之前,将其点的数目保存
int delete_points = (*it).cloud->points.size();
it = mycloud_vec.erase(it);
//QMessageBox::information(this, "information", QString::number(delete_points) + " " + QString::number(mycloud_vec.size()));
total_points -= delete_points;
setPropertyTable();
ui.dataTree->takeTopLevelItem(ui.dataTree->indexOfTopLevelItem(curItem));
}
// 移除之后再添加,避免 id 和资源管理树行号不一致的情况
viewer->removeAllPointClouds();
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());
}
// 输出窗口
consoleLog("Delete point clouds", "Point clouds selected", "", "");
ui.screen->update();
}
//法线估计、曲面重建、网格面片显示
int CloudViewer::convertSurface()
{
/* 问题
好像该方法只能处理PointXYZ的点云,用PointXZYRGBA的点云编译会报错
调用boost::this_thread::sleep好像也会编译出错
*/
pcl::PointXYZ point;
cloud_xyz.reset(new pcl::PointCloud<pcl::PointXYZ>);
for (size_t i = 0; i < mycloud.cloud->size(); i++)
{
point.x = mycloud.cloud->points[i].x;
point.y = mycloud.cloud->points[i].y;
point.z = mycloud.cloud->points[i].z;
cloud_xyz->push_back(point);
}
if (!cloud_xyz)
{
return -1;
}
/****** 法向估计模块 ******/
//创建法线估计对象 n
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> n;
//创建法向数据指针 normals
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
//创建 kdtree 用于法向计算时近邻搜索
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
tree->setInputCloud(cloud_xyz); //为 kdtree 输入点云
n.setInputCloud(cloud_xyz); //为法向估计对象输入点云
n.setSearchMethod(tree); //设置法向估计时所采取的搜索方式为kdtree
n.setKSearch(20); //设置法向估计时,k近邻搜索的点数
n.compute(*normals); //进行法向估计
QMessageBox::information(this, "information", "Normal estimation finished");
/****** 点云数据与法向数据拼接 ******/
//从这之后出现问题
//创建同时包含点和法线的数据结构的指针
pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>);
//将已获得的点数据和法向数据拼接
pcl::concatenateFields(*cloud_xyz, *normals, *cloud_with_normals); //这里编译出错,与cloud的类型有关?改成PointXYZ的点云就没有报错了
//创建另一个kdtree用于重建
pcl::search::KdTree<pcl::PointNormal>::Ptr tree2(new pcl::search::KdTree<pcl::PointNormal>);
//为kdtree输入点云数据,该点云数据类型为点和法向
tree2->setInputCloud(cloud_with_normals);
/****** 曲面重建模块 ******/
//创建贪婪三角形投影重建对象
pcl::GreedyProjectionTriangulation<pcl::PointNormal> gp3;
//创建多边形网格对象,用来存储重建结果
pcl::PolygonMesh triangles;
//设置参数
gp3.setSearchRadius(25); //设置连接点之间最大距离,用于确定k近邻的球半径
gp3.setMu(2.5); //设置最近邻距离的乘子,以得到每个点的最终搜索半径
gp3.setMaximumNearestNeighbors(100); //设置搜索的最近邻点的最大数量
gp3.setMaximumSurfaceAngle(M_PI / 2); //45度 最大平面角
gp3.setMinimumAngle(M_PI / 18); //10度 每个三角的最大角度?
gp3.setMaximumAngle(2 * M_PI / 3); //120度
gp3.setNormalConsistency(false); //若法向量一致,设为true
//设置点云数据和搜索方式
gp3.setInputCloud(cloud_with_normals);
gp3.setSearchMethod(tree2);
// 开始重建
gp3.reconstruct(triangles);
QMessageBox::information(this, "informaiton", "Reconstruction finished");
//将重建结果保存到硬盘文件中,重建结果以VTK格式存储
pcl::io::saveVTKFile("mymesh.vtk", triangles);
/*
//Additional vertex information
std::vector<int> parts = gp3.getPartIDs();
std::vector<int> states = gp3.getPointStates();
fstream fs;
fs.open("partsID.txt", ios::out);
if (!fs)
{
return -2;
}
fs << "number of point clouds:" << parts.size() << "\n";
for (int i = 0; i < parts.size(); i++)
{
if (parts[i] != 0)
{
fs << parts[i] << "\n";
}
}
*/
/****** 图形显示模块 ******/
QMessageBox::information(this, "informaiton", "Start to show");
viewer->addPolygonMesh(triangles, "my"); //设置要显示的网格对象
//设置网格模型显示模式
viewer->setRepresentationToSurfaceForAllActors(); //网格模型以面片形式显示
//viewer->setRepresentationToPointsForAllActors(); //网格模型以点形式显示
//viewer->setRepresentationToWireframeForAllActors(); //网格模型以线框图模式显示
// 输出窗口
consoleLog("Convert surface", "", "", "");
viewer->removeAllShapes();
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
//boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
return 0;
}
int CloudViewer::convertWireframe()
{
/* 问题
好像该方法只能处理PointXYZ的点云,用PointXZYRGBA的点云编译会报错
调用boost::this_thread::sleep好像也会编译出错
*/
pcl::PointXYZ point;
cloud_xyz.reset(new pcl::PointCloud<pcl::PointXYZ>);
for (size_t i = 0; i < mycloud.cloud->size(); i++)
{
point.x = mycloud.cloud->points[i].x;
point.y = mycloud.cloud->points[i].y;
point.z = mycloud.cloud->points[i].z;
cloud_xyz->push_back(point);
}
if (!cloud_xyz)
{
return -1;
}
/****** 法向估计模块 ******/
//创建法线估计对象 n
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> n;
//创建法向数据指针 normals
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
//创建 kdtree 用于法向计算时近邻搜索
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
tree->setInputCloud(cloud_xyz); //为 kdtree 输入点云
n.setInputCloud(cloud_xyz); //为法向估计对象输入点云
n.setSearchMethod(tree); //设置法向估计时所采取的搜索方式为kdtree
n.setKSearch(20); //设置法向估计时,k近邻搜索的点数
n.compute(*normals); //进行法向估计
QMessageBox::information(this, "information", "Normal estimation finished");
/****** 点云数据与法向数据拼接 ******/
//从这之后出现问题
//创建同时包含点和法线的数据结构的指针
pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>);
//将已获得的点数据和法向数据拼接
pcl::concatenateFields(*cloud_xyz, *normals, *cloud_with_normals); //这里编译出错,与cloud的类型有关?改成PointXYZ的点云就没有报错了
//创建另一个kdtree用于重建
pcl::search::KdTree<pcl::PointNormal>::Ptr tree2(new pcl::search::KdTree<pcl::PointNormal>);
//为kdtree输入点云数据,该点云数据类型为点和法向
tree2->setInputCloud(cloud_with_normals);
/****** 曲面重建模块 ******/
//创建贪婪三角形投影重建对象
pcl::GreedyProjectionTriangulation<pcl::PointNormal> gp3;
//创建多边形网格对象,用来存储重建结果
pcl::PolygonMesh triangles;
//设置参数
gp3.setSearchRadius(25); //设置连接点之间最大距离,用于确定k近邻的球半径
gp3.setMu(2.5); //设置最近邻距离的乘子,以得到每个点的最终搜索半径
gp3.setMaximumNearestNeighbors(100); //设置搜索的最近邻点的最大数量
gp3.setMaximumSurfaceAngle(M_PI / 2); //45度 最大平面角
gp3.setMinimumAngle(M_PI / 18); //10度 每个三角的最大角度?
gp3.setMaximumAngle(2 * M_PI / 3); //120度
gp3.setNormalConsistency(false); //若法向量一致,设为true
//设置点云数据和搜索方式
gp3.setInputCloud(cloud_with_normals);
gp3.setSearchMethod(tree2);
// 开始重建
gp3.reconstruct(triangles);
QMessageBox::information(this, "informaiton", "Reconstruction finished");
//将重建结果保存到硬盘文件中,重建结果以VTK格式存储
pcl::io::saveVTKFile("mymesh.vtk", triangles);
/*
//Additional vertex information
std::vector<int> parts = gp3.getPartIDs();
std::vector<int> states = gp3.getPointStates();
fstream fs;
fs.open("partsID.txt", ios::out);
if (!fs)
{
return -2;
}
fs << "number of point clouds:" << parts.size() << "\n";
for (int i = 0; i < parts.size(); i++)
{
if (parts[i] != 0)
{
fs << parts[i] << "\n";
}
}
*/
/****** 图形显示模块 ******/
QMessageBox::information(this, "informaiton", "Start to show");
viewer->addPolygonMesh(triangles, "my"); //设置要显示的网格对象
//设置网格模型显示模式
//viewer->setRepresentationToSurfaceForAllActors(); //网格模型以面片形式显示
//viewer->setRepresentationToPointsForAllActors(); //网格模型以点形式显示
viewer->setRepresentationToWireframeForAllActors(); //网格模型以线框图模式显示
// 输出窗口
consoleLog("Convert wireframe", "", "", "");
viewer->removeAllShapes();
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
//boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/lxj362343/CloudViewer.git
[email protected]:lxj362343/CloudViewer.git
lxj362343
CloudViewer
CloudViewer
master

搜索帮助