PCL 创建点云最小外接箱体

1 篇文章 0 订阅
#include<pcl/io/ply_io.h>
#include<pcl/point_types.h>
#include<pcl/common/common.h>
#include<pcl/visualization/pcl_visualizer.h>
#include<pcl/features/moment_of_inertia_estimation.h>

typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
int main()
{
	PointCloudT::Ptr cloud(new PointCloudT());
	pcl::io::loadPLYFile("2.ply", *cloud);
	pcl::MomentOfInertiaEstimation<PointT>feature_extractor;
	feature_extractor.setInputCloud(cloud);
	feature_extractor.compute();
	
	std::vector<float>moment_of_inertia;
	std::vector<float>eccentricity;
	PointT minAABB;
	PointT maxAABB;
	PointT minOBB;
	PointT maxOBB;
	PointT positionOBB;
	Eigen::Matrix3f rotational_matrix_OBB;
	float majorValue, midValue, minValue;
	Eigen::Vector3f majorVec, midVec, minVec;
	Eigen::Vector3f massCenter;

	feature_extractor.getMomentOfInertia(moment_of_inertia);
	feature_extractor.getEccentricity(eccentricity);
	feature_extractor.getAABB(minAABB, maxAABB);
	feature_extractor.getOBB(minOBB, maxOBB, positionOBB, rotational_matrix_OBB);
	feature_extractor.getEigenValues(majorValue, midValue, minValue);
	feature_extractor.getEigenVectors(majorVec, midVec, minVec);
	feature_extractor.getMassCenter(massCenter);

	//AABB 外接箱体
	pcl::visualization::PCLVisualizer::Ptr view(new pcl::visualization::PCLVisualizer("DispCloud"));
	pcl::visualization::PointCloudColorHandlerGenericField<PointT>field(cloud,"z");
	view->addPointCloud(cloud,field);
	view->addCube(minAABB.x, maxAABB.x, minAABB.y, maxAABB.y, minAABB.z, maxAABB.z, 1.0, 0.0, 0.0, "AABB", 0);
	view->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_REPRESENTATION,
		pcl::visualization::PCL_VISUALIZER_REPRESENTATION_WIREFRAME, "AABB");
	//OBB 外接箱体
	Eigen::Vector3f position(positionOBB.x, positionOBB.y, positionOBB.z);
	Eigen::Quaternionf quat(rotational_matrix_OBB);
	view->addCube(position, quat,static_cast<double>(maxOBB.x - minOBB.x),
		static_cast<double>(maxOBB.y - minOBB.y),static_cast<double>(maxOBB.z - minOBB.z), "OBB");
	view->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_REPRESENTATION,
		pcl::visualization::PCL_VISUALIZER_REPRESENTATION_WIREFRAME, "OBB");
	std::cout << rotational_matrix_OBB<< std::endl;
	std::cout << positionOBB << std::endl;

	PointT center(massCenter(0), massCenter(1), massCenter(2));
	PointT axis_X(majorVec(0) + massCenter(0), majorVec(1) + massCenter(1), majorVec(2) + massCenter(2));
	PointT axis_Y(midVec(0) + massCenter(0), midVec(1) + massCenter(1), midVec(2) + massCenter(2));
	PointT axis_Z(minVec(0) + massCenter(0), minVec(1) + massCenter(1), minVec(2) + massCenter(2));
	view->addLine(center, axis_X, 1.0f, 0.0f, 0.0f, "major eigen vector");
	view->addLine(center, axis_Y, 0.0f, 1.0f, 0.0f, "middle eigen vector");
	view->addLine(center, axis_Z, 0.0f, 0.0f, 1.0f, "minor eigen vector");

	view->spin();
	return 0;
}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值