core/vpgl/algo/vpgl_invmap_cost_function.cxx
Go to the documentation of this file.
00001 #include "vpgl_invmap_cost_function.h"
00002 //:
00003 // \file
00004 #include <vcl_cmath.h>
00005 #include <vcl_iostream.h>
00006 #include <vcl_deprecated.h>
00007 
00008 vpgl_invmap_cost_function::
00009 vpgl_invmap_cost_function(vnl_vector_fixed<double, 2> const& image_point,
00010                           vnl_vector_fixed<double, 4> const& plane,
00011                           const vpgl_camera<double>* const cam)
00012  : vnl_cost_function(2), image_point_(image_point),
00013    plane_(plane), cam_ptr_(cam), pp_(X_Y)
00014 {
00015   //determine which parameterization of the plane to use
00016   // order the plane normals
00017   double anx = vcl_fabs(plane_[0]), any = vcl_fabs(plane_[1]),
00018     anz = vcl_fabs(plane_[2]);
00019   if (anx<any && anz<any)
00020     { pp_ = X_Z; return;}
00021   if (any<anx && anz<anx)
00022      pp_ = Y_Z;
00023 }
00024 
00025 //: The main function.
00026 double vpgl_invmap_cost_function::f(vnl_vector<double> const& x)
00027 {
00028   if (!cam_ptr_)
00029     return 0;
00030   // fill out the 3-d point from the parameters
00031   vnl_vector_fixed<double, 3> p_3d;
00032   this->point_3d(vnl_vector_fixed<double,2>(x[0],x[1]), p_3d);
00033 
00034   // project the current point estimate onto the image
00035   double u,v,X=p_3d[0],Y=p_3d[1],Z=p_3d[2];
00036   cam_ptr_->project(X,Y,Z,u,v);
00037 vnl_vector_fixed<double, 2> p_2d;
00038  p_2d[0]=u; p_2d[1]=v;
00039   // compute the residual
00040   double resid = (image_point_[0] - p_2d[0])*(image_point_[0] - p_2d[0]);
00041   resid += (image_point_[1] - p_2d[1])*(image_point_[1] - p_2d[1]);
00042   return resid;
00043 }
00044 
00045 void vpgl_invmap_cost_function::
00046 set_params(vnl_vector_fixed<double,3> const& xyz, vnl_vector_fixed<double,2> &x)
00047 {
00048   switch (pp_)
00049   {
00050     case X_Y:
00051     {
00052       x[0] = xyz[0];
00053       x[1] = xyz[1];
00054       break;
00055     }
00056     case X_Z:
00057     {
00058       x[0] = xyz[0];
00059       x[1] = xyz[2];
00060       break;
00061     }
00062     case Y_Z:
00063     {
00064       x[0] = xyz[1];
00065       x[1] = xyz[2];
00066       break;
00067     }
00068     default:
00069     {
00070       x[0] = 0; x[1] = 0;
00071       vcl_cerr << "Improper prameterization in vpgl_invmap_cost_function\n";
00072     }
00073   }
00074 }
00075 
00076 void vpgl_invmap_cost_function::
00077 set_params(vnl_vector_fixed<double, 3> const& xyz, vnl_vector<double> &x)
00078 {
00079   VXL_DEPRECATED("vpgl_invmap_cost_function::set_params(, vnl_vector<double>&)");
00080   switch (pp_)
00081   {
00082     case X_Y:
00083     {
00084       x[0] = xyz[0];
00085       x[1] = xyz[1];
00086       break;
00087     }
00088     case X_Z:
00089     {
00090       x[0] = xyz[0];
00091       x[1] = xyz[2];
00092       break;
00093     }
00094     case Y_Z:
00095     {
00096       x[0] = xyz[1];
00097       x[1] = xyz[2];
00098       break;
00099     }
00100     default:
00101     {
00102       x[0] = 0; x[1] = 0;
00103       vcl_cerr << "Improper prameterization in vpgl_invmap_cost_function\n";
00104     }
00105   }
00106 }
00107 
00108 void vpgl_invmap_cost_function::
00109 point_3d(vnl_vector_fixed<double,2> const& x, vnl_vector_fixed<double,3>& xyz)
00110 {
00111   //Switch on plane parameterization
00112   switch (pp_)
00113   {
00114     case X_Y:
00115     {
00116       xyz[0] = x[0];
00117       xyz[1] = x[1];
00118       xyz[2] = -(plane_[0]*x[0] + plane_[1]*x[1] + plane_[3])/plane_[2];
00119       break;
00120     }
00121     case X_Z:
00122     {
00123       xyz[0] = x[0];
00124       xyz[2] = x[1];
00125       xyz[1] = -(plane_[0]*x[0] + plane_[2]*x[1] + plane_[3])/plane_[1];
00126       break;
00127     }
00128     case Y_Z:
00129     {
00130       xyz[1] = x[0];
00131       xyz[2] = x[1];
00132       xyz[0] = -(plane_[1]*x[0] + plane_[2]*x[1] + plane_[3])/plane_[0];
00133       break;
00134     }
00135     default:
00136     {
00137       xyz[0] = 0; xyz[1] = 0; xyz[2] = 0;
00138       vcl_cerr << "Improper prameterization in vpgl_invmap_cost_function\n";
00139     }
00140   }
00141 }
00142 
00143 void vpgl_invmap_cost_function::
00144 point_3d(vnl_vector<double> const& x, vnl_vector_fixed<double, 3>& xyz)
00145 {
00146   VXL_DEPRECATED("vpgl_invmap_cost_function::point_3d(vnl_vector<double>,)");
00147   //Switch on plane parameterization
00148   switch (pp_)
00149   {
00150     case X_Y:
00151     {
00152       xyz[0] = x[0];
00153       xyz[1] = x[1];
00154       xyz[2] = -(plane_[0]*x[0] + plane_[1]*x[1] + plane_[3])/plane_[2];
00155       break;
00156     }
00157     case X_Z:
00158     {
00159       xyz[0] = x[0];
00160       xyz[2] = x[1];
00161       xyz[1] = -(plane_[0]*x[0] + plane_[2]*x[1] + plane_[3])/plane_[1];
00162       break;
00163     }
00164     case Y_Z:
00165     {
00166       xyz[1] = x[0];
00167       xyz[2] = x[1];
00168       xyz[0] = -(plane_[1]*x[0] + plane_[2]*x[1] + plane_[3])/plane_[0];
00169       break;
00170     }
00171     default:
00172     {
00173       xyz[0] = 0; xyz[1] = 0; xyz[2] = 0;
00174       vcl_cerr << "Improper prameterization in vpgl_invmap_cost_function\n";
00175     }
00176   }
00177 }
00178