GeographicZoneDetector.java

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

  18. import java.io.Serializable;

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

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

  45.     /** Serializable UID. */
  46.     private static final long serialVersionUID = 20140619L;

  47.     /** Body on which the geographic zone is defined. */
  48.     private BodyShape body;

  49.     /** Zone definition. */
  50.     private final transient SphericalPolygonsSet zone;

  51.     /** Spherical cap surrounding the zone. */
  52.     private final transient EnclosingBall<Sphere2D, S2Point> cap;

  53.     /** Margin to apply to the zone. */
  54.     private final double margin;

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

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

  80.     /** Private constructor with full parameters.
  81.      * <p>
  82.      * This constructor is private as users are expected to use the builder
  83.      * API with the various {@code withXxx()} methods to set up the instance
  84.      * in a readable manner without using a huge amount of parameters.
  85.      * </p>
  86.      * @param maxCheck maximum checking interval (s)
  87.      * @param threshold convergence threshold (s)
  88.      * @param maxIter maximum number of iterations in the event time search
  89.      * @param handler event handler to call at event occurrences
  90.      * @param body body on which the geographic zone is defined
  91.      * @param zone geographic zone to consider
  92.      * @param cap spherical cap surrounding the zone
  93.      * @param margin angular margin to apply to the zone
  94.      */
  95.     private GeographicZoneDetector(final double maxCheck, final double threshold,
  96.                                    final int maxIter, final EventHandler<? super GeographicZoneDetector> handler,
  97.                                    final BodyShape body,
  98.                                    final SphericalPolygonsSet zone,
  99.                                    final EnclosingBall<Sphere2D, S2Point> cap,
  100.                                    final double margin) {
  101.         super(maxCheck, threshold, maxIter, handler);
  102.         this.body   = body;
  103.         this.zone   = zone;
  104.         this.cap    = cap;
  105.         this.margin = margin;
  106.     }

  107.     /** {@inheritDoc} */
  108.     @Override
  109.     protected GeographicZoneDetector create(final double newMaxCheck, final double newThreshold,
  110.                                             final int newMaxIter, final EventHandler<? super GeographicZoneDetector> newHandler) {
  111.         return new GeographicZoneDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  112.                                           body, zone, cap, margin);
  113.     }

  114.     /**
  115.      * Setup the detector margin.
  116.      * @param newMargin angular margin to apply to the zone
  117.      * @return a new detector with updated configuration (the instance is not changed)
  118.      */
  119.     public GeographicZoneDetector withMargin(final double newMargin) {
  120.         return new GeographicZoneDetector(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), getHandler(),
  121.                                           body, zone, cap, newMargin);
  122.     }

  123.     /** Get the body on which the geographic zone is defined.
  124.      * @return body on which the geographic zone is defined
  125.      */
  126.     public BodyShape getBody() {
  127.         return body;
  128.     }

  129.     /** Get the geographic zone.
  130.      * @return the geographic zone
  131.      */
  132.     public SphericalPolygonsSet getZone() {
  133.         return zone;
  134.     }

  135.     /** Get the angular margin to apply (radians).
  136.      * @return the angular margin to apply (radians)
  137.      */
  138.     public double getMargin() {
  139.         return margin;
  140.     }

  141.     /** Compute the value of the detection function.
  142.      * <p>
  143.      * The value is the signed distance to boundary, minus the margin. It is
  144.      * positive if the spacecraft is outside of the zone and negative if it is inside.
  145.      * </p>
  146.      * @param s the current state information: date, kinematics, attitude
  147.      * @return signed distance to boundary minus the margin
  148.      * @exception OrekitException if some specific error occurs
  149.      */
  150.     public double g(final SpacecraftState s) throws OrekitException {

  151.         // convert state to geodetic coordinates
  152.         final GeodeticPoint gp = body.transform(s.getPVCoordinates().getPosition(),
  153.                                                 s.getFrame(), s.getDate());

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

  156.         // for faster computation, we start using only the surrounding cap, to filter out
  157.         // far away points (which correspond to most of the points if the zone is small)
  158.         final double crudeDistance = cap.getCenter().distance(s2p) - cap.getRadius();
  159.         if (crudeDistance - margin > FastMath.max(FastMath.abs(margin), 0.01)) {
  160.             // we know we are strictly outside of the zone,
  161.             // use the crude distance to compute the (positive) return value
  162.             return crudeDistance - margin;
  163.         }

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

  167.     }

  168.     /** Replace the instance with a data transfer object for serialization.
  169.      * @return data transfer object that will be serialized
  170.      */
  171.     private Object writeReplace() {
  172.         return new DTO(this);
  173.     }

  174.     /** Internal class used only for serialization. */
  175.     private static class DTO implements Serializable {

  176.         /** Serializable UID. */
  177.         private static final long serialVersionUID = 20140619L;

  178.         /** Max check interval. */
  179.         private final double maxCheck;

  180.         /** Convergence threshold. */
  181.         private final double threshold;

  182.         /** Maximum number of iterations in the event time search. */
  183.         private final int maxIter;

  184.         /** Body on which the geographic zone is defined. */
  185.         private final BodyShape body;

  186.         /** Margin to apply to the zone. */
  187.         private final double margin;

  188.         /** Proxy for spherical polygons set. */
  189.         private final SphericalPolygonsSetTransferObject zone;

  190.         /** Simple constructor.
  191.          * @param detector instance to serialize
  192.          */
  193.         private DTO(final GeographicZoneDetector detector) {

  194.             this.maxCheck  = detector.getMaxCheckInterval();
  195.             this.threshold = detector.getThreshold();
  196.             this.maxIter   = detector.getMaxIterationCount();
  197.             this.body      = detector.body;
  198.             this.margin    = detector.margin;
  199.             this.zone      = new SphericalPolygonsSetTransferObject(detector.zone);
  200.         }

  201.         /** Replace the deserialized data transfer object with a {@link GeographicZoneDetector}.
  202.          * @return replacement {@link GeographicZoneDetector}
  203.          */
  204.         private Object readResolve() {
  205.             return new GeographicZoneDetector(body, zone.rebuildZone(), margin).
  206.                             withMaxCheck(maxCheck).
  207.                             withThreshold(threshold).
  208.                             withMaxIter(maxIter);
  209.         }

  210.     }

  211. }