-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
quat.ts
780 lines (684 loc) · 23.3 KB
/
quat.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/*
Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* @category core/math
*/
import { CCClass } from '../data/class';
import { ValueType } from '../value-types/value-type';
import { Mat3 } from './mat3';
import { IQuatLike, IVec3Like } from './type-define';
import { EPSILON, toDegree } from './utils';
import { Vec3 } from './vec3';
import { legacyCC } from '../global-exports';
/**
* 四元数。
*/
export class Quat extends ValueType {
public static IDENTITY = Object.freeze(new Quat());
/**
* @zh 获得指定四元数的拷贝
*/
public static clone<Out extends IQuatLike> (a: Out) {
return new Quat(a.x, a.y, a.z, a.w);
}
/**
* @zh 复制目标四元数
*/
public static copy<Out extends IQuatLike, QuatLike extends IQuatLike> (out: Out, a: QuatLike) {
out.x = a.x;
out.y = a.y;
out.z = a.z;
out.w = a.w;
return out;
}
/**
* @zh 设置四元数值
*/
public static set<Out extends IQuatLike> (out: Out, x: number, y: number, z: number, w: number) {
out.x = x;
out.y = y;
out.z = z;
out.w = w;
return out;
}
/**
* @zh 将目标赋值为单位四元数
*/
public static identity<Out extends IQuatLike> (out: Out) {
out.x = 0;
out.y = 0;
out.z = 0;
out.w = 1;
return out;
}
/**
* @zh 设置四元数为两向量间的最短路径旋转,默认两向量都已归一化
*/
public static rotationTo<Out extends IQuatLike, VecLike extends IVec3Like> (out: Out, a: VecLike, b: VecLike) {
const dot = Vec3.dot(a, b);
if (dot < -0.999999) {
Vec3.cross(v3_1, Vec3.UNIT_X, a);
if (v3_1.length() < 0.000001) {
Vec3.cross(v3_1, Vec3.UNIT_Y, a);
}
Vec3.normalize(v3_1, v3_1);
Quat.fromAxisAngle(out, v3_1, Math.PI);
return out;
} else if (dot > 0.999999) {
out.x = 0;
out.y = 0;
out.z = 0;
out.w = 1;
return out;
} else {
Vec3.cross(v3_1, a, b);
out.x = v3_1.x;
out.y = v3_1.y;
out.z = v3_1.z;
out.w = 1 + dot;
return Quat.normalize(out, out);
}
}
/**
* @zh 获取四元数的旋转轴和旋转弧度
* @param outAxis 旋转轴输出
* @param q 源四元数
* @return 旋转弧度
*/
public static getAxisAngle<Out extends IQuatLike, VecLike extends IVec3Like> (outAxis: VecLike, q: Out) {
const rad = Math.acos(q.w) * 2.0;
const s = Math.sin(rad / 2.0);
if (s !== 0.0) {
outAxis.x = q.x / s;
outAxis.y = q.y / s;
outAxis.z = q.z / s;
} else {
// If s is zero, return any axis (no rotation - axis does not matter)
outAxis.x = 1;
outAxis.y = 0;
outAxis.z = 0;
}
return rad;
}
/**
* @zh 四元数乘法
*/
public static multiply<Out extends IQuatLike, QuatLike_1 extends IQuatLike, QuatLike_2 extends IQuatLike> (out: Out, a: QuatLike_1, b: QuatLike_2) {
const x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;
const y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;
const z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;
const w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;
out.x = x;
out.y = y;
out.z = z;
out.w = w;
return out;
}
/**
* @zh 四元数标量乘法
*/
public static multiplyScalar<Out extends IQuatLike> (out: Out, a: Out, b: number) {
out.x = a.x * b;
out.y = a.y * b;
out.z = a.z * b;
out.w = a.w * b;
return out;
}
/**
* @zh 四元数乘加:A + B * scale
*/
public static scaleAndAdd<Out extends IQuatLike> (out: Out, a: Out, b: Out, scale: number) {
out.x = a.x + b.x * scale;
out.y = a.y + b.y * scale;
out.z = a.z + b.z * scale;
out.w = a.w + b.w * scale;
return out;
}
/**
* @zh 绕 X 轴旋转指定四元数
* @param rad 旋转弧度
*/
public static rotateX<Out extends IQuatLike> (out: Out, a: Out, rad: number) {
rad *= 0.5;
const bx = Math.sin(rad);
const bw = Math.cos(rad);
const { x, y, z, w } = a;
out.x = x * bw + w * bx;
out.y = y * bw + z * bx;
out.z = z * bw - y * bx;
out.w = w * bw - x * bx;
return out;
}
/**
* @zh 绕 Y 轴旋转指定四元数
* @param rad 旋转弧度
*/
public static rotateY<Out extends IQuatLike> (out: Out, a: Out, rad: number) {
rad *= 0.5;
const by = Math.sin(rad);
const bw = Math.cos(rad);
const { x, y, z, w } = a;
out.x = x * bw - z * by;
out.y = y * bw + w * by;
out.z = z * bw + x * by;
out.w = w * bw - y * by;
return out;
}
/**
* @zh 绕 Z 轴旋转指定四元数
* @param rad 旋转弧度
*/
public static rotateZ<Out extends IQuatLike> (out: Out, a: Out, rad: number) {
rad *= 0.5;
const bz = Math.sin(rad);
const bw = Math.cos(rad);
const { x, y, z, w } = a;
out.x = x * bw + y * bz;
out.y = y * bw - x * bz;
out.z = z * bw + w * bz;
out.w = w * bw - z * bz;
return out;
}
/**
* @zh 绕世界空间下指定轴旋转四元数
* @param axis 旋转轴,默认已归一化
* @param rad 旋转弧度
*/
public static rotateAround<Out extends IQuatLike, VecLike extends IVec3Like> (out: Out, rot: Out, axis: VecLike, rad: number) {
// get inv-axis (local to rot)
Quat.invert(qt_1, rot);
Vec3.transformQuat(v3_1, axis, qt_1);
// rotate by inv-axis
Quat.fromAxisAngle(qt_1, v3_1, rad);
Quat.multiply(out, rot, qt_1);
return out;
}
/**
* @zh 绕本地空间下指定轴旋转四元数
* @param axis 旋转轴
* @param rad 旋转弧度
*/
public static rotateAroundLocal<Out extends IQuatLike, VecLike extends IVec3Like> (out: Out, rot: Out, axis: VecLike, rad: number) {
Quat.fromAxisAngle(qt_1, axis, rad);
Quat.multiply(out, rot, qt_1);
return out;
}
/**
* @zh 根据 xyz 分量计算 w 分量,默认已归一化
*/
public static calculateW<Out extends IQuatLike> (out: Out, a: Out) {
out.x = a.x;
out.y = a.y;
out.z = a.z;
out.w = Math.sqrt(Math.abs(1.0 - a.x * a.x - a.y * a.y - a.z * a.z));
return out;
}
/**
* @zh 四元数点积(数量积)
*/
public static dot<Out extends IQuatLike> (a: Out, b: Out) {
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}
/**
* @zh 逐元素线性插值: A + t * (B - A)
*/
public static lerp<Out extends IQuatLike> (out: Out, a: Out, b: Out, t: number) {
out.x = a.x + t * (b.x - a.x);
out.y = a.y + t * (b.y - a.y);
out.z = a.z + t * (b.z - a.z);
out.w = a.w + t * (b.w - a.w);
return out;
}
/**
* @zh 四元数球面插值
*/
public static slerp<Out extends IQuatLike, QuatLike_1 extends IQuatLike, QuatLike_2 extends IQuatLike>
(out: Out, a: QuatLike_1, b: QuatLike_2, t: number) {
// benchmarks:
// http://jsperf.com/quaternion-slerp-implementations
let scale0 = 0;
let scale1 = 0;
let bx = b.x;
let by = b.y;
let bz = b.z;
let bw = b.w;
// calc cosine
let cosom = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
// adjust signs (if necessary)
if (cosom < 0.0) {
cosom = -cosom;
bx = -bx;
by = -by;
bz = -bz;
bw = -bw;
}
// calculate coefficients
if ((1.0 - cosom) > 0.000001) {
// standard case (slerp)
const omega = Math.acos(cosom);
const sinom = Math.sin(omega);
scale0 = Math.sin((1.0 - t) * omega) / sinom;
scale1 = Math.sin(t * omega) / sinom;
} else {
// "from" and "to" quaternions are very close
// ... so we can do a linear interpolation
scale0 = 1.0 - t;
scale1 = t;
}
// calculate final values
out.x = scale0 * a.x + scale1 * bx;
out.y = scale0 * a.y + scale1 * by;
out.z = scale0 * a.z + scale1 * bz;
out.w = scale0 * a.w + scale1 * bw;
return out;
}
/**
* @zh 带两个控制点的四元数球面插值
*/
public static sqlerp<Out extends IQuatLike> (out: Out, a: Out, b: Out, c: Out, d: Out, t: number) {
Quat.slerp(qt_1, a, d, t);
Quat.slerp(qt_2, b, c, t);
Quat.slerp(out, qt_1, qt_2, 2 * t * (1 - t));
return out;
}
/**
* @zh 四元数求逆
*/
public static invert<Out extends IQuatLike, QuatLike extends IQuatLike> (out: Out, a: QuatLike) {
const dot = a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w;
const invDot = dot ? 1.0 / dot : 0;
// TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
out.x = -a.x * invDot;
out.y = -a.y * invDot;
out.z = -a.z * invDot;
out.w = a.w * invDot;
return out;
}
/**
* @zh 求共轭四元数,对单位四元数与求逆等价,但更高效
*/
public static conjugate<Out extends IQuatLike> (out: Out, a: Out) {
out.x = -a.x;
out.y = -a.y;
out.z = -a.z;
out.w = a.w;
return out;
}
/**
* @zh 求四元数长度
*/
public static len<Out extends IQuatLike> (a: Out) {
return Math.sqrt(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w);
}
/**
* @zh 求四元数长度平方
*/
public static lengthSqr<Out extends IQuatLike> (a: Out) {
return a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w;
}
/**
* @zh 归一化四元数
*/
public static normalize<Out extends IQuatLike> (out: Out, a: Out) {
let len = a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w;
if (len > 0) {
len = 1 / Math.sqrt(len);
out.x = a.x * len;
out.y = a.y * len;
out.z = a.z * len;
out.w = a.w * len;
}
return out;
}
/**
* @zh 根据本地坐标轴朝向计算四元数,默认三向量都已归一化且相互垂直
*/
public static fromAxes<Out extends IQuatLike, VecLike extends IVec3Like> (out: Out, xAxis: VecLike, yAxis: VecLike, zAxis: VecLike) {
Mat3.set(m3_1,
xAxis.x, xAxis.y, xAxis.z,
yAxis.x, yAxis.y, yAxis.z,
zAxis.x, zAxis.y, zAxis.z,
);
return Quat.normalize(out, Quat.fromMat3(out, m3_1));
}
/**
* @zh 根据视口的前方向和上方向计算四元数
* @param view 视口面向的前方向,必须归一化
* @param up 视口的上方向,必须归一化,默认为 (0, 1, 0)
*/
public static fromViewUp<Out extends IQuatLike, VecLike extends IVec3Like> (out: Out, view: VecLike, up?: Vec3) {
Mat3.fromViewUp(m3_1, view, up);
return Quat.normalize(out, Quat.fromMat3(out, m3_1));
}
/**
* @zh 根据旋转轴和旋转弧度计算四元数
*/
public static fromAxisAngle<Out extends IQuatLike, VecLike extends IVec3Like> (out: Out, axis: VecLike, rad: number) {
rad = rad * 0.5;
const s = Math.sin(rad);
out.x = s * axis.x;
out.y = s * axis.y;
out.z = s * axis.z;
out.w = Math.cos(rad);
return out;
}
/**
* @zh 根据三维矩阵信息计算四元数,默认输入矩阵不含有缩放信息
*/
public static fromMat3<Out extends IQuatLike> (out: Out, m: Mat3) {
const {
m00: m00, m03: m01, m06: m02,
m01: m10, m04: m11, m07: m12,
m02: m20, m05: m21, m08: m22,
} = m;
const trace = m00 + m11 + m22;
if (trace > 0) {
const s = 0.5 / Math.sqrt(trace + 1.0);
out.w = 0.25 / s;
out.x = (m21 - m12) * s;
out.y = (m02 - m20) * s;
out.z = (m10 - m01) * s;
} else if ((m00 > m11) && (m00 > m22)) {
const s = 2.0 * Math.sqrt(1.0 + m00 - m11 - m22);
out.w = (m21 - m12) / s;
out.x = 0.25 * s;
out.y = (m01 + m10) / s;
out.z = (m02 + m20) / s;
} else if (m11 > m22) {
const s = 2.0 * Math.sqrt(1.0 + m11 - m00 - m22);
out.w = (m02 - m20) / s;
out.x = (m01 + m10) / s;
out.y = 0.25 * s;
out.z = (m12 + m21) / s;
} else {
const s = 2.0 * Math.sqrt(1.0 + m22 - m00 - m11);
out.w = (m10 - m01) / s;
out.x = (m02 + m20) / s;
out.y = (m12 + m21) / s;
out.z = 0.25 * s;
}
return out;
}
/**
* @zh 根据欧拉角信息计算四元数,旋转顺序为 YZX
*/
public static fromEuler<Out extends IQuatLike> (out: Out, x: number, y: number, z: number) {
x *= halfToRad;
y *= halfToRad;
z *= halfToRad;
const sx = Math.sin(x);
const cx = Math.cos(x);
const sy = Math.sin(y);
const cy = Math.cos(y);
const sz = Math.sin(z);
const cz = Math.cos(z);
out.x = sx * cy * cz + cx * sy * sz;
out.y = cx * sy * cz + sx * cy * sz;
out.z = cx * cy * sz - sx * sy * cz;
out.w = cx * cy * cz - sx * sy * sz;
return out;
}
/**
* @zh 返回定义此四元数的坐标系 X 轴向量
*/
public static toAxisX (out: IVec3Like, q: IQuatLike) {
const fy = 2.0 * q.y;
const fz = 2.0 * q.z;
out.x = 1.0 - fy * q.y - fz * q.z;
out.y = fy * q.x + fz * q.w;
out.z = fz * q.x + fy * q.w;
return out;
}
/**
* @zh 返回定义此四元数的坐标系 Y 轴向量
*/
public static toAxisY (out: IVec3Like, q: IQuatLike) {
const fx = 2.0 * q.x;
const fy = 2.0 * q.y;
const fz = 2.0 * q.z;
out.x = fy * q.x - fz * q.w;
out.y = 1.0 - fx * q.x - fz * q.z;
out.z = fz * q.y + fx * q.w;
return out;
}
/**
* @zh 返回定义此四元数的坐标系 Z 轴向量
*/
public static toAxisZ (out: IVec3Like, q: IQuatLike) {
const fx = 2.0 * q.x;
const fy = 2.0 * q.y;
const fz = 2.0 * q.z;
out.x = fz * q.x - fy * q.w;
out.y = fz * q.y - fx * q.w;
out.z = 1.0 - fx * q.x - fy * q.y;
return out;
}
/**
* @zh 根据四元数计算欧拉角,返回角度 x, y 在 [-180, 180] 区间内, z 默认在 [-90, 90] 区间内,旋转顺序为 YZX
* @param outerZ z 取值范围区间改为 [-180, -90] U [90, 180]
*/
public static toEuler (out: IVec3Like, q: IQuatLike, outerZ?: boolean) {
const { x, y, z, w } = q;
let bank = 0;
let heading = 0;
let attitude = 0;
const test = x * y + z * w;
if (test > 0.499999) {
bank = 0; // default to zero
heading = toDegree(2 * Math.atan2(x, w));
attitude = 90;
} else if (test < -0.499999) {
bank = 0; // default to zero
heading = -toDegree(2 * Math.atan2(x, w));
attitude = -90;
} else {
const sqx = x * x;
const sqy = y * y;
const sqz = z * z;
bank = toDegree(Math.atan2(2 * x * w - 2 * y * z, 1 - 2 * sqx - 2 * sqz));
heading = toDegree(Math.atan2(2 * y * w - 2 * x * z, 1 - 2 * sqy - 2 * sqz));
attitude = toDegree(Math.asin(2 * test));
if (outerZ) {
bank = -180 * Math.sign(bank + 1e-6) + bank;
heading = -180 * Math.sign(heading + 1e-6) + heading;
attitude = 180 * Math.sign(attitude + 1e-6) - attitude;
}
}
out.x = bank; out.y = heading; out.z = attitude;
return out;
}
/**
* @zh 四元数转数组
* @param ofs 数组内的起始偏移量
*/
public static toArray<Out extends IWritableArrayLike<number>> (out: Out, q: IQuatLike, ofs = 0) {
out[ofs + 0] = q.x;
out[ofs + 1] = q.y;
out[ofs + 2] = q.z;
out[ofs + 3] = q.w;
return out;
}
/**
* @zh 数组转四元数
* @param ofs 数组起始偏移量
*/
public static fromArray (out: IQuatLike, arr: IWritableArrayLike<number>, ofs = 0) {
out.x = arr[ofs + 0];
out.y = arr[ofs + 1];
out.z = arr[ofs + 2];
out.w = arr[ofs + 3];
return out;
}
/**
* @zh 四元数等价判断
*/
public static strictEquals (a: IQuatLike, b: IQuatLike) {
return a.x === b.x && a.y === b.y && a.z === b.z && a.w === b.w;
}
/**
* @zh 排除浮点数误差的四元数近似等价判断
*/
public static equals (a: IQuatLike, b: IQuatLike, epsilon = EPSILON) {
return (Math.abs(a.x - b.x) <= epsilon * Math.max(1.0, Math.abs(a.x), Math.abs(b.x)) &&
Math.abs(a.y - b.y) <= epsilon * Math.max(1.0, Math.abs(a.y), Math.abs(b.y)) &&
Math.abs(a.z - b.z) <= epsilon * Math.max(1.0, Math.abs(a.z), Math.abs(b.z)) &&
Math.abs(a.w - b.w) <= epsilon * Math.max(1.0, Math.abs(a.w), Math.abs(b.w)));
}
/**
* x 分量。
*/
public declare x: number;
/**
* y 分量。
*/
public declare y: number;
/**
* z 分量。
*/
public declare z: number;
/**
* w 分量。
*/
public declare w: number;
constructor (other: Quat);
constructor (x?: number, y?: number, z?: number, w?: number);
constructor (x?: number | IQuatLike, y?: number, z?: number, w?: number) {
super();
if (x && typeof x === 'object') {
this.x = x.x;
this.y = x.y;
this.z = x.z;
this.w = x.w;
} else {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
this.w = w ?? 1;
}
}
/**
* @zh 克隆当前四元数。
*/
public clone () {
return new Quat(this.x, this.y, this.z, this.w);
}
/**
* @zh 设置当前四元数使其与指定四元数相等。
* @param other 相比较的四元数。
* @returns `this`
*/
public set (other: Quat): Quat;
/**
* @zh 设置当前四元数指定元素值。
* @param x 四元数 x 元素值
* @param y 四元数 y 元素值
* @param z 四元数 z 元素值
* @param w 四元数 w 元素值
* @returns `this`
*/
public set (x?: number, y?: number, z?: number, w?: number): Quat;
public set (x?: number | Quat, y?: number, z?: number, w?: number) {
if (x && typeof x === 'object') {
this.x = x.x;
this.y = x.y;
this.z = x.z;
this.w = x.w;
} else {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
this.w = w ?? 1;
}
return this;
}
/**
* @zh 判断当前向量是否在误差范围内与指定向量相等。
* @param other 相比较的向量。
* @param epsilon 允许的误差,应为非负数。
* @returns 当两向量的各分量都在指定的误差范围内分别相等时,返回 `true`;否则返回 `false`。
*/
public equals (other: Quat, epsilon = EPSILON) {
return (Math.abs(this.x - other.x) <= epsilon * Math.max(1.0, Math.abs(this.x), Math.abs(other.x)) &&
Math.abs(this.y - other.y) <= epsilon * Math.max(1.0, Math.abs(this.y), Math.abs(other.y)) &&
Math.abs(this.z - other.z) <= epsilon * Math.max(1.0, Math.abs(this.z), Math.abs(other.z)) &&
Math.abs(this.w - other.w) <= epsilon * Math.max(1.0, Math.abs(this.w), Math.abs(other.w)));
}
/**
* @zh 判断当前四元数是否与指定四元数相等。
* @param other 相比较的四元数。
* @returns 两四元数的各分量都相等时返回 `true`;否则返回 `false`。
*/
public strictEquals (other: Quat) {
return other && this.x === other.x && this.y === other.y && this.z === other.z && this.w === other.w;
}
/**
* @zh 将当前四元数转化为欧拉角(x-y-z)并赋值给出口向量。
* @param out 出口向量。
*/
public getEulerAngles (out: Vec3) {
return Quat.toEuler(out, this);
}
/**
* @zh 根据指定的插值比率,从当前四元数到目标四元数之间做线性插值。
* @param to 目标四元数。
* @param ratio 插值比率,范围为 [0,1]。
*/
public lerp (to: Quat, ratio: number) {
this.x = this.x + ratio * (to.x - this.x);
this.y = this.y + ratio * (to.y - this.y);
this.z = this.z + ratio * (to.z - this.z);
this.w = this.w + ratio * (to.w - this.w);
return this;
}
/**
* @zh 根据指定的插值比率,从当前四元数到目标四元数之间做球面插值。
* @param to 目标四元数。
* @param ratio 插值比率,范围为 [0,1]。
*/
public slerp (to: Quat, ratio: number) {
return Quat.slerp(this, this, to, ratio);
}
/**
* @zh 求四元数长度
*/
public length () {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
}
/**
* @zh 求四元数长度平方
*/
public lengthSqr () {
return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
}
}
const qt_1 = new Quat();
const qt_2 = new Quat();
const v3_1 = new Vec3();
const m3_1 = new Mat3();
const halfToRad = 0.5 * Math.PI / 180.0;
CCClass.fastDefine('cc.Quat', Quat, { x: 0, y: 0, z: 0, w: 1 });
legacyCC.Quat = Quat;
export function quat (other: Quat): Quat;
export function quat (x?: number, y?: number, z?: number, w?: number): Quat;
export function quat (x: number | Quat = 0, y: number = 0, z: number = 0, w: number = 1) {
return new Quat(x as any, y, z, w);
}
legacyCC.quat = quat;