#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;
}