From 673dbf659fd64fd799b0fb4640bc3d54795eb36f Mon Sep 17 00:00:00 2001
From: Luc Maisonobe <luc@orekit.org>
Date: Fri, 26 Sep 2014 11:32:06 +0200
Subject: [PATCH] Explained the index shift in the getXxxIndex methods.

---
 .../java/org/orekit/rugged/raster/Tile.java   | 12 +++++--
 .../orekit/rugged/raster/SimpleTileTest.java  | 36 +++++++++++++++++++
 2 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/orekit/rugged/raster/Tile.java b/core/src/main/java/org/orekit/rugged/raster/Tile.java
index d6abd003..eb91e9d2 100644
--- a/core/src/main/java/org/orekit/rugged/raster/Tile.java
+++ b/core/src/main/java/org/orekit/rugged/raster/Tile.java
@@ -128,13 +128,21 @@ public interface Tile extends UpdatableTile {
     int getLongitudeColumns();
 
     /** Get the latitude index of a point.
+     * <p>
+     * This method shift indices 1/2 pixel, so that
+     * the specified latitude is always between index and index+1.
+     * </p>
      * @param latitude geodetic latitude
-     * @return latirute index (it may lie outside of the tile!)
+     * @return latitude index (it may lie outside of the tile!)
      */
     int getLatitudeIndex(double latitude);
 
     /** Get the longitude index of a point.
-     * @param longitude geodetic latitude
+     * <p>
+     * This method shift indices 1/2 pixel, so that
+     * the specified longitude is always between index and index+1.
+     * </p>
+     * @param longitude geodetic longitude
      * @return longitude index (it may lie outside of the tile!)
      */
     int getLongitudeIndex(double longitude);
diff --git a/core/src/test/java/org/orekit/rugged/raster/SimpleTileTest.java b/core/src/test/java/org/orekit/rugged/raster/SimpleTileTest.java
index a71c1ce1..ac0d4f97 100644
--- a/core/src/test/java/org/orekit/rugged/raster/SimpleTileTest.java
+++ b/core/src/test/java/org/orekit/rugged/raster/SimpleTileTest.java
@@ -115,6 +115,42 @@ public class SimpleTileTest {
         checkOutOfBound( 50, 200, tile);
     }
 
+    @Test
+    public void testIndexShift() throws RuggedException {
+
+        SimpleTile tile = new SimpleTileFactory().createTile();
+        tile.setGeometry(1.0, 2.0, 0.1, 0.2, 100, 200);
+        tile.setElevation(50, 100, 1000.0);
+        tile.tileUpdateCompleted();
+
+        // indices correspond to pixels centers
+        double latCenterColumn50 = tile.getLatitudeAtIndex(50);
+        double latCenterColumn51 = tile.getLatitudeAtIndex(51);
+        double lonCenterRow23    = tile.getLongitudeAtIndex(23);
+        double lonCenterRow24    = tile.getLongitudeAtIndex(24);
+
+        // getLatitudeIndex shift indices 1/2 pixel, so that
+        // the specified latitude is always between index and index+1
+        // so despite latWestColumn51 is very close to column 51 center,
+        // getLatitudeIndex should return 50
+        double latWestColumn51   = 0.001 * latCenterColumn50 + 0.999 * latCenterColumn51;
+        int retrievedLatIndex = tile.getLatitudeIndex(latWestColumn51);
+        Assert.assertEquals(50, retrievedLatIndex);
+        Assert.assertTrue(tile.getLatitudeAtIndex(retrievedLatIndex) < latWestColumn51);
+        Assert.assertTrue(latWestColumn51 < tile.getLatitudeAtIndex(retrievedLatIndex + 1));
+
+        // getLongitudeIndex shift indices 1/2 pixel, so that
+        // the specified longitude is always between index and index+1
+        // so despite lonSouthRow24 is very close to row 24 center,
+        // getLongitudeIndex should return 23
+        double lonSouthRow24     = 0.001 * lonCenterRow23    + 0.999 * lonCenterRow24;
+        int retrievedLonIndex = tile.getLongitudeIndex(lonSouthRow24);
+        Assert.assertEquals(23, retrievedLonIndex);
+        Assert.assertTrue(tile.getLongitudeAtIndex(retrievedLonIndex) < lonSouthRow24);
+        Assert.assertTrue(lonSouthRow24 < tile.getLongitudeAtIndex(retrievedLonIndex + 1));
+
+    }
+
     private void checkOutOfBound(int i, int j, Tile tile) {
         try {
             tile.setElevation(i, j, 1000.0);
-- 
GitLab