Geometry(几何学)主要就是指的空间变换。学习这一章,可以减少代码编写量。本小节将会介绍一些处理2D、3D旋转、投影和仿射的变换的一些内容,这些内容将会由几何模块(geometry module)提供。
官方文档地址
Geometry模块可以提供以下功能支持:
- 固定大小的齐次变换;
- 平移、缩放及二维和三维的旋转;
- 四元数;
- 叉积 (MatrixBase::cross, MatrixBase::cross3) ;
- 产生正交向量 (MatrixBase::unitOrthogonal);
- 一些线性组件: 参数化直线和超平面;
- 轴对称包围盒 axis aligned bounding boxes;
- 最小二乘矩阵拟合 least-square transformation fitting;
这里主要对Tranform进行支持,这个类是几何变换的基础,代表N维齐次矩阵。
二、Tranform类 2.1 构造函数Transform模板定义如下:
template
class Eigen::Transform
它代表了N维的齐次矩阵(Homogeneous transformation),单应性(homography)在内部由矩阵表示,可以使用matrix()方法返回。
- _Scalar 标量类型, 也就是系数的类型;
- _Dim 空间维度
- _Mode 变换的类型,它可能是:Affine或者AffineCompact或者Projective、Isometry[2]
- _Options 空间存储顺序,可选
对于机器人的变换算子(齐次矩阵)_Mode应该为Isometry,_Dim=3,对应的完整声明为:Transform T;
,和Matrix3f等一样,为了方便声明,有以下别名:
这样一来,我们就能够直接使用Isometry3d来表示机器人的变换算子了。
默认构造函数解释Transform()默认不进行初始化Transform(const EigenBase< OtherDerived > & other)用满足一定规则的Matrix构造Transform(const QTransform< _Scalar, _Dim, _Mode, _Options > & other)QT中的QTransform构造Transform(const QMatrix & other)QT中的QMatrix构造Transform(const Transform< OtherScalarType, Dim, Mode, Options > & other)拷贝构造(Tranform)类型注:第二个构造函数是EigenBase,可用于构造和赋值MatrixBase,MatrixBase是所有dense matrices, vectors, and expressions的基类。像是一开始学到的Matrix类就是MatrixBase的子类,因此,你可以直接使用Matrix构造Transform。
MatrixBase有一个反解欧拉角的方法:Matrix< Scalar, 3, 1 > eulerAngles (Index a0, Index a1, Index a2) const) Index是一个std::ptrdiff_t long型整数,0代表绕x,1代表绕y,2代表z。
2.2 进行变换Transform之间的变换可以通过运算符*来进行,值得注意的是,运算支持了DiagonalBase、EigenBase和Transform ,这也就是说,我们可以直接与Vector、Matrix进行混合运算。 Tranform类会提供两种不同的几何变换:
- 抽象变换。比如旋转(以轴角或者四元数),平移,缩放。这些变换不是由matrix类来表示的,但是你愿意,你仍可以将它们与matrix类、vector类混合使用。
- 投影或者仿射变换则是matrix类。查看Transform类,确实如此。
请注意,Transform虽非一个Matrix类型,但是仍然可以与Matrix、Vector进行混合使用。与一般的Matrix不同的地方在于,Tranform提供了许多性质用于简化组合和使用。具体的,它能够由其他变换(Transfrom,Translation,RotationBase,DiaonalMatrix)隐式转换成齐次向量后组合而成,这个操作就是运算符*
,运算一般就是纯乘法,eigen不可以充分利用这些变换的性质,对其进行优化以提升效率。
坐标变换是将一个坐标变换到另一个坐标,可以分为平移、旋转、缩放等。
变换类型典型的初始化方法二维定角度旋转Rotation2D rot2(angle_in_radian);
三维定轴、定角旋转AngleAxis aa(angle_in_radian, Vector3f(ax,ay,az));
注:轴向量必须标准化,怕出错用,Vector3d::UnitX三维四元数旋转Quaternion q;
q = AngleAxis(angle_in_radian, axis);
N维缩放Scaling(sx, sy)
Scaling(sx, sy, sz)
Scaling(s) Scaling(vecN)
N维平移Translation(tx, ty)
Translation(tx, ty, tz)
Translation(s)
Translation(vecN)
N维维仿射变换Transform t = concatenation_of_any_transformations;
Transform t =Translation3f(p) * AngleAxisf(a,axis) * Scaling(s)
;N维线性变换(纯旋转、缩放等)Matrix t = concatenation_of_rotations_and_scalings;
Matrix t = Rotation2Df(a) * Scaling(s);
Matrix t = AngleAxisf(a,axis) * Scaling(s);
以AngleAxis为例,构造函数有:
- 空默认构造
- 轴角两个参数
- QuaternionBase
- MatrixBase
- AngleAxis
赋值构造函数有:
- MatrixBase
- QuaternionBase
这就是不同四元数、矩阵之间构造时相互转换的基础。
Matrix3d m=Matrix3d::Identity(3,3);
Quaterniond q(m); //Rotation Matrix to Quaternion
AngleAxisd d; //Default construction for AngleAxisd
d.fromRotationMatrix(m); //Rotation Matrix to AngleAxis
auto dq=d*q; //Quaternion=AngleAxis*Quaternion
auto qd=q*d; //Quaternion=Quaternion*AngleAxis
d=q; //AngleAxis=Quaternion
q=d; //Quaternion=AngleAxis
std::cout
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?