libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::Quaternion< Scalar > Class Template Reference

四元数表示与运算,继承自 Eigen::Quaternion / Quaternion representation and operations, inheriting from Eigen::Quaternion More...

#include <transform.hpp>

Inheritance diagram for LibXR::Quaternion< Scalar >:
[legend]
Collaboration diagram for LibXR::Quaternion< Scalar >:
[legend]

Public Member Functions

 Quaternion ()
 默认构造函数,初始化为单位四元数 / Default constructor initializing to an identity quaternion
 
 Quaternion (Scalar w, Scalar x, Scalar y, Scalar z)
 通过四个分量初始化四元数 / Construct a quaternion using four components
 
 Quaternion (const Eigen::Quaternion< Scalar > &q)
 复制构造函数 / Copy constructor
 
 Quaternion (const RotationMatrix< Scalar > &R)
 通过旋转矩阵构造四元数 / Construct a quaternion from a rotation matrix
 
 Quaternion (const Eigen::Matrix< Scalar, 3, 3 > R)
 通过 3x3 旋转矩阵构造四元数 / Construct a quaternion from a 3x3 rotation matrix
 
template<typename T , std::enable_if_t< std::is_same< T, Scalar >::value||std::is_same< T, float >::value||std::is_same< T, double >::value, int > = 0>
 Quaternion (const T(&data)[4])
 通过四维数组初始化四元数 / Construct a quaternion from a 4-element array
 
Scalar operator() (int i) const
 获取四元数的分量 / Retrieve a specific component of the quaternion
 
Quaternion & operator= (const Eigen::Quaternion< Scalar > &q)
 赋值运算符 / Assignment operator
 
Quaternion operator- () const
 取共轭四元数 / Get the conjugate quaternion
 
Quaternion operator+ (const Quaternion &q) const
 四元数加法 / Quaternion addition
 
Quaternion operator+ (const Eigen::Quaternion< Scalar > &q) const
 
Quaternion operator- (const Quaternion &q) const
 
Quaternion operator- (const Eigen::Quaternion< Scalar > &q) const
 
Quaternion operator* (const Quaternion &q) const
 四元数乘法 / Quaternion multiplication
 
Quaternion operator* (const Eigen::Quaternion< Scalar > &q) const
 
Quaternion operator/ (const Quaternion &q) const
 四元数除法(即左乘其共轭) / Quaternion division (multiplication by its conjugate)
 
Quaternion operator/ (const Eigen::Quaternion< Scalar > &q) const
 
template<typename Q , std::enable_if_t< std::is_same< Q, Quaternion >::value||std::is_same< Q, Eigen::Quaternion< Scalar > >::value, int > = 0>
const Quaternion & operator+= (const Q &q)
 
template<typename Q , std::enable_if_t< std::is_same< Q, Quaternion >::value||std::is_same< Q, Eigen::Quaternion< Scalar > >::value, int > = 0>
const Quaternion & operator-= (const Q &q)
 
Position< Scalar > operator* (const Position< Scalar > &p) const
 旋转三维向量 / Rotate a 3D vector
 
Eigen::Matrix< Scalar, 3, 1 > ToEulerAngle () const
 获取四元数的欧拉角表示(默认使用 ZYX 旋转顺序) Get the Euler angles representation of the quaternion (default ZYX order)
 
Eigen::Matrix< Scalar, 3, 1 > ToEulerAngleYZX () const
 
Eigen::Matrix< Scalar, 3, 1 > ToEulerAngleZYX () const
 
Eigen::Matrix< Scalar, 3, 1 > ToEulerAngleYXZ () const
 
Eigen::Matrix< Scalar, 3, 1 > ToEulerAngleZXY () const
 
Eigen::Matrix< Scalar, 3, 1 > ToEulerAngleXZY () const
 
Eigen::Matrix< Scalar, 3, 1 > ToEulerAngleXYZ () const
 
Eigen::Matrix< Scalar, 3, 3 > ToRotationMatrix () const
 将四元数转换为 3x3 旋转矩阵 / Convert the quaternion to a 3x3 rotation matrix
 

Detailed Description

template<typename Scalar>
class LibXR::Quaternion< Scalar >

四元数表示与运算,继承自 Eigen::Quaternion / Quaternion representation and operations, inheriting from Eigen::Quaternion

