GeographicZoneDetector.java

  1. /* Copyright 2002-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.propagation.events;

  18. import org.hipparchus.geometry.enclosing.EnclosingBall;
  19. import org.hipparchus.geometry.spherical.twod.S2Point;
  20. import org.hipparchus.geometry.spherical.twod.Sphere2D;
  21. import org.hipparchus.geometry.spherical.twod.SphericalPolygonsSet;
  22. import org.hipparchus.util.FastMath;
  23. import org.orekit.bodies.BodyShape;
  24. import org.orekit.bodies.GeodeticPoint;
  25. import org.orekit.propagation.SpacecraftState;
  26. import org.orekit.propagation.events.handlers.EventHandler;
  27. import org.orekit.propagation.events.handlers.StopOnIncreasing;

  28. /** Detector for entry/exit of a zone defined by geographic boundaries.
  29.  * <p>This detector identifies when a spacecraft crosses boundaries of
  30.  * general shapes defined on the surface of the globe. Typical shapes
  31.  * of interest can be countries, land masses or physical areas like
  32.  * the south atlantic anomaly. Shapes can be arbitrarily complicated:
  33.  * convex or non-convex, in one piece or several non-connected islands,
  34.  * they can include poles, they can have holes like the Caspian Sea (this
  35.  * would be a hole only if one is interested in land masses, of course).
  36.  * Complex shapes involve of course more computing time than simple shapes.</p>
  37.  * @see FootprintOverlapDetector
  38.  * @author Luc Maisonobe
  39.  * @since 6.2
  40.  */
  41. public class GeographicZoneDetector extends AbstractDetector<GeographicZoneDetector> {

  42.     /** Body on which the geographic zone is defined. */
  43.     private BodyShape body;

  44.     /** Zone definition. */
  45.     private final SphericalPolygonsSet zone;

  46.     /** Spherical cap surrounding the zone. */
  47.     private final EnclosingBall<Sphere2D, S2Point> cap;

  48.     /** Margin to apply to the zone. */
  49.     private final double margin;

  50.     /** Build a new detector.
  51.      * <p>The new instance uses default values for maximal checking interval
  52.      * ({@link #DEFAULT_MAX_CHECK}) and convergence threshold ({@link
  53.      * #DEFAULT_THRESHOLD}).</p>
  54.      * @param body body on which the geographic zone is defined
  55.      * @param zone geographic zone to consider
  56.      * @param margin angular margin to apply to the zone
  57.      */
  58.     public GeographicZoneDetector(final BodyShape body,
  59.                                   final SphericalPolygonsSet zone,  final double margin) {
  60.         this(DEFAULT_MAX_CHECK, DEFAULT_THRESHOLD, body, zone, margin);
  61.     }

  62.     /** Build a detector.
  63.      * @param maxCheck maximal checking interval (s)
  64.      * @param threshold convergence threshold (s)
  65.      * @param body body on which the geographic zone is defined
  66.      * @param zone geographic zone to consider
  67.      * @param margin angular margin to apply to the zone
  68.      */
  69.     public GeographicZoneDetector(final double maxCheck, final double threshold,
  70.                                   final BodyShape body,
  71.                                   final SphericalPolygonsSet zone,  final double margin) {
  72.         this(new EventDetectionSettings(maxCheck, threshold, DEFAULT_MAX_ITER), new StopOnIncreasing(),
  73.              body, zone, zone.getEnclosingCap(), margin);
  74.     }

  75.     /** Protected constructor with full parameters.
  76.      * <p>
  77.      * This constructor is not public as users are expected to use the builder
  78.      * API with the various {@code withXxx()} methods to set up the instance
  79.      * in a readable manner without using a huge amount of parameters.
  80.      * </p>
  81.      * @param detectionSettings event detection settings
  82.      * @param handler event handler to call at event occurrences
  83.      * @param body body on which the geographic zone is defined
  84.      * @param zone geographic zone to consider
  85.      * @param cap spherical cap surrounding the zone
  86.      * @param margin angular margin to apply to the zone
  87.      * @since 13.0
  88.      */
  89.     protected GeographicZoneDetector(final EventDetectionSettings detectionSettings, final EventHandler handler,
  90.                                      final BodyShape body,
  91.                                      final SphericalPolygonsSet zone,
  92.                                      final EnclosingBall<Sphere2D, S2Point> cap,
  93.                                      final double margin) {
  94.         super(detectionSettings, handler);
  95.         this.body   = body;
  96.         this.zone   = zone;
  97.         this.cap    = cap;
  98.         this.margin = margin;
  99.     }

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     protected GeographicZoneDetector create(final EventDetectionSettings detectionSettings, final EventHandler newHandler) {
  103.         return new GeographicZoneDetector(detectionSettings, newHandler,
  104.                                           body, zone, cap, margin);
  105.     }

  106.     /**
  107.      * Setup the detector margin.
  108.      * @param newMargin angular margin to apply to the zone
  109.      * @return a new detector with updated configuration (the instance is not changed)
  110.      */
  111.     public GeographicZoneDetector withMargin(final double newMargin) {
  112.         return new GeographicZoneDetector(getDetectionSettings(), getHandler(), body, zone, cap, newMargin);
  113.     }

  114.     /** Get the body on which the geographic zone is defined.
  115.      * @return body on which the geographic zone is defined
  116.      */
  117.     public BodyShape getBody() {
  118.         return body;
  119.     }

  120.     /** Get the geographic zone.
  121.      * @return the geographic zone
  122.      */
  123.     public SphericalPolygonsSet getZone() {
  124.         return zone;
  125.     }

  126.     /** Get the angular margin to apply (radians).
  127.      * @return the angular margin to apply (radians)
  128.      */
  129.     public double getMargin() {
  130.         return margin;
  131.     }

  132.     /** Compute the value of the detection function.
  133.      * <p>
  134.      * The value is the signed distance to boundary, minus the margin. It is
  135.      * positive if the spacecraft is outside of the zone and negative if it is inside.
  136.      * </p>
  137.      * @param s the current state information: date, kinematics, attitude
  138.      * @return signed distance to boundary minus the margin
  139.      */
  140.     public double g(final SpacecraftState s) {

  141.         // convert state to geodetic coordinates
  142.         final GeodeticPoint gp = body.transform(s.getPosition(),
  143.                                                 s.getFrame(), s.getDate());

  144.         // map the point to a sphere (geodetic coordinates have already taken care of ellipsoid flatness)
  145.         final S2Point s2p = new S2Point(gp.getLongitude(), 0.5 * FastMath.PI - gp.getLatitude());

  146.         // for faster computation, we start using only the surrounding cap, to filter out
  147.         // far away points (which correspond to most of the points if the zone is small)
  148.         final double crudeDistance = cap.getCenter().distance(s2p) - cap.getRadius();
  149.         if (crudeDistance - margin > FastMath.max(FastMath.abs(margin), 0.01)) {
  150.             // we know we are strictly outside of the zone,
  151.             // use the crude distance to compute the (positive) return value
  152.             return crudeDistance - margin;
  153.         }

  154.         // we are close, we need to compute carefully the exact offset
  155.         // project the point to the closest zone boundary
  156.         return zone.projectToBoundary(s2p).getOffset() - margin;

  157.     }

  158. }