用PCL刷新显示变化的点云

在头文件中,定义了:

	int m_colorInc;//演示红色分量不断增长。这是增长步长。

在cpp文件中,刚开始,在文件最上面,打开一个3D窗口,如下面代码所示。

// PCLDemoDlg.cpp : 实现文件

//

#include "stdafx.h"
#include "PCLDemo.h"
#include "PCLDemoDlg.h"
#include "afxdialogex.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//////PCL
#include 

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


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

#include  
boost::shared_ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
//////PCL


接下来,开始3D显示内部循环。我坐在一个按钮里面了。代码如下:

void CPCLDemoDlg::OnBnClickedButton1()//开始3D显示内部循环。
{
	while (!viewer->wasStopped())
	{
		viewer->spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	}
}


最后是更新点云,我坐在另一个按钮中了。代码如下:

void CPCLDemoDlg::OnBnClickedButton6()//更新点云
{
	pcl::PointCloud::Ptr basic_cloud_ptr(new pcl::PointCloud);
	pcl::PointCloud::Ptr point_cloud_ptr(new pcl::PointCloud);
	std::cout << "Genarating example point clouds.\n\n";

	
	// We're going to make an ellipse extruded along the z-axis. The colour for
	// the XYZRGB cloud will gradually go from red to green to blue.
	uint8_t r(255), g(15), b(15);
	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 = 0.5 * cosf(pcl::deg2rad(angle));
			basic_point.y = sinf(pcl::deg2rad(angle));
			basic_point.z = z;
			basic_cloud_ptr->points.push_back(basic_point);

			pcl::PointXYZRGB point;
			point.x = basic_point.x;
			point.y = basic_point.y;
			point.z = basic_point.z;
			uint32_t rgb = (static_cast(r) << 16 |
				static_cast(g) << 8 | static_cast(b));
			point.rgb = *reinterpret_cast(&rgb);
			point_cloud_ptr->points.push_back(point);
		}
		if (z < 0.0)
		{
			r -= 12;
			g += 12;

			r = m_colorInc;
			//g = m_colorInc;
		}
		else
		{
			g -= 12;
			b += 12;
		}
	}

	m_colorInc += 10;




	basic_cloud_ptr->width = (int)basic_cloud_ptr->points.size();
	basic_cloud_ptr->height = 1;
	point_cloud_ptr->width = (int)point_cloud_ptr->points.size();
	point_cloud_ptr->height = 1;


	viewer->removeAllPointClouds();
	viewer->removeAllShapes();


	pcl::visualization::PointCloudColorHandlerRGBField rgb(point_cloud_ptr);
	viewer->addPointCloud(point_cloud_ptr, rgb, "sample cloud");
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
	//viewer->addCoordinateSystem(1.0);//显示坐标轴,有时看着碍事。。所以我把他注释掉了。
	viewer->initCameraParameters();

	viewer->setBackgroundColor(0, 0, 0);
	viewer->setCameraPosition(0, 0, -2, 0, -1, 0, 0);

}



你可能感兴趣的:(工业三维视觉)