-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Quaternion.js
1141 lines (1020 loc) · 34.9 KB
/
Quaternion.js
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Cartesian3 from "./Cartesian3.js";
import Check from "./Check.js";
import defaultValue from "./defaultValue.js";
import defined from "./defined.js";
import FeatureDetection from "./FeatureDetection.js";
import CesiumMath from "./Math.js";
import Matrix3 from "./Matrix3.js";
/**
* A set of 4-dimensional coordinates used to represent rotation in 3-dimensional space.
* @alias Quaternion
* @constructor
*
* @param {Number} [x=0.0] The X component.
* @param {Number} [y=0.0] The Y component.
* @param {Number} [z=0.0] The Z component.
* @param {Number} [w=0.0] The W component.
*
* @see PackableForInterpolation
*/
function Quaternion(x, y, z, w) {
/**
* The X component.
* @type {Number}
* @default 0.0
*/
this.x = defaultValue(x, 0.0);
/**
* The Y component.
* @type {Number}
* @default 0.0
*/
this.y = defaultValue(y, 0.0);
/**
* The Z component.
* @type {Number}
* @default 0.0
*/
this.z = defaultValue(z, 0.0);
/**
* The W component.
* @type {Number}
* @default 0.0
*/
this.w = defaultValue(w, 0.0);
}
let fromAxisAngleScratch = new Cartesian3();
/**
* Computes a quaternion representing a rotation around an axis.
*
* @param {Cartesian3} axis The axis of rotation.
* @param {Number} angle The angle in radians to rotate around the axis.
* @param {Quaternion} [result] The object onto which to store the result.
* @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
*/
Quaternion.fromAxisAngle = function (axis, angle, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("axis", axis);
Check.typeOf.number("angle", angle);
//>>includeEnd('debug');
const halfAngle = angle / 2.0;
const s = Math.sin(halfAngle);
fromAxisAngleScratch = Cartesian3.normalize(axis, fromAxisAngleScratch);
const x = fromAxisAngleScratch.x * s;
const y = fromAxisAngleScratch.y * s;
const z = fromAxisAngleScratch.z * s;
const w = Math.cos(halfAngle);
if (!defined(result)) {
return new Quaternion(x, y, z, w);
}
result.x = x;
result.y = y;
result.z = z;
result.w = w;
return result;
};
const fromRotationMatrixNext = [1, 2, 0];
const fromRotationMatrixQuat = new Array(3);
/**
* Computes a Quaternion from the provided Matrix3 instance.
*
* @param {Matrix3} matrix The rotation matrix.
* @param {Quaternion} [result] The object onto which to store the result.
* @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
*
* @see Matrix3.fromQuaternion
*/
Quaternion.fromRotationMatrix = function (matrix, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
//>>includeEnd('debug');
let root;
let x;
let y;
let z;
let w;
const m00 = matrix[Matrix3.COLUMN0ROW0];
const m11 = matrix[Matrix3.COLUMN1ROW1];
const m22 = matrix[Matrix3.COLUMN2ROW2];
const trace = m00 + m11 + m22;
if (trace > 0.0) {
// |w| > 1/2, may as well choose w > 1/2
root = Math.sqrt(trace + 1.0); // 2w
w = 0.5 * root;
root = 0.5 / root; // 1/(4w)
x = (matrix[Matrix3.COLUMN1ROW2] - matrix[Matrix3.COLUMN2ROW1]) * root;
y = (matrix[Matrix3.COLUMN2ROW0] - matrix[Matrix3.COLUMN0ROW2]) * root;
z = (matrix[Matrix3.COLUMN0ROW1] - matrix[Matrix3.COLUMN1ROW0]) * root;
} else {
// |w| <= 1/2
const next = fromRotationMatrixNext;
let i = 0;
if (m11 > m00) {
i = 1;
}
if (m22 > m00 && m22 > m11) {
i = 2;
}
const j = next[i];
const k = next[j];
root = Math.sqrt(
matrix[Matrix3.getElementIndex(i, i)] -
matrix[Matrix3.getElementIndex(j, j)] -
matrix[Matrix3.getElementIndex(k, k)] +
1.0
);
const quat = fromRotationMatrixQuat;
quat[i] = 0.5 * root;
root = 0.5 / root;
w =
(matrix[Matrix3.getElementIndex(k, j)] -
matrix[Matrix3.getElementIndex(j, k)]) *
root;
quat[j] =
(matrix[Matrix3.getElementIndex(j, i)] +
matrix[Matrix3.getElementIndex(i, j)]) *
root;
quat[k] =
(matrix[Matrix3.getElementIndex(k, i)] +
matrix[Matrix3.getElementIndex(i, k)]) *
root;
x = -quat[0];
y = -quat[1];
z = -quat[2];
}
if (!defined(result)) {
return new Quaternion(x, y, z, w);
}
result.x = x;
result.y = y;
result.z = z;
result.w = w;
return result;
};
const scratchHPRQuaternion = new Quaternion();
let scratchHeadingQuaternion = new Quaternion();
let scratchPitchQuaternion = new Quaternion();
let scratchRollQuaternion = new Quaternion();
/**
* Computes a rotation from the given heading, pitch and roll angles. Heading is the rotation about the
* negative z axis. Pitch is the rotation about the negative y axis. Roll is the rotation about
* the positive x axis.
*
* @param {HeadingPitchRoll} headingPitchRoll The rotation expressed as a heading, pitch and roll.
* @param {Quaternion} [result] The object onto which to store the result.
* @returns {Quaternion} The modified result parameter or a new Quaternion instance if none was provided.
*/
Quaternion.fromHeadingPitchRoll = function (headingPitchRoll, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("headingPitchRoll", headingPitchRoll);
//>>includeEnd('debug');
scratchRollQuaternion = Quaternion.fromAxisAngle(
Cartesian3.UNIT_X,
headingPitchRoll.roll,
scratchHPRQuaternion
);
scratchPitchQuaternion = Quaternion.fromAxisAngle(
Cartesian3.UNIT_Y,
-headingPitchRoll.pitch,
result
);
result = Quaternion.multiply(
scratchPitchQuaternion,
scratchRollQuaternion,
scratchPitchQuaternion
);
scratchHeadingQuaternion = Quaternion.fromAxisAngle(
Cartesian3.UNIT_Z,
-headingPitchRoll.heading,
scratchHPRQuaternion
);
return Quaternion.multiply(scratchHeadingQuaternion, result, result);
};
const sampledQuaternionAxis = new Cartesian3();
const sampledQuaternionRotation = new Cartesian3();
const sampledQuaternionTempQuaternion = new Quaternion();
const sampledQuaternionQuaternion0 = new Quaternion();
const sampledQuaternionQuaternion0Conjugate = new Quaternion();
/**
* The number of elements used to pack the object into an array.
* @type {Number}
*/
Quaternion.packedLength = 4;
/**
* Stores the provided instance into the provided array.
*
* @param {Quaternion} value The value to pack.
* @param {Number[]} array The array to pack into.
* @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
*
* @returns {Number[]} The array that was packed into
*/
Quaternion.pack = function (value, array, startingIndex) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("value", value);
Check.defined("array", array);
//>>includeEnd('debug');
startingIndex = defaultValue(startingIndex, 0);
array[startingIndex++] = value.x;
array[startingIndex++] = value.y;
array[startingIndex++] = value.z;
array[startingIndex] = value.w;
return array;
};
/**
* Retrieves an instance from a packed array.
*
* @param {Number[]} array The packed array.
* @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
* @param {Quaternion} [result] The object into which to store the result.
* @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
*/
Quaternion.unpack = function (array, startingIndex, result) {
//>>includeStart('debug', pragmas.debug);
Check.defined("array", array);
//>>includeEnd('debug');
startingIndex = defaultValue(startingIndex, 0);
if (!defined(result)) {
result = new Quaternion();
}
result.x = array[startingIndex];
result.y = array[startingIndex + 1];
result.z = array[startingIndex + 2];
result.w = array[startingIndex + 3];
return result;
};
/**
* The number of elements used to store the object into an array in its interpolatable form.
* @type {Number}
*/
Quaternion.packedInterpolationLength = 3;
/**
* Converts a packed array into a form suitable for interpolation.
*
* @param {Number[]} packedArray The packed array.
* @param {Number} [startingIndex=0] The index of the first element to be converted.
* @param {Number} [lastIndex=packedArray.length] The index of the last element to be converted.
* @param {Number[]} [result] The object into which to store the result.
*/
Quaternion.convertPackedArrayForInterpolation = function (
packedArray,
startingIndex,
lastIndex,
result
) {
Quaternion.unpack(
packedArray,
lastIndex * 4,
sampledQuaternionQuaternion0Conjugate
);
Quaternion.conjugate(
sampledQuaternionQuaternion0Conjugate,
sampledQuaternionQuaternion0Conjugate
);
for (let i = 0, len = lastIndex - startingIndex + 1; i < len; i++) {
const offset = i * 3;
Quaternion.unpack(
packedArray,
(startingIndex + i) * 4,
sampledQuaternionTempQuaternion
);
Quaternion.multiply(
sampledQuaternionTempQuaternion,
sampledQuaternionQuaternion0Conjugate,
sampledQuaternionTempQuaternion
);
if (sampledQuaternionTempQuaternion.w < 0) {
Quaternion.negate(
sampledQuaternionTempQuaternion,
sampledQuaternionTempQuaternion
);
}
Quaternion.computeAxis(
sampledQuaternionTempQuaternion,
sampledQuaternionAxis
);
const angle = Quaternion.computeAngle(sampledQuaternionTempQuaternion);
if (!defined(result)) {
result = [];
}
result[offset] = sampledQuaternionAxis.x * angle;
result[offset + 1] = sampledQuaternionAxis.y * angle;
result[offset + 2] = sampledQuaternionAxis.z * angle;
}
};
/**
* Retrieves an instance from a packed array converted with {@link convertPackedArrayForInterpolation}.
*
* @param {Number[]} array The array previously packed for interpolation.
* @param {Number[]} sourceArray The original packed array.
* @param {Number} [firstIndex=0] The firstIndex used to convert the array.
* @param {Number} [lastIndex=packedArray.length] The lastIndex used to convert the array.
* @param {Quaternion} [result] The object into which to store the result.
* @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
*/
Quaternion.unpackInterpolationResult = function (
array,
sourceArray,
firstIndex,
lastIndex,
result
) {
if (!defined(result)) {
result = new Quaternion();
}
Cartesian3.fromArray(array, 0, sampledQuaternionRotation);
const magnitude = Cartesian3.magnitude(sampledQuaternionRotation);
Quaternion.unpack(sourceArray, lastIndex * 4, sampledQuaternionQuaternion0);
if (magnitude === 0) {
Quaternion.clone(Quaternion.IDENTITY, sampledQuaternionTempQuaternion);
} else {
Quaternion.fromAxisAngle(
sampledQuaternionRotation,
magnitude,
sampledQuaternionTempQuaternion
);
}
return Quaternion.multiply(
sampledQuaternionTempQuaternion,
sampledQuaternionQuaternion0,
result
);
};
/**
* Duplicates a Quaternion instance.
*
* @param {Quaternion} quaternion The quaternion to duplicate.
* @param {Quaternion} [result] The object onto which to store the result.
* @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided. (Returns undefined if quaternion is undefined)
*/
Quaternion.clone = function (quaternion, result) {
if (!defined(quaternion)) {
return undefined;
}
if (!defined(result)) {
return new Quaternion(
quaternion.x,
quaternion.y,
quaternion.z,
quaternion.w
);
}
result.x = quaternion.x;
result.y = quaternion.y;
result.z = quaternion.z;
result.w = quaternion.w;
return result;
};
/**
* Computes the conjugate of the provided quaternion.
*
* @param {Quaternion} quaternion The quaternion to conjugate.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*/
Quaternion.conjugate = function (quaternion, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("quaternion", quaternion);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result.x = -quaternion.x;
result.y = -quaternion.y;
result.z = -quaternion.z;
result.w = quaternion.w;
return result;
};
/**
* Computes magnitude squared for the provided quaternion.
*
* @param {Quaternion} quaternion The quaternion to conjugate.
* @returns {Number} The magnitude squared.
*/
Quaternion.magnitudeSquared = function (quaternion) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("quaternion", quaternion);
//>>includeEnd('debug');
return (
quaternion.x * quaternion.x +
quaternion.y * quaternion.y +
quaternion.z * quaternion.z +
quaternion.w * quaternion.w
);
};
/**
* Computes magnitude for the provided quaternion.
*
* @param {Quaternion} quaternion The quaternion to conjugate.
* @returns {Number} The magnitude.
*/
Quaternion.magnitude = function (quaternion) {
return Math.sqrt(Quaternion.magnitudeSquared(quaternion));
};
/**
* Computes the normalized form of the provided quaternion.
*
* @param {Quaternion} quaternion The quaternion to normalize.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*/
Quaternion.normalize = function (quaternion, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const inverseMagnitude = 1.0 / Quaternion.magnitude(quaternion);
const x = quaternion.x * inverseMagnitude;
const y = quaternion.y * inverseMagnitude;
const z = quaternion.z * inverseMagnitude;
const w = quaternion.w * inverseMagnitude;
result.x = x;
result.y = y;
result.z = z;
result.w = w;
return result;
};
/**
* Computes the inverse of the provided quaternion.
*
* @param {Quaternion} quaternion The quaternion to normalize.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*/
Quaternion.inverse = function (quaternion, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const magnitudeSquared = Quaternion.magnitudeSquared(quaternion);
result = Quaternion.conjugate(quaternion, result);
return Quaternion.multiplyByScalar(result, 1.0 / magnitudeSquared, result);
};
/**
* Computes the componentwise sum of two quaternions.
*
* @param {Quaternion} left The first quaternion.
* @param {Quaternion} right The second quaternion.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*/
Quaternion.add = function (left, right, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("left", left);
Check.typeOf.object("right", right);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result.x = left.x + right.x;
result.y = left.y + right.y;
result.z = left.z + right.z;
result.w = left.w + right.w;
return result;
};
/**
* Computes the componentwise difference of two quaternions.
*
* @param {Quaternion} left The first quaternion.
* @param {Quaternion} right The second quaternion.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*/
Quaternion.subtract = function (left, right, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("left", left);
Check.typeOf.object("right", right);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result.x = left.x - right.x;
result.y = left.y - right.y;
result.z = left.z - right.z;
result.w = left.w - right.w;
return result;
};
/**
* Negates the provided quaternion.
*
* @param {Quaternion} quaternion The quaternion to be negated.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*/
Quaternion.negate = function (quaternion, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("quaternion", quaternion);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result.x = -quaternion.x;
result.y = -quaternion.y;
result.z = -quaternion.z;
result.w = -quaternion.w;
return result;
};
/**
* Computes the dot (scalar) product of two quaternions.
*
* @param {Quaternion} left The first quaternion.
* @param {Quaternion} right The second quaternion.
* @returns {Number} The dot product.
*/
Quaternion.dot = function (left, right) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("left", left);
Check.typeOf.object("right", right);
//>>includeEnd('debug');
return (
left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w
);
};
/**
* Computes the product of two quaternions.
*
* @param {Quaternion} left The first quaternion.
* @param {Quaternion} right The second quaternion.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*/
Quaternion.multiply = function (left, right, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("left", left);
Check.typeOf.object("right", right);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const leftX = left.x;
const leftY = left.y;
const leftZ = left.z;
const leftW = left.w;
const rightX = right.x;
const rightY = right.y;
const rightZ = right.z;
const rightW = right.w;
const x = leftW * rightX + leftX * rightW + leftY * rightZ - leftZ * rightY;
const y = leftW * rightY - leftX * rightZ + leftY * rightW + leftZ * rightX;
const z = leftW * rightZ + leftX * rightY - leftY * rightX + leftZ * rightW;
const w = leftW * rightW - leftX * rightX - leftY * rightY - leftZ * rightZ;
result.x = x;
result.y = y;
result.z = z;
result.w = w;
return result;
};
/**
* Multiplies the provided quaternion componentwise by the provided scalar.
*
* @param {Quaternion} quaternion The quaternion to be scaled.
* @param {Number} scalar The scalar to multiply with.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*/
Quaternion.multiplyByScalar = function (quaternion, scalar, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("quaternion", quaternion);
Check.typeOf.number("scalar", scalar);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result.x = quaternion.x * scalar;
result.y = quaternion.y * scalar;
result.z = quaternion.z * scalar;
result.w = quaternion.w * scalar;
return result;
};
/**
* Divides the provided quaternion componentwise by the provided scalar.
*
* @param {Quaternion} quaternion The quaternion to be divided.
* @param {Number} scalar The scalar to divide by.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*/
Quaternion.divideByScalar = function (quaternion, scalar, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("quaternion", quaternion);
Check.typeOf.number("scalar", scalar);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result.x = quaternion.x / scalar;
result.y = quaternion.y / scalar;
result.z = quaternion.z / scalar;
result.w = quaternion.w / scalar;
return result;
};
/**
* Computes the axis of rotation of the provided quaternion.
*
* @param {Quaternion} quaternion The quaternion to use.
* @param {Cartesian3} result The object onto which to store the result.
* @returns {Cartesian3} The modified result parameter.
*/
Quaternion.computeAxis = function (quaternion, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("quaternion", quaternion);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const w = quaternion.w;
if (Math.abs(w - 1.0) < CesiumMath.EPSILON6) {
result.x = result.y = result.z = 0;
return result;
}
const scalar = 1.0 / Math.sqrt(1.0 - w * w);
result.x = quaternion.x * scalar;
result.y = quaternion.y * scalar;
result.z = quaternion.z * scalar;
return result;
};
/**
* Computes the angle of rotation of the provided quaternion.
*
* @param {Quaternion} quaternion The quaternion to use.
* @returns {Number} The angle of rotation.
*/
Quaternion.computeAngle = function (quaternion) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("quaternion", quaternion);
//>>includeEnd('debug');
if (Math.abs(quaternion.w - 1.0) < CesiumMath.EPSILON6) {
return 0.0;
}
return 2.0 * Math.acos(quaternion.w);
};
let lerpScratch = new Quaternion();
/**
* Computes the linear interpolation or extrapolation at t using the provided quaternions.
*
* @param {Quaternion} start The value corresponding to t at 0.0.
* @param {Quaternion} end The value corresponding to t at 1.0.
* @param {Number} t The point along t at which to interpolate.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*/
Quaternion.lerp = function (start, end, t, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("start", start);
Check.typeOf.object("end", end);
Check.typeOf.number("t", t);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
lerpScratch = Quaternion.multiplyByScalar(end, t, lerpScratch);
result = Quaternion.multiplyByScalar(start, 1.0 - t, result);
return Quaternion.add(lerpScratch, result, result);
};
let slerpEndNegated = new Quaternion();
let slerpScaledP = new Quaternion();
let slerpScaledR = new Quaternion();
/**
* Computes the spherical linear interpolation or extrapolation at t using the provided quaternions.
*
* @param {Quaternion} start The value corresponding to t at 0.0.
* @param {Quaternion} end The value corresponding to t at 1.0.
* @param {Number} t The point along t at which to interpolate.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*
* @see Quaternion#fastSlerp
*/
Quaternion.slerp = function (start, end, t, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("start", start);
Check.typeOf.object("end", end);
Check.typeOf.number("t", t);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
let dot = Quaternion.dot(start, end);
// The angle between start must be acute. Since q and -q represent
// the same rotation, negate q to get the acute angle.
let r = end;
if (dot < 0.0) {
dot = -dot;
r = slerpEndNegated = Quaternion.negate(end, slerpEndNegated);
}
// dot > 0, as the dot product approaches 1, the angle between the
// quaternions vanishes. use linear interpolation.
if (1.0 - dot < CesiumMath.EPSILON6) {
return Quaternion.lerp(start, r, t, result);
}
const theta = Math.acos(dot);
slerpScaledP = Quaternion.multiplyByScalar(
start,
Math.sin((1 - t) * theta),
slerpScaledP
);
slerpScaledR = Quaternion.multiplyByScalar(
r,
Math.sin(t * theta),
slerpScaledR
);
result = Quaternion.add(slerpScaledP, slerpScaledR, result);
return Quaternion.multiplyByScalar(result, 1.0 / Math.sin(theta), result);
};
/**
* The logarithmic quaternion function.
*
* @param {Quaternion} quaternion The unit quaternion.
* @param {Cartesian3} result The object onto which to store the result.
* @returns {Cartesian3} The modified result parameter.
*/
Quaternion.log = function (quaternion, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("quaternion", quaternion);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const theta = CesiumMath.acosClamped(quaternion.w);
let thetaOverSinTheta = 0.0;
if (theta !== 0.0) {
thetaOverSinTheta = theta / Math.sin(theta);
}
return Cartesian3.multiplyByScalar(quaternion, thetaOverSinTheta, result);
};
/**
* The exponential quaternion function.
*
* @param {Cartesian3} cartesian The cartesian.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*/
Quaternion.exp = function (cartesian, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("cartesian", cartesian);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const theta = Cartesian3.magnitude(cartesian);
let sinThetaOverTheta = 0.0;
if (theta !== 0.0) {
sinThetaOverTheta = Math.sin(theta) / theta;
}
result.x = cartesian.x * sinThetaOverTheta;
result.y = cartesian.y * sinThetaOverTheta;
result.z = cartesian.z * sinThetaOverTheta;
result.w = Math.cos(theta);
return result;
};
const squadScratchCartesian0 = new Cartesian3();
const squadScratchCartesian1 = new Cartesian3();
const squadScratchQuaternion0 = new Quaternion();
const squadScratchQuaternion1 = new Quaternion();
/**
* Computes an inner quadrangle point.
* <p>This will compute quaternions that ensure a squad curve is C<sup>1</sup>.</p>
*
* @param {Quaternion} q0 The first quaternion.
* @param {Quaternion} q1 The second quaternion.
* @param {Quaternion} q2 The third quaternion.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*
* @see Quaternion#squad
*/
Quaternion.computeInnerQuadrangle = function (q0, q1, q2, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("q0", q0);
Check.typeOf.object("q1", q1);
Check.typeOf.object("q2", q2);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const qInv = Quaternion.conjugate(q1, squadScratchQuaternion0);
Quaternion.multiply(qInv, q2, squadScratchQuaternion1);
const cart0 = Quaternion.log(squadScratchQuaternion1, squadScratchCartesian0);
Quaternion.multiply(qInv, q0, squadScratchQuaternion1);
const cart1 = Quaternion.log(squadScratchQuaternion1, squadScratchCartesian1);
Cartesian3.add(cart0, cart1, cart0);
Cartesian3.multiplyByScalar(cart0, 0.25, cart0);
Cartesian3.negate(cart0, cart0);
Quaternion.exp(cart0, squadScratchQuaternion0);
return Quaternion.multiply(q1, squadScratchQuaternion0, result);
};
/**
* Computes the spherical quadrangle interpolation between quaternions.
*
* @param {Quaternion} q0 The first quaternion.
* @param {Quaternion} q1 The second quaternion.
* @param {Quaternion} s0 The first inner quadrangle.
* @param {Quaternion} s1 The second inner quadrangle.
* @param {Number} t The time in [0,1] used to interpolate.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*
*
* @example
* // 1. compute the squad interpolation between two quaternions on a curve
* const s0 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[i - 1], quaternions[i], quaternions[i + 1], new Cesium.Quaternion());
* const s1 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[i], quaternions[i + 1], quaternions[i + 2], new Cesium.Quaternion());
* const q = Cesium.Quaternion.squad(quaternions[i], quaternions[i + 1], s0, s1, t, new Cesium.Quaternion());
*
* // 2. compute the squad interpolation as above but where the first quaternion is a end point.
* const s1 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[0], quaternions[1], quaternions[2], new Cesium.Quaternion());
* const q = Cesium.Quaternion.squad(quaternions[0], quaternions[1], quaternions[0], s1, t, new Cesium.Quaternion());
*
* @see Quaternion#computeInnerQuadrangle
*/
Quaternion.squad = function (q0, q1, s0, s1, t, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("q0", q0);
Check.typeOf.object("q1", q1);
Check.typeOf.object("s0", s0);
Check.typeOf.object("s1", s1);
Check.typeOf.number("t", t);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const slerp0 = Quaternion.slerp(q0, q1, t, squadScratchQuaternion0);
const slerp1 = Quaternion.slerp(s0, s1, t, squadScratchQuaternion1);
return Quaternion.slerp(slerp0, slerp1, 2.0 * t * (1.0 - t), result);
};
const fastSlerpScratchQuaternion = new Quaternion();
const opmu = 1.90110745351730037;
const u = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
const v = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
const bT = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
const bD = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
for (let i = 0; i < 7; ++i) {
const s = i + 1.0;
const t = 2.0 * s + 1.0;
u[i] = 1.0 / (s * t);
v[i] = s / t;
}
u[7] = opmu / (8.0 * 17.0);
v[7] = (opmu * 8.0) / 17.0;
/**
* Computes the spherical linear interpolation or extrapolation at t using the provided quaternions.
* This implementation is faster than {@link Quaternion#slerp}, but is only accurate up to 10<sup>-6</sup>.
*
* @param {Quaternion} start The value corresponding to t at 0.0.
* @param {Quaternion} end The value corresponding to t at 1.0.
* @param {Number} t The point along t at which to interpolate.
* @param {Quaternion} result The object onto which to store the result.
* @returns {Quaternion} The modified result parameter.
*
* @see Quaternion#slerp
*/
Quaternion.fastSlerp = function (start, end, t, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("start", start);
Check.typeOf.object("end", end);
Check.typeOf.number("t", t);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
let x = Quaternion.dot(start, end);
let sign;
if (x >= 0) {
sign = 1.0;
} else {
sign = -1.0;
x = -x;
}
const xm1 = x - 1.0;
const d = 1.0 - t;
const sqrT = t * t;
const sqrD = d * d;
for (let i = 7; i >= 0; --i) {
bT[i] = (u[i] * sqrT - v[i]) * xm1;
bD[i] = (u[i] * sqrD - v[i]) * xm1;
}
const cT =
sign *
t *
(1.0 +
bT[0] *
(1.0 +
bT[1] *
(1.0 +
bT[2] *
(1.0 +
bT[3] *
(1.0 +
bT[4] *
(1.0 + bT[5] * (1.0 + bT[6] * (1.0 + bT[7]))))))));
const cD =
d *
(1.0 +
bD[0] *
(1.0 +
bD[1] *
(1.0 +
bD[2] *
(1.0 +
bD[3] *
(1.0 +
bD[4] *
(1.0 + bD[5] * (1.0 + bD[6] * (1.0 + bD[7]))))))));