-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsfm.c
1903 lines (1586 loc) · 63.4 KB
/
sfm.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
/* Adapting the sba-1.6 demo to our use. There are many static and
* static inline functions, for speed I suppose, which I am coping
* into this file. I modified the CMakeLists to create an lib for the
* rest of the functions in sba-1.6/demo. The main function in this
* file is sba_driver_c() (line 1463) which is a modified version
* which accepts torch tensors rather than filenames in the original
* demo code.
*
* Marco Scoffier Jan. 30, 2011
*
*/
/* Euclidean bundle adjustment demo using the sba package */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <sba.h>
#include "../compiler.h"
#include "eucsbademo.h"
#include "readparams.h"
#define CLOCKS_PER_MSEC (CLOCKS_PER_SEC/1000.0)
#define MAXITER 100
#define MAXITER2 150
/* pointers to additional data, used for computed image projections and their jacobians */
struct globs_{
/* initial rotation parameters, combined with a local rotation
parameterization */
double *rot0params;
/* the 5 intrinsic calibration parameters in the order [fu, u0, v0,
* ar, skew], where ar is the aspect ratio fv/fu. Used only when
* calibration is fixed for all cameras; otherwise, it is null and
* the intrinsic parameters are included in the set of motion
* parameters for each camera
*/
double *intrcalib;
/* number of calibration parameters that must be kept constant.
* 0: all parameters are free
* 1: skew is fixed to its initial value, all other parameters vary (i.e. fu, u0, v0, ar)
* 2: skew and aspect ratio are fixed to their initial values, all other parameters vary (i.e. fu, u0, v0)
* 3: meaningless
* 4: skew, aspect ratio and principal point are fixed to their initial values, only the focal length varies (i.e. fu)
* 5: all intrinsics are kept fixed to their initial values
* >5: meaningless
* Used only when calibration varies among cameras
*/
int nccalib;
/* number of distortion parameters in Bouguet's model that must be kept constant.
* 0: all parameters are free
* 1: 6th order radial distortion term (kc[4]) is fixed
* 2: 6th order radial distortion and one of the tangential distortion terms (kc[3]) are fixed
* 3: 6th order radial distortion and both tangential distortion terms (kc[3], kc[2]) are fixed [i.e., only 2nd & 4th order radial dist.]
* 4: 4th & 6th order radial distortion terms and both tangential distortion ones are fixed [i.e., only 2nd order radial dist.]
* 5: all distortion parameters are kept fixed to their initial values
* >5: meaningless
* Used only when calibration varies among cameras and distortion is to be estimated
*/
int ncdist;
/* dimensions */
int cnp, pnp, mnp;
/* needed only when bundle adjusting for camera parameters only */
double *ptparams;
/* needed only when bundle adjusting for structure parameters only */
double *camparams;
} globs;
/* unit quaternion from vector part */
#define _MK_QUAT_FRM_VEC(q, v){ \
(q)[1]=(v)[0]; (q)[2]=(v)[1]; (q)[3]=(v)[2]; \
(q)[0]=sqrt(1.0 - (q)[1]*(q)[1] - (q)[2]*(q)[2]- (q)[3]*(q)[3]); \
}
/*
* multiplication of the two quaternions in q1 and q2 into p
*/
inline static void quatMult(double q1[FULLQUATSZ], double q2[FULLQUATSZ], double p[FULLQUATSZ])
{
p[0]=q1[0]*q2[0]-q1[1]*q2[1]-q1[2]*q2[2]-q1[3]*q2[3];
p[1]=q1[0]*q2[1]+q2[0]*q1[1]+q1[2]*q2[3]-q1[3]*q2[2];
p[2]=q1[0]*q2[2]+q2[0]*q1[2]+q2[1]*q1[3]-q1[1]*q2[3];
p[3]=q1[0]*q2[3]+q2[0]*q1[3]+q1[1]*q2[2]-q2[1]*q1[2];
}
/*
* fast multiplication of the two quaternions in q1 and q2 into p
* this is the second of the two schemes derived in pg. 8 of
* T. D. Howell, J.-C. Lafon, The complexity of the quaternion product, TR 75-245, Cornell Univ., June 1975.
*
* total additions increase from 12 to 27 (28), but total multiplications decrease from 16 to 9 (12)
*/
inline static void quatMultFast(double q1[FULLQUATSZ], double q2[FULLQUATSZ], double p[FULLQUATSZ])
{
double t1, t2, t3, t4, t5, t6, t7, t8, t9;
//double t10, t11, t12;
t1=(q1[0]+q1[1])*(q2[0]+q2[1]);
t2=(q1[3]-q1[2])*(q2[2]-q2[3]);
t3=(q1[1]-q1[0])*(q2[2]+q2[3]);
t4=(q1[2]+q1[3])*(q2[1]-q2[0]);
t5=(q1[1]+q1[3])*(q2[1]+q2[2]);
t6=(q1[1]-q1[3])*(q2[1]-q2[2]);
t7=(q1[0]+q1[2])*(q2[0]-q2[3]);
t8=(q1[0]-q1[2])*(q2[0]+q2[3]);
#if 0
t9 =t5+t6;
t10=t7+t8;
t11=t5-t6;
t12=t7-t8;
p[0]= t2 + 0.5*(-t9+t10);
p[1]= t1 - 0.5*(t9+t10);
p[2]=-t3 + 0.5*(t11+t12);
p[3]=-t4 + 0.5*(t11-t12);
#endif
/* following fragment it equivalent to the one above */
t9=0.5*(t5-t6+t7+t8);
p[0]= t2 + t9-t5;
p[1]= t1 - t9-t6;
p[2]=-t3 + t9-t8;
p[3]=-t4 + t9-t7;
}
/* Routines to estimate the estimated measurement vector (i.e. "func")
* and its sparse jacobian (i.e. "fjac") needed in BA. Code below
* makes use of the routines calcImgProj() and calcImgProjJacXXX()
* which compute the predicted projection & jacobian of a SINGLE 3D
* point (see imgproj.c). In the terminology of TR-340, these
* routines compute Q and its jacobians A=dQ/da, B=dQ/db. Notice also
* that what follows is two pairs of "func" and corresponding "fjac"
* routines. The first is to be used in full (i.e. motion +
* structure) BA, the second in motion only BA.
*/
static const double zerorotquat[FULLQUATSZ]={1.0, 0.0, 0.0, 0.0};
/************************************************************************/
/* MEASUREMENT VECTOR AND JACOBIAN COMPUTATION FOR VARYING CAMERA POSE
AND 3D STRUCTURE */
/************************************************************************/
/*** MEASUREMENT VECTOR AND JACOBIAN COMPUTATION FOR THE SIMPLE
DRIVERS ***/
/* FULL BUNDLE ADJUSTMENT, I.E. SIMULTANEOUS ESTIMATION OF CAMERA AND
STRUCTURE PARAMETERS */
/* Given the parameter vectors aj and bi of camera j and point i,
* computes in xij the predicted projection of point i on image j
*/
static void img_projRTS(int j, int i, double *aj, double *bi, double *xij, void *adata)
{
double *Kparms, *pr0;
struct globs_ *gl;
gl=(struct globs_ *)adata;
Kparms=gl->intrcalib;
pr0=gl->rot0params+j*FULLQUATSZ; /* full quat for initial rotation
estimate */
calcImgProj(Kparms, pr0, aj, aj+3, bi, xij); /* 3 is the
quaternion's vector
part length */
}
/* Given the parameter vectors aj and bi of camera j and point i,
* computes in Aij, Bij the jacobian of the predicted projection of
* point i on image j
*/
static void img_projRTS_jac(int j, int i, double *aj, double *bi, double *Aij, double *Bij, void *adata)
{
double *Kparms, *pr0;
struct globs_ *gl;
gl=(struct globs_ *)adata;
Kparms=gl->intrcalib;
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
calcImgProjJacRTS(Kparms, pr0, aj, aj+3, bi, (double (*)[6])Aij, (double (*)[3])Bij); // 3 is the quaternion's vector part length
}
/* BUNDLE ADJUSTMENT FOR CAMERA PARAMETERS ONLY */
/* Given the parameter vector aj of camera j, computes in xij the
* predicted projection of point i on image j
*/
static void img_projRT(int j, int i, double *aj, double *xij, void *adata)
{
int pnp;
double *Kparms, *pr0, *ptparams;
struct globs_ *gl;
gl=(struct globs_ *)adata;
pnp=gl->pnp;
Kparms=gl->intrcalib;
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
ptparams=gl->ptparams;
calcImgProj(Kparms, pr0, aj, aj+3, ptparams+i*pnp, xij); // 3 is the quaternion's vector part length
}
/* Given the parameter vector aj of camera j, computes in Aij
* the jacobian of the predicted projection of point i on image j
*/
static void img_projRT_jac(int j, int i, double *aj, double *Aij, void *adata)
{
int pnp;
double *Kparms, *ptparams, *pr0;
struct globs_ *gl;
gl=(struct globs_ *)adata;
pnp=gl->pnp;
Kparms=gl->intrcalib;
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
ptparams=gl->ptparams;
calcImgProjJacRT(Kparms, pr0, aj, aj+3, ptparams+i*pnp, (double (*)[6])Aij); // 3 is the quaternion's vector part length
}
/* BUNDLE ADJUSTMENT FOR STRUCTURE PARAMETERS ONLY */
/* Given the parameter vector bi of point i, computes in xij the
* predicted projection of point i on image j
*/
static void img_projS(int j, int i, double *bi, double *xij, void *adata)
{
int cnp;
double *Kparms, *camparams, *aj;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp;
Kparms=gl->intrcalib;
camparams=gl->camparams;
aj=camparams+j*cnp;
calcImgProjFullR(Kparms, aj, aj+3, bi, xij); /* 3 is the
quaternion's vector
part length */
//calcImgProj(Kparms, (double *)zerorotquat, aj, aj+3, bi, xij); // 3 is the quaternion's vector part length
}
/* Given the parameter vector bi of point i, computes in Bij
* the jacobian of the predicted projection of point i on image j
*/
static void img_projS_jac(int j, int i, double *bi, double *Bij, void *adata)
{
int cnp;
double *Kparms, *camparams, *aj;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp;
Kparms=gl->intrcalib;
camparams=gl->camparams;
aj=camparams+j*cnp;
calcImgProjJacS(Kparms, (double *)zerorotquat, aj, aj+3, bi, (double (*)[3])Bij); // 3 is the quaternion's vector part length
}
/*** MEASUREMENT VECTOR AND JACOBIAN COMPUTATION FOR THE EXPERT
DRIVERS ***/
/* FULL BUNDLE ADJUSTMENT, I.E. SIMULTANEOUS ESTIMATION OF CAMERA AND
STRUCTURE PARAMETERS */
/* Given a parameter vector p made up of the 3D coordinates of n
* points and the parameters of m cameras, compute in hx the
* prediction of the measurements, i.e. the projections of 3D points
* in the m images. The measurements are returned in the order
* (hx_11^T, .. hx_1m^T, ..., hx_n1^T, .. hx_nm^T)^T, where hx_ij is
* the predicted projection of the i-th point on the j-th camera.
* Notice that depending on idxij, some of the hx_ij might be missing
*
*/
static void img_projsRTS_x(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *hx, void *adata)
{
register int i, j;
int cnp, pnp, mnp;
double *pa, *pb, *pqr, *pt, *ppt, *pmeas, *Kparms, *pr0, lrot[FULLQUATSZ], trot[FULLQUATSZ];
//int n;
int m, nnz;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp; pnp=gl->pnp; mnp=gl->mnp;
Kparms=gl->intrcalib;
//n=idxij->nr;
m=idxij->nc;
pa=p; pb=p+m*cnp;
for(j=0; j<m; ++j){
/* j-th camera parameters */
pqr=pa+j*cnp;
pt=pqr+3; // quaternion vector part has 3 elements
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
_MK_QUAT_FRM_VEC(lrot, pqr);
quatMultFast(lrot, pr0, trot); // trot=lrot*pr0
nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs); /* find nonzero hx_ij, i=0...n-1 */
for(i=0; i<nnz; ++i){
ppt=pb + rcsubs[i]*pnp;
pmeas=hx + idxij->val[rcidxs[i]]*mnp; // set pmeas to point to hx_ij
calcImgProjFullR(Kparms, trot, pt, ppt, pmeas); // evaluate Q in pmeas
//calcImgProj(Kparms, pr0, pqr, pt, ppt, pmeas); // evaluate Q in pmeas
}
}
}
/* Given a parameter vector p made up of the 3D coordinates of n
* points and the parameters of m cameras, compute in jac the jacobian
* of the predicted measurements, i.e. the jacobian of the projections
* of 3D points in the m images. The jacobian is returned in the
* order (A_11, ..., A_1m, ..., A_n1, ..., A_nm, B_11, ..., B_1m, ...,
* B_n1, ..., B_nm), where A_ij=dx_ij/db_j and B_ij=dx_ij/db_i (see
* HZ). Notice that depending on idxij, some of the A_ij, B_ij might
* be missing
*
*/
static void img_projsRTS_jac_x(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *jac, void *adata)
{
register int i, j;
int cnp, pnp, mnp;
double *pa, *pb, *pqr, *pt, *ppt, *pA, *pB, *Kparms, *pr0;
//int n;
int m, nnz, Asz, Bsz, ABsz;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp; pnp=gl->pnp; mnp=gl->mnp;
Kparms=gl->intrcalib;
//n=idxij->nr;
m=idxij->nc;
pa=p; pb=p+m*cnp;
Asz=mnp*cnp; Bsz=mnp*pnp; ABsz=Asz+Bsz;
for(j=0; j<m; ++j){
/* j-th camera parameters */
pqr=pa+j*cnp;
pt=pqr+3; // quaternion vector part has 3 elements
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs); /* find nonzero hx_ij, i=0...n-1 */
for(i=0; i<nnz; ++i){
ppt=pb + rcsubs[i]*pnp;
pA=jac + idxij->val[rcidxs[i]]*ABsz; // set pA to point to A_ij
pB=pA + Asz; // set pB to point to B_ij
calcImgProjJacRTS(Kparms, pr0, pqr, pt, ppt, (double (*)[6])pA, (double (*)[3])pB); // evaluate dQ/da, dQ/db in pA, pB
}
}
}
/* BUNDLE ADJUSTMENT FOR CAMERA PARAMETERS ONLY */
/* Given a parameter vector p made up of the parameters of m cameras,
* compute in hx the prediction of the measurements, i.e. the
* projections of 3D points in the m images. The measurements are
* returned in the order (hx_11^T, .. hx_1m^T, ..., hx_n1^T,
* .. hx_nm^T)^T, where hx_ij is the predicted projection of the i-th
* point on the j-th camera. Notice that depending on idxij, some of
* the hx_ij might be missing
*
*/
static void img_projsRT_x(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *hx, void *adata)
{
register int i, j;
int cnp, pnp, mnp;
double *pqr, *pt, *ppt, *pmeas, *Kparms, *ptparams, *pr0, lrot[FULLQUATSZ], trot[FULLQUATSZ];
//int n;
int m, nnz;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp; pnp=gl->pnp; mnp=gl->mnp;
Kparms=gl->intrcalib;
ptparams=gl->ptparams;
//n=idxij->nr;
m=idxij->nc;
for(j=0; j<m; ++j){
/* j-th camera parameters */
pqr=p+j*cnp;
pt=pqr+3; // quaternion vector part has 3 elements
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
_MK_QUAT_FRM_VEC(lrot, pqr);
quatMultFast(lrot, pr0, trot); // trot=lrot*pr0
nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs); /* find nonzero hx_ij, i=0...n-1 */
for(i=0; i<nnz; ++i){
ppt=ptparams + rcsubs[i]*pnp;
pmeas=hx + idxij->val[rcidxs[i]]*mnp; // set pmeas to point to hx_ij
calcImgProjFullR(Kparms, trot, pt, ppt, pmeas); // evaluate Q in pmeas
//calcImgProj(Kparms, pr0, pqr, pt, ppt, pmeas); // evaluate Q in pmeas
}
}
}
/* Given a parameter vector p made up of the parameters of m cameras,
* compute in jac the jacobian of the predicted measurements, i.e. the
* jacobian of the projections of 3D points in the m images. The
* jacobian is returned in the order (A_11, ..., A_1m, ..., A_n1, ...,
* A_nm), where A_ij=dx_ij/db_j (see HZ). Notice that depending on
* idxij, some of the A_ij might be missing
*
*/
static void img_projsRT_jac_x(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *jac, void *adata)
{
register int i, j;
int cnp, pnp, mnp;
double *pqr, *pt, *ppt, *pA, *Kparms, *ptparams, *pr0;
//int n;
int m, nnz, Asz;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp; pnp=gl->pnp; mnp=gl->mnp;
Kparms=gl->intrcalib;
ptparams=gl->ptparams;
//n=idxij->nr;
m=idxij->nc;
Asz=mnp*cnp;
for(j=0; j<m; ++j){
/* j-th camera parameters */
pqr=p+j*cnp;
pt=pqr+3; // quaternion vector part has 3 elements
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs); /* find nonzero hx_ij, i=0...n-1 */
for(i=0; i<nnz; ++i){
ppt=ptparams + rcsubs[i]*pnp;
pA=jac + idxij->val[rcidxs[i]]*Asz; // set pA to point to A_ij
calcImgProjJacRT(Kparms, pr0, pqr, pt, ppt, (double (*)[6])pA); // evaluate dQ/da in pA
}
}
}
/* BUNDLE ADJUSTMENT FOR STRUCTURE PARAMETERS ONLY */
/* Given a parameter vector p made up of the 3D coordinates of n
* points, compute in hx the prediction of the measurements, i.e. the
* projections of 3D points in the m images. The measurements are
* returned in the order (hx_11^T, .. hx_1m^T, ..., hx_n1^T,
* .. hx_nm^T)^T, where hx_ij is the predicted projection of the i-th
* point on the j-th camera. Notice that depending on idxij, some of
* the hx_ij might be missing
*
*/
static void img_projsS_x(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *hx, void *adata)
{
register int i, j;
int cnp, pnp, mnp;
double *pqr, *pt, *ppt, *pmeas, *Kparms, *camparams, trot[FULLQUATSZ];
//int n;
int m, nnz;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp; pnp=gl->pnp; mnp=gl->mnp;
Kparms=gl->intrcalib;
camparams=gl->camparams;
//n=idxij->nr;
m=idxij->nc;
for(j=0; j<m; ++j){
/* j-th camera parameters */
pqr=camparams+j*cnp;
pt=pqr+3; // quaternion vector part has 3 elements
_MK_QUAT_FRM_VEC(trot, pqr);
nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs); /* find nonzero hx_ij, i=0...n-1 */
for(i=0; i<nnz; ++i){
ppt=p + rcsubs[i]*pnp;
pmeas=hx + idxij->val[rcidxs[i]]*mnp; // set pmeas to point to hx_ij
calcImgProjFullR(Kparms, trot, pt, ppt, pmeas); // evaluate Q in pmeas
//calcImgProj(Kparms, (double *)zerorotquat, pqr, pt, ppt, pmeas); // evaluate Q in pmeas
}
}
}
/* Given a parameter vector p made up of the 3D coordinates of n
* points, compute in jac the jacobian of the predicted measurements,
* i.e. the jacobian of the projections of 3D points in the m images.
* The jacobian is returned in the order (B_11, ..., B_1m, ..., B_n1,
* ..., B_nm), where B_ij=dx_ij/db_i (see HZ). Notice that depending
* on idxij, some of the B_ij might be missing
*
*/
static void img_projsS_jac_x(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *jac, void *adata)
{
register int i, j;
int cnp, pnp, mnp;
double *pqr, *pt, *ppt, *pB, *Kparms, *camparams;
//int n;
int m, nnz, Bsz;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp; pnp=gl->pnp; mnp=gl->mnp;
Kparms=gl->intrcalib;
camparams=gl->camparams;
//n=idxij->nr;
m=idxij->nc;
Bsz=mnp*pnp;
for(j=0; j<m; ++j){
/* j-th camera parameters */
pqr=camparams+j*cnp;
pt=pqr+3; // quaternion vector part has 3 elements
nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs); /* find nonzero hx_ij, i=0...n-1 */
for(i=0; i<nnz; ++i){
ppt=p + rcsubs[i]*pnp;
pB=jac + idxij->val[rcidxs[i]]*Bsz; // set pB to point to B_ij
calcImgProjJacS(Kparms, (double *)zerorotquat, pqr, pt, ppt, (double (*)[3])pB); // evaluate dQ/da in pB
}
}
}
/************************************************************************/
/* MEASUREMENT VECTOR AND JACOBIAN COMPUTATION FOR VARYING CAMERA
INTRINSICS, POSE AND 3D STRUCTURE */
/************************************************************************/
/*** MEASUREMENT VECTOR AND JACOBIAN COMPUTATION FOR THE SIMPLE
DRIVERS ***/
/* A note about the computation of Jacobians below:
*
* When performing BA that includes the camera intrinsics, it would be
* very desirable to allow for certain parameters such as skew, aspect
* ratio and principal point to be fixed. The straighforward way to
* implement this would be to code a separate version of the Jacobian
* computation routines for each subset of non-fixed parameters. Here,
* this is bypassed by developing only one set of Jacobian computation
* routines which estimate the former for all 5 intrinsics and then
* set the columns corresponding to fixed parameters to zero.
*/
/* FULL BUNDLE ADJUSTMENT, I.E. SIMULTANEOUS ESTIMATION OF CAMERA AND
STRUCTURE PARAMETERS */
/* Given the parameter vectors aj and bi of camera j and point i,
* computes in xij the predicted projection of point i on image j
*/
static void img_projKRTS(int j, int i, double *aj, double *bi, double *xij, void *adata)
{
double *pr0;
struct globs_ *gl;
gl=(struct globs_ *)adata;
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
calcImgProj(aj, pr0, aj+5, aj+5+3, bi, xij); // 5 for the calibration + 3 for the quaternion's vector part
}
/* Given the parameter vectors aj and bi of camera j and point i,
* computes in Aij, Bij the jacobian of the predicted projection of
* point i on image j
*/
static void img_projKRTS_jac(int j, int i, double *aj, double *bi, double *Aij, double *Bij, void *adata)
{
struct globs_ *gl;
double *pr0;
int ncK;
gl=(struct globs_ *)adata;
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
calcImgProjJacKRTS(aj, pr0, aj+5, aj+5+3, bi, (double (*)[5+6])Aij, (double (*)[3])Bij); // 5 for the calibration + 3 for the quaternion's vector part
/* clear the columns of the Jacobian corresponding to fixed calibration parameters */
gl=(struct globs_ *)adata;
ncK=gl->nccalib;
if(ncK){
int cnp, mnp, j0;
cnp=gl->cnp;
mnp=gl->mnp;
j0=5-ncK;
for(i=0; i<mnp; ++i, Aij+=cnp)
for(j=j0; j<5; ++j)
Aij[j]=0.0; // Aij[i*cnp+j]=0.0;
}
}
/* BUNDLE ADJUSTMENT FOR CAMERA PARAMETERS ONLY */
/* Given the parameter vector aj of camera j, computes in xij the
* predicted projection of point i on image j
*/
static void img_projKRT(int j, int i, double *aj, double *xij, void *adata)
{
int pnp;
double *ptparams, *pr0;
struct globs_ *gl;
gl=(struct globs_ *)adata;
pnp=gl->pnp;
ptparams=gl->ptparams;
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
calcImgProj(aj, pr0, aj+5, aj+5+3, ptparams+i*pnp, xij); // 5 for the calibration + 3 for the quaternion's vector part
}
/* Given the parameter vector aj of camera j, computes in Aij
* the jacobian of the predicted projection of point i on image j
*/
static void img_projKRT_jac(int j, int i, double *aj, double *Aij, void *adata)
{
struct globs_ *gl;
double *ptparams, *pr0;
int pnp, ncK;
gl=(struct globs_ *)adata;
pnp=gl->pnp;
ptparams=gl->ptparams;
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
calcImgProjJacKRT(aj, pr0, aj+5, aj+5+3, ptparams+i*pnp, (double (*)[5+6])Aij); // 5 for the calibration + 3 for the quaternion's vector part
/* clear the columns of the Jacobian corresponding to fixed
calibration parameters */
ncK=gl->nccalib;
if(ncK){
int cnp, mnp, j0;
cnp=gl->cnp;
mnp=gl->mnp;
j0=5-ncK;
for(i=0; i<mnp; ++i, Aij+=cnp)
for(j=j0; j<5; ++j)
Aij[j]=0.0; // Aij[i*cnp+j]=0.0;
}
}
/* BUNDLE ADJUSTMENT FOR STRUCTURE PARAMETERS ONLY */
/* Given the parameter vector bi of point i, computes in xij the
* predicted projection of point i on image j
*/
static void img_projKS(int j, int i, double *bi, double *xij, void *adata)
{
int cnp;
double *camparams, *aj;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp;
camparams=gl->camparams;
aj=camparams+j*cnp;
calcImgProjFullR(aj, aj+5, aj+5+3, bi, xij); // 5 for the calibration + 3 for the quaternion's vector part
//calcImgProj(aj, (double *)zerorotquat, aj+5, aj+5+3, bi, xij); // 5 for the calibration + 3 for the quaternion's vector part
}
/* Given the parameter vector bi of point i, computes in Bij
* the jacobian of the predicted projection of point i on image j
*/
static void img_projKS_jac(int j, int i, double *bi, double *Bij, void *adata)
{
int cnp;
double *camparams, *aj;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp;
camparams=gl->camparams;
aj=camparams+j*cnp;
calcImgProjJacS(aj, (double *)zerorotquat, aj+5, aj+5+3, bi, (double (*)[3])Bij); // 5 for the calibration + 3 for the quaternion's vector part
}
/*** MEASUREMENT VECTOR AND JACOBIAN COMPUTATION FOR THE EXPERT
DRIVERS ***/
/* FULL BUNDLE ADJUSTMENT, I.E. SIMULTANEOUS ESTIMATION OF CAMERA AND
STRUCTURE PARAMETERS */
/* Given a parameter vector p made up of the 3D coordinates of n
* points and the parameters of m cameras, compute in hx the
* prediction of the measurements, i.e. the projections of 3D points
* in the m images. The measurements are returned in the order
* (hx_11^T, .. hx_1m^T, ..., hx_n1^T, .. hx_nm^T)^T, where hx_ij is
* the predicted projection of the i-th point on the j-th camera.
* Notice that depending on idxij, some of the hx_ij might be missing
*
*/
static void img_projsKRTS_x(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *hx, void *adata)
{
register int i, j;
int cnp, pnp, mnp;
double *pa, *pb, *pqr, *pt, *ppt, *pmeas, *pcalib, *pr0, lrot[FULLQUATSZ], trot[FULLQUATSZ];
//int n;
int m, nnz;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp; pnp=gl->pnp; mnp=gl->mnp;
//n=idxij->nr;
m=idxij->nc;
pa=p; pb=p+m*cnp;
for(j=0; j<m; ++j){
/* j-th camera parameters */
pcalib=pa+j*cnp;
pqr=pcalib+5;
pt=pqr+3; // quaternion vector part has 3 elements
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
_MK_QUAT_FRM_VEC(lrot, pqr);
quatMultFast(lrot, pr0, trot); // trot=lrot*pr0
nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs); /* find nonzero hx_ij, i=0...n-1 */
for(i=0; i<nnz; ++i){
ppt=pb + rcsubs[i]*pnp;
pmeas=hx + idxij->val[rcidxs[i]]*mnp; // set pmeas to point to hx_ij
calcImgProjFullR(pcalib, trot, pt, ppt, pmeas); // evaluate Q in pmeas
//calcImgProj(pcalib, pr0, pqr, pt, ppt, pmeas); // evaluate Q in pmeas
}
}
}
/* Given a parameter vector p made up of the 3D coordinates of n
* points and the parameters of m cameras, compute in jac the jacobian
* of the predicted measurements, i.e. the jacobian of the projections
* of 3D points in the m images. The jacobian is returned in the
* order (A_11, ..., A_1m, ..., A_n1, ..., A_nm, B_11, ..., B_1m, ...,
* B_n1, ..., B_nm), where A_ij=dx_ij/db_j and B_ij=dx_ij/db_i (see
* HZ). Notice that depending on idxij, some of the A_ij, B_ij might
* be missing
*
*/
static void img_projsKRTS_jac_x(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *jac, void *adata)
{
register int i, j, ii, jj;
int cnp, pnp, mnp, ncK;
double *pa, *pb, *pqr, *pt, *ppt, *pA, *pB, *pcalib, *pr0;
//int n;
int m, nnz, Asz, Bsz, ABsz;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp; pnp=gl->pnp; mnp=gl->mnp;
ncK=gl->nccalib;
//n=idxij->nr;
m=idxij->nc;
pa=p; pb=p+m*cnp;
Asz=mnp*cnp; Bsz=mnp*pnp; ABsz=Asz+Bsz;
for(j=0; j<m; ++j){
/* j-th camera parameters */
pcalib=pa+j*cnp;
pqr=pcalib+5;
pt=pqr+3; // quaternion vector part has 3 elements
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs); /* find nonzero hx_ij, i=0...n-1 */
for(i=0; i<nnz; ++i){
ppt=pb + rcsubs[i]*pnp;
pA=jac + idxij->val[rcidxs[i]]*ABsz; // set pA to point to A_ij
pB=pA + Asz; // set pB to point to B_ij
calcImgProjJacKRTS(pcalib, pr0, pqr, pt, ppt, (double (*)[5+6])pA, (double (*)[3])pB); // evaluate dQ/da, dQ/db in pA, pB
/* clear the columns of the Jacobian corresponding to fixed calibration parameters */
if(ncK){
int jj0=5-ncK;
for(ii=0; ii<mnp; ++ii, pA+=cnp)
for(jj=jj0; jj<5; ++jj)
pA[jj]=0.0; // pA[ii*cnp+jj]=0.0;
}
}
}
}
/* BUNDLE ADJUSTMENT FOR CAMERA PARAMETERS ONLY */
/* Given a parameter vector p made up of the parameters of m cameras,
* compute in hx the prediction of the measurements, i.e. the
* projections of 3D points in the m images. The measurements are
* returned in the order (hx_11^T, .. hx_1m^T, ..., hx_n1^T,
* .. hx_nm^T)^T, where hx_ij is the predicted projection of the i-th
* point on the j-th camera. Notice that depending on idxij, some of
* the hx_ij might be missing
*
*/
static void img_projsKRT_x(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *hx, void *adata)
{
register int i, j;
int cnp, pnp, mnp;
double *pqr, *pt, *ppt, *pmeas, *pcalib, *ptparams, *pr0, lrot[FULLQUATSZ], trot[FULLQUATSZ];
//int n;
int m, nnz;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp; pnp=gl->pnp; mnp=gl->mnp;
ptparams=gl->ptparams;
//n=idxij->nr;
m=idxij->nc;
for(j=0; j<m; ++j){
/* j-th camera parameters */
pcalib=p+j*cnp;
pqr=pcalib+5;
pt=pqr+3; // quaternion vector part has 3 elements
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
_MK_QUAT_FRM_VEC(lrot, pqr);
quatMultFast(lrot, pr0, trot); // trot=lrot*pr0
nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs); /* find nonzero hx_ij, i=0...n-1 */
for(i=0; i<nnz; ++i){
ppt=ptparams + rcsubs[i]*pnp;
pmeas=hx + idxij->val[rcidxs[i]]*mnp; // set pmeas to point to hx_ij
calcImgProjFullR(pcalib, trot, pt, ppt, pmeas); // evaluate Q in pmeas
//calcImgProj(pcalib, pr0, pqr, pt, ppt, pmeas); // evaluate Q in pmeas
}
}
}
/* Given a parameter vector p made up of the parameters of m cameras,
* compute in jac the jacobian of the predicted measurements, i.e. the
* jacobian of the projections of 3D points in the m images. The
* jacobian is returned in the order (A_11, ..., A_1m, ..., A_n1, ...,
* A_nm), where A_ij=dx_ij/db_j (see HZ). Notice that depending on
* idxij, some of the A_ij might be missing
*
*/
static void img_projsKRT_jac_x(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *jac, void *adata)
{
register int i, j, ii, jj;
int cnp, pnp, mnp, ncK;
double *pqr, *pt, *ppt, *pA, *pcalib, *ptparams, *pr0;
//int n;
int m, nnz, Asz;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp; pnp=gl->pnp; mnp=gl->mnp;
ncK=gl->nccalib;
ptparams=gl->ptparams;
//n=idxij->nr;
m=idxij->nc;
Asz=mnp*cnp;
for(j=0; j<m; ++j){
/* j-th camera parameters */
pcalib=p+j*cnp;
pqr=pcalib+5;
pt=pqr+3; // quaternion vector part has 3 elements
pr0=gl->rot0params+j*FULLQUATSZ; // full quat for initial rotation estimate
nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs); /* find nonzero hx_ij, i=0...n-1 */
for(i=0; i<nnz; ++i){
ppt=ptparams + rcsubs[i]*pnp;
pA=jac + idxij->val[rcidxs[i]]*Asz; // set pA to point to A_ij
calcImgProjJacKRT(pcalib, pr0, pqr, pt, ppt, (double (*)[5+6])pA); // evaluate dQ/da in pA
/* clear the columns of the Jacobian corresponding to fixed
calibration parameters */
if(ncK){
int jj0;
jj0=5-ncK;
for(ii=0; ii<mnp; ++ii, pA+=cnp)
for(jj=jj0; jj<5; ++jj)
pA[jj]=0.0; // pA[ii*cnp+jj]=0.0;
}
}
}
}
/* BUNDLE ADJUSTMENT FOR STRUCTURE PARAMETERS ONLY */
/* Given a parameter vector p made up of the 3D coordinates of n
* points, compute in hx the prediction of the measurements, i.e. the
* projections of 3D points in the m images. The measurements are
* returned in the order (hx_11^T, .. hx_1m^T, ..., hx_n1^T,
* .. hx_nm^T)^T, where hx_ij is the predicted projection of the i-th
* point on the j-th camera. Notice that depending on idxij, some of
* the hx_ij might be missing
*
*/
static void img_projsKS_x(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *hx, void *adata)
{
register int i, j;
int cnp, pnp, mnp;
double *pqr, *pt, *ppt, *pmeas, *pcalib, *camparams, trot[FULLQUATSZ];
//int n;
int m, nnz;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp; pnp=gl->pnp; mnp=gl->mnp;
camparams=gl->camparams;
//n=idxij->nr;
m=idxij->nc;
for(j=0; j<m; ++j){
/* j-th camera parameters */
pcalib=camparams+j*cnp;
pqr=pcalib+5;
pt=pqr+3; // quaternion vector part has 3 elements
_MK_QUAT_FRM_VEC(trot, pqr);
nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs); /* find nonzero hx_ij, i=0...n-1 */
for(i=0; i<nnz; ++i){
ppt=p + rcsubs[i]*pnp;
pmeas=hx + idxij->val[rcidxs[i]]*mnp; // set pmeas to point to hx_ij
calcImgProjFullR(pcalib, trot, pt, ppt, pmeas); // evaluate Q in pmeas
//calcImgProj(pcalib, (double *)zerorotquat, pqr, pt, ppt, pmeas); // evaluate Q in pmeas
}
}
}
/* Given a parameter vector p made up of the 3D coordinates of n
* points, compute in jac the jacobian of the predicted measurements,
* i.e. the jacobian of the projections of 3D points in the m images.
* The jacobian is returned in the order (B_11, ..., B_1m, ..., B_n1,
* ..., B_nm), where B_ij=dx_ij/db_i (see HZ). Notice that depending
* on idxij, some of the B_ij might be missing
*
*/
static void img_projsKS_jac_x(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *jac, void *adata)
{
register int i, j;
int cnp, pnp, mnp;
double *pqr, *pt, *ppt, *pB, *pcalib, *camparams;
//int n;
int m, nnz, Bsz;
struct globs_ *gl;
gl=(struct globs_ *)adata;
cnp=gl->cnp; pnp=gl->pnp; mnp=gl->mnp;
camparams=gl->camparams;
//n=idxij->nr;
m=idxij->nc;
Bsz=mnp*pnp;
for(j=0; j<m; ++j){
/* j-th camera parameters */
pcalib=camparams+j*cnp;
pqr=pcalib+5;
pt=pqr+3; // quaternion vector part has 3 elements
nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs); /* find nonzero hx_ij, i=0...n-1 */
for(i=0; i<nnz; ++i){
ppt=p + rcsubs[i]*pnp;