如何将欧拉角转换为前,上,右矢量
How to convert Euler Angles to Front, Up, Right vectors
本文关键字:转换 更新时间:2023-10-16
我需要一个函数,给定偏航,俯仰和滚动,可以在"世界坐标"中产生前(或看),右和向上向量。
在我的特定世界空间中,从原点(0,0,0)开始,X为正向左,Z为正远离观察者/原点,Y为正向上。
例如,给定…(角度单位:度)
-
偏航=0,俯仰=0,横摇=0,预期输出为:
- front = (0.0,0.0,1.0)
- right = (-1.0,0.0,0.0)
- up = (0.0,1.0,0.0)
-
偏航=90,俯仰=0,横摇=0,预期输出为:
- front = (1.0,0.0,0.0)
- right = (0,0,0.0,1.0)
- up = (0.0,1.0,0.0)
-
偏航=0,俯仰=90,横摇=0,预期输出为:
- front = (0.0,1.0,0.0)
- right = (-1.0,0.0,0.0)
- up = (0.0,0.0,-1.0)
-
偏航=0,俯仰=0,横摇=90,预期输出为:
- front = (0.0,0.0,1.0)
- right = (0.0,1.0,0.0)
- up = (1.0,0.0,0.0)
我正在使用的语言是c++,如果最有意义的话,我很乐意使用glm来解决这个问题。如果我能通过四元数得到它,我对这个解决方案也很满意,因为我已经找到了其他教程,描述了如何从欧拉角度得到四元数。
下面是一个完整的工作示例。它不是很像c++。您可能希望使用一个真正的矩阵类,但出于演示目的,它应该是可以的。从你的问题中有一点不清楚,那就是旋转顺序,但这很容易改变。
#include <iostream>
#include <cmath>
#include <cstdlib>
typedef float Float;
typedef Float Axis[3];
typedef Axis Axes[3];
static void copy(const Axes &from,Axes &to)
{
for (size_t i=0; i!=3; ++i) {
for (size_t j=0; j!=3; ++j) {
to[i][j] = from[i][j];
}
}
}
static void mul(Axes &mat,Axes &b)
{
Axes result;
for (size_t i=0; i!=3; ++i) {
for (size_t j=0; j!=3; ++j) {
Float sum = 0;
for (size_t k=0; k!=3; ++k) {
sum += mat[i][k]*b[k][j];
}
result[i][j] = sum;
}
}
copy(result,mat);
}
static void getAxes(Axes &result,Float yaw,Float pitch,Float roll)
{
Float x = -pitch;
Float y = yaw;
Float z = -roll;
Axes matX = {
{1, 0, 0 },
{0, cos(x),sin(x)},
{0,-sin(x),cos(x)}
};
Axes matY = {
{cos(y),0,-sin(y)},
{ 0,1, 0},
{sin(y),0, cos(y)}
};
Axes matZ = {
{ cos(z),sin(z),0},
{-sin(z),cos(z),0},
{ 0, 0,1}
};
Axes axes = {
{1,0,0},
{0,1,0},
{0,0,1}
};
mul(axes,matX);
mul(axes,matY);
mul(axes,matZ);
copy(axes,result);
}
static void showAxis(const char *desc,const Axis &axis,Float sign)
{
std::cout << " " << desc << " = (";
for (size_t i=0; i!=3; ++i) {
if (i!=0) {
std::cout << ",";
}
std::cout << axis[i]*sign;
}
std::cout << ")n";
}
static void showAxes(const char *desc,Axes &axes)
{
std::cout << desc << ":n";
showAxis("front",axes[2],1);
showAxis("right",axes[0],-1);
showAxis("up",axes[1],1);
}
int main(int,char**)
{
Axes axes;
std::cout.setf(std::ios::fixed);
std::cout.precision(1);
getAxes(axes,0,0,0);
showAxes("yaw=0, pitch=0, roll=0",axes);
getAxes(axes,M_PI/2,0,0);
showAxes("yaw=90, pitch=0, roll=0",axes);
getAxes(axes,0,M_PI/2,0);
showAxes("yaw=0, pitch=90, roll=0",axes);
getAxes(axes,0,0,M_PI/2);
showAxes("yaw=0, pitch=0, roll=90",axes);
return 0;
}
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 正在将指针转换为范围
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 是否可以从int转换为enum类类型
- 了解 GLM- openGL 中的相机转换
- 将无符号char*转换为std::istream*C++