该类提供了对四元数的基本运算,如加法、减法、乘法、除法,以及与旋转矩阵和欧拉角的转换等。 This class provides fundamental quaternion operations, such as addition, subtraction, multiplication, and division, along with conversions to rotation matrices and Euler angles.

Template Parameters
Scalar数值类型(默认使用 DefaultScalar) / Numeric type (default: DefaultScalar)

Definition at line 888 of file transform.hpp.

Constructor & Destructor Documentation

◆ Quaternion() [1/6]

template<typename Scalar >
LibXR::Quaternion< Scalar >::Quaternion ( )
inline

默认构造函数,初始化为单位四元数 / Default constructor initializing to an identity quaternion

Definition at line 895 of file transform.hpp.

895: Eigen::Quaternion<Scalar>(1, 0, 0, 0) {}

◆ Quaternion() [2/6]

template<typename Scalar >
LibXR::Quaternion< Scalar >::Quaternion ( Scalar w,
Scalar x,
Scalar y,
Scalar z )
inline

通过四个分量初始化四元数 / Construct a quaternion using four components

Parameters
w实部 / Real part
xi 分量 / i component
yj 分量 / j component
zk 分量 / k component

Definition at line 904 of file transform.hpp.

905 : Eigen::Quaternion<Scalar>(w, x, y, z)
906 {
907 }

◆ Quaternion() [3/6]

template<typename Scalar >
LibXR::Quaternion< Scalar >::Quaternion ( const Eigen::Quaternion< Scalar > & q)
inline

复制构造函数 / Copy constructor

Parameters
q另一个 Eigen::Quaternion 四元数 / Another Eigen::Quaternion

Definition at line 913 of file transform.hpp.

913: Eigen::Quaternion<Scalar>(q) {}

◆ Quaternion() [4/6]

template<typename Scalar >
LibXR::Quaternion< Scalar >::Quaternion ( const RotationMatrix< Scalar > & R)
inline

通过旋转矩阵构造四元数 / Construct a quaternion from a rotation matrix

Parameters
R旋转矩阵 / Rotation matrix

Definition at line 919 of file transform.hpp.

920 : Eigen::Quaternion<Scalar>(
921 Eigen::Quaternion<Scalar>(static_cast<Eigen::Matrix<Scalar, 3, 3>>(R)))
922 {
923 }

◆ Quaternion() [5/6]

template<typename Scalar >
LibXR::Quaternion< Scalar >::Quaternion ( const Eigen::Matrix< Scalar, 3, 3 > R)
inline

通过 3x3 旋转矩阵构造四元数 / Construct a quaternion from a 3x3 rotation matrix

Parameters
R旋转矩阵 / Rotation matrix

Definition at line 930 of file transform.hpp.

931 : Eigen::Quaternion<Scalar>(
932 Eigen::Quaternion<Scalar>(static_cast<Eigen::Matrix<Scalar, 3, 3>>(R)))
933 {
934 }

◆ Quaternion() [6/6]

template<typename Scalar >
template<typename T , std::enable_if_t< std::is_same< T, Scalar >::value||std::is_same< T, float >::value||std::is_same< T, double >::value, int > = 0>
LibXR::Quaternion< Scalar >::Quaternion ( const T(&) data[4])
inline

通过四维数组初始化四元数 / Construct a quaternion from a 4-element array

Parameters
data4 维数组,表示 (w, x, y, z) / A 4-element array representing (w, x, y, z)

Definition at line 944 of file transform.hpp.

945 : Eigen::Quaternion<Scalar>(
946 static_cast<Scalar>(data[0]), static_cast<Scalar>(data[1]),
947 static_cast<Scalar>(data[2]), static_cast<Scalar>(data[3]))
948 {
949 }

Member Function Documentation

◆ operator()()

template<typename Scalar >
Scalar LibXR::Quaternion< Scalar >::operator() ( int i) const
inline

获取四元数的分量 / Retrieve a specific component of the quaternion

Parameters
i分量索引 (0: x, 1: y, 2: z, 3: w) / Component index (0: x, 1: y, 2: z, 3: w)
Returns
该分量的值 / Value of the component

Definition at line 956 of file transform.hpp.

