/
poseutils.py
1136 lines (810 loc) · 38.8 KB
/
poseutils.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
#!/usr/bin/python3
'''Routines to manipulate poses, transformations and points
Most of these are Python wrappers around the written-in-C Python extension
module mrcal._poseutils. Most of the time you want to use this module instead of
touching mrcal._poseutils directly.
All functions are exported into the mrcal module. So you can call these via
mrcal.poseutils.fff() or mrcal.fff(). The latter is preferred.
'''
import numpy as np
import numpysane as nps
# for python3
from functools import reduce
from . import _poseutils
def r_from_R(R, get_gradients=False, out=None):
r"""Compute a Rodrigues vector from a rotation matrix
SYNOPSIS
r = mrcal.r_from_R(R)
rotation_magnitude = nps.mag(r)
rotation_axis = r / rotation_magnitude
Given a rotation specified as a (3,3) rotation matrix, converts it to a
Rodrigues vector, a unit rotation axis scaled by the rotation magnitude, in
radians.
By default this function returns the Rodrigues vector(s) only. If we also want
gradients, pass get_gradients=True. Logic:
if not get_gradients: return r
else: return (r, dr/dR)
This function supports broadcasting fully.
ARGUMENTS
- R: array of shape (3,3). This matrix defines the rotation. It is assumed that
this is a valid rotation (matmult(R,transpose(R)) = I, det(R) = 1), but that
is not checked
- get_gradients: optional boolean. By default (get_gradients=False) we return an
array of Rodrigues vectors. Otherwise we return a tuple of arrays of Rodrigues
vectors and their gradients.
- out: optional argument specifying the destination. By default, new numpy
array(s) are created and returned. To write the results into existing (and
possibly non-contiguous) arrays, specify them with the 'out' kwarg. If
get_gradients: 'out' is the one numpy array we will write into. Else: 'out' is
a tuple of all the output numpy arrays. If 'out' is given, we return the same
arrays passed in. This is the standard behavior provided by numpysane_pywrap.
RETURNED VALUE
If not get_gradients: we return an array of Rodrigues vector(s). Each
broadcasted slice has shape (3,)
If get_gradients: we return a tuple of arrays containing the Rodrigues vector(s)
and the gradients (r, dr/dR)
1. The Rodrigues vector. Each broadcasted slice has shape (3,)
2. The gradient dr/dR. Each broadcasted slice has shape (3,3,3). The first
dimension selects the element of r, and the last two dimensions select the
element of R
"""
if get_gradients:
return _poseutils._r_from_R_withgrad(R, out=out)
return _poseutils._r_from_R(R, out=out)
def R_from_r(r, get_gradients=False, out=None):
r"""Compute a rotation matrix from a Rodrigues vector
SYNOPSIS
r = rotation_axis * rotation_magnitude
R = mrcal.R_from_r(r)
Given a rotation specified as a Rodrigues vector (a unit rotation axis scaled by
the rotation magnitude, in radians.), converts it to a rotation matrix.
By default this function returns the rotation matrices only. If we also want
gradients, pass get_gradients=True. Logic:
if not get_gradients: return R
else: return (R, dR/dr)
This function supports broadcasting fully.
ARGUMENTS
- r: array of shape (3,). The Rodrigues vector that defines the rotation. This is
a unit rotation axis scaled by the rotation magnitude, in radians
- get_gradients: optional boolean. By default (get_gradients=False) we return an
array of rotation matrices. Otherwise we return a tuple of arrays of rotation
matrices and their gradients.
- out: optional argument specifying the destination. By default, new numpy
array(s) are created and returned. To write the results into existing (and
possibly non-contiguous) arrays, specify them with the 'out' kwarg. If
get_gradients: 'out' is the one numpy array we will write into. Else: 'out' is
a tuple of all the output numpy arrays. If 'out' is given, we return the same
arrays passed in. This is the standard behavior provided by numpysane_pywrap.
RETURNED VALUE
If not get_gradients: we return an array of rotation matrices. Each broadcasted
slice has shape (3,3)
If get_gradients: we return a tuple of arrays containing the rotation matrices
and the gradient (R, dR/dr):
1. The rotation matrix. Each broadcasted slice has shape (3,3). This is a valid
rotation: matmult(R,transpose(R)) = I, det(R) = 1
2. The gradient dR/dr. Each broadcasted slice has shape (3,3,3). The first two
dimensions select the element of R, and the last dimension selects the
element of r
"""
if get_gradients:
return _poseutils._R_from_r_withgrad(r, out=out)
return _poseutils._R_from_r(r, out=out)
def rt_from_Rt(Rt, get_gradients=False, out=None):
r"""Compute an rt transformation from a Rt transformation
SYNOPSIS
Rt = nps.glue(rotation_matrix,translation, axis=-2)
print(Rt.shape)
===>
(4,3)
rt = mrcal.rt_from_Rt(Rt)
print(rt.shape)
===>
(6,)
translation = rt[3:]
rotation_magnitude = nps.mag(rt[:3])
rotation_axis = rt[:3] / rotation_magnitude
Converts an Rt transformation to an rt transformation. Both specify a rotation
and translation. An Rt transformation is a (4,3) array formed by nps.glue(R,t,
axis=-2) where R is a (3,3) rotation matrix and t is a (3,) translation vector.
An rt transformation is a (6,) array formed by nps.glue(r,t, axis=-1) where r is
a (3,) Rodrigues vector and t is a (3,) translation vector.
Applied to a point x the transformed result is rotate(x)+t. Given a matrix R,
the rotation is defined by a matrix multiplication. x and t are stored as a row
vector (that's how numpy stores 1-dimensional arrays), but the multiplication
works as if x was a column vector (to match linear algebra conventions). See the
docs for mrcal._transform_point_Rt() for more detail.
By default this function returns the rt transformations only. If we also want
gradients, pass get_gradients=True. Logic:
if not get_gradients: return rt
else: return (rt, dr/dR)
Note that the translation gradient isn't returned: it is always the identity
This function supports broadcasting fully.
ARGUMENTS
- Rt: array of shape (4,3). This matrix defines the transformation. Rt[:3,:] is
a rotation matrix; Rt[3,:] is a translation. It is assumed that the rotation
matrix is a valid rotation (matmult(R,transpose(R)) = I, det(R) = 1), but that
is not checked
- get_gradients: optional boolean. By default (get_gradients=False) we return an
array of rt transformations. Otherwise we return a tuple of arrays of rt
transformations and their gradients.
- out: optional argument specifying the destination. By default, new numpy
array(s) are created and returned. To write the results into existing (and
possibly non-contiguous) arrays, specify them with the 'out' kwarg. If
get_gradients: 'out' is the one numpy array we will write into. Else: 'out' is
a tuple of all the output numpy arrays. If 'out' is given, we return the same
arrays passed in. This is the standard behavior provided by numpysane_pywrap.
RETURNED VALUE
If not get_gradients: we return the rt transformation. Each broadcasted slice
has shape (6,). rt[:3] is a rotation defined as a Rodrigues vector; rt[3:] is a
translation.
If get_gradients: we return a tuple of arrays containing the rt transformation
and the gradient (rt, dr/dR):
1. The rt transformation. Each broadcasted slice has shape (6,)
2. The gradient dr/dR. Each broadcasted slice has shape (3,3,3). The first
dimension selects the element of r, and the last two dimension select the
element of R
"""
if get_gradients:
return _poseutils._rt_from_Rt_withgrad(Rt, out=out)
return _poseutils._rt_from_Rt(Rt, out=out)
def Rt_from_rt(rt, get_gradients=False, out=None):
r"""Compute an Rt transformation from a rt transformation
SYNOPSIS
r = rotation_axis * rotation_magnitude
rt = nps.glue(r,t, axis=-1)
print(rt.shape)
===>
(6,)
Rt = mrcal.Rt_from_rt(rt)
print(Rt.shape)
===>
(4,3)
translation = Rt[3,:]
rotation_matrix = Rt[:3,:]
Converts an rt transformation to an Rt transformation. Both specify a rotation
and translation. An Rt transformation is a (4,3) array formed by nps.glue(R,t,
axis=-2) where R is a (3,3) rotation matrix and t is a (3,) translation vector.
An rt transformation is a (6,) array formed by nps.glue(r,t, axis=-1) where r is
a (3,) Rodrigues vector and t is a (3,) translation vector.
Applied to a point x the transformed result is rotate(x)+t. Given a matrix R,
the rotation is defined by a matrix multiplication. x and t are stored as a row
vector (that's how numpy stores 1-dimensional arrays), but the multiplication
works as if x was a column vector (to match linear algebra conventions). See the
docs for mrcal._transform_point_Rt() for more detail.
By default this function returns the Rt transformations only. If we also want
gradients, pass get_gradients=True. Logic:
if not get_gradients: return Rt
else: return (Rt, dR/dr)
Note that the translation gradient isn't returned: it is always the identity
This function supports broadcasting fully.
ARGUMENTS
- rt: array of shape (6,). This vector defines the input transformation. rt[:3]
is a rotation defined as a Rodrigues vector; rt[3:] is a translation.
- get_gradients: optional boolean. By default (get_gradients=False) we return an
array of Rt transformations. Otherwise we return a tuple of arrays of Rt
transformations and their gradients.
- out: optional argument specifying the destination. By default, new numpy
array(s) are created and returned. To write the results into existing (and
possibly non-contiguous) arrays, specify them with the 'out' kwarg. If
get_gradients: 'out' is the one numpy array we will write into. Else: 'out' is
a tuple of all the output numpy arrays. If 'out' is given, we return the same
arrays passed in. This is the standard behavior provided by numpysane_pywrap.
RETURNED VALUE
If not get_gradients: we return the Rt transformation. Each broadcasted slice
has shape (4,3). Rt[:3,:] is a rotation matrix; Rt[3,:] is a translation. The
matrix R is a valid rotation: matmult(R,transpose(R)) = I and det(R) = 1
If get_gradients: we return a tuple of arrays containing the Rt transformation
and the gradient (Rt, dR/dr):
1. The Rt transformation. Each broadcasted slice has shape (4,3,)
2. The gradient dR/dr. Each broadcasted slice has shape (3,3,3). The first two
dimensions select the element of R, and the last dimension selects the
element of r
"""
if get_gradients:
return _poseutils._Rt_from_rt_withgrad(rt, out=out)
return _poseutils._Rt_from_rt(rt, out=out)
def invert_Rt(Rt, out=None):
r"""Invert an Rt transformation
SYNOPSIS
Rt01 = nps.glue(rotation_matrix,translation, axis=-2)
print(Rt01.shape)
===>
(4,3)
Rt10 = mrcal.invert_Rt(Rt01)
print(x1.shape)
===>
(3,)
x0 = mrcal.transform_point_Rt(Rt01, x1)
print( nps.norm2( x1 - \
mrcal.transform_point_Rt(Rt10, x0) ))
===>
0
Given an Rt transformation to convert a point representated in coordinate system
1 to coordinate system 0 (let's call it Rt01), returns a transformation that
does the reverse: converts a representation in coordinate system 0 to coordinate
system 1 (let's call it Rt10).
Thus if you have a point in coordinate system 1 (let's call it x1), we can
convert it to a representation in system 0, and then back. And we'll get the
same thing out:
x1 == mrcal.transform_point_Rt( mrcal.invert_Rt(Rt01),
mrcal.transform_point_Rt( Rt01, x1 ))
An Rt transformation represents a rotation and a translation. It is a (4,3)
array formed by nps.glue(R,t, axis=-2) where R is a (3,3) rotation matrix and t
is a (3,) translation vector.
Applied to a point x the transformed result is rotate(x)+t. Given a matrix R,
the rotation is defined by a matrix multiplication. x and t are stored as a row
vector (that's how numpy stores 1-dimensional arrays), but the multiplication
works as if x was a column vector (to match linear algebra conventions). See the
docs for mrcal._transform_point_Rt() for more detail.
This function supports broadcasting fully.
ARGUMENTS
- Rt: array of shape (4,3). This matrix defines the transformation. Rt[:3,:] is
a rotation matrix; Rt[3,:] is a translation. It is assumed that the rotation
matrix is a valid rotation (matmult(R,transpose(R)) = I, det(R) = 1), but that
is not checked
- out: optional argument specifying the destination. By default, a new numpy
array is created and returned. To write the results into an existing (and
possibly non-contiguous) array, specify it with the 'out' kwarg. If 'out' is
given, we return the same array passed in. This is the standard behavior
provided by numpysane_pywrap.
RETURNED VALUE
The inverse Rt transformation in an array of shape (4,3).
"""
return _poseutils._invert_Rt(Rt, out=out)
def invert_rt(rt, get_gradients=False, out=None):
r"""Invert an rt transformation
SYNOPSIS
r = rotation_axis * rotation_magnitude
rt01 = nps.glue(r,t, axis=-1)
print(rt01.shape)
===>
(6,)
rt10 = mrcal.invert_rt(rt01)
print(x1.shape)
===>
(3,)
x0 = mrcal.transform_point_rt(rt01, x1)
print( nps.norm2( x1 -
mrcal.transform_point_rt(rt10, x0) ))
===>
0
Given an rt transformation to convert a point representated in coordinate system
1 to coordinate system 0 (let's call it rt01), returns a transformation that
does the reverse: converts a representation in coordinate system 0 to coordinate
system 1 (let's call it rt10).
Thus if you have a point in coordinate system 1 (let's call it x1), we can
convert it to a representation in system 0, and then back. And we'll get the
same thing out:
x1 == mrcal.transform_point_rt( mrcal.invert_rt(rt01),
mrcal.transform_point_rt( rt01, x1 ))
An rt transformation represents a rotation and a translation. It is a (6,) array
formed by nps.glue(r,t, axis=-1) where r is a (3,) Rodrigues vector and t is a
(3,) translation vector.
Applied to a point x the transformed result is rotate(x)+t. x and t are stored
as a row vector (that's how numpy stores 1-dimensional arrays). See the docs for
mrcal._transform_point_rt() for more detail.
By default this function returns the rt transformation only. If we also want
gradients, pass get_gradients=True. Logic:
if not get_gradients: return rt
else: return (rt, drtout_drtin)
Note that the poseutils C API returns only
- dtout_drin
- dtout_dtin
because
- drout_drin is always -I
- drout_dtin is always 0
This Python function, however fills in those constants to return the full (and
more convenient) arrays.
This function supports broadcasting fully.
ARGUMENTS
- rt: array of shape (6,). This vector defines the input transformation. rt[:3]
is a rotation defined as a Rodrigues vector; rt[3:] is a translation.
- get_gradients: optional boolean. By default (get_gradients=False) we return an
array of rt translation. Otherwise we return a tuple of arrays of rt
translations and their gradients.
- out: optional argument specifying the destination. By default, new numpy
array(s) are created and returned. To write the results into existing (and
possibly non-contiguous) arrays, specify them with the 'out' kwarg. If
get_gradients: 'out' is the one numpy array we will write into. Else: 'out' is
a tuple of all the output numpy arrays. If 'out' is given, we return the same
arrays passed in. This is the standard behavior provided by numpysane_pywrap.
RETURNED VALUE
If not get_gradients: we return an array of rt transformation(s). Each
broadcasted slice has shape (6,)
If get_gradients: we return a tuple of arrays containing the rt transformation(s)
and the gradients (rt, drtout/drtin)
1. The rt transformation. Each broadcasted slice has shape (6,)
2. The gradient drtout/drtin. Each broadcasted slice has shape (6,6). The first
dimension selects elements of rtout, and the last dimension selects elements
of rtin
"""
if get_gradients:
return _poseutils._invert_rt_withgrad(rt, out=out)
return _poseutils._invert_rt(rt, out=out)
def compose_Rt(*Rt, out=None):
r"""Compose Rt transformations
SYNOPSIS
Rt10 = nps.glue(rotation_matrix10,translation10, axis=-2)
Rt21 = nps.glue(rotation_matrix21,translation21, axis=-2)
Rt32 = nps.glue(rotation_matrix32,translation32, axis=-2)
print(Rt10.shape)
===>
(4,3)
Rt30 = mrcal.compose_Rt( Rt32, Rt21, Rt10 )
print(x0.shape)
===>
(3,)
print( mrcal.transform_point_Rt(Rt30, x0) -
mrcal.transform_point_Rt(Rt32,
mrcal.transform_point_Rt(Rt21,
mrcal.transform_point_Rt(Rt10, x0))) )
===>
0
Given some number (2 or more, presumably) of Rt transformations, returns
their composition. An Rt transformation is a (4,3) array formed by
nps.glue(R,t, axis=-2) where R is a (3,3) rotation matrix and t is a (3,)
translation vector. This transformation is defined by a matrix multiplication
and an addition. x and t are stored as a row vector (that's how numpy stores
1-dimensional arrays), but the multiplication works as if x was a column vector
(to match linear algebra conventions):
transform_point_Rt(Rt, x) = transpose( matmult(Rt[:3,:], transpose(x)) +
transpose(Rt[3,:]) ) =
= matmult(x, transpose(Rt[:3,:])) +
Rt[3,:]
This function supports broadcasting fully, so we can compose lots of
transformations at the same time.
ARGUMENTS
- *Rt: a list of transformations to compose. Usually we'll be composing two
transformations, but any number could be given here. Each broadcasted slice
has shape (4,3).
- out: optional argument specifying the destination. By default, a new numpy
array is created and returned. To write the results into an existing (and
possibly non-contiguous) array, specify it with the 'out' kwarg. If 'out' is
given, we return the same array passed in. This is the standard behavior
provided by numpysane_pywrap.
RETURNED VALUE
An array of composed Rt transformations. Each broadcasted slice has shape (4,3)
"""
Rt1onwards = reduce( _poseutils._compose_Rt, Rt[1:], _poseutils.identity_Rt() )
return _poseutils._compose_Rt(Rt[0], Rt1onwards, out=out)
def compose_rt(*rt, get_gradients=False, out=None):
r"""Compose rt transformations
SYNOPSIS
r10 = rotation_axis10 * rotation_magnitude10
r21 = rotation_axis21 * rotation_magnitude21
r32 = rotation_axis32 * rotation_magnitude32
print(rt10.shape)
===>
(6,)
rt30 = mrcal.compose_rt( rt32, rt21, rt10 )
print(x0.shape)
===>
(3,)
print( mrcal.transform_point_rt(rt30, x0) -
mrcal.transform_point_rt(rt32,
mrcal.transform_point_rt(rt21,
mrcal.transform_point_rt(rt10, x0))) )
===>
0
print( [arr.shape for arr in mrcal.compose_rt(rt21,rt10,
get_gradients = True)] )
===>
[(6,), (6,6), (6,6)]
Given some number (2 or more, presumably) of rt transformations, returns their
composition. An rt transformation is a (6,) array formed by nps.glue(r,t,
axis=-1) where r is a (3,) Rodrigues vector and t is a (3,) translation vector.
This transformation is defined by a matrix multiplication and an addition. x and
t are stored as a row vector (that's how numpy stores 1-dimensional arrays), but
the multiplication works as if x was a column vector (to match linear algebra
conventions):
transform_point_rt(rt, x) = transpose( matmult(R_from_r(rt[:3]), transpose(x)) +
transpose(rt[3,:]) ) =
= matmult(x, transpose(R_from_r(rt[:3]))) +
rt[3:]
By default this function returns the composed transformations only. If we also
want gradients, pass get_gradients=True. This is supported ONLY if we have
EXACTLY 2 transformations to compose. Logic:
if not get_gradients: return rt=compose(rt0,rt1)
else: return (rt=compose(rt0,rt1), dr/drt0,dr/drt1)
Note that the poseutils C API returns only
- dr_dr0
- dr_dr1
- dt_dr0
- dt_dt1
because
- dr/dt0 is always 0
- dr/dt1 is always 0
- dt/dr1 is always 0
- dt/dt0 is always the identity matrix
This Python function, however fills in those constants to return the full (and
more convenient) arrays.
This function supports broadcasting fully, so we can compose lots of
transformations at the same time.
ARGUMENTS
- *rt: a list of transformations to compose. Usually we'll be composing two
transformations, but any number could be given here. Each broadcasted slice
has shape (6,)
- get_gradients: optional boolean. By default (get_gradients=False) we return an
array of composed transformations. Otherwise we return a tuple of arrays of
composed transformations and their gradients. Gradient reporting is only
supported when exactly two transformations are given
- out: optional argument specifying the destination. By default, new numpy
array(s) are created and returned. To write the results into existing (and
possibly non-contiguous) arrays, specify them with the 'out' kwarg. If
get_gradients: 'out' is the one numpy array we will write into. Else: 'out' is
a tuple of all the output numpy arrays. If 'out' is given, we return the same
arrays passed in. This is the standard behavior provided by numpysane_pywrap.
RETURNED VALUE
If not get_gradients: we return an array of composed rt transformations. Each
broadcasted slice has shape (4,3)
If get_gradients: we return a tuple of arrays containing the composed
transformations and the gradients (rt=compose(rt0,rt1),
drt/drt0,drt/drt1):
1. The composed transformation. Each broadcasted slice has shape (6,)
2. The gradient drt/dr0. Each broadcasted slice has shape (6,6). The first
dimension selects the element of rt, and the last dimension selects the
element of rt0
3. The gradient drt/drt1. Each broadcasted slice has shape (6,6). The first
dimension selects the element of rt, and the last dimension selects the
element of rt1
"""
if get_gradients:
if len(rt) != 2:
raise Exception("compose_rt(..., get_gradients=True) is supported only if exactly 2 inputs are given")
return _poseutils._compose_rt_withgrad(*rt, out=out)
# I convert them all to Rt and compose for efficiency. Otherwise each
# internal composition will convert to Rt, compose, and then convert back to
# rt. The way I'm doing it I convert to rt just once, at the end. This will
# save operations if I'm composing more than 2 transformations
Rt = compose_Rt(*[_poseutils._Rt_from_rt(_rt) for _rt in rt])
return _poseutils._rt_from_Rt( Rt, out=out)
def rotate_point_r(r, x, get_gradients=False, out=None):
r"""Rotate point(s) using a Rodrigues vector
SYNOPSIS
r = rotation_axis * rotation_magnitude
print(r.shape)
===>
(3,)
print(x.shape)
===>
(10,3)
print(mrcal.rotate_point_r(r, x).shape)
===>
(10,3)
print( [arr.shape for arr in mrcal.rotate_point_r(r, x,
get_gradients = True)] )
===>
[(10,3), (10,3,3), (10,3,3)]
Rotate point(s) by a rotation matrix. The Rodrigues vector is converted to a
rotation matrix internally, and then this function is a matrix multiplication. x
is stored as a row vector (that's how numpy stores 1-dimensional arrays), but
the multiplication works as if x was a column vector (to match linear algebra
conventions):
rotate_point_r(r,x) = transpose( matmult(R(r), transpose(x))) =
= matmult(x, transpose(R(r)))
By default this function returns the rotated points only. If we also want
gradients, pass get_gradients=True. Logic:
if not get_gradients: return u=r(x)
else: return (u=r(x),du/dr,du/dx)
This function supports broadcasting fully, so we can rotate lots of points at
the same time and/or apply lots of different rotations at the same time
ARGUMENTS
- r: array of shape (3,). The Rodrigues vector that defines the rotation. This is
a unit rotation axis scaled by the rotation magnitude, in radians
- x: array of shape (3,). The point being rotated
- get_gradients: optional boolean. By default (get_gradients=False) we return an
array of rotated points. Otherwise we return a tuple of arrays of rotated
points and their gradients.
- out: optional argument specifying the destination. By default, new numpy
array(s) are created and returned. To write the results into existing (and
possibly non-contiguous) arrays, specify them with the 'out' kwarg. If
get_gradients: 'out' is the one numpy array we will write into. Else: 'out' is
a tuple of all the output numpy arrays. If 'out' is given, we return the same
arrays passed in. This is the standard behavior provided by numpysane_pywrap.
RETURNED VALUE
If not get_gradients: we return an array of rotated point(s). Each broadcasted
slice has shape (3,)
If get_gradients: we return a tuple of arrays containing the rotated points and
the gradients (u=r(x),du/dr,du/dx):
A tuple (u=r(x),du/dr,du/dx):
1. The rotated point(s). Each broadcasted slice has shape (3,)
2. The gradient du/dr. Each broadcasted slice has shape (3,3). The first
dimension selects the element of u, and the last dimension selects the
element of r
3. The gradient du/dx. Each broadcasted slice has shape (3,3). The first
dimension selects the element of u, and the last dimension selects the
element of x
"""
if not get_gradients:
return _poseutils._rotate_point_r(r,x, out=out)
return _poseutils._rotate_point_r_withgrad(r,x, out=out)
def rotate_point_R(R, x, get_gradients=False, out=None):
r"""Rotate point(s) using a rotation matrix
SYNOPSIS
r = rotation_axis * rotation_magnitude
R = mrcal.R_from_r(r)
print(R.shape)
===>
(3,3)
print(x.shape)
===>
(10,3)
print( mrcal.rotate_point_R(R, x).shape )
===>
(10,3)
print( [arr.shape for arr in mrcal.rotate_point_R(R, x,
get_gradients = True)] )
===>
[(10,3), (10,3,3,3), (10,3,3)]
Rotate point(s) by a rotation matrix. This is a matrix multiplication. x is
stored as a row vector (that's how numpy stores 1-dimensional arrays), but the
multiplication works as if x was a column vector (to match linear algebra
conventions):
rotate_point_R(R,x) = transpose( matmult(R, transpose(x))) =
= matmult(x, transpose(R))
By default this function returns the rotated points only. If we also want
gradients, pass get_gradients=True. Logic:
if not get_gradients: return u=R(x)
else: return (u=R(x),du/dR,du/dx)
This function supports broadcasting fully, so we can rotate lots of points at
the same time and/or apply lots of different rotations at the same time
ARGUMENTS
- R: array of shape (3,3). This matrix defines the rotation. It is assumed that
this is a valid rotation (matmult(R,transpose(R)) = I, det(R) = 1), but that
is not checked
- x: array of shape (3,). The point being rotated
- get_gradients: optional boolean. By default (get_gradients=False) we return an
array of rotated points. Otherwise we return a tuple of arrays of rotated
points and their gradients.
- out: optional argument specifying the destination. By default, new numpy
array(s) are created and returned. To write the results into existing (and
possibly non-contiguous) arrays, specify them with the 'out' kwarg. If
get_gradients: 'out' is the one numpy array we will write into. Else: 'out' is
a tuple of all the output numpy arrays. If 'out' is given, we return the same
arrays passed in. This is the standard behavior provided by numpysane_pywrap.
RETURNED VALUE
If not get_gradients: we return an array of rotated point(s). Each broadcasted
slice has shape (3,)
If get_gradients: we return a tuple of arrays containing the rotated points and
the gradients (u=R(x),du/dR,du/dx):
1. The rotated point(s). Each broadcasted slice has shape (3,)
2. The gradient du/dR. Each broadcasted slice has shape (3,3,3). The first
dimension selects the element of u, and the last 2 dimensions select the
element of R
3. The gradient du/dx. Each broadcasted slice has shape (3,3). The first
dimension selects the element of u, and the last dimension selects the
element of x
"""
if not get_gradients:
return _poseutils._rotate_point_R(R,x, out=out)
return _poseutils._rotate_point_R_withgrad(R,x, out=out)
def transform_point_rt(rt, x, get_gradients=False, out=None):
r"""Transform point(s) using an rt transformation
SYNOPSIS
r = rotation_axis * rotation_magnitude
rt = nps.glue(r,t, axis=-1)
print(rt.shape)
===>
(6,)
print(x.shape)
===>
(10,3)
print( mrcal.transform_point_rt(rt, x).shape )
===>
(10,3)
print( [arr.shape
for arr in mrcal.transform_point_rt(rt, x,
get_gradients = True)] )
===>
[(10,3), (10,3,6), (10,3,3)]
Transform point(s) by an rt transformation: a (6,) array formed by
nps.glue(r,t, axis=-1) where r is a (3,) Rodrigues vector and t is a (3,)
translation vector. This transformation is defined by a matrix multiplication
and an addition. x and t are stored as a row vector (that's how numpy stores
1-dimensional arrays), but the multiplication works as if x was a column vector
(to match linear algebra conventions):
transform_point_rt(rt, x) = transpose( matmult(R_from_r(rt[:3]), transpose(x)) +
transpose(rt[3,:]) ) =
= matmult(x, transpose(R_from_r(rt[:3]))) +
rt[3:]
By default this function returns the transformed points only. If we also want
gradients, pass get_gradients=True. Logic:
if not get_gradients: return u=rt(x)
else: return (u=rt(x),du/drt,du/dx)
This function supports broadcasting fully, so we can transform lots of points at
the same time and/or apply lots of different transformations at the same time
ARGUMENTS
- rt: array of shape (6,). This vector defines the transformation. rt[:3] is a
rotation defined as a Rodrigues vector; rt[3:] is a translation.
- x: array of shape (3,). The point being transformed
- get_gradients: optional boolean. By default (get_gradients=False) we return an
array of transformed points. Otherwise we return a tuple of arrays of
transformed points and their gradients.
- out: optional argument specifying the destination. By default, new numpy
array(s) are created and returned. To write the results into existing (and
possibly non-contiguous) arrays, specify them with the 'out' kwarg. If
get_gradients: 'out' is the one numpy array we will write into. Else: 'out' is
a tuple of all the output numpy arrays. If 'out' is given, we return the same
arrays passed in. This is the standard behavior provided by numpysane_pywrap.
RETURNED VALUE
If not get_gradients: we return an array of transformed point(s). Each
broadcasted slice has shape (3,)
If get_gradients: we return a tuple of arrays of transformed points and the
gradients (u=rt(x),du/drt,du/dx):
1. The transformed point(s). Each broadcasted slice has shape (3,)
2. The gradient du/drt. Each broadcasted slice has shape (3,6). The first
dimension selects the element of u, and the last dimension selects the
element of rt
3. The gradient du/dx. Each broadcasted slice has shape (3,3). The first
dimension selects the element of u, and the last dimension selects the
element of x
"""
if not get_gradients:
return _poseutils._transform_point_rt(rt,x, out=out)
return _poseutils._transform_point_rt_withgrad(rt,x, out=out)
def transform_point_Rt(Rt, x, get_gradients=False, out=None):
r"""Transform point(s) using an Rt transformation
SYNOPSIS
Rt = nps.glue(rotation_matrix,translation, axis=-2)
print(Rt.shape)
===>
(4,3)
print(x.shape)
===>
(10,3)
print( mrcal.transform_point_Rt(Rt, x).shape )
===>
(10,3)
print( [arr.shape
for arr in mrcal.transform_point_Rt(Rt, x,
get_gradients = True)] )
===>
[(10,3), (10,3,4,3), (10,3,3)]
Transform point(s) by an Rt transformation: a (4,3) array formed by
nps.glue(R,t, axis=-2) where R is a (3,3) rotation matrix and t is a (3,)
translation vector. This transformation is defined by a matrix multiplication
and an addition. x and t are stored as a row vector (that's how numpy stores
1-dimensional arrays), but the multiplication works as if x was a column vector
(to match linear algebra conventions):
transform_point_Rt(Rt, x) = transpose( matmult(Rt[:3,:], transpose(x)) +
transpose(Rt[3,:]) ) =
= matmult(x, transpose(Rt[:3,:])) +
Rt[3,:]
By default this function returns the transformed points only. If we also want
gradients, pass get_gradients=True. Logic:
if not get_gradients: return u=Rt(x)
else: return (u=Rt(x),du/dRt,du/dx)
This function supports broadcasting fully, so we can transform lots of points at
the same time and/or apply lots of different transformations at the same time
ARGUMENTS
- Rt: array of shape (4,3). This matrix defines the transformation. Rt[:3,:] is
a rotation matrix; Rt[3,:] is a translation. It is assumed that the rotation
matrix is a valid rotation (matmult(R,transpose(R)) = I, det(R) = 1), but that
is not checked
- x: array of shape (3,). The point being transformed
- get_gradients: optional boolean. By default (get_gradients=False) we return an
array of transformed points. Otherwise we return a tuple of arrays of
transformed points and their gradients.
- out: optional argument specifying the destination. By default, new numpy
array(s) are created and returned. To write the results into existing (and
possibly non-contiguous) arrays, specify them with the 'out' kwarg. If
get_gradients: 'out' is the one numpy array we will write into. Else: 'out' is
a tuple of all the output numpy arrays. If 'out' is given, we return the same
arrays passed in. This is the standard behavior provided by numpysane_pywrap.
RETURNED VALUE
If not get_gradients: we return an array of transformed point(s). Each
broadcasted slice has shape (3,)
If get_gradients: we return a tuple of arrays of transformed points and the
gradients (u=Rt(x),du/dRt,du/dx):
1. The transformed point(s). Each broadcasted slice has shape (3,)
2. The gradient du/dRt. Each broadcasted slice has shape (3,4,3). The first
dimension selects the element of u, and the last 2 dimensions select the
element of Rt
3. The gradient du/dx. Each broadcasted slice has shape (3,3). The first
dimension selects the element of u, and the last dimension selects the
element of x
"""
if not get_gradients:
return _poseutils._transform_point_Rt(Rt,x, out=out)
return _poseutils._transform_point_Rt_withgrad(Rt,x, out=out)
@nps.broadcast_define( ((4,),),
(3,3) )