Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestProjectionParametersSphericalFast.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  @file TestProjectionParametersSphericalSimpleFast.cpp
25  @brief Test the spherical projection parameters
26  @relates ProjectionParametersSphericalFast
27  @ingroup g_tests
28  @author MIP
29 */
30 
31 
32 #include <Geometry/ProjectionParametersSphericalFast.hh>
33 #include <Base/Common/SharedPtr.hh>
34 #include <Base/Math/Random.hh>
35 
36 using namespace BIAS;
37 using namespace std;
38 
39 int main(int argc, char *argv[]) {
40 
41  // create the proj param object
42  const unsigned width = 640, height = 480;
44  new ProjectionParametersSphericalFast(600, 75.0/180.0*M_PI, width, height);
45 
46  RMatrixBase Rtmp;
47  Rtmp.SetXYZ(1.0, 2.15, -0.7);
48  const RMatrixBase R(Rtmp); // ensure const R
49  proj->SetR(R);
50  const Vector3<double> C(13.0, 24.415, -4.17);
51  proj->SetC(C);
52 
53  vector<double> coeffsDist(5, 0.0);
54  coeffsDist[0] = 1.01;
55  coeffsDist[1] = -0.025;
56  coeffsDist[2] = 0.02;
57  proj->InitPolyCoeffs(75.0/180.0*M_PI, coeffsDist, false);
58 
60 
61 
62  cout << "projection " << *proj << endl;
63 
64  // test clone and operator=
65  cout << "testing clone and = operator " << endl;
67  (dynamic_cast<ProjectionParametersSphericalFast*>(proj->Clone()));
68  if(proj->DoIntrinsicsDiffer(proj2)){
69  BIASERR("clone operator does not work" << endl);
70  return -1;
71  }
72  cout << "clone " << *proj2 << endl;
74  if(proj->DoIntrinsicsDiffer(&proj3)){
75  BIASERR("= operator does not work" << endl);
76  return -1;
77  }
78  cout << "= " << proj3 << endl;
79 
80 
81 #ifdef BIAS_HAVE_XML2
82  // test io
83 
84  const string filename = "TestProjectionParametersSimple.xml";
85  if (proj->XMLWrite(filename)!=0) { BIASABORT; }
87  if (proj6.XMLRead(filename)!=0) { BIASABORT; }
88  cout << "proj " << *proj << endl;
89  cout << "proj6 " << proj6 << endl;
90  if(proj->DoIntrinsicsDiffer(&proj6)){
91  BIASERR("xml input/output does not work" << endl);
92  return -1;
93  }
94 
95  string tmp;
96  if (proj->XMLWriteToString(tmp)!=0) { BIASABORT; }
98  if (proj5.XMLReadFromString(tmp)!=0) { BIASABORT; }
99  cout << "proj " << *proj << endl;
100  cout << "proj5 " << proj5 << endl;
101  if(proj->DoIntrinsicsDiffer(&proj5)){
102  BIASERR("xml streaming does not work" << endl);
103  return -1;
104  }
105 #endif
106 
107  cout << "proj used for project-unproject test " << *proj << endl;
108  proj->TestPolynomialInversion();
109  Vector3<double> p3dtmp(-15,-10,100);
110  HomgPoint2D p2dtmp = proj->ProjectLocal(p3dtmp);
111  cout << "p3d " << p3dtmp << " p2d " << p2dtmp << endl;
112  Vector3<double> pos, dir;
113  proj->UnProjectLocal(p2dtmp, pos, dir);
114  double lambda = (p3dtmp[0]-pos[0])/dir[0];
115  double lambda2 = (p3dtmp[1]-pos[1])/dir[1];
116  double lambda3 = (p3dtmp[2]-pos[2])/dir[2];
117  cout << "lambda " << lambda << " " << lambda2 << " " << lambda3 << endl;
118  cout << "unprojected " << pos << " " << dir << " point " << pos + lambda3 * dir << endl;
119 
120  //BIASABORT;
121 
122  // test project and unproject consistency
123  Vector3<double> p3d, ref, p;
124  Random r;
125  HomgPoint2D p2d;
126  int num;
127  const int num_tries = 1e4;
128  double minAcc = 1e-4;
129  for (int n=0; n<num_tries; n++){
130  // generate random 3d point p3d
131  num = 0;
132  do {
133  p3d.Set(r.GetUniformDistributed(-10.0, 10.0),
134  r.GetUniformDistributed(-10.0, 10.0),
135  r.GetUniformDistributed(-10.0, 10.0));
136  num++;
137  } while (Equal(p3d.NormL2(), 0.) && num<100);
138  if (Equal(p3d.NormL2(), 0.)) { BIASABORT; }
139  Vector3<double> p3dorig = p3d;
140  p3d /= p3d.NormL2();
141 
142  // test local project and unproject consistency
143  p2d = proj->ProjectLocal(p3d);
144  // cout << "p2d " << p2d << " p3d " << p3d << " p3d orig " << p3dorig << endl;
145  if(p2d[0] == 0 && p2d[1] == 0 && p2d[2] == 0){
146  // cout << "point outside image" << endl;
147  } else {
148  proj->UnProjectLocal(p2d, p, ref);
149  if (Equal(ref.NormL2(), 0.)){ BIASABORT; }
150  ref /= ref.NormL2(); // scale ref for comparison
151  double error = (ref-p3d).NormL2();
152  if(error > minAcc){
153  cout << "high error orig : " << p3d << " proj/unproj " << ref << " error " << error << endl;
154  }
155  }
156 
157  // test project and unproject consistency
158  p2d = proj->Project(HomgPoint3D(p3d));
159  if(p2d[0] == 0 && p2d[1] == 0 && p2d[2] == 0){
160  // cout << "point outside image" << endl;
161  } else {
162  double depth = (p3d-C).NormL2();
163  HomgPoint3D refh = proj->UnProjectToImagePlane(p2d, depth);
164  ref = refh.GetEuclidean();
165  if (Equal(ref.NormL2(), 0.)){ BIASABORT; }
166  ref /= ref.NormL2(); // scale ref for comparison
167  double error = (ref-p3d).NormL2();
168  if(error > minAcc){
169  cout << "high error in second part orig " << p3d << " proj/unproj " << ref << " error " << error << endl;
170  }
171  }
172 
173 
174  }
175 
176  return 0;
177 }
int XMLReadFromString(const std::string &str)
reconstruct xml tree from string
Definition: XMLBase.cpp:111
void GetEuclidean(Vector3< HOMGPOINT3D_TYPE > &dest) const
calculate affine coordinates of this and write them to dest affine coordinates are projective coordin...
Definition: HomgPoint3D.hh:336
void Set(const T *pv)
copy the array of vectorsize beginning at *T to this-&gt;data_
Definition: Vector3.hh:532
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
void SetXYZ(ROTATION_MATRIX_TYPE PhiX, ROTATION_MATRIX_TYPE PhiY, ROTATION_MATRIX_TYPE PhiZ)
Set Euler angles (in rad) in order XYZ.
int XMLRead(const std::string &Filename)
derived classes must implement the function XMLIn which is called by this function XMLRead to read ev...
Definition: XMLBase.cpp:78
spherical camera that uses polynomial only and should therefore be faster than other spherical camera...
virtual void SetR(const BIAS::RMatrix &R)
Set orientation from rotation matrix R.
HomgPoint3D UnProjectToImagePlane(const HomgPoint2D &pos, const double &depth=1.0, bool IgnoreDistortion=false) const
map points from image onto unit diameter image plane in 3D.
virtual void UnProjectLocal(const HomgPoint2D &pos, Vector3< double > &origin, Vector3< double > &direction, bool IgnoreDistortion=false) const
calculates the viewing ray from the camera center (in the camera coordinate system) which belongs to ...
int XMLWriteToString(std::string &str, std::string encoding="UTF-8") const
serialize xml tree to string
Definition: XMLBase.cpp:61
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
virtual HomgPoint2D Project(const HomgPoint3D &X, bool IgnoreDistortion=false) const
calculates the projection of a point in the world coordinate system to a pixel in the image plane of ...
int InitPolyCoeffs(const double maxCamAngle, const std::vector< double > &coefficients, const bool undistCoeffs=true)
Set undistortion polynomial from from polynomial coefficients of undistortion (mapping radius to z co...
virtual HomgPoint2D ProjectLocal(const Vector3< double > &point, bool IgnoreDistortion=false) const
calculates the projection of a point in the local camera coordinate system to a pixel in the image pl...
int XMLWrite(const std::string &Filename, int CompressionLevel=0, bool AutoAddCompressionSuffix=true, std::string encoding="UTF-8") const
call this to add the class to a new xml tree and write it to the file Filename.
Definition: XMLBase.cpp:40
virtual ProjectionParametersSphericalFast * Clone() const
covariant virtual copy constructor for use in Projection
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_...
virtual const bool DoIntrinsicsDiffer(const ProjectionParametersBase *p) const
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
virtual void SetC(const BIAS::Vector3< double > &C)
Set projection center.
class for producing random numbers from different distributions
Definition: Random.hh:51
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Definition: Vector3.hh:633
class BIASGeometryBase_EXPORT HomgPoint3D