957 {
958 switch (i)
959 {
960 case 3:
961 return this->w();
962 case 0:
963 return this->x();
964 case 1:
965 return this->y();
966 case 2:
967 return this->z();
968 default:
969 return 0;
970 }
971 }

◆ operator*() [1/3]

template<typename Scalar >
Quaternion LibXR::Quaternion< Scalar >::operator* ( const Eigen::Quaternion< Scalar > & q) const
inline

Definition at line 1029 of file transform.hpp.

1030 {
1031 return Eigen::Quaternion<Scalar>(*this) * q;
1032 }

◆ operator*() [2/3]

template<typename Scalar >
Position< Scalar > LibXR::Quaternion< Scalar >::operator* ( const Position< Scalar > & p) const
inline

旋转三维向量 / Rotate a 3D vector

Parameters
p需要旋转的 Position 向量 / Position vector to be rotated
Returns
旋转后的 Position 向量 / Rotated Position vector

Definition at line 1072 of file transform.hpp.

1073 {
1074 return Eigen::Quaternion<Scalar>(*this) * Eigen::Matrix<Scalar, 3, 1>(p);
1075 }

◆ operator*() [3/3]

template<typename Scalar >
Quaternion LibXR::Quaternion< Scalar >::operator* ( const Quaternion< Scalar > & q) const
inline

四元数乘法 / Quaternion multiplication

Parameters
q另一个四元数 / Another quaternion
Returns
计算后的四元数 / Computed quaternion

Definition at line 1024 of file transform.hpp.

1025 {
1026 return Eigen::Quaternion<Scalar>(*this) * Eigen::Quaternion<Scalar>(q);
1027 }

◆ operator+() [1/2]

template<typename Scalar >
Quaternion LibXR::Quaternion< Scalar >::operator+ ( const Eigen::Quaternion< Scalar > & q) const
inline

Definition at line 1001 of file transform.hpp.

1002 {
1003 return Quaternion(this->w() + q.w(), this->x() + q.x(), this->y() + q.y(),
1004 this->z() + q.z());
1005 }
Quaternion()
默认构造函数,初始化为单位四元数 / Default constructor initializing to an identity quaternion

◆ operator+() [2/2]

template<typename Scalar >
Quaternion LibXR::Quaternion< Scalar >::operator+ ( const Quaternion< Scalar > & q) const
inline

四元数加法 / Quaternion addition

Parameters
q另一个四元数 / Another quaternion
Returns
计算后的四元数 / Computed quaternion

Definition at line 995 of file transform.hpp.

996 {
997 return Quaternion(this->w() + q.w(), this->x() + q.x(), this->y() + q.y(),
998 this->z() + q.z());
999 }

◆ operator+=()

template<typename Scalar >
template<typename Q , std::enable_if_t< std::is_same< Q, Quaternion >::value||std::is_same< Q, Eigen::Quaternion< Scalar > >::value, int > = 0>
const Quaternion & LibXR::Quaternion< Scalar >::operator+= ( const Q & q)
inline

Definition at line 1051 of file transform.hpp.

1052 {
1053 *this = *this + q;
1054 return *this;
1055 }

◆ operator-() [1/3]

template<typename Scalar >
Quaternion LibXR::Quaternion< Scalar >::operator- ( ) const
inline

取共轭四元数 / Get the conjugate quaternion

Returns
共轭四元数 / Conjugated quaternion

Definition at line 988 of file transform.hpp.

988{ return Quaternion((*this).conjugate()); }

◆ operator-() [2/3]

template<typename Scalar >
Quaternion LibXR::Quaternion< Scalar >::operator- ( const Eigen::Quaternion< Scalar > & q) const
inline

Definition at line 1013 of file transform.hpp.

1014 {
1015 return Quaternion(this->w() - q.w(), this->x() - q.x(), this->y() - q.y(),
1016 this->z() - q.z());
1017 }

◆ operator-() [3/3]

template<typename Scalar >
Quaternion LibXR::Quaternion< Scalar >::operator- ( const Quaternion< Scalar > & q) const
inline

Definition at line 1007 of file transform.hpp.

1008 {
1009 return Quaternion(this->w() - q.w(), this->x() - q.x(), this->y() - q.y(),
1010 this->z() - q.z());
1011 }

◆ operator-=()

