使用PCL绘制网格

引言

用过PCL点云库的应该知道,PCL是基于点云进行绘制的,顾名思义就是所有图像均是通过点来构造的。基于此,绘制一些简单图形就需要构造一系列点,然后将这些点交给PCL绘制。

源码

void DrawGridAssist()
{
	const GRID_MARK_CNT = 10; // 10x10的网格
	const double delt = 0.001; // 线段点云的精度
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	QString strRgb = gIniFile->Color_GetViewLine(); // 这里是从配置中读取的,可以自己给颜色
	QColor rtColor(strRgb);

	for (int i = -GRID_MARK_CNT; i <= GRID_MARK_CNT; i++)
	{
		pcl::PointXYZ horStartPt(-1, 0, i * 0.1);
		pcl::PointXYZ horEndPt(1, 0, i * 0.1);
		pcl::PointXYZ verStartPt(i * 0.1, 0, -1);
		pcl::PointXYZ verEndPt(i * 0.1, 0, 1);

		// 构造线段
		CreatSegment(horStartPt, horEndPt, cloud, delt);
		CreatSegment(verStartPt, verEndPt, cloud, delt);

		// 绘制Z方向刻度
		pcl::PointXYZ showHorPos(0, 0, i * 0.1);
		QString strHorID = QString("Grid-z%1").arg(i);
		mViewer->removeText3D(strHorID.toStdString());
		mViewer->addText3D(QString::number(-gMaxDistance/ GRID_MARK_CNT *i).toStdString(), showHorPos, 0.01, rtColor.red(), rtColor.green(), rtColor.blue(), strHorID.toStdString());
	
		// 绘制X方向刻度
		pcl::PointXYZ showVerPos(i * 0.1, 0, 0);
		QString strVerID = QString("Grid-x%1").arg(i);
		mViewer->removeText3D(strVerID.toStdString());
		mViewer->addText3D(QString::number(-gMaxDistance / GRID_MARK_CNT * i).toStdString(), showVerPos, 0.01, rtColor.red(), rtColor.green(), rtColor.blue(), strVerID.toStdString());
	}

	// 绘制点云网格
	pcl::visualization::PointCloudColorHandlerCustom colorHandler(cloud, rtColor.red(), rtColor.green(), rtColor.blue());
	mViewer->removePointCloud(GRIDE_PTCLOUD); // 这里如果使用updatePointCloud,先调用一次addPointCloud
	mViewer->addPointCloud(cloud, colorHandler, GRIDE_PTCLOUD);

// 构造线段点云
void CreatSegment(pcl::PointXYZ& p1, pcl::PointXYZ& p2, pcl::PointCloud ::Ptr cloud, double delt)
{
	Eigen::Vector3d k(p1.x - p2.x, p1.y - p2.y, p1.z - p2.z);
	int num = k.norm() / delt;
	k = k / k.norm();
	for (auto i = 0; i < num; ++i) {
		pcl::PointXYZ pt;
		pt.x = p2.x + delt * i * k[0];
		pt.y = p2.y + delt * i * k[1];
		pt.z = p2.z + delt * i * k[2];
		cloud->push_back(pt);
	}
	
	cloud->push_back(p1);
	cloud->push_back(p2);
}

你可能感兴趣的:(PCL+VTK,PCL,点云绘图,网格)