四元数,欧拉角和旋转矩阵之间的互相转换

摘自:http://www.oschina.net/code/snippet_876234_20178


公式是从网上搜索的,代码自己编写的,适用任意支持C的平台,原本是为了6轴融合模拟9轴效果用的。 
标签: <无>

代码片段(1)[全屏查看所有代码]

1. [文件] 四元数与欧拉角以及矩阵之间的转换.c ~ 4KB     下载(93)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//公式都是网上搜罗的,下面这些经过简单的测试,确认可用。
 
//ps: x,y,z,w 分别是四元素的四个值。稍微修改下就可以用。
   // 由旋转矩阵创建四元数
   inline CQuaternion( const _Matrix4& m)
   {
    float tr, s, q[4];
    int i, j, k;
    
    int nxt[3] = {1, 2, 0 };
    // 计算矩阵轨迹
    tr = m._11 + m._22 + m._33;
    
    // 检查矩阵轨迹是正还是负
    if (tr>0.0f)
    {
     s = sqrt (tr + 1.0f);
     this ->w = s / 2.0f;
     s = 0.5f / s;
     this ->x = (m._23 - m._32) * s;
     this ->y = (m._31 - m._13) * s;
     this ->z = (m._12 - m._21) * s;
    }
    else
    {
     // 轨迹是负
     // 寻找m11 m22 m33中的最大分量
     i = 0;
     if (m.m[1][1]>m.m[0][0]) i = 1;
     if (m.m[2][2]>m.m[i][i]) i = 2;
     j = nxt[i];
     k = nxt[j];
     
     s = sqrt ((m.m[i][i] - (m.m[j][j] + m.m[k][k])) + 1.0f);
     q[i] = s * 0.5f;
     if ( s!= 0.0f) s = 0.5f / s;
     q[3] = (m.m[j][k] - m.m[k][j]) * s;
     q[j] = (m.m[i][j] - m.m[j][i]) * s;
     q[k] = (m.m[i][k] - m.m[k][i]) * s;
     this ->x = q[0];
     this ->y = q[1];
     this ->z = q[2];
     this ->w = q[3];
    }
   };
 
   // 由欧拉角创建四元数
   inline CQuaternion( const _Vector3& angle)
   {
    float cx = cos (angle.x/2);
    float sx = sin (angle.x/2);
    float cy = cos (angle.y/2);
    float sy = sin (angle.y/2);
    float cz = cos (angle.z/2);
    float sz = sin (angle.z/2);
 
    this ->w = cx*cy*cz + sx*sy*sz;
    this ->x = sx*cy*cz - cx*sy*sz;
    this ->y = cx*sy*cz + sx*cy*sz;
    this ->z = cx*cy*sz - sx*sy*cz;
   };
 
   // 给定角度和轴创建四元数
   inline CQuaternion(_Vector3 anxi, const float & angle)
   {
    CVector3 t;
    t.x = anxi.x;
    t.y = anxi.y;
    t.z = anxi.z;
    t.Normalize();
    float cosa = cos (angle);
    float sina = sin (angle);
    this ->w = cosa;
    this ->x = sina * t.x;
    this ->y = sina * t.y;
    this ->z = sina * t.z;
   };
 
