-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quaternion.ts
413 lines (375 loc) · 9.64 KB
/
Quaternion.ts
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import Vector3 from './Vector3'
import Matrix4 from './Matrix4'
import EulerAngles from './EulerAngles'
import * as MathUtil from './MathUtil'
/**
* 四元数类
*
* @class Quaternion
*/
class Quaternion {
/**
* 单位四元数
*
* @static
* @memberof Quaternion
*/
static QuaternionIdentity = new Quaternion(1, 0, 0, 0)
w: number
x: number
y: number
z: number
constructor(w: number = 0, x: number = 0, y: number = 0, z: number = 0) {
this.w = w
this.x = x
this.y = y
this.z = z
}
/**
* 四元数求负
*
* @static
* @param {Quaternion} a
* @returns {Quaternion}
* @memberof Quaternion
*/
static negate(a: Quaternion): Quaternion {
return new Quaternion(-a.w, -a.x, -a.y, -a.z)
}
/**
* 四元数求模
*
* @static
* @param {Quaternion} a
* @returns {number}
* @memberof Quaternion
*/
static getNorm(a: Quaternion): number {
return Math.sqrt(a.w * a.w + a.x * a.x + a.y * a.y + a.z * a.z)
}
/**
* 四元数求共轭
*
* @static
* @param {Quaternion} a
* @returns {Quaternion}
* @memberof Quaternion
*/
static getConjugate(a: Quaternion): Quaternion {
return new Quaternion(a.w, -a.x, -a.y, -a.z)
}
/**
* 四元数点乘
*
* @static
* @param {Quaternion} a
* @param {Quaternion} b
* @returns {number}
* @memberof Quaternion
*/
static dotProduct(a: Quaternion, b: Quaternion): number {
return a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z
}
/**
* 四元数叉乘
*
* @static
* @param {...Quaternion[]} args
* @returns {Quaternion}
* @memberof Quaternion
*/
static crossProduct(...args: Quaternion[]): Quaternion {
if(args.length < 2) {
throw Error('四元数叉乘至少需要两个参数')
}
// 与标准定义相反
return args.reduce((a: Quaternion, b: Quaternion): Quaternion => {
let w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
let x = a.w * b.x + a.x * b.w + a.z * b.y - a.y * b.z
let y = a.w * b.y + a.y * b.w + a.x * b.z - a.z * b.x
let z = a.w * b.z + a.z * b.w + a.y * b.x - a.x * b.y
return new Quaternion(w, x, y, z)
})
}
/**
* 标量乘
*
* @static
* @param {number} scalar
* @param {Quaternion} a
* @returns {Quaternion}
* @memberof Quaternion
*/
static scalarMultiply(scalar: number, a: Quaternion): Quaternion {
return new Quaternion(scalar * a.w, scalar * a.x, scalar * a.y, scalar * a.z)
}
/**
* 四元数对数
*
* @static
* @param {Quaternion} a
* @returns {Quaternion}
* @memberof Quaternion
*/
static log(a: Quaternion): Quaternion {
let theta: number = a.getRotationAngle()
return new Quaternion(0, theta / 2 * a.x, theta / 2 * a.y, theta / 2 * a.z)
}
/**
* 四元数求幂
*
* @static
* @param {Quaternion} a
* @param {number} exponent
* @returns {Quaternion}
* @memberof Quaternion
*/
static pow(a: Quaternion, exponent: number): Quaternion {
if(Math.abs(a.w) > 0.999) {
return a
}
let alpha = MathUtil.safeAcos(a.w)
let newAlpha = alpha * exponent
let mult = Math.sin(newAlpha) / Math.sin(alpha)
return new Quaternion(Math.cos(alpha), a.x * mult, a.y * mult, a.z * mult)
}
/**
* 由四元数 a 到四元数 b 的角位移
*
* @param {Quaternion} a
* @param {Quaternion} b
* @returns {Quaternion}
* @memberof Quaternion
*/
static getAngularDisplacement(a: Quaternion, b: Quaternion): Quaternion {
return Quaternion.crossProduct(Quaternion.getConjugate(a), b)
}
/**
* 四元数 slerp 插值
*
* @static
* @param {Quaternion} a
* @param {Quaternion} b
* @param {number} t
* @returns {Quaternion}
* @memberof Quaternion
*/
static slerp(a: Quaternion, b: Quaternion, t: number): Quaternion {
let k0: number
let k1: number
let cosOmega: number = Quaternion.dotProduct(a, b)
// 反转,找最短弧度
if(cosOmega < 0) {
b = Quaternion.negate(b)
cosOmega = -cosOmega
}
// 夹角过小,当做平行线
if(cosOmega > 0.9999) {
k0 = 1 - t
k1 = t
}else {
let sinOmega = Math.sqrt(1 - cosOmega * cosOmega)
let omega = Math.atan2(sinOmega, cosOmega)
k0 = Math.sin((1 - t) * omega) / sinOmega
k1 = Math.sin(t * omega) / sinOmega
}
let w: number = a.w * k0 + b.w * k1
let x: number = a.x * k0 + b.x * k1
let y: number = a.y * k0 + b.y * k1
let z: number = a.z * k1 + b.z * k1
return new Quaternion(w, x, y, z)
}
/**
* 从旋转矩阵提取四元数
*
* @static
* @param {RotationMatrix} m
* @returns {Quaternion}
* @memberof Quaternion
*/
static fromRotationMatrix(m: Matrix4): Quaternion {
let w: number = 0
let x: number = 0
let y: number = 0
let z: number = 0
let fourWSquaredMinus1: number = m.m11 + m.m22 + m.m33
let fourXSquaredMinus1: number = m.m11 - m.m22 - m.m33
let fourYSquaredMinus1: number = m.m22 - m.m11 - m.m33
let fourZSquaredMinus1: number = m.m33 - m.m11 - m.m22
let biggestIndex: number = 0
let fourBiggestSquaredMinus1: number = fourWSquaredMinus1
if(fourXSquaredMinus1 > fourBiggestSquaredMinus1) {
fourBiggestSquaredMinus1 = fourXSquaredMinus1
biggestIndex = 1
}
if(fourYSquaredMinus1 > fourBiggestSquaredMinus1) {
fourBiggestSquaredMinus1 = fourYSquaredMinus1
biggestIndex = 2
}
if(fourZSquaredMinus1 > fourBiggestSquaredMinus1) {
fourBiggestSquaredMinus1 = fourZSquaredMinus1
biggestIndex = 3
}
let biggestVal: number = Math.sqrt(fourBiggestSquaredMinus1 + 1) * 0.5
let mult: number = 0.25 / biggestVal
switch(biggestIndex) {
case(0): {
w = biggestVal
x = (m.m23 - m.m32) * mult
y = (m.m31 - m.m13) * mult
z = (m.m12 - m.m21) * mult
break
}
case(1): {
w = biggestVal
x = (m.m23 - m.m32) * mult
y = (m.m12 + m.m21) * mult
z = (m.m31 + m.m13) * mult
break
}
case(2): {
w = biggestVal
x = (m.m31 - m.m13) * mult
y = (m.m12 + m.m21) * mult
z = (m.m23 + m.m32) * mult
break
}
case(3): {
w = biggestVal
x = (m.m12 - m.m21) * mult
y = (m.m31 + m.m13) * mult
z = (m.m23 + m.m13) * mult
break
}
}
return new Quaternion(w, x, y, z)
}
/**
* 从欧拉角构建物体——世界四元数
*
* @static
* @param {EulerAngles} orientation
* @returns {Quaternion}
* @memberof Quaternion
*/
static setObjectToWorldFromEulerAngles(orientation: EulerAngles): Quaternion {
let sinHOver2 = Math.sin(orientation.heading / 2)
let cosHOver2 = Math.cos(orientation.heading / 2)
let sinPOver2 = Math.sin(orientation.picth / 2)
let cosPOver2 = Math.cos(orientation.picth / 2)
let sinBOver2 = Math.sin(orientation.bank / 2)
let cosBOver2 = Math.cos(orientation.bank / 2)
return new Quaternion(
cosHOver2 * cosPOver2 * cosBOver2 + sinHOver2 * sinPOver2 * sinBOver2,
cosHOver2 * sinPOver2 * cosBOver2 + sinHOver2 * cosPOver2 * sinBOver2,
-cosHOver2 * sinPOver2 * sinBOver2 + sinHOver2 * cosPOver2 * cosBOver2,
-sinHOver2 * sinPOver2 * cosBOver2 + cosHOver2 * cosPOver2 * sinBOver2
)
}
/**
* 从欧拉角构建世界——物体四元数
*
* @static
* @param {EulerAngles} orientation
* @returns {Quaternion}
* @memberof Quaternion
*/
static setWorldToObjectFromEulerAngles(orientation: EulerAngles): Quaternion {
return Quaternion.getConjugate(Quaternion.setObjectToWorldFromEulerAngles(orientation))
}
/**
* 绕 X 轴旋转
*
* @param {number} theta
* @memberof Quaternion
*/
setToRotateAboutX(theta: number): void {
this.w = Math.cos(theta / 2)
this.x = Math.sin(theta / 2)
this.y = 0
this.z = 0
}
/**
* 绕 Y 轴旋转
*
* @param {number} theta
* @memberof Quaternion
*/
setToRotateAboutY(theta: number): void {
this.w = Math.cos(theta / 2)
this.x = 0
this.y = Math.sin(theta / 2)
this.z = 0
}
/**
* 绕 Z 轴旋转
*
* @param {number} theta
* @memberof Quaternion
*/
setToRotateAboutZ(theta: number): void {
this.w = Math.cos(theta / 2)
this.x = 0
this.y = 0
this.z = Math.sin(theta / 2)
}
/**
* 绕指定轴旋转
*
* @param {Vector3} axis
* @param {number} theta
* @memberof Quaternion
*/
setToRotateAboutAxis(axis: Vector3, theta: number): void {
// 旋转轴向量必须标准化
if(Vector3.getNorm(axis) - 1 >= 0.01) {
throw Error('构建四元数时,旋转轴向量必须标准化')
}
this.w = Math.cos(theta / 2)
this.x = Math.sin(theta / 2) * axis.x
this.y = Math.sin(theta / 2) * axis.y
this.z = Math.sin(theta / 2) * axis.z
}
/**
* 标准化四元数
*
* @memberof Quaternion
*/
normalize(): void {
let norm = Quaternion.getNorm(this)
if(norm) {
this.w = this.w / norm
this.x = this.x / norm
this.y = this.y / norm
this.z = this.z / norm
}else {
this.w = 1
this.x = 0
this.y = 0
this.z = 0
}
}
/**
* 提取旋转角
*
* @returns {number}
* @memberof Quaternion
*/
getRotationAngle(): number {
return 2 * MathUtil.safeAcos(this.w)
}
/**
* 提取旋转轴
*
* @returns {Vector3}
* @memberof Quaternion
*/
getRotationAxis(): Vector3 {
let sinThetaOver2 = Math.sqrt(1 - this.w * this.w)
if(!sinThetaOver2) {
return new Vector3(1, 0, 0)
}
return new Vector3(this.x / sinThetaOver2, this.y / sinThetaOver2, this.z / sinThetaOver2)
}
}
export default Quaternion