Basic Image AlgorithmS Library 2.8.0

TestProjectionParametersSphericalSimple.cpp

00001 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
00002 
00003    Copyright (C) 2003-2009    (see file CONTACT for details)
00004    Multimediale Systeme der Informationsverarbeitung
00005    Institut fuer Informatik
00006    Christian-Albrechts-Universitaet Kiel
00007    
00008    BIAS is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU Lesser General Public License as published by
00010    the Free Software Foundation; either version 2.1 of the License, or
00011    (at your option) any later version.
00012    
00013    BIAS is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU Lesser General Public License for more details.
00017    
00018    You should have received a copy of the GNU Lesser General Public License
00019    along with BIAS; if not, write to the Free Software
00020    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
00021 
00022 
00023 /**
00024    @file TestProjectionParametersSphericalSimple.cpp
00025    @brief Test the spherical projection parameters
00026    @relates ProjectionParametersSphericalSimple
00027    @ingroup g_tests
00028    @author MIP
00029 */
00030 
00031 
00032 #include <Geometry/ProjectionParametersSphericalSimple.hh>
00033 #include <Base/Common/SharedPtr.hh>
00034 #include <Base/Math/Random.hh>
00035 
00036 using namespace BIAS;
00037 using namespace std;
00038 
00039 void TestContent(const ProjectionParametersSphericalSimple *proj,
00040                  const double phiOffs, const double thetaOffs, 
00041                  const double dPhi, const double dTheta, const unsigned width,
00042                  const unsigned height, const RMatrixBase& R,
00043                  const Vector3<double> &C)
00044 {
00045   unsigned pw, ph;
00046   if (proj->GetImageSize(pw, ph)!=0) { BIASABORT; }
00047   if (pw!=width) { BIASABORT; }
00048   if (ph!=height) { BIASABORT; }
00049 
00050   // phi and theta are cyclic, it is only relevant, that the loca coos
00051   // from phi offset and theta offset are identical
00052   Vector3<double> l1, l2;
00053   proj->Angles2Local(proj->GetPhiOffset(), proj->GetThetaOffset(), l1);
00054   proj->Angles2Local(phiOffs, thetaOffs, l2);
00055   for (int i=0; i<3; i++){
00056     if (!Equal(l1[i]-l2[i], 0., 1e-9)) { 
00057       cout << l1<<"\n"<<l2<<"\n"<<l1-l2<<"\n"<<(l1-l2).NormL2()<<endl; 
00058       BIASABORT; 
00059     }
00060   }
00061   if (!Equal(proj->GetDPhi(), dPhi)) { BIASABORT; }
00062   if (!Equal(proj->GetDTheta(), dTheta)) { BIASABORT; }
00063 
00064   RMatrixBase Rp = proj->GetRCache(), id = Rp.Transpose() * R;
00065   if (!id.IsIdentity(1e-10)) { 
00066     cout << "R: "<<R<<"\nRp: "<<Rp<<"\nid: "<<id<<endl; BIASABORT; 
00067   }
00068   Rp = proj->GetR();
00069   id = Rp.Transpose() * R;
00070   if (!id.IsIdentity(1e-10)) { 
00071     cout << "R: "<<R<<"\nRp: "<<Rp<<"\nid: "<<id<<endl; BIASABORT; 
00072   }
00073   Vector3<double> Cp = proj->GetC();
00074   if (!Equal((Cp-C).NormL2(), 0., 1e-13)){
00075     cout << "C: "<<C<<"\nCp: "<<Cp<<"\tdiff: "<< C-Cp<<endl;
00076     BIASABORT;
00077   }
00078 }
00079 
00080 
00081 int main(int argc, char *argv[])
00082 {
00083   // create the proj param object
00084   const double phiOffs = 0.7, thetaOffs = -0.3, dPhi = 0.05, dTheta = 0.025;
00085   const unsigned width = 640, height = 480;
00086   SharedPtr<ProjectionParametersSphericalSimple> proj = 
00087     SharedPtr<ProjectionParametersSphericalSimple>
00088     (new ProjectionParametersSphericalSimple(phiOffs, thetaOffs, dPhi, dTheta,
00089                                              width, height));
00090   RMatrixBase Rtmp;
00091   Rtmp.SetXYZ(1.0, 2.15, -0.7);
00092   const RMatrixBase R(Rtmp); // ensure const R
00093   proj->SetR(R);
00094   const Vector3<double> C(13.0, 24.415, -4.17);
00095   proj->SetC(C);
00096 
00097   // test clone and operator==
00098   SharedPtr<ProjectionParametersSphericalSimple> proj2 = 
00099     SharedPtr<ProjectionParametersSphericalSimple>
00100     (dynamic_cast<ProjectionParametersSphericalSimple *>(proj->Clone()));
00101   TestContent(Get(proj2), phiOffs, thetaOffs, dPhi, dTheta, width, height, R, C);
00102   ProjectionParametersSphericalSimple proj3 = *proj;
00103   TestContent(&proj3, phiOffs, thetaOffs, dPhi, dTheta, width, height, R, C);
00104   ProjectionParametersSphericalSimple proj4(*proj);
00105   TestContent(&proj4, phiOffs, thetaOffs, dPhi, dTheta, width, height, R, C);
00106 
00107 #ifdef BIAS_HAVE_XML
00108   // test io
00109   string tmp;
00110   if (proj->XMLWriteToString(tmp)!=0) { BIASABORT; }
00111   ProjectionParametersSphericalSimple proj5;
00112   if (proj5.XMLReadFromString(tmp)!=0) { BIASABORT; }
00113   proj->Print();
00114   proj5.Print();
00115   TestContent(&proj5, phiOffs, thetaOffs, dPhi, dTheta, width, height, R, C);
00116   const string filename = "TestProjectionParametersSimple.xml";
00117   if (proj->XMLWrite(filename)!=0) { BIASABORT; }
00118   ProjectionParametersSphericalSimple proj6;
00119   if (proj6.XMLRead(filename)!=0) { BIASABORT; }
00120   TestContent(&proj6, phiOffs, thetaOffs, dPhi, dTheta, width, height, R, C);
00121 #endif
00122  
00123   // test project and unproject consistency
00124   Vector3<double> p3d, ref, p;
00125   Random r;
00126   Vector2<double> pixel;
00127   HomgPoint2D p2d;
00128   int num;
00129   const int num_tries = 100000;
00130   for (int n=0; n<num_tries; n++){
00131     // generate random 3d point p3d
00132     num = 0;
00133     do {
00134       p3d.Set(r.GetUniformDistributed(-10.0, 10.0), 
00135               r.GetUniformDistributed(-10.0, 10.0), 
00136               r.GetUniformDistributed(-10.0, 10.0));
00137       num++;
00138     } while (Equal(p3d.NormL2(), 0.) && num<100);
00139     if (Equal(p3d.NormL2(), 0.)) { BIASABORT; }
00140     p3d /= p3d.NormL2();
00141 
00142     // test local project and unproject consistency
00143     p2d = proj->ProjectLocal(p3d);
00144     proj->UnProjectLocal(p2d, p, ref);
00145     if (Equal(ref.NormL2(), 0.)){ BIASABORT; }
00146     ref /= ref.NormL2(); // scale ref for comparison
00147     for (int i=0; i<3; i++){
00148       if (!Equal(ref[i]-p3d[i], 0., 1e-13)) { 
00149         cout << "orig: "<<p3d<<"\nproj/unproj: "<<ref<<endl
00150              <<i<<"\t"<<ref[i]-p3d[i]<<endl;
00151         BIASABORT; 
00152       }
00153     }
00154 
00155     // test project and unproject consistency
00156     p2d = proj->Project(HomgPoint3D(p3d));
00157     double depth = (p3d-C).NormL2();
00158     HomgPoint3D refh = proj->UnProjectToImagePlane(p2d, depth);
00159     ref = refh.GetEuclidean();
00160     if (Equal(ref.NormL2(), 0.)){ BIASABORT; }
00161     ref /= ref.NormL2(); // scale ref for comparison
00162     for (int i=0; i<3; i++){
00163       if (!Equal(ref[i]-p3d[i], 0., 1e-13)) { 
00164         cout <<   "orig[i]    : "<<setprecision(20)<<p3d[i]
00165              << "\nproj/unproj: "<<setprecision(20)<<ref[i]<<endl;
00166         cout <<   "orig       : "<<p3d
00167              << "\nproj/unproj: "<<ref<<endl
00168              <<i<<"\t"<<ref[i]-p3d[i]<<endl;
00169         BIASABORT; 
00170       }
00171     }
00172 
00173     // semilocal
00174     proj->GlobalOrientation2Pixel(p3d, pixel);
00175     proj->Pixel2GlobalOrientation(pixel, ref);
00176     if (Equal(ref.NormL2(), 0.)){ BIASABORT; }
00177     ref /= ref.NormL2(); // scale ref for comparison
00178     for (int i=0; i<3; i++){
00179       if (!Equal(ref[i]-p3d[i], 0., 1e-13)) { 
00180         cout << "orig: "<<p3d<<"\nproj: "<<ref<<endl
00181              <<i<<"\t"<<ref[i]-p3d[i]<<endl;
00182         BIASABORT; 
00183       }
00184     }
00185   }
00186 
00187   return 0;
00188 }
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends