BasicScanAlgorithm.java

  1. /* Copyright 2013-2025 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.      * @param isOverlappingTiles flag to tell if the DEM tiles are overlapping:
  55.      *                          true if overlapping; false otherwise.
  56.      */
  57.     public BasicScanAlgorithm(final TileUpdater updater, final int maxCachedTiles, final boolean isOverlappingTiles) {
  58.         this.cache = new TilesCache<>(new SimpleTileFactory(), updater, maxCachedTiles, isOverlappingTiles);
  59.         this.hMin  = Double.POSITIVE_INFINITY;
  60.         this.hMax  = Double.NEGATIVE_INFINITY;
  61.         this.algorithmId = AlgorithmId.BASIC_SLOW_EXHAUSTIVE_SCAN_FOR_TESTS_ONLY;
  62.     }

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

  67.         DumpManager.dumpAlgorithm(this.algorithmId);

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

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

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

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

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

  111.         }

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

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

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

  140.                     }
  141.                 }
  142.             }
  143.         }

  144.         return intersectionGP;
  145.     }

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

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

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

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

  184.         boolean changedMinMax = false;

  185.         for (final SimpleTile tile : tiles) {

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

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

  196.         }

  197.         return changedMinMax;

  198.     }

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

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

  210.         // the tile was not there, add it
  211.         list.add(tile);

  212.     }

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

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