如何从矢量轴旋转中得到每个轴的旋转? [英] how do I get rotations of each axis from rotations with a vector axis?

查看:53
本文介绍了如何从矢量轴旋转中得到每个轴的旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个以vector3D作为轴和角度的AxisAngle4f对象,如何获得x,y,z轴的旋转角度?

so I have an AxisAngle4f object with a vector3D as axis and an angle, how do I get rotation angle for each of x,y,z axes?

推荐答案

  1. 创建代表您旋转的4x4均匀变换矩阵

首先请参见了解4x4均匀变换矩阵,因此基本上,您需要3个基向量和单位矩阵的原点,然后分别旋转您的轮换(为此,您可以使用this或 glRotate 或其他任何方式).这里是C ++示例:

first see Understanding 4x4 homogenous transform matrices so basically you want 3 basis vectors and origin of unit matrix then rotate each by your rotation (for that you can use this or glRotate or whatever). Here C++ example:

void rotate3d(float alfa,float *axis,float *point)
     {
     float p[3],q[3],c=cos(alfa),s=sin(alfa);
     //Euler Rodrigues' rotation formula
     vector_mul(q,point,c);
     vector_mul(p,axis,point);
     vector_mul(p,p,s);
     vector_add(p,p,q);
     vector_mul(q,axis,vector_mul(axis,point)*(1.0-c));
     vector_add(point,p,q);
     }

上面链接中描述了矢量数学函数(带有源代码).使用它们时,只需将 double 更改为 float .因此,在C ++中它可以归结为这样的东西:

The vector math functions are described (with source) in the link above. Just change the double into float as you are using those. So it boils up to something like this in C++:

float X[3] = { 1.0,0.0,0.0 };
float Y[3] = { 0.0,1.0,0.0 };
float Z[3] = { 0.0,0.0,1.0 };
float O[3] = { 0.0,0.0,0.0 };
float M[16];
float AxisAngle4f[4]={x,y,z,angle};
rotate3d(AxisAngle4f[3],AxisAngle4f,X);
rotate3d(AxisAngle4f[3],AxisAngle4f,Y);
rotate3d(AxisAngle4f[3],AxisAngle4f,Z);
rotate3d(AxisAngle4f[3],AxisAngle4f,O);
M[0]=X[0]; M[4]=Y[0]; M[ 8]=Z[0]; M[12]=O[0];
M[1]=X[1]; M[5]=Y[1]; M[ 9]=Z[1]; M[13]=O[1];
M[2]=X[2]; M[6]=Y[2]; M[10]=Z[2]; M[14]=O[2];
M[3]= 0.0; M[7]= 0.0; M[11]= 0.0; M[15]= 1.0;

其中 M 是表示旋转的OpenGL样式直接矩阵.

Where M is OpenGL style direct matrix representing your rotation.

M 转换为您的欧拉角

convert M into your Euler angles

请参见是否可以通过4x4矩阵计算X和Y轴上的3D旋转有关如何(再次更改)的方法到 floats )...

see Is there a way to calculate 3D rotation on X and Y axis from a 4x4 matrix on how (again change to floats)...

const float deg=M_PI/180.0;
const float rad=180.0/M_PI;
// variables
float e[3],m[16];
int euler_cfg[_euler_cfgs];
// init angles
e[0]=10.0*deg;
e[1]=20.0*deg;
e[2]=30.0*deg;
// compute coresponding rotation matrix with your environment
m = some_rotate_of yours(e)
// cross match e,m -> euler_cfg
matrix2euler_init(e,m,euler_cfg);

// now we can convert M into e
matrix2euler(e,M,euler_cfg);
// e holds your euler angles you want

只需一次初始化 euler_cfg ,然后您就可以随意使用 matrix2euler .

The init of euler_cfg is needed just once then you can use matrix2euler at will.

这篇关于如何从矢量轴旋转中得到每个轴的旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