-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathautocalibration.cpp
More file actions
4463 lines (3702 loc) · 113 KB
/
autocalibration.cpp
File metadata and controls
4463 lines (3702 loc) · 113 KB
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, Matthew Toews
All rights reserved.
By downloading, copying, installing or using the software you agree to this license.
If you do not agree to this license, do not download, install, copy or use the software.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
#include "neldermead.h"
//#include "nlopt.h"
#define PARAMETER_DIM_ROT 3
#define PARAMETER_DIM_RIG 7
void
vec3D_cross_3d(
float *pv1,
float *pv2,
float *pvCross
)
{
pvCross[0] = pv1[1]*pv2[2] - pv1[2]*pv2[1];
pvCross[1] = -pv1[0]*pv2[2] + pv1[2]*pv2[0];
pvCross[2] = pv1[0]*pv2[1] - pv1[1]*pv2[0];
}
void
vec3D_diff_3d(
float *pv1,
float *pv2,
float *pv12
)
{
pv12[0] = pv2[0]-pv1[0];
pv12[1] = pv2[1]-pv1[1];
pv12[2] = pv2[2]-pv1[2];
}
float
vec3D_dot_3d(
float *pv1,
float *pv2
)
{
return pv2[0]*pv1[0] + pv2[1]*pv1[1] + pv2[2]*pv1[2];
}
float
vec3D_dist_3d(
float *pv1,
float *pv2
)
{
float dx = pv2[0]-pv1[0];
float dy = pv2[1]-pv1[1];
float dz = pv2[2]-pv1[2];
return sqrt( dx*dx+dy*dy+dz*dz );
}
float
vec3D_distsqr_3d(
float *pv1,
float *pv2
)
{
float dx = pv2[0]-pv1[0];
float dy = pv2[1]-pv1[1];
float dz = pv2[2]-pv1[2];
return dx*dx+dy*dy+dz*dz;
}
void
vec3D_mult_scalar(
float *pf1,
float mult
)
{
pf1[0] *= mult;
pf1[1] *= mult;
pf1[2] *= mult;
}
void
vec3D_norm_3d(
float *pf1
)
{
float fSumSqr = pf1[0]*pf1[0] + pf1[1]*pf1[1] + pf1[2]*pf1[2];
if( fSumSqr > 0 )
{
float fDiv = 1.0/sqrt( fSumSqr );
pf1[0] *= fDiv;
pf1[1] *= fDiv;
pf1[2] *= fDiv;
}
else
{
pf1[0] = 1;
pf1[1] = 0;
pf1[2] = 0;
}
}
float
vec3D_mag(
float *pf1
)
{
float fSumSqr = pf1[0]*pf1[0] + pf1[1]*pf1[1] + pf1[2]*pf1[2];
if( fSumSqr > 0 )
{
return sqrt( fSumSqr );
}
else
{
return 0;
}
}
void
mult_3x3_matrix(
float *mat1,
float *mat2,
float *mat_out
)
{
for( int i = 0; i < 3; i++ )
{
for( int j = 0; j < 3; j++ )
{
mat_out[i*3+j]=0;
for( int ii = 0; ii < 3; ii++ )
{
mat_out[i*3+j] += mat1[i*3+ii]*mat2[ii*3+j];
}
}
}
}
void
mult_3x3_scalar(
float *mat1,
float multiple,
float *mat_out
)
{
for( int i = 0; i < 3; i++ )
{
for( int j = 0; j < 3; j++ )
{
mat_out[i*3+j] = mat1[i*3+j]*multiple;
}
}
}
void
mult_1x3_scalar(
float *vec,
float multiple,
float *vec_out
)
{
for( int i = 0; i < 3; i++ )
{
vec_out[i] = vec[i]*multiple;
}
}
void
sum_3x3_matrix(
float *mat1,
float *mat2,
float *mat_out
)
{
for( int i = 0; i < 3; i++ )
{
for( int j = 0; j < 3; j++ )
{
mat_out[i*3+j] = mat1[i*3+j] + mat2[i*3+j];
}
}
}
void
mult_3x3_vector(
float *mat,
float *vec_in,
float *vec_out
)
{
for( int i = 0; i < 3; i++ )
{
vec_out[i] = 0;
for( int j = 0; j < 3; j++ )
{
vec_out[i] += mat[i*3+j]*vec_in[j];
}
}
}
void
mult_4x4_matrix(
float *mat1,
float *mat2,
float *mat_out
)
{
for( int i = 0; i < 4; i++ )
{
for( int j = 0; j < 4; j++ )
{
mat_out[i*4+j]=0;
for( int ii = 0; ii < 4; ii++ )
{
mat_out[i*4+j] += mat1[i*4+ii]*mat2[ii*4+j];
}
}
}
}
void
mult_4x4_vector(
float *mat,
float *vec_in,
float *vec_out
)
{
for( int i = 0; i < 4; i++ )
{
vec_out[i] = 0;
for( int j = 0; j < 4; j++ )
{
vec_out[i] += mat[i*4+j]*vec_in[j];
}
}
}
//
// Assume last row of mat is [0,0,0,1] and that
// vec_in[3] = 1
// vec_out[3] = 1
//
// Here, mat is a 4x4 matrix, vec_in is 1x3, vec_out is 1x3
//
void
mult_4x4_vector_homogenous(
float *mat,
float *vec_in,
float *vec_out
)
{
for( int i = 0; i < 3; i++ )
{
vec_out[i] = 0;
for( int j = 0; j < 3; j++ )
{
vec_out[i] += mat[i*4+j]*vec_in[j];
}
vec_out[i] += mat[i*4+3];
}
//vec_out[3] = 1;
}
void
mult_4x4_scalar(
float *mat1,
float multiple,
float *mat_out
)
{
for( int i = 0; i < 4; i++ )
{
for( int j = 0; j < 4; j++ )
{
mat_out[i*4+j] = mat1[i*4+j]*multiple;
}
}
}
int
invert_4x4(
float *m,
float *invOut
)
{
float inv[16], det;
int i;
inv[0] = m[5] * m[10] * m[15] -
m[5] * m[11] * m[14] -
m[9] * m[6] * m[15] +
m[9] * m[7] * m[14] +
m[13] * m[6] * m[11] -
m[13] * m[7] * m[10];
inv[4] = -m[4] * m[10] * m[15] +
m[4] * m[11] * m[14] +
m[8] * m[6] * m[15] -
m[8] * m[7] * m[14] -
m[12] * m[6] * m[11] +
m[12] * m[7] * m[10];
inv[8] = m[4] * m[9] * m[15] -
m[4] * m[11] * m[13] -
m[8] * m[5] * m[15] +
m[8] * m[7] * m[13] +
m[12] * m[5] * m[11] -
m[12] * m[7] * m[9];
inv[12] = -m[4] * m[9] * m[14] +
m[4] * m[10] * m[13] +
m[8] * m[5] * m[14] -
m[8] * m[6] * m[13] -
m[12] * m[5] * m[10] +
m[12] * m[6] * m[9];
inv[1] = -m[1] * m[10] * m[15] +
m[1] * m[11] * m[14] +
m[9] * m[2] * m[15] -
m[9] * m[3] * m[14] -
m[13] * m[2] * m[11] +
m[13] * m[3] * m[10];
inv[5] = m[0] * m[10] * m[15] -
m[0] * m[11] * m[14] -
m[8] * m[2] * m[15] +
m[8] * m[3] * m[14] +
m[12] * m[2] * m[11] -
m[12] * m[3] * m[10];
inv[9] = -m[0] * m[9] * m[15] +
m[0] * m[11] * m[13] +
m[8] * m[1] * m[15] -
m[8] * m[3] * m[13] -
m[12] * m[1] * m[11] +
m[12] * m[3] * m[9];
inv[13] = m[0] * m[9] * m[14] -
m[0] * m[10] * m[13] -
m[8] * m[1] * m[14] +
m[8] * m[2] * m[13] +
m[12] * m[1] * m[10] -
m[12] * m[2] * m[9];
inv[2] = m[1] * m[6] * m[15] -
m[1] * m[7] * m[14] -
m[5] * m[2] * m[15] +
m[5] * m[3] * m[14] +
m[13] * m[2] * m[7] -
m[13] * m[3] * m[6];
inv[6] = -m[0] * m[6] * m[15] +
m[0] * m[7] * m[14] +
m[4] * m[2] * m[15] -
m[4] * m[3] * m[14] -
m[12] * m[2] * m[7] +
m[12] * m[3] * m[6];
inv[10] = m[0] * m[5] * m[15] -
m[0] * m[7] * m[13] -
m[4] * m[1] * m[15] +
m[4] * m[3] * m[13] +
m[12] * m[1] * m[7] -
m[12] * m[3] * m[5];
inv[14] = -m[0] * m[5] * m[14] +
m[0] * m[6] * m[13] +
m[4] * m[1] * m[14] -
m[4] * m[2] * m[13] -
m[12] * m[1] * m[6] +
m[12] * m[2] * m[5];
inv[3] = -m[1] * m[6] * m[11] +
m[1] * m[7] * m[10] +
m[5] * m[2] * m[11] -
m[5] * m[3] * m[10] -
m[9] * m[2] * m[7] +
m[9] * m[3] * m[6];
inv[7] = m[0] * m[6] * m[11] -
m[0] * m[7] * m[10] -
m[4] * m[2] * m[11] +
m[4] * m[3] * m[10] +
m[8] * m[2] * m[7] -
m[8] * m[3] * m[6];
inv[11] = -m[0] * m[5] * m[11] +
m[0] * m[7] * m[9] +
m[4] * m[1] * m[11] -
m[4] * m[3] * m[9] -
m[8] * m[1] * m[7] +
m[8] * m[3] * m[5];
inv[15] = m[0] * m[5] * m[10] -
m[0] * m[6] * m[9] -
m[4] * m[1] * m[10] +
m[4] * m[2] * m[9] +
m[8] * m[1] * m[6] -
m[8] * m[2] * m[5];
det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
if (det == 0)
return false;
det = 1.0 / det;
for (i = 0; i < 16; i++)
invOut[i] = inv[i] * det;
return true;
}
nlopt_result
nldrmd_minimize(
int n,
nlopt_func f,
void *f_data,
const double *lb,
const double *ub, /* bounds */
double *x, /* in: initial guess, out: minimizer */
double *minf,
const double *xstep, /* initial step sizes */
nlopt_stopping *stop
);
//
// Declare constraint data
//
typedef struct _my_constraint_data
{
int iPoints;
float *pfPoints1;
float *pfPoints2;
float fPercentToKeep; // usuall 0.75 of lowest error points, for robustness
int iErrorCalib; // Boolean, if true, algorithm evaluates error of pfPoints1/2 using ground truth calib
float pfCalib[PARAMETER_DIM_RIG]; // Ground truth calibration matrix: 16 floats
int bPerformAlign; // If true, generate and apply the alignment matrix
float pfAlign[PARAMETER_DIM_RIG]; // Alignment matrix: 16 floats
// *** 2014 3D reconstruction
// Keep track of frame indices
int *piFrames1;
int *piFrames2;
} my_constraint_data;
int
convert_vector_to_rotation_matrix(
float *vec,
float *mat
)
{
float pfOmega[3];
memcpy( pfOmega, vec, sizeof(pfOmega) );
float fTheta = vec3D_mag( pfOmega );
vec3D_norm_3d( pfOmega );
float fThetaSin = sin( fTheta );
float fThetaCos = cos( fTheta );
float fThetaCosInv = 1.0f-fThetaCos;
float pfOmegaHat[9];
memset( pfOmegaHat, 0, sizeof(pfOmegaHat) );
pfOmegaHat[1] = -pfOmega[2];
pfOmegaHat[2] = pfOmega[1];
pfOmegaHat[3] = pfOmega[2];
pfOmegaHat[5] = -pfOmega[0];
pfOmegaHat[6] = -pfOmega[1];
pfOmegaHat[7] = pfOmega[0];
float pfOmegaHatSqr[9];
mult_3x3_matrix( pfOmegaHat, pfOmegaHat, pfOmegaHatSqr );
memset( mat, 0, sizeof(float)*9 );
mat[0]=1;
mat[4]=1;
mat[8]=1;
mult_3x3_scalar( pfOmegaHat, fThetaSin, pfOmegaHat );
mult_3x3_scalar( pfOmegaHatSqr, fThetaCosInv, pfOmegaHatSqr );
sum_3x3_matrix( mat, pfOmegaHat, mat );
sum_3x3_matrix( mat, pfOmegaHatSqr, mat );
return 1;
}
int
convert_rotation_matrix_to_vector(
float *vec,
float *mat
)
{
float fTrace = mat[0] + mat[4] + mat[8];
float fTheta = acos( (fTrace - 1.0) / 2.0 );
vec[0] = mat[7]-mat[5];
vec[1] = mat[2]-mat[6];
vec[2] = mat[3]-mat[1];
vec3D_norm_3d( vec );
vec[0] *= fTheta;
vec[1] *= fTheta;
vec[2] *= fTheta;
return 1;
}
int
set_vector_to_identity(
float *vec
)
{
for( int m = 0; m < 7; m++ )
{
// zero rotation,
vec[m] = 0;
}
return 1;
}
int
set_vector_to_identity(
double *vec
)
{
for( int m = 0; m < 7; m++ )
{
// zero rotation,
vec[m] = 0;
}
return 1;
}
int
convert_vector_to_matrix4x4(
float *vec,
float *mat44 // length 16 vector
)
{
convert_vector_to_rotation_matrix( vec, mat44 );
//mult_3x3_scalar( mat44, exp(vec[6]), mat44 );
mat44[10] = mat44[8];
mat44[9] = mat44[7];
mat44[8] = mat44[6];
mat44[6] = mat44[5];
mat44[5] = mat44[4];
mat44[4] = mat44[3];
mat44[3] = vec[3];
mat44[7] = vec[4];
mat44[11] = vec[5];
mat44[12] = 0;
mat44[13] = 0;
mat44[14] = 0;
mat44[15] = 1;
return 1;
}
//
// This code has been verified.
// The plane parameters (a,b,c) are normalized to a unit vector.
//
int
determine_plane_3point(
float *pfP01, float *pfP02, float *pfP03, // points in image 1 (3d)
float *pfPlane // ax + by + cz + d = 0;
)
{
float pfV0_12[3];
float pfV0_13[3];
float pfV0_nm[3];
// Subtract point 1 to convert to vectors
vec3D_diff_3d( pfP01, pfP02, pfV0_12 );
vec3D_diff_3d( pfP01, pfP03, pfV0_13 );
// Normalize vectors
//vec3D_norm_3d( pfV0_12 );
//vec3D_norm_3d( pfV0_13 );
// Cross product between 2 vectors to get normal
vec3D_cross_3d( pfV0_12, pfV0_13, pfV0_nm );
vec3D_norm_3d( pfV0_nm );
pfPlane[0] = pfV0_nm[0]; // a
pfPlane[1] = pfV0_nm[1]; // b
pfPlane[2] = pfV0_nm[2]; // c
pfPlane[3] = -(pfV0_nm[0]*pfP01[0] + pfV0_nm[1]*pfP01[1] + pfV0_nm[2]*pfP01[2]); // d
pfPlane[3] = -(pfV0_nm[0]*pfP02[0] + pfV0_nm[1]*pfP02[1] + pfV0_nm[2]*pfP02[2]); // d
pfPlane[3] = -(pfV0_nm[0]*pfP03[0] + pfV0_nm[1]*pfP03[1] + pfV0_nm[2]*pfP03[2]); // d
return 1;
}
//
// ransac_plane_estimation()
//
// Determine robust plane estimate from 3 or more points.
//
int
ransac_plane_estimation(
int iPoints,
float *pts,
int iIterations,
float fDistThreshold,
float *pfPlane // 4 parameters: a b c d, abc is normalized
)
{
if( iPoints < 3 )
{
// 3 unique points are required
return -1;
}
if( iPoints == 3 )
{
// Determine exact plane
determine_plane_3point( pts + 3*0, pts + 3*1, pts + 3*2, pfPlane );
return 3;
}
int iMaxInliers = 0;
for( int i = 0; i < iIterations; i++ )
{
// Find a set of unique points
int i1 = (rand()*iPoints)/(RAND_MAX+1.0f);
int i2 = (rand()*iPoints)/(RAND_MAX+1.0f);
int i3 = (rand()*iPoints)/(RAND_MAX+1.0f);
while( i1 == i2 || i1 == i3 || i2 == i3 )
{
i1 = (rand()*iPoints)/(RAND_MAX+1.0f);
i2 = (rand()*iPoints)/(RAND_MAX+1.0f);
i3 = (rand()*iPoints)/(RAND_MAX+1.0f);
}
float *pfP01 = pts + 3*i1;
float *pfP02 = pts + 3*i2;
float *pfP03 = pts + 3*i3;
float pfPTest[4];
determine_plane_3point( pfP01, pfP02, pfP03, pfPTest );
if( pfPTest[0]*pfPTest[1]*pfPTest[2]*pfPTest[3] == 0 )
{
// Error - probably duplicate points
continue;
}
int iInliers = 0;
for( int j = 0; j < iPoints; j++ )
{
// Compute distance from each point to plane
float *pfPt = pts + 3*j;
float fDist = pfPTest[0]*pfPt[0] + pfPTest[1]*pfPt[1] + pfPTest[2]*pfPt[2] + pfPTest[3];
if( fDist < 0 )
{
fDist = -fDist;
}
if( fDist < fDistThreshold )
{
iInliers++;
}
}
if( iInliers > iMaxInliers )
{
iMaxInliers = iInliers;
for( int j = 0; j < 4; j++ )
{
pfPlane[j] = pfPTest[j];
}
// Go through and compute error again
for( int j = 0; j < iPoints; j++ )
{
// Compute distance from each point to plane
float *pfPt = pts + 3*j;
float fDist = pfPTest[0]*pfPt[0] + pfPTest[1]*pfPt[1] + pfPTest[2]*pfPt[2] + pfPTest[3];
if( fDist < 0 )
{
fDist = -fDist;
}
if( fDist < fDistThreshold )
{
iInliers++;
}
}
}
}
return iMaxInliers;
}
int
convert_vector_to_rotation_matrix_4X4(
float *vec,
float *mat
)
{
convert_vector_to_rotation_matrix( vec, mat );
mat[10] = mat[8];
mat[9] = mat[7];
mat[8] = mat[6];
mat[6] = mat[5];
mat[5] = mat[4];
mat[4] = mat[3];
mat[3] = 0;
mat[7] = 0;
mat[11] = 0;
mat[12] = mat[13] = mat[14] = 0;
mat[15] = 1;
return 1;
}
int
generate_random_rigid_transformation_matrix_4x4(
float *mat
)
{
float vec[3];
vec[0] = rand()/(RAND_MAX+1.0f) - 0.5;
vec[1] = rand()/(RAND_MAX+1.0f) - 0.5;
vec[2] = rand()/(RAND_MAX+1.0f) - 0.5;
convert_vector_to_rotation_matrix_4X4( vec, mat );
// Put in translation
mat[3] = 100*(rand()/(RAND_MAX+1.0f) - 0.5);
mat[7] = 100*(rand()/(RAND_MAX+1.0f) - 0.5);
mat[11] = 100*(rand()/(RAND_MAX+1.0f) - 0.5);
return 1;
}
int
test_convert_vector_to_rotation_matrix(
)
{
//float vec[3] = {-0.406417548, -0.247176698, -0.334497981 };
//float vec[3] = {-2.19563893, -1.335352724, -1.80709911};
float vec[3] = {-6.586916789, -4.006058171, -5.421297331};
float vec2[3];
float mat[9];
convert_vector_to_rotation_matrix( vec, mat );
float vec_out[3];
mult_3x3_vector( mat, vec, vec_out );
mult_1x3_scalar( vec, 102, vec2 );
convert_vector_to_rotation_matrix( vec2, mat );
mult_3x3_vector( mat, vec, vec_out );
return 1;
}
//
// select_half_data()
//
// Select a random subset of half the data.
//
int
select_half_data(
my_constraint_data &dat,
int iSubset
)
{
int iNewPoints = dat.iPoints / 2;
if( iSubset > 0 )
{
dat.pfPoints1 += 15*iNewPoints;
dat.pfPoints2 += 15*iNewPoints;
}
dat.iPoints = iNewPoints;
return 1;
}
int
select_half_data_interleaved(
my_constraint_data &dat,
int iSubset
)
{
int iBit = iSubset != 0 ? 1 : 0;
int iNewPoints = dat.iPoints / 2;
for( int i = 0; i < iNewPoints; i++ )
{
memcpy( dat.pfPoints1 + 15*i, dat.pfPoints1 + 15*(2*i+iBit), 15*sizeof(float) );
memcpy( dat.pfPoints2 + 15*i, dat.pfPoints2 + 15*(2*i+iBit), 15*sizeof(float) );
}
dat.iPoints = iNewPoints;
return 1;
}
int
generate_minimization_test_data(
my_constraint_data &dat,
int iPoints
)
{
dat.iPoints = iPoints;
dat.pfPoints1 = new float[3*iPoints];
dat.pfPoints2 = new float[3*iPoints];
float vec_original[3] = {-6.586916789, -4.006058171, -5.421297331};
for( int i = 0; i < iPoints; i++ )
{
// Create random vector & rotation matrix
float vec2[3];
memcpy( vec2, vec_original, sizeof(vec2) );
vec2[0] += rand()/(RAND_MAX+1.0) - 0.5f;
vec2[1] += rand()/(RAND_MAX+1.0) - 0.5f;
vec2[2] += rand()/(RAND_MAX+1.0) - 0.5f;
float mat[9];
convert_vector_to_rotation_matrix( vec2, mat );
float *point_in = dat.pfPoints1 + 3*i;
float *point_out = dat.pfPoints2 + 3*i;
// Create random input point (-10 to 10)
point_in[0] = (rand()/(RAND_MAX+1.0) - 0.5f)*10;
point_in[1] = (rand()/(RAND_MAX+1.0) - 0.5f)*10;
point_in[2] = (rand()/(RAND_MAX+1.0) - 0.5f)*10;
// Create random output point: multiply input point by random rotation matrix
mult_3x3_vector( mat, point_in, point_out );
}
return 1;
}
//
// Format: | rot-vec 3 | trs-vec 3 | log scale 1 |
//
int
generate_minimization_test_data_rigid(
my_constraint_data &dat,
int iPoints
)
{
dat.iPoints = iPoints;
dat.pfPoints1 = new float[3*iPoints];
dat.pfPoints2 = new float[3*iPoints];
//srand(50);
#define LN_2 0.69314718
float vec_original[PARAMETER_DIM_RIG] = {-6.586916789, -4.006058171, -5.421297331, 30, -25, -5, LN_2 };
for( int i = 0; i < iPoints; i++ )
{
// Create random vector
float vec2[PARAMETER_DIM_RIG];
memcpy( vec2, vec_original, sizeof(vec2) );
// Theta +- 0.5
vec2[0] += rand()/(RAND_MAX+1.0) - 0.5f;
vec2[1] += rand()/(RAND_MAX+1.0) - 0.5f;
vec2[2] += rand()/(RAND_MAX+1.0) - 0.5f;
// Trans +- 10
vec2[3] += (rand()/(RAND_MAX+1.0) - 0.5f)*10;
vec2[4] += (rand()/(RAND_MAX+1.0) - 0.5f)*10;
vec2[5] += (rand()/(RAND_MAX+1.0) - 0.5f)*10;
// Scale +- 0.25
vec2[6] += (rand()/(RAND_MAX+1.0) - 0.5f)*.5;
float mat[9];
// Create rotation matrix
convert_vector_to_rotation_matrix( vec2, mat );
// Multiply scale
mult_3x3_scalar( mat, exp(vec2[6]), mat );
float *point_in = dat.pfPoints1 + 3*i;
float *point_out = dat.pfPoints2 + 3*i;
// Create random input point (-50 to 50)
point_in[0] = (rand()/(RAND_MAX+1.0) - 0.5f)*100;
point_in[1] = (rand()/(RAND_MAX+1.0) - 0.5f)*100;
point_in[2] = (rand()/(RAND_MAX+1.0) - 0.5f)*100;
// Create random output point: multiply input point by random rotation matrix
mult_3x3_vector( mat, point_in, point_out );
// Add translation and we're done
point_out[0] += vec2[3];
point_out[1] += vec2[4];
point_out[2] += vec2[5];
}
return 1;
}
//
// Format: | rot-vec 3 | trs-vec 3 | log scale 1 |
//
int
generate_minimization_test_data_rigid_US(
my_constraint_data &dat,
int iPoints
)
{
dat.iPoints = iPoints;
dat.pfPoints1 = new float[15*iPoints];
dat.pfPoints2 = new float[15*iPoints];
#define LN_2 0.69314718
float vec_original[PARAMETER_DIM_RIG] = {0.0001, 0.0001, 0.0001, 30, -25, -5, LN_2 };