template <typename PointT> inline unsigned int pcl::computeCovarianceMatrix (const pcl::PointCloud<PointT> &cloud, Eigen::Matrix3f &covariance_matrix) { // create the buffer on the stack which is much faster than using cloud.points[indices[i]] and centroid as a buffer Eigen::Matrix<float, 1, 6, Eigen::RowMajor> accu = Eigen::Matrix<float, 1, 6, Eigen::RowMajor>::Zero (); unsigned int point_count; if (cloud.is_dense) { point_count = static_cast<unsigned int> (cloud.size ()); // For each point in the cloud for (size_t i = 0; i < point_count; ++i) { accu [0] += cloud[i].x * cloud[i].x; accu [1] += cloud[i].x * cloud[i].y; accu [2] += cloud[i].x * cloud[i].z; accu [3] += cloud[i].y * cloud[i].y; accu [4] += cloud[i].y * cloud[i].z; accu [5] += cloud[i].z * cloud[i].z; } } else { point_count = 0; for (size_t i = 0; i < cloud.points.size (); ++i) { if (!isFinite (cloud[i])) continue; accu [0] += cloud[i].x * cloud[i].x; accu [1] += cloud[i].x * cloud[i].y; accu [2] += cloud[i].x * cloud[i].z; accu [3] += cloud[i].y * cloud[i].y; accu [4] += cloud[i].y * cloud[i].z; accu [5] += cloud[i].z * cloud[i].z; ++point_count; } } if (point_count != 0) { accu /= static_cast<float> (point_count); covariance_matrix.coeffRef (0) = accu [0]; covariance_matrix.coeffRef (1) = covariance_matrix.coeffRef (3) = accu [1]; covariance_matrix.coeffRef (2) = covariance_matrix.coeffRef (6) = accu [2]; covariance_matrix.coeffRef (4) = accu [3]; covariance_matrix.coeffRef (5) = covariance_matrix.coeffRef (7) = accu [4]; covariance_matrix.coeffRef (8) = accu [5]; } return (point_count); }
template <typename PointT> inline unsigned int pcl::computeCovarianceMatrix (const pcl::PointCloud<PointT> &cloud, const std::vector<int> &indices, Eigen::Matrix3f &covariance_matrix) { Eigen::Matrix<float, 1, 6, Eigen::RowMajor> accu = Eigen::Matrix<float, 1, 6, Eigen::RowMajor>::Zero (); unsigned int point_count; if (cloud.is_dense) { point_count = static_cast<unsigned int> (indices.size ()); for (std::vector<int>::const_iterator iIt = indices.begin (); iIt != indices.end (); ++iIt) { //const PointT& point = cloud[*iIt]; accu [0] += cloud[*iIt].x * cloud[*iIt].x; accu [1] += cloud[*iIt].x * cloud[*iIt].y; accu [2] += cloud[*iIt].x * cloud[*iIt].z; accu [3] += cloud[*iIt].y * cloud[*iIt].y; accu [4] += cloud[*iIt].y * cloud[*iIt].z; accu [5] += cloud[*iIt].z * cloud[*iIt].z; } } else { point_count = 0; for (std::vector<int>::const_iterator iIt = indices.begin (); iIt != indices.end (); ++iIt) { if (!isFinite (cloud[*iIt])) continue; ++point_count; accu [0] += cloud[*iIt].x * cloud[*iIt].x; accu [1] += cloud[*iIt].x * cloud[*iIt].y; accu [2] += cloud[*iIt].x * cloud[*iIt].z; accu [3] += cloud[*iIt].y * cloud[*iIt].y; accu [4] += cloud[*iIt].y * cloud[*iIt].z; accu [5] += cloud[*iIt].z * cloud[*iIt].z; } } if (point_count != 0) { accu /= static_cast<float> (point_count); covariance_matrix.coeffRef (0) = accu [0]; covariance_matrix.coeffRef (1) = covariance_matrix.coeffRef (3) = accu [1]; covariance_matrix.coeffRef (2) = covariance_matrix.coeffRef (6) = accu [2]; covariance_matrix.coeffRef (4) = accu [3]; covariance_matrix.coeffRef (5) = covariance_matrix.coeffRef (7) = accu [4]; covariance_matrix.coeffRef (8) = accu [5]; } return (point_count); }
void pcl::getCameraMatrixFromProjectionMatrix ( const Eigen::Matrix<float, 3, 4, Eigen::RowMajor>& projection_matrix, Eigen::Matrix3f& camera_matrix) { Eigen::Matrix3f KR = projection_matrix.topLeftCorner <3, 3> (); Eigen::Matrix3f KR_KRT = KR * KR.transpose (); Eigen::Matrix3f cam = KR_KRT / KR_KRT.coeff (8); memset (&(camera_matrix.coeffRef (0)), 0, sizeof (Eigen::Matrix3f::Scalar) * 9); camera_matrix.coeffRef (8) = 1.0; if (camera_matrix.Flags & Eigen::RowMajorBit) { camera_matrix.coeffRef (2) = cam.coeff (2); camera_matrix.coeffRef (5) = cam.coeff (5); camera_matrix.coeffRef (4) = static_cast<float> (std::sqrt (cam.coeff (4) - cam.coeff (5) * cam.coeff (5))); camera_matrix.coeffRef (1) = (cam.coeff (1) - cam.coeff (2) * cam.coeff (5)) / camera_matrix.coeff (4); camera_matrix.coeffRef (0) = static_cast<float> (std::sqrt (cam.coeff (0) - camera_matrix.coeff (1) * camera_matrix.coeff (1) - cam.coeff (2) * cam.coeff (2))); } else { camera_matrix.coeffRef (6) = cam.coeff (2); camera_matrix.coeffRef (7) = cam.coeff (5); camera_matrix.coeffRef (4) = static_cast<float> (std::sqrt (cam.coeff (4) - cam.coeff (5) * cam.coeff (5))); camera_matrix.coeffRef (3) = (cam.coeff (1) - cam.coeff (2) * cam.coeff (5)) / camera_matrix.coeff (4); camera_matrix.coeffRef (0) = static_cast<float> (std::sqrt (cam.coeff (0) - camera_matrix.coeff (3) * camera_matrix.coeff (3) - cam.coeff (2) * cam.coeff (2))); } }
template <typename PointT> inline unsigned int pcl::computeMeanAndCovarianceMatrix (const pcl::PointCloud<PointT> &cloud, const std::vector<int> &indices, Eigen::Matrix3f &covariance_matrix, Eigen::Vector4f ¢roid) { // create the buffer on the stack which is much faster than using cloud.points[indices[i]] and centroid as a buffer Eigen::Matrix<float, 1, 9, Eigen::RowMajor> accu = Eigen::Matrix<float, 1, 9, Eigen::RowMajor>::Zero (); size_t point_count; if (cloud.is_dense) { point_count = indices.size (); for (std::vector<int>::const_iterator iIt = indices.begin (); iIt != indices.end (); ++iIt) { //const PointT& point = cloud[*iIt]; accu [0] += cloud[*iIt].x * cloud[*iIt].x; accu [1] += cloud[*iIt].x * cloud[*iIt].y; accu [2] += cloud[*iIt].x * cloud[*iIt].z; accu [3] += cloud[*iIt].y * cloud[*iIt].y; accu [4] += cloud[*iIt].y * cloud[*iIt].z; accu [5] += cloud[*iIt].z * cloud[*iIt].z; accu [6] += cloud[*iIt].x; accu [7] += cloud[*iIt].y; accu [8] += cloud[*iIt].z; } } else { point_count = 0; for (std::vector<int>::const_iterator iIt = indices.begin (); iIt != indices.end (); ++iIt) { if (!isFinite (cloud[*iIt])) continue; ++point_count; accu [0] += cloud[*iIt].x * cloud[*iIt].x; accu [1] += cloud[*iIt].x * cloud[*iIt].y; accu [2] += cloud[*iIt].x * cloud[*iIt].z; accu [3] += cloud[*iIt].y * cloud[*iIt].y; // 4 accu [4] += cloud[*iIt].y * cloud[*iIt].z; // 5 accu [5] += cloud[*iIt].z * cloud[*iIt].z; // 8 accu [6] += cloud[*iIt].x; accu [7] += cloud[*iIt].y; accu [8] += cloud[*iIt].z; } } accu /= static_cast<float> (point_count); //Eigen::Vector3f vec = accu.tail<3> (); //centroid.head<3> () = vec;//= accu.tail<3> (); //centroid.head<3> () = accu.tail<3> (); -- does not compile with Clang 3.0 centroid[0] = accu[6]; centroid[1] = accu[7]; centroid[2] = accu[8]; centroid[3] = 0; covariance_matrix.coeffRef (0) = accu [0] - accu [6] * accu [6]; covariance_matrix.coeffRef (1) = accu [1] - accu [6] * accu [7]; covariance_matrix.coeffRef (2) = accu [2] - accu [6] * accu [8]; covariance_matrix.coeffRef (4) = accu [3] - accu [7] * accu [7]; covariance_matrix.coeffRef (5) = accu [4] - accu [7] * accu [8]; covariance_matrix.coeffRef (8) = accu [5] - accu [8] * accu [8]; covariance_matrix.coeffRef (3) = covariance_matrix.coeff (1); covariance_matrix.coeffRef (6) = covariance_matrix.coeff (2); covariance_matrix.coeffRef (7) = covariance_matrix.coeff (5); return (static_cast<unsigned int> (point_count)); }
template <typename PointT> inline unsigned int pcl::computeMeanAndCovarianceMatrix (const pcl::PointCloud<PointT> &cloud, Eigen::Matrix3f &covariance_matrix, Eigen::Vector4f ¢roid) { // create the buffer on the stack which is much faster than using cloud.points[indices[i]] and centroid as a buffer Eigen::Matrix<float, 1, 9, Eigen::RowMajor> accu = Eigen::Matrix<float, 1, 9, Eigen::RowMajor>::Zero (); unsigned point_count; if (cloud.is_dense) { point_count = cloud.size (); // For each point in the cloud for (size_t i = 0; i < point_count; ++i) { accu [0] += cloud[i].x * cloud[i].x; accu [1] += cloud[i].x * cloud[i].y; accu [2] += cloud[i].x * cloud[i].z; accu [3] += cloud[i].y * cloud[i].y; // 4 accu [4] += cloud[i].y * cloud[i].z; // 5 accu [5] += cloud[i].z * cloud[i].z; // 8 accu [6] += cloud[i].x; accu [7] += cloud[i].y; accu [8] += cloud[i].z; } } else { point_count = 0; for (size_t i = 0; i < cloud.points.size (); ++i) { if (!isFinite (cloud[i])) continue; accu [0] += cloud[i].x * cloud[i].x; accu [1] += cloud[i].x * cloud[i].y; accu [2] += cloud[i].x * cloud[i].z; accu [3] += cloud[i].y * cloud[i].y; accu [4] += cloud[i].y * cloud[i].z; accu [5] += cloud[i].z * cloud[i].z; accu [6] += cloud[i].x; accu [7] += cloud[i].y; accu [8] += cloud[i].z; ++point_count; } } accu /= (float) point_count; if (point_count != 0) { centroid.head<3> () = accu.tail<3> (); centroid[3] = 0; covariance_matrix.coeffRef (0) = accu [0] - accu [6] * accu [6]; covariance_matrix.coeffRef (1) = accu [1] - accu [6] * accu [7]; covariance_matrix.coeffRef (2) = accu [2] - accu [6] * accu [8]; covariance_matrix.coeffRef (4) = accu [3] - accu [7] * accu [7]; covariance_matrix.coeffRef (5) = accu [4] - accu [7] * accu [8]; covariance_matrix.coeffRef (8) = accu [5] - accu [8] * accu [8]; covariance_matrix.coeffRef (3) = covariance_matrix.coeff (1); covariance_matrix.coeffRef (6) = covariance_matrix.coeff (2); covariance_matrix.coeffRef (7) = covariance_matrix.coeff (5); } return (point_count); }