好,我们这一次继续搭建点云框架,不知道QGLViewer的童鞋就先复习一下下上一篇文章:
http://blog.youkuaiyun.com/qq_30547073/article/details/79038422
想看干货的童鞋们可以跳过这一段。再简单说一下为毛用QGLViewer,笔者是个Opengl小白,为了搭建QT的OpenglEs2的点云平台,费了好大的功夫,最后相机也做得不怎么样。而笔者有一天突发奇想想要学CGAL,我发现环境里面有个一叫做QGLViewer的东西。打开一看,哇,相机做的不错,可以选点,可以旋转平移缩放,光照也做得不错,所以干脆就用这个得了。于是乎花了一天的时间摸索着把库配好,开始了QGLViewer学习。然而QGLViewer仍然是基于传统Opengl的,显示速度仍然有限,我们后期还要做很多优化。现在先不说这么多。
好,那么说一下需求。我们有时需要分两个窗体显示。在其中一个窗体里面选点云,然后在另一个窗体里面对图像进行操作,最后进行融合。那么这个效果出来应该是这个样子,我们先上效果:
我们发现左边是QGLViewer的那一坨翔,然后右边就可以放我们的其他东西了。本人是用MainWindow作为CenterWidget做出的这一个效果。整个工程叫做NinjaScarlet_GLViewerFramWork,源码以及可执行文件都完全分享给各位,不用谢了,我是活雷锋:
链接:https://pan.baidu.com/s/1i5MRSbF 密码:ku8f
测试用的 测试PLY点云.rar 也在里面了,也请大家积极下载吧。免费的哦。
关于可分割的中心窗体的DockWidget实现,本人首先参考了这个:
http://blog.youkuaiyun.com/czyt1988/article/details/51209619
然后我把它作为中心窗体,放到了主窗体里面去。我把中心窗体的核心代码放上来。
当然了,所有代码是密不可分的,其余代码已经分享到网盘中了,不会用的同学也请加我QQ 498771026
首先是h文件:
#ifndef SCARLET_CENTERWINDOW_H
#define SCARLET_CENTERWINDOW_H
#include <QMainWindow>
#include "scarletglviewer.h"
using namespace std;
using namespace qglviewer;
namespace Ui {
class Scarlet_CenterWindow;
}
typedef enum
{
IS_IMGFILE,
IS_CLOUDFILE,
IS_OTHERFILE,
IS_NOTFILE,
}enum_WhatFile;
class Scarlet_CenterWindow : public QMainWindow
{
Q_OBJECT
public:
explicit Scarlet_CenterWindow(QWidget *parent = 0);
~Scarlet_CenterWindow();
public slots:
bool slot_OpenFile();
signals:
private:
void Start();
enum_WhatFile WhatFile(QString StringToAnalyse);
void do_SetCenterWidget(QWidget* viewer);//设置中心窗体
void do_SetCenterWidget(QWidget* viewer1 ,QWidget*viewer2);
bool do_OpenIMGFile(QString CloudfileName);
bool do_OpenCloudFile(QString CloudfileName);
void do_DeleteCenterWidget();
void do_SetBackGroundColor(QWidget* WidgetToSet,QColor ColorToSet);
ScarletGLViewer* m_ninGLViewer;
Ui::Scarlet_CenterWindow *ui;
};
#endif // SCARLET_CENTERWINDOW_H
然后是CPP文件:
#include "scarlet_centerwindow.h"
#include "ui_scarlet_centerwindow.h"
#include <QGridLayout>
#include <QDebug>
#include <QPalette>
#include <QFileDialog>
#include <QString>
QList<QString> g_CloudSuffixList;
QList<QString> g_IMGSuffixsList;
Scarlet_CenterWindow::Scarlet_CenterWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Scarlet_CenterWindow)
{
ui->setupUi(this);
Start();
}
void Load_SUFFIX()
{
g_CloudSuffixList.clear();
g_CloudSuffixList.push_back("ptx");
g_CloudSuffixList.push_back("txt");
g_CloudSuffixList.push_back("pts");
g_CloudSuffixList.push_back("xyz");
g_CloudSuffixList.push_back("ply");
g_IMGSuffixsList.clear();
g_IMGSuffixsList.push_back("bmp");
g_IMGSuffixsList.push_back("dib");
g_IMGSuffixsList.push_back("jpg");
g_IMGSuffixsList.push_back("jpeg");
g_IMGSuffixsList.push_back("jpe");
g_IMGSuffixsList.push_back("png");
g_IMGSuffixsList.push_back("pbm");
g_IMGSuffixsList.push_back("pgm");
g_IMGSuffixsList.push_back("ppm");
g_IMGSuffixsList.push_back("tif");
g_IMGSuffixsList.push_back("tiff");
g_IMGSuffixsList.push_back("exr");
g_IMGSuffixsList.push_back("jp2");
}
void Scarlet_CenterWindow::Start()
{
Load_SUFFIX();
//中心窗体中心窗体主要继承自MainWindow,===================
//可以分成两部分,也可以嵌套=============================
do_DeleteCenterWidget();
this->setDockNestingEnabled(true);
//we dont have centerWidget now so dont have to delete it;
this->addDockWidget(Qt::LeftDockWidgetArea,ui->dockWidget_2D);
this->addDockWidget(Qt::RightDockWidgetArea,ui->dockWidget_3D);
this->splitDockWidget(ui->dockWidget_3D,ui->dockWidget_2D,Qt::Horizontal);
this->tabifyDockWidget(ui->dockWidget_2D,ui->dockWidget_3D);
ui->dockWidget_2D->setToolTip("DockWidget_2D");
ui->dockWidget_3D->setToolTip("dockWidget_3D");
m_ninGLViewer = new ScarletGLViewer();
QGridLayout *GridGLLayout = new QGridLayout();
GridGLLayout->addWidget(m_ninGLViewer, 0, 0);
//最终放置的时候一定要注意是在tabWidget中
ui->tab_3DView->setLayout(GridGLLayout);
}
//删除MainWindow的中心窗体
void Scarlet_CenterWindow::do_DeleteCenterWidget()
{
QWidget* p = takeCentralWidget();
if(p)
delete p;
}
//给控件设置背景颜色
void Scarlet_CenterWindow::do_SetBackGroundColor(QWidget* WidgetToSet,QColor ColorToSet)
{
WidgetToSet->setAutoFillBackground(true);
QPalette Pal(palette());
Pal.setColor(QPalette::Background, ColorToSet);
WidgetToSet->setPalette(Pal);
}
//设置中心窗体
void Scarlet_CenterWindow::do_SetCenterWidget(QWidget* viewer)
{
this->setCentralWidget(viewer);
viewer->setAttribute(Qt::WA_DeleteOnClose);//关闭立刻释放
viewer->show();
statusBar()->showMessage("Case loaded!", 3000);
}
void Scarlet_CenterWindow::do_SetCenterWidget(QWidget* viewer1 ,QWidget*viewer2)
{
QWidget *CenterWidget = new QWidget();
QHBoxLayout* myHBoxLayout = new QHBoxLayout();
myHBoxLayout->addWidget(viewer1);
myHBoxLayout->addWidget(viewer2);
viewer1->show();
viewer2->show();
CenterWidget->setLayout(myHBoxLayout);
this->setCentralWidget(CenterWidget);
statusBar()->showMessage("Case loaded!", 3000);
}
enum_WhatFile Scarlet_CenterWindow::WhatFile(QString StringToAnalyse)
{
QFileInfo PathAnalyser(StringToAnalyse);
if(PathAnalyser.isFile())
{
if(g_CloudSuffixList.contains(PathAnalyser.suffix().toLower()))
return IS_CLOUDFILE;
else if(g_IMGSuffixsList.contains(PathAnalyser.suffix().toLower()))
return IS_IMGFILE;
else
return IS_OTHERFILE;
}
else
return IS_NOTFILE;
}
QString GenerateFilterFromList(QList<QString> SuffixList)
{
QString FilterString;
if(SuffixList.size() ==0)
return "";
FilterString+="(*."+SuffixList[0];
for(int i=1;i<SuffixList.size();i++)
{
FilterString+=" *."+SuffixList[i];
}
FilterString+=")";
return FilterString;
}
QString GenerateStringFilter()
{
QString CloudFilterString ="Cloud files";
CloudFilterString+=GenerateFilterFromList(g_CloudSuffixList);
CloudFilterString+=";;";
QString ImageFilterString ="Image files";
ImageFilterString+=GenerateFilterFromList(g_IMGSuffixsList);
ImageFilterString+=";;";
QString AllFileFilterString =CloudFilterString+ImageFilterString+ "All files(*.*)";
qDebug()<<AllFileFilterString;
return AllFileFilterString;
}
QStringList do_OpenFileDlg()
{
QString InitString ="D:/Ninja/NinjaResource/PointCloud/ScarletIOTestCloud";
QStringList QStrIMGfileName = QFileDialog::getOpenFileNames(
0,"open any file",InitString,
GenerateStringFilter());
return QStrIMGfileName;
}
bool Scarlet_CenterWindow::do_OpenIMGFile(QString CloudfileName)
{
return false;
}
bool Scarlet_CenterWindow::do_OpenCloudFile(QString CloudfileName)
{
return m_ninGLViewer->nin_InitFromPath(CloudfileName);
}
bool Scarlet_CenterWindow::slot_OpenFile()
{
QStringList fileNames = do_OpenFileDlg();
bool IfSuccess;
for(int i=0;i<fileNames.size();i++)
{
QString fileName = fileNames[i];
QFileInfo PathAnalyser(fileName);
if(PathAnalyser.isFile())
{
switch(WhatFile(fileName))
{
case IS_IMGFILE:
break;
case IS_CLOUDFILE:
IfSuccess = do_OpenCloudFile(fileName);
break;
case IS_OTHERFILE:break;
case IS_NOTFILE:break;
}
}
else
{
IfSuccess = false;
}
}
return IfSuccess;
}
Scarlet_CenterWindow::~Scarlet_CenterWindow()
{
delete ui;
}