// 由旋转四元数推导出矩阵
   inline CMatrix4 GetMatrixLH()
   {
    CMatrix4 ret;
    float xx = x*x;
    float yy = y*y;
    float zz = z*z;
    float xy = x*y;
    float wz = w*z;
    float wy = w*y;
    float xz = x*z;
    float yz = y*z;
    float wx = w*x;
 
    ret._11 = 1.0f-2*(yy+zz);
    ret._12 = 2*(xy-wz);
    ret._13 = 2*(wy+xz);
    ret._14 = 0.0f;
 
    ret._21 = 2*(xy+wz);
    ret._22 = 1.0f-2*(xx+zz);
    ret._23 = 2*(yz-wx);
    ret._24 = 0.0f;
 
    ret._31 = 2*(xy-wy);
    ret._32 = 2*(yz+wx);
    ret._33 = 1.0f-2*(xx+yy);
    ret._34 = 0.0f;
 
    ret._41 = 0.0f;
    ret._42 = 0.0f;
    ret._43 = 0.0f;
    ret._44 = 1.0f;
 
    return ret;
   };
   inline CMatrix4 GetMatrixRH()
   {
    CMatrix4 ret;
    float xx = x*x;
    float yy = y*y;
    float zz = z*z;
    float xy = x*y;
    float wz = -w*z;
    float wy = -w*y;
    float xz = x*z;
    float yz = y*z;
    float wx = -w*x;
 
    ret._11 = 1.0f-2*(yy+zz);
    ret._12 = 2*(xy-wz);
    ret._13 = 2*(wy+xz);
    ret._14 = 0.0f;
 
    ret._21 = 2*(xy+wz);
    ret._22 = 1.0f-2*(xx+zz);
    ret._23 = 2*(yz-wx);
    ret._24 = 0.0f;
 
    ret._31 = 2*(xy-wy);
    ret._32 = 2*(yz+wx);
    ret._33 = 1.0f-2*(xx+yy);
    ret._34 = 0.0f;
 
    ret._41 = 0.0f;
    ret._42 = 0.0f;
    ret._43 = 0.0f;
    ret._44 = 1.0f;
 
    return ret;
   };
 
   // 由四元数返回欧拉角(主要是这个dx api里没有提供)
   inline CVector3 GetEulerAngle()
   {
    CVector3 ret;
 
    float test = y*z + x*w;
    if (test > 0.4999f)
    {
     ret.z = 2.0f * atan2 (y, w);
     ret.y = PIOver2;
     ret.x = 0.0f;
     return ret;
    }
    if (test < -0.4999f)
    {
     ret.z = 2.0f * atan2 (y, w);
     ret.y = -PIOver2;
     ret.x = 0.0f;
     return ret;
    }
    float sqx = x * x;
    float sqy = y * y;
    float sqz = z * z;
    ret.z = atan2 (2.0f * z * w - 2.0f * y * x, 1.0f - 2.0f * sqz - 2.0f * sqx);
    ret.y = asin (2.0f * test);
    ret.x = atan2 (2.0f * y * w - 2.0f * z * x, 1.0f - 2.0f * sqy - 2.0f * sqx);
      
    return ret;
   };

### 如何将四元数转换旋转矩阵 #### 方法与公式 四元数可以表示三维空间中的旋转,其形式通常写作 \( q = w + xi + yj + zk \),其中 \( w, x, y, z \) 是实数分量。为了将其转化为旋转矩阵,可以通过以下公式实现: 设四元数为 \( q = (w, x, y, z) \),则对应的旋转矩阵 \( R \) 可以通过下述方式构建[^1]: \[ R = \begin{bmatrix} 1 - 2(y^2 + z^2) & 2(xy - wz) & 2(xz + wy) \\ 2(xy + wz) & 1 - 2(x^2 + z^2) & 2(yz - wx) \\ 2(xz - wy) & 2(yz + wx) & 1 - 2(x^2 + y^2) \end{bmatrix} \] 此公式的推导基于四元数的性质以及它在线性变换中的应用。 #### 示例代码 以下是 Python 中的一个简单示例,展示如何将四元数转换旋转矩阵: ```python import numpy as np def quaternion_to_rotation_matrix(q): """ 将四元数转换旋转矩阵 参数: q: 四元数列表 [w, x, y, z] 返回: 3x3 的旋转矩阵 """ w, x, y, z = q xx, yy, zz = x * x, y * y, z * z xy, xz, yz = x * y, x * z, y * z wx, wy, wz = w * x, w * y, w * z rotation_matrix = np.array([ [1 - 2*(yy + zz), 2*(xy - wz), 2*(xz + wy)], [ 2*(xy + wz), 1 - 2*(xx + zz), 2*(yz - wx)], [ 2*(xz - wy), 2*(yz + wx), 1 - 2*(xx + yy)] ]) return rotation_matrix # 测试用例 quaternion = [0.7071, 0.0, 0.7071, 0.0] # 示例四元数 rotation_matrix = quaternion_to_rotation_matrix(quaternion) print("旋转矩阵:") print(rotation_matrix) ``` 以上代码定义了一个函数 `quaternion_to_rotation_matrix`,该函数接受一个四元数作为输入返回相应的旋转矩阵[^1]。 #### Unity 实现案例 在 Unity 中,也可以轻松完成这一过程。下面是一个简单的 C# 示例,演示如何利用内置 API 完成四元数旋转矩阵的转化[^3]: ```csharp using UnityEngine; public class Example : MonoBehaviour { void Start() { // 创建一个四元数 Quaternion q = Quaternion.Euler(90, 45, 0); // 构建旋转矩阵 Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, q, Vector3.one); Debug.Log(matrix.ToString()); } } ``` 在此例子中,Unity 提供了方便的方法来处理四元数旋转矩阵之间的相互转换。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值