利用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)
下面给出代码:
- #include <iostream>
- #include <boost/thread/thread.hpp>
- #include <pcl/common/common_headers.h>
- #include <pcl/features/normal_3d.h>
- #include <pcl/io/pcd_io.h>
- #include <pcl/visualization/pcl_visualizer.h>
- // #include <pcl/console/parse.h>
- boost::shared_ptr<pcl::visualization::PCLVisualizer> simpleVis(pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
- {
- boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
- viewer->setBackgroundColor(0.0, 0.0, 0.0);
- viewer->addPointCloud<pcl::PointXYZ>(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<pcl::PointXYZ>::Ptr basic_cloud_ptr)
- {
- for(float z = -1.0; z <= 1.0; z += 0.05)
- {
- for (float angle = 0.0; angle <= 360.0; angle += 5.0)
- {
- pcl::PointXYZ basic_point;
- basic_point.x = cosf(pcl::deg2rad(angle));
- basic_point.y = sinf(pcl::deg2rad(angle));
- basic_point.z = z;
- basic_cloud_ptr->points.push_back(basic_point);
- }
- }
- basic_cloud_ptr->width = (int)basic_cloud_ptr->points.size();
- basic_cloud_ptr->height = 1;
- }
- //构造球体点云
- void ConsSphereCloud(pcl::PointCloud<pcl::PointXYZ>::Ptr basic_cloud_ptr)
- {
- float radius = 0.2;
- for (float angle1 = 0.0; angle1 <= 180.0; angle1 += 5.0)
- {
- for (float angle2 = 0.0; angle2 <= 360.0; angle2 += 5.0)
- {
- pcl::PointXYZ basic_point;
- basic_point.x = radius * sinf(pcl::deg2rad(angle1)) * cosf(pcl::deg2rad(angle2));
- basic_point.y = radius * sinf(pcl::deg2rad(angle1)) * sinf(pcl::deg2rad(angle2));
- basic_point.z = radius * cosf(pcl::deg2rad(angle1));
- basic_cloud_ptr->points.push_back(basic_point);
- }
- }
- basic_cloud_ptr->width = (int)basic_cloud_ptr->points.size();
- basic_cloud_ptr->height = 1;
- }
- int main(int _Argc, char *argv[])
- {
- pcl::PointCloud<pcl::PointXYZ>::Ptr basic_cloud_ptr(new pcl::PointCloud<pcl::PointXYZ>);
- pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr(new pcl::PointCloud<pcl::PointXYZRGB>);
- #define SPHERE
- #if defined CYLINDER
- ConsCylinderCloud(basic_cloud_ptr);
- #elif defined SPHERE
- ConsSphereCloud(basic_cloud_ptr);
- #endif
- boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer = simpleVis(basic_cloud_ptr);
- while ( !viewer->wasStopped())
- {
- viewer->spinOnce(100);
- boost::this_thread::sleep(boost::posix_time::microseconds(100000));
- }
- return 0;
- }
这个是画出的球面。
如果要画出圆柱面,则将代码中的#define SPHERE改为#define CYLINDER,然后编译运行即可显示以xoy平面对称的圆柱面。
以上显示方式是在程序中写死的,也可以将点云保存为文件,
程序中的main函数中稍微修改下,将viewer的代码替换为
- ConsSphereCloud(basic_cloud_ptr);
- pcl::io::savePCDFileASCII("sphere.pcd", *basic_cloud_ptr);
然后在控制台下切换到C:\Program Files (x86)\PCL 1.6.0\bin目录中,再执行
- 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
显示的图像如下所示: