Isis 3 Programmer Reference
CameraDistortionMap.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include "IString.h"
8 #include "CameraDistortionMap.h"
9 
10 namespace Isis {
24  CameraDistortionMap::CameraDistortionMap(Camera *parent, double zDirection) {
25  p_camera = parent;
27  p_zDirection = zDirection;
28  }
29 
30 
35  }
36 
37 
58  void CameraDistortionMap::SetDistortion(int naifIkCode) {
59  QString odkkey = "INS" + toString(naifIkCode) + "_OD_K";
60  for (int i = 0; i < 3; ++i) {
61  p_odk.push_back(p_camera->Spice::getDouble(odkkey, i));
62  }
63  }
64 
65 
83  bool CameraDistortionMap::SetFocalPlane(double dx, double dy) {
84  p_focalPlaneX = dx;
85  p_focalPlaneY = dy;
86 
87  // No coefficients == no distortion
88  if (p_odk.size() <= 0) {
91  return true;
92  }
93 
94  // Get the distance from the focal plane center and if we are close
95  // then skip the distortion
96  double r2 = (dx * dx) + (dy * dy);
97  if (r2 <= 1.0E-6) {
100  return true;
101  }
102 
103  // Ok we need to apply distortion correction
104  double drOverR = p_odk[0] + (r2 * (p_odk[1] + (r2 * p_odk[2])));
105  p_undistortedFocalPlaneX = dx - (drOverR * dx);
106  p_undistortedFocalPlaneY = dy - (drOverR * dy);
107  return true;
108  }
109 
110 
131  const double uy) {
134 
135  // No coefficients == nodistortion
136  if (p_odk.size() <= 0) {
137  p_focalPlaneX = ux;
138  p_focalPlaneY = uy;
139  return true;
140  }
141 
142  // Compute the distance from the focal plane center and if we are
143  // close to the center then no distortion is required
144  double rp2 = (ux * ux) + (uy * uy);
145  if (rp2 <= 1.0E-6) {
146  p_focalPlaneX = ux;
147  p_focalPlaneY = uy;
148  return true;
149  }
150 
151  // Ok make the correction, start by computing
152  // fractional distortion at rp (r-prime)
153  double rp = sqrt(rp2);
154  double drOverR = p_odk[0] + (rp2 * (p_odk[1] + (rp2 * p_odk[2])));
155 
156  // Estimate r
157  double r = rp + (drOverR * rp);
158  double r_prev, r2_prev;
159  double tolMilliMeters = p_camera->PixelPitch() / 100.0;
160  int iteration = 0;
161  do {
162  // Don't get in an end-less loop. This algorithm should
163  // converge quickly. If not then we are probably way outside
164  // of the focal plane. Just set the distorted position to the
165  // undistorted position. Also, make sure the focal plane is less
166  // than 1km, it is unreasonable for it to grow larger than that.
167  if (iteration >= 15 || r > 1E9) {
168  drOverR = 0.0;
169  break;
170  }
171 
172  r_prev = r;
173  r2_prev = r * r;
174 
175  // Compute new fractional distortion:
176  drOverR = p_odk[0] + (r2_prev * (p_odk[1] + (r2_prev * p_odk[2])));
177 
178  r = rp + (drOverR * r_prev); // Compute new estimate of r
179  iteration++;
180  }
181  while (fabs(r - r_prev) > tolMilliMeters);
182 
183  p_focalPlaneX = ux / (1.0 - drOverR);
184  p_focalPlaneY = uy / (1.0 - drOverR);
185  return true;
186  }
187 
188 
195  return p_odk;
196  }
197 
198 
205  return p_zDirection;
206  }
207 
208 
216  return p_focalPlaneX;
217  }
218 
219 
227  return p_focalPlaneY;
228  }
229 
230 
239  }
240 
241 
250  }
251 
252 
261  return p_zDirection *p_camera->FocalLength();
262  }
263 
264 }
Isis::CameraDistortionMap::SetDistortion
virtual void SetDistortion(int naifIkCode)
Load distortion coefficients.
Definition: CameraDistortionMap.cpp:58
Isis::CameraDistortionMap::p_focalPlaneX
double p_focalPlaneX
Distorted focal plane x.
Definition: CameraDistortionMap.h:65
Isis::CameraDistortionMap::OpticalDistortionCoefficients
std::vector< double > OpticalDistortionCoefficients() const
Retrieve the distortion coefficients used for this model.
Definition: CameraDistortionMap.cpp:194
Isis::CameraDistortionMap::UndistortedFocalPlaneX
double UndistortedFocalPlaneX() const
Gets the x-value in the undistorted focal plane coordinate system.
Definition: CameraDistortionMap.cpp:237
Isis::CameraDistortionMap::p_undistortedFocalPlaneY
double p_undistortedFocalPlaneY
Undistorted focal plane y.
Definition: CameraDistortionMap.h:68
Isis::CameraDistortionMap::SetUndistortedFocalPlane
virtual bool SetUndistortedFocalPlane(double ux, double uy)
Compute distorted focal plane x/y.
Definition: CameraDistortionMap.cpp:130
Isis::CameraDistortionMap::p_undistortedFocalPlaneX
double p_undistortedFocalPlaneX
Undistorted focal plane x.
Definition: CameraDistortionMap.h:67
Isis::CameraDistortionMap::FocalPlaneX
double FocalPlaneX() const
Gets the x-value in the focal plane coordinate system.
Definition: CameraDistortionMap.cpp:215
Isis::CameraDistortionMap::FocalPlaneY
double FocalPlaneY() const
Gets the y-value in the focal plane coordinate system.
Definition: CameraDistortionMap.cpp:226
Isis::CameraDistortionMap::UndistortedFocalPlaneZ
double UndistortedFocalPlaneZ() const
Gets the z-value in the undistorted focal plane coordinate system.
Definition: CameraDistortionMap.cpp:260
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::CameraDistortionMap::CameraDistortionMap
CameraDistortionMap(Camera *parent, double zDirection=1.0)
Camera distortion map constructor.
Definition: CameraDistortionMap.cpp:24
Isis::CameraDistortionMap::p_camera
Camera * p_camera
The camera to distort/undistort.
Definition: CameraDistortionMap.h:63
Isis::CameraDistortionMap::~CameraDistortionMap
virtual ~CameraDistortionMap()
Destructor for the ISIS default camera distortion map.
Definition: CameraDistortionMap.cpp:34
Isis::CameraDistortionMap::ZDirection
double ZDirection() const
Gets the z-direction for this camera.
Definition: CameraDistortionMap.cpp:204
Isis::Camera::FocalLength
double FocalLength() const
Returns the focal length.
Definition: Camera.cpp:2732
Isis::Camera::PixelPitch
double PixelPitch() const
Returns the pixel pitch.
Definition: Camera.cpp:2742
Isis::CameraDistortionMap::SetFocalPlane
virtual bool SetFocalPlane(double dx, double dy)
Compute undistorted focal plane x/y.
Definition: CameraDistortionMap.cpp:83
Isis::E
const double E
Sets some basic constants for use in ISIS programming.
Definition: Constants.h:39
Isis::CameraDistortionMap::p_odk
std::vector< double > p_odk
Vector of distortion coefficients.
Definition: CameraDistortionMap.h:71
Isis::CameraDistortionMap::UndistortedFocalPlaneY
double UndistortedFocalPlaneY() const
Gets the y-value in the undistorted focal plane coordinate system.
Definition: CameraDistortionMap.cpp:248
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
Isis::CameraDistortionMap::p_zDirection
double p_zDirection
Undistorted focal plane z.
Definition: CameraDistortionMap.h:69
Isis::Camera::SetDistortionMap
void SetDistortionMap(CameraDistortionMap *map, bool deleteExisting=true)
Sets the Distortion Map.
Definition: Camera.cpp:2342