BasicScanAlgorithm.java

  1. /* Copyright 2013-2022 CS GROUP
  2.  * Licensed to CS GROUP (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.  * @author Guylaine Prat
  40.  */
  41. public class BasicScanAlgorithm implements IntersectionAlgorithm {

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

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

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

  48.     /** Algorithm Id.
  49.      * @since 2.2 */
  50.     private final AlgorithmId algorithmId;

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

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public NormalizedGeodeticPoint intersection(final ExtendedEllipsoid ellipsoid,
  64.             final Vector3D position, final Vector3D los) {

  65.         DumpManager.dumpAlgorithm(this.algorithmId);

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

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

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

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

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

  109.         }

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

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

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

  138.                     }
  139.                 }
  140.             }
  141.         }

  142.         return intersectionGP;
  143.     }

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

  165.     /** {@inheritDoc} */
  166.     @Override
  167.     public double getElevation(final double latitude, final double longitude) {
  168.         DumpManager.dumpAlgorithm(this.algorithmId);
  169.         final Tile tile = cache.getTile(latitude, longitude);
  170.         return tile.interpolateElevation(latitude, longitude);
  171.     }

  172.     /** {@inheritDoc} */
  173.     @Override
  174.     public AlgorithmId getAlgorithmId() {
  175.         return this.algorithmId;
  176.     }

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

  182.         boolean changedMinMax = false;

  183.         for (final SimpleTile tile : tiles) {

  184.             // check minimum altitude
  185.             if (tile.getMinElevation() < hMin) {
  186.                 hMin          = tile.getMinElevation();
  187.                 changedMinMax = true;
  188.             }

  189.             // check maximum altitude
  190.             if (tile.getMaxElevation() > hMax) {
  191.                 hMax          = tile.getMaxElevation();
  192.                 changedMinMax = true;
  193.             }

  194.         }

  195.         return changedMinMax;

  196.     }

  197.     /** Add a tile to a list if not already present.
  198.      * @param list tiles list
  199.      * @param tile new tile to consider
  200.      */
  201.     private void addIfNotPresent(final List<SimpleTile> list, final SimpleTile tile) {

  202.         // look for existing tiles in the list
  203.         for (final SimpleTile existing : list) {
  204.             if (existing == tile) {
  205.                 return;
  206.             }
  207.         }

  208.         // the tile was not there, add it
  209.         list.add(tile);

  210.     }

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

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