From be2a14d6e8e52976ae93c3a4eb90f0255fc13572 Mon Sep 17 00:00:00 2001
From: Oleg Alexandrov <oleg.alexandrov@gmail.com>
Date: Wed, 20 Apr 2022 14:41:12 -0700
Subject: [PATCH] Fix for 0.5 pixel shift in the SAR model (#370)

* Bugfix for a 0.5 pixel offset in the SAR model

* Fix tests too

* Beautify the test

* Remove extra code from the test
---
 src/UsgsAstroSarSensorModel.cpp | 6 +++---
 tests/SarTests.cpp              | 9 ++++-----
 2 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/src/UsgsAstroSarSensorModel.cpp b/src/UsgsAstroSarSensorModel.cpp
index dfbd5e8..f84b206 100644
--- a/src/UsgsAstroSarSensorModel.cpp
+++ b/src/UsgsAstroSarSensorModel.cpp
@@ -374,7 +374,7 @@ csm::ImageCoord UsgsAstroSarSensorModel::groundToImage(
         groundPt, time, slantRangeValue, groundTolerance);
 
     double line = (time - m_startingEphemerisTime) / m_exposureDuration + 0.5;
-    double sample = groundRange / m_scaledPixelWidth;
+    double sample = groundRange / m_scaledPixelWidth + 0.5;
     return csm::ImageCoord(line, sample);
   } catch (std::exception& error) {
     std::string message = "Could not calculate groundToImage, with error [";
@@ -504,7 +504,7 @@ csm::EcefCoord UsgsAstroSarSensorModel::imageToGround(
               imagePt.line, height, desiredPrecision);
   double time =
       m_startingEphemerisTime + (imagePt.line - 0.5) * m_exposureDuration;
-  double groundRange = imagePt.samp * m_scaledPixelWidth;
+  double groundRange = (imagePt.samp - 0.5) * m_scaledPixelWidth;
   std::vector<double> coeffs = getRangeCoefficients(time);
   double slantRange = groundRangeToSlantRange(groundRange, coeffs);
 
@@ -640,7 +640,7 @@ csm::EcefLocus UsgsAstroSarSensorModel::imageToProximateImagingLocus(
   // Compute the slant range
   double time =
       m_startingEphemerisTime + (imagePt.line - 0.5) * m_exposureDuration;
-  double groundRange = imagePt.samp * m_scaledPixelWidth;
+  double groundRange = (imagePt.samp - 0.5) * m_scaledPixelWidth;
   std::vector<double> coeffs = getRangeCoefficients(time);
   double slantRange = groundRangeToSlantRange(groundRange, coeffs);
 
diff --git a/tests/SarTests.cpp b/tests/SarTests.cpp
index 00285c2..8871d4f 100644
--- a/tests/SarTests.cpp
+++ b/tests/SarTests.cpp
@@ -43,14 +43,13 @@ TEST_F(SarSensorModel, State) {
 TEST_F(SarSensorModel, Center) {
   csm::ImageCoord imagePt(500.0, 500.0);
   csm::EcefCoord groundPt = sensorModel->imageToGround(imagePt, 0.0);
-  EXPECT_NEAR(groundPt.x, 1737387.8590770673, 1e-3);
-  EXPECT_NEAR(groundPt.y, -5303.280537826621, 1e-3);
+  EXPECT_NEAR(groundPt.x, 1737387.8671710272, 1e-3);
+  EXPECT_NEAR(groundPt.y, -5300.6282306119301, 1e-3);
   EXPECT_NEAR(groundPt.z, -3749.9796183814506, 1e-3);
 }
 
 TEST_F(SarSensorModel, GroundToImage) {
-  csm::EcefCoord groundPt(1737387.8590770673, -5303.280537826621,
-                          -3749.9796183814506);
+  csm::EcefCoord groundPt(1737387.8671710272, -5300.6282306119301, -3749.9796358514604);
   csm::ImageCoord imagePt = sensorModel->groundToImage(groundPt, 0.001);
   EXPECT_NEAR(imagePt.line, 500.0, 1e-3);
   EXPECT_NEAR(imagePt.samp, 500.0, 1e-3);
@@ -100,7 +99,7 @@ TEST_F(SarSensorModel, imageToProximateImagingLocus) {
       0.001,
       &precision,
       &warnings);
-  EXPECT_NEAR(locus.point.x, 1737388.1260092105, 1e-2);
+  EXPECT_NEAR(locus.point.x, 1737388.1411342232, 1e-2);
   EXPECT_NEAR(locus.point.y, -5403.0102509726485, 1e-2);
   EXPECT_NEAR(locus.point.z, -3749.9801945280433, 1e-2);
   EXPECT_NEAR(locus.direction.x, 0.002701478402694769, 1e-5);
-- 
GitLab