当一个物体边与坐标轴平行时,生成的ABB最小外接立方体有偏差,故这里采用AABB算法。
示例代码:
#include <pcl/features/moment_of_inertia_estimation.h>
#include <vector>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <boost/thread/thread.hpp>
int main()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr down_cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile("..\\testdata\\point0823-1.pcd", *down_cloud) == -1)
return (-1);
vector<double> down = calculate_AABB(down_cloud);
}
vector<double> calculate_AABB(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
//实例化一个Momentof...
pcl::MomentOfInertiaEstimation <pcl::PointXYZ> feature_extractor;
feature_extractor.setInputCloud(cloud);
feature_extractor.compute();
//声明一些必要的变量
std::vector <float> moment_of_inertia;
std::vector <float> eccentricity;
pcl::PointXYZ min_point_AABB;
pcl::PointXYZ max_point_AABB;
//计算描述符和其他的特征
feature_extractor.getMomentOfInertia(moment_of_inertia);
feature_extractor.getEccentricity(eccentricity);
feature_extractor.getAABB(min_point_AABB, max_point_AABB);
//显示
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0, 0, 0);
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
viewer->addPointCloud<pcl::PointXYZ>(cloud, "sample cloud");
//addCube
viewer->addCube(min_point_AABB.x, max_point_AABB.x, min_point_AABB.y, max_point_AABB.y, min_point_AABB.z, max_point_AABB.z, 1.0, 1.0, 0.0, "AABB");//AABB盒子
viewer->setRepresentationToWireframeForAllActors();//线框模式
viewer->resetCameraViewpoint();
cout << min_point_AABB.x << endl;
cout << min_point_AABB.y << endl;
cout << max_point_AABB.x << endl;
cout << max_point_AABB.y << endl;
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
return { min_point_AABB.x, max_point_AABB.x, min_point_AABB.y, max_point_AABB.y };
}
测试效果: