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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
|
#!/usr/bin/env python3
# Copyright (c) 2017-2023 California Institute of Technology ("Caltech"). U.S.
# Government sponsorship acknowledged. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
r'''Python-wrap the mrcal geometry routines
'''
import sys
import os
import numpy as np
import numpysane as nps
import numpysane_pywrap as npsp
docstring_module = '''Low-level routines to manipulate poses, transformations and points
This is the written-in-C Python extension module. Most of the time you want to
use the mrcal.poseutils wrapper module instead of this module directly. Any
functions not prefixed with "_" are meant to be called directly, without the
wrapper.
All functions are exported into the mrcal module. So you can call these via
mrcal._poseutils.fff() or mrcal.fff(). The latter is preferred.
'''
m = npsp.module( name = "_poseutils_npsp",
docstring = docstring_module,
header = r'''
#include "poseutils.h"
#include <string.h>
''')
m.function( "identity_R",
"""Return an identity rotation matrix
SYNOPSIS
print( mrcal.identity_R() )
===>
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
As with all the poseutils functions, the output can be written directly into a
(possibly-non-contiguous) array, by specifying the destination in the 'out'
kwarg """,
args_input = (),
prototype_input = (),
prototype_output = (3,3),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_identity_R_full( (double*)data_slice__output,
strides_slice__output[0],
strides_slice__output[1] );
return true;
'''})
m.function( "identity_r",
"""Return an identity Rodrigues rotation
SYNOPSIS
print( mrcal.identity_r() )
===>
[0. 0. 0.]
As with all the poseutils functions, the output can be written directly into a
(possibly-non-contiguous) array, by specifying the destination in the 'out'
kwarg""",
args_input = (),
prototype_input = (),
prototype_output = (3,),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_identity_r_full( (double*)data_slice__output,
strides_slice__output[0] );
return true;
'''})
m.function( "identity_Rt",
"""Return an identity Rt transformation
SYNOPSIS
print( mrcal.identity_Rt() )
===>
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]]
As with all the poseutils functions, the output can be written directly into a
(possibly-non-contiguous) array, by specifying the destination in the 'out'
kwarg""",
args_input = (),
prototype_input = (),
prototype_output = (4,3),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_identity_Rt_full( (double*)data_slice__output,
strides_slice__output[0],
strides_slice__output[1] );
return true;
'''})
m.function( "identity_rt",
"""Return an identity rt transformation
SYNOPSIS
print( mrcal.identity_rt() )
===>
[0. 0. 0. 0. 0. 0.]
As with all the poseutils functions, the output can be written directly into a
(possibly-non-contiguous) array, by specifying the destination in the 'out'
kwarg""",
args_input = (),
prototype_input = (),
prototype_output = (6,),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_identity_rt_full( (double*)data_slice__output,
strides_slice__output[0] );
return true;
'''})
m.function( "_rotate_point_R",
"""Rotate a point using a rotation matrix
This is an internal function. You probably want mrcal.rotate_point_R(). See the
docs for that function for details.
""",
args_input = ('R', 'x'),
prototype_input = ((3,3), (3,)),
prototype_output = (3,),
extra_args = (("int", "inverted", "false", "p"),),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_rotate_point_R_full( (double*)data_slice__output,
strides_slice__output[0],
NULL,0,0,0,
NULL,0,0,
(const double*)data_slice__R,
strides_slice__R[0],
strides_slice__R[1],
(const double*)data_slice__x,
strides_slice__x[0],
*inverted );
return true;
'''},
)
m.function( "_rotate_point_R_withgrad",
"""Rotate a point using a rotation matrix; report the result and gradients
This is an internal function. You probably want mrcal.rotate_point_R(). See the
docs for that function for details.
""",
args_input = ('R', 'x'),
prototype_input = ((3,3), (3,)),
prototype_output = ((3,), (3,3,3), (3,3)),
extra_args = (("int", "inverted", "false", "p"),),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_rotate_point_R_full( (double*)data_slice__output0,
strides_slice__output0[0],
(double*)data_slice__output1,
strides_slice__output1[0],
strides_slice__output1[1],
strides_slice__output1[2],
(double*)data_slice__output2,
strides_slice__output2[0],
strides_slice__output2[1],
(const double*)data_slice__R,
strides_slice__R[0],
strides_slice__R[1],
(const double*)data_slice__x,
strides_slice__x[0],
*inverted );
return true;
'''},
)
m.function( "_rotate_point_r",
"""Rotate a point using a Rodrigues vector
This is an internal function. You probably want mrcal.rotate_point_r(). See the
docs for that function for details.
""",
args_input = ('r', 'x'),
prototype_input = ((3,), (3,)),
prototype_output = (3,),
extra_args = (("int", "inverted", "false", "p"),),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_rotate_point_r_full( (double*)data_slice__output,
strides_slice__output[0],
NULL,0,0,
NULL,0,0,
(const double*)data_slice__r,
strides_slice__r[0],
(const double*)data_slice__x,
strides_slice__x[0],
*inverted);
return true;
'''},
)
m.function( "_rotate_point_r_withgrad",
"""Rotate a point using a Rodrigues vector; report the result and gradients
This is an internal function. You probably want mrcal.rotate_point_r(). See the
docs for that function for details.
""",
args_input = ('r', 'x'),
prototype_input = ((3,), (3,)),
prototype_output = ((3,), (3,3), (3,3)),
extra_args = (("int", "inverted", "false", "p"),),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_rotate_point_r_full( (double*)data_slice__output0,
strides_slice__output0[0],
(double*)data_slice__output1,
strides_slice__output1[0],
strides_slice__output1[1],
(double*)data_slice__output2,
strides_slice__output2[0],
strides_slice__output2[1],
(const double*)data_slice__r,
strides_slice__r[0],
(const double*)data_slice__x,
strides_slice__x[0],
*inverted);
return true;
'''},
)
m.function( "_transform_point_Rt",
"""Transform a point using an Rt transformation
This is an internal function. You probably want mrcal.transform_point_Rt(). See
the docs for that function for details.
""",
args_input = ('Rt', 'x'),
prototype_input = ((4,3), (3,)),
prototype_output = (3,),
extra_args = (("int", "inverted", "false", "p"),),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_transform_point_Rt_full( (double*)data_slice__output,
strides_slice__output[0],
NULL,0,0,0,
NULL,0,0,
(const double*)data_slice__Rt,
strides_slice__Rt[0],
strides_slice__Rt[1],
(const double*)data_slice__x,
strides_slice__x[0],
*inverted );
return true;
'''},
)
m.function( "_transform_point_Rt_withgrad",
"""Transform a point using an Rt transformation; report the result and gradients
This is an internal function. You probably want mrcal.transform_point_Rt(). See
the docs for that function for details.
""",
args_input = ('Rt', 'x'),
prototype_input = ((4,3), (3,)),
prototype_output = ((3,), (3,4,3), (3,3)),
extra_args = (("int", "inverted", "false", "p"),),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_transform_point_Rt_full( (double*)data_slice__output0,
strides_slice__output0[0],
(double*)data_slice__output1,
strides_slice__output1[0],
strides_slice__output1[1],
strides_slice__output1[2],
(double*)data_slice__output2,
strides_slice__output2[0],
strides_slice__output2[1],
(const double*)data_slice__Rt,
strides_slice__Rt[0],
strides_slice__Rt[1],
(const double*)data_slice__x,
strides_slice__x[0],
*inverted );
return true;
'''},
)
m.function( "_transform_point_rt",
"""Transform a point using an rt transformation
This is an internal function. You probably want mrcal.transform_point_rt(). See
the docs for that function for details.
""",
args_input = ('rt', 'x'),
prototype_input = ((6,), (3,)),
prototype_output = (3,),
extra_args = (("int", "inverted", "false", "p"),),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_transform_point_rt_full( (double*)data_slice__output,
strides_slice__output[0],
NULL,0,0,
NULL,0,0,
(const double*)data_slice__rt,
strides_slice__rt[0],
(const double*)data_slice__x,
strides_slice__x[0],
*inverted );
return true;
'''},
)
m.function( "_transform_point_rt_withgrad",
"""Transform a point using an rt transformation; report the result and gradients
This is an internal function. You probably want mrcal.transform_point_rt(). See
the docs for that function for details.
""",
args_input = ('rt', 'x'),
prototype_input = ((6,), (3,)),
prototype_output = ((3,), (3,6), (3,3)),
extra_args = (("int", "inverted", "false", "p"),),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_transform_point_rt_full( (double*)data_slice__output0,
strides_slice__output0[0],
(double*)data_slice__output1,
strides_slice__output1[0],
strides_slice__output1[1],
(double*)data_slice__output2,
strides_slice__output2[0],
strides_slice__output2[1],
(const double*)data_slice__rt,
strides_slice__rt[0],
(const double*)data_slice__x,
strides_slice__x[0],
*inverted );
return true;
'''},
)
m.function( "_r_from_R",
"""Compute a Rodrigues vector from a rotation matrix
This is an internal function. You probably want mrcal.r_from_R(). See the docs
for that function for details.
""",
args_input = ('R',),
prototype_input = ((3,3),),
prototype_output = (3,),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_r_from_R_full(
(double*)data_slice__output,strides_slice__output[0],
NULL,0,0,0,
(const double*)data_slice__R,strides_slice__R[0], strides_slice__R[1] );
return true;
'''}
)
m.function( "_r_from_R_withgrad",
"""Compute a Rodrigues vector from a rotation matrix
This is an internal function. You probably want mrcal.r_from_R(). See the docs
for that function for details.
""",
args_input = ('R',),
prototype_input = ((3,3),),
prototype_output = ((3,),(3,3,3)),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_r_from_R_full(
(double*)data_slice__output0,strides_slice__output0[0],
(double*)data_slice__output1,strides_slice__output1[0], strides_slice__output1[1],strides_slice__output1[2],
(const double*)data_slice__R,strides_slice__R[0], strides_slice__R[1] );
return true;
'''}
)
m.function( "_R_from_r",
"""Compute a rotation matrix from a Rodrigues vector
This is an internal function. You probably want mrcal.R_from_r(). See the docs
for that function for details.
""",
args_input = ('r',),
prototype_input = ((3,),),
prototype_output = (3,3),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_R_from_r_full(
(double*)data_slice__output, strides_slice__output[0], strides_slice__output[1],
NULL,0,0,0,
(const double*)data_slice__r, strides_slice__r[0] );
return true;
'''}
)
m.function( "_R_from_r_withgrad",
"""Compute a rotation matrix from a Rodrigues vector
This is an internal function. You probably want mrcal.R_from_r(). See the docs
for that function for details.
""",
args_input = ('r',),
prototype_input = ((3,),),
prototype_output = ((3,3), (3,3,3)),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_R_from_r_full(
(double*)data_slice__output0,strides_slice__output0[0], strides_slice__output0[1],
(double*)data_slice__output1,strides_slice__output1[0], strides_slice__output1[1],strides_slice__output1[2],
(const double*)data_slice__r, strides_slice__r[0] );
return true;
'''}
)
m.function( "_invert_R",
"""Invert a rotation matrix
This is an internal function. You probably want mrcal.invert_R(). See the docs
for that function for details.
""",
args_input = ('R',),
prototype_input = ((3,3),),
prototype_output = (3,3),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_invert_R_full( (double*)data_slice__output,
strides_slice__output[0], strides_slice__output[1],
(const double*)data_slice__R,
strides_slice__R[0], strides_slice__R[1] );
return true;
'''},
)
m.function( "_rt_from_Rt",
"""Compute an rt transformation from a Rt transformation
This is an internal function. You probably want mrcal.rt_from_Rt(). See the docs
for that function for details.
""",
args_input = ('Rt',),
prototype_input = ((4,3),),
prototype_output = (6,),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_rt_from_Rt_full(
(double*)data_slice__output,strides_slice__output[0],
NULL,0,0,0,
(const double*)data_slice__Rt,strides_slice__Rt[0], strides_slice__Rt[1] );
return true;
'''}
)
m.function( "_rt_from_Rt_withgrad",
"""Compute an rt transformation from a Rt transformation
This is an internal function. You probably want mrcal.rt_from_Rt(). See the docs
for that function for details.
""",
args_input = ('Rt',),
prototype_input = ((4,3),),
prototype_output = ((6,), (3,3,3)),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_rt_from_Rt_full(
(double*)data_slice__output0,strides_slice__output0[0],
(double*)data_slice__output1,strides_slice__output1[0], strides_slice__output1[1],strides_slice__output1[2],
(const double*)data_slice__Rt,strides_slice__Rt[0], strides_slice__Rt[1] );
return true;
'''}
)
m.function( "_Rt_from_rt",
"""Compute an Rt transformation from a rt transformation
This is an internal function. You probably want mrcal.Rt_from_rt(). See the docs
for that function for details.
""",
args_input = ('rt',),
prototype_input = ((6,),),
prototype_output = (4,3),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_Rt_from_rt_full(
(double*)data_slice__output, strides_slice__output[0],strides_slice__output[1],
NULL,0,0,0,
(const double*)data_slice__rt, strides_slice__rt[0] );
return true;
'''}
)
m.function( "_Rt_from_rt_withgrad",
"""Compute an Rt transformation from a rt transformation
This is an internal function. You probably want mrcal.Rt_from_rt(). See the docs
for that function for details.
""",
args_input = ('rt',),
prototype_input = ((6,),),
prototype_output = ((4,3), (3,3,3)),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_Rt_from_rt_full(
(double*)data_slice__output0, strides_slice__output0[0],strides_slice__output0[1],
(double*)data_slice__output1,strides_slice__output1[0], strides_slice__output1[1],strides_slice__output1[2],
(const double*)data_slice__rt, strides_slice__rt[0] );
return true;
'''}
)
m.function( "_invert_Rt",
"""Invert an Rt transformation
This is an internal function. You probably want mrcal.invert_Rt(). See the docs
for that function for details.
""",
args_input = ('Rt',),
prototype_input = ((4,3),),
prototype_output = (4,3),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_invert_Rt_full( (double*)data_slice__output,
strides_slice__output[0], strides_slice__output[1],
(const double*)data_slice__Rt,
strides_slice__Rt[0], strides_slice__Rt[1] );
return true;
'''},
)
m.function( "_invert_rt",
"""Invert an rt transformation
This is an internal function. You probably want mrcal.invert_rt(). See the docs
for that function for details.
""",
args_input = ('rt',),
prototype_input = ((6,),),
prototype_output = (6,),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_invert_rt_full( (double*)data_slice__output,
strides_slice__output[0],
NULL,0,0,
NULL,0,0,
(const double*)data_slice__rt,
strides_slice__rt[0] );
return true;
'''},
)
m.function( "_invert_rt_withgrad",
"""Invert an rt transformation
This is an internal function. You probably want mrcal.invert_rt(). See the docs
for that function for details.
Note that the C library returns limited gradients:
- It returns dtout_drin,dtout_dtin only because
- drout_drin always -I
- drout_dtin always 0
THIS function combines these into a full drtout_drtin array
""",
args_input = ('rt',),
prototype_input = ((6,),),
prototype_output = ((6,), (6,6)),
# output1 is drtout/drtin = [ drout/drin drout/dtin ]
# [ dtout/drin dtout/dtin ]
#
# = [ -I 0 ]
# [ dtout/drin dtout/dtin ]
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_invert_rt_full( (double*)data_slice__output0,
strides_slice__output0[0],
&item__output1(3,0),
strides_slice__output1[0], strides_slice__output1[1],
&item__output1(3,3),
strides_slice__output1[0], strides_slice__output1[1],
(const double*)data_slice__rt,
strides_slice__rt[0] );
for(int i=0; i<3; i++)
for(int j=0; j<6; j++)
item__output1(i,j) = 0;
item__output1(0,0) = -1.;
item__output1(1,1) = -1.;
item__output1(2,2) = -1.;
return true;
'''},
)
m.function( "_compose_Rt",
"""Composes two Rt transformations
This is an internal function. You probably want mrcal.compose_Rt(). See the docs
for that function for details. This internal function differs from compose_Rt():
- It supports exactly two arguments, while compose_Rt() can compose N
transformations
""",
args_input = ('Rt0', 'Rt1'),
prototype_input = ((4,3,), (4,3,)),
prototype_output = (4,3),
extra_args = (("int", "inverted0", "false", "p"),
("int", "inverted1", "false", "p"),),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_compose_Rt_full( (double*)data_slice__output,
strides_slice__output[0], strides_slice__output[1],
(const double*)data_slice__Rt0,
strides_slice__Rt0[0], strides_slice__Rt0[1],
(const double*)data_slice__Rt1,
strides_slice__Rt1[0], strides_slice__Rt1[1],
*inverted0, *inverted1);
return true;
'''},
)
m.function( "_compose_r",
"""Compose two angle-axis rotations
This is an internal function. You probably want mrcal.compose_r(). See the docs
for that function for details. This internal function differs from compose_r():
- It supports exactly two arguments, while compose_r() can compose N rotations
- It never reports gradients
""",
args_input = ('r0', 'r1'),
prototype_input = ((3,), (3,)),
prototype_output = (3,),
extra_args = (("int", "inverted0", "false", "p"),
("int", "inverted1", "false", "p"),),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_compose_r_full( (double*)data_slice__output,
strides_slice__output[0],
NULL,0,0,
NULL,0,0,
(const double*)data_slice__r0,
strides_slice__r0[0],
(const double*)data_slice__r1,
strides_slice__r1[0],
*inverted0, *inverted1);
return true;
'''},
)
m.function( "_compose_r_withgrad",
"""Compose two angle-axis rotations; return (r,dr/dr0,dr/dr1)
This is an internal function. You probably want mrcal.compose_r(). See the docs
for that function for details. This internal function differs from compose_r():
- It supports exactly two arguments, while compose_r() can compose N rotations
- It always reports gradients
""",
args_input = ('r0', 'r1'),
prototype_input = ((3,), (3,)),
prototype_output = ((3,), (3,3),(3,3)),
extra_args = (("int", "inverted0", "false", "p"),
("int", "inverted1", "false", "p"),),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_compose_r_full( (double*)data_slice__output0,
strides_slice__output0[0],
// dr/dr0
&item__output1(0,0),
strides_slice__output1[0], strides_slice__output1[1],
// dr/dr1
&item__output2(0,0),
strides_slice__output2[0], strides_slice__output2[1],
(const double*)data_slice__r0,
strides_slice__r0[0],
(const double*)data_slice__r1,
strides_slice__r1[0],
*inverted0, *inverted1);
return true;
'''},
)
m.function( "compose_r_tinyr0_gradientr0",
r"""Special-case rotation composition for the uncertainty computation
SYNOPSIS
r1 = rotation_axis1 * rotation_magnitude1
dr01_dr0 = compose_r_tinyr0_gradientr0(r1)
### Another way to get the same thing (but possibly less efficiently)
_,dr01_dr0,_ = compose_r(np.zeros((3,),),
r1,
get_gradients=True)
This is a special-case subset of compose_r(). It is the same, except:
- r0 is assumed to be 0, so we don't ingest it, and we don't report the
composition result
- we ONLY report the dr01/dr0 gradient
This special-case function is a part of the projection uncertainty computation,
so it exists separate from compose_r(). See the documentation for compose_r()
for all the details.
This function supports broadcasting fully.
ARGUMENTS
- r1: the second of the two rotations being composed. The first rotation is an
identity, so it's not given
- 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
RETURNED VALUE
We return a single array of shape (...,3,3): dr01/dr0
""",
args_input = ('r1',),
prototype_input = ((3,),),
prototype_output = (3,3),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_compose_r_tinyr0_gradientr0_full(
// dr/dr0
&item__output(0,0),
strides_slice__output[0], strides_slice__output[1],
(const double*)data_slice__r1,
strides_slice__r1[0] );
return true;
'''},
)
m.function( "compose_r_tinyr1_gradientr1",
r"""Special-case rotation composition for the uncertainty computation
SYNOPSIS
r0 = rotation_axis0 * rotation_magnitude0
dr01_dr1 = compose_r_tinyr1_gradientr1(r0)
### Another way to get the same thing (but possibly less efficiently)
_,_,dr01_dr1 = compose_r(r0,
np.zeros((3,),),
get_gradients=True)
This is a special-case subset of compose_r(). It is the same, except:
- r1 is assumed to be 0, so we don't ingest it, and we don't report the
composition result
- we ONLY report the dr01/dr1 gradient
This special-case function is a part of the projection uncertainty computation,
so it exists separate from compose_r(). See the documentation for compose_r()
for all the details.
This function supports broadcasting fully.
ARGUMENTS
- r0: the first of the two rotations being composed. The second rotation is an
identity, so it's not given
- 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
RETURNED VALUE
We return a single array of shape (...,3,3): dr01/dr1
""",
args_input = ('r0',),
prototype_input = ((3,),),
prototype_output = (3,3),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_compose_r_tinyr1_gradientr1_full(
// dr/dr1
&item__output(0,0),
strides_slice__output[0], strides_slice__output[1],
(const double*)data_slice__r0,
strides_slice__r0[0] );
return true;
'''},
)
m.function( "_compose_rt",
"""Compose two rt transformations
This is an internal function. You probably want mrcal.compose_rt(). See the docs
for that function for details. This internal function differs from compose_rt():
- It supports exactly two arguments, while compose_rt() can compose N
transformations
- It never reports gradients
""",
args_input = ('rt0', 'rt1'),
prototype_input = ((6,), (6,)),
prototype_output = (6,),
extra_args = (("int", "inverted0", "false", "p"),
("int", "inverted1", "false", "p"),),
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_compose_rt_full( (double*)data_slice__output,
strides_slice__output[0],
NULL,0,0,
NULL,0,0,
NULL,0,0,
NULL,0,0,
NULL,0,0,
NULL,0,0,
(const double*)data_slice__rt0,
strides_slice__rt0[0],
(const double*)data_slice__rt1,
strides_slice__rt1[0],
*inverted0, *inverted1);
return true;
'''},
)
m.function( "_compose_rt_withgrad",
"""Compose two rt transformations; return (rt,drt/drt0,drt/drt1)
This is an internal function. You probably want mrcal.compose_rt(). See the docs
for that function for details. This internal function differs from compose_rt():
- It supports exactly two arguments, while compose_rt() can compose N
transformations
- It always reports gradients
Note that the C library returns limited gradients:
- dr/dt0 is not returned: it is always 0
- dr/dt1 is not returned: it is always 0
THIS function combines these into the full drtout_drt0,drtout_drt1 arrays
""",
args_input = ('rt0', 'rt1'),
prototype_input = ((6,), (6,)),
prototype_output = ((6,), (6,6),(6,6)),
extra_args = (("int", "inverted0", "false", "p"),
("int", "inverted1", "false", "p"),),
# output1 is drt/drt0 = [ dr/dr0 dr/dt0 ]
# [ dt/dr0 dt/dt0 ]
#
# = [ dr/dr0 0 ]
# [ dt/dr0 I ]
#
# output2 is drt/drt1 = [ dr/dr1 dr/dt1 ]
# [ dt/dr1 dt/dt1 ]
#
# = [ dr/dr1 0 ]
# [ 0 dt/dt1 ]
Ccode_slice_eval = \
{np.float64:
r'''
mrcal_compose_rt_full( (double*)data_slice__output0,
strides_slice__output0[0],
// dr/dr0
&item__output1(0,0),
strides_slice__output1[0], strides_slice__output1[1],
// dr/dr1
&item__output2(0,0),
strides_slice__output2[0], strides_slice__output2[1],
// dt/dr0
&item__output1(3,0),
strides_slice__output1[0], strides_slice__output1[1],
// dt/dr1
&item__output2(3,0),
strides_slice__output2[0], strides_slice__output2[1],
// dt/dt0
&item__output1(3,3),
strides_slice__output1[0], strides_slice__output1[1],
// dt/dt1
&item__output2(3,3),
strides_slice__output2[0], strides_slice__output2[1],
(const double*)data_slice__rt0,
strides_slice__rt0[0],
(const double*)data_slice__rt1,
strides_slice__rt1[0],
*inverted0, *inverted1 );
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
{
item__output1(i, j+3) = 0;
item__output2(i, j+3) = 0;
}
return true;
'''},
)
m.function( "R_from_quat",
r"""Convert a rotation defined as a unit quaternion rotation to a rotation matrix
SYNOPSIS
s = np.sin(rotation_magnitude/2.)
c = np.cos(rotation_magnitude/2.)
quat = nps.glue( c, s*rotation_axis, axis = -1)
print(quat.shape)
===>
(4,)
R = mrcal.R_from_quat(quat)
print(R.shape)
===>
(3,3)
This is mostly for compatibility with some old stuff. mrcal doesn't use
quaternions anywhere. Test this thoroughly before using.
This function supports broadcasting fully.
ARGUMENTS
- quat: array of shape (4,). The unit quaternion that defines the rotation. The
values in the array are (u,i,j,k)
- 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 'out'
is given, we return the 'out' that was passed in. This is the standard
behavior provided by numpysane_pywrap.
RETURNED VALUE
We return an array of rotation matrices. Each broadcasted slice has shape (3,3)
""",
args_input = ('q',),
prototype_input = ((4,),),
prototype_output = (3,3),
Ccode_slice_eval = \
{np.float64:
r'''
// From the expression in wikipedia
const double r = item__q(0);
const double i = item__q(1);
const double j = item__q(2);
const double k = item__q(3);
const double ii = i*i;
const double ij = i*j;
const double ik = i*k;
const double ir = i*r;
const double jj = j*j;
const double jk = j*k;
const double jr = j*r;
const double kk = k*k;
const double kr = k*r;
item__output(0,0) = 1. - 2.*(jj+kk);
item__output(0,1) = 2.*(ij-kr);
item__output(0,2) = 2.*(ik+jr);
item__output(1,0) = 2.*(ij+kr);
item__output(1,1) = 1. - 2.*(ii+kk);
item__output(1,2) = 2.*(jk-ir);
item__output(2,0) = 2.*(ik-jr);
item__output(2,1) = 2.*(jk+ir);
item__output(2,2) = 1. - 2.*(ii+jj);
return true;
'''}
)
m.function( "skew_symmetric",
r"""Return the skew-symmetric matrix used in a cross product
SYNOPSIS
a = np.array(( 1., 5., 7.))
b = np.array(( 3., -.1, -10.))
A = mrcal.skew_symmetric(a)
print( nps.inner(A,b) )
===>
[-49.3 31. -15.1]
print( np.cross(a,b) )
===>
[-49.3 31. -15.1]
A vector cross-product a x b can be represented as a matrix multiplication A*b
where A is a skew-symmetric matrix based on the vector a. This function computes
this matrix A from the vector a.
This function supports broadcasting fully.
ARGUMENTS
- a: array of shape (3,)
- 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 'out'
is given, we return the 'out' that was passed in. This is the standard
behavior provided by numpysane_pywrap.
RETURNED VALUE
We return the matrix A in a (3,3) numpy array
""",
args_input = ('a',),
prototype_input = ((3,),),
prototype_output = (3,3),
Ccode_slice_eval = \
{np.float64:
r'''
// diagonal is zero
item__output(0,0) = 0.0;
item__output(1,1) = 0.0;
item__output(2,2) = 0.0;
item__output(0,1) = -item__a(2);
item__output(0,2) = item__a(1);
item__output(1,0) = item__a(2);
item__output(1,2) = -item__a(0);
item__output(2,0) = -item__a(1);
item__output(2,1) = item__a(0);
return true;
'''}
)
for w in ('weights', 'noweights'):
for kind in ('R01', 'Rt01'):
if w == 'weights':
args_input = ('v0','v1','weights')
prototype_input = (('N',3,), ('N',3,), ('N',))
weightarg = "(double*)data_slice__weights"
else:
args_input = ('v0','v1')
prototype_input = (('N',3,), ('N',3,))
weightarg = "NULL"
if kind == 'R01':
what = 'vectors'
prototype_output = (3,3)
Nelements_output = 9
else:
what = 'points'
prototype_output = (4,3)
Nelements_output = 12
m.function( f"_align_procrustes_{what}_{kind}_{w}",
r"""Compute a rotation to align two sets of direction vectors or points
This is the written-in-C Python extension module. Most of the time you want to
use the mrcal.poseutils wrapper module instead of this module directly. Any
functions not prefixed with "_" are meant to be called directly, without the
wrapper.
All functions are exported into the mrcal module. So you can call these via
mrcal._poseutils.fff() or mrcal.fff(). The latter is preferred.
""",
args_input = args_input,
prototype_input = prototype_input,
prototype_output = prototype_output,
Ccode_validate = r'''
return CHECK_CONTIGUOUS_AND_SETERROR_ALL();''',
Ccode_slice_eval = \
{np.float64:
rf'''
bool result =
mrcal_align_procrustes_{what}_{kind}((double*)data_slice__output,
dims_slice__v0[0],
(double*)data_slice__v0,
(double*)data_slice__v1,
{weightarg});
if(!result && 0.0 == *(double*)data_slice__output)
{{
// Poorly-defined problem. I indicate this with an all-zero
// output, but I return true. This allows us to process
// lots of data via broadcasting, without breaking ALL
// the slices if one slice is broken
memset((double*)data_slice__output, 0, {Nelements_output}*sizeof(double));
return true;
}}
return result;
'''}
)
m.function( f"R_aligned_to_vector",
r'''Compute a rotation to map a given vector to [0,0,1]
SYNOPSIS
# I have a plane that passes through a point p, and has a normal n. I
# compute a transformation from the world to a coord system aligned to the
# plane, with p at the origin. R_plane_world p + t_plane_world = 0:
Rt_plane_world = np.zeros((4,3), dtype=float)
Rt_plane_world[:3,:] = mrcal.R_aligned_to_vector(n)
Rt_plane_world[ 3,:] = -mrcal.rotate_point_R(Rt_plane_world[:3,:],p)
This rotation is not unique: adding any rotation around v still maps v to
[0,0,1]. An arbitrary acceptable rotation is returned.
ARGUMENTS
- v: a numpy array of shape (3,). The vector that the computed rotation maps to
[0,0,1]. Does not need to be normalized. Must be non-0
RETURNED VALUES
The rotation in a (3,3) array
''',
args_input = ('v',),
prototype_input = ( (3,), ),
prototype_output = (3,3),
Ccode_validate = r'''
return CHECK_CONTIGUOUS_AND_SETERROR_ALL();''',
Ccode_slice_eval = \
{np.float64:
rf'''
mrcal_R_aligned_to_vector((double*)data_slice__output,
(double*)data_slice__v);
return true;
'''}
)
m.write()
|