|
楼主 |
发表于 2015-3-8 17:28:28
|
显示全部楼层
zt19920302 发表于 2014-12-24 21:06
你好!能给我发一份完整的代码吗?谢谢!
if (cFileDialog.DoModal() == IDOK)
{
/////////////////////////////////////////////////////////////////////////////
//文档名称
std::string filename;
filename = cFileDialog.GetPathName();
//reset data
this->binary_blob.reset();
binary_blob = sensor_msgs::PointCloud2::Ptr (new sensor_msgs::PointCloud2);
// read new data
//*.ply文件
pcl::PLYReader ply_reader;
ply_reader.read (filename, *binary_blob);
//////////////// 三角化 ////////////////////////////////////////////////////////////////////////
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); //定义点云对象指针
sensor_msgs::PointCloud2 cloud_blob;
pcl::fromROSMsg (*binary_blob, *cloud); //*数据最终存储在cloud中
// Normal estimation*
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>);//定义KD树指针
tree->setInputCloud (cloud); //用cloud构建tree对象
n.setInputCloud (cloud); //为法线估计对象设置输入点云
n.setSearchMethod (tree); //设置搜索方法
n.setKSearch (20); //设置K搜索的K值为20
n.compute (*normals); //估计法线存储结果到normals
//* normals should not contain the point normals + surface curvatures
// Concatenate the XYZ and normal fields*
pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals (new pcl::PointCloud<pcl::PointNormal>);
pcl::concatenateFields (*cloud, *normals, *cloud_with_normals); //连接字段,cloud_with_normals存储有向点云
//* cloud_with_normals = cloud + normals
// Create search tree*
pcl::search::KdTree<pcl::PointNormal>::Ptr tree2 (new pcl::search::KdTree<pcl::PointNormal>);//定义搜索树对象
tree2->setInputCloud (cloud_with_normals);//利用点云构建搜索树
// Initialize objects
pcl::GreedyProjectionTriangulation<pcl::PointNormal> gp3;//定义三角化对象
pcl::PolygonMesh triangles; //存储最终三角化的网格模型
// Set the maximum distance between connected points (maximum edge length)
gp3.setSearchRadius (0.025);//设置连接点之间的最大距离(即为三角形最大边长)为0.025
// Set typical values for the parameters,设置各参数特征值
gp3.setMu (2.5); //设置样本点搜索其邻近点的最远距离为2.5,为了适应点云密度的变化
gp3.setMaximumNearestNeighbors (100); //设置样本点可搜索的邻域个数为100
gp3.setMaximumSurfaceAngle(M_PI/4); // 45 degrees //设置某点法线方向偏离样本点法线方向的最大角度为45°
gp3.setMinimumAngle(M_PI/18); // 10 degrees //设置三角化后得到三角形内角最小角度为10°
gp3.setMaximumAngle(2*M_PI/3); // 120 degrees //设置三角化后得到三角形内角最大角度为120°
gp3.setNormalConsistency(false); //设置该参数保证法线朝向一致
// Get result
gp3.setInputCloud (cloud_with_normals);//设置输入点云为有向点云cloud_with_normals
gp3.setSearchMethod (tree2); //设置搜索方式为tree2
gp3.reconstruct (triangles); //重建提取三角化
//以上代码设置参数特征值和实际三角化的过程
// std::cout << triangles;
// Additional vertex information
std::vector<int> parts = gp3.getPartIDs();
std::vector<int> states = gp3.getPointStates();
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
viewer->addPolygonMesh(triangles,"my");
//viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
// 主循环
while (!viewer->wasStopped ())
{
viewer->spinOnce (100);
boost::this_thread::sleep (boost::posix_time::microseconds (100000));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// // read new data
// //*.pcd文件
// pcl::PCDReader pcd_reader;
// pcd_reader.read (filename, *binary_blob);
/*if (pcd_reader.read ((char*)_bstr_t(filename.c_str()), *binary_blob) != 0) //* load the file
{
MessageBox (_T("Couldn't read PCData file!"));
return;
}*/
}
if (binary_blob == NULL)
{
// MessageBox("Please load PCD file firstly!");
MessageBox("Please load PLY file firstly!");
return;
}
else
{
//其他句柄
if (pcl::getFieldIndex(*binary_blob, "rgb") > 0)
{
color_Handler = pcl::mfc_visualization::PointCloudColorHandlerRGBField<sensor_msgs::PointCloud2>::Ptr
(new pcl::mfc_visualization::PointCloudColorHandlerRGBField<sensor_msgs::PointCloud2> (binary_blob));
this->viewer->addPointCloud(binary_blob, color_Handler, sensor_origin, sensor_orientation);
}
else
{
xyz_Handler = pcl::mfc_visualization::PointCloudGeometryHandlerXYZ<sensor_msgs::PointCloud2>::Ptr
(new pcl::mfc_visualization::PointCloudGeometryHandlerXYZ<sensor_msgs::PointCloud2> (binary_blob));
this->viewer->addPointCloud(binary_blob, xyz_Handler, sensor_origin, sensor_orientation);
}
//this->viewer->setCameraPose(0.018, 0.018, -4.87, 0.018, 0.018, 0, 0, -1, 0, 0);
this->viewer->resetCamera();
}
} |
|