vpgl_invmap_cost_function.cxx
Go to the documentation of this file.
1 #include <cmath>
2 #include <iostream>
4 //:
5 // \file
6 #include <vcl_compiler_detection.h>
7 #ifdef _MSC_VER
8 # include <vcl_msvc_warnings.h>
9 #endif
10 #include <vcl_deprecated.h>
11 
14  vnl_vector_fixed<double, 4> const& plane,
15  const vpgl_camera<double>* const cam)
16  : vnl_cost_function(2), image_point_(image_point),
17  plane_(plane), cam_ptr_(cam), pp_(X_Y)
18 {
19  //determine which parameterization of the plane to use
20  // order the plane normals
21  double anx = std::fabs(plane_[0]), any = std::fabs(plane_[1]),
22  anz = std::fabs(plane_[2]);
23  if (anx<any && anz<any)
24  { pp_ = X_Z; return;}
25  if (any<anx && anz<anx)
26  pp_ = Y_Z;
27 }
28 
29 //: The main function.
31 {
32  if (!cam_ptr_)
33  return 0;
34  // fill out the 3-d point from the parameters
36  this->point_3d(vnl_vector_fixed<double,2>(x[0],x[1]), p_3d);
37 
38  // project the current point estimate onto the image
39  double u,v,X=p_3d[0],Y=p_3d[1],Z=p_3d[2];
40  cam_ptr_->project(X,Y,Z,u,v);
42  p_2d[0]=u; p_2d[1]=v;
43  // compute the residual
44  double resid = (image_point_[0] - p_2d[0])*(image_point_[0] - p_2d[0]);
45  resid += (image_point_[1] - p_2d[1])*(image_point_[1] - p_2d[1]);
46  return resid;
47 }
48 
51 {
52  switch (pp_)
53  {
54  case X_Y:
55  {
56  x[0] = xyz[0];
57  x[1] = xyz[1];
58  break;
59  }
60  case X_Z:
61  {
62  x[0] = xyz[0];
63  x[1] = xyz[2];
64  break;
65  }
66  case Y_Z:
67  {
68  x[0] = xyz[1];
69  x[1] = xyz[2];
70  break;
71  }
72  default:
73  {
74  x[0] = 0; x[1] = 0;
75  std::cerr << "Improper prameterization in vpgl_invmap_cost_function\n";
76  }
77  }
78 }
79 
82 {
83  VXL_DEPRECATED_MACRO("vpgl_invmap_cost_function::set_params(, vnl_vector<double>&)");
84  switch (pp_)
85  {
86  case X_Y:
87  {
88  x[0] = xyz[0];
89  x[1] = xyz[1];
90  break;
91  }
92  case X_Z:
93  {
94  x[0] = xyz[0];
95  x[1] = xyz[2];
96  break;
97  }
98  case Y_Z:
99  {
100  x[0] = xyz[1];
101  x[1] = xyz[2];
102  break;
103  }
104  default:
105  {
106  x[0] = 0; x[1] = 0;
107  std::cerr << "Improper prameterization in vpgl_invmap_cost_function\n";
108  }
109  }
110 }
111 
114 {
115  //Switch on plane parameterization
116  switch (pp_)
117  {
118  case X_Y:
119  {
120  xyz[0] = x[0];
121  xyz[1] = x[1];
122  xyz[2] = -(plane_[0]*x[0] + plane_[1]*x[1] + plane_[3])/plane_[2];
123  break;
124  }
125  case X_Z:
126  {
127  xyz[0] = x[0];
128  xyz[2] = x[1];
129  xyz[1] = -(plane_[0]*x[0] + plane_[2]*x[1] + plane_[3])/plane_[1];
130  break;
131  }
132  case Y_Z:
133  {
134  xyz[1] = x[0];
135  xyz[2] = x[1];
136  xyz[0] = -(plane_[1]*x[0] + plane_[2]*x[1] + plane_[3])/plane_[0];
137  break;
138  }
139  default:
140  {
141  xyz[0] = 0; xyz[1] = 0; xyz[2] = 0;
142  std::cerr << "Improper prameterization in vpgl_invmap_cost_function\n";
143  }
144  }
145 }
146 
149 {
150  VXL_DEPRECATED_MACRO("vpgl_invmap_cost_function::point_3d(vnl_vector<double>,)");
151  //Switch on plane parameterization
152  switch (pp_)
153  {
154  case X_Y:
155  {
156  xyz[0] = x[0];
157  xyz[1] = x[1];
158  xyz[2] = -(plane_[0]*x[0] + plane_[1]*x[1] + plane_[3])/plane_[2];
159  break;
160  }
161  case X_Z:
162  {
163  xyz[0] = x[0];
164  xyz[2] = x[1];
165  xyz[1] = -(plane_[0]*x[0] + plane_[2]*x[1] + plane_[3])/plane_[1];
166  break;
167  }
168  case Y_Z:
169  {
170  xyz[1] = x[0];
171  xyz[2] = x[1];
172  xyz[0] = -(plane_[1]*x[0] + plane_[2]*x[1] + plane_[3])/plane_[0];
173  break;
174  }
175  default:
176  {
177  xyz[0] = 0; xyz[1] = 0; xyz[2] = 0;
178  std::cerr << "Improper prameterization in vpgl_invmap_cost_function\n";
179  }
180  }
181 }
void set_params(vnl_vector_fixed< double, 3 > const &xyz, vnl_vector_fixed< double, 2 > &x)
set the parameter values from the 3-d point.
void point_3d(vnl_vector_fixed< double, 2 > const &x, vnl_vector_fixed< double, 3 > &xyz)
get the 3-d point defined by the parameters (and the plane).
vnl_vector_fixed< double, 4 > plane_
plane coefficients.
#define v
const vpgl_camera< double > * cam_ptr_
rational camera.
double f(vnl_vector< double > const &x) override
The cost function. x is a vector holding the two plane parameters.
vpgl_invmap_cost_function(vnl_vector_fixed< double, 2 > const &image_point, vnl_vector_fixed< double, 4 > const &plane, const vpgl_camera< double > *rcam)
Constructor - rcam pointer is not deleted by this class.
plane_param pp_
the well-conditioned parameterization.
virtual void project(const T x, const T y, const T z, T &u, T &v) const =0
The generic camera interface. u represents image column, v image row.
vnl_vector_fixed< double, 2 > image_point_
image point.