From c2c9f659ff16befd4036413159f14b26265d6a4d Mon Sep 17 00:00:00 2001
From: Luc Maisonobe <luc@orekit.org>
Date: Sat, 8 Mar 2014 17:40:20 +0100
Subject: [PATCH] Added method to check ground point coverage.

---
 .../orekit/rugged/core/dem/SimpleTile.java    | 51 +++++++++++--------
 .../java/org/orekit/rugged/core/dem/Tile.java | 19 ++++---
 .../rugged/core/dem/SimpleTileTest.java       | 13 +++--
 3 files changed, 52 insertions(+), 31 deletions(-)

diff --git a/rugged-core/src/main/java/org/orekit/rugged/core/dem/SimpleTile.java b/rugged-core/src/main/java/org/orekit/rugged/core/dem/SimpleTile.java
index fa618c1b..8aac9fa9 100644
--- a/rugged-core/src/main/java/org/orekit/rugged/core/dem/SimpleTile.java
+++ b/rugged-core/src/main/java/org/orekit/rugged/core/dem/SimpleTile.java
@@ -16,18 +16,18 @@
  */
 package org.orekit.rugged.core.dem;
 
-import org.orekit.rugged.api.UpdatableTile;
+import org.apache.commons.math3.util.FastMath;
 
-/** Simple implementation of a {@link UpdatableTile}.
+/** Simple implementation of a {@link Tile}.
  * @author Luc Maisonobe
  */
 public class SimpleTile implements Tile {
 
-    /** Reference latitude. */
-    private double referenceLatitude;
+    /** Minimum latitude. */
+    private double minLatitude;
 
-    /** Reference longitude. */
-    private double referenceLongitude;
+    /** Minimum longitude. */
+    private double minLongitude;
 
     /** Step in latitude (size of one raster element). */
     private double latitudeStep;
@@ -54,16 +54,16 @@ public class SimpleTile implements Tile {
 
     /** {@inheritDoc} */
     @Override
-    public void setGeometry(final double referenceLatitude, final double referenceLongitude,
-                     final double latitudeStep, final double longitudeStep,
-                     final int latitudeRows, final int longitudeColumns) {
-        this.referenceLatitude  = referenceLatitude;
-        this.referenceLongitude = referenceLongitude;
-        this.latitudeStep       = latitudeStep;
-        this.longitudeStep      = longitudeStep;
-        this.latitudeRows       = latitudeRows;
-        this.longitudeColumns   = longitudeColumns;
-        this.elevations         = new double[latitudeRows * longitudeColumns];
+    public void setGeometry(final double minLatitude, final double minLongitude,
+                            final double latitudeStep, final double longitudeStep,
+                            final int latitudeRows, final int longitudeColumns) {
+        this.minLatitude      = minLatitude;
+        this.minLongitude     = minLongitude;
+        this.latitudeStep     = latitudeStep;
+        this.longitudeStep    = longitudeStep;
+        this.latitudeRows     = latitudeRows;
+        this.longitudeColumns = longitudeColumns;
+        this.elevations       = new double[latitudeRows * longitudeColumns];
     }
 
     /** {@inheritDoc} */
@@ -76,14 +76,14 @@ public class SimpleTile implements Tile {
 
     /** {@inheritDoc} */
     @Override
-    public double getReferenceLatitude() {
-        return referenceLatitude;
+    public double getMinimumLatitude() {
+        return minLatitude;
     }
 
     /** {@inheritDoc} */
     @Override
-    public double getReferenceLongitude() {
-        return referenceLongitude;
+    public double getMinimumLongitude() {
+        return minLongitude;
     }
 
     /** {@inheritDoc} */
@@ -118,6 +118,15 @@ public class SimpleTile implements Tile {
         return elevations[latitudeIndex * longitudeColumns + longitudeIndex];
     }
 
+    /** {@inheritDoc} */
+    @Override
+    public boolean covers(final double latitude, final double longitude) {
+        final int latitudeIndex  = (int) FastMath.floor((latitude  - minLatitude)  / latitudeStep);
+        final int longitudeIndex = (int) FastMath.floor((longitude - minLongitude) / longitudeStep);
+        return latitudeIndex  >= 0 && latitudeIndex  < latitudeRows &&
+               longitudeIndex >= 0 && longitudeIndex < longitudeColumns;
+    }
+
     /** Check indices.
      * @param latitudeIndex
      * @param longitudeIndex
@@ -125,7 +134,7 @@ public class SimpleTile implements Tile {
      */
     private void checkIndices(int latitudeIndex, int longitudeIndex)
         throws IllegalArgumentException {
-        if (latitudeIndex < 0 || latitudeIndex >= latitudeRows ||
+        if (latitudeIndex  < 0 || latitudeIndex  >= latitudeRows ||
             longitudeIndex < 0 || longitudeIndex >= longitudeColumns) {
             throw new IllegalArgumentException();
         }        
diff --git a/rugged-core/src/main/java/org/orekit/rugged/core/dem/Tile.java b/rugged-core/src/main/java/org/orekit/rugged/core/dem/Tile.java
index f6e3fbcb..82e53d5f 100644
--- a/rugged-core/src/main/java/org/orekit/rugged/core/dem/Tile.java
+++ b/rugged-core/src/main/java/org/orekit/rugged/core/dem/Tile.java
@@ -23,15 +23,15 @@ import org.orekit.rugged.api.UpdatableTile;
  */
 public interface Tile extends UpdatableTile {
 
-    /** Get reference latitude.
-     * @return reference latitude
+    /** Get minimum latitude.
+     * @return minimum latitude
      */
-    double getReferenceLatitude();
+    double getMinimumLatitude();
 
-    /** Get reference longitude.
-     * @return reference longitude
+    /** Get minimum longitude.
+     * @return minimum longitude
      */
-    double getReferenceLongitude();
+    double getMinimumLongitude();
 
     /** Get step in latitude (size of one raster element).
      * @return step in latitude
@@ -62,4 +62,11 @@ public interface Tile extends UpdatableTile {
     double getElevationAtIndices(int latitudeIndex, int longitudeIndex)
         throws IllegalArgumentException;
 
+    /** Check if a tile covers a ground point.
+     * @param latitude ground point latitude
+     * @param longitude ground point longitude
+     * @return true if the tile covers the ground point
+     */
+    boolean covers(double latitude, double longitude);
+
 }
diff --git a/rugged-core/src/test/java/orekit/rugged/core/dem/SimpleTileTest.java b/rugged-core/src/test/java/orekit/rugged/core/dem/SimpleTileTest.java
index f182feb3..9faae345 100644
--- a/rugged-core/src/test/java/orekit/rugged/core/dem/SimpleTileTest.java
+++ b/rugged-core/src/test/java/orekit/rugged/core/dem/SimpleTileTest.java
@@ -25,8 +25,8 @@ public class SimpleTileTest {
     @Test
     public void testEmpty() {
         SimpleTile tile = new SimpleTile();
-        Assert.assertEquals(0, tile.getReferenceLatitude(), 1.0e-10);
-        Assert.assertEquals(0, tile.getReferenceLongitude(), 1.0e-10);
+        Assert.assertEquals(0, tile.getMinimumLatitude(), 1.0e-10);
+        Assert.assertEquals(0, tile.getMinimumLongitude(), 1.0e-10);
         Assert.assertEquals(0, tile.getLatitudeStep(), 1.0e-10);
         Assert.assertEquals(0, tile.getLongitudeStep(), 1.0e-10);
         Assert.assertEquals(0, tile.getLatitudeRows());
@@ -44,13 +44,18 @@ public class SimpleTileTest {
             }
         }
 
-        Assert.assertEquals(1.0, tile.getReferenceLatitude(), 1.0e-10);
-        Assert.assertEquals(2.0, tile.getReferenceLongitude(), 1.0e-10);
+        Assert.assertEquals(1.0, tile.getMinimumLatitude(), 1.0e-10);
+        Assert.assertEquals(2.0, tile.getMinimumLongitude(), 1.0e-10);
         Assert.assertEquals(0.1, tile.getLatitudeStep(), 1.0e-10);
         Assert.assertEquals(0.2, tile.getLongitudeStep(), 1.0e-10);
         Assert.assertEquals(100, tile.getLatitudeRows());
         Assert.assertEquals(200, tile.getLongitudeColumns());
 
+        Assert.assertTrue(tile.covers(  6.0, 22.0));
+        Assert.assertFalse(tile.covers( 0.0, 22.0));
+        Assert.assertFalse(tile.covers(12.0, 22.0));
+        Assert.assertFalse(tile.covers( 6.0,  1.0));
+        Assert.assertFalse(tile.covers( 6.0, 43.0));
 
         for (int i = 0; i < tile.getLatitudeRows(); ++i) {
             for (int j = 0; j < tile.getLongitudeColumns(); ++j) {
-- 
GitLab