From 06c5ed0a172016bac14b33601e9bc2bdf60d5d0c Mon Sep 17 00:00:00 2001
From: Luc Maisonobe <luc@orekit.org>
Date: Wed, 17 Sep 2014 10:15:39 +0200
Subject: [PATCH] Added detection of wrong position/line-of-sight that misses
 the ground.

---
 .../org/orekit/rugged/api/RuggedMessages.java    |  1 +
 .../duvenhage/DuvenhageAlgorithm.java            |  3 +++
 .../org/orekit/rugged/RuggedMessages_en.utf8     |  3 +++
 .../org/orekit/rugged/RuggedMessages_fr.utf8     |  3 +++
 .../orekit/rugged/api/RuggedMessagesTest.java    |  2 +-
 .../duvenhage/DuvenhageAlgorithmTest.java        | 16 ++++++++++++++++
 src/site/xdoc/changes.xml                        |  3 +++
 7 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/orekit/rugged/api/RuggedMessages.java b/core/src/main/java/org/orekit/rugged/api/RuggedMessages.java
index 044571dd..0f63da90 100644
--- a/core/src/main/java/org/orekit/rugged/api/RuggedMessages.java
+++ b/core/src/main/java/org/orekit/rugged/api/RuggedMessages.java
@@ -57,6 +57,7 @@ public enum RuggedMessages implements Localizable {
     UNINITIALIZED_CONTEXT("general context has not been initialized"),
     EMPTY_TILE("tile is empty: {0} ⨉ {1}"),
     UNKNOWN_SENSOR("unknown sensor {0}"),
+    LINE_OF_SIGHT_DOES_NOT_REACH_GROUND("line-of-sight does not reach ground"),
     LINE_OF_SIGHT_NEVER_CROSSES_LATITUDE("line-of-sight never crosses latitude {0}"),
     LINE_OF_SIGHT_NEVER_CROSSES_LONGITUDE("line-of-sight never crosses longitude {0}"),
     LINE_OF_SIGHT_NEVER_CROSSES_ALTITUDE("line-of-sight never crosses altitude {0}"),
diff --git a/core/src/main/java/org/orekit/rugged/intersection/duvenhage/DuvenhageAlgorithm.java b/core/src/main/java/org/orekit/rugged/intersection/duvenhage/DuvenhageAlgorithm.java
index 9079bc36..2d1b03cd 100644
--- a/core/src/main/java/org/orekit/rugged/intersection/duvenhage/DuvenhageAlgorithm.java
+++ b/core/src/main/java/org/orekit/rugged/intersection/duvenhage/DuvenhageAlgorithm.java
@@ -72,6 +72,9 @@ public class DuvenhageAlgorithm implements IntersectionAlgorithm {
 
             // compute intersection with ellipsoid
             final GeodeticPoint gp0 = ellipsoid.pointOnGround(position, los);
+            if (gp0 == null) {
+                throw new RuggedException(RuggedMessages.LINE_OF_SIGHT_DOES_NOT_REACH_GROUND);
+            }
 
             // locate the entry tile along the line-of-sight
             MinMaxTreeTile tile = cache.getTile(gp0.getLatitude(), gp0.getLongitude());
diff --git a/core/src/main/resources/assets/org/orekit/rugged/RuggedMessages_en.utf8 b/core/src/main/resources/assets/org/orekit/rugged/RuggedMessages_en.utf8
index 587c7eb1..118da04a 100644
--- a/core/src/main/resources/assets/org/orekit/rugged/RuggedMessages_en.utf8
+++ b/core/src/main/resources/assets/org/orekit/rugged/RuggedMessages_en.utf8
@@ -19,6 +19,9 @@ EMPTY_TILE = tile is empty: {0} ⨉ {1}
 # unknown sensor {0}
 UNKNOWN_SENSOR = unknown sensor {0}
 
+# line-of-sight does not reach ground
+LINE_OF_SIGHT_DOES_NOT_REACH_GROUND = line-of-sight does not reach ground
+
 # line-of-sight never crosses latitude {0}
 LINE_OF_SIGHT_NEVER_CROSSES_LATITUDE = line-of-sight never crosses latitude {0}
 
diff --git a/core/src/main/resources/assets/org/orekit/rugged/RuggedMessages_fr.utf8 b/core/src/main/resources/assets/org/orekit/rugged/RuggedMessages_fr.utf8
index 825795b4..dbbd3973 100644
--- a/core/src/main/resources/assets/org/orekit/rugged/RuggedMessages_fr.utf8
+++ b/core/src/main/resources/assets/org/orekit/rugged/RuggedMessages_fr.utf8
@@ -19,6 +19,9 @@ EMPTY_TILE = la tuile est vide : {0} ⨉ {1}
 # unknown sensor {0}
 UNKNOWN_SENSOR = capteur {0} inconnu
 
+# line-of-sight does not reach ground
+LINE_OF_SIGHT_DOES_NOT_REACH_GROUND = la ligne de visée n''atteint pas le sol
+
 # line-of-sight never crosses latitude {0}
 LINE_OF_SIGHT_NEVER_CROSSES_LATITUDE = la ligne de visée ne franchit jamais la latitude {0}
 
diff --git a/core/src/test/java/org/orekit/rugged/api/RuggedMessagesTest.java b/core/src/test/java/org/orekit/rugged/api/RuggedMessagesTest.java
index abde2c08..9b7d0608 100644
--- a/core/src/test/java/org/orekit/rugged/api/RuggedMessagesTest.java
+++ b/core/src/test/java/org/orekit/rugged/api/RuggedMessagesTest.java
@@ -29,7 +29,7 @@ public class RuggedMessagesTest {
 
     @Test
     public void testMessageNumber() {
-        Assert.assertEquals(13, RuggedMessages.values().length);
+        Assert.assertEquals(14, RuggedMessages.values().length);
     }
 
     @Test
diff --git a/core/src/test/java/org/orekit/rugged/intersection/duvenhage/DuvenhageAlgorithmTest.java b/core/src/test/java/org/orekit/rugged/intersection/duvenhage/DuvenhageAlgorithmTest.java
index 8c5bc03b..ce1e1f9c 100644
--- a/core/src/test/java/org/orekit/rugged/intersection/duvenhage/DuvenhageAlgorithmTest.java
+++ b/core/src/test/java/org/orekit/rugged/intersection/duvenhage/DuvenhageAlgorithmTest.java
@@ -18,10 +18,12 @@ package org.orekit.rugged.intersection.duvenhage;
 
 
 import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
+import org.junit.Assert;
 import org.junit.Test;
 import org.orekit.bodies.GeodeticPoint;
 import org.orekit.errors.OrekitException;
 import org.orekit.rugged.api.RuggedException;
+import org.orekit.rugged.api.RuggedMessages;
 import org.orekit.rugged.intersection.AbstractAlgorithmTest;
 import org.orekit.rugged.intersection.IntersectionAlgorithm;
 import org.orekit.rugged.intersection.duvenhage.DuvenhageAlgorithm;
@@ -55,4 +57,18 @@ public class DuvenhageAlgorithmTest extends AbstractAlgorithmTest {
         checkIntersection(position, los, intersection);
     }
 
+    @Test
+    public void testWrongPositionMissesGround() throws RuggedException, OrekitException {
+        setUpMayonVolcanoContext();
+        final IntersectionAlgorithm algorithm = createAlgorithm(updater, 8);
+        Vector3D position = new Vector3D(7.551889113912788E9, -3.173692685491814E10, 1.5727517321541348E9);
+        Vector3D los = new Vector3D(0.010401349221417867, -0.17836068905951286, 0.9839101973923178);
+        try {
+            algorithm.intersection(earth, position, los);
+            Assert.fail("an exception should have been thrown");
+        } catch (RuggedException re) {
+            Assert.assertEquals(RuggedMessages.LINE_OF_SIGHT_DOES_NOT_REACH_GROUND, re.getSpecifier());
+        }
+    }
+
 }
diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml
index 2751dc15..1c42211d 100644
--- a/src/site/xdoc/changes.xml
+++ b/src/site/xdoc/changes.xml
@@ -22,6 +22,9 @@
   <body>
     <release version="1.0" date="TBD"
              description="TBD">
+      <action dev="luc" type="fix">
+        Added detection of wrong position/line-of-sight that misses the ground.
+      </action>
       <action dev="luc" type="add">
         Added a way to reuse transform interpolator from one run to another
         by dumping its state into a file, thus avoiding costly initialization.
-- 
GitLab