template<typename Scalar >
template<typename Q , std::enable_if_t< std::is_same< Q, Quaternion >::value||std::is_same< Q, Eigen::Quaternion< Scalar > >::value, int > = 0>
const Quaternion & LibXR::Quaternion< Scalar >::operator-= ( const Q & q)
inline

Definition at line 1061 of file transform.hpp.

1062 {
1063 *this = *this - q;
1064 return *this;
1065 }

◆ operator/() [1/2]

template<typename Scalar >
Quaternion LibXR::Quaternion< Scalar >::operator/ ( const Eigen::Quaternion< Scalar > & q) const
inline

Definition at line 1042 of file transform.hpp.

1043 {
1044 return Eigen::Quaternion<Scalar>(*this) * q.conjugate();
1045 }

◆ operator/() [2/2]

template<typename Scalar >
Quaternion LibXR::Quaternion< Scalar >::operator/ ( const Quaternion< Scalar > & q) const
inline

四元数除法(即左乘其共轭) / Quaternion division (multiplication by its conjugate)

Parameters
q另一个四元数 / Another quaternion
Returns
计算后的四元数 / Computed quaternion

Definition at line 1040 of file transform.hpp.

1040{ return (*this) * (-q); }

◆ operator=()

template<typename Scalar >
Quaternion & LibXR::Quaternion< Scalar >::operator= ( const Eigen::Quaternion< Scalar > & q)
inline

赋值运算符 / Assignment operator

Parameters
q另一个四元数 / Another quaternion
Returns
赋值后的 Quaternion / Assigned Quaternion

Definition at line 978 of file transform.hpp.

979 {
980 *this = Quaternion(q);
981 return *this;
982 }

◆ ToEulerAngle()

template<typename Scalar >
Eigen::Matrix< Scalar, 3, 1 > LibXR::Quaternion< Scalar >::ToEulerAngle ( ) const
inline

获取四元数的欧拉角表示(默认使用 ZYX 旋转顺序) Get the Euler angles representation of the quaternion (default ZYX order)

该方法将四元数转换为欧拉角,表示旋转顺序为 ZYX(即先绕 Z 轴旋转,再绕 Y 轴,最后绕 X 轴)。 This method converts the quaternion into Euler angles using the ZYX rotation order (first rotate around the Z-axis, then Y-axis, and finally the X-axis).

Returns
计算得到的欧拉角向量 (roll, pitch, yaw) / Computed Euler angles vector (roll, pitch, yaw)

Definition at line 1089 of file transform.hpp.

1089{ return ToEulerAngleZYX(); }

◆ ToEulerAngleXYZ()

template<typename Scalar >
Eigen::Matrix< Scalar, 3, 1 > LibXR::Quaternion< Scalar >::ToEulerAngleXYZ ( ) const
inline

Definition at line 1141 of file transform.hpp.

1142 {
1143 Scalar yaw = std::atan2(-2 * (this->x() * this->y() - this->w() * this->z()),
1144 1 - 2 * (this->z() * this->z() + this->y() * this->y()));
1145 Scalar pitch = std::asin(2 * (this->w() * this->y() + this->x() * this->z()));
1146 Scalar roll = std::atan2(-2 * (this->y() * this->z() - this->w() * this->x()),
1147 1 - 2 * (this->y() * this->y() + this->x() * this->x()));
1148 return Eigen::Matrix<Scalar, 3, 1>(roll, pitch, yaw);
1149 }

◆ ToEulerAngleXZY()

template<typename Scalar >
Eigen::Matrix< Scalar, 3, 1 > LibXR::Quaternion< Scalar >::ToEulerAngleXZY ( ) const
inline

Definition at line 1131 of file transform.hpp.

1132 {
1133 Scalar pitch = std::atan2(2 * (this->w() * this->y() + this->x() * this->z()),
1134 1 - 2 * (this->z() * this->z() + this->y() * this->y()));
1135 Scalar yaw = std::asin(2 * (this->w() * this->z() - this->x() * this->y()));
1136 Scalar roll = std::atan2(2 * (this->w() * this->x() + this->y() * this->z()),
1137 1 - 2 * (this->z() * this->z() + this->x() * this->x()));
1138 return Eigen::Matrix<Scalar, 3, 1>(roll, pitch, yaw);
1139 }

◆ ToEulerAngleYXZ()

