osg显示点云

来源:互联网 发布:软件企业 认定 2016 编辑:程序博客网 时间:2024/05/17 07:45

利用pcl读取点云,osg显示,相比pcl中的vtk显示,osg显示点云稍微复杂一些。

#include <osgViewer/Viewer>#include <osgViewer/ViewerEventHandlers>#include <osg/Node>#include <osg/Geode>#include <osg/Geometry>#include <osgDB/ReaderWriter>#include <osgDB/ReadFile>#include <osgDB/WriteFile>#include <osgGA/StateSetManipulator>#include <osgUtil/Optimizer>#include <osgUtil/DelaunayTriangulator>#include <pcl/io/ply_io.h>#include <pcl/point_types.h>#include <iostream>int main(){    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer() ;    viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet())) ;    osg::ref_ptr<osg::Group> root = new osg::Group() ;    //创建顶点数组    osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array() ;    osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array() ;    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>) ;    if (pcl::io::loadPLYFile("3-1-2.ply" , *cloud) == -1)    {        std::cout<<"读取点云失败!\n" ;        return -1 ;    }    int nums = cloud->size() ;    std::cout<<"点云数据:"<<nums<<std::endl ;    int k = 0 ;    for (int i = 0 ; i < nums ; ++i)    {        coords->push_back(osg::Vec3(cloud->points[i].x , cloud->points[i].y , cloud->points[i].z)) ;        color->push_back(osg::Vec4(1.0f , 0.0f , 0.0f , 0.3f)) ;        k++ ;    }    //创建几何体    osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry() ;    //设置顶点数组    geometry->setVertexArray(coords.get()) ;    geometry->setColorArray(color.get()) ;    geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX) ;    osg::Vec3Array *normals = new osg::Vec3Array ;    normals->push_back(osg::Vec3(0.0f , 1.0f , 0.0f)) ;    geometry->setNormalArray(normals) ;    geometry->setNormalBinding(osg::Geometry::BIND_OVERALL) ;    geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS , 0 , k)) ; //设置关联方式    //添加到叶节点    osg::ref_ptr<osg::Geode> geode = new osg::Geode() ;    geode->addDrawable(geometry.get()) ;    root->addChild(geode.get()) ;    //优化场景数据    osgUtil::Optimizer optimizer ;    optimizer.optimize(root.get()) ;    viewer->setSceneData(root.get()) ;    viewer->realize() ;    viewer->run() ;    return 1 ;}

工程下载附带点云

0 0
原创粉丝点击