最近在用PCL的时候,需要对点云绕某一个轴进行旋转。于是上网查了些资料自己弄了个函数,分享给大家。
使用的是Eigen这个库,首先要#include <Eigen/Dense>
参数中vector是轴的方向(一定要先单位化后再传参),point表示这个轴所经过的某一个点,t表示角度(如90度:PI/2)
// Returns the rotation matrix around a vector placed at a point , rotate by angle t
Eigen::Matrix4f rot_mat(const Eigen::Vector3f& point, const Eigen::Vector3f& vector, const float t)
{
float u = vector(0);
float v = vector(1);
float w = vector(2);
float a = point(0);
float b = point(1);
float c = point(2);
Eigen::Matrix4f matrix;
matrix<<u*u + (v*v + w*w)*cos(t), u*v*(1 - cos(t)) - w*sin(t), u*w*(1 - cos(t)) + v*sin(t), (a*(v*v + w*w) - u*(b*v + c*w))*(1 - cos(t)) + (b*w - c*v)*sin(t),
u*v*(1 - cos(t)) + w*sin(t), v*v + (u*u + w*w)*cos(t), v*w*(1 - cos