-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposes_euler.py
1238 lines (993 loc) · 43.3 KB
/
poses_euler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import math_utility
import poses_quat
import numpy as np
import scipy.linalg.matfuncs
import scipy.spatial.transform
import scipy.stats
"""
Operations and functions related to SE(3) poses / poses pdf expressed as 6d vector euler + 3d
Notes
-----
Implementation based on [1]. The last updated version of the paper can be found here :
https://github.com/jlblancoc/tutorial-se3-manifold
Pose pdf are represented are dict with keys "pose_mean" and "pose_cov".
Point pdf are are represented are dict with keys "mean" and "cov".
References
----------
.. [1] "J.-L. Blanco. A tutorial on se (3) transformation parameterizations and
on-manifold optimization. University of Malaga, Tech. Rep, 3, 2010." (Last update : 11/05/2020)
.. [2] "Y. Breux, MpIC_oldVersion_noApprox" (in doc folder)
.. [3] "Breux, Yohan, André Mas, and Lionel Lapierre.
On-manifold Probabilistic ICP: Application to Underwater Karst Exploration." (2021)
"""
def composePoseEuler(q1, q2):
""" Composition of two poses expressed in Euler + 3D (6D vector)
Parameters
----------
q1 : array
first pose (Euler + 3D)
q2 : array
second pose (Euler + 3D)
Returns
-------
array
Pose resulting from the composition
Notes
-----
See [1], section 5.1
"""
rot1 = scipy.spatial.transform.Rotation.from_euler('ZYX',q1[0:3])
rot2 = scipy.spatial.transform.Rotation.from_euler('ZYX',q2[0:3])
t1 = q1[3:6]
t2 = q2[3:6]
rot_12 = scipy.spatial.transform.Rotation.from_matrix(np.matmul(rot1.as_matrix(), rot2.as_matrix()))
t_12 = rot1.apply(t2) + t1
return np.block([rot_12.as_euler('ZYX'), t_12])
def composePoseEuler_array(q1, q2_array):
"""
Composition of one pose with an array of poses in Euler + 3D (6D vector)
Parameters
----------
q1 : array
first pose (Euler + 3D)
q2_array : 2D array
array of second poses (Euler + 3D)
Returns
-------
2D array
Poses resulting from the composition of q1 with each pose in q2_array
Notes
-----
See [1], section 5.1
"""
res = np.empty((q2_array.shape[0],6))
rot1 = scipy.spatial.transform.Rotation.from_euler('ZYX',q1[0:3])
rot2 = scipy.spatial.transform.Rotation.from_euler('ZYX',q2_array[:,0:3])
t1 = q1[3:6]
t2 = q2_array[:,3:6]
rot_12 = scipy.spatial.transform.Rotation.from_matrix(np.einsum('ij,kjl->kil', rot1.as_matrix(), rot2.as_matrix())) #np.matmul(rot1.as_matrix(), rot2.as_matrix()))
res[:,0:3] = scipy.spatial.transform.Rotation.from_matrix(np.einsum('ij,kjl->kil', rot1.as_matrix(), rot2.as_matrix())).as_euler('ZYX')
res[:,3:6] = rot1.apply(t2) + t1
return res
def composePoseEulerPoint(q_poseEuler, x):
"""
Compose a pose (Euler + 3D) with a 3D point
Parameters
----------
q_poseEuler : array
pose (Euler + 3D)
x : array
3D point
Returns
-------
array
Resulting point from the pose-point composition
Notes
-----
See [1], section 3.1
"""
rot = scipy.spatial.transform.Rotation.from_euler('ZYX', q_poseEuler[0:3])
t = q_poseEuler[3:6]
x_composed = rot.apply(x) + t
return x_composed
def inverseComposePoseEuler(q_poseEuler1, q_poseEuler2):
"""
Inverse pose composition in Euler + 3D (q_poseEuler1 - q_poseEuler2)
Parameters
----------
q_poseEuler1 : array
first pose (Euler + 3D)
q_poseEuler2 : array
second pose (Euler + 3D)
Returns
-------
array
Result of the inverse composition, corresponding to
the relative coords of q_poseEuler1 wrt q_poseEuler2
Notes
-----
See [1], section 5.1 / 6.1. Note that q1 - q2 = -q2 + q1
"""
rot1 = scipy.spatial.transform.Rotation.from_euler('ZYX', q_poseEuler1[0:3])
rot2 = scipy.spatial.transform.Rotation.from_euler('ZYX', q_poseEuler2[0:3]).inv()
t1 = q_poseEuler1[3:6]
t2 = -rot2.apply(q_poseEuler2[3:6])
rot_12 = scipy.spatial.transform.Rotation.from_matrix(np.matmul(rot1.as_matrix(), rot2.as_matrix()))
t_12 = rot1.apply(t2) + t1
return np.block([rot_12.as_euler('ZYX'), t_12])
def inverseComposePoseEuler_array(q_poseEuler1_array, q_poseEuler2):
"""
Inverse pose composition of an array of poses with a pose (q_poseEuler1_array - q_poseEuler2)
Parameters
----------
q_poseEuler1_array : 2D array
first poses (Euler + 3D)
q_poseEuler2 : array
second pose (Euler + 3D)
Returns
-------
2D array
Result of the inverse composition, corresponding to
the relative coords of each pose in q_poseEuler1 wrt q_poseEuler2
Notes
-----
See [1], section 5.1 / 6.1. Note that q1 - q2 = -q2 + q1
"""
res = np.empty((q_poseEuler1_array.shape[0],6))
rot1 = scipy.spatial.transform.Rotation.from_euler('ZYX', q_poseEuler1_array[:,0:3])
rot2 = scipy.spatial.transform.Rotation.from_euler('ZYX', q_poseEuler2[0:3]).inv()
t1 = q_poseEuler1_array[:,3:6]
t2 = -rot2.apply(q_poseEuler2[3:6])
rot1_mat = rot1.as_matrix()
res[:,0:3] = scipy.spatial.transform.Rotation.from_matrix(np.einsum('...ij,jk->...ik', rot1_mat, rot2.as_matrix())).as_euler('ZYX')
res[:,3:6] = np.einsum('...ij,j->...i', rot1_mat, t2) + t1
return res
def inverseComposePoseEulerPoint(q_poseEuler, x):
"""
Inverse pose-point composition (x - q_poseEuler) in Euler + 3D
Parameters
----------
q_poseEuler : array
pose (Euler + 3D angles)
x : array
3D point
Returns
-------
array
Resulting 3D point, corresponding to the relative coords of x wrt q_poseEuler
Notes
-----
See [1], section 4.1.
"""
rot_T = scipy.spatial.transform.Rotation.from_euler('ZYX', q_poseEuler[0:3]).inv()
t = q_poseEuler[3:6]
x_composed = rot_T.apply(x - t)
return x_composed
def inverseComposePoseEulerPoint_opt(q_poseEuler_array, x):
"""
Inverse pose-point composition of a point x with each pose in q_poseEuler_arra (in Euler + 3D)
Parameters
----------
q_poseEuler_array : 2D array
array of poses (Euler + 3D)
x : array
3D point
Returns
-------
2D array
Resulting array 3D point, corresponding to the relative coords of x wrt poses in q_poseEuler_array
Notes
-----
See [1], section 4.1.
"""
rot_T = scipy.spatial.transform.Rotation.from_euler('ZYX', q_poseEuler_array[:,0:3]).inv()
t = q_poseEuler_array[:,3:6]
x_composed = rot_T.apply(x - t)
return x_composed
def inverseComposePoseEulerPoint_opt_array(q_poseEuler_tensor, x_array):
"""
Inverse pose-point composition of an array of points x_array
with a 2D array of poses in Euler + 3D angles.
Parameters
----------
q_poseEuler_tensor : 3D array
Tensor of poses (samples X points X pose) (Euler + 3D)
x_array : 2D array
3D points
Returns
-------
3D array
Resulting 3D points, corresponding to the relative coords of each x in x_array
wrt to each pose in q_poseEuler_tensor
Notes
-----
Functions specifically implemented for mypicp algo.
The 3D array input q_poseEuler_array has dimensions #samples X #points X 6D.
The samples correspond to the samples of poses in the robot trajectory from the CUT algorithm.
The points correspond to the point clouds.
See [1], section 4.1. and [2], section 5.3
"""
# Reshape the n_samples X n_points X 6 3D array of q_poseEuler to n_samples * n_points X 6 2D array
n_samples = q_poseEuler_tensor.shape[0]
n_points = q_poseEuler_tensor.shape[1]
q_posesEuler_reshaped = q_poseEuler_tensor.reshape((n_samples * n_points, 6), order='F')
rot_T = scipy.spatial.transform.Rotation.from_euler('ZYX', q_posesEuler_reshaped[:, 0:3]).inv()
t = q_poseEuler_tensor[:,:,3:6]
# Dimension : n_samples * n_points X 3 X 1
diff = (x_array - t).reshape((n_samples*n_points,3,1),order='F')
# Multiply slice by slice ie R_{i,j}(x_i - t_{i,j})
# Reshape the results back to a 3D array with samples as the depth dimension
x_composed_array = np.einsum('...ij,...jh->...ih', rot_T.as_matrix(), diff,optimize=True).reshape((n_samples, n_points, 3),order='F')
return x_composed_array
def inverseComposePoseEulerPoint_opt_array_association(q_poseEuler_tensor, x_array):
"""
Inverse pose-point composition of an array of points x_array
with a 2D array of poses in Euler + 3D.
Parameters
----------
q_poseEuler_tensor : array
Tensor of poses (samples X points X pose) (Euler + 3D)
x : array
3D point
Returns
-------
array
Resulting 3D point, corresponding to the relative coords of x wrt q_poseEuler
Notes
-----
Functions specifically implemented for mypicp algo (for association step).
The 3D array input q_poseEuler_array has dimensions #samples X #points X 6D.
The samples correspond to the samples of poses in the robot trajectory from the CUT algorithm.
The points correspond to the point clouds.
See [1], section 4.1. and [2], section 5.3
"""
# Reshape the n_samples X n_points X 6 3D array of q_poseEuler to n_samples * n_points X 6 2D array
n_samples = q_poseEuler_tensor.shape[0]
n_points_q = q_poseEuler_tensor.shape[1]
n_points_x = x_array.shape[0]
q_posesEuler_reshaped = q_poseEuler_tensor.reshape((n_samples * n_points_q, 6), order='C')
rot_T = scipy.spatial.transform.Rotation.from_euler('ZYX', q_posesEuler_reshaped[:, 0:3]).inv()
t = q_posesEuler_reshaped[:,3:6]
# Dimension of diff : n_samples X n_points_q X n_points_x (x_array) X 3
# We have diff[k][i][j] = x_array[j] - t[k][i]
diff = (x_array - t[:,None]).reshape((n_samples,n_points_q,n_points_x, 3))
# Multiply slice by slice ie R_{i,k}(x_j - t_{i,k})
x_composed_array = np.einsum('kilm,kijm->kijl', rot_T.as_matrix().reshape((n_samples,n_points_q,3,3)), diff,optimize=True)
return x_composed_array
def fromPoseEulerToPoseQuat(q_poseEuler):
"""
Convert a pose in Euler + 3D to a pose in quaternion + 3D
Parameters
----------
q_poseEuler : array
pose in Euler + 3D
Returns
-------
array
pose in quaternion + 3D
Notes
-----
See [1], section 2.1
"""
rot = scipy.spatial.transform.Rotation.from_euler('ZYX', q_poseEuler[0:3])
quat = rot.as_quat()
# numpy quaternion -> [qx qy qz qr] , mrpt quat is [qr qx qy qz]
quat_arr = poses_quat.fromqxyzrToqrxyz(quat)
q_poseQuat = np.block([quat_arr, np.array(q_poseEuler[3:6])])
return q_poseQuat
def fromPoseEulerToPoseQuat_array(q_poseEuler_array):
"""
Convert an array of poses in Euler + 3D to an array of poses in quaternion + 3D
Parameters
----------
q_poseEuler_array : 2D array
array of poses (euler + 3D)
Returns
-------
2D array
array of poses (quaternion + 3D)
Notes
-----
See [1], section 2.1
"""
rot = scipy.spatial.transform.Rotation.from_euler('ZYX', q_poseEuler_array[:,0:3])
# numpy quaternion -> [qx qy qz qr] , mrpt quat is [qr qx qy qz]
q = rot.as_quat()
q_poseQuat_array = np.empty((q_poseEuler_array.shape[0],7))
q_poseQuat_array[:, 0] = q[:, 3]
q_poseQuat_array[:, 1] = q[:, 0]
q_poseQuat_array[:, 2] = q[:, 1]
q_poseQuat_array[:, 3] = q[:, 2]
q_poseQuat_array[:, 4:7] = q_poseEuler_array[:, 3:6]
return q_poseQuat_array
''' Convert Pose in Quaternion+3D to Pose in Euler+3D '''
def fromPoseQuatToPoseEuler(q_poseQuat):
"""
Convert a pose in quaternion + 3D to pose in euler + 3D
Parameters
----------
q_poseQuat : array
pose (quaternion + 3D)
Returns
-------
array
pose (euler + 3D)
Notes
-----
See [1], section 2.2
"""
quat = poses_quat.fromqrxyzToqxyzr(q_poseQuat)
# Quaternion is normalized here
quat[0:4] /= np.linalg.norm(quat[0:4])
rot = scipy.spatial.transform.Rotation.from_quat(quat)
q_poseEuler = np.block([np.array(rot.as_euler('ZYX')), np.array(q_poseQuat[4:7])])
return q_poseEuler
def fromPoseQuatToPoseEuler_array(q_poseQuat_array):
"""
Convert an array of poses in quaternion + 3D to an array of poses in euler + 3D
Parameters
----------
q_poseQuat_array : 2D array
array of poses (quaternion + 3D)
Returns
-------
2D array
array of poses (euler + 3D)
Notes
-----
See [1], section 2.2
"""
quat = poses_quat.fromqrxyzToqxyzr_array(q_poseQuat_array)
# Quaternion is normalized here
rot = scipy.spatial.transform.Rotation.from_quat(quat)
q_poseEuler_array = np.empty((q_poseQuat_array.shape[0], 6))
q_poseEuler_array[:,0:3] = rot.as_euler('ZYX')
q_poseEuler_array[:,3:6] = q_poseQuat_array[:,4:7]
return q_poseEuler_array
def computeJacobian_quatToEuler(q_poseQuat):
"""
Compute the jacobian of the quaternion + 3D pose to euler + 3D pose convertion.
Parameters
----------
q_poseQuat : array
quaternion + 3D pose at which the jacobian is computed
Results
-------
2D array
Returns a 6 X 7 matrix
Notes
-----
See [1], section 2.2.2
"""
# Compute the jacobian of euler angles wrt to quaternion
determinant = q_poseQuat[0]*q_poseQuat[2] - q_poseQuat[1]*q_poseQuat[3]
jacobian_euler_quat = np.zeros((3, 4))
if(determinant > 0.49999):
num = 2./(q_poseQuat[0]**2 + q_poseQuat[1]**2)
jacobian_euler_quat[0][0] = num*q_poseQuat[1]
jacobian_euler_quat[0][2] = -num*q_poseQuat[0]
elif(determinant < -0.49999):
num = 2. / (q_poseQuat[0] ** 2 + q_poseQuat[1] ** 2)
jacobian_euler_quat[0][0] = -num * q_poseQuat[1]
jacobian_euler_quat[0][2] = num * q_poseQuat[0]
else:
x_sqr = q_poseQuat[1]**2
y_sqr = q_poseQuat[2]**2
z_sqr = q_poseQuat[3]**2
r_z = q_poseQuat[0]*q_poseQuat[3]
r_x = q_poseQuat[0]*q_poseQuat[1]
x_y = q_poseQuat[1]*q_poseQuat[2]
y_z = q_poseQuat[2]*q_poseQuat[3]
a = 1. - 2.*(y_sqr + z_sqr)
a_sqr = a**2
b = 2.*(r_z + x_y)
c = 1. - 2.*(x_sqr + y_sqr)
c_sqr = c**2
d = 2.*(r_x + y_z)
atan_prime_yaw = 1./(1. + (b/a)**2)
atan_prime_roll = 1./(1. + (d/c)**2)
asin_prime = 1./np.sqrt(1. - 4.*determinant**2)
jacobian_euler_quat[0][0] = 2.*q_poseQuat[3]*atan_prime_yaw/a
jacobian_euler_quat[0][1] = 2.*q_poseQuat[2]*atan_prime_yaw/a
jacobian_euler_quat[0][2] = 2.*((q_poseQuat[1]*a + 2.*q_poseQuat[2]*b)/a_sqr)*atan_prime_yaw
jacobian_euler_quat[0][3] = 2.*((q_poseQuat[0]*a + 2.*q_poseQuat[3]*b)/a_sqr)*atan_prime_yaw
jacobian_euler_quat[1][0] = 2.*q_poseQuat[2]*asin_prime
jacobian_euler_quat[1][1] = -2. * q_poseQuat[3] * asin_prime
jacobian_euler_quat[1][2] = 2. * q_poseQuat[0] * asin_prime
jacobian_euler_quat[1][3] = -2. * q_poseQuat[1] * asin_prime
jacobian_euler_quat[2][0] = 2.*(q_poseQuat[1]/c)*atan_prime_roll
jacobian_euler_quat[2][1] = 2. * ((q_poseQuat[0]*c + 2.*q_poseQuat[1]*d)/c_sqr) * atan_prime_roll
jacobian_euler_quat[2][2] = 2. * ((q_poseQuat[3]*c + 2.*q_poseQuat[2]*d)/c_sqr) * atan_prime_roll
jacobian_euler_quat[2][3] = 2. * (q_poseQuat[2] / c) * atan_prime_roll
J_norm = poses_quat.jacobianQuatNormalization(q_poseQuat[0:4])
jacobian = np.block([[jacobian_euler_quat@J_norm , np.zeros((3,3))],
[np.zeros((3,4)), np.eye(3)]
])
return jacobian
def computeJacobian_quatToEuler_array(q_poseQuat_array):
"""
Compute the jacobian of the quaternion + 3D pose to euler + 3D pose convertion
at each pose in q_poseQuat_array
Parameters
----------
q_poseQuat_array : 2D array
Array of quaternion + 3D poses at which the jacobian is computed
Results
-------
3D array
Returns a q_poseQuat_array.shape[0] X 6 X 7 tensor
Notes
-----
See [1], section 2.2.2
"""
# Compute the jacobian of euler angles wrt to quaternion
determinant = q_poseQuat_array[:,0]*q_poseQuat_array[:,2] - q_poseQuat_array[:,1]*q_poseQuat_array[:,3]
jacobian_euler_quat = np.zeros((q_poseQuat_array.shape[0],3, 4))
num = 2./(np.power(q_poseQuat_array[:,0],2) + np.power(q_poseQuat_array[:,1],2))
num_poseQuat0 = num*q_poseQuat_array[:,0]
num_poseQuat1 = num * q_poseQuat_array[:, 1]
x_sqr = np.power(q_poseQuat_array[:,1],2)
y_sqr = np.power(q_poseQuat_array[:,2],2)
z_sqr = np.power(q_poseQuat_array[:,3],2)
r_z = q_poseQuat_array[:,0] * q_poseQuat_array[:,3]
r_x = q_poseQuat_array[:,0] * q_poseQuat_array[:,1]
x_y = q_poseQuat_array[:,1] * q_poseQuat_array[:,2]
y_z = q_poseQuat_array[:,2] * q_poseQuat_array[:,3]
a = 1. - 2. * (y_sqr + z_sqr)
a_inv = 1./a
a_sqr = np.power(a,2)
a_sqr_inv = 1./a_sqr
b = 2. * (r_z + x_y)
c = 1. - 2. * (x_sqr + y_sqr)
c_inv = 1./c
c_sqr_inv = 1./np.power(c,2)
d = 2. * (r_x + y_z)
atan_prime_yaw = 1. / (1. + np.power(b*a_inv,2))
atan_prime_roll = 1. / (1. + np.power(d*c_inv,2))
asin_prime = 1. / np.sqrt(1. - 4. * np.power(determinant,2))
a00 = 2.*q_poseQuat_array[:,3]*atan_prime_yaw*a_inv
a01 = 2.*q_poseQuat_array[:,2]*atan_prime_yaw*a_inv
a02 = 2.*((q_poseQuat_array[:,1]*a + 2.*q_poseQuat_array[:,2]*b)*a_sqr_inv)*atan_prime_yaw
a03 = 2. * ((q_poseQuat_array[:,0] * a + 2. * q_poseQuat_array[:,3] * b)* a_sqr_inv) * atan_prime_yaw
a10 = 2.*q_poseQuat_array[:,2]*asin_prime
a11 = -2. * q_poseQuat_array[:,3] * asin_prime
a12 = 2. * q_poseQuat_array[:,0] * asin_prime
a13 = -2. * q_poseQuat_array[:,1] * asin_prime
a20 = 2.*(q_poseQuat_array[:,1]*c_inv)*atan_prime_roll
a21 = 2. * ((q_poseQuat_array[:,0]*c + 2.*q_poseQuat_array[:,1]*d)*c_sqr_inv) * atan_prime_roll
a22 = 2. * ((q_poseQuat_array[:,3]*c + 2.*q_poseQuat_array[:,2]*d)*c_sqr_inv) * atan_prime_roll
a23 = 2. * (q_poseQuat_array[:,2]*c_inv) * atan_prime_roll
for k in range(0,determinant.shape[0]):
det = determinant[k]
if(det> 0.49999):
jacobian_euler_quat[k,0,0] = num_poseQuat1[k]
jacobian_euler_quat[k,0,2] = -num_poseQuat0[k]
elif(det< -0.49999):
jacobian_euler_quat[k,0,0] = -num_poseQuat1[k]
jacobian_euler_quat[k,0,2] = num_poseQuat0[k]
else:
jacobian_euler_quat[k,0,0] = a00[k]
jacobian_euler_quat[k,0,1] = a01[k]
jacobian_euler_quat[k,0,2] = a02[k]
jacobian_euler_quat[k,0,3] = a03[k]
jacobian_euler_quat[k,1,0] = a10[k]
jacobian_euler_quat[k,1,1] = a11[k]
jacobian_euler_quat[k,1,2] = a12[k]
jacobian_euler_quat[k,1,3] = a13[k]
jacobian_euler_quat[k,2,0] = a20[k]
jacobian_euler_quat[k,2,1] = a21[k]
jacobian_euler_quat[k,2,2] = a22[k]
jacobian_euler_quat[k,2,3] = a23[k]
J_norm = poses_quat.jacobianQuatNormalization_array(q_poseQuat_array[:,0:4])
jacobian = np.zeros((q_poseQuat_array.shape[0],6,7))
jacobian[:,0:3,0:4] = np.einsum('kij,kjl->kil',jacobian_euler_quat,J_norm,optimize=True)
ones = np.full(q_poseQuat_array.shape[0],1.)
jacobian[:,3,4] = ones
jacobian[:,4,5] = ones
jacobian[:,5,6] = ones
return jacobian
def computeJacobian_eulerToQuat(q_poseEuler):
"""
Compute the jacobian of the euler + 3D pose to quaternion + 3D pose convertion.
Parameters
----------
q_poseEuler : array
euler + 3D pose at which the jacobian is computed
Results
-------
2D array
Returns a 7 X 6 matrix
Notes
-----
See [1], section 2.1.2
"""
half_yaw = 0.5*q_poseEuler[0]
half_pitch = 0.5*q_poseEuler[1]
half_roll = 0.5*q_poseEuler[2]
cos_yaw = np.cos(half_yaw)
sin_yaw = np.sin(half_yaw)
cos_pitch = np.cos(half_pitch)
sin_pitch = np.sin(half_pitch)
cos_roll = np.cos(half_roll)
sin_roll = np.sin(half_roll)
ccc = cos_roll*cos_pitch*cos_yaw
ccs = cos_roll*cos_pitch*sin_yaw
csc = cos_roll*sin_pitch*cos_yaw
css = cos_roll*sin_pitch*sin_yaw
scs = sin_roll*cos_pitch*sin_yaw
scc = sin_roll*cos_pitch*cos_yaw
ssc = sin_roll*sin_pitch*cos_yaw
sss = sin_roll*sin_pitch*sin_yaw
jacobian_quat_euler = 0.5*np.array([[ssc - ccs, scs - csc, css - scc],
[-(csc + scs), -(ssc + ccs), ccc + sss],
[scc - css, ccc - sss, ccs - ssc],
[ccc + sss, -(css + scc), -(csc + scs)]])
jacobian = np.block([[jacobian_quat_euler, np.zeros((4,3))],
[np.zeros((3,3)), np.eye(3)]
])
return jacobian
def computeJacobian_eulerToQuat_array(q_poseEuler_array):
"""
Compute the jacobian of the euler + 3D pose to quaternion + 3D pose convertion
at each pose in q_poseEuler_array.
Parameters
----------
q_poseEuler_array : 2D array
euler + 3D array of poses at which the jacobian is computed
Results
-------
3D array
Returns a q_poseEuler_array.shape[0] X 7 X 6 tensor
Notes
-----
See [1], section 2.1.2
"""
half_yaw = 0.5*q_poseEuler_array[:,0]
half_pitch = 0.5*q_poseEuler_array[:,1]
half_roll = 0.5*q_poseEuler_array[:,2]
cos_yaw = np.cos(half_yaw)
sin_yaw = np.sin(half_yaw)
cos_pitch = np.cos(half_pitch)
sin_pitch = np.sin(half_pitch)
cos_roll = np.cos(half_roll)
sin_roll = np.sin(half_roll)
ccc = cos_roll*cos_pitch*cos_yaw
ccs = cos_roll*cos_pitch*sin_yaw
csc = cos_roll*sin_pitch*cos_yaw
css = cos_roll*sin_pitch*sin_yaw
scs = sin_roll*cos_pitch*sin_yaw
scc = sin_roll*cos_pitch*cos_yaw
ssc = sin_roll*sin_pitch*cos_yaw
sss = sin_roll*sin_pitch*sin_yaw
a11 = ssc - ccs
a12 = scs - csc
a13 = css - scc
a21 = -(csc + scs)
a22 = -(ssc + ccs)
a23 = ccc + sss
a32 = ccc - sss
a42 = -(css + scc)
jacobian_array = np.zeros((q_poseEuler_array.shape[0],7,6))
jacobian_array[:, 0, 0] = 0.5*a11
jacobian_array[:, 0, 1] = 0.5*a12
jacobian_array[:, 0, 2] = 0.5*a13
jacobian_array[:, 1, 0] = 0.5*a21
jacobian_array[:, 1, 1] = 0.5*a22
jacobian_array[:, 1, 2] = 0.5*a23
jacobian_array[:, 2, 0] = -0.5*a13
jacobian_array[:, 2, 1] = 0.5*a32
jacobian_array[:, 2, 2] = -0.5*a11
jacobian_array[:, 3, 0] = 0.5*a23
jacobian_array[:, 3, 1] = 0.5*a42
jacobian_array[:, 3, 2] = 0.5*a21
ones = np.full(q_poseEuler_array.shape[0], 1.)
jacobian_array[:, 4, 3] = ones
jacobian_array[:, 5, 4] = ones
jacobian_array[:, 6, 5] = ones
return jacobian_array
def computeJacobianEuler_composePose(q_poseEuler1, q_poseEuler2, q_poseEuler_compose_mean):
"""
Compute the jacobian of euler + 3D pose composition
Parameters
----------
q_poseEuler1 : array
first euler + 3D pose at which the jacobian is computed
q_poseEuler2 : array
second euler + 3D pose at which the jacobian is computed
q_poseEuler_compose_mean : array
pre-computed composition q_poseEuler1 + q_poseEuler2
Returns
-------
jacobian_q1 : 2D array
Jacobian relative to the first pose (6 X 6 matrix)
jacobian_q2 : 2D array
Jacobian relative to the second pose (6 X 6 matrix)
Notes
-----
See [1], section 5.1.2
"""
q_poseQuat1 = fromPoseEulerToPoseQuat(q_poseEuler1)
q_poseQuat2 = fromPoseEulerToPoseQuat(q_poseEuler2)
q_poseQuat_compose = fromPoseEulerToPoseQuat(q_poseEuler_compose_mean)
jacobian_quat_q1, jacobian_quat_q2 = poses_quat.computeJacobianQuat_composePose(q_poseQuat1, q_poseQuat2)
jacobian_quatToEuler_q_compose = computeJacobian_quatToEuler(q_poseQuat_compose)
jacobian_eulerToQuat_q1 = computeJacobian_eulerToQuat(q_poseEuler1)
jacobian_eulerToQuat_q2 = computeJacobian_eulerToQuat(q_poseEuler2)
jacobian_q1 = np.matmul(jacobian_quatToEuler_q_compose, np.matmul(jacobian_quat_q1, jacobian_eulerToQuat_q1))
jacobian_q2 = np.matmul(jacobian_quatToEuler_q_compose, np.matmul(jacobian_quat_q2, jacobian_eulerToQuat_q2))
return jacobian_q1, jacobian_q2
def computeJacobianEuler_composePose_array(q_poseEuler1, q_poseEuler2_array, q_poseEuler_compose_mean_array):
"""
Compute the jacobian of euler + 3D pose composition
at pose q_poseEuler1 and at each pose in q_poseEuler2_array
Parameters
----------
q_poseEuler1 : array
first euler + 3D pose at which the jacobian is computed
q_poseEuler2_array : 2D array
array of second euler + 3D poses at which the jacobian is computed
q_poseEuler_compose_mean_array : 2D array
pre-computed composition of q_poseEuler1 + q_poseEuler2
for each q_poseEuler2 in q_poseEuler2_array
Returns
-------
jacobian_q1 : 3D array
Jacobian relative to the first pose (q_poseEuler2_array.shape[0] X 6 X 6 tensor)
jacobian_q2 : 3D array
Jacobian relative to the second pose (q_poseEuler2_array.shape[0] X 6 X 6 tensor)
Notes
-----
See [1], section 5.1.2
"""
q_poseQuat1 = fromPoseEulerToPoseQuat(q_poseEuler1)
q_poseQuat2_array = fromPoseEulerToPoseQuat_array(q_poseEuler2_array)
q_poseQuat_compose_array = fromPoseEulerToPoseQuat_array(q_poseEuler_compose_mean_array)
jacobian_quat_q1, jacobian_quat_q2 = poses_quat.computeJacobianQuat_composePose_array(q_poseQuat1, q_poseQuat2_array)
jacobian_quatToEuler_q_compose = computeJacobian_quatToEuler_array(q_poseQuat_compose_array)
jacobian_eulerToQuat_q1 = computeJacobian_eulerToQuat(q_poseEuler1)
jacobian_eulerToQuat_q2 = computeJacobian_eulerToQuat_array(q_poseEuler2_array)
return np.einsum('kij,kjl->kil',jacobian_quatToEuler_q_compose, np.einsum('kij,jl->kil',jacobian_quat_q1, jacobian_eulerToQuat_q1)), \
np.einsum('kij,kjl->kil',jacobian_quatToEuler_q_compose, np.einsum('kij,kjl->kil',jacobian_quat_q2, jacobian_eulerToQuat_q2))
def computeJacobianEuler_composePosePDFPoint_pose(q_mean, point_mean):
"""
Compute jacobian of the pose-point composition relative to the pose (in euler + 3D)
Parameters
----------
q_mean : array
euler + 3D pose at which the jacobian is computed
point_mean : array
3D point at which the jacobian is computed
Returns
-------
2D array
jacobian relative to the pose (3 X 6 matrix)
Notes
-----
See [1], section 3.1.2
"""
cos_yaw = np.cos(q_mean[0])
sin_yaw = np.sin(q_mean[0])
cos_pitch = np.cos(q_mean[1])
sin_pitch = np.sin(q_mean[1])
cos_roll = np.cos(q_mean[2])
sin_roll = np.sin(q_mean[2])
a11 = -point_mean[0]*sin_yaw*cos_pitch - point_mean[1]*(sin_yaw*sin_pitch*sin_roll + cos_yaw*cos_roll) + point_mean[2]*(-sin_yaw*sin_pitch*cos_roll + cos_yaw*sin_roll)
a12 = -point_mean[0]*cos_yaw*sin_pitch + point_mean[1]*cos_yaw*cos_pitch*sin_roll + point_mean[2]*cos_yaw*cos_pitch*cos_roll
a13 = point_mean[1]*(cos_yaw*sin_pitch*cos_roll + sin_yaw*sin_roll) + point_mean[2]*(-cos_yaw*sin_pitch*sin_roll + sin_yaw*cos_roll)
a21 = point_mean[0]*cos_yaw*cos_pitch + point_mean[1]*(cos_yaw*sin_pitch*sin_roll - sin_yaw*cos_roll) + point_mean[2]*(cos_yaw*sin_pitch*cos_roll + sin_yaw*sin_roll)
a22 = -point_mean[0]*sin_yaw*sin_pitch + point_mean[1]*sin_yaw*cos_pitch*sin_roll + point_mean[2]*sin_yaw*cos_pitch*cos_roll
a23 = point_mean[1]*(sin_yaw*sin_pitch*cos_roll - cos_yaw*sin_roll) - point_mean[2]*(sin_yaw*sin_pitch*sin_roll + cos_yaw*cos_roll)
a31 = 0.
a32 = -(point_mean[0]*cos_pitch + point_mean[1]*sin_pitch*sin_roll + point_mean[2]*sin_pitch*cos_roll)
a33 = point_mean[1]*cos_pitch*cos_roll - point_mean[2]*cos_pitch*sin_roll
return np.block([np.array([[a11,a12,a13],
[a21,a22,a23],
[a31,a32,a33]]),np.eye(3)])
def computeJacobianEuler_composePosePDFPoint_pose_array(q_mean, point_mean_array):
"""
Compute jacobian of the pose-point composition relative to the pose (in euler + 3D)
evaluated at each point in point_mean_array
Parameters
----------
q_mean : array
euler + 3D pose at which the jacobian is computed
point_mean_array : 2D array
array of 3D points at which the jacobian is computed
Returns
-------
3D array
jacobians relative to the pose (point_mean_array.shape[0] X 3 X 6 tensor)
Notes
-----
See [1], section 3.1.2
"""
cos_yaw = np.cos(q_mean[0])
sin_yaw = np.sin(q_mean[0])
cos_pitch = np.cos(q_mean[1])
sin_pitch = np.sin(q_mean[1])
cos_roll = np.cos(q_mean[2])
sin_roll = np.sin(q_mean[2])
jacobian = np.zeros((point_mean_array.shape[0], 3, 6))
jacobian[:,0,0] = -point_mean_array[:,0]*sin_yaw*cos_pitch - point_mean_array[:,1]*(sin_yaw*sin_pitch*sin_roll + cos_yaw*cos_roll) + point_mean_array[:,2]*(-sin_yaw*sin_pitch*cos_roll + cos_yaw*sin_roll)
jacobian[:,0,1] = -point_mean_array[:,0]*cos_yaw*sin_pitch + point_mean_array[:,1]*cos_yaw*cos_pitch*sin_roll + point_mean_array[:,2]*cos_yaw*cos_pitch*cos_roll
jacobian[:,0,2] = point_mean_array[:,1]*(cos_yaw*sin_pitch*cos_roll + sin_yaw*sin_roll) + point_mean_array[:,2]*(-cos_yaw*sin_pitch*sin_roll + sin_yaw*cos_roll)
jacobian[:,1,0] = point_mean_array[:,0]*cos_yaw*cos_pitch + point_mean_array[:,1]*(cos_yaw*sin_pitch*sin_roll - sin_yaw*cos_roll) + point_mean_array[:,2]*(cos_yaw*sin_pitch*cos_roll + sin_yaw*sin_roll)
jacobian[:,1,1] = -point_mean_array[:,0]*sin_yaw*sin_pitch + point_mean_array[:,1]*sin_yaw*cos_pitch*sin_roll + point_mean_array[:,2]*sin_yaw*cos_pitch*cos_roll
jacobian[:,1,2] = point_mean_array[:,1]*(sin_yaw*sin_pitch*cos_roll - cos_yaw*sin_roll) - point_mean_array[:,2]*(sin_yaw*sin_pitch*sin_roll + cos_yaw*cos_roll)
jacobian[:,2,0] = 0.
jacobian[:,2,1] = -(point_mean_array[:,0]*cos_pitch + point_mean_array[:,1]*sin_pitch*sin_roll + point_mean_array[:,2]*sin_pitch*cos_roll)
jacobian[:,2,2] = point_mean_array[:,1]*cos_pitch*cos_roll - point_mean_array[:,2]*cos_pitch*sin_roll
ones = np.full(point_mean_array.shape[0],1.)
jacobian[:,0,3] = ones
jacobian[:,1,4] = ones
jacobian[:,2,5] = ones
return jacobian
def composePosePDFEuler(q_posePDFEuler1, q_posePDFEuler2):
"""
Compute pose pdf (mean + covariance) composition in euler + 3D
Parameters
----------
q_posePDFEuler1 : dict
first pose pdf
q_posePDFEuler1 : dict
second pose pdf
Results
-------
dict
pose pdf of q_posePDFEuler1 + q_posePDFEuler2
Notes
-----
See [1], section 5.1
"""
q_poseEuler_compose_mean = composePoseEuler(q_posePDFEuler1['pose_mean'], q_posePDFEuler2['pose_mean'])
jacobian_q1, jacobian_q2 = computeJacobianEuler_composePose(q_posePDFEuler1['pose_mean'], q_posePDFEuler2['pose_mean'], q_poseEuler_compose_mean)
q_poseEuler_compose_cov = np.matmul(jacobian_q1, np.matmul(q_posePDFEuler1['pose_cov'], np.transpose(jacobian_q1))) +\
np.matmul(jacobian_q2, np.matmul(q_posePDFEuler2['pose_cov'], np.transpose(jacobian_q2)))
q_posePDFEuler_compose = {'pose_mean' : q_poseEuler_compose_mean, 'pose_cov' : q_poseEuler_compose_cov}
return q_posePDFEuler_compose
def composePosePDFEuler_array(q_posePDFEuler1, q_posePDFEuler2_array):
"""
Compute pose pdf (mean + covariance) composition in euler + 3D for each
pose in q_posePDFEuler2_array
Parameters
----------
q_posePDFEuler1 : dict
first pose pdf
q_posePDFEuler2_array : array of dict
second poses pdf
Returns
-------
array of dict
poses pdf of q_posePDFEuler1 + q2 for each q2 in q_posePDFEuler2_array
Notes
-----
See [1], section 5.1
"""
q_poseEuler_compose_mean = composePoseEuler_array(q_posePDFEuler1['pose_mean'], q_posePDFEuler2_array['pose_mean'])
jacobian_q1, jacobian_q2 = computeJacobianEuler_composePose_array(q_posePDFEuler1['pose_mean'], q_posePDFEuler2_array['pose_mean'], q_poseEuler_compose_mean)
q_poseEuler_compose_cov = np.einsum('kij,kjl->kil', jacobian_q1, np.einsum('ij,klj->kil',q_posePDFEuler1['pose_cov'],jacobian_q1,optimize=True),optimize=True) + \
np.einsum('kij,kjl->kil', jacobian_q2,
np.einsum('kij,klj->kil', q_posePDFEuler2_array['pose_cov'], jacobian_q2,
optimize=True), optimize=True)
q_posePDFEuler_compose = {'pose_mean' : q_poseEuler_compose_mean, 'pose_cov' : q_poseEuler_compose_cov}
return q_posePDFEuler_compose
def composePosePDFEulerPoint(q_posePDFEuler, point):
"""
Compute composition of pose pdf - point pdf
Parameters
----------
q_posePDFEuler : dict
pose pdf in euler + 3D
point : dict
3D point pdf
Returns
-------
dict
the point pdf q_posePDFEuler + point
Notes
-----
See [1], section 3.1.1
"""
jacobian_pose = computeJacobianEuler_composePosePDFPoint_pose(q_posePDFEuler['pose_mean'], point['mean'])
jacobian_point = scipy.spatial.transform.Rotation.from_euler('ZYX', q_posePDFEuler['pose_mean'][0:3]).as_matrix()
cov = jacobian_pose@q_posePDFEuler['pose_cov']@jacobian_pose.T + jacobian_point@point['cov']@jacobian_point.T
mean = composePoseEulerPoint(q_posePDFEuler['pose_mean'], point['mean'])
return {'mean': mean, 'cov': cov}
def composePosePDFEulerPoint_array(q_posePDFEuler, point_array):
"""
Compute composition of pose pdf - point pdf for each point in point_array
Parameters
----------
q_posePDFEuler : dict
pose pdf in euler + 3D
point_array: array of dict
array of 3D point pdf
Returns