summaryrefslogtreecommitdiff
path: root/indra/llmath/llquaternion.cpp
blob: aefb82b2f0dc69573e16655ab0029e2c915ff72e (plain)
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
/**
 * @file llquaternion.cpp
 * @brief LLQuaternion class implementation.
 *
 * $LicenseInfo:firstyear=2000&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"

#include "llmath.h" // for F_PI

#include "llquaternion.h"

//#include "vmath.h"
#include "v3math.h"
#include "v3dmath.h"
#include "v4math.h"
#include "m4math.h"
#include "m3math.h"
#include "llquantize.h"

// WARNING: Don't use this for global const definitions!  using this
// at the top of a *.cpp file might not give you what you think.
const LLQuaternion LLQuaternion::DEFAULT;

// Constructors

LLQuaternion::LLQuaternion(const LLMatrix4 &mat)
{
    *this = mat.quaternion();
    normalize();
}

LLQuaternion::LLQuaternion(const LLMatrix3 &mat)
{
    *this = mat.quaternion();
    normalize();
}

LLQuaternion::LLQuaternion(F32 angle, const LLVector4 &vec)
{
    F32 mag = sqrtf(vec.mV[VX] * vec.mV[VX] + vec.mV[VY] * vec.mV[VY] + vec.mV[VZ] * vec.mV[VZ]);
    if (mag > FP_MAG_THRESHOLD)
    {
        angle *= 0.5;
        F32 c = cosf(angle);
        F32 s = sinf(angle) / mag;
        mQ[VX] = vec.mV[VX] * s;
        mQ[VY] = vec.mV[VY] * s;
        mQ[VZ] = vec.mV[VZ] * s;
        mQ[VW] = c;
    }
    else
    {
        loadIdentity();
    }
}

LLQuaternion::LLQuaternion(F32 angle, const LLVector3 &vec)
{
    F32 mag = sqrtf(vec.mV[VX] * vec.mV[VX] + vec.mV[VY] * vec.mV[VY] + vec.mV[VZ] * vec.mV[VZ]);
    if (mag > FP_MAG_THRESHOLD)
    {
        angle *= 0.5;
        F32 c = cosf(angle);
        F32 s = sinf(angle) / mag;
        mQ[VX] = vec.mV[VX] * s;
        mQ[VY] = vec.mV[VY] * s;
        mQ[VZ] = vec.mV[VZ] * s;
        mQ[VW] = c;
    }
    else
    {
        loadIdentity();
    }
}

LLQuaternion::LLQuaternion(const LLVector3 &x_axis,
                           const LLVector3 &y_axis,
                           const LLVector3 &z_axis)
{
    LLMatrix3 mat;
    mat.setRows(x_axis, y_axis, z_axis);
    *this = mat.quaternion();
    normalize();
}

LLQuaternion::LLQuaternion(const LLSD &sd)
{
    setValue(sd);
}

// Quatizations
void    LLQuaternion::quantize16(F32 lower, F32 upper)
{
    F32 x = mQ[VX];
    F32 y = mQ[VY];
    F32 z = mQ[VZ];
    F32 s = mQ[VS];

    x = U16_to_F32(F32_to_U16_ROUND(x, lower, upper), lower, upper);
    y = U16_to_F32(F32_to_U16_ROUND(y, lower, upper), lower, upper);
    z = U16_to_F32(F32_to_U16_ROUND(z, lower, upper), lower, upper);
    s = U16_to_F32(F32_to_U16_ROUND(s, lower, upper), lower, upper);

    mQ[VX] = x;
    mQ[VY] = y;
    mQ[VZ] = z;
    mQ[VS] = s;

    normalize();
}

void    LLQuaternion::quantize8(F32 lower, F32 upper)
{
    mQ[VX] = U8_to_F32(F32_to_U8_ROUND(mQ[VX], lower, upper), lower, upper);
    mQ[VY] = U8_to_F32(F32_to_U8_ROUND(mQ[VY], lower, upper), lower, upper);
    mQ[VZ] = U8_to_F32(F32_to_U8_ROUND(mQ[VZ], lower, upper), lower, upper);
    mQ[VS] = U8_to_F32(F32_to_U8_ROUND(mQ[VS], lower, upper), lower, upper);

    normalize();
}

// LLVector3 Magnitude and Normalization Functions


// Set LLQuaternion routines

const LLQuaternion& LLQuaternion::setAngleAxis(F32 angle, F32 x, F32 y, F32 z)
{
    F32 mag = sqrtf(x * x + y * y + z * z);
    if (mag > FP_MAG_THRESHOLD)
    {
        angle *= 0.5;
        F32 c = cosf(angle);
        F32 s = sinf(angle) / mag;
        mQ[VX] = x * s;
        mQ[VY] = y * s;
        mQ[VZ] = z * s;
        mQ[VW] = c;
    }
    else
    {
        loadIdentity();
    }
    return (*this);
}

const LLQuaternion& LLQuaternion::setAngleAxis(F32 angle, const LLVector3 &vec)
{
    F32 mag = sqrtf(vec.mV[VX] * vec.mV[VX] + vec.mV[VY] * vec.mV[VY] + vec.mV[VZ] * vec.mV[VZ]);
    if (mag > FP_MAG_THRESHOLD)
    {
        angle *= 0.5;
        F32 c = cosf(angle);
        F32 s = sinf(angle) / mag;
        mQ[VX] = vec.mV[VX] * s;
        mQ[VY] = vec.mV[VY] * s;
        mQ[VZ] = vec.mV[VZ] * s;
        mQ[VW] = c;
    }
    else
    {
        loadIdentity();
    }
    return (*this);
}

const LLQuaternion& LLQuaternion::setAngleAxis(F32 angle, const LLVector4 &vec)
{
    F32 mag = sqrtf(vec.mV[VX] * vec.mV[VX] + vec.mV[VY] * vec.mV[VY] + vec.mV[VZ] * vec.mV[VZ]);
    if (mag > FP_MAG_THRESHOLD)
    {
        angle *= 0.5;
        F32 c = cosf(angle);
        F32 s = sinf(angle) / mag;
        mQ[VX] = vec.mV[VX] * s;
        mQ[VY] = vec.mV[VY] * s;
        mQ[VZ] = vec.mV[VZ] * s;
        mQ[VW] = c;
    }
    else
    {
        loadIdentity();
    }
    return (*this);
}

const LLQuaternion& LLQuaternion::setEulerAngles(F32 roll, F32 pitch, F32 yaw)
{
    LLMatrix3 rot_mat(roll, pitch, yaw);
    rot_mat.orthogonalize();
    *this = rot_mat.quaternion();

    normalize();
    return (*this);
}

// deprecated
const LLQuaternion& LLQuaternion::set(const LLMatrix3 &mat)
{
    *this = mat.quaternion();
    normalize();
    return (*this);
}

// deprecated
const LLQuaternion& LLQuaternion::set(const LLMatrix4 &mat)
{
    *this = mat.quaternion();
    normalize();
    return (*this);
}

// deprecated
const LLQuaternion& LLQuaternion::setQuat(F32 angle, F32 x, F32 y, F32 z)
{
    F32 mag = sqrtf(x * x + y * y + z * z);
    if (mag > FP_MAG_THRESHOLD)
    {
        angle *= 0.5;
        F32 c = cosf(angle);
        F32 s = sinf(angle) / mag;
        mQ[VX] = x * s;
        mQ[VY] = y * s;
        mQ[VZ] = z * s;
        mQ[VW] = c;
    }
    else
    {
        loadIdentity();
    }
    return (*this);
}

// deprecated
const LLQuaternion& LLQuaternion::setQuat(F32 angle, const LLVector3 &vec)
{
    F32 mag = sqrtf(vec.mV[VX] * vec.mV[VX] + vec.mV[VY] * vec.mV[VY] + vec.mV[VZ] * vec.mV[VZ]);
    if (mag > FP_MAG_THRESHOLD)
    {
        angle *= 0.5;
        F32 c = cosf(angle);
        F32 s = sinf(angle) / mag;
        mQ[VX] = vec.mV[VX] * s;
        mQ[VY] = vec.mV[VY] * s;
        mQ[VZ] = vec.mV[VZ] * s;
        mQ[VW] = c;
    }
    else
    {
        loadIdentity();
    }
    return (*this);
}

const LLQuaternion& LLQuaternion::setQuat(F32 angle, const LLVector4 &vec)
{
    F32 mag = sqrtf(vec.mV[VX] * vec.mV[VX] + vec.mV[VY] * vec.mV[VY] + vec.mV[VZ] * vec.mV[VZ]);
    if (mag > FP_MAG_THRESHOLD)
    {
        angle *= 0.5;
        F32 c = cosf(angle);
        F32 s = sinf(angle) / mag;
        mQ[VX] = vec.mV[VX] * s;
        mQ[VY] = vec.mV[VY] * s;
        mQ[VZ] = vec.mV[VZ] * s;
        mQ[VW] = c;
    }
    else
    {
        loadIdentity();
    }
    return (*this);
}

const LLQuaternion& LLQuaternion::setQuat(F32 roll, F32 pitch, F32 yaw)
{
    roll  *= 0.5f;
    pitch *= 0.5f;
    yaw   *= 0.5f;
    F32 sinX = sinf(roll);
    F32 cosX = cosf(roll);
    F32 sinY = sinf(pitch);
    F32 cosY = cosf(pitch);
    F32 sinZ = sinf(yaw);
    F32 cosZ = cosf(yaw);
    mQ[VW] = cosX * cosY * cosZ - sinX * sinY * sinZ;
    mQ[VX] = sinX * cosY * cosZ + cosX * sinY * sinZ;
    mQ[VY] = cosX * sinY * cosZ - sinX * cosY * sinZ;
    mQ[VZ] = cosX * cosY * sinZ + sinX * sinY * cosZ;
    return (*this);
}

const LLQuaternion& LLQuaternion::setQuat(const LLMatrix3 &mat)
{
    *this = mat.quaternion();
    normalize();
    return (*this);
}

const LLQuaternion& LLQuaternion::setQuat(const LLMatrix4 &mat)
{
    *this = mat.quaternion();
    normalize();
    return (*this);
//#if 1
//  // NOTE: LLQuaternion's are actually inverted with respect to
//  // the matrices, so this code also assumes inverted quaternions
//  // (-x, -y, -z, w). The result is that roll,pitch,yaw are applied
//  // in reverse order (yaw,pitch,roll).
//  F64 cosX = cos(roll);
//    F64 cosY = cos(pitch);
//    F64 cosZ = cos(yaw);
//
//    F64 sinX = sin(roll);
//    F64 sinY = sin(pitch);
//    F64 sinZ = sin(yaw);
//
//    mQ[VW] = (F32)sqrt(cosY*cosZ - sinX*sinY*sinZ + cosX*cosZ + cosX*cosY + 1.0)*.5;
//  if (fabs(mQ[VW]) < F_APPROXIMATELY_ZERO)
//  {
//      // null rotation, any axis will do
//      mQ[VX] = 0.0f;
//      mQ[VY] = 1.0f;
//      mQ[VZ] = 0.0f;
//  }
//  else
//  {
//      F32 inv_s = 1.0f / (4.0f * mQ[VW]);
//      mQ[VX] = (F32)-(-sinX*cosY - cosX*sinY*sinZ - sinX*cosZ) * inv_s;
//      mQ[VY] = (F32)-(-cosX*sinY*cosZ + sinX*sinZ - sinY) * inv_s;
//      mQ[VZ] = (F32)-(-cosY*sinZ - sinX*sinY*cosZ - cosX*sinZ) * inv_s;
//  }
//
//#else // This only works on a certain subset of roll/pitch/yaw
//
//  F64 cosX = cosf(roll/2.0);
//    F64 cosY = cosf(pitch/2.0);
//    F64 cosZ = cosf(yaw/2.0);
//
//    F64 sinX = sinf(roll/2.0);
//    F64 sinY = sinf(pitch/2.0);
//    F64 sinZ = sinf(yaw/2.0);
//
//    mQ[VW] = (F32)(cosX*cosY*cosZ + sinX*sinY*sinZ);
//    mQ[VX] = (F32)(sinX*cosY*cosZ - cosX*sinY*sinZ);
//    mQ[VY] = (F32)(cosX*sinY*cosZ + sinX*cosY*sinZ);
//    mQ[VZ] = (F32)(cosX*cosY*sinZ - sinX*sinY*cosZ);
//#endif
//
//  normalize();
//  return (*this);
}

// SJB: This code is correct for a logicly stored (non-transposed) matrix;
//      Our matrices are stored transposed, OpenGL style, so this generates the
//      INVERSE matrix, or the CORRECT matrix form an INVERSE quaternion.
//      Because we use similar logic in LLMatrix3::quaternion(),
//      we are internally consistant so everything works OK :)
LLMatrix3   LLQuaternion::getMatrix3(void) const
{
    LLMatrix3   mat;
    F32     xx, xy, xz, xw, yy, yz, yw, zz, zw;

    xx      = mQ[VX] * mQ[VX];
    xy      = mQ[VX] * mQ[VY];
    xz      = mQ[VX] * mQ[VZ];
    xw      = mQ[VX] * mQ[VW];

    yy      = mQ[VY] * mQ[VY];
    yz      = mQ[VY] * mQ[VZ];
    yw      = mQ[VY] * mQ[VW];

    zz      = mQ[VZ] * mQ[VZ];
    zw      = mQ[VZ] * mQ[VW];

    mat.mMatrix[0][0]  = 1.f - 2.f * ( yy + zz );
    mat.mMatrix[0][1]  =       2.f * ( xy + zw );
    mat.mMatrix[0][2]  =       2.f * ( xz - yw );

    mat.mMatrix[1][0]  =       2.f * ( xy - zw );
    mat.mMatrix[1][1]  = 1.f - 2.f * ( xx + zz );
    mat.mMatrix[1][2]  =       2.f * ( yz + xw );

    mat.mMatrix[2][0]  =       2.f * ( xz + yw );
    mat.mMatrix[2][1]  =       2.f * ( yz - xw );
    mat.mMatrix[2][2]  = 1.f - 2.f * ( xx + yy );

    return mat;
}

LLMatrix4   LLQuaternion::getMatrix4(void) const
{
    LLMatrix4   mat;
    F32     xx, xy, xz, xw, yy, yz, yw, zz, zw;

    xx      = mQ[VX] * mQ[VX];
    xy      = mQ[VX] * mQ[VY];
    xz      = mQ[VX] * mQ[VZ];
    xw      = mQ[VX] * mQ[VW];

    yy      = mQ[VY] * mQ[VY];
    yz      = mQ[VY] * mQ[VZ];
    yw      = mQ[VY] * mQ[VW];

    zz      = mQ[VZ] * mQ[VZ];
    zw      = mQ[VZ] * mQ[VW];

    mat.mMatrix[0][0]  = 1.f - 2.f * ( yy + zz );
    mat.mMatrix[0][1]  =       2.f * ( xy + zw );
    mat.mMatrix[0][2]  =       2.f * ( xz - yw );

    mat.mMatrix[1][0]  =       2.f * ( xy - zw );
    mat.mMatrix[1][1]  = 1.f - 2.f * ( xx + zz );
    mat.mMatrix[1][2]  =       2.f * ( yz + xw );

    mat.mMatrix[2][0]  =       2.f * ( xz + yw );
    mat.mMatrix[2][1]  =       2.f * ( yz - xw );
    mat.mMatrix[2][2]  = 1.f - 2.f * ( xx + yy );

    // TODO -- should we set the translation portion to zero?

    return mat;
}




// Other useful methods


// calculate the shortest rotation from a to b
void LLQuaternion::shortestArc(const LLVector3 &a, const LLVector3 &b)
{
    F32 ab = a * b; // dotproduct
    LLVector3 c = a % b; // crossproduct
    F32 cc = c * c; // squared length of the crossproduct
    if (ab * ab + cc) // test if the arguments have sufficient magnitude
    {
        if (cc > 0.0f) // test if the arguments are (anti)parallel
        {
            F32 s = sqrtf(ab * ab + cc) + ab; // note: don't try to optimize this line
            F32 m = 1.0f / sqrtf(cc + s * s); // the inverted magnitude of the quaternion
            mQ[VX] = c.mV[VX] * m;
            mQ[VY] = c.mV[VY] * m;
            mQ[VZ] = c.mV[VZ] * m;
            mQ[VW] = s * m;
            return;
        }
        if (ab < 0.0f) // test if the angle is bigger than PI/2 (anti parallel)
        {
            c = a - b; // the arguments are anti-parallel, we have to choose an axis
            F32 m = sqrtf(c.mV[VX] * c.mV[VX] +  c.mV[VY] * c.mV[VY]); // the length projected on the XY-plane
            if (m > FP_MAG_THRESHOLD)
            {
                mQ[VX] = -c.mV[VY] / m; // return the quaternion with the axis in the XY-plane
                mQ[VY] =  c.mV[VX] / m;
                mQ[VZ] = 0.0f;
                mQ[VW] = 0.0f;
                return;
            }
            else // the vectors are parallel to the Z-axis
            {
                mQ[VX] = 1.0f; // rotate around the X-axis
                mQ[VY] = 0.0f;
                mQ[VZ] = 0.0f;
                mQ[VW] = 0.0f;
                return;
            }
        }
    }
    loadIdentity();
}

// constrains rotation to a cone angle specified in radians
const LLQuaternion &LLQuaternion::constrain(F32 radians)
{
    const F32 cos_angle_lim = cosf( radians/2 );    // mQ[VW] limit
    const F32 sin_angle_lim = sinf( radians/2 );    // rotation axis length limit

    if (mQ[VW] < 0.f)
    {
        mQ[VX] *= -1.f;
        mQ[VY] *= -1.f;
        mQ[VZ] *= -1.f;
        mQ[VW] *= -1.f;
    }

    // if rotation angle is greater than limit (cos is less than limit)
    if( mQ[VW] < cos_angle_lim )
    {
        mQ[VW] = cos_angle_lim;
        F32 axis_len = sqrtf( mQ[VX]*mQ[VX] + mQ[VY]*mQ[VY] + mQ[VZ]*mQ[VZ] ); // sin(theta/2)
        F32 axis_mult_fact = sin_angle_lim / axis_len;
        mQ[VX] *= axis_mult_fact;
        mQ[VY] *= axis_mult_fact;
        mQ[VZ] *= axis_mult_fact;
    }

    return *this;
}

// Operators

std::ostream& operator<<(std::ostream &s, const LLQuaternion &a)
{
    s << "{ "
        << a.mQ[VX] << ", " << a.mQ[VY] << ", " << a.mQ[VZ] << ", " << a.mQ[VW]
    << " }";
    return s;
}


// Does NOT renormalize the result
LLQuaternion    operator*(const LLQuaternion &a, const LLQuaternion &b)
{
//  LLQuaternion::mMultCount++;

    LLQuaternion q(
        b.mQ[3] * a.mQ[0] + b.mQ[0] * a.mQ[3] + b.mQ[1] * a.mQ[2] - b.mQ[2] * a.mQ[1],
        b.mQ[3] * a.mQ[1] + b.mQ[1] * a.mQ[3] + b.mQ[2] * a.mQ[0] - b.mQ[0] * a.mQ[2],
        b.mQ[3] * a.mQ[2] + b.mQ[2] * a.mQ[3] + b.mQ[0] * a.mQ[1] - b.mQ[1] * a.mQ[0],
        b.mQ[3] * a.mQ[3] - b.mQ[0] * a.mQ[0] - b.mQ[1] * a.mQ[1] - b.mQ[2] * a.mQ[2]
    );
    return q;
}

/*
LLMatrix4   operator*(const LLMatrix4 &m, const LLQuaternion &q)
{
    LLMatrix4 qmat(q);
    return (m*qmat);
}
*/



