BasicScanAlgorithm.java

  1. /* Copyright 2013-2017 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.rugged.intersection;

  18. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  19. import org.hipparchus.util.FastMath;
  20. import java.util.ArrayList;
  21. import java.util.List;

  22. import org.orekit.bodies.GeodeticPoint;
  23. import org.orekit.errors.OrekitException;
  24. import org.orekit.rugged.api.AlgorithmId;
  25. import org.orekit.rugged.errors.DumpManager;
  26. import org.orekit.rugged.errors.RuggedException;
  27. import org.orekit.rugged.raster.SimpleTile;
  28. import org.orekit.rugged.raster.SimpleTileFactory;
  29. import org.orekit.rugged.raster.Tile;
  30. import org.orekit.rugged.raster.TileUpdater;
  31. import org.orekit.rugged.raster.TilesCache;
  32. import org.orekit.rugged.utils.ExtendedEllipsoid;
  33. import org.orekit.rugged.utils.NormalizedGeodeticPoint;

  34. /** Intersection computation using a basic algorithm based on exhaustive scan.
  35.  * <p>
  36.  * The algorithm simply computes entry and exit points at high and low altitudes,
  37.  * and scans all Digital Elevation Models in the sub-tiles defined by these two
  38.  * corner points. It is not designed for operational use.
  39.  * </p>
  40.  * @author Luc Maisonobe
  41.  */
  42. public class BasicScanAlgorithm implements IntersectionAlgorithm {

  43.     /** Cache for DEM tiles. */
  44.     private final TilesCache<SimpleTile> cache;

  45.     /** Minimum altitude encountered. */
  46.     private double hMin;

  47.     /** Maximum altitude encountered. */
  48.     private double hMax;

  49.     /** Simple constructor.
  50.      * @param updater updater used to load Digital Elevation Model tiles
  51.      * @param maxCachedTiles maximum number of tiles stored in the cache
  52.      */
  53.     public BasicScanAlgorithm(final TileUpdater updater, final int maxCachedTiles) {
  54.         cache = new TilesCache<SimpleTile>(new SimpleTileFactory(), updater, maxCachedTiles);
  55.         hMin  = Double.POSITIVE_INFINITY;
  56.         hMax  = Double.NEGATIVE_INFINITY;
  57.     }

  58.     /** {@inheritDoc} */
  59.     @Override
  60.     public NormalizedGeodeticPoint intersection(final ExtendedEllipsoid ellipsoid,
  61.                                                 final Vector3D position, final Vector3D los)
  62.         throws RuggedException {
  63.         try {

  64.             DumpManager.dumpAlgorithm(AlgorithmId.BASIC_SLOW_EXHAUSTIVE_SCAN_FOR_TESTS_ONLY);

  65.             // find the tiles between the entry and exit point in the Digital Elevation Model
  66.             NormalizedGeodeticPoint entryPoint = null;
  67.             NormalizedGeodeticPoint exitPoint  = null;
  68.             double minLatitude  = Double.NaN;
  69.             double maxLatitude  = Double.NaN;
  70.             double minLongitude = Double.NaN;
  71.             double maxLongitude = Double.NaN;
  72.             final List<SimpleTile> scannedTiles = new ArrayList<SimpleTile>();
  73.             double centralLongitude = Double.NaN;
  74.             for (boolean changedMinMax = true; changedMinMax; changedMinMax = checkMinMax(scannedTiles)) {

  75.                 scannedTiles.clear();
  76.                 // compute entry and exit points
  77.                 entryPoint = ellipsoid.transform(ellipsoid.pointAtAltitude(position, los, Double.isInfinite(hMax) ? 0.0 : hMax),
  78.                                                  ellipsoid.getBodyFrame(), null,
  79.                                                  Double.isNaN(centralLongitude) ? 0.0 : centralLongitude);
  80.                 final SimpleTile entryTile = cache.getTile(entryPoint.getLatitude(), entryPoint.getLongitude());
  81.                 if (Double.isNaN(centralLongitude)) {
  82.                     centralLongitude = entryTile.getMinimumLongitude();
  83.                     entryPoint = new NormalizedGeodeticPoint(entryPoint.getLatitude(), entryPoint.getLongitude(),
  84.                                                              entryPoint.getAltitude(), centralLongitude);
  85.                 }
  86.                 addIfNotPresent(scannedTiles, entryTile);

  87.                 exitPoint = ellipsoid.transform(ellipsoid.pointAtAltitude(position, los, Double.isInfinite(hMin) ? 0.0 : hMin),
  88.                                                 ellipsoid.getBodyFrame(), null, centralLongitude);
  89.                 final SimpleTile exitTile = cache.getTile(exitPoint.getLatitude(), exitPoint.getLongitude());
  90.                 addIfNotPresent(scannedTiles, exitTile);

  91.                 minLatitude  = FastMath.min(entryPoint.getLatitude(),  exitPoint.getLatitude());
  92.                 maxLatitude  = FastMath.max(entryPoint.getLatitude(),  exitPoint.getLatitude());
  93.                 minLongitude = FastMath.min(entryPoint.getLongitude(), exitPoint.getLongitude());
  94.                 maxLongitude = FastMath.max(entryPoint.getLongitude(), exitPoint.getLongitude());

  95.                 if (scannedTiles.size() > 1) {
  96.                     // the entry and exit tiles are different, maybe other tiles should be added on the way
  97.                     // in the spirit of simple and exhaustive, we add all tiles in a rectangular area
  98.                     final double latStep = 0.5 * FastMath.min(entryTile.getLatitudeStep()  * entryTile.getLatitudeRows(),
  99.                                                               exitTile.getLatitudeStep()   * exitTile.getLatitudeRows());
  100.                     final double lonStep = 0.5 * FastMath.min(entryTile.getLongitudeStep() * entryTile.getLongitudeColumns(),
  101.                                                               exitTile.getLongitudeStep()  * exitTile.getLongitudeColumns());
  102.                     for (double latitude = minLatitude; latitude <= maxLatitude; latitude += latStep) {
  103.                         for (double longitude = minLongitude; longitude < maxLongitude; longitude += lonStep) {
  104.                             addIfNotPresent(scannedTiles, cache.getTile(latitude, longitude));
  105.                         }
  106.                     }
  107.                 }

  108.             }

  109.             // scan the tiles
  110.             NormalizedGeodeticPoint intersectionGP = null;
  111.             double intersectionDot = Double.POSITIVE_INFINITY;
  112.             for (final SimpleTile tile : scannedTiles) {
  113.                 for (int i = latitudeIndex(tile, minLatitude); i <= latitudeIndex(tile, maxLatitude); ++i) {
  114.                     for (int j = longitudeIndex(tile, minLongitude); j <= longitudeIndex(tile, maxLongitude); ++j) {
  115.                         final NormalizedGeodeticPoint gp = tile.cellIntersection(entryPoint, ellipsoid.convertLos(entryPoint, los), i, j);
  116.                         if (gp != null) {

  117.                             // improve the point, by projecting it back on the 3D line, fixing the small body curvature at cell level
  118.                             final Vector3D      delta     = ellipsoid.transform(gp).subtract(position);
  119.                             final double        s         = Vector3D.dotProduct(delta, los) / los.getNormSq();
  120.                             final GeodeticPoint projected = ellipsoid.transform(new Vector3D(1, position, s, los),
  121.                                                                                 ellipsoid.getBodyFrame(), null);
  122.                             final NormalizedGeodeticPoint normalizedProjected = new NormalizedGeodeticPoint(projected.getLatitude(),
  123.                                                                                                             projected.getLongitude(),
  124.                                                                                                             projected.getAltitude(),
  125.                                                                                                             gp.getLongitude());
  126.                             final NormalizedGeodeticPoint gpImproved = tile.cellIntersection(normalizedProjected,
  127.                                                                                              ellipsoid.convertLos(normalizedProjected, los),
  128.                                                                                              i, j);

  129.                             if (gpImproved != null) {
  130.                                 final Vector3D point = ellipsoid.transform(gpImproved);
  131.                                 final double dot = Vector3D.dotProduct(point.subtract(position), los);
  132.                                 if (dot < intersectionDot) {
  133.                                     intersectionGP  = gpImproved;
  134.                                     intersectionDot = dot;
  135.                                 }
  136.                             }

  137.                         }
  138.                     }
  139.                 }
  140.             }

  141.             return intersectionGP;

  142.         } catch (OrekitException oe) {
  143.             // this should never happen
  144.             throw new RuggedException(oe, oe.getSpecifier(), oe.getParts());
  145.         }
  146.     }

  147.     /** {@inheritDoc} */
  148.     @Override
  149.     public NormalizedGeodeticPoint refineIntersection(final ExtendedEllipsoid ellipsoid,
  150.                                                       final Vector3D position, final Vector3D los,
  151.                                                       final NormalizedGeodeticPoint closeGuess)
  152.         throws RuggedException {
  153.         try {
  154.             DumpManager.dumpAlgorithm(AlgorithmId.BASIC_SLOW_EXHAUSTIVE_SCAN_FOR_TESTS_ONLY);
  155.             final Vector3D      delta     = ellipsoid.transform(closeGuess).subtract(position);
  156.             final double        s         = Vector3D.dotProduct(delta, los) / los.getNormSq();
  157.             final GeodeticPoint projected = ellipsoid.transform(new Vector3D(1, position, s, los),
  158.                                                                 ellipsoid.getBodyFrame(), null);
  159.             final NormalizedGeodeticPoint normalizedProjected = new NormalizedGeodeticPoint(projected.getLatitude(),
  160.                                                                                             projected.getLongitude(),
  161.                                                                                             projected.getAltitude(),
  162.                                                                                             closeGuess.getLongitude());
  163.             final Tile          tile      = cache.getTile(normalizedProjected.getLatitude(),
  164.                                                           normalizedProjected.getLongitude());
  165.             return tile.cellIntersection(normalizedProjected,
  166.                                          ellipsoid.convertLos(normalizedProjected, los),
  167.                                          tile.getFloorLatitudeIndex(normalizedProjected.getLatitude()),
  168.                                          tile.getFloorLongitudeIndex(normalizedProjected.getLongitude()));
  169.         } catch (OrekitException oe) {
  170.             throw new RuggedException(oe, oe.getSpecifier(), oe.getParts());
  171.         }
  172.     }

  173.     /** {@inheritDoc} */
  174.     @Override
  175.     public double getElevation(final double latitude, final double longitude)
  176.         throws RuggedException {
  177.         DumpManager.dumpAlgorithm(AlgorithmId.BASIC_SLOW_EXHAUSTIVE_SCAN_FOR_TESTS_ONLY);
  178.         final Tile tile = cache.getTile(latitude, longitude);
  179.         return tile.interpolateElevation(latitude, longitude);
  180.     }

  181.     /** Check the overall min and max altitudes.
  182.      * @param tiles tiles to check
  183.      * @return true if the tile changed either min or max altitude
  184.      */
  185.     private boolean checkMinMax(final List<SimpleTile> tiles) {

  186.         boolean changedMinMax = false;

  187.         for (final SimpleTile tile : tiles) {

  188.             // check minimum altitude
  189.             if (tile.getMinElevation() < hMin) {
  190.                 hMin          = tile.getMinElevation();
  191.                 changedMinMax = true;
  192.             }

  193.             // check maximum altitude
  194.             if (tile.getMaxElevation() > hMax) {
  195.                 hMax          = tile.getMaxElevation();
  196.                 changedMinMax = true;
  197.             }

  198.         }

  199.         return changedMinMax;

  200.     }

  201.     /** Add a tile to a list if not already present.
  202.      * @param list tiles list
  203.      * @param tile new tile to consider
  204.      */
  205.     private void addIfNotPresent(final List<SimpleTile> list, final SimpleTile tile) {

  206.         // look for existing tiles in the list
  207.         for (final SimpleTile existing : list) {
  208.             if (existing == tile) {
  209.                 return;
  210.             }
  211.         }

  212.         // the tile was not there, add it
  213.         list.add(tile);

  214.     }

  215.     /** Get latitude index.
  216.      * @param tile current tile
  217.      * @param latitude current latitude
  218.      * @return index of latitude, truncated at tiles limits
  219.      */
  220.     private int latitudeIndex(final SimpleTile tile, final double latitude) {
  221.         final int rawIndex = tile.getFloorLatitudeIndex(latitude);
  222.         return FastMath.min(FastMath.max(0, rawIndex), tile.getLatitudeRows());
  223.     }

  224.     /** Get longitude index.
  225.      * @param tile current tile
  226.      * @param longitude current longitude
  227.      * @return index of longitude, truncated at tiles limits
  228.      */
  229.     private int longitudeIndex(final SimpleTile tile, final double longitude) {
  230.         final int rawIndex = tile.getFloorLongitudeIndex(longitude);
  231.         return FastMath.min(FastMath.max(0, rawIndex), tile.getLongitudeColumns());
  232.     }

  233. }