1 
2 // Copyright (c) 2007, 2008 libmv authors.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to
6 // deal in the Software without restriction, including without limitation the
7 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 // sell copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 // IN THE SOFTWARE.
21 
22 // This file is part of OpenMVG, an Open Multiple View Geometry C++ library.
23 
24 // Copyright (c) 2012, 2013 Pierre MOULON.
25 
26 // This Source Code Form is subject to the terms of the Mozilla Public
27 // License, v. 2.0. If a copy of the MPL was not distributed with this
28 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
29 
30 #include "openMVG/numeric/numeric.h"
31 
32 #include "CppUnitLite/TestHarness.h"
33 #include "testing/testing.h"
34 
35 #include <iostream>
36 #include <set>
37 
38 using namespace openMVG;
39 using namespace std;
40 
41 //-- Assert that stream interface is available
TEST(TinyMatrix,print)42 TEST ( TinyMatrix, print )
43 {
44   Mat3 testMatrix = Mat3::Identity();
45   std::cout << testMatrix;
46 }
47 
TEST(TinyMatrix,checkIdentity)48 TEST ( TinyMatrix, checkIdentity )
49 {
50   Mat3 testMatrix, expected;
51 
52   // build expected matrix and the test matrix
53   expected.fill(0);
54   expected(0,0) = expected(1,1) = expected(2,2) = 1.0;
55 
56   testMatrix.setIdentity();
57   std::cout << std::endl << testMatrix;
58   //-- Compare expected to the testMatrix.
59   EXPECT_MATRIX_NEAR( expected, testMatrix, 1e-8);
60 }
61 
TEST(TinyMatrix,product)62 TEST ( TinyMatrix, product )
63 {
64   Mat3 a, b, expected;
65 
66   // build expected matrix and the test matrix
67   a(0,0) = 1.0; a(0,1) = 2.0; a(0,2) = 3.0;
68   a(1,0) = 4.0; a(1,1) = 5.0; a(1,2) = 6.0;
69   a(2,0) = 7.0; a(2,1) = 8.0; a(2,2) = 9.0;
70 
71   b(0,0) = 10.0; b(0,1) = 11.0; b(0,2) = 12.0;
72   b(1,0) = 13.0; b(1,1) = 14.0; b(1,2) = 15.0;
73   b(2,0) = 16.0; b(2,1) = 17.0; b(2,2) = 18.0;
74 
75   Mat3 resAxB = a*b;
76   Mat3 expected_resAxB;
77   {
78     Mat3 & t = expected_resAxB;
79     t(0,0) = 84.0;  t(0,1) = 90.0;    t(0,2) = 96.0;
80     t(1,0) = 201.0; t(1,1) = 216.0;  t(1,2) = 231.0;
81     t(2,0) = 318.0; t(2,1) = 342.0;  t(2,2) = 366.0;
82   }
83 
84   Mat3 resBxA = b*a;
85   Mat3 expected_resBxA;
86   {
87     Mat3 & t = expected_resBxA;
88     t(0,0) = 138; t(0,1) = 171;  t(0,2) = 204;
89     t(1,0) = 174; t(1,1) = 216;  t(1,2) = 258;
90     t(2,0) = 210; t(2,1) = 261;  t(2,2) = 312;
91   }
92 
93   //-- Tests
94   EXPECT_MATRIX_NEAR( expected_resAxB, resAxB, 1e-8);
95   EXPECT_MATRIX_NEAR( expected_resBxA, resBxA, 1e-8);
96 }
97 
TEST(TinyMatrix,LookAt)98 TEST(TinyMatrix, LookAt) {
99   // Simple orthogonality check.
100   Vec3 e; e[0]= 1; e[1] = 2; e[2] = 3;
101   Mat3 R = LookAt(e);
102   Mat3 I = Mat3::Identity();
103   Mat3 RRT = R*R.transpose();
104   Mat3 RTR = R.transpose()*R;
105 
106   EXPECT_MATRIX_NEAR(I, RRT, 1e-15);
107   EXPECT_MATRIX_NEAR(I, RTR, 1e-15);
108 }
109 
TEST(Numeric,MeanAndVarianceAlongRows)110 TEST(Numeric, MeanAndVarianceAlongRows) {
111   int n = 4;
112   Mat points(2,n);
113   points << 0, 0, 1, 1,
114     0, 2, 1, 3;
115 
116   Vec mean, variance;
117   MeanAndVarianceAlongRows(points, &mean, &variance);
118 
119   EXPECT_NEAR(0.5, mean(0), 1e-8);
120   EXPECT_NEAR(1.5, mean(1), 1e-8);
121   EXPECT_NEAR(0.25, variance(0), 1e-8);
122   EXPECT_NEAR(1.25, variance(1), 1e-8);
123 }
124 
TEST(Numeric,minMaxMeanMedian)125 TEST(Numeric, minMaxMeanMedian)
126 {
127   const int vec_size = 12;
128   std::vector<double> values(vec_size);
129   std::iota(values.begin(), values.end(), 0.0);
130   double min, max, mean, median;
131   minMaxMeanMedian(values.cbegin(), values.cend(), min, max, mean, median);
132 
133   // For the GT value, use a set to sort the value
134   //  and then collect the min, max, median and mean value
135   const std::set<double> set_values(values.cbegin(), values.cend());
136   EXPECT_NEAR(0.0, min, 1e-8);
137   EXPECT_NEAR(values.size() - 1, max, 1e-8);
138   auto set_begin_it = set_values.cbegin();
139   std::advance (set_begin_it, values.size() / 2);
140   EXPECT_NEAR(*set_begin_it, median, 1e-8);
141   const double mean_gt = std::accumulate(values.begin(), values.end(), 0.0) / static_cast<double>(values.size());
142   EXPECT_NEAR(mean_gt, mean, 1e-8);
143 }
144 
145 /* ************************************************************************* */
main()146 int main() { TestResult tr; return TestRegistry::runAllTests(tr);}
147 /* ************************************************************************* */
148