template<typename Scalar >
Eigen::Matrix< Scalar, 3, 1 > LibXR::Quaternion< Scalar >::ToEulerAngleYXZ ( ) const
inline

Definition at line 1111 of file transform.hpp.

1112 {
1113 Scalar roll = std::asin(2 * (this->w() * this->x() - this->y() * this->z()));
1114 Scalar yaw = std::atan2(2 * (this->w() * this->z() + this->x() * this->y()),
1115 1 - 2 * (this->z() * this->z() + this->x() * this->x()));
1116 Scalar pitch = std::atan2(2 * (this->x() * this->z() + this->w() * this->y()),
1117 1 - 2 * (this->y() * this->y() + this->x() * this->x()));
1118 return Eigen::Matrix<Scalar, 3, 1>(roll, pitch, yaw);
1119 }

◆ ToEulerAngleYZX()

template<typename Scalar >
Eigen::Matrix< Scalar, 3, 1 > LibXR::Quaternion< Scalar >::ToEulerAngleYZX ( ) const
inline

Definition at line 1091 of file transform.hpp.

1092 {
1093 Scalar roll = std::atan2(2 * this->w() * this->x() - 2 * this->y() * this->z(),
1094 1 - 2 * (this->x() * this->x() + this->z() * this->z()));
1095 Scalar pitch = std::atan2(2 * this->w() * this->y() - 2 * this->x() * this->z(),
1096 1 - 2 * (this->y() * this->y() + this->z() * this->z()));
1097 Scalar yaw = std::asin(2 * (this->w() * this->z() + this->x() * this->y()));
1098 return Eigen::Matrix<Scalar, 3, 1>(roll, pitch, yaw);
1099 }

◆ ToEulerAngleZXY()

template<typename Scalar >
Eigen::Matrix< Scalar, 3, 1 > LibXR::Quaternion< Scalar >::ToEulerAngleZXY ( ) const
inline

Definition at line 1121 of file transform.hpp.

1122 {
1123 Scalar pitch = std::atan2(-2 * (this->x() * this->z() - this->w() * this->y()),
1124 1 - 2 * (this->y() * this->y() + this->x() * this->x()));
1125 Scalar roll = std::asin(2 * (this->w() * this->x() + this->y() * this->z()));
1126 Scalar yaw = std::atan2(-2 * (this->x() * this->y() - this->w() * this->z()),
1127 1 - 2 * (this->z() * this->z() + this->x() * this->x()));
1128 return Eigen::Matrix<Scalar, 3, 1>(roll, pitch, yaw);
1129 }

◆ ToEulerAngleZYX()

template<typename Scalar >
Eigen::Matrix< Scalar, 3, 1 > LibXR::Quaternion< Scalar >::ToEulerAngleZYX ( ) const
inline

Definition at line 1101 of file transform.hpp.

1102 {
1103 Scalar yaw = std::atan2(2 * (this->w() * this->z() + this->x() * this->y()),
1104 1 - 2 * (this->z() * this->z() + this->y() * this->y()));
1105 Scalar pitch = std::asin(2 * (this->w() * this->y() - this->x() * this->z()));
1106 Scalar roll = std::atan2(2 * (this->w() * this->x() + this->y() * this->z()),
1107 1 - 2 * (this->y() * this->y() + this->x() * this->x()));
1108 return Eigen::Matrix<Scalar, 3, 1>(roll, pitch, yaw);
1109 }

◆ ToRotationMatrix()

template<typename Scalar >
Eigen::Matrix< Scalar, 3, 3 > LibXR::Quaternion< Scalar >::ToRotationMatrix ( ) const
inline

将四元数转换为 3x3 旋转矩阵 / Convert the quaternion to a 3x3 rotation matrix

该方法使用四元数生成等效的 3x3 旋转矩阵,矩阵可用于坐标变换、姿态计算等。 This method generates an equivalent 3x3 rotation matrix from the quaternion. The resulting matrix can be used for coordinate transformations, attitude calculations, etc.

Returns
计算得到的 3x3 旋转矩阵 / Computed 3x3 rotation matrix

Definition at line 1161 of file transform.hpp.

1162 {
1163 return Eigen::Matrix<Scalar, 3, 3>(*this);
1164 }

The documentation for this class was generated from the following file: