USGS

Isis 3.0 Object Programmers' Reference

Home

CameraDistortionMap.cpp
Go to the documentation of this file.
1 
23 #include "IString.h"
24 #include "CameraDistortionMap.h"
25 
26 namespace Isis {
39  CameraDistortionMap::CameraDistortionMap(Camera *parent, double zDirection) {
40  p_camera = parent;
41  p_camera->SetDistortionMap(this);
42  p_zDirection = zDirection;
43  }
44 
45 
65  void CameraDistortionMap::SetDistortion(int naifIkCode) {
66  QString odkkey = "INS" + toString(naifIkCode) + "_OD_K";
67  for (int i = 0; i < 3; ++i) {
68  p_odk.push_back(p_camera->Spice::getDouble(odkkey, i));
69  }
70  }
71 
72 
89  bool CameraDistortionMap::SetFocalPlane(double dx, double dy) {
90  p_focalPlaneX = dx;
91  p_focalPlaneY = dy;
92 
93  // No coefficients == no distortion
94  if (p_odk.size() <= 0) {
95  p_undistortedFocalPlaneX = dx;
96  p_undistortedFocalPlaneY = dy;
97  return true;
98  }
99 
100  // Get the distance from the focal plane center and if we are close
101  // then skip the distortion
102  double r2 = (dx * dx) + (dy * dy);
103  if (r2 <= 1.0E-6) {
104  p_undistortedFocalPlaneX = dx;
105  p_undistortedFocalPlaneY = dy;
106  return true;
107  }
108 
109  // Ok we need to apply distortion correction
110  double drOverR = p_odk[0] + (r2 * (p_odk[1] + (r2 * p_odk[2])));
111  p_undistortedFocalPlaneX = dx - (drOverR * dx);
112  p_undistortedFocalPlaneY = dy - (drOverR * dy);
113  return true;
114  }
115 
116 
136  const double uy) {
137  p_undistortedFocalPlaneX = ux;
138  p_undistortedFocalPlaneY = uy;
139 
140  // No coefficients == nodistortion
141  if (p_odk.size() <= 0) {
142  p_focalPlaneX = ux;
143  p_focalPlaneY = uy;
144  return true;
145  }
146 
147  // Compute the distance from the focal plane center and if we are
148  // close to the center then no distortion is required
149  double rp2 = (ux * ux) + (uy * uy);
150  if (rp2 <= 1.0E-6) {
151  p_focalPlaneX = ux;
152  p_focalPlaneY = uy;
153  return true;
154  }
155 
156  // Ok make the correction, start by computing
157  // fractional distortion at rp (r-prime)
158  double rp = sqrt(rp2);
159  double drOverR = p_odk[0] + (rp2 * (p_odk[1] + (rp2 * p_odk[2])));
160 
161  // Estimate r
162  double r = rp + (drOverR * rp);
163  double r_prev, r2_prev;
164  double tolMilliMeters = p_camera->PixelPitch() / 100.0;
165  int iteration = 0;
166  do {
167  // Don't get in an end-less loop. This algorithm should
168  // converge quickly. If not then we are probably way outside
169  // of the focal plane. Just set the distorted position to the
170  // undistorted position. Also, make sure the focal plane is less
171  // than 1km, it is unreasonable for it to grow larger than that.
172  if (iteration >= 15 || r > 1E9) {
173  drOverR = 0.0;
174  break;
175  }
176 
177  r_prev = r;
178  r2_prev = r * r;
179 
180  // Compute new fractional distortion:
181  drOverR = p_odk[0] + (r2_prev * (p_odk[1] + (r2_prev * p_odk[2])));
182 
183  r = rp + (drOverR * r_prev); // Compute new estimate of r
184  iteration++;
185  }
186  while (fabs(r - r_prev) > tolMilliMeters);
187 
188  p_focalPlaneX = ux / (1.0 - drOverR);
189  p_focalPlaneY = uy / (1.0 - drOverR);
190  return true;
191  }
192 
193 
196  return p_odk;
197  }
198 
199 
202  return p_zDirection;
203  }
204 
205 }
206