Isis 3 Programmer Reference
TaylorCameraDistortionMap.cpp
1 
7 /* SPDX-License-Identifier: CC0-1.0 */
8 
9 #include <cmath>
10 #include "IString.h"
11 #include "TaylorCameraDistortionMap.h"
12 
13 using namespace std;
14 namespace Isis {
15 
29  TaylorCameraDistortionMap::TaylorCameraDistortionMap(Camera *parent, double zDirection):
30  CameraDistortionMap(parent, zDirection) {
31  }
32 
63  void TaylorCameraDistortionMap::SetDistortion(const int naifIkCode) {
64  QString odtxkey = "INS" + toString(naifIkCode) + "_OD_T_X";
65  QString odtykey = "INS" + toString(naifIkCode) + "_OD_T_Y";
66  for(int i = 0; i < 10; ++i) {
67  p_odtx.push_back(p_camera->getDouble(odtxkey, i));
68  p_odty.push_back(p_camera->getDouble(odtykey, i));
69  }
70  }
71 
92  bool TaylorCameraDistortionMap::SetFocalPlane(const double dx, const double dy) {
93  p_focalPlaneX = dx;
94  p_focalPlaneY = dy;
95 
96  // No coefficients == no distortion
97  if(p_odtx.size() <= 0 && p_odty.size() <= 0) {
100  return true;
101  }
102 
103  // Solve the distortion equation using the Newton-Raphson method.
104  // Set the error tolerance to about one millionth of a NAC pixel.
105  const double tol = 1.4E-5;
106 
107  // The maximum number of iterations of the Newton-Raphson method.
108  const int maxTries = 20;
109 
110  double x;
111  double y;
112  double fx;
113  double fy;
114  double Jxx;
115  double Jxy;
116  double Jyx;
117  double Jyy;
118 
119  // Initial guess at the root
120  x = dx;
121  y = dy;
122 
123  this->DistortionFunction(x, y, &fx, &fy);
124 
125  for(int count = 1; ((fabs(fx) + fabs(fy)) > tol) && (count < maxTries); count++) {
126 
127  this->DistortionFunction(x, y, &fx, &fy);
128 
129  fx = dx - fx;
130  fy = dy - fy;
131 
132  this->DistortionFunctionJacobian(x, y, &Jxx, &Jxy, &Jyx, &Jyy);
133 
134  double determinant = Jxx * Jyy - Jxy * Jyx;
135  if(determinant < 1E-6) {
136  //
137  // Near-zero determinant. Add error handling here.
138  //
139  //-- Just break out and return with no convergence
140  break;
141  }
142 
143  x = x + (Jyy * fx - Jxy * fy) / determinant;
144  y = y + (Jxx * fy - Jyx * fx) / determinant;
145  }
146 
147  if((fabs(fx) + fabs(fy)) <= tol) {
148  // The method converged to a root.
151  }
152  else {
153  // The method did not converge to a root within the maximum
154  // number of iterations. Return with no distortion.
157  }
158 
159  return true;
160  }
161 
178  const double uy) {
181 
182  // No coefficients == nodistortion
183  if(p_odtx.size() <= 0 && p_odty.size() <= 0) {
184  p_focalPlaneX = ux;
185  p_focalPlaneY = uy;
186  return true;
187  }
188 
190 
191  return true;
192  }
193 
203  void TaylorCameraDistortionMap::DistortionFunction(double ux, double uy, double *dx, double *dy) {
204 
205  double f[10];
206  f[0] = 1;
207  f[1] = ux;
208  f[2] = uy;
209  f[3] = ux * ux;
210  f[4] = ux * uy;
211  f[5] = uy * uy;
212  f[6] = ux * ux * ux;
213  f[7] = ux * ux * uy;
214  f[8] = ux * uy * uy;
215  f[9] = uy * uy * uy;
216 
217  *dx = 0.0;
218  *dy = 0.0;
219 
220  for(int i = 0; i < 10; i++) {
221  *dx = *dx + f[i] * p_odtx[i];
222  *dy = *dy + f[i] * p_odty[i];
223  }
224 
225  }
226 
239  void TaylorCameraDistortionMap::DistortionFunctionJacobian(double x, double y, double *Jxx, double *Jxy, double *Jyx, double *Jyy) {
240 
241  double d_dx[10];
242  d_dx[0] = 0;
243  d_dx[1] = 1;
244  d_dx[2] = 0;
245  d_dx[3] = 2 * x;
246  d_dx[4] = y;
247  d_dx[5] = 0;
248  d_dx[6] = 3 * x * x;
249  d_dx[7] = 2 * x * y;
250  d_dx[8] = y * y;
251  d_dx[9] = 0;
252  double d_dy[10];
253  d_dy[0] = 0;
254  d_dy[1] = 0;
255  d_dy[2] = 1;
256  d_dy[3] = 0;
257  d_dy[4] = x;
258  d_dy[5] = 2 * y;
259  d_dy[6] = 0;
260  d_dy[7] = x * x;
261  d_dy[8] = 2 * x * y;
262  d_dy[9] = 3 * y * y;
263 
264  *Jxx = 0.0;
265  *Jxy = 0.0;
266  *Jyx = 0.0;
267  *Jyy = 0.0;
268 
269  for(int i = 0; i < 10; i++) {
270  *Jxx = *Jxx + d_dx[i] * p_odtx[i];
271  *Jxy = *Jxy + d_dy[i] * p_odtx[i];
272  *Jyx = *Jyx + d_dx[i] * p_odty[i];
273  *Jyy = *Jyy + d_dy[i] * p_odty[i];
274  }
275  }
276 
277 }
Isis::CameraDistortionMap::p_focalPlaneX
double p_focalPlaneX
Distorted focal plane x.
Definition: CameraDistortionMap.h:65
Isis::TaylorCameraDistortionMap::SetDistortion
void SetDistortion(const int naifIkCode)
Load distortion coefficients.
Definition: TaylorCameraDistortionMap.cpp:63
Isis::CameraDistortionMap::p_undistortedFocalPlaneY
double p_undistortedFocalPlaneY
Undistorted focal plane y.
Definition: CameraDistortionMap.h:68
Isis::TaylorCameraDistortionMap::SetFocalPlane
bool SetFocalPlane(const double dx, const double dy)
Compute undistorted focal plane x/y.
Definition: TaylorCameraDistortionMap.cpp:92
Isis::CameraDistortionMap::p_undistortedFocalPlaneX
double p_undistortedFocalPlaneX
Undistorted focal plane x.
Definition: CameraDistortionMap.h:67
Isis::TaylorCameraDistortionMap::DistortionFunctionJacobian
void DistortionFunctionJacobian(double x, double y, double *Jxx, double *Jxy, double *Jyx, double *Jyy)
Jacobian of the distortion function.
Definition: TaylorCameraDistortionMap.cpp:239
Isis::TaylorCameraDistortionMap::DistortionFunction
void DistortionFunction(double ux, double uy, double *dx, double *dy)
Compute distorted focal plane dx,dy given an undistorted focal plane ux,uy.
Definition: TaylorCameraDistortionMap.cpp:203
Isis::TaylorCameraDistortionMap::SetUndistortedFocalPlane
bool SetUndistortedFocalPlane(const double ux, const double uy)
Compute distorted focal plane x/y.
Definition: TaylorCameraDistortionMap.cpp:177
Isis::Camera
Definition: Camera.h:236
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::TaylorCameraDistortionMap::p_odtx
std::vector< double > p_odtx
distortion x coefficients
Definition: TaylorCameraDistortionMap.h:54
Isis::CameraDistortionMap::p_camera
Camera * p_camera
The camera to distort/undistort.
Definition: CameraDistortionMap.h:63
Isis::CameraDistortionMap
Distort/undistort focal plane coordinates.
Definition: CameraDistortionMap.h:41
std
Namespace for the standard library.
Isis::TaylorCameraDistortionMap::p_odty
std::vector< double > p_odty
distortion y coefficients
Definition: TaylorCameraDistortionMap.h:55
Isis::E
const double E
Sets some basic constants for use in ISIS programming.
Definition: Constants.h:39
Isis::Spice::getDouble
SpiceDouble getDouble(const QString &key, int index=0)
This returns a value from the NAIF text pool.
Definition: Spice.cpp:1039
Isis::CameraDistortionMap::p_focalPlaneY
double p_focalPlaneY
Distorted focal plane y.
Definition: CameraDistortionMap.h:66
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16