LLVector4       operator*(const LLVector4 &a, const LLQuaternion &rot)
{
    F32 rw = - rot.mQ[VX] * a.mV[VX] - rot.mQ[VY] * a.mV[VY] - rot.mQ[VZ] * a.mV[VZ];
    F32 rx =   rot.mQ[VW] * a.mV[VX] + rot.mQ[VY] * a.mV[VZ] - rot.mQ[VZ] * a.mV[VY];
    F32 ry =   rot.mQ[VW] * a.mV[VY] + rot.mQ[VZ] * a.mV[VX] - rot.mQ[VX] * a.mV[VZ];
    F32 rz =   rot.mQ[VW] * a.mV[VZ] + rot.mQ[VX] * a.mV[VY] - rot.mQ[VY] * a.mV[VX];

    F32 nx = - rw * rot.mQ[VX] +  rx * rot.mQ[VW] - ry * rot.mQ[VZ] + rz * rot.mQ[VY];
    F32 ny = - rw * rot.mQ[VY] +  ry * rot.mQ[VW] - rz * rot.mQ[VX] + rx * rot.mQ[VZ];
    F32 nz = - rw * rot.mQ[VZ] +  rz * rot.mQ[VW] - rx * rot.mQ[VY] + ry * rot.mQ[VX];

    return LLVector4(nx, ny, nz, a.mV[VW]);
}

