1. 程式人生 > >用PCL重新整理顯示變化的點雲

用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 <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>


// 用於應用程式“關於”選單項的 CAboutDlg 對話方塊

#include  <conio.h>
boost::shared_ptr<pcl::visualization::PCLVisualizer> 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<pcl::PointXYZ>::Ptr basic_cloud_ptr(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr(new pcl::PointCloud<pcl::PointXYZRGB>);
	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<uint32_t>(r) << 16 |
				static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));
			point.rgb = *reinterpret_cast<float*>(&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<pcl::PointXYZRGB> rgb(point_cloud_ptr);
	viewer->addPointCloud<pcl::PointXYZRGB>(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);

}