PCL读取点云并三角化

win10 x64 vs2013 pcl1.8.0

代码如下:

#include
#include
#include
#include
#include
#include

int
main(/*int argc, char** argv*/)
{
    // Load input file into a PointCloud with an appropriate type
    pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
    pcl::PCLPointCloud2 cloud_blob;
    pcl::io::loadPCDFile("bunny.pcd", cloud_blob);
    pcl::fromPCLPointCloud2(cloud_blob, *cloud);
    //* the data should be available in cloud

    // Normal estimation*
    pcl::NormalEstimation n;
    pcl::PointCloud::Ptr normals(new pcl::PointCloud);
    pcl::search::KdTree::Ptr tree(new pcl::search::KdTree);
    tree->setInputCloud(cloud);
    n.setInputCloud(cloud);
    n.setSearchMethod(tree);
    n.setKSearch(20);
    n.compute(*normals);
    //* normals should not contain the point normals + surface curvatures

    // Concatenate the XYZ and normal fields*
    pcl::PointCloud::Ptr cloud_with_normals(new pcl::PointCloud);
    pcl::concatenateFields(*cloud, *normals, *cloud_with_normals);
    //* cloud_with_normals = cloud + normals

    // Create search tree*
    pcl::search::KdTree::Ptr tree2(new pcl::search::KdTree);
    tree2->setInputCloud(cloud_with_normals);

    // Initialize objects
    pcl::GreedyProjectionTriangulation gp3;
    pcl::PolygonMesh triangles;

    // Set the maximum distance between connected points (maximum edge length)
    gp3.setSearchRadius(0.025);

    // Set typical values for the parameters
    gp3.setMu(2.5);
    gp3.setMaximumNearestNeighbors(100);
    gp3.setMaximumSurfaceAngle(M_PI / 4); // 45 degrees
    gp3.setMinimumAngle(M_PI / 18); // 10 degrees
    gp3.setMaximumAngle(2 * M_PI / 3); // 120 degrees
    gp3.setNormalConsistency(false);

    // Get result
    gp3.setInputCloud(cloud_with_normals);
    gp3.setSearchMethod(tree2);
    gp3.reconstruct(triangles);

    // Additional vertex information
    std::vector parts = gp3.getPartIDs();
    std::vector states = gp3.getPointStates();

    boost::shared_ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
    viewer->setBackgroundColor(0, 0, 0);

    viewer->addPolygonMesh(triangles, "triangles");

//    viewer->addPolylineFromPolygonMesh(triangles);   //添加此行代码只显示三角化后的网格

    viewer->addCoordinateSystem(1.0);
    viewer->initCameraParameters();
    while (!viewer->wasStopped()){
        viewer->spinOnce(100);
        boost::this_thread::sleep(boost::posix_time::microseconds(100000));
    }


    // Finish
    return (0);
}

 

你可能感兴趣的:(PCL读取点云并三角化)