From 214170e46fbc57800b74d9278fec3848fbc9f7ba Mon Sep 17 00:00:00 2001
From: acpaquette <acp263@nau.edu>
Date: Fri, 4 Nov 2022 16:01:00 -0700
Subject: [PATCH] Added tests for cahvor distortion

---
 src/Distortion.cpp        | 11 +++-----
 tests/DistortionTests.cpp | 56 +++++++++++++++++++++++++++++++++++++++
 tests/Fixtures.h          |  3 +++
 3 files changed, 63 insertions(+), 7 deletions(-)

diff --git a/src/Distortion.cpp b/src/Distortion.cpp
index 5c08ed6..a40ae12 100644
--- a/src/Distortion.cpp
+++ b/src/Distortion.cpp
@@ -204,14 +204,11 @@ void removeDistortion(double dx, double dy, double &ux, double &uy,
       double r = sqrt(r2);
       double r3 = r2 * r;
 
-      int xPointer = 0;
-      int yPointer = 5;
-
       double dr_x = odkx[0] + odkx[1] * r + odkx[2] * r2 + odkx[3] * r3;
       double dr_y = odky[0] + odky[1] * r + odky[2] * r2 + odky[3] * r3;
 
-      ux = dx + dr_x + boresightX;
-      uy = dy + dr_y + boresightY;
+      ux += dr_x + boresightX;
+      uy += dr_y + boresightY;
     } break;
 
     // The dawn distortion model is "reversed" from other distortion models so
@@ -593,8 +590,8 @@ void applyDistortion(double ux, double uy, double &dx, double &dy,
           iteration++;
         } while (fabs(r - r_prev) > desiredPrecision);
 
-        dx = ux / (1.0 - drOverR);
-        dy = uy / (1.0 - drOverR);
+        dx = shiftedUx / (1.0 - drOverR);
+        dy = shiftedUy / (1.0 - drOverR);
         dx += opticalDistCoeffs[3];
         dy += opticalDistCoeffs[4];
       }
diff --git a/tests/DistortionTests.cpp b/tests/DistortionTests.cpp
index 7c00358..1c44432 100644
--- a/tests/DistortionTests.cpp
+++ b/tests/DistortionTests.cpp
@@ -324,3 +324,59 @@ TEST(LroLrocNac, testZeroCoeffs) {
   ASSERT_DOUBLE_EQ(ux, 0.0);
   ASSERT_DOUBLE_EQ(uy, 0.0);
 }
+
+INSTANTIATE_TEST_SUITE_P(CahvorTest, CoeffOffsetParameterizedTest,
+                         ::testing::Values(std::vector<double>(2, 0),
+                                           std::vector<double>(2, 1)));
+
+TEST_P(CoeffOffsetParameterizedTest, RemoveDistortionCahvorTest)
+{
+  csm::ImageCoord imagePt(0.0, 4.0);
+
+  double ux, uy;
+  std::vector<double> offsets = GetParam();
+  std::vector<double> coeffs = {0, 0, 0};
+  coeffs.insert(coeffs.end(), offsets.begin(), offsets.end());
+
+  removeDistortion(imagePt.samp, imagePt.line, ux, uy, coeffs,
+                   DistortionType::CAHVOR);
+
+  EXPECT_NEAR(ux, 4, 1e-8);
+  EXPECT_NEAR(uy, 0, 1e-8);
+}
+
+// If coeffs are 0 then this will have the same result as removeDistortion
+// with 0 distortion coefficients
+TEST_P(CoeffOffsetParameterizedTest, InverseDistortionCahvorTest)
+{
+  csm::ImageCoord imagePt(0.0, 4.0);
+
+  double dx, dy;
+  double desiredPrecision = 0.01;
+  std::vector<double> offsets = GetParam();
+  std::vector<double> coeffs = {0, 0, 0};
+  coeffs.insert(coeffs.end(), offsets.begin(), offsets.end());
+
+  applyDistortion(imagePt.samp, imagePt.line, dx, dy, coeffs,
+                  DistortionType::CAHVOR, desiredPrecision);
+
+  EXPECT_NEAR(dx, 4, 1e-8);
+  EXPECT_NEAR(dy, 0, 1e-8);
+}
+
+TEST_P(CoeffOffsetParameterizedTest, InverseOnesCoeffsCahvorTest)
+{
+  csm::ImageCoord imagePt(0.0, 4.0);
+
+  double dx, dy;
+  double desiredPrecision = 0.01;
+  std::vector<double> offsets = GetParam();
+  std::vector<double> coeffs = {1, 1, 1};
+  coeffs.insert(coeffs.end(), offsets.begin(), offsets.end());
+
+  applyDistortion(imagePt.samp, imagePt.line, dx, dy, coeffs,
+                  DistortionType::CAHVOR, desiredPrecision);
+
+  EXPECT_NEAR(dx, 4, 1e-8);
+  EXPECT_NEAR(dy, 0, 1e-8);
+}
diff --git a/tests/Fixtures.h b/tests/Fixtures.h
index c1a19ad..2b5b053 100644
--- a/tests/Fixtures.h
+++ b/tests/Fixtures.h
@@ -177,6 +177,9 @@ class ConstVelLineScanIsdTest : public ::testing::Test {
 class ImageCoordParameterizedTest
     : public ::testing::TestWithParam<csm::ImageCoord> {};
 
+class CoeffOffsetParameterizedTest
+    : public ::testing::TestWithParam<std::vector<double>> {};
+
 class FramerParameterizedTest
     : public ::testing::TestWithParam<csm::ImageCoord> {
  protected:
-- 
GitLab