BasicScanAlgorithm.java

  1. /* Copyright 2013-2019 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 java.util.ArrayList;
  19. import java.util.List;

  20. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  21. import org.hipparchus.util.FastMath;
  22. import org.orekit.bodies.GeodeticPoint;
  23. import org.orekit.rugged.api.AlgorithmId;
  24. import org.orekit.rugged.errors.DumpManager;
  25. import org.orekit.rugged.raster.SimpleTile;
  26. import org.orekit.rugged.raster.SimpleTileFactory;
  27. import org.orekit.rugged.raster.Tile;
  28. import org.orekit.rugged.raster.TileUpdater;
  29. import org.orekit.rugged.raster.TilesCache;
  30. import org.orekit.rugged.utils.ExtendedEllipsoid;
  31. import org.orekit.rugged.utils.NormalizedGeodeticPoint;

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

  41.     /** Cache for DEM tiles. */
  42.     private final TilesCache<SimpleTile> cache;

  43.     /** Minimum altitude encountered. */
  44.     private double hMin;

  45.     /** Maximum altitude encountered. */
  46.     private double hMax;

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

  56.     /** {@inheritDoc} */
  57.     @Override
  58.     public NormalizedGeodeticPoint intersection(final ExtendedEllipsoid ellipsoid,
  59.             final Vector3D position, final Vector3D los) {

  60.         DumpManager.dumpAlgorithm(AlgorithmId.BASIC_SLOW_EXHAUSTIVE_SCAN_FOR_TESTS_ONLY);

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

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

  83.             exitPoint = ellipsoid.transform(ellipsoid.pointAtAltitude(position, los, Double.isInfinite(hMin) ? 0.0 : hMin),
  84.                     ellipsoid.getBodyFrame(), null, centralLongitude);
  85.             final SimpleTile exitTile = cache.getTile(exitPoint.getLatitude(), exitPoint.getLongitude());
  86.             addIfNotPresent(scannedTiles, exitTile);

  87.             minLatitude  = FastMath.min(entryPoint.getLatitude(),  exitPoint.getLatitude());
  88.             maxLatitude  = FastMath.max(entryPoint.getLatitude(),  exitPoint.getLatitude());
  89.             minLongitude = FastMath.min(entryPoint.getLongitude(), exitPoint.getLongitude());
  90.             maxLongitude = FastMath.max(entryPoint.getLongitude(), exitPoint.getLongitude());

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

  104.         }

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

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

  125.                         if (gpImproved != null) {
  126.                             final Vector3D point = ellipsoid.transform(gpImproved);
  127.                             final double dot = Vector3D.dotProduct(point.subtract(position), los);
  128.                             if (dot < intersectionDot) {
  129.                                 intersectionGP  = gpImproved;
  130.                                 intersectionDot = dot;
  131.                             }
  132.                         }

  133.                     }
  134.                 }
  135.             }
  136.         }

  137.         return intersectionGP;
  138.     }

  139.     /** {@inheritDoc} */
  140.     @Override
  141.     public NormalizedGeodeticPoint refineIntersection(final ExtendedEllipsoid ellipsoid,
  142.                                                       final Vector3D position, final Vector3D los,
  143.                                                       final NormalizedGeodeticPoint closeGuess) {
  144.         DumpManager.dumpAlgorithm(AlgorithmId.BASIC_SLOW_EXHAUSTIVE_SCAN_FOR_TESTS_ONLY);
  145.         final Vector3D      delta     = ellipsoid.transform(closeGuess).subtract(position);
  146.         final double        s         = Vector3D.dotProduct(delta, los) / los.getNormSq();
  147.         final GeodeticPoint projected = ellipsoid.transform(new Vector3D(1, position, s, los),
  148.                 ellipsoid.getBodyFrame(), null);
  149.         final NormalizedGeodeticPoint normalizedProjected = new NormalizedGeodeticPoint(projected.getLatitude(),
  150.                 projected.getLongitude(),
  151.                 projected.getAltitude(),
  152.                 closeGuess.getLongitude());
  153.         final Tile          tile      = cache.getTile(normalizedProjected.getLatitude(),
  154.                 normalizedProjected.getLongitude());
  155.         return tile.cellIntersection(normalizedProjected,
  156.                 ellipsoid.convertLos(normalizedProjected, los),
  157.                 tile.getFloorLatitudeIndex(normalizedProjected.getLatitude()),
  158.                 tile.getFloorLongitudeIndex(normalizedProjected.getLongitude()));
  159.     }

  160.     /** {@inheritDoc} */
  161.     @Override
  162.     public double getElevation(final double latitude, final double longitude) {
  163.         DumpManager.dumpAlgorithm(AlgorithmId.BASIC_SLOW_EXHAUSTIVE_SCAN_FOR_TESTS_ONLY);
  164.         final Tile tile = cache.getTile(latitude, longitude);
  165.         return tile.interpolateElevation(latitude, longitude);
  166.     }

  167.     /** Check the overall min and max altitudes.
  168.      * @param tiles tiles to check
  169.      * @return true if the tile changed either min or max altitude
  170.      */
  171.     private boolean checkMinMax(final List<SimpleTile> tiles) {

  172.         boolean changedMinMax = false;

  173.         for (final SimpleTile tile : tiles) {

  174.             // check minimum altitude
  175.             if (tile.getMinElevation() < hMin) {
  176.                 hMin          = tile.getMinElevation();
  177.                 changedMinMax = true;
  178.             }

  179.             // check maximum altitude
  180.             if (tile.getMaxElevation() > hMax) {
  181.                 hMax          = tile.getMaxElevation();
  182.                 changedMinMax = true;
  183.             }

  184.         }

  185.         return changedMinMax;

  186.     }

  187.     /** Add a tile to a list if not already present.
  188.      * @param list tiles list
  189.      * @param tile new tile to consider
  190.      */
  191.     private void addIfNotPresent(final List<SimpleTile> list, final SimpleTile tile) {

  192.         // look for existing tiles in the list
  193.         for (final SimpleTile existing : list) {
  194.             if (existing == tile) {
  195.                 return;
  196.             }
  197.         }

  198.         // the tile was not there, add it
  199.         list.add(tile);

  200.     }

  201.     /** Get latitude index.
  202.      * @param tile current tile
  203.      * @param latitude current latitude
  204.      * @return index of latitude, truncated at tiles limits
  205.      */
  206.     private int latitudeIndex(final SimpleTile tile, final double latitude) {
  207.         final int rawIndex = tile.getFloorLatitudeIndex(latitude);
  208.         return FastMath.min(FastMath.max(0, rawIndex), tile.getLatitudeRows());
  209.     }

  210.     /** Get longitude index.
  211.      * @param tile current tile
  212.      * @param longitude current longitude
  213.      * @return index of longitude, truncated at tiles limits
  214.      */
  215.     private int longitudeIndex(final SimpleTile tile, final double longitude) {
  216.         final int rawIndex = tile.getFloorLongitudeIndex(longitude);
  217.         return FastMath.min(FastMath.max(0, rawIndex), tile.getLongitudeColumns());
  218.     }

  219. }