一些點雲的小知識,從官方文件中發現的例子

尧舜语發表於2024-09-27

1、判斷點雲的點是否是有效的

	pcl::PointXYZ p_valid;
	p_valid.x = 0;
	p_valid.y = 0;
	p_valid.z = 0;
	std::cout << "Is p_valid valid? " << pcl::isFinite(p_valid) << std::endl;

	// If any component is NaN, the point is not finite.
	pcl::PointXYZ p_invalid;
	p_invalid.x = std::numeric_limits<float>::quiet_NaN();
	p_invalid.y = 0;
	p_invalid.z = 0;
	std::cout << "Is p_invalid valid? " << pcl::isFinite(p_invalid) << std::endl;

  列印結果:

2、複製同類的點雲

	// 複製點雲
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());

	pcl::PointCloud<pcl::PointXYZ>::PointType p;// 相當於 pcl::PointXYZ p;
	p.x = 1;
	p.y = 2;
	p.z = 3;
	cloud->push_back(p);
	std::cout << p.x << " " << p.y << " " << p.z << std::endl;

	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2(new pcl::PointCloud<pcl::PointXYZ>());
	copyPointCloud(*cloud, *cloud2);// 相同型別複製

	pcl::PointCloud<pcl::PointXYZ>::PointType p_retrieved = (*cloud2)[0];
	//pcl::PointXYZ p_retrieved = cloud2->points.at(0);// 同上
	std::cout << p_retrieved.x << " " << p_retrieved.y << " " << p_retrieved.z << std::endl;

  結果:

3、 型別不同的點雲複製

// 複製點雲
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
	pcl::PointCloud<pcl::PointXYZ>::PointType p;// 相當於 pcl::PointXYZ p;
	p.x = 1;
	p.y = 2;
	p.z = 3;
	cloud->push_back(p);
	std::cout << p.x << " " << p.y << " " << p.z << std::endl;

	pcl::PointCloud<pcl::PointNormal>::Ptr cloud2(new pcl::PointCloud<pcl::PointNormal>());
	copyPointCloud(*cloud, *cloud2);// 不同型別複製,注意cloud2是包含點和法線的,若型別是pcl::Normal會報錯

	//pcl::PointCloud<pcl::PointNormal>::PointType p_retrieved = (*cloud2)[0];
	pcl::PointNormal p_retrieved = cloud2->points.at(0);// 同上
	std::cout << p_retrieved.x << " " << p_retrieved.y << " " << p_retrieved.z << std::endl;
	std::cout << cloud2->points.at(0).x << " " << cloud2->points.at(0).y << " " << cloud2->points.at(0).z << std::endl;
	std::cout << cloud2->points.at(0).normal[0] << std::endl;
	std::cout << cloud2->points.at(0).normal_y << std::endl;
	std::cout << cloud2->points.at(0).normal[2] << std::endl;

  結果:

4、獲取匯入點雲檔案的最大值和最小值點

    pcl::PointXYZ minPt, maxPt;
	pcl::getMinMax3D(*n.cloud, minPt, maxPt);
	std::cout << "Max x: " << maxPt.x << std::endl;
	std::cout << "Max y: " << maxPt.y << std::endl;
	std::cout << "Max z: " << maxPt.z << std::endl;
	std::cout << "Min x: " << minPt.x << std::endl;
	std::cout << "Min y: " << minPt.y << std::endl;
	std::cout << "Min z: " << minPt.z << std::endl;    

  結果:

另一種寫法:

// 遍歷點雲區間
	for (const auto& p : n.cloud->points)
	{
		if (minX > p.x) minX = p.x;
		if (minX > p.y) minY = p.y;
		if (minX > p.z) minZ = p.z;

		if (maxX < p.x) maxX = p.x;
		if (maxY < p.y) maxY = p.y;
		if (maxZ < p.z) maxZ = p.z;
	}
	qDebug() << minX << minY << minZ << maxX << maxY << maxZ;

  結果:

匯入的是同一個檔案,但為什麼結果不一樣呢,因為這個沒有做無效點判斷,而上面那個底層是有判斷的

5、組織有序的點雲

	// Setup the cloud
	using PointType = pcl::PointXYZ;
	using CloudType = pcl::PointCloud<PointType>;
	CloudType::Ptr cloud(new CloudType);

	// Make the cloud a 10x10 grid
	cloud->height = 10;
	cloud->width = 10;
	cloud->is_dense = true;
	cloud->resize(cloud->height * cloud->width);

	// Output the (0,0) point
	std::cout << (*cloud)(0, 0) << std::endl;

	// Set the (0,0) point
	PointType p; p.x = 1; p.y = 2; p.z = 3;
	(*cloud)(0, 0) = p;

	// Confirm that the point was set
	std::cout << (*cloud)(0, 0) << std::endl;

  結果:

相關文章