利用PCL库画简单的三维立体图形需要知道各种图形的参数方程,然后给每个参数赋值便可以。
圆柱面的参数方程为:x = R*cos(θ); y = R*sin(θ); z = z;其中 θ范围是[-2*PI, 2*PI), z的范围是(-∞,+∞)
球面的参数方程是:x = R*sin(θ)*cos(ψ); y = R*sin(θ)*sin(ψ); z = R*cos(θ);其中θ∈[0, PI), ψ∈[0, 2*PI)
下面给出代码:
[cpp]
view plain
copy
print
?
#include #include #include #include #include #include // #include boost::shared_ptr simpleVis(pcl::PointCloud::ConstPtr cloud) { boost::shared_ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer")); viewer->setBackgroundColor(0.0, 0.0, 0.0); viewer->addPointCloud(cloud, "sample cloud"); viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1.0, "sample cloud"); viewer->addCoordinateSystem(1.0); viewer->initCameraParameters(); return viewer; } //构造圆柱体点云 void ConsCylinderCloud(pcl::PointCloud::Ptr basic_cloud_ptr) { for(float z = -1.0; z width = (int)basic_cloud_ptr->points.size(); basic_cloud_ptr->height = 1; } //构造球体点云 void ConsSphereCloud(pcl::PointCloud::Ptr basic_cloud_ptr) { float radius = 0.2; for (float angle1 = 0.0; angle1 width = (int)basic_cloud_ptr->points.size(); basic_cloud_ptr->height = 1; } int main(int _Argc, char *argv[]) { pcl::PointCloud::Ptr basic_cloud_ptr(new pcl::PointCloud); pcl::PointCloud::Ptr point_cloud_ptr(new pcl::PointCloud); #define SPHERE #if defined CYLINDER ConsCylinderCloud(basic_cloud_ptr); #elif defined SPHERE ConsSphereCloud(basic_cloud_ptr); #endif boost::shared_ptr viewer = simpleVis(basic_cloud_ptr); while ( !viewer->wasStopped()) { viewer->spinOnce(100); boost::this_thread::sleep(boost::posix_time::microseconds(100000)); } return 0; }
![](https://img-blog.csdn.net/20140114171309828?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG1pbmdfMDg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
这个是画出的球面。
如果要画出圆柱面,则将代码中的#define SPHERE改为#define CYLINDER,然后编译运行即可显示以xoy平面对称的圆柱面。
以上显示方式是在程序中写死的,也可以将点云保存为文件,
程序中的main函数中稍微修改下,将viewer的代码替换为
[cpp]
view plain
copy
print
?
ConsSphereCloud(basic_cloud_ptr); pcl::io::savePCDFileASCII("sphere.pcd", *basic_cloud_ptr);
然后在控制台下切换到C:\Program Files (x86)\PCL 1.6.0\bin目录中,再执行
[cpp]
view plain
copy
print
?
C:\Users\pc>pcd_viewer_release D:\pcl_test\visualizer\cmake-bin\sphere.pcd The viewer window provides interactive commands; for help, press 'h' or 'H' from within the window. > Loading D:\pcl_test\visualizer\cmake-bin\sphere.pcd [done, 140 ms : 2701 points] Available dimensions: x y z
显示的图像如下所示:
|