forked from petercorke/spatialmath-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitQuaternion.m
1375 lines (1229 loc) · 54.1 KB
/
UnitQuaternion.m
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
%UnitQuaternion Unit quaternion class
%
% A UnitQuaternion is a compact method of representing a 3D rotation that has
% computational advantages including speed and numerical robustness.
% A quaternion has 2 parts, a scalar s, and a vector v and is typically
% written: q = s <vx, vy, vz>.
%
% A UnitQuaternion is one for which s^2+vx^2+vy^2+vz^2 = 1. It can be
% considered as a rotation by an angle theta about a unit-vector V in space where
%
% q = cos (theta/2) < v sin(theta/2)>
%
% Constructors::
% UnitQuaternion general constructor
% UnitQuaternion.angvec constructor, from (angle and vector)
% UnitQuaternion.eul constructor, from Euler angles
% UnitQuaternion.omega constructor for angle*vector
% UnitQuaternion.rpy constructor, from roll-pitch-yaw angles
% UnitQuaternion.Rx constructor, from x-axis rotation
% UnitQuaternion.Ry constructor, from y-axis rotation
% UnitQuaternion.Rz constructor, from z-axis rotation
% UnitQuaternion.vec constructor, from 3-vector
%
% Display and print methods::
% animate animates a coordinate frame
% display print in human readable form
% plot plot a coordinate frame representing orientation of quaternion
%
% Group operations::
% * ^quaternion (Hamilton) product
% .* quaternion (Hamilton) product and renormalize
% / ^multiply by inverse
% ./ multiply by inverse and renormalize
% ^ ^exponentiate (integer only)
% exp ^exponential
% inv ^inverse
% log ^logarithm
% prod product of elements
%
% Methods::
% angle angle between two quaternions
% conj ^conjugate
% dot derivative of quaternion with angular velocity
% inner ^inner product
% interp interpolation (slerp) between two quaternions
% norm ^norm, or length
% unit unitized quaternion
% UnitQuaternion.qvmul multiply unit-quaternions in 3-vector form
%
% Conversion methods::
% char convert to string
% double ^convert to 4-vector
% matrix convert to 4x4 matrix
% R convert to 3x3 rotation matrix
% SE3 convert to SE3 object
% SO3 convert to SO3 object
% T convert to 4x4 homogeneous transform matrix
% toangvec convert to angle vector form
% toeul convert to Euler angles
% torpy convert to roll-pitch-yaw angles
% tovec convert to 3-vector
%
% Operators::
% + elementwise sum of quaternion elements (result is a Quaternion)
% - elementwise difference of quaternion elements (result is a Quaternion)
% == test for equality
% ~= ^test for inequality
%
% ^ means inherited from Quaternion class.
%
% Properties (read only)::
% s real part
% v vector part
%
% Notes::
% - A subclass of Quaternion
% - Many methods and operators are inherited from the Quaternion superclass.
% - UnitQuaternion objects can be used in vectors and arrays.
% - The + and - operators return a Quaternion object not a UnitQuaternion
% since these are not group operators.
% - For display purposes a Quaternion differs from a UnitQuaternion by
% using << >> notation rather than < >.
% - To a large extent polymorphic with the SO3 class.
%
% References::
% - Animating rotation with quaternion curves,
% K. Shoemake,
% in Proceedings of ACM SIGGRAPH, (San Fran cisco), pp. 245-254, 1985.
% - On homogeneous transforms, quaternions, and computational efficiency,
% J. Funda, R. Taylor, and R. Paul,
% IEEE Transactions on Robotics and Automation, vol. 6, pp. 382-388, June 1990.
% - Quaternions for Computer Graphics, J. Vince, Springer 2011.
% - Robotics, Vision & Control: Second Edition, P. Corke, Springer 2016; p44-45.
%
% See also Quaternion, SO3.
% Copyright (C) 1993-2019 Peter I. Corke
%
% This file is part of The Spatial Math Toolbox for MATLAB (SMTB).
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
% of the Software, and to permit persons to whom the Software is furnished to do
% so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all
% copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
% FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
% COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
% IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
% CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
% https://github.com/petercorke/spatial-math
% TODO
% constructor handles R, T trajectory and returns vector
% .r, .t on a quaternion vector??
% TODO
% add & test dotb, add dot, dotb to SO3
% rpy/eul, to and from should be vectorised
classdef UnitQuaternion < Quaternion
methods
function uq = UnitQuaternion(s, v)
%UnitQuaternion.Quaternion Construct a unit quaternion object
%
% Construct a UnitQuaternion from various other orientation representations.
%
% Q = UnitQuaternion() is the identitity UnitQuaternion 1<0,0,0> representing a null rotation.
%
% Q = UnitQuaternion(Q1) is a copy of the UnitQuaternion Q1, if Q1 is a
% Quaternion it is normalised.
%
% Q = UnitQuaternion(S, V) is a UnitQuaternion formed by specifying directly
% its scalar and vector parts which are normalised.
%
% Q = UnitQuaternion([S, V1, V2, V3]) is a UnitQuaternion formed by specifying
% directly its 4 elements which are normalised.
%
% Q = Quaternion(R) is a UnitQuaternion corresponding to the SO(3)
% orthonormal rotation matrix R (3x3). If R (3x3xN) is a sequence then Q
% (Nx1) is a vector of Quaternions corresponding to the elements of R.
%
% Q = Quaternion(T) is a UnitQuaternion equivalent to the rotational part
% of the SE(3) homogeneous transform T (4x4). If T (4x4xN) is a sequence
% then Q (Nx1) is a vector of Quaternions corresponding to the elements of
% T.
%
% Notes::
% - Only the R and T forms are vectorised.
% - To convert an SO3 or SE3 object to a UnitQuaternion use their
% UnitQuaternion conversion methods.
%
% See also UnitQuaternion.eul, UnitQuaternion.rpy, UnitQuaternion.angvec,
% UnitQuaternion.omega, UnitQuaternion.Rx, UnitQuaternion.Ry,
% UnitQuaternion.Rz, SE3.UnitQuaternion, SO3.UnitQuaternion.
if nargin == 0
% null constructor
% uq.v = [0,0,0];
uq.s = 1;
elseif isa(s, 'Quaternion')
% passed a quaternion of some kind, optionally normalize
if ~isa(s, 'UnitQuaternion')
s = s.unit();
end
uq.s = s.s;
uq.v = s.v;
elseif isa(s, 'SO3')
% Q = Quaternion(R) from a 3x3 or 4x4 matrix
uq(length(s)) = UnitQuaternion(); % preallocate
for i=1:length(s)
[qs,qv] = UnitQuaternion.tr2q(s(i).R);
uq(i) = UnitQuaternion(qs,qv);
end
elseif isrot(s) || ishomog(s)
% Q = Quaternion(R) from a 3x3 or 4x4 matrix
uq(size(s,3)) = UnitQuaternion(); % preallocate
for i=1:size(s,3)
[qs,qv] = UnitQuaternion.tr2q(s(:,:,i));
uq(i) = UnitQuaternion(qs,qv);
end
elseif nargin == 2 && isscalar(s) && isvec(v,3)
% ensure its a unit quaternion
if isa(v, 'symfun')
v = formula(v);
end
n = norm([s; v(:)]);
if s < 0
% enforce non-negative scalar
s = -s;
v = -v;
end
uq.s = s/n;
uq.v = v/n;
elseif isvec(s,4)
% passed in a 4-vector of components
% normalize it
if ~isa(s, 'sym')
s = unit(s);
end
if s(1) < 0
% enforce non-negative scalar
s = -s;
end
uq.s = s(1);
uq.v = s(2:4);
else
error ('SMTB:UnitQuaternion:badarg', 'bad argument to quaternion constructor');
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% UNIT QUATERNION FUNCTIONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function qi = inv(q)
%UnitQuaternion.inv Invert a UnitQuaternion
%
% Q.inv() is a UnitQuaternion object representing the inverse of Q. If
% Q is a vector (1xN) the result is a vector of elementwise inverses.
%
% See also Quaternion.conj.
for i=1:length(q)
qi(i) = UnitQuaternion( [q(i).s -q(i).v] );
end
end
function q = interp(Q1, varargin)
%UnitQuaternion.interp Interpolate UnitQuaternion
%
% QI = Q.scale(S, OPTIONS) is a UnitQuaternion that interpolates between a null
% rotation (identity UnitQuaternion) for S=0 to Q for S=1.
%
% QI = Q1.interp(Q2, S, OPTIONS) as above but interpolates a rotation
% between Q1 for S=0 and Q2 for S=1.
%
% If S is a vector QI is a vector of UnitQuaternions, each element
% corresponding to sequential elements of S.
%
% Options::
% 'shortest' Take the shortest path along the great circle
%
% Notes::
% - This is a spherical linear interpolation (slerp) that can be interpretted
% as interpolation along a great circle arc on a sphere.
% - It is an error if any element of S is outside the interval 0 to 1.
%
% References::
% - Animating rotation with quaternion curves, K. Shoemake,
% in Proceedings of ACM SIGGRAPH, (San Francisco), pp. 245-254, 1985.
%
% See also ctraj.
opt.shortest = false;
[opt,args] = tb_optparse(opt, varargin);
if isa(args{1}, 'UnitQuaternion')
q1 = double(Q1);
q2 = double(args{1});
r = args{2};
else
q1 = [1 0 0 0]; % unit quaternion
q2 = double(Q1); % given quaternion
r = args{1};
end
% now, interpolate between q1 and q2
cosTheta = q1*q2';
if opt.shortest
% take shortest path along the great circle, patch by Gauthier Gras
if cosTheta < 0
q1 = - q1;
cosTheta = - cosTheta;
end
end
theta = acos(cosTheta);
if length(r) == 1 && r > 1 && (r == floor(r))
% integer value
r = [0:(r-1)] / (r-1);
elseif any(r<0 | r>1)
error('SMTB:UnitQuaternion:interp', 'values of S outside interval [0,1]');
end
q(length(r)) = UnitQuaternion(); % preallocate space for Quaternion vector
for i=1:length(r)
if theta == 0
q(i) = Q1;
else
q(i) = UnitQuaternion( (sin((1-r(i))*theta) * q1 + sin(r(i)*theta) * q2) / sin(theta) );
end
end
end
% polymorphic with SE3
% function v = isidentity(obj)
% v = all(all(obj.T == eye(4,4)));
% end
function qu = increment(obj, w)
%UnitQuaternion.increment Update UnitQuaternion by angular displacement
%
% QU = Q.increment(OMEGA) updates Q by an infinitessimal rotation which is given
% as a spatial displacement OMEGA (3x1) whose direction is the rotation axis and
% magnitude is the amount of rotation.
%
% Notes::
% - OMEGA is an approximation to the instantaneous spatial velocity multiplied by
% time step.
%
% See also tr2delta.
qu = obj .* UnitQuaternion.omega( w );
end
function th = angle(q1, q2)
%UnitQuaternion.angle Angle between two UnitQuaternions
%
% A = Q1.angle(Q2) is the angle (in radians) between two UnitQuaternions Q1 and Q2.
%
% Notes::
% - If either, or both, of Q1 or Q2 are vectors, then the result is a vector.
% - if Q1 is a vector (1xN) then A is a vector (1xN) such that A(i) = P1(i).angle(Q2).
% - if Q2 is a vector (1xN) then A is a vector (1xN) such that A(i) = P1.angle(P2(i)).
% - if both Q1 and Q2 are vectors (1xN) then A is a vector (1xN) such
% that A(i) = P1(i).angle(Q2(i)).
%
% References::
% - Metrics for 3D rotations: comparison and analysis, Du Q. Huynh,
% J.Math Imaging Vis. DOFI 10.1007/s10851-009-0161-2.
%
% See also Quaternion.angvec.
c = zeros(1, max(length(q1), length(q2)));
q1d = q1.double; q2d = q2.double;
if length(q1) == length(q2)
th = 2*atan2( colnorm((q1d-q2d)'), colnorm((q1d+q2d)') );
elseif length(q1) == 1
for i=1:length(q2)
th(i) = 2*atan2( colnorm((q1d-q2d(i,:))'), colnorm((q1d+q2d(i,:))') );
end
elseif length(q2) == 1
for i=1:length(q1)
th(i) = 2*atan2( colnorm((q1d(i,:)-q2d)'), colnorm((q1d(i,:)+q2d)') );
end
else
error('SMTB:Quaternion:badarg', 'angle arguments must be vectors of same length');
end
% clip it, just in case it's unnormalized
c(c<-1) = -1; c(c>1) = 1;
% Huynh method 3, LaValle p157
% acos( abs(dot(q1d,q2d)) )
%th = 2*acos(c);
%% use 2 atan2(|q1-q2|, |q1+q2|)
end
function qu = unit(q)
%UnitQuaternion.unit Unitize unit-quaternion
%
% QU = Q.unit() is a UnitQuaternion with a norm of 1. If Q is a vector (1xN) then
% QU is also a vector (1xN).
%
% Notes::
% - This is UnitQuaternion of unit norm, not a Quaternion of unit norm.
%
% See also Quaternion.norm.
for i=1:length(q)
qu(i) = UnitQuaternion( q(i).double / norm(q(i)) );
end
end
function qd = dot(q, omega)
%UnitQuaternion.dot UnitQuaternion derivative in world frame
%
% QD = Q.dot(omega) is the rate of change of the UnitQuaternion Q expressed
% as a Quaternion in the world frame. Q represents the orientation of a body
% frame with angular velocity OMEGA (1x3).
%
% Notes::
% - This is not a group operator, but it is useful to have the result as a
% Quaternion.
%
% Reference::
% - Robotics, Vision & Control, 2nd edition, Peter Corke, pp.64.
%
% See also UnitQuaternion.dotb.
% UnitQuaternion.pure(omega) * q
E = q.s*eye(3,3) - skew(q.v);
omega = omega(:);
qd = 0.5 * Quaternion(-q.v*omega, E*omega);
end
function qd = dotb(q, omega)
%UnitQuaternion.dot UnitQuaternion derivative in body frame
%
% QD = Q.dotb(omega) is the rate of change of the UnitQuaternion Q expressed
% as a Quaternion in the body frame. Q represents the orientation of a body
% frame with angular velocity OMEGA (1x3).
%
% Notes::
% - This is not a group operator, but it is useful to have the result as a
% quaternion.
%
% Reference::
% - Robotics, Vision & Control, 2nd edition, Peter Corke, pp.64.
%
% See also UnitQuaternion.dot.
% q * UnitQuaternion.pure(omega)
E = q.s*eye(3,3) + skew(q.v);
omega = omega(:);
qd = 0.5 * Quaternion(-q.v*omega, E*omega);
end
function out = prod(q)
%UnitQuaternion.prod Product of unit quaternions
%
% prod(Q) is the product of the elements of the vector of UnitQuaternion objects Q.
%
% Note::
% - Multiplication is performed with the .* operator, ie. the product is
% renormalized at every step.
%
% See also UnitQuaternion.times, RTBPose.prod.
out = q(1);
for qq = q(2:end)
out = out * qq;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% ARITHMETIC OPERATORS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function qp = mtimes(q1, q2)
%UnitQuaternion.mtimes Multiply UnitQuaternion's
%
% R = Q1*Q2 is a UnitQuaternion object formed by Hamilton product
% of Q1 and Q2 where Q1 and Q2 are both UnitQuaternion objects.
%
% Q*V is a vector (3x1) formed by rotating the vector V (3x1)by the UnitQuaternion Q.
%
% Notes::
% - Overloaded operator '*'
% - If either, or both, of Q1 or Q2 are vectors, then the result is a vector.
% - if Q1 is a vector (1xN) then R is a vector (1xN) such that R(i) = Q1(i)*Q2.
% - if Q2 is a vector (1xN) then R is a vector (1xN) such that R(i) = Q1*Q2(i).
% - if both Q1 and Q2 are vectors (1xN) then R is a vector (1xN) such
% that R(i) = Q1(i)*Q2(i).
%
% See also Quaternion.mrdivide, Quaternion.mpower, Quaternion.plus, Quaternion.minus.
if isa(q2, 'UnitQuaternion')
%QQMUL Multiply UnitQuaternion by UnitQuaternion
%
% QQ = qqmul(Q1, Q2)
%
% Return a product of UnitQuaternions.
%
% See also: TR2Q
% get the superclass to do this for us
qp = mtimes@Quaternion(q1, q2);
elseif isa(q2, 'Quaternion')
% get the superclass to do this for us
qp = mtimes@Quaternion(q1, q2);
elseif isa(q1, 'Quaternion') && (isnumeric(q2) || isa(q2, 'sym'))
%QVMUL Multiply vector by UnitQuaternion
%
% VT = qvmul(Q, V)
%
% Rotate the vector V by the UnitQuaternion Q.
%
% See also: QQMUL, QINV
%% 2?qv ×(?qv ×v+qwv)+v
%% 2*q.v x ( q.v x v + s v) + v
assert(isreal(q2), 'SMTB:UnitQuaternion:*', 'quat-double: matrix must be real');
assert(size(q2,1) == 3, 'SMTB:UnitQuaternion:*', 'quat-double: matrix must have 3 rows');
if isnumeric(q2)
q2 = double(q2); % force to double
end
% if q1(1).issym || isa(q2, 'sym')
% qp = zeros(3, max(length(q1), numcols(q2)), 'sym');
% else
% qp = zeros(3, max(length(q1), numcols(q2)));
% end
if length(q1) == numcols(q2)
for i=1:length(q1)
q = q1(i) * Quaternion.pure(q2(:,i)) * inv(q1(i));
qp(:,i) = q.v(:);
end
elseif length(q1) == 1
for i=1:numcols(q2)
q = q1 * Quaternion.pure(q2(:,i)) * inv(q1);
qp(:,i) = q.v(:);
end
elseif numcols(q2) == 1
for i=1:length(q1)
q = q1(i) * Quaternion.pure(q2(:)) * inv(q1(i));
qp(:,i) = q.v(:);
end
else
error('SMTB:UnitQuaternion:badarg', 'quaternion-double product: vectors lengths incorrect');
end
else
error('SMTB:UnitQuaternion:badarg', 'quaternion product: incorrect right hand operand');
end
end
function qp = times(q1, q2)
%UnitQuaternion.times Multiply UnitQuaternion's and unitize
%
% R = Q1.*Q2 is a UnitQuaternion object formed by Hamilton product of Q1 and
% Q2. The result is explicitly unitized.
%
% Notes::
% - Overloaded operator '.*'
% - If either, or both, of Q1 or Q2 are vectors, then the result is a vector.
% - if Q1 is a vector (1xN) then R is a vector (1xN) such that R(i) = Q1(i).*Q2.
% - if Q2 is a vector (1xN) then R is a vector (1xN) such that R(i) = Q1.*Q2(i).
% - if both Q1 and Q2 are vectors (1xN) then R is a vector (1xN) such
% that R(i) = Q1(i).*Q2(i).
%
% See also Quaternion.mtimes.
if isa(q2, 'UnitQuaternion')
qp = unit(q1*q2 );
else
error('SMTB:UnitQuaternion:badarg', 'quaternion product .*: incorrect operands');
end
end
function qq = mrdivide(q1, q2)
%UnitQuaternion.mrdivide Divide unit quaternions
%
% R = Q1/Q2 is a UnitQuaternion object formed by Hamilton product of Q1 and
% inv(Q2) where Q1 and Q2 are both UnitQuaternion objects.
%
% Notes::
% - Overloaded operator '/'.
% - If either, or both, of Q1 or Q2 are vectors, then the result is a vector.
% - if Q1 is a vector (1xN) then R is a vector (1xN) such that R(i) = Q1(i)/Q2.
% - if Q2 is a vector (1xN) then R is a vector (1xN) such that R(i) = Q1/Q2(i).
% - if both Q1 and Q2 are vectors (1xN) then R is a vector (1xN) such
% that R(i) = Q1(i)/Q2(i).
%
% See also Quaternion.mtimes, Quaternion.mpower, Quaternion.plus, Quaternion.minus.
if isa(q2, 'UnitQuaternion')
% qq = q1 / q2
% = q1 * qinv(q2)
qq = q1 * inv(q2);
else
error('SMTB:UnitQuaternion:badarg', 'quaternion divide /: incorrect RH operands');
end
end
function qp = rdivide(q1, q2)
%UnitQuaternion.rdivide Divide unit quaternions and unitize
%
% Q1./Q2 is a UnitQuaternion object formed by Hamilton product of Q1 and
% inv(Q2) where Q1 and Q2 are both UnitQuaternion objects. The result is
% explicitly unitized.
%
% Notes::
% - Overloaded operator './'.
% - If either, or both, of Q1 or Q2 are vectors, then the result is a vector.
% - if Q1 is a vector (1xN) then R is a vector (1xN) such that R(i) = Q1(i)./Q2.
% - if Q2 is a vector (1xN) then R is a vector (1xN) such that R(i) = Q1./Q2(i).
% - if both Q1 and Q2 are vectors (1xN) then R is a vector (1xN) such
% that R(i) = Q1(i)./Q2(i).
%
% See also Quaternion.mtimes.
if isa(q2, 'UnitQuaternion')
qp = unit( q1/q2 );
else
error('SMTB:UnitQuaternion:badarg', 'quaternion product .*: incorrect operands');
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% GRAPHICS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function hout = plot(Q, varargin)
%UnitQuaternion.plot Plot a quaternion object
%
% Q.plot(options) plots the UnitQuaternion as an oriented coordinate frame.
%
% H = Q.plot(options) as above but returns a handle which can be used for animation.
%
% Animation::
%
% Firstly, create a plot and keep the the handle as per above.
%
% Q.plot('handle', H) updates the coordinate frame described by the handle H to
% the orientation of Q.
%
% Options::
%
% 'color',C The color to draw the axes, MATLAB colorspec C
% 'frame',F The frame is named {F} and the subscript on the axis labels is F.
% 'view',V Set plot view parameters V=[az el] angles, or 'auto'
% for view toward origin of coordinate frame
% 'handle',h Update the specified handle
%
% These options are passed to trplot, see trplot for more options.
%
% See also trplot.
%axis([-1 1 -1 1 -1 1])
h = trplot( Q.R, varargin{:});
if nargout > 0
hout = h;
end
end
function animate(Q, varargin)
%UnitQuaternion.animate Animate UnitQuaternion object
%
% Q.animate(options) animates a UnitQuaternion array Q (1xN) as a 3D coordinate frame.
%
% Q.animate(QF, options) animates a 3D coordinate frame moving from
% orientation Q to orientation QF.
%
% Options::
% Options are passed to tranimate and include:
%
% 'fps', fps Number of frames per second to display (default 10)
% 'nsteps', n The number of steps along the path (default 50)
% 'axis',A Axis bounds [xmin, xmax, ymin, ymax, zmin, zmax]
% 'movie',M Save frames as files in the folder M
% 'cleanup' Remove the frame at end of animation
% 'noxyz' Don't label the axes
% 'rgb' Color the axes in the order x=red, y=green, z=blue
% 'retain' Retain frames, don't animate
% Additional options are passed through to TRPLOT.
%
% See also tranimate, trplot.
if nargin > 1 && isa(varargin{1}, 'Quaternion')
QF = varargin{1};
tranimate(Q.R, QF.R, varargin{2:end});
else
tranimate(Q.R, varargin{:});
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% TYPE CONVERSION METHODS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function s = char(q)
%UnitQuaternion.char Convert to string
%
% S = Q.char() is a compact string representation of the UnitQuaternion's value
% as a 4-tuple. If Q is a vector then S has one line per element.
%
% Notes::
% - The vector part is delimited by single angle brackets, to differentiate
% from a Quaternion which is delimited by double angle brackets.
%
% See also Quaternion.char.
if length(q) > 1
s = [];
for qq = q
if isempty(s)
s = char(qq);
else
s = char(s, char(qq));
end
end
return
end
function s = render(x)
if isnumeric(x)
s = num2str(x);
elseif isa(x, 'sym')
s = char(x);
end
end
s = [render(q.s), ' < ' ...
render(q.v(1)) ', ' render(q.v(2)) ', ' render(q.v(3)) ' >'];
end
function r = R(q)
%UnitQuaternion.R Convert to SO(3) rotation matrix
%
% R = Q.R() is the equivalent SO(3) orthonormal rotation matrix (3x3). If
% Q represents a sequence (Nx1) then R is 3x3xN.
%
% See also UnitQuaternion.T, UnitQuaternion.SO3.
if ~issym(q(1))
r = zeros(3,3,numel(q));
end
for i=1:numel(q)
r(:,:,i) = q(i).torot();
end
end
function v = issym(q)
v = isa(q.s, 'sym') || isa(q.v, 'sym');
end
function t = T(q)
%UnitQuaternion.T Convert to homogeneous transformation matrix
%
% T = Q.T() is the equivalent SE(3) homogeneous transformation
% matrix (4x4). If Q is a sequence (Nx1) then T is 4x4xN.
%
% Notes:
% - Has a zero translational component.
%
% See also UnitQuaternion.R, UnitQuaternion.SE3.
if ~issym(q(1))
t = zeros(4,4,numel(q));
end
for i=1:numel(q)
t(1:3,1:3,i) = q(i).torot();
t(4,4,i) = 1;
end
end
function obj = SO3(q)
%UnitQuaternion.SO3 Convert to SO3 object
%
% Q.SO3() is an SO3 object with equivalent rotation.
%
% Notes::
% - If Q is a vector then an equivalent vector of SO3 objects is created.
%
% See also UnitQuaternion.SE3, SO3.
obj = repmat(SO3, 1, length(q));
for i=1:numel(q)
obj(i) = SO3( q(i).R );
end
end
function obj = SE3(q)
%UnitQuaternion.SE3 Convert to SE3 object
%
% Q.SE3() is an SE3 object with equivalent rotation and zero translation.
%
% Notes::
% - The translational part of the SE3 object is zero
% - If Q is a vector then an equivalent vector of SE3 objects is created.
%
% See also UnitQuaternion.SE3, SE3.
obj = repmat(SE3, 1, length(q));
for i=1:numel(q)
obj(i) = SE3( q(i).T );
end
end
function rpy = torpy(q, varargin)
%UnitQuaternion.torpy Convert to roll-pitch-yaw angle form.
%
% RPY = Q.torpy(OPTIONS) are the roll-pitch-yaw angles (1x3) corresponding to
% the UnitQuaternion Q. These correspond to rotations about the Z, Y, X axes
% respectively. RPY = [ROLL, PITCH, YAW].
%
% If Q is a vector (1xN) then each row of RPY corresponds to an element of
% the vector.
%
% Options::
% 'deg' Compute angles in degrees (radians default)
% 'xyz' Return solution for sequential rotations about X, Y, Z axes
% 'yxz' Return solution for sequential rotations about Y, X, Z axes
%
% Notes::
% - There is a singularity for the case where P=pi/2 in which case R is arbitrarily
% set to zero and Y is the sum (R+Y).
%
% See also UnitQuaternion.toeul, tr2rpy.
rpy = zeros(length(q), 3);
for i=1:length(q)
rpy(i,:) = tr2rpy( q(i).R, varargin{:} );
end
end
function eul = toeul(q, varargin)
%UnitQuaternion.toeul Convert to roll-pitch-yaw angle form.
%
% EUL = Q.toeul(OPTIONS) are the Euler angles (1x3) corresponding to
% the UnitQuaternion Q. These correspond to rotations about the Z, Y, Z axes
% respectively. EUL = [PHI,THETA,PSI].
%
% If Q is a vector (1xN) then each row of EUL corresponds to an element of
% the vector.
%
% Options::
% 'deg' Compute angles in degrees (radians default)
%
% Notes::
% - There is a singularity for the case where THETA=0 in which case PHI is arbitrarily
% set to zero and PSI is the sum (PHI+PSI).
%
% See also UnitQuaternion.torpy, tr2eul.
eul = zeros(length(q), 3);
for i=1:length(q)
eul(i,:) = tr2eul( q(i).R, varargin{:} );
end
end
function [theta_,n_] = toangvec(q, varargin)
%UnitQuaternion.angvec Convert to angle-vector form
%
% TH = Q.toangvec(OPTIONS) is the rotational angle, about some vector,
% corresponding to this UnitQuaternion. If Q is a UnitQuaternion
% vector (1xN) then TH (1xN) and V (Nx3).
%
% [TH,V] = Q.toangvec(OPTIONS) as above but also returns a unit vector
% parallel to the rotation axis.
%
% Q.toangvec(OPTIONS) prints a compact single line representation of the
% rotational angle and rotation vector corresponding to this UnitQuaternion.
% If Q is a UnitQuaternion vector then print one line per element.
%
% Options::
% 'deg' Display/return angle in degrees rather than radians
%
% Notes::
% - Due to the double cover of the UnitQuaternion, the returned rotation angles
% will be in the interval [-2pi, 2pi).
%
% See also UnitQuaternion.angvec.
opt.deg = false;
opt = tb_optparse(opt, varargin);
if opt.deg
theta = theta * 180/pi;
units = 'deg';
else
units = 'rad';
end
% compute the angle and vector for each quaternion
for i=1:length(q)
qi = q(i);
if norm(qi.v) < 10*eps
% identity quaternion, null rotation
theta(i) = 0;
n(i,:) = [0 0 0];
else
% finite rotation
n(i,:) = unit(qi.v);
theta(i) = 2*atan2( norm(qi.v), qi.s);
end
end
% optionally display them
if nargout == 0
% if no output arguments display the angle and vector
for i=1:length(q)
fprintf('Rotation: %f %s x [%f %f %f]\n', theta(i), units, n(i,:));
end
elseif nargout == 1
theta_ = theta;
elseif nargout == 2
theta_ = theta;
n_ = n;
end
end
function qv = tovec(q)
%UnitQuaternion.tovec Convert to unique 3-vector
%
% V = Q.tovec() is a vector (1x3) that uniquely represents the UnitQuaternion. The scalar
% component can be recovered by 1 - norm(V) and will always be positive.
%
% Notes::
% - UnitQuaternions have double cover of SO(3) so the vector is derived
% from the UnitQuaternion with positive scalar component.
% - This unique and concise vector representation of a UnitQuaternion is often used
% in bundle adjustment problems.
%
% See also UnitQuaternion.vec, UnitQuaternion.qvmul.
if q.s < 0
qv = -q.v;
else
qv = q.v;
end
end
function e = eq(q1, q2)
%UnitQuaternion.eq Test for equality
%
% Q1 == Q2 is true if the two UnitQuaternions represent the same rotation.
%
% Notes::
% - The double mapping of the UnitQuaternion is taken into account, that is,
% UnitQuaternions are equal if Q1.s == -Q1.s && Q1.v == -Q2.v.
% - If Q1 is a vector of UnitQuaternions, each element is compared to
% Q2 and the result is a logical array of the same length as Q1.
% - If Q2 is a vector of UnitQuaternion, each element is compared to
% Q1 and the result is a logical array of the same length as Q2.
% - If Q1 and Q2 are equal length vectors of UnitQuaternion, then the result
% is a logical array of the same length.
% overloaded version for UnitQuaternions to support double mapping
if (numel(q1) == 1) && (numel(q2) == 1)
e = (sum(abs(q1.double - q2.double)) < 100*eps) || (sum(abs(q1.double + q2.double)) < 100*eps);
elseif (numel(q1) > 1) && (numel(q2) == 1)
e = zeros(1, numel(q1), 'logical');
for i=1:numel(q1)
e(i) = q1(i) == q2;
end
elseif (numel(q1) == 1) && (numel(q2) > 1)
e = zeros(1, numel(q2), 'logical');
for i=1:numel(q2)
e(i) = q2(i) == q1;
end
elseif numel(q1) == numel(q2)
e = zeros(1, numel(q1), 'logical');
for i=1:numel(q1)
e(i) = q1(i) == q2(i);
end
else
error('SMTB:UnitQuaternion:eq: badargs');
end
end
function d = angdist(q1, q2, method)
%UnitQuaternion.angdist Distance metric
%
% Q1.angdist(Q2) is a distance metric between two unit quaternions.
%
% Q1.angdist(Q2, M) as above but uses the method specified by M
%
% M = 1 (default): 1 - | < q1, q2> | in [0,pi/2]
% M = 2: acos | <q1, q2> | in [0,1]
%
%
% Note::
% - angdist(q, -q) is equal to zero due to double mapping
%
% See also: SO3.angdist
if ~isa(q2, 'UnitQuaternion')
error('SMTB:UnitQuaternion:badarg', 'angdist: incorrect operand');
end
if nargin == 2
method = 1;
end
switch method
case 1
d = 1 - abs(q1.inner(q2));
case 2
d = acos(abs(q1.inner(q2)));
end
end
end % methods
methods (Access=private)