LLVector3       operator*(const LLVector3 &a, const LLQuaternion &rot)
{
    F32 rw = - rot.mQ[VX] * a.mV[VX] - rot.mQ[VY] * a.mV[VY] - rot.mQ[VZ] * a.mV[VZ];
    F32 rx =   rot.mQ[VW] * a.mV[VX] + rot.mQ[VY] * a.mV[VZ] - rot.mQ[VZ] * a.mV[VY];
    F32 ry =   rot.mQ[VW] * a.mV[VY] + rot.mQ[VZ] * a.mV[VX] - rot.mQ[VX] * a.mV[VZ];
    F32 rz =   rot.mQ[VW] * a.mV[VZ] + rot.mQ[VX] * a.mV[VY] - rot.mQ[VY] * a.mV[VX];

    F32 nx = - rw * rot.mQ[VX] +  rx * rot.mQ[VW] - ry * rot.mQ[VZ] + rz * rot.mQ[VY];
    F32 ny = - rw * rot.mQ[VY] +  ry * rot.mQ[VW] - rz * rot.mQ[VX] + rx * rot.mQ[VZ];
    F32 nz = - rw * rot.mQ[VZ] +  rz * rot.mQ[VW] - rx * rot.mQ[VY] + ry * rot.mQ[VX];

    return LLVector3(nx, ny, nz);
}

