Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestTriangulation.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8  BIAS is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 2.1 of the License, or
11  (at your option) any later version.
12 
13  BIAS is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with BIAS; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21 
22 
23 
24 /**
25  @file TestTriangulation.cpp
26  @brief unit test for Triangulation::TriangulateProjective
27  @relates Triangulation
28  @ingroup g_tests
29  @author woelk 06/2008 (c) www.vision-n.de
30 */
31 
32 #include <Geometry/Triangulation.hh>
33 #include <Geometry/ProjectionParametersPerspective.hh>
34 #include <Geometry/ProjectionParametersSphericalSimple.hh>
35 #include <Base/Common/SharedPtr.hh>
36 
37 using namespace BIAS;
38 using namespace std;
39 
40 bool verbose = true;
41 
42 /**\cond HIDDEN_SYMBOLS */
43 struct STestData
44 {
46  HomgPoint2D p1, p2;
47  HomgPoint3D p3d;
48  int triangulation_result;
49 };
50 /** \endcond */
51 
52 void FillTestCases(vector<struct STestData> &cases);
53 
54 bool RunAndCheck(const struct STestData &data);
55 
56 
57 int main(int argc, char *argv[])
58 {
59  if (argc==2){
60  string argv1 = argv[1];
61  if (argv1=="silent") verbose = false;
62  }
63  vector<struct STestData> cases;
64  FillTestCases(cases);
65 
66  for (unsigned i=0; i<cases.size(); i++){
67  if (!RunAndCheck(cases[i])){
68  if (verbose) cout << "triangulation failed\n";
69  return -1;
70  }
71  //if (verbose) cout << "." << flush;
72  }
73 
74  return 0;
75 }
76 
77 
78 bool RunAndCheck(const struct STestData &data)
79 {
80  Triangulation tri;
81  HomgPoint3D p3d, tmp;
82 
83  int res = tri.TriangulateProjective(Get(data.proj1), Get(data.proj2),
84  data.p1, data.p2, p3d);
85 
86  if (verbose) cout << "\nexpected: "<<data.p3d
87  << "\ngot: "<<p3d<<endl;
88 
89  if (res != data.triangulation_result){
90  if (verbose) cout << "result mismatch: expected: "
91  <<data.triangulation_result<<" got: "<<res<<"\n";
92  return false;
93  }
94  tmp = data.p3d;
95  BIASASSERT(!Equal(tmp.NormL2(), 0.0));
96  tmp /= tmp.NormL2();
97 
98  BIASASSERT(!Equal(p3d.NormL2(), 0.0));
99  p3d /= p3d.NormL2();
100  // difference vector
101  Vector4<double> diff = tmp - p3d;
102 
103  double eps = numeric_limits<double>::epsilon() * 5.0;
104  if (diff.NormL2() > eps){
105  if (verbose) cout << "result point is incorrect: got "<<p3d
106  <<"\n expected: "<<tmp<<"\n";
107  return false;
108  }
109 
110 
111  return true;
112 }
113 
114 
115 void FillTestCases(vector<struct STestData> &cases)
116 {
117  cases.clear();
118 
119  Vector3<double> C1(0., 0., 0.), C2(1., 0., 0.);
121  Quaternion<double> Q1(0., 0., 0., 1.), Q2(0., 0., 0., 1.);
125  proj1->SetK(K);
126  proj1->SetC(C1);
127  proj1->SetQ(Q1);
128  proj2->SetK(K);
129  proj2->SetC(C2);
130  proj2->SetQ(Q2);
131 
132  const int width = 640, height = 480;
133  const double phiOffs=0.0, thetaOffs=0., dPhi=0.001, dTheta=0.001;
135  projsph1 = SharedPtr<ProjectionParametersSphericalSimple>(new ProjectionParametersSphericalSimple(phiOffs, thetaOffs, dPhi, dTheta, width, height));
136  projsph2 = SharedPtr<ProjectionParametersSphericalSimple>(new ProjectionParametersSphericalSimple(phiOffs, thetaOffs, dPhi, dTheta, width, height));
137  projsph1->SetC(C1);
138  projsph1->SetQ(Q1);
139  projsph2->SetC(C2);
140  projsph2->SetQ(Q2);
141 
142  struct STestData data;
143 
144  data.proj1 = proj1;
145  data.proj2 = proj2;
146 
147  // exact intersection
148  data.p1.Set(0., 0., 1.);
149  data.p2.Set(-0.1, 0., 1.);
150  data.p3d.Set(0., 0., 10., 1.);
151  data.triangulation_result = 0;
152  cases.push_back(data);
153 
154  // exact intersection
155  data.p1.Set(0.1, 0., 1.);
156  data.p2.Set(0.0, 0., 1.);
157  data.p3d.Set(1., 0., 10., 1.);
158  data.triangulation_result = 0;
159  cases.push_back(data);
160 
161  // distant point
162  data.p1.Set(0.001, 0., 1.);
163  data.p2.Set(0.0, 0., 1.);
164  data.p3d.Set(1., 0., 1000., 1.);
165  data.triangulation_result = 0;
166  cases.push_back(data);
167 
168  // resulting point behind camera
169  data.p1.Set(0.0, 0.0, 1.);
170  data.p2.Set(0.1, 0.0, 1.);
171  data.p3d.Set(0., 0., -10., 1.);
172  data.triangulation_result = TRIANG_RES_BEHIND_CAMERA1;
173  cases.push_back(data);
174 
175  // resulting point at infinity
176  data.p1.Set(0.0, 0.0, 1.);
177  data.p2.Set(0.0, 0.0, 1.);
178  data.p3d.Set(0., 0., 1., 0.);
179  data.triangulation_result = TRIANG_RES_PARALLEL;
180  cases.push_back(data);
181 
182  // lines are not parallel, but closest point is between camera centers
183  data.p1.Set(0.0, 0.1, 1.);
184  data.p2.Set(0.0, 0.0, 1.);
185  data.p3d.Set(0.5, 0., 0., 1.);
186  data.triangulation_result = 0;
187  cases.push_back(data);
188 
189  // lines do not intersect exactly
190  data.p1.Set(0.1, 0.01, 1.);
191  data.p2.Set(-0.1, -0.01, 1.);
192  data.p3d.Set(0.5, 0., 4.950495049504950, 1.);
193  data.triangulation_result = 0;
194  cases.push_back(data);
195 
196  // now spherical projection parameters
197  data.proj1 = projsph1;
198  data.proj2 = projsph2;
199 
200  // optical axes
201  data.p1.Set(M_PI/2./dTheta, 0.0, 1.);
202  data.p2.Set(M_PI/2./dTheta, 0.0, 1.);
203  data.p3d.Set(0., 0., 1., 0.);
204  data.triangulation_result = TRIANG_RES_PARALLEL;
205  cases.push_back(data);
206 
207  // exact intersection
208  data.p1.Set(M_PI/2./dTheta, 0.0, 1.);
209  data.p2.Set(3.*M_PI/4./dTheta, 0.0, 1.);
210  data.p3d.Set(0., 0., 1., 1.);
211  data.triangulation_result = 0;
212  cases.push_back(data);
213 
214  // behind the camera
215  data.p1.Set(M_PI/2./dTheta, 0.0, 1.);
216  data.p2.Set(M_PI/4./dTheta, 0.0, 1.);
217  data.p3d.Set(0., 0., -1., 1.);
218  data.triangulation_result = TRIANG_RES_BEHIND_CAMERA1;
219  cases.push_back(data);
220 
221 
222 }
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
T * Get(SharedPtr< T > &t)
projection parameters camera parameters which define the mapping between rays in the camera coordinat...
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
pointer with reference count and automatic deletion
Definition: SharedPtr.hh:50
double NormL2() const
Return the L2 norm: sqrt(a^2 + b^2 + c^2 + d^2)
Definition: Vector4.hh:510
int TriangulateProjective(const ProjectionParametersBase *P1, const ProjectionParametersBase *P2, const HomgPoint2D &p1, const HomgPoint2D &p2, HomgPoint3D &point3d)
Triangulation for metric Poses (using C and R)
Class for triangulation of 3Dpoints from 2D matches. Covariance matrix (refering to an uncertainty el...
class Vector4 contains a Vector of dim.
Definition: Vector4.hh:65
virtual void SetQ(const BIAS::Quaternion< double > &Q)
Set orientation from unit quaternion Q.
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
Definition: KMatrix.hh:48
virtual void SetQ(const BIAS::Quaternion< double > &Q)
Set orientation from unit quaternion Q.
virtual void SetK(const KMatrix &K)
sets the internal parameters from a given KMatrix and updates the cached K and its inverse ...
virtual void SetC(const BIAS::Vector3< double > &C)
Set projection center.