Isis 3 Programmer Reference
OsirisRexDistortionMap.cpp
1
7/* SPDX-License-Identifier: CC0-1.0 */
8
9#include <QDebug>
10#include <QtGlobal>
11#include <QtMath>
12
13#include "NaifStatus.h"
14#include "OsirisRexDistortionMap.h"
15#include "CameraFocalPlaneMap.h"
16
17namespace Isis {
40
41
47
48
71 void OsirisRexDistortionMap::SetDistortion(int naifIkCode, QString filter="UNKNOWN") {
72 // Load distortion coefficients, including filter if we have it.
73 QString odkkey;
74
75 if (filter.toUpper().compare("UNKNOWN") == 0) {
76 odkkey = "INS" + toString(naifIkCode) + "_OD_K";
77 }
78 else {
79 odkkey = "INS" + toString(naifIkCode) + "_OD_K_" + filter.trimmed().toUpper();
80 }
81
82 try {
83 for (int i = 0; i < 5; ++i) {
84 p_odk.push_back(p_camera->Spice::getDouble(odkkey, i));
85 }
86 }
87 catch (IException &e) {
88 // This means that this is an older image without a filter provided
89 // don't update p_odk, we will not apply the distortion in this case
92 return;
93 }
94
95 // Load center-of-distortion coordinates, including filter if we have it
96 QString odcenterkey;
97 if (filter.toUpper().compare("UNKNOWN") == 0) {
98 odcenterkey = "INS" + toString(naifIkCode) + "_OD_CENTER";
99 }
100 else {
101 odcenterkey = "INS" + toString(naifIkCode) + "_OD_CENTER_" + filter.trimmed().toUpper();
102 }
103 m_distortionOriginSample = p_camera->Spice::getDouble(odcenterkey, 0);
104 m_distortionOriginLine = p_camera->Spice::getDouble(odcenterkey, 1);
105 }
106
107
120 bool OsirisRexDistortionMap::SetFocalPlane(double dx, double dy) {
121 p_focalPlaneX = dx;
122 p_focalPlaneY = dy;
123
124 // Only apply the distortion if we have the correct number of coefficients
125 if (p_odk.size() < 2) {
128 return true;
129 }
130
133
134 double xt = dx;
135 double yt = dy;
136
137 double xx, yy, r, rr, rrr, rrrr;
138 double xdistortion, ydistortion;
139 double xdistorted, ydistorted;
140 double xprevious, yprevious;
141 double drOverR;
142
143 xprevious = 1000000.0;
144 yprevious = 1000000.0;
145
146 double tolerance = 0.000001;
147
148 bool bConverged = false;
149
150 // Iterating to introduce distortion...
151 // We stop when the difference between distorted coordinates
152 // in successive iterations is at or below the given tolerance.
153 for(int i = 0; i < 50; i++) {
154 xx = (xt-x0) * (xt-x0);
155 yy = (yt-y0) * (yt-y0);
156 rr = xx + yy;
157 r = qSqrt(rr);
158 rrr = rr * r;
159 rrrr = rr * rr;
160
161 drOverR = p_odk[0] + p_odk[1]*r + p_odk[2]*rr + p_odk[3]*rrr + p_odk[4]*rrrr;
162
163 // distortion at the current point location
164 xdistortion = drOverR * (xt-x0);
165 ydistortion = drOverR * (yt-y0);
166
167 // updated image coordinates
168 xt = dx - xdistortion;
169 yt = dy - ydistortion;
170
171 xdistorted = xt;
172 ydistorted = yt;
173
174 // check for convergence
175 if((fabs(xt - xprevious) <= tolerance) && (fabs(yt - yprevious) <= tolerance)) {
176 bConverged = true;
177 break;
178 }
179
180 xprevious = xt;
181 yprevious = yt;
182 }
183
184 if(bConverged) {
185 p_undistortedFocalPlaneX = xdistorted;
186 p_undistortedFocalPlaneY = ydistorted;
187 }
188 return bConverged;
189 }
190
191
207 const double uy) {
210
211 // Only apply the distortion if we have the correct number of coefficients.
212 if (p_odk.size() < 2) {
213 p_focalPlaneX = ux;
214 p_focalPlaneY = uy;
215 return true;
216 }
219
220 // Compute the distance from the focal plane center. If we are
221 // close to the center then no distortion is required
222 double x = ux;
223 double y = uy;
224 double r = qSqrt(((x-x0) * (x-x0)) + ((y - y0) * (y-y0)));
225
226 if (r <= 1.0E-6) {
227 p_focalPlaneX = ux;
228 p_focalPlaneY = uy;
229 return true;
230 }
231
232 double r2 = r*r;
233 double r3 = r2*r;
234 double r4 = r2*r2;
235
236 double drOverR = p_odk[0] + p_odk[1]*r + p_odk[2]*r2 + p_odk[3]*r3 + p_odk[4]*r4;
237
238 p_focalPlaneX = x + drOverR * (x-x0);
239 p_focalPlaneY = y + drOverR * (y-y0);
240 return true;
241 }
242}
Distort/undistort focal plane coordinates.
double p_focalPlaneX
Distorted focal plane x.
double p_undistortedFocalPlaneX
Undistorted focal plane x.
std::vector< double > p_odk
Vector of distortion coefficients.
double p_undistortedFocalPlaneY
Undistorted focal plane y.
Camera * p_camera
The camera to distort/undistort.
double p_focalPlaneY
Distorted focal plane y.
double PixelPitch() const
Returns the pixel pitch.
Definition Camera.cpp:2772
CameraFocalPlaneMap * FocalPlaneMap()
Returns a pointer to the CameraFocalPlaneMap object.
Definition Camera.cpp:2866
Isis exception class.
Definition IException.h:91
virtual bool SetFocalPlane(double dx, double dy)
Compute undistorted focal plane x/y.
virtual ~OsirisRexDistortionMap()
Default Destructor.
OsirisRexDistortionMap(Camera *parent, double zDirection=1.0)
OSIRIS REx Camera distortion map constructor.
double m_distortionOriginSample
The distortion's origin sample coordinate.
virtual void SetDistortion(int naifIkCode, QString filterName)
Load distortion coefficients and center-of-distortion for OCAMS.
double m_detectorOriginLine
The origin of the detector's line coordinate.
virtual bool SetUndistortedFocalPlane(double ux, double uy)
Compute distorted focal plane x/y.
double m_pixelPitch
The pixel pitch for OCAMS.
double m_distortionOriginLine
The distortion's origin line coordinate.
double m_detectorOriginSample
The origin of the detector's sample coordinate.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition IString.cpp:211