-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathp4pf.c
892 lines (760 loc) · 39 KB
/
p4pf.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
/*
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Minimal solution to the P4Pf problem from 3D - 2D correspondences:
// Assuming four known 3D points projecting on an image, the pose of
// the camera as well as its focal length are estimated. A simplified
// model is employed for the remaining intrinsics, i.e. square pixels,
// principal point on the image center.
//
// Implements the algorithm in
// M. Bujnak et al., "A general solution to the P4P problem for camera with unknown focal length", CVPR 2008
//
// Coded in C based on a matlab implementation by the authors (see http://cmp.felk.cvut.cz/minimal/p4pf.php)
// by Manolis Lourakis (lourakis **at** ics.forth.gr)
// Institute of Computer Science, Foundation for Research & Technology - Hellas
// Heraklion, Crete, Greece, November 2011.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <math.h>
#include "compiler.h"
#include "p4pf.h"
#include "svd3.h"
/* LAPACK prototypes */
/* eigenvalues & eigenvectors for NxN real nonsymmetric matrix */
extern int F77_FUNC(dgeev)(
char *jobvl, char *jobvr, int *n, double *a, int *lda, double *wr, double *wi,
double *vl, int *ldvl, double *vr, int *ldvr, double *work, int *lwork, int *info);
#define SQR(x) ( (x)*(x) )
#define DABS(x) ( ((x)>0)? (x) : -(x) )
/*
// P4P + unknown focal length MEX helper
// given a set of 4x 2D<->3D correspondences, calculate action matrix
//
// by Martin Bujnak, (c)apr2008
//
//
// Please refer to the following paper, when using this code :
//
// Bujnak, M., Kukelova, Z., and Pajdla, T. A general solution to the p4p
// problem for camera with unknown focal length. CVPR 2008, Anchorage,
// Alaska, USA, June 2008
//
*/
static void GJ(double *A, int rcnt, int ccnt, double tol)
{
int r = 0; /* row */
int c = 0; /* col */
int k;
int l;
int dstofs;
int srcofs;
int ofs = 0;
int pofs = 0;
double pivot_i;
double b;
/* gj */
ofs = 0;
pofs = 0;
while (r < rcnt && c < ccnt) {
/* find pivot */
double apivot = 0;
double pivot = 0;
int pivot_r = -1;
pofs = ofs;
for (k = r; k < rcnt; k++) {
/* pivot selection criteria here ! */
if (DABS(*(A+pofs)) > apivot) {
pivot = *(A+pofs);
apivot = DABS(pivot);
pivot_r = k;
}
pofs += ccnt;
}
if (apivot < tol) {
/* empty col - shift to next col (or jump) */
c++;
ofs++;
} else {
/* process rows */
pivot_i = 1.0/pivot;
/* exchange pivot and selected rows */
/* + divide row */
if (pivot_r == r) {
srcofs = ofs;
for (l = c; l < ccnt; l++) {
*(A+srcofs) = *(A+srcofs)*pivot_i;
srcofs++;
}
} else {
srcofs = ofs;
dstofs = ccnt*pivot_r+c;
for (l = c; l < ccnt; l++) {
b = *(A+srcofs);
*(A+srcofs) = *(A+dstofs)*pivot_i;
*(A+dstofs) = b;
srcofs++;
dstofs++;
}
}
/* zero bottom */
pofs = ofs + ccnt;
for (k = r + 1; k < rcnt; k++) {
if (DABS(*(A+pofs)) > tol) {
/* nonzero row */
b = *(A+pofs);
dstofs = pofs + 1;
srcofs = ofs + 1;
for (l = c + 1; l < ccnt; l++) {
*(A+dstofs) = (*(A+dstofs) - *(A+srcofs) * b);
dstofs++;
srcofs++;
}
*(A+pofs) = 0;
}
pofs += ccnt;
}
/* zero top */
pofs = c;
for (k = 0; k < r; k++) {
if (DABS(*(A+pofs)) > tol) {
/* nonzero row */
b = *(A+pofs);
dstofs = pofs + 1;
srcofs = ofs + 1;
for (l = c + 1; l < ccnt; l++) {
*(A+dstofs) = (*(A+dstofs) - *(A+srcofs) * b);
dstofs++;
srcofs++;
}
*(A+pofs) = 0;
}
pofs += ccnt;
}
r++;
c++;
ofs += ccnt + 1;
}
}
}
/* prepare polynomial coefficients */
static void CalcCoefs(double const *src1, double const *src2, double const *src3, double const *src4, double const *src5, double *dst1)
{
/* symbolic names. */
#define glab (src1[0])
#define glac (src1[1])
#define glad (src1[2])
#define glbc (src1[3])
#define glbd (src1[4])
#define glcd (src1[5])
#define a1 (src2[0])
#define a2 (src2[1])
#define b1 (src3[0])
#define b2 (src3[1])
#define c1 (src4[0])
#define c2 (src4[1])
#define d1 (src5[0])
#define d2 (src5[1])
double t1;
double t11;
double t12;
double t13;
double t14;
double t16;
double t2;
double t24;
double t27;
double t28;
double t3;
double t32;
double t33;
double t34;
double t35;
double t37;
double t39;
double t4;
double t41;
double t42;
double t43;
double t46;
double t5;
double t51;
double t53;
double t56;
double t59;
double t60;
double t67;
double t84;
double t9;
/* consts */
/* destination group 1 */
t1 = 1/glad;
t2 = t1*glbc;
t3 = glab*t1;
t4 = glac*t1;
t5 = t2-t3-t4;
t9 = d2*d2;
t11 = t3*t9;
t12 = t4*t9;
t13 = d1*d1;
t14 = t4*t13;
t16 = t3*t13;
t24 = -a2*b2-b1*a1;
t27 = -c1*a1-c2*a2;
t28 = a1*t1;
t32 = t1*d1;
t33 = a1*glac*t32;
t34 = a2*d2;
t35 = t4*t34;
t37 = a1*glab*t32;
t39 = t3*t34;
t41 = a2*a2;
t42 = a1*a1;
t43 = t4*t41;
t46 = t42*glac*t1;
t51 = t42*glab*t1;
t53 = t42*t1;
t56 = t3*t41;
t59 = c1*c1;
t60 = c2*c2;
t67 = t1*glbd;
t84 = glcd*t1;
/* destination group 2 */
/* destination group 0 */
/* destination group 1 */
dst1[0] = 1.0;
dst1[1] = t5/2.0;
dst1[2] = -1.0;
dst1[3] = -1.0;
dst1[4] = c2*b2+c1*b1;
dst1[5] = -t5;
dst1[6] = t2*t9/2.0-t11/2.0-t12/2.0-t14/2.0+t2*t13/2.0-t16/2.0;
dst1[7] = 1.0-t4/2.0-t3/2.0+t2/2.0;
dst1[8] = t24;
dst1[9] = t27;
dst1[10] = -t28*glbc*d1+t33+t35+t37-t2*t34+t39;
dst1[11] = t41+t42-t43/2.0-t46/2.0+t2*t41/2.0-t51/2.0+t53*glbc/2.0-t56/2.0;
dst1[12] = 1.0;
dst1[13] = -t4;
dst1[14] = -2.0;
dst1[15] = t59+t60;
dst1[16] = 2.0*t4;
dst1[17] = -t14-t12;
dst1[18] = -t4+1.0;
dst1[19] = 2.0*t27;
dst1[20] = 2.0*t33+2.0*t35;
dst1[21] = -t43+t41+t42-t46;
dst1[22] = 1.0;
dst1[23] = t67/2.0-1.0/2.0-t3/2.0;
dst1[24] = -1.0;
dst1[25] = t3-t67;
dst1[26] = d2*b2+b1*d1;
dst1[27] = -t11/2.0-t16/2.0+t67*t9/2.0+t67*t13/2.0-t9/2.0-t13/2.0;
dst1[28] = -t3/2.0+t67/2.0+1.0/2.0;
dst1[29] = t24;
dst1[30] = -t28*glbd*d1+t37+t39-t67*t34;
dst1[31] = t67*t41/2.0+t53*glbd/2.0-t56/2.0-t51/2.0+t42/2.0+t41/2.0;
dst1[32] = 1.0;
dst1[33] = -t4/2.0+t84/2.0-1.0/2.0;
dst1[34] = -1.0;
dst1[35] = t4-t84;
dst1[36] = c1*d1+c2*d2;
dst1[37] = t84*t13/2.0-t12/2.0-t14/2.0-t13/2.0-t9/2.0+t84*t9/2.0;
dst1[38] = -t4/2.0+1.0/2.0+t84/2.0;
dst1[39] = t27;
dst1[40] = t35+t33-t84*t34-glcd*a1*t32;
dst1[41] = t42/2.0+t41/2.0-t43/2.0+glcd*t42*t1/2.0-t46/2.0+t84*t41/2.0;
/* destination group 2 */
#undef glab
#undef glac
#undef glad
#undef glbc
#undef glbd
#undef glcd
#undef a1
#undef a2
#undef b1
#undef b2
#undef c1
#undef c2
#undef d1
#undef d2
}
/*
// [glab, glac, glad, glbc, glbd, glcd], [a1; a2], [b1; b2], [c1; c2], [d1;d2]
// glXY - ||X-Y||^2 - quadratic distances between 3D points X and Y
// a1 (a2) = x (resp y) measurement of the first 2D point
// b1 (b2) = x (resp y) measurement of the second 2D point
// c1 (c2) = x (resp y) measurement of the third 2D point
// d1 (d2) = x (resp y) measurement of the fourth 2D point
//
// output *A - 10x10 action matrix
//
*/
static void p4pfmex(double *glab, double *a1, double *b1, double *c1, double *d1, double *A)
{
/* precalculate polynomial equations coefficients */
double M[6864];
double coefs[42];
CalcCoefs(glab, a1, b1, c1, d1, coefs);
memset(M, 0, 6864*sizeof(double));
M[64] = coefs[0]; M[403] = coefs[0]; M[486] = coefs[0]; M[572] = coefs[0]; M[1533] = coefs[0]; M[1616] = coefs[0]; M[1702] = coefs[0]; M[1787] = coefs[0]; M[1874] = coefs[0]; M[1960] = coefs[0]; M[3979] = coefs[0]; M[4063] = coefs[0]; M[4149] = coefs[0]; M[4234] = coefs[0]; M[4321] = coefs[0]; M[4407] = coefs[0]; M[4494] = coefs[0]; M[6161] = coefs[0]; M[6248] = coefs[0];
M[71] = coefs[1]; M[411] = coefs[1]; M[496] = coefs[1]; M[582] = coefs[1]; M[1539] = coefs[1]; M[1626] = coefs[1]; M[1712] = coefs[1]; M[1798] = coefs[1]; M[1884] = coefs[1]; M[4071] = coefs[1]; M[4157] = coefs[1]; M[4244] = coefs[1]; M[4330] = coefs[1]; M[4416] = coefs[1]; M[4500] = coefs[1]; M[6165] = coefs[1]; M[6252] = coefs[1];
M[75] = coefs[2]; M[419] = coefs[2]; M[504] = coefs[2]; M[590] = coefs[2]; M[1551] = coefs[2]; M[1635] = coefs[2]; M[1721] = coefs[2]; M[1806] = coefs[2]; M[1892] = coefs[2]; M[4001] = coefs[2]; M[4085] = coefs[2]; M[4171] = coefs[2]; M[4256] = coefs[2]; M[4342] = coefs[2]; M[4428] = coefs[2]; M[4512] = coefs[2]; M[6171] = coefs[2]; M[6255] = coefs[2];
M[76] = coefs[3]; M[420] = coefs[3]; M[505] = coefs[3]; M[591] = coefs[3]; M[1552] = coefs[3]; M[1636] = coefs[3]; M[1722] = coefs[3]; M[1807] = coefs[3]; M[1893] = coefs[3]; M[4002] = coefs[3]; M[4086] = coefs[3]; M[4172] = coefs[3]; M[4257] = coefs[3]; M[4343] = coefs[3]; M[4429] = coefs[3]; M[4513] = coefs[3]; M[6172] = coefs[3]; M[6256] = coefs[3];
M[77] = coefs[4]; M[421] = coefs[4]; M[506] = coefs[4]; M[592] = coefs[4]; M[1553] = coefs[4]; M[1637] = coefs[4]; M[1723] = coefs[4]; M[1808] = coefs[4]; M[1894] = coefs[4]; M[1980] = coefs[4]; M[4087] = coefs[4]; M[4173] = coefs[4]; M[4258] = coefs[4]; M[4344] = coefs[4]; M[4430] = coefs[4]; M[4514] = coefs[4]; M[6173] = coefs[4]; M[6257] = coefs[4];
M[79] = coefs[5]; M[423] = coefs[5]; M[508] = coefs[5]; M[1555] = coefs[5]; M[1640] = coefs[5]; M[1726] = coefs[5]; M[1812] = coefs[5]; M[1898] = coefs[5]; M[4003] = coefs[5]; M[4090] = coefs[5]; M[4176] = coefs[5]; M[4262] = coefs[5]; M[4348] = coefs[5]; M[4517] = coefs[5]; M[6176] = coefs[5]; M[6260] = coefs[5];
M[82] = coefs[6]; M[426] = coefs[6]; M[513] = coefs[6]; M[599] = coefs[6]; M[1645] = coefs[6]; M[1731] = coefs[6]; M[1818] = coefs[6]; M[1904] = coefs[6]; M[1990] = coefs[6]; M[4179] = coefs[6]; M[4354] = coefs[6]; M[4440] = coefs[6]; M[4524] = coefs[6]; M[6181] = coefs[6]; M[6266] = coefs[6];
M[83] = coefs[7]; M[431] = coefs[7]; M[516] = coefs[7]; M[1567] = coefs[7]; M[1652] = coefs[7]; M[1825] = coefs[7]; M[1911] = coefs[7]; M[4019] = coefs[7]; M[4104] = coefs[7]; M[4190] = coefs[7]; M[4276] = coefs[7]; M[4362] = coefs[7]; M[6277] = coefs[7];
M[84] = coefs[8]; M[432] = coefs[8]; M[517] = coefs[8]; M[603] = coefs[8]; M[1568] = coefs[8]; M[1653] = coefs[8]; M[1739] = coefs[8]; M[1826] = coefs[8]; M[1912] = coefs[8]; M[1998] = coefs[8]; M[4020] = coefs[8]; M[4105] = coefs[8]; M[4191] = coefs[8]; M[4277] = coefs[8]; M[4363] = coefs[8]; M[4449] = coefs[8]; M[4532] = coefs[8]; M[6195] = coefs[8]; M[6278] = coefs[8];
M[85] = coefs[9]; M[433] = coefs[9]; M[518] = coefs[9]; M[604] = coefs[9]; M[1569] = coefs[9]; M[1654] = coefs[9]; M[1740] = coefs[9]; M[1913] = coefs[9]; M[1999] = coefs[9]; M[4021] = coefs[9]; M[4106] = coefs[9]; M[4192] = coefs[9]; M[4364] = coefs[9]; M[4450] = coefs[9]; M[4533] = coefs[9]; M[6196] = coefs[9]; M[6279] = coefs[9];
M[86] = coefs[10]; M[434] = coefs[10]; M[521] = coefs[10]; M[607] = coefs[10]; M[1570] = coefs[10]; M[1657] = coefs[10]; M[1743] = coefs[10]; M[1830] = coefs[10]; M[1916] = coefs[10]; M[4109] = coefs[10]; M[4195] = coefs[10]; M[4282] = coefs[10]; M[4368] = coefs[10]; M[4454] = coefs[10]; M[4538] = coefs[10]; M[6200] = coefs[10]; M[6284] = coefs[10];
M[87] = coefs[11]; M[438] = coefs[11]; M[525] = coefs[11]; M[611] = coefs[11]; M[1578] = coefs[11]; M[1665] = coefs[11]; M[1751] = coefs[11]; M[1838] = coefs[11]; M[1924] = coefs[11]; M[4034] = coefs[11]; M[4121] = coefs[11]; M[4207] = coefs[11]; M[4294] = coefs[11]; M[4380] = coefs[11]; M[4551] = coefs[11]; M[6214] = coefs[11]; M[6298] = coefs[11];
M[153] = coefs[12]; M[668] = coefs[12]; M[750] = coefs[12]; M[837] = coefs[12]; M[2062] = coefs[12]; M[2145] = coefs[12]; M[2232] = coefs[12]; M[2319] = coefs[12]; M[2403] = coefs[12]; M[2490] = coefs[12]; M[2577] = coefs[12]; M[4596] = coefs[12]; M[4679] = coefs[12]; M[4766] = coefs[12]; M[4850] = coefs[12]; M[4937] = coefs[12]; M[5024] = coefs[12]; M[5110] = coefs[12]; M[6338] = coefs[12]; M[6424] = coefs[12];
M[159] = coefs[13]; M[675] = coefs[13]; M[759] = coefs[13]; M[846] = coefs[13]; M[2067] = coefs[13]; M[2154] = coefs[13]; M[2241] = coefs[13]; M[2328] = coefs[13]; M[2413] = coefs[13]; M[2499] = coefs[13]; M[4686] = coefs[13]; M[4773] = coefs[13]; M[4859] = coefs[13]; M[4945] = coefs[13]; M[5032] = coefs[13]; M[5115] = coefs[13]; M[6341] = coefs[13]; M[6427] = coefs[13];
M[164] = coefs[14]; M[684] = coefs[14]; M[768] = coefs[14]; M[855] = coefs[14]; M[2080] = coefs[14]; M[2164] = coefs[14]; M[2251] = coefs[14]; M[2338] = coefs[14]; M[2422] = coefs[14]; M[2508] = coefs[14]; M[4618] = coefs[14]; M[4701] = coefs[14]; M[4788] = coefs[14]; M[4872] = coefs[14]; M[4958] = coefs[14]; M[5045] = coefs[14]; M[5128] = coefs[14]; M[6348] = coefs[14]; M[6431] = coefs[14];
M[166] = coefs[15]; M[686] = coefs[15]; M[770] = coefs[15]; M[857] = coefs[15]; M[2082] = coefs[15]; M[2253] = coefs[15]; M[2340] = coefs[15]; M[2424] = coefs[15]; M[2510] = coefs[15]; M[2597] = coefs[15]; M[4703] = coefs[15]; M[4790] = coefs[15]; M[4874] = coefs[15]; M[4960] = coefs[15]; M[5047] = coefs[15]; M[5130] = coefs[15]; M[6350] = coefs[15]; M[6433] = coefs[15];
M[167] = coefs[16]; M[687] = coefs[16]; M[771] = coefs[16]; M[2083] = coefs[16]; M[2168] = coefs[16]; M[2255] = coefs[16]; M[2342] = coefs[16]; M[2427] = coefs[16]; M[2513] = coefs[16]; M[4619] = coefs[16]; M[4705] = coefs[16]; M[4792] = coefs[16]; M[4877] = coefs[16]; M[4963] = coefs[16]; M[5132] = coefs[16]; M[6352] = coefs[16]; M[6435] = coefs[16];
M[170] = coefs[17]; M[690] = coefs[17]; M[776] = coefs[17]; M[863] = coefs[17]; M[2173] = coefs[17]; M[2260] = coefs[17]; M[2347] = coefs[17]; M[2433] = coefs[17]; M[2519] = coefs[17]; M[2606] = coefs[17]; M[4795] = coefs[17]; M[4969] = coefs[17]; M[5056] = coefs[17]; M[5139] = coefs[17]; M[6357] = coefs[17]; M[6441] = coefs[17];
M[171] = coefs[18]; M[695] = coefs[18]; M[779] = coefs[18]; M[2095] = coefs[18]; M[2180] = coefs[18]; M[2267] = coefs[18]; M[2440] = coefs[18]; M[2526] = coefs[18]; M[4635] = coefs[18]; M[4719] = coefs[18]; M[4806] = coefs[18]; M[4891] = coefs[18]; M[4977] = coefs[18]; M[6452] = coefs[18];
M[173] = coefs[19]; M[697] = coefs[19]; M[781] = coefs[19]; M[868] = coefs[19]; M[2097] = coefs[19]; M[2182] = coefs[19]; M[2269] = coefs[19]; M[2356] = coefs[19]; M[2442] = coefs[19]; M[2528] = coefs[19]; M[2615] = coefs[19]; M[4637] = coefs[19]; M[4721] = coefs[19]; M[4808] = coefs[19]; M[4893] = coefs[19]; M[4979] = coefs[19]; M[5066] = coefs[19]; M[5148] = coefs[19]; M[6372] = coefs[19]; M[6454] = coefs[19];
M[174] = coefs[20]; M[698] = coefs[20]; M[784] = coefs[20]; M[871] = coefs[20]; M[2098] = coefs[20]; M[2185] = coefs[20]; M[2272] = coefs[20]; M[2359] = coefs[20]; M[2445] = coefs[20]; M[2531] = coefs[20]; M[4724] = coefs[20]; M[4811] = coefs[20]; M[4897] = coefs[20]; M[4983] = coefs[20]; M[5070] = coefs[20]; M[5153] = coefs[20]; M[6376] = coefs[20]; M[6459] = coefs[20];
M[175] = coefs[21]; M[702] = coefs[21]; M[788] = coefs[21]; M[875] = coefs[21]; M[2106] = coefs[21]; M[2193] = coefs[21]; M[2280] = coefs[21]; M[2367] = coefs[21]; M[2453] = coefs[21]; M[2539] = coefs[21]; M[4650] = coefs[21]; M[4736] = coefs[21]; M[4823] = coefs[21]; M[4909] = coefs[21]; M[4995] = coefs[21]; M[5166] = coefs[21]; M[6390] = coefs[21]; M[6473] = coefs[21];
M[243] = coefs[22]; M[935] = coefs[22]; M[1019] = coefs[22]; M[1105] = coefs[22]; M[2681] = coefs[22]; M[2765] = coefs[22]; M[2851] = coefs[22]; M[2936] = coefs[22]; M[3022] = coefs[22]; M[3108] = coefs[22]; M[5209] = coefs[22]; M[5293] = coefs[22]; M[5379] = coefs[22]; M[5463] = coefs[22]; M[6515] = coefs[22]; M[6601] = coefs[22];
M[247] = coefs[23]; M[939] = coefs[23]; M[1024] = coefs[23]; M[1110] = coefs[23]; M[2683] = coefs[23]; M[2770] = coefs[23]; M[2856] = coefs[23]; M[2942] = coefs[23]; M[3028] = coefs[23]; M[5213] = coefs[23]; M[5298] = coefs[23]; M[5384] = coefs[23]; M[5468] = coefs[23]; M[6517] = coefs[23]; M[6604] = coefs[23];
M[251] = coefs[24]; M[947] = coefs[24]; M[1032] = coefs[24]; M[1118] = coefs[24]; M[2695] = coefs[24]; M[2779] = coefs[24]; M[2865] = coefs[24]; M[2950] = coefs[24]; M[3036] = coefs[24]; M[5227] = coefs[24]; M[5310] = coefs[24]; M[5396] = coefs[24]; M[5480] = coefs[24]; M[6523] = coefs[24]; M[6607] = coefs[24];
M[255] = coefs[25]; M[951] = coefs[25]; M[1036] = coefs[25]; M[2699] = coefs[25]; M[2784] = coefs[25]; M[2870] = coefs[25]; M[2956] = coefs[25]; M[3042] = coefs[25]; M[5232] = coefs[25]; M[5316] = coefs[25]; M[5485] = coefs[25]; M[6528] = coefs[25]; M[6612] = coefs[25];
M[256] = coefs[26]; M[952] = coefs[26]; M[1037] = coefs[26]; M[1123] = coefs[26]; M[2700] = coefs[26]; M[2785] = coefs[26]; M[2871] = coefs[26]; M[2957] = coefs[26]; M[3043] = coefs[26]; M[3129] = coefs[26]; M[5233] = coefs[26]; M[5317] = coefs[26]; M[5403] = coefs[26]; M[5486] = coefs[26]; M[6529] = coefs[26]; M[6613] = coefs[26];
M[258] = coefs[27]; M[954] = coefs[27]; M[1041] = coefs[27]; M[1127] = coefs[27]; M[2789] = coefs[27]; M[2875] = coefs[27]; M[2962] = coefs[27]; M[3048] = coefs[27]; M[3134] = coefs[27]; M[5235] = coefs[27]; M[5322] = coefs[27]; M[5408] = coefs[27]; M[5492] = coefs[27]; M[6533] = coefs[27]; M[6618] = coefs[27];
M[259] = coefs[28]; M[959] = coefs[28]; M[1044] = coefs[28]; M[2711] = coefs[28]; M[2796] = coefs[28]; M[2969] = coefs[28]; M[3055] = coefs[28]; M[5246] = coefs[28]; M[5330] = coefs[28]; M[6629] = coefs[28];
M[260] = coefs[29]; M[960] = coefs[29]; M[1045] = coefs[29]; M[1131] = coefs[29]; M[2712] = coefs[29]; M[2797] = coefs[29]; M[2883] = coefs[29]; M[2970] = coefs[29]; M[3056] = coefs[29]; M[3142] = coefs[29]; M[5247] = coefs[29]; M[5331] = coefs[29]; M[5417] = coefs[29]; M[5500] = coefs[29]; M[6547] = coefs[29]; M[6630] = coefs[29];
M[262] = coefs[30]; M[962] = coefs[30]; M[1049] = coefs[30]; M[1135] = coefs[30]; M[2714] = coefs[30]; M[2801] = coefs[30]; M[2887] = coefs[30]; M[2974] = coefs[30]; M[3060] = coefs[30]; M[5251] = coefs[30]; M[5336] = coefs[30]; M[5422] = coefs[30]; M[5506] = coefs[30]; M[6552] = coefs[30]; M[6636] = coefs[30];
M[263] = coefs[31]; M[966] = coefs[31]; M[1053] = coefs[31]; M[1139] = coefs[31]; M[2722] = coefs[31]; M[2809] = coefs[31]; M[2895] = coefs[31]; M[2982] = coefs[31]; M[3068] = coefs[31]; M[5263] = coefs[31]; M[5348] = coefs[31]; M[5519] = coefs[31]; M[6566] = coefs[31]; M[6650] = coefs[31];
M[332] = coefs[32]; M[1200] = coefs[32]; M[1284] = coefs[32]; M[1371] = coefs[32]; M[1458] = coefs[32]; M[3210] = coefs[32]; M[3294] = coefs[32]; M[3381] = coefs[32]; M[3468] = coefs[32]; M[3553] = coefs[32]; M[3640] = coefs[32]; M[3727] = coefs[32]; M[3814] = coefs[32]; M[3901] = coefs[32]; M[5564] = coefs[32]; M[5651] = coefs[32]; M[5738] = coefs[32]; M[5822] = coefs[32]; M[5908] = coefs[32]; M[5994] = coefs[32]; M[6080] = coefs[32]; M[6692] = coefs[32]; M[6778] = coefs[32];
M[335] = coefs[33]; M[1203] = coefs[33]; M[1288] = coefs[33]; M[1375] = coefs[33]; M[1462] = coefs[33]; M[3211] = coefs[33]; M[3298] = coefs[33]; M[3385] = coefs[33]; M[3472] = coefs[33]; M[3558] = coefs[33]; M[3645] = coefs[33]; M[3732] = coefs[33]; M[3819] = coefs[33]; M[5567] = coefs[33]; M[5654] = coefs[33]; M[5741] = coefs[33]; M[5826] = coefs[33]; M[5912] = coefs[33]; M[5999] = coefs[33]; M[6084] = coefs[33]; M[6693] = coefs[33]; M[6780] = coefs[33];
M[340] = coefs[34]; M[1212] = coefs[34]; M[1297] = coefs[34]; M[1384] = coefs[34]; M[1471] = coefs[34]; M[3224] = coefs[34]; M[3308] = coefs[34]; M[3395] = coefs[34]; M[3482] = coefs[34]; M[3567] = coefs[34]; M[3654] = coefs[34]; M[3741] = coefs[34]; M[3828] = coefs[34]; M[5582] = coefs[34]; M[5669] = coefs[34]; M[5756] = coefs[34]; M[5839] = coefs[34]; M[5925] = coefs[34]; M[6011] = coefs[34]; M[6097] = coefs[34]; M[6700] = coefs[34]; M[6784] = coefs[34];
M[343] = coefs[35]; M[1215] = coefs[35]; M[1300] = coefs[35]; M[1387] = coefs[35]; M[3227] = coefs[35]; M[3312] = coefs[35]; M[3399] = coefs[35]; M[3486] = coefs[35]; M[3572] = coefs[35]; M[3659] = coefs[35]; M[3746] = coefs[35]; M[3833] = coefs[35]; M[5586] = coefs[35]; M[5673] = coefs[35]; M[5760] = coefs[35]; M[5844] = coefs[35]; M[6016] = coefs[35]; M[6101] = coefs[35]; M[6704] = coefs[35]; M[6788] = coefs[35];
M[345] = coefs[36]; M[1217] = coefs[36]; M[1302] = coefs[36]; M[1389] = coefs[36]; M[1476] = coefs[36]; M[3229] = coefs[36]; M[3314] = coefs[36]; M[3401] = coefs[36]; M[3488] = coefs[36]; M[3661] = coefs[36]; M[3748] = coefs[36]; M[3835] = coefs[36]; M[3922] = coefs[36]; M[5762] = coefs[36]; M[5846] = coefs[36]; M[5932] = coefs[36]; M[6018] = coefs[36]; M[6103] = coefs[36]; M[6706] = coefs[36]; M[6790] = coefs[36];
M[346] = coefs[37]; M[1218] = coefs[37]; M[1305] = coefs[37]; M[1392] = coefs[37]; M[1479] = coefs[37]; M[3317] = coefs[37]; M[3404] = coefs[37]; M[3491] = coefs[37]; M[3578] = coefs[37]; M[3665] = coefs[37]; M[3752] = coefs[37]; M[3839] = coefs[37]; M[3926] = coefs[37]; M[5763] = coefs[37]; M[5850] = coefs[37]; M[5936] = coefs[37]; M[6023] = coefs[37]; M[6108] = coefs[37]; M[6709] = coefs[37]; M[6794] = coefs[37];
M[347] = coefs[38]; M[1223] = coefs[38]; M[1308] = coefs[38]; M[1395] = coefs[38]; M[3239] = coefs[38]; M[3324] = coefs[38]; M[3411] = coefs[38]; M[3585] = coefs[38]; M[3672] = coefs[38]; M[3759] = coefs[38]; M[3846] = coefs[38]; M[5600] = coefs[38]; M[5687] = coefs[38]; M[5774] = coefs[38]; M[5858] = coefs[38]; M[6030] = coefs[38]; M[6805] = coefs[38];
M[349] = coefs[39]; M[1225] = coefs[39]; M[1310] = coefs[39]; M[1397] = coefs[39]; M[1484] = coefs[39]; M[3241] = coefs[39]; M[3326] = coefs[39]; M[3413] = coefs[39]; M[3500] = coefs[39]; M[3674] = coefs[39]; M[3761] = coefs[39]; M[3848] = coefs[39]; M[3935] = coefs[39]; M[5602] = coefs[39]; M[5689] = coefs[39]; M[5776] = coefs[39]; M[5860] = coefs[39]; M[5946] = coefs[39]; M[6032] = coefs[39]; M[6117] = coefs[39]; M[6724] = coefs[39]; M[6807] = coefs[39];
M[350] = coefs[40]; M[1226] = coefs[40]; M[1313] = coefs[40]; M[1400] = coefs[40]; M[1487] = coefs[40]; M[3242] = coefs[40]; M[3329] = coefs[40]; M[3416] = coefs[40]; M[3503] = coefs[40]; M[3590] = coefs[40]; M[3677] = coefs[40]; M[3764] = coefs[40]; M[3851] = coefs[40]; M[5605] = coefs[40]; M[5692] = coefs[40]; M[5779] = coefs[40]; M[5864] = coefs[40]; M[5950] = coefs[40]; M[6037] = coefs[40]; M[6122] = coefs[40]; M[6728] = coefs[40]; M[6812] = coefs[40];
M[351] = coefs[41]; M[1230] = coefs[41]; M[1317] = coefs[41]; M[1404] = coefs[41]; M[1491] = coefs[41]; M[3250] = coefs[41]; M[3337] = coefs[41]; M[3424] = coefs[41]; M[3511] = coefs[41]; M[3598] = coefs[41]; M[3685] = coefs[41]; M[3772] = coefs[41]; M[3859] = coefs[41]; M[5617] = coefs[41]; M[5704] = coefs[41]; M[5791] = coefs[41]; M[5876] = coefs[41]; M[6050] = coefs[41]; M[6135] = coefs[41]; M[6742] = coefs[41]; M[6826] = coefs[41];
/* GJ elimination */
GJ(M, 78, 88, 2.2204e-11);
/* action matrix */
memset(A, 0, sizeof(double)*100);
A[1] = 1;
A[15] = 1;
A[26] = 1;
A[37] = 1;
A[48] = 1;
A[50] = -M[6599]; A[51] = -M[6598]; A[52] = -M[6597]; A[53] = -M[6596]; A[54] = -M[6595]; A[55] = -M[6594]; A[56] = -M[6593]; A[57] = -M[6592]; A[58] = -M[6591]; A[59] = -M[6590];
A[60] = -M[6511]; A[61] = -M[6510]; A[62] = -M[6509]; A[63] = -M[6508]; A[64] = -M[6507]; A[65] = -M[6506]; A[66] = -M[6505]; A[67] = -M[6504]; A[68] = -M[6503]; A[69] = -M[6502];
A[70] = -M[6423]; A[71] = -M[6422]; A[72] = -M[6421]; A[73] = -M[6420]; A[74] = -M[6419]; A[75] = -M[6418]; A[76] = -M[6417]; A[77] = -M[6416]; A[78] = -M[6415]; A[79] = -M[6414];
A[80] = -M[6335]; A[81] = -M[6334]; A[82] = -M[6333]; A[83] = -M[6332]; A[84] = -M[6331]; A[85] = -M[6330]; A[86] = -M[6329]; A[87] = -M[6328]; A[88] = -M[6327]; A[89] = -M[6326];
A[90] = -M[6247]; A[91] = -M[6246]; A[92] = -M[6245]; A[93] = -M[6244]; A[94] = -M[6243]; A[95] = -M[6242]; A[96] = -M[6241]; A[97] = -M[6240]; A[98] = -M[6239]; A[99] = -M[6238];
}
/* ################################################################################################## */
/*
find rigid transformation (rotation translation) from p0->p1 given
3 or more points in the 3D space
based on : K.Arun, T.Huangs, D.Blostein. Least-squares
fitting of two 3D point sets IEEE PAMI 1987
based on a matlab implementation by
by Martin Bujnak, nov2007
Set LHsys=1 for a left-handed coordinate system
returns 0 on success, 1 on failure
*/
static int getXform(double (*p0)[3], double (*p1)[3], int npts, int LHsys, double R[9], double t[3])
{
register int i;
register double tmp;
double p0mean[3], p1mean[3];
double (*u1)[3], (*u2)[3], C[3*3];
/* SVD stuff */
double S[3], U[3*3], Vt[3*3];
u1=(double (*)[3])malloc(npts*sizeof(double[3]));
u2=(double (*)[3])malloc(npts*sizeof(double[3]));
#if 0
for(i=0; i<3; ++i)
printf("%d: %g %g %g %g\n", i, p0[0][i], p0[1][i], p0[2][i], p0[3][i]);
printf("\n");
for(i=0; i<3; ++i)
printf("%d: %g %g %g %g\n", i, p1[0][i], p1[1][i], p1[2][i], p1[3][i]);
#endif
/* shift centers of gravity to the origin */
p0mean[0]=p0mean[1]=p0mean[2]=0.0;
p1mean[0]=p1mean[1]=p1mean[2]=0.0;
for(i=npts; i-->0; ){
p0mean[0]+=p0[i][0];
p0mean[1]+=p0[i][1];
p0mean[2]+=p0[i][2];
p1mean[0]+=p1[i][0];
p1mean[1]+=p1[i][1];
p1mean[2]+=p1[i][2];
}
p0mean[0]/=npts; p0mean[1]/=npts; p0mean[2]/=npts;
p1mean[0]/=npts; p1mean[1]/=npts; p1mean[2]/=npts;
#if 0
printf("MEAN %g %g %g\n", p0mean[0], p0mean[1], p0mean[2]);
printf("MEAN %g %g %g\n", p1mean[0], p1mean[1], p1mean[2]);
#endif
for(i=npts; i-->0; ){
u1[i][0]=p0[i][0]-p0mean[0];
u1[i][1]=p0[i][1]-p0mean[1];
u1[i][2]=p0[i][2]-p0mean[2];
u2[i][0]=p1[i][0]-p1mean[0];
u2[i][1]=p1[i][1]-p1mean[1];
u2[i][2]=p1[i][2]-p1mean[2];
}
/* normalize to unit size */
for(i=npts; i-->0; ){
tmp=1./sqrt(SQR(u1[i][0]) + SQR(u1[i][1]) + SQR(u1[i][2]));
u1[i][0]*=tmp; u1[i][1]*=tmp; u1[i][2]*=tmp;
tmp=1./sqrt(SQR(u2[i][0]) + SQR(u2[i][1]) + SQR(u2[i][2]));
u2[i][0]*=tmp; u2[i][1]*=tmp; u2[i][2]*=tmp;
}
#if 0
for(i=0; i<3; ++i)
printf("%d: %g %g %g %g\n", i, u1[0][i], u1[1][i], u1[2][i], u1[3][i]);
printf("\n");
for(i=0; i<3; ++i)
printf("%d: %g %g %g %g\n", i, u2[0][i], u2[1][i], u2[2][i], u2[3][i]);
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
#endif
/* calc rotation */
/* compute u1^T*u2 which is equivalent to u2^T*u1 in column-major for LAPACK */
for(i=0; i<3; ++i){
C[i*3+0]=u1[0][i]*u2[0][0] + u1[1][i]*u2[1][0] + u1[2][i]*u2[2][0] + u1[3][i]*u2[3][0];
C[i*3+1]=u1[0][i]*u2[0][1] + u1[1][i]*u2[1][1] + u1[2][i]*u2[2][1] + u1[3][i]*u2[3][1];
C[i*3+2]=u1[0][i]*u2[0][2] + u1[1][i]*u2[1][2] + u1[2][i]*u2[2][2] + u1[3][i]*u2[3][2];
}
#if 0
printf("C\n");
printf("%g %g %g\n", C[0], C[1], C[2]);
printf("%g %g %g\n", C[3], C[4], C[5]);
printf("%g %g %g\n", C[6], C[7], C[8]);
#endif
#if 1 // use custom 3x3 SVD
/* a more efficient alternative to the LAPACK-based SVD is to use a variant tailored to
* a 3x3 matrix like this one: http://www.rohitab.com/discuss/topic/36251-c-svd-of-3x3-matrix/
*/
svd3(U, S, Vt, C);
/* svd3 actually returns V; for compatibility with LAPACK which returns V^T,
* the computed Vt is transposed in place to yield the true Vt
*/
tmp=Vt[1]; Vt[1]=Vt[3]; Vt[3]=tmp;
tmp=Vt[2]; Vt[2]=Vt[6]; Vt[6]=tmp;
tmp=Vt[5]; Vt[5]=Vt[7]; Vt[7]=tmp;
#else // use generic SVD
{
double work[32];
int info;
const int three=3, lwork=32;
/* SVD */
extern int F77_FUNC(dgesvd)(
char *jobu, char *jobvt, int *m, int *n, double *a, int *lda, double *s,
double *u, int *ldu, double *vt, int *ldvt, double *work, int *lwork, int *info);
F77_FUNC(dgesvd)("S", "S", (int*)&three, (int*)&three, C, (int*)&three, S, U, (int*)&three, Vt, (int*)&three, work, (int*)&lwork, &info);
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dgesvd in getXform()\n", -info);
exit(1);
}
else if(info>0){
fprintf(stderr, "LAPACK error: dbdsqr did not converge in getXform();\n%d %s", info,
"superdiagonals of an intermediate bidiagonal form did not converge to zero\n");
return 1;
}
}
#endif
/* note that Vt, U are in column major! */
/* fit to rotation space */
S[0]=(S[0]>0.)? 1 : ( (S[0]<0.)? -1. : 0. );
S[1]=(S[1]>0.)? 1 : ( (S[1]<0.)? -1. : 0. );
/* compute U * V^t, save temporarily in R */
R[0]=Vt[0] * U[0] + Vt[1] * U[3] + Vt[2] * U[6];
R[3]=Vt[0] * U[1] + Vt[1] * U[4] + Vt[2] * U[7];
R[6]=Vt[0] * U[2] + Vt[1] * U[5] + Vt[2] * U[8];
R[1]=Vt[3] * U[0] + Vt[4] * U[3] + Vt[5] * U[6];
R[4]=Vt[3] * U[1] + Vt[4] * U[4] + Vt[5] * U[7];
R[7]=Vt[3] * U[2] + Vt[4] * U[5] + Vt[5] * U[8];
R[2]=Vt[6] * U[0] + Vt[7] * U[3] + Vt[8] * U[6];
R[5]=Vt[6] * U[1] + Vt[7] * U[4] + Vt[8] * U[7];
R[8]=Vt[6] * U[2] + Vt[7] * U[5] + Vt[8] * U[8];
/* compute det(U*Vt), code generated by maple */
{
double t1, t11, t13, t14, t15, t17, t18, t2, t20, t21, t23, t4, t5, t8, t9;
t1 = R[4];
t2 = R[8];
t4 = R[5];
t5 = R[7];
t8 = R[0];
t9 = t8*t1;
t11 = t8*t4;
t13 = R[3];
t14 = R[1];
t15 = t13*t14;
t17 = R[2];
t18 = t13*t17;
t20 = R[6];
t21 = t20*t14;
t23 = t20*t17;
tmp = t9*t2-t11*t5-t15*t2+t18*t5+t21*t4-t23*t1;
}
#if 0
printf("R, det %g\n", tmp);
printf("%g %g %g\n", R[0], R[1], R[2]);
printf("%g %g %g\n", R[3], R[4], R[5]);
printf("%g %g %g\n", R[6], R[7], R[8]);
#endif
S[2]=(tmp>0.)? 1 : ( (tmp<0.)? -1. : 0. );
if(LHsys) S[2]*=-1.;
/* compute U*S: equivalent to negating columns U that correspond to negative eigenvalues in S */
if(S[0]<0.0){
U[0]=-U[0]; U[1]=-U[1]; U[2]=-U[2];
}
if(S[1]<0.0){
U[3]=-U[3]; U[4]=-U[4]; U[5]=-U[5];
}
if(S[2]<0.0){
U[6]=-U[6]; U[7]=-U[7]; U[8]=-U[8];
}
/* U*S*Vt */
/* compute R as V * U^t */
R[0]=Vt[0] * U[0] + Vt[1] * U[3] + Vt[2] * U[6];
R[3]=Vt[0] * U[1] + Vt[1] * U[4] + Vt[2] * U[7];
R[6]=Vt[0] * U[2] + Vt[1] * U[5] + Vt[2] * U[8];
R[1]=Vt[3] * U[0] + Vt[4] * U[3] + Vt[5] * U[6];
R[4]=Vt[3] * U[1] + Vt[4] * U[4] + Vt[5] * U[7];
R[7]=Vt[3] * U[2] + Vt[4] * U[5] + Vt[5] * U[8];
R[2]=Vt[6] * U[0] + Vt[7] * U[3] + Vt[8] * U[6];
R[5]=Vt[6] * U[1] + Vt[7] * U[4] + Vt[8] * U[7];
R[8]=Vt[6] * U[2] + Vt[7] * U[5] + Vt[8] * U[8];
#if 0
printf("R\n");
printf("%g %g %g\n", R[0], R[1], R[2]);
printf("%g %g %g\n", R[3], R[4], R[5]);
printf("%g %g %g\n", R[6], R[7], R[8]);
#endif
/* t = p1mean - R*p0mean */
t[0]=p1mean[0] - R[0]*p0mean[0] + R[1]*p0mean[1] + R[2]*p0mean[2];
t[1]=p1mean[1] - R[3]*p0mean[0] + R[4]*p0mean[1] + R[5]*p0mean[2];
t[2]=p1mean[2] - R[6]*p0mean[0] + R[7]*p0mean[1] + R[8]*p0mean[2];
#if 0
printf("t\n");
printf("%g %g %g\n", t[0], t[1], t[2]);
#endif
free(u1);
free(u2);
return 0;
}
/* estimate "point" pose P=K[R t] and an unknown focal length s.t. m=P*M, with m, M specified by ptsidx */
int p4pf_solve(double m[4][2], double M[4][3],
double R[MAX_NUM_P4PF_SOL][3][3], double t[MAX_NUM_P4PF_SOL][3], double foc[MAX_NUM_P4PF_SOL])
{
register int i, j;
int n;
const double tol=2.2204e-10;
register double tmp;
double mean3d[3], var, var2d;
double gl[6], glab, glac, glad, glbc, glbd, glcd, A[10*10];
#define __WORKSZ__ 400 // dgeev() queried for the 10x10 workspace size returned 340, add a little more...
const int ten=10, lwork=__WORKSZ__;
double wr[10], wi[10], vr[10*10], work[__WORKSZ__];
#undef __WORKSZ__
int info;
double sol[4*10], zb, zc, zd;
double m0[2], M0[3], m1[2], M1[3], m2[2], M2[3], m3[2], M3[3];
double xM[4][3], *xM0=xM[0], *xM1=xM[1], *xM2=xM[2], *xM3=xM[3], scM[4][3];
/* save input points so that they are not modified */
for(i=0; i<2; ++i){
m0[i]=m[0][i]; m1[i]=m[1][i]; m2[i]=m[2][i]; m3[i]=m[3][i];
M0[i]=M[0][i]; M1[i]=M[1][i]; M2[i]=M[2][i]; M3[i]=M[3][i];
}
M0[2]=M[0][2]; M1[2]=M[1][2]; M2[2]=M[2][2]; M3[2]=M[3][2];
/* normalize 2D, 3D */
/* shift 3D data so that variance = sqrt(2), mean = 0 */
mean3d[0]=0.25*(M0[0]+M1[0]+M2[0]+M3[0]);
mean3d[1]=0.25*(M0[1]+M1[1]+M2[1]+M3[1]);
mean3d[2]=0.25*(M0[2]+M1[2]+M2[2]+M3[2]);
M0[0]-=mean3d[0]; M0[1]-=mean3d[1]; M0[2]-=mean3d[2];
M1[0]-=mean3d[0]; M1[1]-=mean3d[1]; M1[2]-=mean3d[2];
M2[0]-=mean3d[0]; M2[1]-=mean3d[1]; M2[2]-=mean3d[2];
M3[0]-=mean3d[0]; M3[1]-=mean3d[1]; M3[2]-=mean3d[2];
/* variance (isotropic) */
var=0.25*((sqrt(SQR(M0[0]) + SQR(M0[1]) + SQR(M0[2])) +
sqrt(SQR(M1[0]) + SQR(M1[1]) + SQR(M1[2]))) +
(sqrt(SQR(M2[0]) + SQR(M2[1]) + SQR(M2[2])) +
sqrt(SQR(M3[0]) + SQR(M3[1]) + SQR(M3[2]))));
tmp=1./var;
M0[0]*=tmp; M0[1]*=tmp; M0[2]*=tmp;
M1[0]*=tmp; M1[1]*=tmp; M1[2]*=tmp;
M2[0]*=tmp; M2[1]*=tmp; M2[2]*=tmp;
M3[0]*=tmp; M3[1]*=tmp; M3[2]*=tmp;
/* save normalized 3D points in scM */
for(i=0; i<3; ++i){
scM[0][i]=M0[i]; scM[1][i]=M1[i]; scM[2][i]=M2[i]; scM[3][i]=M3[i];
}
#if 0
for(i=0; i<3; ++i)
printf("%d: %g %g %g %g\n", i, M0[i], M1[i], M2[i], M3[i]);
printf("==================================\n");
#endif
/* scale 2D data */
var2d=0.25*((sqrt(SQR(m0[0]) + SQR(m0[1])) +
sqrt(SQR(m1[0]) + SQR(m1[1]))) +
(sqrt(SQR(m2[0]) + SQR(m2[1])) +
sqrt(SQR(m3[0]) + SQR(m3[1]))));
tmp=1./var2d;
m0[0]*=tmp; m0[1]*=tmp;
m1[0]*=tmp; m1[1]*=tmp;
m2[0]*=tmp; m2[1]*=tmp;
m3[0]*=tmp; m3[1]*=tmp;
#if 0
for(i=0; i<2; ++i)
printf("%d: %g %g %g %g\n", i, m0[i], m1[i], m2[i], m3[i]);
#endif
/* calculate quadratic distances between 3D points */
gl[0]=glab=SQR(M0[0]-M1[0]) + SQR(M0[1]-M1[1]) + SQR(M0[2]-M1[2]); /* 0-1 */
gl[1]=glac=SQR(M0[0]-M2[0]) + SQR(M0[1]-M2[1]) + SQR(M0[2]-M2[2]); /* 0-2 */
gl[2]=glad=SQR(M0[0]-M3[0]) + SQR(M0[1]-M3[1]) + SQR(M0[2]-M3[2]); /* 0-3 */
gl[3]=glbc=SQR(M1[0]-M2[0]) + SQR(M1[1]-M2[1]) + SQR(M1[2]-M2[2]); /* 1-2 */
gl[4]=glbd=SQR(M1[0]-M3[0]) + SQR(M1[1]-M3[1]) + SQR(M1[2]-M3[2]); /* 1-3 */
gl[5]=glcd=SQR(M2[0]-M3[0]) + SQR(M2[1]-M3[1]) + SQR(M2[2]-M3[2]); /* 2-3 */
#if 0
printf("GL: %g %g %g %g %g %g\n", glab, glac, glad, glbc, glbd, glcd);
#endif
if(glab*glac*glad*glbc*glbd*glcd<tol) /* initial solution degeneracy - invalid input */
return 0;
p4pfmex(gl, m0, m1, m2, m3, A);
#if 0
for(i=0; i<10; ++i){
for(j=0; j<10; ++j)
printf("%g ", A[i*10+j]);
printf("\n");
}
#endif
/* transpose A in place preparing for LAPACK */
for(i=0; i<ten; ++i)
for(j=i+1; j<ten; ++j){
tmp=A[i+j*ten];
A[i+j*ten]=A[j+i*ten];
A[j+i*ten]=tmp;
}
/* compute right eigenvectors & eigenvalues */
F77_FUNC(dgeev)("N", "V", (int*)&ten, A, (int*)&ten, wr, wi, NULL, (int*)&ten, vr, (int*)&ten, work, (int*)&lwork, &info);
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dgeev in p4pf_solve()\n", -info);
exit(1);
}
else if(info>0){
fprintf(stderr, "LAPACK error: dgeev failed to converge in p4pf_solve();\n"
"and no eigenvectors have been computed; elements %d:N-1 of WR and WI"
"contain eigenvalues which have converged\n", info);
return 0;
}
#if 0
printf("\neigvals\n");
for(i=0; i<10; ++i)
printf("%d: %g+ %g * i\n", i, wr[i], wi[i]);
printf("\neigvecs\n");
for(i=0; i<10; ++i){
for(j=0; j<10; ++j)
printf("%g ", vr[i+j*10]);
printf("\n");
}
#endif
/* select rows 1, 2, 3, 4 of vr divided by row 0 */
/* Note that we don't bother to do a complex division as
* any imaginary solutions will be eliminated below
*/
for(j=0; j<10; ++j){
double d;
d=1./vr[0+j*10];
sol[j ]=vr[1+j*10]*d;
sol[10+j]=vr[2+j*10]*d;
sol[20+j]=vr[3+j*10]*d;
sol[30+j]=vr[4+j*10]*d;
}
#if 0
printf("\nsol\n");
for(i=0; i<4; ++i){
for(j=0; j<10; ++j)
printf("%g ", sol[i*10+j]);
printf("\n");
}
#endif
for(i=0; i<4*10; ++i)
if(!POSEST_FINITE(sol[i])) return 0;
/* select real & positive elements on row 3 of sol */
/* Code below assumes that if element j on row 4 of vr
* is imaginary, so will be element j on row 3 of sol
*/
for(j=n=0; j<10; ++j){
double tt[3], f;
if(wi[j]==0.0 && (f=sol[30+j])>0.0){
f=foc[n]=sqrt(f);
#if 0
printf("**** %g\n", f);
#endif
zd=sol[j];
zc=sol[10+j];
zb=sol[20+j];
#if 0
printf("zb zc zd %g %g %g\n", zb, zc, zd);
#endif
/* create p3d points in a camera coordinate system (using depths) */
xM0[0]= m0[0]; xM0[1]= m0[1]; xM0[2]= f;
xM1[0]=zb*m1[0]; xM1[1]=zb*m1[1]; xM1[2]=zb*f;
xM2[0]=zc*m2[0]; xM2[1]=zc*m2[1]; xM2[2]=zc*f;
xM3[0]=zd*m3[0]; xM3[1]=zd*m3[1]; xM3[2]=zd*f;
/* fix scale (recover 'za' in tmp) */
tmp =sqrt(glab / (SQR(xM0[0]-xM1[0]) + SQR(xM0[1]-xM1[1]) + SQR(xM0[2]-xM1[2]))); /* 0-1 */
tmp+=sqrt(glac / (SQR(xM0[0]-xM2[0]) + SQR(xM0[1]-xM2[1]) + SQR(xM0[2]-xM2[2]))); /* 0-2 */
tmp+=sqrt(glad / (SQR(xM0[0]-xM3[0]) + SQR(xM0[1]-xM3[1]) + SQR(xM0[2]-xM3[2]))); /* 0-3 */
tmp+=sqrt(glbc / (SQR(xM1[0]-xM2[0]) + SQR(xM1[1]-xM2[1]) + SQR(xM1[2]-xM2[2]))); /* 1-2 */
tmp+=sqrt(glbd / (SQR(xM1[0]-xM3[0]) + SQR(xM1[1]-xM3[1]) + SQR(xM1[2]-xM3[2]))); /* 1-3 */
tmp+=sqrt(glcd / (SQR(xM2[0]-xM3[0]) + SQR(xM2[1]-xM3[1]) + SQR(xM2[2]-xM3[2]))); /* 2-3 */
tmp*=0.1667; // average
#if 0
printf("#### %g\n", tmp);
#endif
xM0[0]*=tmp; xM0[1]*=tmp; xM0[2]*=tmp;
xM1[0]*=tmp; xM1[1]*=tmp; xM1[2]*=tmp;
xM2[0]*=tmp; xM2[1]*=tmp; xM2[2]*=tmp;
xM3[0]*=tmp; xM3[1]*=tmp; xM3[2]*=tmp;
#if 0
for(i=0; i<3; ++i)
printf("%d: %g %g %g %g\n", i, xM0[i], xM1[i], xM2[i], xM3[i]);
printf("##################################################\n");
#endif
if(getXform(scM, xM, 4, 0, (double *)R[n], tt)) continue;
/* t = var*tt - R*mean3d' */
t[n][0]=var*tt[0] - (R[n][0][0]*mean3d[0] + R[n][0][1]*mean3d[1] + R[n][0][2]*mean3d[2]);
t[n][1]=var*tt[1] - (R[n][1][0]*mean3d[0] + R[n][1][1]*mean3d[1] + R[n][1][2]*mean3d[2]);
t[n][2]=var*tt[2] - (R[n][2][0]*mean3d[0] + R[n][2][1]*mean3d[1] + R[n][2][2]*mean3d[2]);
foc[n]*=var2d;
#if 0
printf("RR\n");
printf("%g %g %g\n", R[n][0][0], R[n][0][1], R[n][0][2]);
printf("%g %g %g\n", R[n][1][0], R[n][1][1], R[n][1][2]);
printf("%g %g %g\n", R[n][2][0], R[n][2][1], R[n][2][2]);
printf("TT %g %g %g\n", t[n][0], t[n][1], t[n][2]);
printf("FOC %g\n", foc[n]);
#endif
++n;
if(n==MAX_NUM_P4PF_SOL){
fprintf(stderr, "maximum number of solutions exceeded in p4pf_solve(), "
"increase 'MAX_NUM_P4PF_SOL' and recompile [currently %d]\n", MAX_NUM_P4PF_SOL);
break;
}
}
}
return n;
}