LLVector3d      operator*(const LLVector3d &a, const LLQuaternion &rot)
{
    F64 rw = - rot.mQ[VX] * a.mdV[VX] - rot.mQ[VY] * a.mdV[VY] - rot.mQ[VZ] * a.mdV[VZ];
    F64 rx =   rot.mQ[VW] * a.mdV[VX] + rot.mQ[VY] * a.mdV[VZ] - rot.mQ[VZ] * a.mdV[VY];
    F64 ry =   rot.mQ[VW] * a.mdV[VY] + rot.mQ[VZ] * a.mdV[VX] - rot.mQ[VX] * a.mdV[VZ];
    F64 rz =   rot.mQ[VW] * a.mdV[VZ] + rot.mQ[VX] * a.mdV[VY] - rot.mQ[VY] * a.mdV[VX];

    F64 nx = - rw * rot.mQ[VX] +  rx * rot.mQ[VW] - ry * rot.mQ[VZ] + rz * rot.mQ[VY];
    F64 ny = - rw * rot.mQ[VY] +  ry * rot.mQ[VW] - rz * rot.mQ[VX] + rx * rot.mQ[VZ];
    F64 nz = - rw * rot.mQ[VZ] +  rz * rot.mQ[VW] - rx * rot.mQ[VY] + ry * rot.mQ[VX];

    return LLVector3d(nx, ny, nz);
}

