Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestProjectionParametersSphericalSimple.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 TestProjectionParametersSphericalSimple.cpp
25  @brief Test the spherical projection parameters
26  @relates ProjectionParametersSphericalSimple
27  @ingroup g_tests
28  @author MIP
29 */
30 
31 
32 #include <Geometry/ProjectionParametersSphericalSimple.hh>
33 #include <Base/Common/SharedPtr.hh>
34 #include <Base/Math/Random.hh>
35 
36 using namespace BIAS;
37 using namespace std;
38 
39 void TestContent(const ProjectionParametersSphericalSimple *proj,
40  const double phiOffs, const double thetaOffs,
41  const double dPhi, const double dTheta, const unsigned width,
42  const unsigned height, const RMatrixBase& R,
43  const Vector3<double> &C)
44 {
45  unsigned pw, ph;
46  if (proj->GetImageSize(pw, ph)!=0) { BIASABORT; }
47  if (pw!=width) { BIASABORT; }
48  if (ph!=height) { BIASABORT; }
49 
50  // phi and theta are cyclic, it is only relevant, that the loca coos
51  // from phi offset and theta offset are identical
52  Vector3<double> l1, l2;
53  proj->Angles2Local(proj->GetPhiOffset(), proj->GetThetaOffset(), l1);
54  proj->Angles2Local(phiOffs, thetaOffs, l2);
55  for (int i=0; i<3; i++){
56  if (!Equal(l1[i]-l2[i], 0., 1e-9)) {
57  cout << l1<<"\n"<<l2<<"\n"<<l1-l2<<"\n"<<(l1-l2).NormL2()<<endl;
58  BIASABORT;
59  }
60  }
61  if (!Equal(proj->GetDPhi(), dPhi)) { BIASABORT; }
62  if (!Equal(proj->GetDTheta(), dTheta)) { BIASABORT; }
63 
64  RMatrixBase Rp = proj->GetRCache(), id = Rp.Transpose() * R;
65  if (!id.IsIdentity(1e-10)) {
66  cout << "R: "<<R<<"\nRp: "<<Rp<<"\nid: "<<id<<endl; BIASABORT;
67  }
68  Rp = proj->GetR();
69  id = Rp.Transpose() * R;
70  if (!id.IsIdentity(1e-10)) {
71  cout << "R: "<<R<<"\nRp: "<<Rp<<"\nid: "<<id<<endl; BIASABORT;
72  }
73  Vector3<double> Cp = proj->GetC();
74  if (!Equal((Cp-C).NormL2(), 0., 1e-13)){
75  cout << "C: "<<C<<"\nCp: "<<Cp<<"\tdiff: "<< C-Cp<<endl;
76  BIASABORT;
77  }
78 }
79 
80 
81 int main(int argc, char *argv[])
82 {
83  // create the proj param object
84  const double phiOffs = 0.7, thetaOffs = -0.3, dPhi = 0.05, dTheta = 0.025;
85  const unsigned width = 640, height = 480;
88  (new ProjectionParametersSphericalSimple(phiOffs, thetaOffs, dPhi, dTheta,
89  width, height));
90  RMatrixBase Rtmp;
91  Rtmp.SetXYZ(1.0, 2.15, -0.7);
92  const RMatrixBase R(Rtmp); // ensure const R
93  proj->SetR(R);
94  const Vector3<double> C(13.0, 24.415, -4.17);
95  proj->SetC(C);
96 
97  // test clone and operator==
100  (dynamic_cast<ProjectionParametersSphericalSimple *>(proj->Clone()));
101  TestContent(Get(proj2), phiOffs, thetaOffs, dPhi, dTheta, width, height, R, C);
103  TestContent(&proj3, phiOffs, thetaOffs, dPhi, dTheta, width, height, R, C);
105  TestContent(&proj4, phiOffs, thetaOffs, dPhi, dTheta, width, height, R, C);
106 
107 #ifdef BIAS_HAVE_XML
108  // test io
109  string tmp;
110  if (proj->XMLWriteToString(tmp)!=0) { BIASABORT; }
112  if (proj5.XMLReadFromString(tmp)!=0) { BIASABORT; }
113  proj->Print();
114  proj5.Print();
115  TestContent(&proj5, phiOffs, thetaOffs, dPhi, dTheta, width, height, R, C);
116  const string filename = "TestProjectionParametersSimple.xml";
117  if (proj->XMLWrite(filename)!=0) { BIASABORT; }
119  if (proj6.XMLRead(filename)!=0) { BIASABORT; }
120  TestContent(&proj6, phiOffs, thetaOffs, dPhi, dTheta, width, height, R, C);
121 #endif
122 
123  // test project and unproject consistency
124  Vector3<double> p3d, ref, p;
125  Random r;
126  Vector2<double> pixel;
127  HomgPoint2D p2d;
128  int num;
129  const int num_tries = 100000;
130  for (int n=0; n<num_tries; n++){
131  // generate random 3d point p3d
132  num = 0;
133  do {
134  p3d.Set(r.GetUniformDistributed(-10.0, 10.0),
135  r.GetUniformDistributed(-10.0, 10.0),
136  r.GetUniformDistributed(-10.0, 10.0));
137  num++;
138  } while (Equal(p3d.NormL2(), 0.) && num<100);
139  if (Equal(p3d.NormL2(), 0.)) { BIASABORT; }
140  p3d /= p3d.NormL2();
141 
142  // test local project and unproject consistency
143  p2d = proj->ProjectLocal(p3d);
144  proj->UnProjectLocal(p2d, p, ref);
145  if (Equal(ref.NormL2(), 0.)){ BIASABORT; }
146  ref /= ref.NormL2(); // scale ref for comparison
147  for (int i=0; i<3; i++){
148  if (!Equal(ref[i]-p3d[i], 0., 1e-13)) {
149  cout << "orig: "<<p3d<<"\nproj/unproj: "<<ref<<endl
150  <<i<<"\t"<<ref[i]-p3d[i]<<endl;
151  BIASABORT;
152  }
153  }
154 
155  // test project and unproject consistency
156  p2d = proj->Project(HomgPoint3D(p3d));
157  double depth = (p3d-C).NormL2();
158  HomgPoint3D refh = proj->UnProjectToImagePlane(p2d, depth);
159  ref = refh.GetEuclidean();
160  if (Equal(ref.NormL2(), 0.)){ BIASABORT; }
161  ref /= ref.NormL2(); // scale ref for comparison
162  for (int i=0; i<3; i++){
163  if (!Equal(ref[i]-p3d[i], 0., 1e-13)) {
164  cout << "orig[i] : "<<setprecision(20)<<p3d[i]
165  << "\nproj/unproj: "<<setprecision(20)<<ref[i]<<endl;
166  cout << "orig : "<<p3d
167  << "\nproj/unproj: "<<ref<<endl
168  <<i<<"\t"<<ref[i]-p3d[i]<<endl;
169  BIASABORT;
170  }
171  }
172 
173  // semilocal
174  proj->GlobalOrientation2Pixel(p3d, pixel);
175  proj->Pixel2GlobalOrientation(pixel, ref);
176  if (Equal(ref.NormL2(), 0.)){ BIASABORT; }
177  ref /= ref.NormL2(); // scale ref for comparison
178  for (int i=0; i<3; i++){
179  if (!Equal(ref[i]-p3d[i], 0., 1e-13)) {
180  cout << "orig: "<<p3d<<"\nproj: "<<ref<<endl
181  <<i<<"\t"<<ref[i]-p3d[i]<<endl;
182  BIASABORT;
183  }
184  }
185  }
186 
187  return 0;
188 }
virtual BIAS::Vector3< double > GetC() const
Get projection center.
virtual double GetThetaOffset() const
position of image on sphere in theta direction
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:522
virtual double GetDPhi() const
set resolution in radian/pixel for phi direction
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
T * Get(SharedPtr< T > &t)
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
projection parameters camera parameters which define the mapping between rays in the camera coordinat...
pointer with reference count and automatic deletion
Definition: SharedPtr.hh:50
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
void Angles2Local(const double &phi, const double &theta, Vector3< double > &local) const
Transform local camera centric coos to sphere angles.
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 ...
virtual void SetR(const BIAS::RMatrix &R)
Set orientation from rotation matrix R.
void GlobalOrientation2Pixel(const Vector3< double > &p2d, Vector2< double > &pixel) const
Transform from a semilocal reference frame to pixel coo.
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 ProjectionParametersBase * Clone() const
Covariant virtual copy constructor used in BIAS::Projection.
virtual BIAS::RMatrix GetR() const
Get orientation as rotation matrix R.
virtual double GetDTheta() const
set resolution in radian/pixel for theta direction
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
void Pixel2GlobalOrientation(const Vector2< double > &pixel, Vector3< double > &p2d) const
Transform from pixel coo to semilocal reference frame.
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_...
Matrix3x3< T > Transpose() const
returns transposed matrix tested 12.06.2002
Definition: Matrix3x3.cpp:167
virtual void UnProjectLocal(const HomgPoint2D &pos, Vector3< double > &pointOnRay, Vector3< double > &direction, bool IgnoreDistortion=false) const
calculates the unit size viewing ray from the camera center (in the camera coordinate system) which b...
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...
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
virtual double GetPhiOffset() const
position of image on sphere in phi direction can also considered to be rotation of the principal poin...
virtual void SetC(const BIAS::Vector3< double > &C)
Set projection center.
class for producing random numbers from different distributions
Definition: Random.hh:51
virtual 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.
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Definition: Vector3.hh:623
class BIASGeometryBase_EXPORT HomgPoint3D