-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathposeutils.c
1684 lines (1489 loc) · 62 KB
/
poseutils.c
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
// 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
// Apparently I need this in MSVC to get constants
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "poseutils.h"
#include "strides.h"
#include "minimath/minimath.h"
// All arrays stored in row-major order
//
// I have two different representations of pose transformations:
//
// - Rt is a concatenated (4,3) array: Rt = nps.glue(R,t, axis=-2). The
// transformation is R*x+t
//
// - rt is a concatenated (6) array: rt = nps.glue(r,t, axis=-1). The
// transformation is R*x+t where R = R_from_r(r)
// row vectors: vout = matmult(v,Mt)
// equivalent col vector expression: vout = matmult(M,v)
#define mul_vec3_gen33t_vout_scaled_full(vout, vout_stride0, \
v, v_stride0, \
Mt, Mt_stride0, Mt_stride1, \
scale) \
do { \
/* needed for in-place operations */ \
double outcopy[3] = { \
scale * \
(_P2(Mt,Mt_stride0,Mt_stride1,0,0)*_P1(v,v_stride0,0) + \
_P2(Mt,Mt_stride0,Mt_stride1,0,1)*_P1(v,v_stride0,1) + \
_P2(Mt,Mt_stride0,Mt_stride1,0,2)*_P1(v,v_stride0,2) ), \
scale * \
(_P2(Mt,Mt_stride0,Mt_stride1,1,0)*_P1(v,v_stride0,0) + \
_P2(Mt,Mt_stride0,Mt_stride1,1,1)*_P1(v,v_stride0,1) + \
_P2(Mt,Mt_stride0,Mt_stride1,1,2)*_P1(v,v_stride0,2) ), \
scale * \
(_P2(Mt,Mt_stride0,Mt_stride1,2,0)*_P1(v,v_stride0,0) + \
_P2(Mt,Mt_stride0,Mt_stride1,2,1)*_P1(v,v_stride0,1) + \
_P2(Mt,Mt_stride0,Mt_stride1,2,2)*_P1(v,v_stride0,2) ) }; \
_P1(vout,vout_stride0,0) = outcopy[0]; \
_P1(vout,vout_stride0,1) = outcopy[1]; \
_P1(vout,vout_stride0,2) = outcopy[2]; \
} while(0)
#define mul_vec3_gen33t_vout_full(vout, vout_stride0, \
v, v_stride0, \
Mt, Mt_stride0, Mt_stride1) \
mul_vec3_gen33t_vout_scaled_full(vout, vout_stride0, \
v, v_stride0, \
Mt, Mt_stride0, Mt_stride1, 1.0)
// row vectors: vout = scale*matmult(v,M)
#define mul_vec3_gen33_vout_scaled_full(vout, vout_stride0, \
v, v_stride0, \
M, M_stride0, M_stride1, \
scale) \
do { \
/* needed for in-place operations */ \
double outcopy[3] = { \
scale * \
(_P2(M,M_stride0,M_stride1,0,0)*_P1(v,v_stride0,0) + \
_P2(M,M_stride0,M_stride1,1,0)*_P1(v,v_stride0,1) + \
_P2(M,M_stride0,M_stride1,2,0)*_P1(v,v_stride0,2)), \
scale * \
(_P2(M,M_stride0,M_stride1,0,1)*_P1(v,v_stride0,0) + \
_P2(M,M_stride0,M_stride1,1,1)*_P1(v,v_stride0,1) + \
_P2(M,M_stride0,M_stride1,2,1)*_P1(v,v_stride0,2)), \
scale * \
(_P2(M,M_stride0,M_stride1,0,2)*_P1(v,v_stride0,0) + \
_P2(M,M_stride0,M_stride1,1,2)*_P1(v,v_stride0,1) + \
_P2(M,M_stride0,M_stride1,2,2)*_P1(v,v_stride0,2)) }; \
_P1(vout,vout_stride0,0) = outcopy[0]; \
_P1(vout,vout_stride0,1) = outcopy[1]; \
_P1(vout,vout_stride0,2) = outcopy[2]; \
} while(0)
#define mul_vec3_gen33_vout_full(vout, vout_stride0, \
v, v_stride0, \
Mt, Mt_stride0, Mt_stride1) \
mul_vec3_gen33_vout_scaled_full(vout, vout_stride0, \
v, v_stride0, \
Mt, Mt_stride0, Mt_stride1, 1.0)
// row vectors: vout = matmult(v,Mt)
// equivalent col vector expression: vout = matmult(M,v)
#define mul_vec3_gen33t_vaccum_full(vout, vout_stride0, \
v, v_stride0, \
Mt, Mt_stride0, Mt_stride1) \
do { \
/* needed for in-place operations */ \
double outcopy[3] = { \
_P1(vout,vout_stride0,0) + \
_P2(Mt,Mt_stride0,Mt_stride1,0,0)*_P1(v,v_stride0,0) + \
_P2(Mt,Mt_stride0,Mt_stride1,0,1)*_P1(v,v_stride0,1) + \
_P2(Mt,Mt_stride0,Mt_stride1,0,2)*_P1(v,v_stride0,2), \
_P1(vout,vout_stride0,1) + \
_P2(Mt,Mt_stride0,Mt_stride1,1,0)*_P1(v,v_stride0,0) + \
_P2(Mt,Mt_stride0,Mt_stride1,1,1)*_P1(v,v_stride0,1) + \
_P2(Mt,Mt_stride0,Mt_stride1,1,2)*_P1(v,v_stride0,2), \
_P1(vout,vout_stride0,2) + \
_P2(Mt,Mt_stride0,Mt_stride1,2,0)*_P1(v,v_stride0,0) + \
_P2(Mt,Mt_stride0,Mt_stride1,2,1)*_P1(v,v_stride0,1) + \
_P2(Mt,Mt_stride0,Mt_stride1,2,2)*_P1(v,v_stride0,2) }; \
_P1(vout,vout_stride0,0) = outcopy[0]; \
_P1(vout,vout_stride0,1) = outcopy[1]; \
_P1(vout,vout_stride0,2) = outcopy[2]; \
} while(0)
// row vectors: vout = scale*matmult(v,M)
#define mul_vec3_gen33_vaccum_scaled_full(vout, vout_stride0, \
v, v_stride0, \
M, M_stride0, M_stride1, \
scale) \
do { \
/* needed for in-place operations */ \
double outcopy[3] = { \
_P1(vout,vout_stride0,0) + scale * \
(_P2(M,M_stride0,M_stride1,0,0)*_P1(v,v_stride0,0) + \
_P2(M,M_stride0,M_stride1,1,0)*_P1(v,v_stride0,1) + \
_P2(M,M_stride0,M_stride1,2,0)*_P1(v,v_stride0,2)), \
_P1(vout,vout_stride0,1) + scale * \
(_P2(M,M_stride0,M_stride1,0,1)*_P1(v,v_stride0,0) + \
_P2(M,M_stride0,M_stride1,1,1)*_P1(v,v_stride0,1) + \
_P2(M,M_stride0,M_stride1,2,1)*_P1(v,v_stride0,2)), \
_P1(vout,vout_stride0,2) + scale * \
(_P2(M,M_stride0,M_stride1,0,2)*_P1(v,v_stride0,0) + \
_P2(M,M_stride0,M_stride1,1,2)*_P1(v,v_stride0,1) + \
_P2(M,M_stride0,M_stride1,2,2)*_P1(v,v_stride0,2)) }; \
_P1(vout,vout_stride0,0) = outcopy[0]; \
_P1(vout,vout_stride0,1) = outcopy[1]; \
_P1(vout,vout_stride0,2) = outcopy[2]; \
} while(0)
// multiply two (3,3) matrices
static inline
void mul_gen33_gen33_vout_full(// output
double* m0m1,
int m0m1_stride0, int m0m1_stride1,
// input
const double* m0,
int m0_stride0, int m0_stride1,
const double* m1,
int m1_stride0, int m1_stride1)
{
/* needed for in-place operations */
double outcopy2[9];
for(int i=0; i<3; i++)
// one row at a time
mul_vec3_gen33_vout_scaled_full(&outcopy2[i*3], sizeof(outcopy2[0]),
&_P2(m0 , m0_stride0, m0_stride1, i,0), m0_stride1,
m1, m1_stride0, m1_stride1,
1.0);
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
P2(m0m1, i,j) = outcopy2[3*i+j];
}
static inline
void mul_gen33t_gen33_vout_full(// output
double* m0m1,
int m0m1_stride0, int m0m1_stride1,
// input
const double* m0,
int m0_stride0, int m0_stride1,
const double* m1,
int m1_stride0, int m1_stride1)
{
mul_gen33_gen33_vout_full(m0m1,
m0m1_stride0, m0m1_stride1,
m0,
m0_stride1, m0_stride0,
m1,
m1_stride0, m1_stride1);
}
static inline
void mul_gen33_gen33t_vout_full(// output
double* m0m1,
int m0m1_stride0, int m0m1_stride1,
// input
const double* m0,
int m0_stride0, int m0_stride1,
const double* m1,
int m1_stride0, int m1_stride1)
{
mul_gen33_gen33_vout_full(m0m1,
m0m1_stride0, m0m1_stride1,
m0,
m0_stride0, m0_stride1,
m1,
m1_stride1, m1_stride0);
}
static inline
void mul_gen33t_gen33t_vout_full(// output
double* m0m1,
int m0m1_stride0, int m0m1_stride1,
// input
const double* m0,
int m0_stride0, int m0_stride1,
const double* m1,
int m1_stride0, int m1_stride1)
{
mul_gen33_gen33_vout_full(m0m1,
m0m1_stride0, m0m1_stride1,
m0,
m0_stride1, m0_stride0,
m1,
m1_stride1, m1_stride0);
}
static inline
double inner3(const double* restrict a,
const double* restrict b)
{
double s = 0.0;
for (int i=0; i<3; i++) s += a[i]*b[i];
return s;
}
// Make an identity rotation or transformation
void mrcal_identity_R_full(double* R, // (3,3) array
int R_stride0, // in bytes. <= 0 means "contiguous"
int R_stride1 // in bytes. <= 0 means "contiguous"
)
{
init_stride_2D(R, 3,3);
P2(R, 0,0) = 1.0; P2(R, 0,1) = 0.0; P2(R, 0,2) = 0.0;
P2(R, 1,0) = 0.0; P2(R, 1,1) = 1.0; P2(R, 1,2) = 0.0;
P2(R, 2,0) = 0.0; P2(R, 2,1) = 0.0; P2(R, 2,2) = 1.0;
}
void mrcal_identity_r_full(double* r, // (3,) array
int r_stride0 // in bytes. <= 0 means "contiguous"
)
{
init_stride_1D(r, 3);
P1(r, 0) = 0.0; P1(r, 1) = 0.0; P1(r, 2) = 0.0;
}
void mrcal_identity_Rt_full(double* Rt, // (4,3) array
int Rt_stride0, // in bytes. <= 0 means "contiguous"
int Rt_stride1 // in bytes. <= 0 means "contiguous"
)
{
init_stride_2D(Rt, 4,3);
mrcal_identity_R_full(Rt, Rt_stride0, Rt_stride1);
for(int i=0; i<3; i++) P2(Rt, 3, i) = 0.0;
}
void mrcal_identity_rt_full(double* rt, // (6,) array
int rt_stride0 // in bytes. <= 0 means "contiguous"
)
{
init_stride_1D(rt, 6);
mrcal_identity_r_full(rt, rt_stride0);
for(int i=0; i<3; i++) P1(rt, i+3) = 0.0;
}
void mrcal_rotate_point_R_full( // output
double* x_out, // (3,) array
int x_out_stride0, // in bytes. <= 0 means "contiguous"
double* J_R, // (3,3,3) array. May be NULL
int J_R_stride0, // in bytes. <= 0 means "contiguous"
int J_R_stride1, // in bytes. <= 0 means "contiguous"
int J_R_stride2, // in bytes. <= 0 means "contiguous"
double* J_x, // (3,3) array. May be NULL
int J_x_stride0, // in bytes. <= 0 means "contiguous"
int J_x_stride1, // in bytes. <= 0 means "contiguous"
// input
const double* R, // (3,3) array. May be NULL
int R_stride0, // in bytes. <= 0 means "contiguous"
int R_stride1, // in bytes. <= 0 means "contiguous"
const double* x_in, // (3,) array. May be NULL
int x_in_stride0, // in bytes. <= 0 means "contiguous"
bool inverted // if true, I apply a
// rotation in the opposite
// direction. J_R corresponds
// to the input R
)
{
init_stride_1D(x_out, 3);
init_stride_3D(J_R, 3,3,3 );
init_stride_2D(J_x, 3,3 );
init_stride_2D(R, 3,3 );
init_stride_1D(x_in, 3 );
if(inverted)
{
// transpose R
int tmp;
tmp = R_stride0;
R_stride0 = R_stride1;
R_stride1 = tmp;
tmp = J_R_stride1;
J_R_stride1 = J_R_stride2;
J_R_stride2 = tmp;
}
if(J_R)
{
// out[i] = inner(R[i,:],in)
for(int i=0; i<3; i++)
{
int j=0;
for(; j<i; j++)
for(int k=0; k<3; k++)
P3(J_R, i,j,k) = 0.0;
for(int k=0; k<3; k++)
P3(J_R, i,j,k) = P1(x_in, k);
for(j++; j<3; j++)
for(int k=0; k<3; k++)
P3(J_R, i,j,k) = 0.0;
}
}
if(J_x)
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
P2(J_x, i,j) = P2(R, i,j);
// R*x
mul_vec3_gen33t_vout_full(x_out, x_out_stride0,
x_in, x_in_stride0,
R, R_stride0, R_stride1);
}
// mrcal_rotate_point_r() uses auto-differentiation, so it's implemented in C++
// in poseutils-uses-autodiff.cc
// Apply a transformation to a point
void mrcal_transform_point_Rt_full( // output
double* x_out, // (3,) array
int x_out_stride0, // in bytes. <= 0 means "contiguous"
double* J_Rt, // (3,4,3) array. May be NULL
int J_Rt_stride0, // in bytes. <= 0 means "contiguous"
int J_Rt_stride1, // in bytes. <= 0 means "contiguous"
int J_Rt_stride2, // in bytes. <= 0 means "contiguous"
double* J_x, // (3,3) array. May be NULL
int J_x_stride0, // in bytes. <= 0 means "contiguous"
int J_x_stride1, // in bytes. <= 0 means "contiguous"
// input
const double* Rt, // (4,3) array. May be NULL
int Rt_stride0, // in bytes. <= 0 means "contiguous"
int Rt_stride1, // in bytes. <= 0 means "contiguous"
const double* x_in, // (3,) array. May be NULL
int x_in_stride0, // in bytes. <= 0 means "contiguous"
bool inverted // if true, I apply a
// transformation in the opposite
// direction. J_Rt corresponds
// to the input Rt
)
{
init_stride_1D(x_out, 3);
init_stride_3D(J_Rt, 3,4,3 );
// init_stride_2D(J_x, 3,3 );
init_stride_2D(Rt, 4,3 );
init_stride_1D(x_in, 3 );
if(!inverted)
{
// for in-place operation
double t[] = { P2(Rt,3,0), P2(Rt,3,1), P2(Rt,3,2) };
// I want R*x + t
// First R*x
mrcal_rotate_point_R_full(x_out, x_out_stride0,
J_Rt, J_Rt_stride0, J_Rt_stride1, J_Rt_stride2,
J_x, J_x_stride0, J_x_stride1,
Rt, Rt_stride0, Rt_stride1,
x_in, x_in_stride0,
false);
// And now +t. The J_R, J_x gradients are unaffected. J_t is identity
for(int i=0; i<3; i++)
P1(x_out,i) += t[i];
if(J_Rt)
mrcal_identity_R_full(&P3(J_Rt,0,3,0), J_Rt_stride0, J_Rt_stride2);
}
else
{
// inverted operation means
// y = transpose(R) (x - t)
double x_minus_t[] = { P1(x_in,0) - P2(Rt,3,0),
P1(x_in,1) - P2(Rt,3,1),
P1(x_in,2) - P2(Rt,3,2)};
// Compute. After this:
// x_out is done
// J_R is done
// J_x is done
mrcal_rotate_point_R_full(x_out, x_out_stride0,
J_Rt, J_Rt_stride0, J_Rt_stride1, J_Rt_stride2,
J_x, J_x_stride0, J_x_stride1,
Rt, Rt_stride0, Rt_stride1,
x_minus_t, sizeof(double),
true);
// I want J_t = -transpose(R)
if(J_Rt)
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
P3(J_Rt, i, 3, j) = -P2(Rt, j, i);
}
}
// Invert a rotation matrix. This is a transpose
//
// The input is given in R_in in a (3,3) array
//
// The result is returned in a (3,3) array R_out. In-place operation is
// supported
void mrcal_invert_R_full( // output
double* R_out, // (3,3) array
int R_out_stride0, // in bytes. <= 0 means "contiguous"
int R_out_stride1, // in bytes. <= 0 means "contiguous"
// input
const double* R_in, // (3,3) array
int R_in_stride0, // in bytes. <= 0 means "contiguous"
int R_in_stride1 // in bytes. <= 0 means "contiguous"
)
{
init_stride_2D(R_out, 3,3);
init_stride_2D(R_in, 3,3);
// transpose(R). Extra stuff to make in-place operations work
for(int i=0; i<3; i++)
P2(R_out,i,i) = P2(R_in,i,i);
for(int i=0; i<3; i++)
for(int j=i+1; j<3; j++)
{
double tmp = P2(R_in,i,j);
P2(R_out,i,j) = P2(R_in,j,i);
P2(R_out,j,i) = tmp;
}
}
// Convert a transformation representation from Rt to rt. This is mostly a
// convenience functions since 99% of the work is done by mrcal_r_from_R().
void mrcal_rt_from_Rt_full(// output
double* rt, // (6,) vector
int rt_stride0, // in bytes. <= 0 means "contiguous"
double* J_R, // (3,3,3) array. Gradient. May be NULL
// No J_t. It's always the identity
int J_R_stride0, // in bytes. <= 0 means "contiguous"
int J_R_stride1, // in bytes. <= 0 means "contiguous"
int J_R_stride2, // in bytes. <= 0 means "contiguous"
// input
const double* Rt, // (4,3) array
int Rt_stride0, // in bytes. <= 0 means "contiguous"
int Rt_stride1 // in bytes. <= 0 means "contiguous"
)
{
mrcal_r_from_R_full(rt, rt_stride0,
J_R, J_R_stride0, J_R_stride1, J_R_stride2,
Rt, Rt_stride0, Rt_stride1);
init_stride_1D(rt, 6);
// init_stride_3D(J_R, 3,3,3);
init_stride_2D(Rt, 4,3);
for(int i=0; i<3; i++)
P1(rt, i+3) = P2(Rt,3,i);
}
// Convert a transformation representation from Rt to rt. This is mostly a
// convenience functions since 99% of the work is done by mrcal_R_from_r().
void mrcal_Rt_from_rt_full(// output
double* Rt, // (4,3) array
int Rt_stride0, // in bytes. <= 0 means "contiguous"
int Rt_stride1, // in bytes. <= 0 means "contiguous"
double* J_r, // (3,3,3) array. Gradient. May be NULL
// No J_t. It's just the identity
int J_r_stride0, // in bytes. <= 0 means "contiguous"
int J_r_stride1, // in bytes. <= 0 means "contiguous"
int J_r_stride2, // in bytes. <= 0 means "contiguous"
// input
const double* rt, // (6,) vector
int rt_stride0 // in bytes. <= 0 means "contiguous"
)
{
mrcal_R_from_r_full(Rt, Rt_stride0, Rt_stride1,
J_r, J_r_stride0, J_r_stride1, J_r_stride2,
rt, rt_stride0);
init_stride_1D(rt, 6);
// init_stride_3D(J_r, 3,3,3);
init_stride_2D(Rt, 4,3);
for(int i=0; i<3; i++)
P2(Rt,3,i) = P1(rt,i+3);
}
// Invert an Rt transformation
//
// b = Ra + t -> a = R'b - R't
void mrcal_invert_Rt_full( // output
double* Rt_out, // (4,3) array
int Rt_out_stride0, // in bytes. <= 0 means "contiguous"
int Rt_out_stride1, // in bytes. <= 0 means "contiguous"
// input
const double* Rt_in, // (4,3) array
int Rt_in_stride0, // in bytes. <= 0 means "contiguous"
int Rt_in_stride1 // in bytes. <= 0 means "contiguous"
)
{
init_stride_2D(Rt_out, 4,3);
init_stride_2D(Rt_in, 4,3);
// transpose(R). Extra stuff to make in-place operations work
for(int i=0; i<3; i++)
P2(Rt_out,i,i) = P2(Rt_in,i,i);
for(int i=0; i<3; i++)
for(int j=i+1; j<3; j++)
{
double tmp = P2(Rt_in,i,j);
P2(Rt_out,i,j) = P2(Rt_in,j,i);
P2(Rt_out,j,i) = tmp;
}
// -transpose(R)*t
mul_vec3_gen33t_vout_scaled_full(&P2(Rt_out,3,0), Rt_out_stride1,
&P2(Rt_in, 3,0), Rt_in_stride1,
Rt_out, Rt_out_stride0, Rt_out_stride1,
-1.0);
}
// Invert an rt transformation
//
// b = rotate(a) + t -> a = invrotate(b) - invrotate(t)
//
// drout_drin is not returned: it is always -I
// drout_dtin is not returned: it is always 0
void mrcal_invert_rt_full( // output
double* rt_out, // (6,) array
int rt_out_stride0, // in bytes. <= 0 means "contiguous"
double* dtout_drin, // (3,3) array
int dtout_drin_stride0, // in bytes. <= 0 means "contiguous"
int dtout_drin_stride1, // in bytes. <= 0 means "contiguous"
double* dtout_dtin, // (3,3) array
int dtout_dtin_stride0, // in bytes. <= 0 means "contiguous"
int dtout_dtin_stride1, // in bytes. <= 0 means "contiguous"
// input
const double* rt_in, // (6,) array
int rt_in_stride0 // in bytes. <= 0 means "contiguous"
)
{
init_stride_1D(rt_out, 6);
// init_stride_2D(dtout_drin, 3,3);
init_stride_2D(dtout_dtin, 3,3);
init_stride_1D(rt_in, 6);
// r uses an angle-axis representation, so to undo a rotation r, I can apply
// a rotation -r (same axis, equal and opposite angle)
for(int i=0; i<3; i++)
P1(rt_out,i) = -P1(rt_in,i);
mrcal_rotate_point_r_full( &P1(rt_out,3), rt_out_stride0,
dtout_drin, dtout_drin_stride0, dtout_drin_stride1,
dtout_dtin, dtout_dtin_stride0, dtout_dtin_stride1,
// input
rt_out, rt_out_stride0,
&P1(rt_in,3), rt_in_stride0,
false);
for(int i=0; i<3; i++)
P1(rt_out,3+i) *= -1.;
if(dtout_dtin)
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
P2(dtout_dtin,i,j) *= -1.;
}
// Compose two Rt transformations
// R0*(R1*x + t1) + t0 =
// (R0*R1)*x + R0*t1+t0
void mrcal_compose_Rt_full( // output
double* Rt_out, // (4,3) array
int Rt_out_stride0, // in bytes. <= 0 means "contiguous"
int Rt_out_stride1, // in bytes. <= 0 means "contiguous"
// input
const double* Rt_0, // (4,3) array
int Rt_0_stride0, // in bytes. <= 0 means "contiguous"
int Rt_0_stride1, // in bytes. <= 0 means "contiguous"
const double* Rt_1, // (4,3) array
int Rt_1_stride0, // in bytes. <= 0 means "contiguous"
int Rt_1_stride1, // in bytes. <= 0 means "contiguous"
bool inverted0,
bool inverted1)
{
init_stride_2D(Rt_out, 4,3);
init_stride_2D(Rt_0, 4,3);
init_stride_2D(Rt_1, 4,3);
/*
I have 4 cases based on the values of inverted0,inverted1. Nominally we have:
R0 R1 x + R0 t1 + t0
-> R01 = R0 R1
t01 = R0 t1 + t0
If we invert anything we use the inverted transform for r,t:
r x + t = y -> x = Rt y - Rt t
-> r becomes Rt, t becomes -Rt t
So
inverted0:
R01 = R0t R1
t01 = R0t t1 - R0t t0
= R0t (t1-t0)
inverted1:
R01 = R0 R1t
t01 = -R0 R1t t1 + t0
inverted01:
R01 = R0t R1t
t01 = -R0t R1t t1 - R0t t0
*/
if(!inverted0 && !inverted1)
{
// R01 = R0 R1
// t01 = R0 t1 + t0
// for in-place operation
const double t0[] = { P2(Rt_0,3,0),
P2(Rt_0,3,1),
P2(Rt_0,3,2) };
// t <- R0*t1
mul_vec3_gen33t_vout_full(&P2(Rt_out,3,0), Rt_out_stride1,
&P2(Rt_1, 3,0), Rt_1_stride1,
Rt_0, Rt_0_stride0, Rt_0_stride1);
// R <- R0*R1
mul_gen33_gen33_vout_full( Rt_out, Rt_out_stride0, Rt_out_stride1,
Rt_0, Rt_0_stride0, Rt_0_stride1,
Rt_1, Rt_1_stride0, Rt_1_stride1 );
// t <- R0*t1+t0
for(int i=0; i<3; i++)
P2(Rt_out,3,i) += t0[i];
}
else if(inverted0 && !inverted1)
{
// R01 = R0t R1
// t01 = R0t t1 - R0t t0
// = R0t (t1-t0)
const double t10[] = { P2(Rt_1,3,0) - P2(Rt_0,3,0),
P2(Rt_1,3,1) - P2(Rt_0,3,1),
P2(Rt_1,3,2) - P2(Rt_0,3,2) };
// t <- R0t*(t1-t0)
mul_vec3_gen33_vout_full(&P2(Rt_out,3,0), Rt_out_stride1,
t10, sizeof(t10[0]),
Rt_0, Rt_0_stride0, Rt_0_stride1);
// R <- R0t*R1
mul_gen33t_gen33_vout_full( Rt_out, Rt_out_stride0, Rt_out_stride1,
Rt_0, Rt_0_stride0, Rt_0_stride1,
Rt_1, Rt_1_stride0, Rt_1_stride1 );
}
else if(!inverted0 && inverted1)
{
// R01 = R0 R1t
// t01 = -R0 R1t t1 + t0
// for in-place operation
const double t0[] = { P2(Rt_0,3,0),
P2(Rt_0,3,1),
P2(Rt_0,3,2) };
// R <- R0*R1t
mul_gen33_gen33t_vout_full( Rt_out, Rt_out_stride0, Rt_out_stride1,
Rt_0, Rt_0_stride0, Rt_0_stride1,
Rt_1, Rt_1_stride0, Rt_1_stride1 );
// t01 <- R0 R1t t1
mul_vec3_gen33t_vout_full(&P2(Rt_out,3,0), Rt_out_stride1,
&P2(Rt_1, 3,0), Rt_1_stride1,
&P2(Rt_out,0,0), Rt_out_stride0, Rt_out_stride1);
// t01 <- -R0 R1t t1 + t0
for(int i=0; i<3; i++)
P2(Rt_out,3,i) = -P2(Rt_out,3,i) + t0[i];
}
else
{
// R01 = R0t R1t
// t01 = -R0t R1t t1 - R0t t0
const double R0t_t0[3];
mul_vec3_gen33_vout_full(R0t_t0, sizeof(R0t_t0[0]),
&P2(Rt_0, 3,0), Rt_0_stride1,
&P2(Rt_0,0,0), Rt_0_stride0, Rt_0_stride1);
// R <- R0t*R1t
mul_gen33t_gen33t_vout_full( Rt_out, Rt_out_stride0, Rt_out_stride1,
Rt_0, Rt_0_stride0, Rt_0_stride1,
Rt_1, Rt_1_stride0, Rt_1_stride1 );
// t01 <- R0t R1t t1
mul_vec3_gen33t_vout_full(&P2(Rt_out,3,0), Rt_out_stride1,
&P2(Rt_1, 3,0), Rt_1_stride1,
&P2(Rt_out,0,0), Rt_out_stride0, Rt_out_stride1);
// t01 <- -R0t R1t t1 - R0t t0
for(int i=0; i<3; i++)
P2(Rt_out,3,i) = -P2(Rt_out,3,i) - R0t_t0[i];
}
}
// Compose two rt transformations. It is assumed that we're getting no gradients
// at all or we're getting ALL the gradients: only dr_r0 is checked for NULL
//
// dr_dt0 is not returned: it is always 0
// dr_dt1 is not returned: it is always 0
void mrcal_compose_rt_full( // output
double* rt_out, // (6,) array
int rt_out_stride0, // in bytes. <= 0 means "contiguous"
double* dr_r0, // (3,3) array; may be NULL
int dr_r0_stride0, // in bytes. <= 0 means "contiguous"
int dr_r0_stride1, // in bytes. <= 0 means "contiguous"
double* dr_r1, // (3,3) array; may be NULL
int dr_r1_stride0, // in bytes. <= 0 means "contiguous"
int dr_r1_stride1, // in bytes. <= 0 means "contiguous"
double* dt_r0, // (3,3) array; may be NULL
int dt_r0_stride0, // in bytes. <= 0 means "contiguous"
int dt_r0_stride1, // in bytes. <= 0 means "contiguous"
double* dt_r1, // (3,3) array; may be NULL
int dt_r1_stride0, // in bytes. <= 0 means "contiguous"
int dt_r1_stride1, // in bytes. <= 0 means "contiguous"
double* dt_t0, // (3,3) array; may be NULL
int dt_t0_stride0, // in bytes. <= 0 means "contiguous"
int dt_t0_stride1, // in bytes. <= 0 means "contiguous"
double* dt_t1, // (3,3) array; may be NULL
int dt_t1_stride0, // in bytes. <= 0 means "contiguous"
int dt_t1_stride1, // in bytes. <= 0 means "contiguous"
// input
const double* rt_0, // (6,) array
int rt_0_stride0, // in bytes. <= 0 means "contiguous"
const double* rt_1, // (6,) array
int rt_1_stride0, // in bytes. <= 0 means "contiguous"
bool inverted0,
bool inverted1)
{
init_stride_1D(rt_out, 6);
init_stride_2D(dr_r0, 3,3);
init_stride_2D(dr_r1, 3,3);
init_stride_2D(dt_r0, 3,3);
init_stride_2D(dt_r1, 3,3);
init_stride_2D(dt_t0, 3,3);
init_stride_2D(dt_t1, 3,3);
init_stride_1D(rt_0, 6);
init_stride_1D(rt_1, 6);
/*
I have 4 cases based on the values of inverted0,inverted1. Nominally we have:
r0 r1 x + r0 t1 + t0
-> r01 = r0 r1
t01 = r0 t1 + t0
If we invert anything we use the inverted transform for r,t:
r x + t = y -> x = rt y - rt t
-> r becomes rt, t becomes -rt t
So
inverted0:
r01 = r0t r1
t01 = r0t t1 - r0t t0
inverted1:
r01 = r0 r1t
t01 = -r0 r1t t1 + t0
inverted01:
r01 = r0t r1t
t01 = -r0t r1t t1 - r0t t0
All the r stuff (inversions, gradients) is handled by
mrcal_compose_r_full(). For the t I have custom logic in this function
*/
// to make in-place operation work
double rt0[6];
double rt1[6];
for(int i=0; i<6; i++) rt0[i] = P1(rt_0, i);
for(int i=0; i<6; i++) rt1[i] = P1(rt_1, i);
// Compute r01
mrcal_compose_r_full( rt_out, rt_out_stride0,
dr_r0, dr_r0_stride0, dr_r0_stride1,
dr_r1, dr_r1_stride0, dr_r1_stride1,
rt_0, rt_0_stride0,
rt_1, rt_1_stride0,
inverted0, inverted1);
if(!inverted0 && !inverted1)
{
// t01 <- r0 t1 + t0
mrcal_rotate_point_r_full( &P1(rt_out,3), rt_out_stride0,
dt_r0, dt_r0_stride0, dt_r0_stride1,
dt_t1, dt_t1_stride0, dt_t1_stride1,
rt0, -1,
&P1(rt_1,3), rt_1_stride0,
false );
for(int i=0; i<3; i++)
P1(rt_out,3+i) += rt0[3+i];
// dt01/dt0 = I
if(dt_t0 != NULL)
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
P2(dt_t0,i,j) = (i==j) ? 1. : 0.;
// dt01/dr1 = 0
if(dt_r1 != NULL)
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
P2(dt_r1,i,j) = 0.;
}
else if(inverted0 && !inverted1)
{
// t01 <- r0t t1 - r0t t0
// = r0t (t1-t0)
double t10[3] = { rt1[0+3] - rt0[0+3],
rt1[1+3] - rt0[1+3],
rt1[2+3] - rt0[2+3] };
mrcal_rotate_point_r_full( &P1(rt_out,3), rt_out_stride0,
dt_r0, dt_r0_stride0, dt_r0_stride1,
dt_t1, dt_t1_stride0, dt_t1_stride1,
rt0, -1,
t10, -1,
true );
// dt01/dr1 = 0
if(dt_r1 != NULL)
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
P2(dt_r1,i,j) = 0.;
// dt01/dt0 = -dt01/dt1
if(dt_t0 != NULL)
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
P2(dt_t0,i,j) = -P2(dt_t1,i,j);
}
else if(!inverted0 && inverted1)
{
// t01 <- -r0 r1t t1 + t0
// let p = -r1t t1
double p[3];
double dp_r1[9];
double dp_t1[9];
mrcal_rotate_point_r_full( p, -1,
dp_r1, -1, -1,
dp_t1, -1, -1,
rt1, -1,
&rt1[3], -1,
true );
for(int i=0; i<3; i++)
p[i] *= -1;
for(int i=0; i<9; i++)
{
dp_r1[i] *= -1;
dp_t1[i] *= -1;
}
// t01 <- r0 p = -r0 r1t t1
double dt_p[9];
mrcal_rotate_point_r_full( &P1(rt_out,3), rt_out_stride0,
dt_r0, dt_r0_stride0, dt_r0_stride1,
dt_p, -1, -1,
rt0, -1,
p, -1,
false );
if(dt_r1 != NULL)
mul_gen33_gen33_vout_full(&P2(dt_r1,0,0), dt_r1_stride0, dt_r1_stride1,
// input
dt_p, 3*sizeof(double), sizeof(double),
dp_r1,3*sizeof(double), sizeof(double));
if(dt_t1 != NULL)
mul_gen33_gen33_vout_full(&P2(dt_t1,0,0), dt_t1_stride0, dt_t1_stride1,
// input
dt_p, 3*sizeof(double), sizeof(double),
dp_t1,3*sizeof(double), sizeof(double));
for(int i=0; i<3; i++)
P1(rt_out,3+i) += rt0[3+i];
// dt01/dt0 = I
if(dt_t0 != NULL)
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
P2(dt_t0,i,j) = (i==j) ? 1. : 0.;
}
else
{
// t01 <- -r0t r1t t1 - r0t t0
// = r0t (-r1t t1 - t0)
// let p = -r1t t1
double p[3];
double dp_r1[9];
double dp_t1[9];
mrcal_rotate_point_r_full( p, -1,
dp_r1, -1, -1,
dp_t1, -1, -1,
rt1, -1,
&rt1[3], -1,
true );
for(int i=0; i<3; i++)
p[i] *= -1;
for(int i=0; i<9; i++)
{
dp_r1[i] *= -1;
dp_t1[i] *= -1;
}
// p = -r1t t1 - t0
for(int i=0; i<3; i++)
p[i] -= rt0[3+i];
// t01 <- r0 p = -r0 r1t t1
double dt_p[9];
mrcal_rotate_point_r_full( &P1(rt_out,3), rt_out_stride0,
dt_r0, dt_r0_stride0, dt_r0_stride1,
dt_p, -1, -1,
rt0, -1,
p, -1,
true );
if(dt_r1 != NULL)
mul_gen33_gen33_vout_full(&P2(dt_r1,0,0), dt_r1_stride0, dt_r1_stride1,
// input
dt_p, 3*sizeof(double), sizeof(double),
dp_r1,3*sizeof(double), sizeof(double));
if(dt_t1 != NULL)
mul_gen33_gen33_vout_full(&P2(dt_t1,0,0), dt_t1_stride0, dt_t1_stride1,
// input
dt_p, 3*sizeof(double), sizeof(double),
dp_t1,3*sizeof(double), sizeof(double));
// dt01/dt0 = -dt/dp
if(dt_t0 != NULL)
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
P2(dt_t0,i,j) = -dt_p[3*i+j];
}