F32 dot(const LLQuaternion &a, const LLQuaternion &b)
{
    return a.mQ[VX] * b.mQ[VX] +
           a.mQ[VY] * b.mQ[VY] +
           a.mQ[VZ] * b.mQ[VZ] +
           a.mQ[VW] * b.mQ[VW];
}

// DEMO HACK: This lerp is probably inocrrect now due intermediate normalization
// it should look more like the lerp below
#if 0
// linear interpolation
LLQuaternion lerp(F32 t, const LLQuaternion &p, const LLQuaternion &q)
{
    LLQuaternion r;
    r = t * (q - p) + p;
    r.normalize();
    return r;
}
#endif

// lerp from identity to q
LLQuaternion lerp(F32 t, const LLQuaternion &q)
{
    LLQuaternion r;
    r.mQ[VX] = t * q.mQ[VX];
    r.mQ[VY] = t * q.mQ[VY];
    r.mQ[VZ] = t * q.mQ[VZ];
    r.mQ[VW] = t * (q.mQ[VZ] - 1.f) + 1.f;
    r.normalize();
    return r;
}

LLQuaternion lerp(F32 t, const LLQuaternion &p, const LLQuaternion &q)
{
    LLQuaternion r;
    F32 inv_t;

    inv_t = 1.f - t;

    r.mQ[VX] = t * q.mQ[VX] + (inv_t * p.mQ[VX]);
    r.mQ[VY] = t * q.mQ[VY] + (inv_t * p.mQ[VY]);
    r.mQ[VZ] = t * q.mQ[VZ] + (inv_t * p.mQ[VZ]);
    r.mQ[VW] = t * q.mQ[VW] + (inv_t * p.mQ[VW]);
    r.normalize();
    return r;
}


