Isis 3 Programmer Reference
Chandrayaan1M3DistortionMap.cpp
1
7/* SPDX-License-Identifier: CC0-1.0 */
8
9#include <iomanip>
10
11#include "Chandrayaan1M3DistortionMap.h"
12#include "CameraFocalPlaneMap.h"
13
14using namespace std;
15
16namespace Isis {
17 Chandrayaan1M3DistortionMap::Chandrayaan1M3DistortionMap(Camera *parent,
18 double xp, double yp,
19 double k1, double k2, double k3,
20 double p1, double p2) :
21 CameraDistortionMap(parent, 1.0) {
22
23 p_xp = xp;
24 p_yp = yp;
25 p_k1 = k1;
26 p_k2 = k2;
27 p_k3 = k3;
28 p_p1 = p1;
29 p_p2 = p2;
30 }
31
44 bool Chandrayaan1M3DistortionMap::SetFocalPlane(const double dx, const double dy) {
45 p_focalPlaneX = dx;
46 p_focalPlaneY = dy;
47
48 // reducing to principal point offset (xp,yp)
49 double x = dx - p_xp;
50 double y = dy - p_yp;
51
52// std::cout << setprecision(14) << " dx " << dx << " dy " << dy << " p_xp " << p_xp << " p_yp " << p_yp;
53 // r is the distance between the principal point and the measured point on the image
54 double rr = x * x + y * y;
55// std::cout << " rr " << rr << " r " << sqrt(rr);
56
57 // dr is the radial distortion contribution
58 double dr = p_k1 + p_k2 * rr + p_k3 * rr * rr;
59// std::cout << " dr " << dr;
60
61 // dtx and dty are the decentering distortion contribution in x and y
62 double dtx = p_p1 * (rr + 2.0 * x * x) + 2.0 * p_p2 * x * y;
63 double dty = 2.0 * p_p1 * x * y + p_p2 * (rr + 2 * y * y);
64// std::cout << " dtx " << dtx << " dty " << dty << std::endl;
65
66 // image coordinates corrected for principal point, radial and decentering distortion (p1,p2,p3)
67 p_undistortedFocalPlaneX = dx + x * dr + dtx;
68 p_undistortedFocalPlaneY = dy + y * dr + dty;
69
70 return true;
71 }
72
73
86 bool Chandrayaan1M3DistortionMap::SetUndistortedFocalPlane(const double ux, const double uy) {
87
88 double xt = ux;
89 double yt = uy;
90
91 double xx, yy, rr, dr;
92 double xdistortion, ydistortion;
93 double xdistorted, ydistorted;
94 double xprevious, yprevious;
95
96 // dr is the radial distortion contribution
97 // dtx and dty are the decentering distortion contributions in x and y
98
99 xprevious = 1000000.0;
100 yprevious = 1000000.0;
101
102 double tolerance = 0.000001;
103
104 bool bConverged = false;
105
106 // iterating to introduce distortion...
107 // we stop when the difference between distorted coordinates
108 // in successive iterations is at or below the given tolerance
109 for (int i = 0; i < 50; i++) {
110 xx = xt * xt;
111 yy = yt * yt;
112 rr = xx + yy;
113
114 // radial distortion
115 // dr is the radial distortion contribution
116 // -dt*sin(p_t0) is the decentering distortion contribution in the x-direction
117 // dt*cos(p_t0) is the decentering distortion contribution in the y-direction
118 dr = p_k1 + p_k2 * rr + p_k3 * rr * rr;
119
120 double dtx = p_p1 * (rr + 2.0 * xt * xt) + 2.0 * p_p2 * xt * yt;
121 double dty = 2.0 * p_p1 * xt * yt + p_p2 * (rr + 2 * yt * yt);
122
123 // distortion at the current point location
124 xdistortion = dr * xt + dtx;
125 ydistortion = dr * yt + dty;
126
127 // updated image coordinates
128 xt = ux - xdistortion;
129 yt = uy - ydistortion;
130
131 // distorted point corrected for principal point
132 xdistorted = xt + p_xp;
133 ydistorted = yt + p_yp;
134
135 // check for convergence
136 if ((fabs(xt - xprevious) <= tolerance) && (fabs(yt - yprevious) <= tolerance)) {
137 bConverged = true;
138 break;
139 }
140
141 xprevious = xt;
142 yprevious = yt;
143 }
144
145 if (bConverged) {
146 p_undistortedFocalPlaneX = ux;
147 p_undistortedFocalPlaneY = uy;
148
149 p_focalPlaneX = xdistorted;
150 p_focalPlaneY = ydistorted;
151 }
152
153 return bConverged;
154 }
155}
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Namespace for the standard library.