1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h> #include <pcl/io/ply_io.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/features/normal_3d_omp.h> #include <pcl/features/normal_3d.h>
#include <pcl/surface/gp3.h> #include <pcl/surface/poisson.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp> #include <fstream> #include <iostream> #include <stdio.h> #include <string.h> #include <string>
int main(int argc, char** argv) { char tmpStr[100]; strcpy(tmpStr,argv[1]); char* pext = strrchr(tmpStr, '.'); std::string extply("ply"); std::string extpcd("pcd"); if(pext){ *pext='\0'; pext++; } std::string ext(pext); if (!((ext == extply)||(ext == extpcd))){ std::cout << "文件格式不支持!" << std::endl; std::cout << "支持文件格式:*.pcd和*.ply!" << std::endl; return(-1); }
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>) ; if (ext == extply){ if (pcl::io::loadPLYFile(argv[1] , *cloud) == -1){ PCL_ERROR("Could not read ply file!\n") ; return -1; } } else{ if (pcl::io::loadPCDFile(argv[1] , *cloud) == -1){ PCL_ERROR("Could not read pcd file!\n") ; return -1; } }
pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>); pcl::NormalEstimation<pcl::PointXYZ , pcl::Normal> n ; pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>) ; pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>) ; tree->setInputCloud(cloud) ; n.setInputCloud(cloud) ; n.setSearchMethod(tree) ; n.setKSearch(20); n.compute(*normals);
pcl::concatenateFields(*cloud , *normals , *cloud_with_normals) ;
pcl::search::KdTree<pcl::PointNormal>::Ptr tree2(new pcl::search::KdTree<pcl::PointNormal>) ; tree2->setInputCloud(cloud_with_normals) ; pcl::Poisson<pcl::PointNormal> pn ; pn.setConfidence(false); pn.setDegree(2); pn.setDepth(8); pn.setIsoDivide(8); pn.setManifold(false); pn.setOutputPolygons(false); pn.setSamplesPerNode(3.0); pn.setScale(1.25); pn.setSolverDivide(8);
pn.setSearchMethod(tree2); pn.setInputCloud(cloud_with_normals); pcl::PolygonMesh mesh ; pn.performReconstruction(mesh);
pcl::io::savePLYFile("result.ply", mesh);
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D viewer")) ; viewer->setBackgroundColor(0 , 0 , 0) ; viewer->addPolygonMesh(mesh , "my") ; viewer->addCoordinateSystem (50.0); viewer->initCameraParameters() ; while (!viewer->wasStopped()){ viewer->spinOnce(100) ; boost::this_thread::sleep(boost::posix_time::microseconds(100000)) ; }
return 0; }
|