// spherical linear interpolation
LLQuaternion slerp( F32 u, const LLQuaternion &a, const LLQuaternion &b )
{
    // cosine theta = dot product of a and b
    F32 cos_t = a.mQ[0]*b.mQ[0] + a.mQ[1]*b.mQ[1] + a.mQ[2]*b.mQ[2] + a.mQ[3]*b.mQ[3];

    // if b is on opposite hemisphere from a, use -a instead
    bool bflip;
    if (cos_t < 0.0f)
    {
        cos_t = -cos_t;
        bflip = true;
    }
    else
        bflip = false;

    // if B is (within precision limits) the same as A,
    // just linear interpolate between A and B.
    F32 alpha;  // interpolant
    F32 beta;       // 1 - interpolant
    if (1.0f - cos_t < 0.00001f)
    {
        beta = 1.0f - u;
        alpha = u;
    }
    else
    {
        F32 theta = acosf(cos_t);
        F32 sin_t = sinf(theta);
        beta = sinf(theta - u*theta) / sin_t;
        alpha = sinf(u*theta) / sin_t;
    }

    if (bflip)
        beta = -beta;

    // interpolate
    LLQuaternion ret;
    ret.mQ[0] = beta*a.mQ[0] + alpha*b.mQ[0];
    ret.mQ[1] = beta*a.mQ[1] + alpha*b.mQ[1];
    ret.mQ[2] = beta*a.mQ[2] + alpha*b.mQ[2];
    ret.mQ[3] = beta*a.mQ[3] + alpha*b.mQ[3];

    return ret;
}

// lerp whenever possible
LLQuaternion nlerp(F32 t, const LLQuaternion &a, const LLQuaternion &b)
{
    if (dot(a, b) < 0.f)
    {
        return slerp(t, a, b);
    }
    else
    {
        return lerp(t, a, b);
    }
}

LLQuaternion nlerp(F32 t, const LLQuaternion &q)
{
    if (q.mQ[VW] < 0.f)
    {
        return slerp(t, q);
    }
    else
    {
        return lerp(t, q);
    }
}

