osg读取点云显示在qt

1 篇文章 0 订阅

使用PCL读取pcd文件并传给osg显示,将窗口嵌入到qt

cpp文件:

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator>
#include <osgUtil/Optimizer>

#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osg/MatrixTransform>

_osgQtView::_osgQtView(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    osgWidget = new osgQOpenGLWidget(ui.widget);          // qt拖入widget控件
    osgWidget->setGeometry(ui.widget->geometry());        // 指定osg窗口显示大小
    //m_fileName = "D:/code_cpp/cow.osg";  // 显示模型路径
    //connect(osgWidget, SIGNAL(initialized()), this, SLOT(initOSG()));   
    connect(osgWidget, SIGNAL(initialized()), this, SLOT(initPCD()));
}

//读取模型
void _osgQtView::initOSG()
{
    osgViewer::Viewer* pViewer = osgWidget->getOsgViewer();
    pViewer->setCameraManipulator(new osgGA::TrackballManipulator);
    osg::Node* node = osgDB::readNodeFile(m_fileName.toStdString());
    pViewer->setSceneData(node);
}

void _osgQtView::initPCD()
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::io::loadPCDFile("D:/code_cpp/rabbit.pcd", *cloud);

    osg::ref_ptr<osg::Node> rootNode = createPointCloudNode(cloud);
    osgViewer::Viewer* viewer = osgWidget->getOsgViewer();
    viewer->setCameraManipulator(new osgGA::TrackballManipulator);
    viewer->setSceneData(rootNode);
    //viewer->run();
}

osg::ref_ptr<osg::Node> _osgQtView::createPointCloudNode(pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud)
{
    osg::ref_ptr<osg::Geometry> pointGeom = new osg::Geometry;

    // 创建顶点数组
    osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array();
    // 创建颜色
    osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array();


    int cloudNum = cloud->points.size();
    for (size_t i = 0; i < cloudNum; i++) {
        coords->push_back(osg::Vec3(cloud->points[i].x, cloud->points[i].y, cloud->points[i].z));
        color->push_back(osg::Vec4(255.0, 0, 0, 1.0f));
    }

    pointGeom->setVertexArray(coords);
    pointGeom->setColorArray(color);
    pointGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
    pointGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, coords->size()));

    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    geode->addDrawable(pointGeom);
    osg::ref_ptr<osg::MatrixTransform> pointCloudNode = new osg::MatrixTransform;
    pointCloudNode->addChild(geode);

    return pointCloudNode;
}

头文件:

#include <QtWidgets/QMainWindow>
#include <QWidget>
#include <osgQOpenGL/osgQOpenGLWidget>   //链接器添加 osgQOpenGLd.lib
#include "ui__osgQtView.h"
#include <osg/Node>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>

class _osgQtView : public QMainWindow
{
    Q_OBJECT

public:
    _osgQtView(QWidget *parent = nullptr);
    ~_osgQtView();
    osg::ref_ptr<osg::Node> createPointCloudNode(pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud);

protected slots:
    void initOSG();
    
    void initPCD();
    void on_pushButton_clicked();

private:
    Ui::_osgQtViewClass ui;
    osgQOpenGLWidget* osgWidget;
    QString m_fileName;
};

参考链接:https://www.5axxw.com/questions/simple/lxzrb3

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值