-
Notifications
You must be signed in to change notification settings - Fork 3
/
cqrlib.h
1541 lines (1235 loc) · 46.3 KB
/
cqrlib.h
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
/*
* cqrlib.h
*
*
* Created by Herbert J. Bernstein on 2/15/09.
* Copyright 2009 Herbert J. Bernstein. All rights reserved.
*
* Revised, 8 July 2009 for CQR_FAR macro -- HJB
*/
/* Work supported in part by NIH NIGMS under grant 1R15GM078077-01 and DOE
under grant ER63601-1021466-0009501. Any opinions, findings, and
conclusions or recommendations expressed in this material are those of the
author(s) and do not necessarily reflect the views of the funding agencies.
*/
/**********************************************************************
* *
* YOU MAY REDISTRIBUTE THE CQRlib API UNDER THE TERMS OF THE LGPL *
* *
**********************************************************************/
/************************* LGPL NOTICES *******************************
* *
* 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; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* 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 St, Fifth Floor, Boston, *
* MA 02110-1301 USA *
* *
**********************************************************************/
/* A utility library for quaternion arithmetic and
quaternion rotation math. See
"Quaternions and spatial rotation", Wikipedia
http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
K. Shoemake, "Quaternions", Department of Computer Science,
University of Pennsylvania, Philadelphia, PA 19104,
ftp://ftp.cis.upenn.edu/pub/graphics/shoemake/quatut.ps.Z
K. Shoemake, "Animating rotation with quaternion curves",
ACM SIGGRAPH Computer Graphics, Vol 19, No. 3, pp 245--254,
1985.
*/
#ifndef CQRLIB_H_INCLUDED
#define CQRLIB_H_INCLUDED
#ifdef __cplusplus
#include <climits>
#include <cmath>
#include <cfloat>
template< typename DistanceType=double, typename VectorType=double[3], typename MatrixType=double[9] >
class CPPQR
{
private:
DistanceType w, x, y, z;
public:
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR( ) : // default constructor
w( DistanceType( 0.0 ) ),
x( DistanceType( 0.0 ) ),
y( DistanceType( 0.0 ) ),
z( DistanceType( 0.0 ) )
{
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR( const CPPQR& q ) // copy constructor
{
if ( this != &q )
{
w = q.w;
x = q.x;
y = q.y;
z = q.z;
}
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR( // constructor
const DistanceType& wi,
const DistanceType& xi,
const DistanceType& yi,
const DistanceType& zi
) :
w( wi ),
x( xi ),
y( yi ),
z( zi )
{
}
/* Set -- set the values of an existing quaternion = w +ix+jy+kz */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline void Set ( const DistanceType& wi,
const DistanceType& xi,
const DistanceType& yi,
const DistanceType& zi
)
{
w = wi;
x = xi;
y = yi;
z = zi;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline DistanceType GetW( void ) const
{
return( w );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline DistanceType GetX( void ) const
{
return( x );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline DistanceType GetY( void ) const
{
return( y );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline DistanceType GetZ( void ) const
{
return( z );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR GetIm( void ) const
{
return( CPPQR(0.,x,y,z) );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR GetAxis( void ) const
{
DistanceType anorm=sqrt((double)(x*x + y*y + z*z));
if (anorm < DBL_MIN) return(CPPQR(0.,0.,0.,0.));
return( CPPQR(0.,x/anorm,y/anorm,z/anorm) );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline double GetAngle( void ) const
{
double cosangle, sinangle;
DistanceType radius, anorm;
radius = sqrt( (double) (w*w + x*x + y*y + z*z) );
anorm = sqrt( (double) (x*x + y*y + z*z) );
if ( anorm <= DBL_MIN) {
return 0.;
}
cosangle = w/radius;
sinangle = anorm/radius;
return(atan2(sinangle,cosangle));
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR log( void ) const
{
CPPQR axis = (*this).GetAxis();
double PI;
double ipnormsq;
double angle = (*this).GetAngle();
if ( w < 0. ) {
ipnormsq = (double) (x*x + y*y + z*z);
if (ipnormsq <= DBL_MIN ) {
PI = std::atan2(1.,1.)*4.;
axis = CPPQR(0.,1.,0.,0.);
angle = PI;
}
}
return (CPPQR(std::log((double)((*this).Norm())),axis.x*angle,axis.y*angle,axis.z*angle));
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR exp( void ) const
{
const CPPQR impart = (*this).GetIm( );
const double angle = (double)impart.Norm();
if (angle <= DBL_MIN) {
return (CPPQR(cos(angle)*std::exp(w),0.,0.,0.));
}
const double rat = std::exp(w)*sin(angle)/angle;
return(CPPQR(cos(angle)*std::exp(w),rat*x,rat*y,rat*z));
}
template <typename powertype>
inline CPPQR pow( const powertype p) const {
return(((*this).log()*p).exp());
}
inline CPPQR pow( const int p) const {
CPPQR qtemp, qaccum;
unsigned int ptemp;
if ( p == 0 ) return (CPPQR(1.0,0.,0.,0.));
else if ( p > 0 ) {
qtemp = *this;
ptemp = p;
} else {
qtemp = (*this).Inverse();
ptemp = -p;
}
qaccum = CPPQR(1.0,0.,0.,0.);
while(1) {
if ((ptemp&1)!= 0) {
qaccum *= qtemp;
}
ptemp >>= 1;
if (ptemp==0) break;
qtemp *= qtemp;
}
return qaccum;
}
/* root -- take the given integer root of a quaternion, returning
the indicated mth choice from among multiple roots.
For reals the cycle runs through first the i-based
roots, then the j-based roots and then the k-based roots,
out of the infinite number of possible roots of negative reals. */
inline CPPQR root( const int r, const int m) const
{
const double PI = 4.*atan2(1.,1.);
CPPQR qlog,qlogoverr,qaxis;
double recip, qaxisnormsq;
int cycle;
if (r == 0) return CPPQR(DBL_MAX,DBL_MAX,DBL_MAX,DBL_MAX);
recip = 1./((double)r);
qlog = (*this).log();
if (m != 0) {
qaxis =(*this).GetAxis();
qaxisnormsq = qaxis.Normsq();
if (qaxisnormsq <= DBL_MIN) {
cycle = (m/r)%3;
switch (cycle) {
case 1:
qaxis = CPPQR(0.,0.,1.,0.);
break;
case 2:
qaxis = CPPQR(0.,0.,0.,1.);
break;
default:
qaxis = CPPQR(0.,1.,0.,0.);
break;
}
}
qaxis *= 2.*PI*((double)m);
qlog += qaxis;
}
qlogoverr = qlog*recip;
return qlogoverr.exp();
}
/* Dot product of 2 quaternions as 4-vectors */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline DistanceType Dot( const CPPQR& q) const
{
return (w*q.w+x*q.x+y*q.y+z*q.z);
}
/* Add -- add a quaternion (q1) to a quaternion (q2) */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR operator+ ( const CPPQR& q ) const
{
CPPQR temp;
temp.w = w + q.w;
temp.x = x + q.x;
temp.y = y + q.y;
temp.z = z + q.z;
return( temp );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR& operator+= ( const CPPQR& q )
{
w += q.w;
x += q.x;
y += q.y;
z += q.z;
return (*this);
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR& operator-= ( const CPPQR& q )
{
w -= q.w;
x -= q.x;
y -= q.y;
z -= q.z;
return (*this);
}
/* Subtract -- subtract a quaternion (q2) from a quaternion (q1) */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR operator- ( const CPPQR& q ) const
{
CPPQR temp;
temp.w = w - q.w;
temp.x = x - q.x;
temp.y = y - q.y;
temp.z = z - q.z;
return( temp );
}
/* Unary minus -- negate a quaterion */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR operator- ( void ) const
{
CPPQR temp;
temp.w = -w;
temp.x = -x;
temp.y = -y;
temp.z = -z;
return( temp );
}
/* Multiply -- multiply a quaternion (q1) by quaternion (q2) */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR operator* ( const CPPQR& q ) const // multiply two quaternions
{
CPPQR temp;
temp.w = -z*q.z - y*q.y - x*q.x + w*q.w;
temp.x = y*q.z - z*q.y + w*q.x + x*q.w;
temp.y = -x*q.z + w*q.y + z*q.x + y*q.w;
temp.z = w*q.z + x*q.y - y*q.x + z*q.w;
return( temp );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR& operator*= ( const CPPQR& q )
{
CPPQR temp;
temp.w = -z*q.z - y*q.y - x*q.x + w*q.w;
temp.x = y*q.z - z*q.y + w*q.x + x*q.w;
temp.y = -x*q.z + w*q.y + z*q.x + y*q.w;
temp.z = w*q.z + x*q.y - y*q.x + z*q.w;
w = temp.w;
x = temp.x;
y = temp.y;
z = temp.z;
return (*this);
}
/* Divide -- Divide a quaternion (q1) by quaternion (q2) */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR operator/ ( const CPPQR& q2 ) const
{
const DistanceType norm2sq = q2.w*q2.w + q2.x*q2.x + q2.y*q2.y + q2.z*q2.z;
if ( norm2sq == 0.0 )
{
return( CPPQR( DBL_MAX, DBL_MAX, DBL_MAX, DBL_MAX ) );
}
CPPQR q;
q.w = z*q2.z + y*q2.y + x*q2.x + w*q2.w;
q.x = -y*q2.z + z*q2.y - w*q2.x + x*q2.w;
q.y = x*q2.z - w*q2.y - z*q2.x + y*q2.w;
q.z = -w*q2.z - x*q2.y + y*q2.x + z*q2.w;
return( q / norm2sq );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR& operator/= ( const CPPQR& q2 )
{
const DistanceType norm2sq = q2.w*q2.w + q2.x*q2.x + q2.y*q2.y + q2.z*q2.z;
if ( norm2sq == 0.0 )
{
return( CPPQR( DBL_MAX, DBL_MAX, DBL_MAX, DBL_MAX ) );
}
CPPQR q;
q.w = z*q2.z + y*q2.y + x*q2.x + w*q2.w;
q.x = -y*q2.z + z*q2.y - w*q2.x + x*q2.w;
q.y = x*q2.z - w*q2.y - z*q2.x + y*q2.w;
q.z = -w*q2.z - x*q2.y + y*q2.x + z*q2.w;
w = q.w/norm2sq;
x = q.x/norm2sq;
y = q.y/norm2sq;
z = q.z/norm2sq;
return (*this);
}
/* ScalarMultiply -- multiply a quaternion (q) by scalar (s) */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR operator* ( const DistanceType& d ) const // multiply by a constant
{
CPPQR temp;
temp.w = w*d;
temp.x = x*d;
temp.y = y*d;
temp.z = z*d;
return( temp );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR& operator*= ( const DistanceType& d )
{
w *= d;
x *= d;
y *= d;
z *= d;
return (*this);
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR& operator/= ( const DistanceType& d )
{
if ( std::abs((double)d) <= DBL_MIN ) {
w = DBL_MAX;
x = DBL_MAX;
y = DBL_MAX;
z = DBL_MAX;
} else {
w /= d;
x /= d;
y /= d;
z /= d;
}
return (*this);
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR operator/ ( const DistanceType& d ) const // divide by a constant
{
CPPQR temp;
temp.w = w/d;
temp.x = x/d;
temp.y = y/d;
temp.z = z/d;
return( temp );
}
/* Conjugate -- Form the conjugate of a quaternion qconj */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR Conjugate ( void ) const
{
CPPQR conjugate;
conjugate.w = w;
conjugate.x = -x;
conjugate.y = -y;
conjugate.z = -z;
return( conjugate );
}
/* Normsq -- Form the normsquared of a quaternion */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline DistanceType Normsq ( void ) const
{
return( w*w + x*x + y*y + z*z );
}
/* Norm -- Form the norm of a quaternion */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline DistanceType Norm ( void ) const
{
return sqrt( w*w + x*x + y*y + z*z );
}
/* Distsq -- Form the distance squared from a quaternion */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline DistanceType Distsq ( const CPPQR& q ) const
{
return( (w-q.w)*(w-q.w) + (x-q.x)*(x-q.x) + (y-q.y)*(y-q.y) + (z-q.z)*(z-q.z) );
}
/* Dist -- Form the distance from a quaternion */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline DistanceType Dist ( const CPPQR& q ) const
{
return sqrt( (w-q.w)*(w-q.w) + (x-q.x)*(x-q.x) + (y-q.y)*(y-q.y) + (z-q.z)*(z-q.z) );
}
/* Inverse -- Form the inverse of a quaternion */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR Inverse ( void ) const
{
CPPQR inversequaternion = (*this).Conjugate();
const DistanceType normsq = (*this).Normsq( );
if ( normsq > DistanceType( 0.0 ) )
{
inversequaternion = inversequaternion * DistanceType( 1.0 ) / normsq;
}
else
{
inversequaternion = CPPQR( DBL_MAX, DBL_MAX, DBL_MAX, DBL_MAX );
}
return( inversequaternion );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR& operator= ( const CPPQR& q )
{
if ( this != &q )
{
w = q.w;
x = q.x;
y = q.y;
z = q.z;
}
return( *this );
}
/* Equal */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline bool operator== ( const CPPQR& q ) const
{
return( w==q.w && x==q.x && y==q.y && z==q.z );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline bool operator!= ( const CPPQR& q ) const
{
return( (w!=q.w) || (x!=q.x) || (y!=q.y) | (z!=q.z) );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline VectorType& operator* ( const VectorType& v )
{
return( RotateByQuaternion( v ) );
}
/* RotateByQuaternion -- Rotate a vector by a Quaternion, w = qvq* */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline void RotateByQuaternion(VectorType &w, const VectorType v )
{
CPPQR vquat( 0.0, v[0], v[1], v[2] );
const CPPQR wquat = (*this)*vquat;
const CPPQR qconj = (*this).Conjugate( );
vquat = wquat * qconj;
w[0] = vquat.x; w[1] = vquat.y; w[2] = vquat.z;
return;
}
inline VectorType& RotateByQuaternion(const VectorType v )
{
CPPQR vquat( 0.0, v[0], v[1], v[2] );
const CPPQR wquat = (*this)*vquat;
const CPPQR qconj = (*this).Conjugate( );
vquat = wquat * qconj;
return VectorType(vquat.x, vquat.y, vquat.z);
}
/* Axis2Quaternion -- Form the quaternion for a rotation around axis v by angle theta */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static inline CPPQR Axis2Quaternion ( const DistanceType& angle, const VectorType v )
{
return( Axis2Quaternion( v, angle ) );
}
/* Axis2Quaternion -- Form the quaternion for a rotation around axis v by angle theta */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static inline CPPQR Axis2Quaternion ( const VectorType v, const DistanceType& angle )
{
const DistanceType norm2sq = v[0]*v[0]+v[1]*v[1]+v[2]*v[2];
if ( norm2sq == DistanceType(0.0) )
{
return( CPPQR( DBL_MAX, DBL_MAX, DBL_MAX, DBL_MAX ) );
}
const DistanceType sinOverNorm = sin(angle/2.0)/sqrt(norm2sq);
const CPPQR q( cos(angle/2.0), sinOverNorm*v[0], sinOverNorm*v[1], sinOverNorm*v[2]);
return( q );
}
/* Matrix2Quaterion -- Form the quaternion from a 3x3 rotation matrix R */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static inline void Matrix2Quaternion ( CPPQR& rotquaternion, const MatrixType m )
{
const DistanceType trace = m[0] + m[4] + m[8];
if ( trace > -0.75 )
{
rotquaternion.w = sqrt( (1.0+trace)) / 2.0;
const DistanceType recip = 0.25 / rotquaternion.w;
rotquaternion.z = (m[3]-m[1]) * recip;
rotquaternion.y = (m[2]-m[6]) * recip;
rotquaternion.x = (m[7]-m[5]) * recip;
return;
}
const DistanceType fourxsq = 1.0 + m[0] - m[4] - m[8];
if ( fourxsq >= 0.25 )
{
rotquaternion.x = sqrt( fourxsq ) / 2.0;
const DistanceType recip = 0.25 /rotquaternion.x;
rotquaternion.y = (m[3] + m[1])*recip;
rotquaternion.z = (m[2] + m[6])*recip;
rotquaternion.w = (m[7] - m[5])*recip;
return;
}
const DistanceType fourysq = 1.0 + m[4] - m[0] - m[8];
if ( fourysq >= 0.25 )
{
rotquaternion.y = sqrt( fourysq ) / 2.0;
const DistanceType recip = 0.25 / rotquaternion.y;
rotquaternion.x = (m[3] + m[1])*recip;
rotquaternion.w = (m[2] - m[6])*recip;
rotquaternion.z = (m[7] + m[5])*recip;
return;
}
const DistanceType fourzsq = 1. + m[8] - m[0] - m[4];
if ( fourzsq >= 0.25 )
{
rotquaternion.z = sqrt( fourzsq ) / 2.0;
const DistanceType recip = 0.25 / rotquaternion.z;
rotquaternion.w = (m[3] - m[1])*recip;
rotquaternion.x = (m[2] + m[6])*recip;
rotquaternion.y = (m[7] + m[5])*recip;
return;
}
rotquaternion.x = rotquaternion.y = rotquaternion.z = rotquaternion.w = 0;
return;
}
static inline void Matrix2Quaternion (CPPQR& rotquaternion, const DistanceType R[3][3] )
{
const DistanceType trace = R[0][0] + R[1][1] + R[2][2];
if ( trace > -0.75 )
{
rotquaternion.w = sqrt( (1.0+trace)) / 2.0;
const DistanceType recip = 0.25 / rotquaternion.w;
rotquaternion.z = (R[1][0]-R[0][1]) * recip;
rotquaternion.y = (R[0][2]-R[2][0]) * recip;
rotquaternion.x = (R[2][1]-R[1][2]) * recip;
return;
}
const DistanceType fourxsq = 1.0 + R[0][0] - R[1][1] - R[2][2];
if ( fourxsq >= 0.25 )
{
rotquaternion.x = sqrt( fourxsq ) / 2.0;
const DistanceType recip = 0.25 /rotquaternion.x;
rotquaternion.y = (R[1][0]+R[0][1])*recip;
rotquaternion.z = (R[0][2]+R[2][0])*recip;
rotquaternion.w = (R[2][1]-R[1][2])*recip;
return;
}
const DistanceType fourysq = 1.0 + R[1][1] - R[0][0] - R[2][2];
if ( fourysq >= 0.25 )
{
rotquaternion.y = sqrt( fourysq ) / 2.0;
const DistanceType recip = 0.25 / rotquaternion.y;
rotquaternion.x = (R[1][0]+R[0][1])*recip;
rotquaternion.w = (R[0][2]-R[2][0])*recip;
rotquaternion.z = (R[2][1]+R[1][2])*recip;
return;
}
const DistanceType fourzsq = 1. + R[2][2] - R[0][0] - R[1][1];
if ( fourzsq >= 0.25 )
{
rotquaternion.z = sqrt( fourzsq ) / 2.0;
const DistanceType recip = 0.25 / rotquaternion.z;
rotquaternion.w = (R[1][0]-R[0][1])*recip;
rotquaternion.x = (R[0][2]+R[2][0])*recip;
rotquaternion.y = (R[2][1]+R[1][2])*recip;
return;
}
rotquaternion.x = rotquaternion.y = rotquaternion.z = rotquaternion.w = 0;
return;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
DistanceType operator[] ( const int k ) const
{
const int i = (k<0) ? 0 : ( (k>3)?3:k ) ;
if ( i==0 ) return w;
if ( i==1 ) return x;
if ( i==2 ) return y;
if ( i==3 ) return z;
return( 0 ); // just to keep compilers happy
}
/* Quaternion2Matrix -- Form the 3x3 rotation matrix from a quaternion */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static inline void Quaternion2Matrix(MatrixType& m, const CPPQR q )
{
const DistanceType ww = q.w*q.w;
const DistanceType xx = q.x*q.x;
const DistanceType yy = q.y*q.y;
const DistanceType zz = q.z*q.z;
const DistanceType twoxy = 2.0 * q.x*q.y;
const DistanceType twoyz = 2.0 * q.y*q.z;
const DistanceType twoxz = 2.0 * q.x*q.z;
const DistanceType twowx = 2.0 * q.w*q.x;
const DistanceType twowy = 2.0 * q.w*q.y;
const DistanceType twowz = 2.0 * q.w*q.z;
m[0] = ww+xx-yy-zz; m[1] = twoxy - twowz; m[2] = twoxz + twowy;
m[3] = twoxy + twowz; m[4] = ww-xx+yy-zz; m[5] = twoyz - twowx;
m[6] = twoxz - twowy; m[7] = twoyz + twowx; m[8] = ww-xx-yy+zz;
return;
}
static inline void Quaternion2Matrix( DistanceType m[3][3], const CPPQR q )
{
const DistanceType ww = q.w*q.w;
const DistanceType xx = q.x*q.x;
const DistanceType yy = q.y*q.y;
const DistanceType zz = q.z*q.z;
const DistanceType twoxy = 2.0 * q.x*q.y;
const DistanceType twoyz = 2.0 * q.y*q.z;
const DistanceType twoxz = 2.0 * q.x*q.z;
const DistanceType twowx = 2.0 * q.w*q.x;
const DistanceType twowy = 2.0 * q.w*q.y;
const DistanceType twowz = 2.0 * q.w*q.z;
m[0][0] = ww+xx-yy-zz; m[0][1] = twoxy - twowz; m[0][2] = twoxz + twowy;
m[1][0] = twoxy + twowz; m[1][1] = ww-xx+yy-zz; m[0][2] = twoyz - twowx;
m[2][0] = twoxz - twowy; m[2][1] = twoyz + twowx; m[2][2] = ww-xx-yy+zz;
return;
}
// end Quaternion2Matrix
/* Get a unit quaternion from a general one */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR UnitQ( void ) const
{
const DistanceType normsq = Normsq( );
if ( normsq == 0.0 )
{
return( CPPQR( DBL_MAX, DBL_MAX, DBL_MAX, DBL_MAX ) );
}
else
{
return( (*this) / sqrt( Normsq( ) ) );
}
} // end UnitQ
/* Quaternion2Angles -- Convert a Quaternion into Euler Angles for Rz(Ry(Rx))) convention */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline bool Quaternion2Angles ( DistanceType& rotX, DistanceType& rotY, DistanceType& rotZ ) const
{
const DistanceType PI = 4.0 * atan( 1.0 );
const DistanceType rmx0 = w*w + x*x - y*y - z*z;
const DistanceType rmx1 = 2.0 * ( x*y - w*z );
const DistanceType rmy0 = 2.0 * ( x*y + w*z );
const DistanceType rmy1 = w*w - x*x + y*y - z*z;
const DistanceType rmz0 = 2.0 * (x*z - w*y );
const DistanceType rmz1 = 2.0 * (w*x + y*z );
const DistanceType rmz2 = w*w - x*x - y*y + z*z;
DistanceType srx, sry, srz, trX, trY, trZ;
if ( rmz0 >= 1.0 )
{
sry = -0.5 * PI;
}
else if ( rmz0 <= -1.0 )
{
sry = 0.5 * PI;
}
else
{
sry = asin( -rmz0 );
}
if ( rmz0 > 0.9999995 )
{
srx = atan2( -rmx1, rmy1 );
srz = 0.0;
}
else if ( rmz0 < -0.9999995 )
{
srx = atan2( rmx1, rmy1 );
srz = 0.0;
}
else
{
srx = atan2( rmz1, rmz2 );
srz = atan2( rmy0, rmx0 );
}
trX = PI + srx;
if ( trX > 2.0 * PI ) trX -= 2.0 * PI;
trY = PI + sry;
if ( trY > 2.0 * PI ) trY -= 2.0 * PI;
trZ = PI + srz;
if ( trZ > 2.0 * PI ) trZ -= 2.0 * PI;
const DistanceType nsum = fabs( cos(srx)-cos(rotX)) + fabs( sin(srx)-sin(rotX))
+ fabs( cos(sry)-cos(rotY)) + fabs( sin(sry)-sin(rotY))
+ fabs( cos(srz)-cos(rotZ)) + fabs( sin(srz)-sin(rotZ));
const DistanceType tsum = fabs( cos(trX)-cos(rotX)) + fabs( sin(trX)-sin(rotX))
+ fabs( cos(trY)-cos(rotY)) + fabs( sin(trY)-sin(rotY))
+ fabs( cos(trZ)-cos(rotZ)) + fabs( sin(trZ)-sin(rotZ));
if ( nsum < tsum )
{
rotX = srx; rotY = sry; rotZ = srz;
}
else
{
rotX = trX; rotY = trY; rotZ = trZ;
}
return( true );
} // end Quaternion2Angles
/* Angles2Quaternion -- Convert Euler Angles for Rz(Ry(Rx))) convention into a quaternion */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static inline CPPQR Angles2Quaternion ( const DistanceType& rotX, const DistanceType& rotY, const DistanceType& rotZ )
{
const DistanceType cx = cos( rotX / 2.0 );
const DistanceType sx = sin( rotX / 2.0 );
const DistanceType cy = cos( rotY / 2.0 );
const DistanceType sy = sin( rotY / 2.0 );
const DistanceType cz = cos( rotZ / 2.0 );
const DistanceType sz = sin( rotZ / 2.0 );
const CPPQR q( cx*cy*cz + sx*sy*sz,
sx*cy*cz - cx*sy*sz,
cx*sy*cz + sx*cy*sz,
cx*cy*sz - sx*sy*cz
);
return( q );
} // end Angles2Quaternion
static inline CPPQR Point2Quaternion( const DistanceType v[3] )
{
return( CPPQR( 0.0, v[0], v[1], v[2] ) );
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* SLERP -- Spherical Linear Interpolation
Take two quaternions and two weights and combine them
following a great circle on the unit quaternion 4-D sphere
and linear interpolation between the radii
This version keeps a quaternion separate from the negative
of the same quaternion and is not appropriate for
quaternions representing rotations. Use CQRHLERP
to apply SLERP to quaternions representing rotations
*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline CPPQR SLERP (const CPPQR& q, DistanceType w1, DistanceType w2) const
{
CPPQR s1;
CPPQR s2;
CPPQR st1;
CPPQR st2;
CPPQR sout;
DistanceType normsq;
const DistanceType norm1sq=(*this).Normsq();
const DistanceType norm2sq=q.Normsq();
DistanceType r1,r2;
DistanceType cosomega,sinomega;
DistanceType omega;
DistanceType t, t1, t2;
t = w1/(w1+w2);
if (norm1sq <= DBL_MIN) return q*(1-t);
if (norm2sq <= DBL_MIN) return (*this)*t;
if (fabs(norm1sq-1.)<= DBL_MIN) {
r1 = 1.;
s1 = *this;
} else {
r1 = sqrt(norm1sq);
s1 = (*this)*(1/r1);
}
if (fabs(norm2sq-1.)<= DBL_MIN) {
r2 = 1.;
s2 = q;
} else {
r2 = sqrt(norm1sq);
s2 = q*(1./r2);
}
cosomega = s1.Dot(s2);
if (cosomega>=1. || cosomega<=-1.) {
sinomega = 0.;
} else {
sinomega=sqrt(1.-cosomega*cosomega);
}
omega=atan2(sinomega,cosomega);
if (sinomega <= 0.05) {
t1=t*(1-t*t*omega*omega/6.);
t2=(1-t)*(1.-(1-t)*(1-t)*omega*omega/6.);
st1=s1*t1;
st2=s2*t2;
if (cosomega >=0.) {
sout=st1+st2;
} else {
if (sinomega <= 0.00001) {
sout=CPPQR(-st1.x,st1.w,st1.z,-st1.y)-CPPQR(-st2.x,st2.w,st2.z,-st2.y);
} else {
sout = s1+s2;
}
sout=sout*(1/sout.Norm());
if (t >= 0.5) {
sout=sout.SLERP(s1,2-2.*t,2.*t-1.);
}else {
sout=sout.SLERP(s2,2.*t,1.-2.*t);
}
}
normsq = sout.Normsq();
if (normsq <= DBL_MIN) {
return CPPQR(0.,0.,0.,0.);
} else {
return CPPQR(sout*(t*r1+(1-t)*r2)/sqrt(normsq));
}
}
t1 = sin(t*omega);
t2 = sin((1-t)*omega);
st1=s1*t1;
st2=s2*t2;
sout=st1+st2;
normsq = sout.Normsq();
return CPPQR(sout*((r1*t+r2*(1-t))/sqrt(normsq)));
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* HLERP -- Hemispherical Linear Interpolation
Take two quaternions and two weights and combine them
following a great circle on the unit quaternion 4-D sphere
and linear interpolation between the radii