// slerp from identity quaternion to another quaternion
LLQuaternion slerp(F32 t, const LLQuaternion &q)
{
    F32 c = q.mQ[VW];
    if (1.0f == t  ||  1.0f == c)
    {
        // the trivial cases
        return q;
    }

    LLQuaternion r;
    F32 s, angle, stq, stp;

    s = (F32) sqrt(1.f - c*c);

    if (c < 0.0f)
    {
        // when c < 0.0 then theta > PI/2
        // since quat and -quat are the same rotation we invert one of
        // p or q to reduce unecessary spins
        // A equivalent way to do it is to convert acos(c) as if it had
        // been negative, and to negate stp
        angle   = (F32) acos(-c);
        stp     = -(F32) sin(angle * (1.f - t));
        stq     = (F32) sin(angle * t);
    }
    else
    {
        angle   = (F32) acos(c);
        stp     = (F32) sin(angle * (1.f - t));
        stq     = (F32) sin(angle * t);
    }

    r.mQ[VX] = (q.mQ[VX] * stq) / s;
    r.mQ[VY] = (q.mQ[VY] * stq) / s;
    r.mQ[VZ] = (q.mQ[VZ] * stq) / s;
    r.mQ[VW] = (stp + q.mQ[VW] * stq) / s;

    return r;
}

LLQuaternion mayaQ(F32 xRot, F32 yRot, F32 zRot, LLQuaternion::Order order)
{
    LLQuaternion xQ( xRot*DEG_TO_RAD, LLVector3(1.0f, 0.0f, 0.0f) );
    LLQuaternion yQ( yRot*DEG_TO_RAD, LLVector3(0.0f, 1.0f, 0.0f) );
    LLQuaternion zQ( zRot*DEG_TO_RAD, LLVector3(0.0f, 0.0f, 1.0f) );
    LLQuaternion ret;
    switch( order )
    {
    case LLQuaternion::XYZ:
        ret = xQ * yQ * zQ;
        break;
    case LLQuaternion::YZX:
        ret = yQ * zQ * xQ;
        break;
    case LLQuaternion::ZXY:
        ret = zQ * xQ * yQ;
        break;
    case LLQuaternion::XZY:
        ret = xQ * zQ * yQ;
        break;
    case LLQuaternion::YXZ:
        ret = yQ * xQ * zQ;
        break;
    case LLQuaternion::ZYX:
        ret = zQ * yQ * xQ;
        break;
    }
    return ret;
}

const char *OrderToString( const LLQuaternion::Order order )
{
    const char *p = NULL;
    switch( order )
    {
    default:
    case LLQuaternion::XYZ:
        p = "XYZ";
        break;
    case LLQuaternion::YZX:
        p = "YZX";
        break;
    case LLQuaternion::ZXY:
        p = "ZXY";
        break;
    case LLQuaternion::XZY:
        p = "XZY";
        break;
    case LLQuaternion::YXZ:
        p = "YXZ";
        break;
    case LLQuaternion::ZYX:
        p = "ZYX";
        break;
    }
    return p;
}

LLQuaternion::Order StringToOrder( const char *str )
{
    if (strncmp(str, "XYZ", 3)==0 || strncmp(str, "xyz", 3)==0)
        return LLQuaternion::XYZ;

    if (strncmp(str, "YZX", 3)==0 || strncmp(str, "yzx", 3)==0)
        return LLQuaternion::YZX;

    if (strncmp(str, "ZXY", 3)==0 || strncmp(str, "zxy", 3)==0)
        return LLQuaternion::ZXY;

    if (strncmp(str, "XZY", 3)==0 || strncmp(str, "xzy", 3)==0)
        return LLQuaternion::XZY;

    if (strncmp(str, "YXZ", 3)==0 || strncmp(str, "yxz", 3)==0)
        return LLQuaternion::YXZ;

    if (strncmp(str, "ZYX", 3)==0 || strncmp(str, "zyx", 3)==0)
        return LLQuaternion::ZYX;

    return LLQuaternion::XYZ;
}

void LLQuaternion::getAngleAxis(F32* angle, LLVector3 &vec) const
{
    F32 v = sqrtf(mQ[VX] * mQ[VX] + mQ[VY] * mQ[VY] + mQ[VZ] * mQ[VZ]); // length of the vector-component
    if (v > FP_MAG_THRESHOLD)
    {
        F32 oomag = 1.0f / v;
        F32 w = mQ[VW];
        if (mQ[VW] < 0.0f)
        {
            w = -w; // make VW positive
            oomag = -oomag; // invert the axis
        }
        vec.mV[VX] = mQ[VX] * oomag; // normalize the axis
        vec.mV[VY] = mQ[VY] * oomag;
        vec.mV[VZ] = mQ[VZ] * oomag;
        *angle = 2.0f * atan2f(v, w); // get the angle
    }
    else
    {
        *angle = 0.0f; // no rotation
        vec.mV[VX] = 0.0f; // around some dummy axis
        vec.mV[VY] = 0.0f;
        vec.mV[VZ] = 1.0f;
    }
}

