1717
1818#include < Eigen/Dense>
1919
20- #include < pcl/common/pca.h>
21-
2220namespace
2321{
2422
@@ -52,29 +50,25 @@ Eigen::Matrix3f createMatrix(const Eigen::Vector3f& norm)
5250Ogre::Quaternion estimateNormal (const std::vector<Ogre::Vector3>& points,
5351 const Ogre::Vector3& camera_norm)
5452{
55- pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ> ());
53+ Eigen::MatrixXf data;
54+ data.resize (points.size (), 3 );
5655
5756 for (std::size_t i = 0 ; i < points.size (); ++i)
5857 {
59- pcl::PointXYZ pt;
60- pt.x = points[i].x ;
61- pt.y = points[i].y ;
62- pt.z = points[i].z ;
63-
64- cloud->push_back (pt);
58+ data.row (i) = Eigen::Map<const Eigen::Vector3f>(points[i].ptr ());
6559 }
6660
61+ Eigen::MatrixXf centered = data.rowwise () - data.colwise ().mean ();
62+
6763 // Use principal component analysis to the get eigenvectors
68- pcl::PCA<pcl::PointXYZ> pca;
69- pca.setInputCloud (cloud);
70- Eigen::Matrix3f& evecs = pca.getEigenVectors ();
64+ Eigen::MatrixXf cov = centered.transpose () * centered;
65+ Eigen::SelfAdjointEigenSolver<Eigen::MatrixXf> eig (cov);
7166
7267 // Get the eigenvector associated with the smallest eigenvalue
7368 // (should be Z-axis, assuming points are relatively planar)
74- Eigen::Vector3f norm = evecs. col (2 );
69+ Eigen::Vector3f norm = eig. eigenvectors (). col (0 );
7570 norm.normalize ();
7671
77- // The
7872 Eigen::Vector3f camera_normal;
7973 camera_normal << camera_norm.x , camera_norm.y , camera_norm.z ;
8074 camera_normal.normalize ();
@@ -84,7 +78,7 @@ Ogre::Quaternion estimateNormal(const std::vector<Ogre::Vector3>& points,
8478 norm *= -1 ;
8579 }
8680
87- // Create a random orientation matrix with the normal being in the direction of the smalles eigenvector
81+ // Create an arbitrary orientation matrix with the normal being in the direction of the smallest eigenvector
8882 Eigen::Matrix3f mat = createMatrix (norm);
8983
9084 Eigen::Quaternionf q (mat); // Eigen::AngleAxisf(0.0, evecs.col(2)));
0 commit comments