forked from cdcseacave/openMVS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.inl
1298 lines (1220 loc) · 51.2 KB
/
Util.inl
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
////////////////////////////////////////////////////////////////////
// Util.inl
//
// Copyright 2007 cDc@seacave
// Distributed under the Boost Software License, Version 1.0
// (See http://www.boost.org/LICENSE_1_0.txt)
// D E F I N E S ///////////////////////////////////////////////////
namespace SEACAVE {
// I N L I N E /////////////////////////////////////////////////////
// compute relative pose of the given two cameras
template<typename TYPE>
inline void ComputeRelativeRotation(const TMatrix<TYPE,3,3>& Ri, const TMatrix<TYPE,3,3>& Rj, TMatrix<TYPE,3,3>& Rij) {
Rij = Rj * Ri.t();
} // ComputeRelativeRotation
template<typename TYPE>
inline void ComputeRelativePose(const TMatrix<TYPE,3,3>& Ri, const TPoint3<TYPE>& Ci, const TMatrix<TYPE,3,3>& Rj, const TPoint3<TYPE>& Cj, TMatrix<TYPE,3,3>& Rij, TPoint3<TYPE>& Cij) {
Rij = Rj * Ri.t();
Cij = Ri * (Cj - Ci);
} // ComputeRelativePose
/*----------------------------------------------------------------*/
// compute the homography matrix transforming points from image A to image B,
// given the relative pose of B with respect to A, and the plane
// (see R. Hartley, "Multiple View Geometry," 2004, pp. 234)
template <typename TYPE>
TMatrix<TYPE,3,3> HomographyMatrixComposition(const TMatrix<TYPE,3,3>& R, const TPoint3<TYPE>& C, const TMatrix<TYPE,3,1>& n, const TYPE& d) {
const TMatrix<TYPE,3,1> t(R*(-C));
return TMatrix<TYPE,3,3>(R - t*(n.t()*INVERT(d)));
}
template <typename TYPE>
TMatrix<TYPE,3,3> HomographyMatrixComposition(const TMatrix<TYPE,3,3>& R, const TPoint3<TYPE>& C, const TMatrix<TYPE,3,1>& n, const TMatrix<TYPE,3,1>& X) {
return HomographyMatrixComposition(R, C, n, -n.dot(X));
}
template <typename TYPE>
TMatrix<TYPE,3,3> HomographyMatrixComposition(const TMatrix<TYPE,3,3>& Ri, const TPoint3<TYPE>& Ci, const TMatrix<TYPE,3,3>& Rj, const TPoint3<TYPE>& Cj, const TMatrix<TYPE,3,1>& n, const TYPE& d) {
#if 0
Matrix3x3 R; Point3 C;
ComputeRelativePose(Ri, Ci, Rj, Cj, R, C);
return HomographyMatrixComposition(R, C, n, d);
#else
const TMatrix<TYPE,3,1> t((Ci - Cj)*INVERT(d));
return TMatrix<TYPE,3,3>(Rj * (Ri.t() - t*n.t()));
#endif
}
template <typename TYPE>
TMatrix<TYPE,3,3> HomographyMatrixComposition(const TMatrix<TYPE,3,3>& Ri, const TPoint3<TYPE>& Ci, const TMatrix<TYPE,3,3>& Rj, const TPoint3<TYPE>& Cj, const TMatrix<TYPE,3,1>& n, const TMatrix<TYPE,3,1>& X) {
#if 0
Matrix3x3 R; Point3 C;
ComputeRelativePose(Ri, Ci, Rj, Cj, R, C);
return HomographyMatrixComposition(R, C, n, X);
#else
const TMatrix<TYPE,3,1> t((Ci - Cj)*INVERT(n.dot(X)));
return TMatrix<TYPE,3,3>(Rj * (Ri.t() + t*n.t()));
#endif
}
/*----------------------------------------------------------------*/
// transform essential matrix to fundamental matrix
template<typename TYPE>
inline TMatrix<TYPE,3,3> TransformE2F(const TMatrix<TYPE,3,3>& E, const TMatrix<TYPE,3,3>& K1, const TMatrix<TYPE,3,3>& K2) {
return K2.inv().t() * E * K1.inv();
} // TransformE2F
// transform fundamental matrix to essential matrix
template<typename TYPE>
inline TMatrix<TYPE,3,3> TransformF2E(const TMatrix<TYPE,3,3>& F, const TMatrix<TYPE,3,3>& K1, const TMatrix<TYPE,3,3>& K2) {
return K2.t() * F * K1;
} // TransformF2E
/*----------------------------------------------------------------*/
// Creates a skew symmetric cross product matrix from the provided tuple.
// compose the cross-product matrix from the given vector: axb=[a]xb
template<typename TYPE>
inline TMatrix<TYPE,3,3> CreateCrossProductMatrix3(const TMatrix<TYPE,3,1>& x) {
TMatrix<TYPE,3,3> X;
X(0, 0) = TYPE(0); X(1, 0) = -x(2); X(2, 0) = x(1);
X(0, 1) = x(2); X(1, 1) = TYPE(0); X(2, 1) = -x(0);
X(0, 2) = -x(1); X(1, 2) = x(0); X(2, 2) = TYPE(0);
return X;
} // CreateCrossProductMatrix3
// same as above, but transposed: axb=[b]xTa
template<typename TYPE>
inline TMatrix<TYPE,3,3> CreateCrossProductMatrix3T(const TMatrix<TYPE,3,1>& x) {
TMatrix<TYPE,3,3> X;
X(0, 0) = TYPE(0); X(1, 0) = x(2); X(2, 0) = -x(1);
X(0, 1) = -x(2); X(1, 1) = TYPE(0); X(2, 1) = x(0);
X(0, 2) = x(1); X(1, 2) = -x(0); X(2, 2) = TYPE(0);
return X;
} // CreateCrossProductMatrix3T
/*----------------------------------------------------------------*/
// compose essential matrix from the given rotation and direction
template<typename TYPE>
inline TMatrix<TYPE,3,3> CreateE(const TMatrix<TYPE,3,3>& R, const TMatrix<TYPE,3,1>& C) {
const TMatrix<TYPE,3,1> t = -(R * C);
return CreateCrossProductMatrix3(t) * R;
} // CreateE
// compose fundamental matrix from the given rotation, direction and camera matrix
template<typename TYPE>
inline TMatrix<TYPE,3,3> CreateF(const TMatrix<TYPE,3,3>& R, const TMatrix<TYPE,3,1>& C, const TMatrix<TYPE,3,3>& K1, const TMatrix<TYPE,3,3>& K2) {
const TMatrix<TYPE,3,3> E = CreateE(R, C);
return TransformE2F(E, K1, K2);
} // CreateF
/*----------------------------------------------------------------*/
// if the matrix is a rotation, then the the transpose should be the inverse
template<typename TYPE>
inline bool IsRotationMatrix(const TMatrix<TYPE,3,3>& R) {
ASSERT(sizeof(TMatrix<TYPE,3,3>) == sizeof(TRMatrixBase<TYPE>));
return ((const TRMatrixBase<TYPE>&)R).IsValid();
} // IsRotationMatrix
template<typename TYPE, int O>
inline bool IsRotationMatrix(const Eigen::Matrix<TYPE,3,3,O>& R) {
// the trace should be three and the determinant should be one
return (ISEQUAL(R.determinant(), TYPE(1)) && ISEQUAL((R*R.transpose()).trace(), TYPE(3)));
} // IsRotationMatrix
/*----------------------------------------------------------------*/
// enforce matrix orthogonality
template<typename TYPE>
inline void EnsureRotationMatrix(TMatrix<TYPE,3,3>& R) {
ASSERT(sizeof(TMatrix<TYPE,3,3>) == sizeof(TRMatrixBase<TYPE>));
((TRMatrixBase<TYPE>&)R).EnforceOrthogonality();
} // EnsureRotationMatrix
/*----------------------------------------------------------------*/
// compute the angle between the two rotations given
// as in: "Disambiguating Visual Relations Using Loop Constraints", 2010
// returns cos(angle) (same as cos(ComputeAngleSO3(I))
template<typename TYPE>
inline TYPE ComputeAngle(const TMatrix<TYPE,3,3>& I) {
return CLAMP(((TYPE)cv::trace(I)-TYPE(1))*TYPE(0.5), TYPE(-1), TYPE(1));
} // ComputeAngle
template<typename TYPE>
FORCEINLINE TYPE ComputeAngle(const TMatrix<TYPE,3,3>& R1, const TMatrix<TYPE,3,3>& R2) {
return ComputeAngle(TMatrix<TYPE,3,3>(R1*R2.t()));
} // ComputeAngle
// compute the distance on SO(3) between the two given rotations
// using log(R) as in: "Efficient and Robust Large-Scale Rotation Averaging", 2013
// same result as above, but returns directly the angle
template<typename TYPE>
inline TYPE ComputeAngleSO3(const TMatrix<TYPE,3,3>& I) {
return TYPE(norm(((const TRMatrixBase<TYPE>&)I).GetRotationAxisAngle()));
} // ComputeAngleSO3
template<typename TYPE>
FORCEINLINE TYPE ComputeAngleSO3(const TMatrix<TYPE,3,3>& R1, const TMatrix<TYPE,3,3>& R2) {
return ComputeAngleSO3(TMatrix<TYPE,3,3>(R1*R2.t()));
} // ComputeAngleSO3
/*----------------------------------------------------------------*/
// compute the Frobenius (or Euclidean) norm of the distance between the two matrices
template<typename TYPE, int m, int n>
inline TYPE FrobeniusNorm(const TMatrix<TYPE,m,n>& M) {
return SQRT(((typename TMatrix<TYPE,m,n>::EMatMap)M).cwiseAbs2().sum());
} // FrobeniusNorm
template<typename TYPE, int m, int n>
FORCEINLINE TYPE FrobeniusNorm(const TMatrix<TYPE,m,n>& M1, const TMatrix<TYPE,m,n>& M2) {
return FrobeniusNorm(TMatrix<TYPE,m,n>(M1-M2));
} // FrobeniusNorm
/*----------------------------------------------------------------*/
// check if any three of the given 2D points are on the same line
template<typename TYPE>
bool CheckCollinearity(const TPoint2<TYPE>* ptr, int count, bool checkPartialSubsets=false) {
for (int i = (checkPartialSubsets?count-1:2); i < count; ++i) {
// check that the i-th selected point does not belong
// to a line connecting some previously selected points
for (int j = 1; j < i; ++j) {
const TPoint2<TYPE> d1 = ptr[j] - ptr[i];
for (int k = 0; k < j; ++k) {
const TPoint2<TYPE> d2 = ptr[k] - ptr[i];
if (ABS(d2.x*d1.y - d2.y*d1.x) <= FLT_EPSILON*(ABS(d1.x) + ABS(d1.y) + ABS(d2.x) + ABS(d2.y)))
return true;
}
}
}
return false;
}
// check if any three of the given 3D points are on the same line
template<typename TYPE>
bool CheckCollinearity(const TPoint3<TYPE>* ptr, int count, bool checkPartialSubsets=false) {
for (int i = (checkPartialSubsets?count-1:2); i < count; ++i) {
// check that the i-th selected point does not belong
// to a line connecting some previously selected points
for (int j = 1; j < i; ++j) {
const TPoint3<TYPE> d1 = ptr[j] - ptr[i];
for (int k = 0; k < j; ++k) {
const TPoint3<TYPE> d2 = ptr[k] - ptr[i];
if (normSq(d1.cross(d2)) < 1.e-16)
return true;
}
}
}
return false;
}
/*----------------------------------------------------------------*/
// compute the corresponding ray for a given projection matrix P[3,4] and image point pt[2,1]
// output ray[3,1]
template<typename TYPE1, typename TYPE2>
inline void RayPoint_3x4_2_3(const TYPE1* P, const TYPE2* pt, TYPE1* ray) {
Eigen::Map< const Eigen::Matrix<TYPE1,3,4> > mP(P);
const Eigen::Matrix<TYPE1,3,3> mM(mP.template topLeftCorner<3,3>());
TYPE1 M[9];
InvertMatrix3x3(mM.data(), M);
ray[0] = M[0*3+0]*pt[0] + M[0*3+1]*pt[1] + M[0*3+2];
ray[1] = M[1*3+0]*pt[0] + M[1*3+1]*pt[1] + M[1*3+2];
ray[2] = M[2*3+0]*pt[0] + M[2*3+1]*pt[1] + M[2*3+2];
} // RayPoint_3x4_2_3
/*----------------------------------------------------------------*/
// project column vertex - used only for visualization purposes
// (optimized ProjectVertex for P[3,4] and X[3,1], output pt[3,1])
template<typename TYPE1, typename TYPE2>
inline void ProjectVertex_3x4_3_3(const TYPE1* P, const TYPE1* X, TYPE2* pt) {
pt[0] = (TYPE2)(P[0*4+0]*X[0] + P[0*4+1]*X[1] + P[0*4+2]*X[2] + P[0*4+3]);
pt[1] = (TYPE2)(P[1*4+0]*X[0] + P[1*4+1]*X[1] + P[1*4+2]*X[2] + P[1*4+3]);
pt[2] = (TYPE2)(P[2*4+0]*X[0] + P[2*4+1]*X[1] + P[2*4+2]*X[2] + P[2*4+3]);
} // ProjectVertex_3x4_3_3
// (optimized ProjectVertex for P[3,4] and X[4,1], output pt[3,1])
template<typename TYPE1, typename TYPE2>
inline void ProjectVertex_3x4_4_3(const TYPE1* P, const TYPE1* X, TYPE2* pt) {
pt[0] = (TYPE2)(P[0*4+0]*X[0] + P[0*4+1]*X[1] + P[0*4+2]*X[2] + P[0*4+3]*X[3]);
pt[1] = (TYPE2)(P[1*4+0]*X[0] + P[1*4+1]*X[1] + P[1*4+2]*X[2] + P[1*4+3]*X[3]);
pt[2] = (TYPE2)(P[2*4+0]*X[0] + P[2*4+1]*X[1] + P[2*4+2]*X[2] + P[2*4+3]*X[3]);
} // ProjectVertex_3x4_4_3
// (optimized ProjectVertex for R[3,3], C[3,1] and X[3,1], output pt[2,1])
template<typename TYPE1, typename TYPE2>
inline void ProjectVertex_3x3_3_3_2(const TYPE1* R, const TYPE1* C, const TYPE1* X, TYPE2* pt) {
const TYPE1 T[3] = {X[0]-C[0], X[1]-C[1], X[2]-C[2]};
const TYPE1 invW(INVERT(R[2*3+0]*T[0] + R[2*3+1]*T[1] + R[2*3+2]*T[2]));
pt[0] = (TYPE2)((R[0*3+0]*T[0] + R[0*3+1]*T[1] + R[0*3+2]*T[2]) * invW);
pt[1] = (TYPE2)((R[1*3+0]*T[0] + R[1*3+1]*T[1] + R[1*3+2]*T[2]) * invW);
} // ProjectVertex_3x3_3_3_2
// (optimized ProjectVertex for R[3,3], C[3,1] and X[3,1], output pt[3,1])
template<typename TYPE1, typename TYPE2>
inline void ProjectVertex_3x3_3_3_3(const TYPE1* R, const TYPE1* C, const TYPE1* X, TYPE2* pt) {
const TYPE1 T[3] = {X[0]-C[0], X[1]-C[1], X[2]-C[2]};
pt[0] = (TYPE2)(R[0*3+0]*T[0] + R[0*3+1]*T[1] + R[0*3+2]*T[2]);
pt[1] = (TYPE2)(R[1*3+0]*T[0] + R[1*3+1]*T[1] + R[1*3+2]*T[2]);
pt[2] = (TYPE2)(R[2*3+0]*T[0] + R[2*3+1]*T[1] + R[2*3+2]*T[2]);
} // ProjectVertex_3x3_3_3_3
// (optimized ProjectVertex for H[3,3] and X[2,1], output pt[3,1])
template<typename TYPE1, typename TYPE2, typename TYPE3>
inline void ProjectVertex_3x3_2_3(const TYPE1* H, const TYPE2* X, TYPE3* pt) {
pt[0] = (TYPE3)(H[0*3+0]*X[0] + H[0*3+1]*X[1] + H[0*3+2]);
pt[1] = (TYPE3)(H[1*3+0]*X[0] + H[1*3+1]*X[1] + H[1*3+2]);
pt[2] = (TYPE3)(H[2*3+0]*X[0] + H[2*3+1]*X[1] + H[2*3+2]);
} // ProjectVertex_3x3_2_3
// (optimized ProjectVertex for H[3,3] and X[2,1], output pt[2,1])
template<typename TYPE1, typename TYPE2, typename TYPE3>
inline void ProjectVertex_3x3_2_2(const TYPE1* H, const TYPE2* X, TYPE3* pt) {
const TYPE1 invZ(INVERT(H[2*3+0]*X[0] + H[2*3+1]*X[1] + H[2*3+2]));
pt[0] = (TYPE3)((H[0*3+0]*X[0] + H[0*3+1]*X[1] + H[0*3+2])*invZ);
pt[1] = (TYPE3)((H[1*3+0]*X[0] + H[1*3+1]*X[1] + H[1*3+2])*invZ);
} // ProjectVertex_3x3_2_2
/*----------------------------------------------------------------*/
// project column vertex - used only for visualization purposes
// (optimized ProjectVertex for P[3,4] and X[3,1], output pt[2,1])
template<typename TYPE1, typename TYPE2>
inline void ProjectVertex_3x4_3_2(const TYPE1* P, const TYPE1* X, TYPE2* pt) {
const TYPE1 invW(INVERT(P[2*4+0]*X[0] + P[2*4+1]*X[1] + P[2*4+2]*X[2] + P[2*4+3]));
pt[0] = (TYPE2)((P[0*4+0]*X[0] + P[0*4+1]*X[1] + P[0*4+2]*X[2] + P[0*4+3]) * invW);
pt[1] = (TYPE2)((P[1*4+0]*X[0] + P[1*4+1]*X[1] + P[1*4+2]*X[2] + P[1*4+3]) * invW);
} // ProjectVertex_3x4_3_2
// (optimized ProjectVertex for P[3,4] and X[4,1], output pt[2,1])
template<typename TYPE1, typename TYPE2>
inline void ProjectVertex_3x4_4_2(const TYPE1* P, const TYPE1* X, TYPE2* pt) {
const TYPE1 invW(INVERT(P[2*4+0]*X[0] + P[2*4+1]*X[1] + P[2*4+2]*X[2] + P[2*4+3]*X[3]));
pt[0] = (TYPE2)((P[0*4+0]*X[0] + P[0*4+1]*X[1] + P[0*4+2]*X[2] + P[0*4+3]*X[3]) * invW);
pt[1] = (TYPE2)((P[1*4+0]*X[0] + P[1*4+1]*X[1] + P[1*4+2]*X[2] + P[1*4+3]*X[3]) * invW);
} // ProjectVertex_3x4_4_2
/*----------------------------------------------------------------*/
// project column vertex using the transpose of the given projective matrix
// (optimized ProjectVertex for P[3,4].t() and X[3,1], output pt[4,1])
template<typename TYPE1, typename TYPE2, typename TYPE3>
inline void ProjectVertex_3x4t_3_4(const TYPE1* P, const TYPE2* X, TYPE3* pt) {
pt[0] = (TYPE3)(P[0*4+0]*X[0] + P[1*4+0]*X[1] + P[2*4+0]*X[2]);
pt[1] = (TYPE3)(P[0*4+1]*X[0] + P[1*4+1]*X[1] + P[2*4+1]*X[2]);
pt[2] = (TYPE3)(P[0*4+2]*X[0] + P[1*4+2]*X[1] + P[2*4+2]*X[2]);
pt[3] = (TYPE3)(P[0*4+3]*X[0] + P[1*4+3]*X[1] + P[2*4+3]*X[2]);
} // ProjectVertex_3x4t_3_4
// (optimized ProjectVertex for P[3,4].t() and X[3,1], output pt[3,1] - the first 3 coordinates)
template<typename TYPE1, typename TYPE2, typename TYPE3>
inline void ProjectVertex_3x4t_3_3(const TYPE1* P, const TYPE2* X, TYPE3* pt) {
pt[0] = (TYPE3)(P[0*4+0]*X[0] + P[1*4+0]*X[1] + P[2*4+0]*X[2]);
pt[1] = (TYPE3)(P[0*4+1]*X[0] + P[1*4+1]*X[1] + P[2*4+1]*X[2]);
pt[2] = (TYPE3)(P[0*4+2]*X[0] + P[1*4+2]*X[1] + P[2*4+2]*X[2]);
} // ProjectVertex_3x4t_3_3
/*----------------------------------------------------------------*/
// given the camera matrix, compute the error between the given 2D point and the reprojected 3D point;
// note that here we're using "dirty" projection method which is suitable only for visualization; on the upside, it shouldn't matter because
// if a point is projected as infinite, there's something wrong with it anyway;
// (optimized ComputeReprojectionError for P[3,4] and X[3,1], output pt[2,1])
template<typename TYPE1, typename TYPE2>
inline TYPE2 ComputeReprojectionError_3x4_3_2(const TYPE1* P, const TYPE1* X, const TYPE2* pt) {
TYPE2 reprojection[2];
ProjectVertex_3x4_3_2(P, X, reprojection);
return SQUARE(reprojection[0]-pt[0])+SQUARE(reprojection[1]-pt[1]);
} // ComputeReprojectionError_3x4_3_2
// (optimized ComputeReprojectionError for P[3,4] and X[4,1], output pt[2,1])
template<typename TYPE1, typename TYPE2>
inline TYPE2 ComputeReprojectionError_3x4_4_2(const TYPE1* P, const TYPE1* X, const TYPE2* pt) {
TYPE2 reprojection[2];
ProjectVertex_3x4_4_2(P, X, reprojection);
return SQUARE(reprojection[0]-pt[0])+SQUARE(reprojection[1]-pt[1]);
} // ComputeReprojectionError_3x4_4_2
// (optimized ComputeReprojectionError for R[3,3], C[3,1] and X[3,1], output pt[2,1])
template<typename TYPE1, typename TYPE2>
inline TYPE2 ComputeReprojectionError_3x3_3_3_2(const TYPE1* R, const TYPE1* C, const TYPE1* X, const TYPE2* pt) {
TYPE2 reprojection[2];
ProjectVertex_3x3_3_3_2(R, C, X, reprojection);
return SQUARE(reprojection[0]-pt[0])+SQUARE(reprojection[1]-pt[1]);
} // ComputeReprojectionError_3x3_3_3_2
/*----------------------------------------------------------------*/
// computes the depth of the given point
// from the point of view of the given camera pose
template<typename TYPE>
inline TYPE PointDepth(const TYPE* R, const TYPE* C, const TYPE* X) {
return R[2*3+0]*X[0] + R[2*3+1]*X[1] + R[2*3+2]*X[2] + C[2];
} // PointDepth
template<typename TYPE>
inline TYPE PointDepth(const TYPE* P, const TYPE* X) {
return P[2*4+0]*X[0] + P[2*4+1]*X[1] + P[2*4+2]*X[2] + P[2*4+3];
} // PointDepth
/*----------------------------------------------------------------*/
// reproject the given 3D point with the given view;
// return squared reprojection error
// or FLT_MAX if the point is behind the camera
// reproj return the homogeneous reprojection
template<typename TYPE1, typename TYPE2>
inline TYPE2 ProjectPoint(const TYPE1* P, const TYPE1* X, const TYPE2* proj, TYPE2* reproj) {
// reproject the 3D point on this view
ProjectVertex_3x4_3_3(P, X, reproj);
// filter out points behind
if (reproj[2] <= TYPE2(0))
return FLT_MAX;
// return the reprojection error
return SQUARE(reproj[0]/reproj[2]-proj[0])+SQUARE(reproj[1]/reproj[2]-proj[1]);
} // ProjectPoint
template<typename TYPE1, typename TYPE2>
inline TYPE2 ProjectPoint(const TYPE1* R, const TYPE1* C, const TYPE1* X, const TYPE2* proj, TYPE2* reproj) {
// reproject the 3D point on this view
ProjectVertex_3x3_3_3_3(R, C, X, reproj);
// filter out points behind
if (reproj[2] <= TYPE2(0))
return FLT_MAX;
// return the reprojection error
return SQUARE(reproj[0]/reproj[2]-proj[0])+SQUARE(reproj[1]/reproj[2]-proj[1]);
} // ProjectPoint
/*----------------------------------------------------------------*/
// reproject the given 3D point with the given view;
// return true if the point is not behind the camera
// and the reprojection error is small enough;
// returns false otherwise
// reproj returns the homogeneous reprojection (first 3) and errSq (last 1)
template<typename TYPE1, typename TYPE2>
inline bool IsPointInlier(const TYPE1* P, const TYPE1* X, const TYPE2* proj, TYPE2* reproj, TYPE2 thresholdSq) {
// reproject the 3D point on this view
ProjectVertex_3x4_3_3(P, X, reproj);
// filter out points behind the camera or
// having the reprojection error bigger than the given threshold
return (reproj[2] > TYPE2(0) &&
(reproj[3]=(SQUARE(reproj[0]/reproj[2]-proj[0])+SQUARE(reproj[1]/reproj[2]-proj[1]))) <= thresholdSq);
} // IsPointInlier
template<typename TYPE1, typename TYPE2>
inline bool IsPointInlier(const TYPE1* R, const TYPE1* C, const TYPE1* X, const TYPE2* proj, TYPE2* reproj, TYPE2 thresholdSq) {
// reproject the 3D point on this view
ProjectVertex_3x3_3_3_3(R, C, X, reproj);
// filter out points behind the camera or
// having the reprojection error bigger than the given threshold
return (reproj[2] > TYPE2(0) &&
(reproj[3]=(SQUARE(reproj[0]/reproj[2]-proj[0])+SQUARE(reproj[1]/reproj[2]-proj[1]))) <= thresholdSq);
} // IsPointInlier
/*----------------------------------------------------------------*/
// given a rotation matrix, return the angle in radians corresponding to the axis/angle decomposition
template<typename TYPE>
inline TYPE AngleFromRotationMatrix(const TYPE* R) {
const TYPE a = (R[0*3+0]+R[1*3+1]+R[2*3+2]-TYPE(1))/TYPE(2);
if (a < TYPE(-1))
return acos(TYPE(-1));
if (a > TYPE(1))
return acos(TYPE(1));
return acos(a);
}
template<typename TYPE>
FORCEINLINE TYPE AngleFromRotationMatrix(const TMatrix<TYPE,3,3>& R) {
return AngleFromRotationMatrix(R.val);
}
/*----------------------------------------------------------------*/
// given two 3D vectors,
// compute the angle between them
// returns cos(angle)
template<typename TYPE1, typename TYPE2>
inline TYPE2 ComputeAngle(const TYPE1* V1, const TYPE1* V2) {
// compute the angle between the rays
return CLAMP(TYPE2((V1[0]*V2[0]+V1[1]*V2[1]+V1[2]*V2[2])/SQRT((V1[0]*V1[0]+V1[1]*V1[1]+V1[2]*V1[2])*(V2[0]*V2[0]+V2[1]*V2[1]+V2[2]*V2[2]))), TYPE2(-1), TYPE2(1));
} // ComputeAngle
// given three 3D points,
// compute the angle between the vectors formed by the first point with the other two
// returns cos(angle)
template<typename TYPE1, typename TYPE2>
inline TYPE2 ComputeAngle(const TYPE1* B, const TYPE1* X1, const TYPE1* X2) {
// create the two vectors
const TYPE1 V1[] = {X1[0]-B[0], X1[1]-B[1], X1[2]-B[2]};
const TYPE1 V2[] = {X2[0]-B[0], X2[1]-B[1], X2[2]-B[2]};
return ComputeAngle<TYPE1,TYPE2>(V1, V2);
} // ComputeAngle
// given four 3D points,
// compute the angle between the two vectors formed by the first and second pair of points
// returns cos(angle)
template<typename TYPE1, typename TYPE2>
inline TYPE2 ComputeAngle(const TYPE1* X1, const TYPE1* C1, const TYPE1* X2, const TYPE1* C2) {
// subtract out the camera center
const TYPE1 V1[] = {X1[0]-C1[0], X1[1]-C1[1], X1[2]-C1[2]};
const TYPE1 V2[] = {X2[0]-C2[0], X2[1]-C2[1], X2[2]-C2[2]};
return ComputeAngle<TYPE1,TYPE2>(V1, V2);
} // ComputeAngle
/*----------------------------------------------------------------*/
// given a triangle defined by three 3D points,
// compute its normal (plane's normal oriented according to the given points order)
template<typename TYPE>
inline TPoint3<TYPE> ComputeTriangleNormal(const TPoint3<TYPE>& v0, const TPoint3<TYPE>& v1, const TPoint3<TYPE>& v2) {
return (v1-v0).cross(v2-v0);
} // ComputeTriangleNormal
/*----------------------------------------------------------------*/
// compute the area of a triangle using Heron's formula
template <typename TYPE>
TYPE ComputeTriangleAreaSqLen(TYPE lena, TYPE lenb, TYPE lenc) {
const TYPE s((lena+lenb+lenc)/TYPE(2));
return s*(s-lena)*(s-lenb)*(s-lenc);
}
template <typename TYPE>
TYPE ComputeTriangleAreaLen(TYPE lena, TYPE lenb, TYPE lenc) {
return (TYPE)sqrt(ComputeTriangleAreaSqLen(lena, lenb, lenc));
}
template <typename TYPE>
TYPE ComputeTriangleAreaSqLenSq(TYPE lenaSq, TYPE lenbSq, TYPE lencSq) {
return SQUARE(lenaSq+lenbSq+lencSq)/TYPE(2)-(lenaSq*lenaSq+lenbSq*lenbSq+lencSq*lencSq);
}
template <typename TYPE>
TYPE ComputeTriangleAreaLenSq(TYPE lenaSq, TYPE lenbSq, TYPE lencSq) {
return (TYPE)sqrt(ComputeTriangleAreaSqLenSq(lenaSq, lenbSq, lencSq));
}
// compute area for a triangle defined by three 2D points
template <typename TYPE>
TYPE ComputeTriangleArea(const TPoint2<TYPE>& x0, const TPoint2<TYPE>& x1, const TPoint2<TYPE>& x2) {
return abs((TYPE)((x0-x2).cross(x0-x1)/TYPE(2)));
}
// compute area for a triangle defined by three 3D points
template <typename TYPE>
TYPE ComputeTriangleAreaSq(const TPoint3<TYPE>& x0, const TPoint3<TYPE>& x1, const TPoint3<TYPE>& x2) {
return (TYPE)(normSq((x1-x0).cross(x2-x0))/TYPE(4));
}
template <typename TYPE>
TYPE ComputeTriangleArea(const TPoint3<TYPE>& x0, const TPoint3<TYPE>& x1, const TPoint3<TYPE>& x2) {
return (TYPE)sqrt(ComputeTriangleAreaSq(x0, x1, x2));
} // ComputeTriangleArea
/*----------------------------------------------------------------*/
// compute signed volume of the tetrahedron defined by the given triangle connected to origin
template <typename TYPE>
TYPE ComputeTriangleVolume(const TPoint3<TYPE>& x0, const TPoint3<TYPE>& x1, const TPoint3<TYPE>& x2) {
return (TYPE)(x0.dot(x1.cross(x2)) / TYPE(6));
} // ComputeTriangleVolume
/*----------------------------------------------------------------*/
// compute a shape quality measure of the triangle composed by vertices (v0,v1,v2)
// returns 2*AreaTri/(MaxEdge^2) in range [0, 0.866]
// (ex: equilateral sqrt(3)/2, half-square 1/2, up to a line that has zero quality)
template<typename TYPE>
inline TYPE ComputeTriangleQuality(const TPoint3<TYPE>& v0, const TPoint3<TYPE>& v1, const TPoint3<TYPE>& v2) {
const TPoint3<TYPE> d10(v1-v0);
const TPoint3<TYPE> d20(v2-v0);
const TPoint3<TYPE> d12(v1-v2);
const TYPE a((TYPE)norm(d10.cross(d20)));
if (a == 0) return 0; // area zero triangles have surely zero quality
const TYPE nd10(normSq(d10));
if (nd10 == 0) return 0; // area zero triangles have surely zero quality
const TYPE b(MAXF3(nd10, normSq(d20), normSq(d12)));
return a/b;
} // ComputeTriangleQuality
/*----------------------------------------------------------------*/
// given a triangle defined by 3 vertex positions and a point,
// compute the barycentric coordinates corresponding to that point
// (only the first two values: alpha and beta)
template <typename TYPE>
inline TPoint2<TYPE> BarycentricCoordinatesUV(const TPoint3<TYPE>& A, const TPoint3<TYPE>& B, const TPoint3<TYPE>& C, const TPoint3<TYPE>& P) {
#if 0
// the triangle normal
const TPoint3<TYPE> normalVec((B-A).cross(C-A));
//const TYPE normalLen(norm(normalVec));
// the area of the triangles
const TYPE invAreaABC(INVERT(normSq(normalVec)/*/(2*normalLen)*/));
const TPoint3<TYPE> CP(C-P);
const TYPE areaPBC(normalVec.dot((B-P).cross(CP))/*/(2*normalLen)*/);
const TYPE areaPCA(normalVec.dot(CP.cross(A-P))/*/(2*normalLen)*/);
// the barycentric coordinates
return TPoint2<TYPE>(
areaPBC * invAreaABC, // alpha
areaPCA * invAreaABC // beta
);
#else
// using the Cramer's rule for solving a linear system
const TPoint3<TYPE> v0(A-C), v1(B-C), v2(P-C);
const TYPE d00(normSq(v0));
const TYPE d01(v0.dot(v1));
const TYPE d11(normSq(v1));
const TYPE d20(v2.dot(v0));
const TYPE d21(v2.dot(v1));
const TYPE invDenom(INVERT(d00 * d11 - d01 * d01));
return TPoint2<TYPE>(
(d11 * d20 - d01 * d21) * invDenom, // alpha
(d00 * d21 - d01 * d20) * invDenom // beta
);
#endif
}
// same as above, but returns all three barycentric coordinates
template <typename TYPE>
inline TPoint3<TYPE> BarycentricCoordinates(const TPoint3<TYPE>& A, const TPoint3<TYPE>& B, const TPoint3<TYPE>& C, const TPoint3<TYPE>& P) {
TPoint2<TYPE> b(BarycentricCoordinatesUV(A, B, C, P));
return TPoint3<TYPE>(
b.x, // alpha
b.y, // beta
TYPE(1)-b.x-b.y // gamma
);
}
// same as above, but for 2D triangle case
template <typename TYPE>
inline TPoint2<TYPE> BarycentricCoordinatesUV(const TPoint2<TYPE>& A, const TPoint2<TYPE>& B, const TPoint2<TYPE>& C, const TPoint2<TYPE>& P) {
const TPoint2<TYPE> D(P - C);
const TYPE d00(A.x - C.x);
const TYPE d01(B.x - C.x);
const TYPE d10(A.y - C.y);
const TYPE d11(B.y - C.y);
const TYPE invDet(INVERT(d00 * d11 - d10 * d01));
return TPoint2<TYPE>(
(d11 * D.x - d01 * D.y) * invDet, // alpha
(d00 * D.y - d10 * D.x) * invDet // beta
);
}
// same as above, but returns all three barycentric coordinates
template <typename TYPE>
inline TPoint3<TYPE> BarycentricCoordinates(const TPoint2<TYPE>& A, const TPoint2<TYPE>& B, const TPoint2<TYPE>& C, const TPoint2<TYPE>& P) {
TPoint2<TYPE> b(BarycentricCoordinatesUV(A, B, C, P));
return TPoint3<TYPE>(
b.x, // alpha
b.y, // beta
TYPE(1)-b.x-b.y // gamma
);
}
// correct the barycentric coordinates in case of numerical errors
// (the corresponding point is assumed to be inside the triangle)
template <typename TYPE>
inline TPoint3<TYPE> CorrectBarycentricCoordinates(TPoint2<TYPE> b) {
if (b.x < TYPE(0)) // alpha
b.x = TYPE(0);
else if (b.x > TYPE(1))
b.x = TYPE(1);
if (b.y < TYPE(0)) // beta
b.y = TYPE(0);
else if (b.y > TYPE(1))
b.y = TYPE(1);
TYPE z(TYPE(1) - b.x - b.y); // gamma
if (z < 0) {
// equally distribute the error
const TYPE half(-z/TYPE(2));
if (half > b.x) {
b.x = TYPE(0);
b.y = TYPE(1);
} else
if (half > b.y) {
b.x = TYPE(1);
b.y = TYPE(0);
} else {
b.x -= half;
b.y -= half;
}
z = TYPE(0);
}
// check that the given point is inside the triangle
ASSERT((b.x >= 0) && (b.y >= 0) && (b.x+b.y <= 1) && ISEQUAL(b.x+b.y+z, TYPE(1)));
return TPoint3<TYPE>(b.x, b.y, z);
}
/*----------------------------------------------------------------*/
// Encodes/decodes a normalized 3D vector in two parameters for the direction
template<typename T, typename TR>
inline void Normal2Dir(const TPoint3<T>& d, TPoint2<TR>& p) {
ASSERT(ISEQUAL(norm(d), T(1)));
p.x = TR(atan2(d.y, d.x));
p.y = TR(acos(d.z));
}
template<typename T, typename TR>
inline void Dir2Normal(const TPoint2<T>& p, TPoint3<TR>& d) {
const T siny(sin(p.y));
d.x = TR(cos(p.x)*siny);
d.y = TR(sin(p.x)*siny);
d.z = TR(cos(p.y));
ASSERT(ISEQUAL(norm(d), TR(1)));
}
// Encodes/decodes a 3D vector in two parameters for the direction and one parameter for the scale
template<typename T, typename TR>
inline void Vector2DirScale(const T vect[3], TR dir[2], TR* scale)
{
const T scl(sqrt(SQUARE(vect[0])+SQUARE(vect[1])+SQUARE(vect[2])));
ASSERT(!ISZERO(scl));
scale[0] = TR(scl);
dir[0] = TR(atan2(vect[1], vect[0]));
dir[1] = TR(acos(vect[2] / scl));
}
template<typename T, typename TR>
inline void DirScale2Vector(const T dir[2], const T* scale, TR vect[3])
{
ASSERT(!ISZERO(*scale));
const T siny(*scale*sin(dir[1]));
vect[0] = TR(cos(dir[0])*siny);
vect[1] = TR(sin(dir[0])*siny);
vect[2] = TR(*scale*cos(dir[1]));
}
/*----------------------------------------------------------------*/
template<typename T>
inline T MaxDepthDifference(T d, T threshold) {
#if 0
return (d*threshold*T(2))/(threshold+T(2));
#else
return d*threshold;
#endif
}
template<typename T>
inline T DepthSimilarity(T d0, T d1) {
ASSERT(d0 > 0 && d1 > 0);
#if 0
return ABS(d0-d1)*T(2)/(d0+d1);
#else
return ABS(d0-d1)/d0;
#endif
}
template<typename T>
inline bool IsDepthSimilar(T d0, T d1, T threshold=T(0.01)) {
return DepthSimilarity(d0, d1) < threshold;
}
template<typename T>
inline bool IsNormalSimilar(const TPoint3<T>& n0, const TPoint3<T>& n1, T threshold=T(0.996194698)/*COS(FD2R(5.f))*/) {
return ComputeAngle<T,T>(n0.ptr(), n1.ptr()) > threshold;
}
/*----------------------------------------------------------------*/
// searches min/max value
template<typename TYPE>
inline TYPE FindMinElement(const TYPE* values, size_t n) {
TYPE m = std::numeric_limits<TYPE>::max();
while (n) if (values[--n] < m) m = values[n];
return m;
}
template<typename TYPE>
inline TYPE FindMaxElement(const TYPE* values, size_t n) {
TYPE m = -std::numeric_limits<TYPE>::max();
while (n) if (values[--n] > m) m = values[n];
return m;
}
// like above, but considers absolute value only
template<typename TYPE>
inline TYPE FindAbsMinElement(const TYPE* values, size_t n) {
TYPE m = std::numeric_limits<TYPE>::max();
while (n) if (ABS(values[--n]) < m) m = ABS(values[n]);
return m;
}
template<typename TYPE>
inline TYPE FindAbsMaxElement(const TYPE* values, size_t n) {
TYPE m = -std::numeric_limits<TYPE>::max();
while (n) if (ABS(values[--n]) > m) m = ABS(values[n]);
return m;
}
/*----------------------------------------------------------------*/
// given an array of values and their bound, approximate the area covered, in percentage
template<typename TYPE, int n, int s, bool bCentered>
inline TYPE ComputeCoveredArea(const TYPE* values, size_t size, const TYPE* bound, int stride=n) {
ASSERT(size > 0);
typedef Eigen::Matrix<TYPE,1,n,Eigen::RowMajor> Vector;
typedef Eigen::Map<const Vector,Eigen::Unaligned> MapVector;
typedef Eigen::Matrix<TYPE,Eigen::Dynamic,n,Eigen::RowMajor> Matrix;
typedef Eigen::Map<const Matrix,Eigen::Unaligned,Eigen::OuterStride<> > MapMatrix;
typedef Eigen::Matrix<unsigned,s,s,Eigen::RowMajor> MatrixSurface;
const MapMatrix points(values, size, n, Eigen::OuterStride<>(stride));
const Vector norm = MapVector(bound);
const Vector offset(Vector::Constant(bCentered ? TYPE(0.5) : TYPE(0)));
MatrixSurface surface;
surface.setZero();
for (size_t i=0; i<size; ++i) {
const Vector point((points.row(i).cwiseQuotient(norm)+offset)*TYPE(s));
ASSERT((point(0)>=0 && point(0)<s) && (point(1)>=0 && point(1)<s));
surface(FLOOR2INT(point(0)), FLOOR2INT(point(1))) = 1;
}
return TYPE(surface.sum())/(s*s);
} // ComputeCoveredArea
/*----------------------------------------------------------------*/
// given an array of values, compute the variance-covariance matrix
template<typename TYPE, int n>
inline void ComputeCovarianceMatrix(const TYPE* values, size_t size, TYPE* cov, int stride=n) {
ASSERT(size > 0);
typedef Eigen::Matrix<TYPE,1,n,Eigen::RowMajor> Vector;
typedef Eigen::Matrix<TYPE,Eigen::Dynamic,n,Eigen::RowMajor> Matrix;
typedef Eigen::Map<const Matrix,Eigen::Unaligned,Eigen::OuterStride<> > MapMatrix;
typedef Eigen::Matrix<TYPE,n,n,Eigen::RowMajor> MatrixOut;
typedef Eigen::Map<MatrixOut> MapMatrixOut;
const MapMatrix points(values, size, n, Eigen::OuterStride<>(stride));
const Vector mean(points.colwise().sum() * (TYPE(1)/size));
const Matrix transPoints(points.rowwise() - mean);
MapMatrixOut mapCov(cov);
mapCov = transPoints.transpose() * transPoints * (TYPE(1)/(size-1));
} // ComputeCovarianceMatrix
/*----------------------------------------------------------------*/
// given an array of values, compute the mean
template<typename TYPE, typename TYPEW>
inline void ComputeMean(const TYPE* values, size_t size, TYPEW& mean) {
ASSERT(size > 0);
TYPEW sum(0);
for (size_t i=0; i<size; ++i)
sum += TYPEW(values[i]);
mean = sum / size;
} // ComputeMean
/*----------------------------------------------------------------*/
// given an array of values, compute the geometric mean
template<typename TYPE, typename TYPEW>
inline void ComputeGeometricMean(const TYPE* values, size_t size, TYPEW& gmean) {
ASSERT(size > 0);
TYPEW prod(1);
for (size_t i=0; i<size; ++i)
prod *= TYPEW(values[i]);
gmean = POW(prod, 1.f/size);
} // ComputeGeometricMean
/*----------------------------------------------------------------*/
// given an array of values, compute the mean and standard deviance
template<typename TYPE, typename TYPEW>
inline void ComputeMeanStdOffline(const TYPE* values, size_t size, TYPEW& mean, TYPEW& stddev) {
ASSERT(size > 0);
TYPEW sum(0);
FOREACHRAWPTR(pVal, values, size)
sum += TYPEW(*pVal);
mean = sum / (float)size;
TYPEW sumSq(0);
FOREACHRAWPTR(pVal, values, size)
sumSq += SQUARE(TYPEW(*pVal)-mean);
const TYPEW variance(sumSq / (size - 1));
stddev = SQRT(variance);
} // ComputeMeanStdOffline
// same as above, but uses one pass only (online)
template<typename TYPE, typename TYPEW=TYPE, typename ARGTYPE=const TYPE&>
struct MeanStdOnline {
TYPEW mean;
TYPEW stddev;
size_t size;
MeanStdOnline() : mean(0), stddev(0), size(0) {}
MeanStdOnline(const TYPE* values, size_t _size) : mean(0), stddev(0), size(0) {
Compute(values, _size);
}
void Update(ARGTYPE v) {
const TYPEW val(v);
const TYPEW delta(val-mean);
mean += delta / (float)(++size);
stddev += delta * (val-mean);
}
void Compute(const TYPE* values, size_t _size) {
for (size_t i=0; i<_size; ++i)
Update(values[i]);
}
TYPEW GetMean() const { return mean; }
TYPEW GetVariance() const { return (stddev / (float)(size - 1)); }
TYPEW GetStdDev() const { return SQRT(GetVariance()); }
};
template<typename TYPE, typename TYPEW>
inline void ComputeMeanStdOnline(const TYPE* values, size_t size, TYPEW& mean, TYPEW& stddev) {
ASSERT(size > 0);
mean = TYPEW(0);
stddev = TYPEW(0);
for (size_t i=0; i<size; ++i) {
const TYPEW val(values[i]);
const TYPEW delta(val-mean);
mean += delta / (i+1);
stddev += delta * (val-mean);
}
const TYPEW variance(stddev / (float)(size - 1));
stddev = SQRT(variance);
} // ComputeMeanStdOnline
// same as above, but less stable in general (and faster)
template<typename TYPE, typename TYPEW=TYPE, typename ARGTYPE=const TYPE&>
struct MeanStdOnlineFast {
TYPEW sum;
TYPEW sumSq;
size_t size;
MeanStdOnlineFast() : sum(0), sumSq(0), size(0) {}
MeanStdOnlineFast(const TYPE* values, size_t _size) : sum(0), sumSq(0), size(0) {
Compute(values, _size);
}
void Update(ARGTYPE v) {
const TYPEW val(v);
sum += val;
sumSq += SQUARE(val);
++size;
}
void Compute(const TYPE* values, size_t _size) {
for (size_t i=0; i<_size; ++i)
Update(values[i]);
}
TYPEW GetMean() const { return sum / (float)size; }
TYPEW GetVariance() const { return (sumSq - SQUARE(sum) / (float)size) / (float)(size - 1); }
TYPEW GetStdDev() const { return SQRT(GetVariance()); }
};
template<typename TYPE, typename TYPEW>
inline void ComputeMeanStdOnlineFast(const TYPE* values, size_t size, TYPEW& mean, TYPEW& stddev) {
ASSERT(size > 0);
TYPEW sum(0), sumSq(0);
FOREACHRAWPTR(pVal, values, size) {
const TYPEW val(*pVal);
sum += val;
sumSq += SQUARE(val);
}
const TYPEW invSize(TYPEW(1)/(float)size);
mean = sum * invSize;
const TYPEW variance((sumSq - SQUARE(sum) * invSize) / (float)(size - 1));
stddev = SQRT(variance);
} // ComputeMeanStdOnlineFast
#define ComputeMeanStd ComputeMeanStdOnlineFast
#define MeanStd MeanStdOnlineFast
/*----------------------------------------------------------------*/
// given an array of values, compute the X84 threshold as in:
// Hampel FR, Rousseeuw PJ, Ronchetti EM, Stahel WA
// "Robust Statistics: the Approach Based on Influence Functions"
// Wiley Series in Probability and Mathematical Statistics, John Wiley & Sons, 1986
// returns the pair(median,trust_region)
// upper-bound threshold = median+trust_region
// lower-bound threshold = median-trust_region
template<typename TYPE, typename TYPEW>
inline std::pair<TYPEW,TYPEW> ComputeX84Threshold(const TYPE* const values, size_t size, TYPEW mul=TYPEW(5.2), const uint8_t* mask=NULL) {
ASSERT(size > 0);
// median = MEDIAN(values);
cList<TYPE, const TYPE&, 0> data;
if (mask) {
// use only masked data
data.Reserve(size);
for (size_t i=0; i<size; ++i)
if (mask[i])
data.Insert(values[i]);
} else {
// use all data
data.CopyOf(values, size);
}
TYPE* const mid = data.Begin() + size / 2;
std::nth_element(data.Begin(), mid, data.End());
const TYPEW median(*mid);
// threshold = 5.2 * MEDIAN(ABS(values-median));
using TYPEI = typename MakeSigned<TYPE>::type;
for (TYPE& val: data)
val = TYPE(ABS(TYPEI(val)-TYPEI(median)));
std::nth_element(data.Begin(), mid, data.End());
return std::make_pair(median, mul*TYPEW(*mid));
} // ComputeX84Threshold
/*----------------------------------------------------------------*/
// given an array of values, compute the upper/lower threshold using quartiles
template<typename TYPE, typename TYPEW>
inline std::tuple<TYPEW,TYPEW,TYPEW,TYPEW,TYPEW> ComputeQuartileThreshold(const TYPE* const values, size_t size, TYPEW mul=TYPEW(1.5), const uint8_t* mask=NULL) {
ASSERT(size > 0);
cList<TYPE, const TYPE&, 0> data(size);
if (mask) {
// use only masked data
data.Reserve(size);
for (size_t i=0; i<size; ++i)
if (mask[i])
data.Insert(values[i]);
} else {
// use all data
data.CopyOf(values, size);
}
TYPE* const pQB = data.Begin();
TYPE* const pQE = data.End();
TYPE* const pQ1 = pQB + size / 4;
TYPE* const pQ2 = pQB + size / 2;
TYPE* const pQ3 = pQB + size * 3 / 4;
std::nth_element( pQB, pQ2, pQE);
std::nth_element( pQB, pQ1, pQ2);
std::nth_element(pQ2+1, pQ3, pQE);
const TYPEW Q1(*pQ1), Q2(*pQ2), Q3(*pQ3);
const TYPEW IQR(Q3 - Q1);
return std::make_tuple(Q1-mul*IQR, Q3+mul*IQR, Q1, Q2, Q3);
} // ComputeQuartileThreshold
/*----------------------------------------------------------------*/
template <typename TYPE>
inline REAL ComputeSNR(const TDMatrix<TYPE>& x0, const TDMatrix<TYPE>& x) {
ASSERT(x0.area() == x.area());
const REAL err(norm(TDMatrix<TYPE>(x0 - x)));
const REAL x0Norm(norm(x0));
REAL ret(std::numeric_limits<REAL>::infinity());
if (ISZERO(x0Norm) && err > 0) ret = 0;
else if (err > 0) ret = 20.0 * std::log10(x0Norm / err);
return ret;
} // ComputeSNR
template<typename TYPE, int N>
inline REAL ComputeSNR(const TMatrix<TYPE,N,1>& x0, const TMatrix<TYPE,N,1>& x) {
return ComputeSNR(TDMatrix<TYPE>(N,1,(TYPE*)x0.val), TDMatrix<TYPE>(N,1,(TYPE*)x.val));
} // ComputeSNR
template<typename TYPE>
inline REAL ComputePSNR(const TDMatrix<TYPE>& x0, const TDMatrix<TYPE>& x) {
ASSERT(x0.area() == x.area());
const size_t N(x0.area());
const REAL err(normSq(TDMatrix<TYPE>(x0 - x)) / N);
TYPE max1(0), max2(0);
for (unsigned i=0; i<N; ++i) {
max1 = MAXF(max1, x0[i]);
max2 = MAXF(max2, x[i]);
}
const TYPE maxBoth(MAXF(max1, max2));
REAL ret(std::numeric_limits<REAL>::infinity());
if (ISZERO(maxBoth) && err > 0) ret = 0;
else if (err > 0) ret = 10.0 * std::log10(static_cast<REAL>(maxBoth*maxBoth) / err);
return ret;
} // ComputePSNR
template<typename TYPE, int N>
inline REAL ComputePSNR(const TMatrix<TYPE,N,1>& x0, const TMatrix<TYPE,N,1>& x) {
return ComputePSNR(TDMatrix<TYPE>(N,1,(TYPE*)x0.val), TDMatrix<TYPE>(N,1,(TYPE*)x.val));
} // ComputePSNR
/*----------------------------------------------------------------*/
// Builds histograms using "Kernel Density Estimation" and Gaussian kernels;
// see: L. Wassermann: "All of Statistics" for example.
// Samples should be composed of two point: value and weight
template <typename TYPE>
inline TYPE BandwidthGaussianKDE(const TMatrix<TYPE,2,1>* samples, size_t numSamples, TYPE sigma=0, TYPE* pSigma=NULL) {
if (sigma == 0) {
TYPE avg(0);
FOREACHRAWPTR(pSample, samples, numSamples) {
const TYPE& sample((*pSample)(0));
avg += sample;
sigma += SQUARE(sample);
}
avg /= numSamples;
sigma = SQRT(sigma/numSamples - avg*avg); // Standard Deviation
if (pSigma) *pSigma = sigma;
}
// This is the optimal bandwidth if the point distribution is Gaussian.
// (see "Applied Smoothing Techniques for Data Analysis" by Adrian W, Bowman & Adelchi Azzalini (1997))
return POW(TYPE(4)/(TYPE(3)*numSamples), TYPE(1)/TYPE(5))*sigma;
}
// estimate density at the given value
template <typename TYPE>
inline TYPE KernelDensityEstimation(const TMatrix<TYPE,2,1>* samples, size_t numSamples, TYPE x, TYPE bandwidth) {
TYPE y(0);
const TYPE invBandwidth(TYPE(1)/bandwidth);
FOREACHRAWPTR(pSample, samples, numSamples) {
const TMatrix<TYPE,2,1>& sample(*pSample);
const TYPE val((x - sample(0))*invBandwidth);
y += sample(1) * EXP(-TYPE(0.5)*val*val)*invBandwidth;
}
return (y / numSamples) * INV_SQRT_2PI;
}
// estimate density at the given range of values;
// INSERTER should have defined the operator(TYPE,TYPE) that receives a pair of value and density
template <typename TYPE, typename INSERTER>
void KernelDensityEstimation(const TMatrix<TYPE,2,1>* samples, size_t numSamples, TYPE bandwidth, TYPE xMin, TYPE xMax, size_t steps, INSERTER& inserter) {
const TYPE step((xMax-xMin)/steps);