const LLQuaternion& LLQuaternion::setFromAzimuthAndAltitude(F32 azimuthRadians, F32 altitudeRadians)
{
    // euler angle inputs are complements of azimuth/altitude which are measured from zenith
    F32 pitch = llclamp(F_PI_BY_TWO - altitudeRadians, 0.0f, F_PI_BY_TWO);
    F32 yaw   = llclamp(F_PI_BY_TWO - azimuthRadians,  0.0f, F_PI_BY_TWO);
    setEulerAngles(0.0f, pitch, yaw);
    return *this;
}

void LLQuaternion::getAzimuthAndAltitude(F32 &azimuthRadians, F32 &altitudeRadians)
{
    F32 rick_roll;
    F32 pitch;
    F32 yaw;
    getEulerAngles(&rick_roll, &pitch, &yaw);
    // make these measured from zenith
    altitudeRadians = llclamp(F_PI_BY_TWO - pitch, 0.0f, F_PI_BY_TWO);
    azimuthRadians  = llclamp(F_PI_BY_TWO - yaw,   0.0f, F_PI_BY_TWO);
}

// quaternion does not need to be normalized
void LLQuaternion::getEulerAngles(F32 *roll, F32 *pitch, F32 *yaw) const
{
    F32 sx = 2 * (mQ[VX] * mQ[VW] - mQ[VY] * mQ[VZ]); // sine of the roll
    F32 sy = 2 * (mQ[VY] * mQ[VW] + mQ[VX] * mQ[VZ]); // sine of the pitch
    F32 ys = mQ[VW] * mQ[VW] - mQ[VY] * mQ[VY]; // intermediate cosine 1
    F32 xz = mQ[VX] * mQ[VX] - mQ[VZ] * mQ[VZ]; // intermediate cosine 2
    F32 cx = ys - xz; // cosine of the roll
    F32 cy = sqrtf(sx * sx + cx * cx); // cosine of the pitch
    if (cy > GIMBAL_THRESHOLD) // no gimbal lock
    {
        *roll  = atan2f(sx, cx);
        *pitch = atan2f(sy, cy);
        *yaw   = atan2f(2 * (mQ[VZ] * mQ[VW] - mQ[VX] * mQ[VY]), ys + xz);
    }
    else // gimbal lock
    {
        if (sy > 0)
        {
            *pitch = F_PI_BY_TWO;
            *yaw = 2 * atan2f(mQ[VZ] + mQ[VX], mQ[VW] + mQ[VY]);
        }
        else
        {
            *pitch = -F_PI_BY_TWO;
            *yaw = 2 * atan2f(mQ[VZ] - mQ[VX], mQ[VW] - mQ[VY]);
        }
        *roll = 0;
    }
}

// Saves space by using the fact that our quaternions are normalized
LLVector3 LLQuaternion::packToVector3() const
{
    F32 x = mQ[VX];
    F32 y = mQ[VY];
    F32 z = mQ[VZ];
    F32 w = mQ[VW];
    F32 mag = sqrtf(x * x + y * y + z * z + w * w);
    if (mag > FP_MAG_THRESHOLD)
    {
        x /= mag;
        y /= mag;
        z /= mag; // no need to normalize w, it's not used
    }
    if( mQ[VW] >= 0 )
    {
        return LLVector3( x, y , z );
    }
    else
    {
        return LLVector3( -x, -y, -z );
    }
}

// Saves space by using the fact that our quaternions are normalized
void LLQuaternion::unpackFromVector3( const LLVector3& vec )
{
    mQ[VX] = vec.mV[VX];
    mQ[VY] = vec.mV[VY];
    mQ[VZ] = vec.mV[VZ];
    F32 t = 1.f - vec.magVecSquared();
    if( t > 0 )
    {
        mQ[VW] = sqrt( t );
    }
    else
    {
        // Need this to avoid trying to find the square root of a negative number due
        // to floating point error.
        mQ[VW] = 0;
    }
}

bool LLQuaternion::parseQuat(const std::string& buf, LLQuaternion* value)
{
    if( buf.empty() || value == NULL)
    {
        return false;
    }

    LLQuaternion quat;
    S32 count = sscanf( buf.c_str(), "%f %f %f %f", quat.mQ + 0, quat.mQ + 1, quat.mQ + 2, quat.mQ + 3 );
    if( 4 == count )
    {
        value->set( quat );
        return true;
    }

    return false;
}


// End