FootprintOverlapDetector.java

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

  20. import org.hipparchus.geometry.enclosing.EnclosingBall;
  21. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  22. import org.hipparchus.geometry.spherical.twod.Edge;
  23. import org.hipparchus.geometry.spherical.twod.S2Point;
  24. import org.hipparchus.geometry.spherical.twod.Sphere2D;
  25. import org.hipparchus.geometry.spherical.twod.SphericalPolygonsSet;
  26. import org.hipparchus.geometry.spherical.twod.Vertex;
  27. import org.hipparchus.ode.events.Action;
  28. import org.hipparchus.util.FastMath;
  29. import org.hipparchus.util.SinCos;
  30. import org.orekit.bodies.BodyShape;
  31. import org.orekit.bodies.GeodeticPoint;
  32. import org.orekit.bodies.OneAxisEllipsoid;
  33. import org.orekit.frames.Transform;
  34. import org.orekit.geometry.fov.FieldOfView;
  35. import org.orekit.models.earth.tessellation.DivertedSingularityAiming;
  36. import org.orekit.models.earth.tessellation.EllipsoidTessellator;
  37. import org.orekit.propagation.SpacecraftState;
  38. import org.orekit.propagation.events.handlers.EventHandler;
  39. import org.orekit.propagation.events.handlers.StopOnIncreasing;

  40. /** Detector triggered by geographical region entering/leaving a spacecraft sensor
  41.  * {@link FieldOfView Field Of View}.
  42.  * <p>
  43.  * This detector is a mix between to {@link FieldOfViewDetector} and {@link
  44.  * GeographicZoneDetector}. Similar to the first detector above, it triggers events
  45.  * related to entry/exit of targets in a Field Of View, taking attitude into account.
  46.  * Similar to the second detector above, its target is an entire geographic region
  47.  * (which can even be split in several non-connected patches and can have holes).
  48.  * </p>
  49.  * <p>
  50.  * This detector is typically used for ground observation missions with agile
  51.  * satellites than can look away from nadir.
  52.  * </p>
  53.  * <p>The default implementation behavior is to {@link Action#CONTINUE continue}
  54.  * propagation at FOV entry and to {@link Action#STOP stop} propagation
  55.  * at FOV exit. This can be changed by calling
  56.  * {@link #withHandler(EventHandler)} after construction.</p>
  57.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  58.  * @see FieldOfViewDetector
  59.  * @see GeographicZoneDetector
  60.  * @author Luc Maisonobe
  61.  * @since 7.1
  62.  */
  63. public class FootprintOverlapDetector extends AbstractDetector<FootprintOverlapDetector> {

  64.     /** Field of view. */
  65.     private final FieldOfView fov;

  66.     /** Body on which the geographic zone is defined. */
  67.     private final OneAxisEllipsoid body;

  68.     /** Geographic zone to consider. */
  69.     private final SphericalPolygonsSet zone;

  70.     /** Linear step used for sampling the geographic zone. */
  71.     private final double samplingStep;

  72.     /** Sampling of the geographic zone. */
  73.     private final List<SamplingPoint> sampledZone;

  74.     /** Center of the spherical cap surrounding the zone. */
  75.     private final Vector3D capCenter;

  76.     /** Cosine of the radius of the spherical cap surrounding the zone. */
  77.     private final double capCos;

  78.     /** Sine of the radius of the spherical cap surrounding the zone. */
  79.     private final double capSin;

  80.     /** Build a new instance.
  81.      * <p>The maximal interval between distance to FOV boundary checks should
  82.      * be smaller than the half duration of the minimal pass to handle,
  83.      * otherwise some short passes could be missed.</p>
  84.      * @param fov sensor field of view
  85.      * @param body body on which the geographic zone is defined
  86.      * @param zone geographic zone to consider
  87.      * @param samplingStep linear step used for sampling the geographic zone (in meters)
  88.      * @deprecated as of 10.1, replaced by {@link #FootprintOverlapDetector(FieldOfView, OneAxisEllipsoid, SphericalPolygonsSet, double)}
  89.      */
  90.     @Deprecated
  91.     public FootprintOverlapDetector(final org.orekit.propagation.events.FieldOfView fov,
  92.                                     final OneAxisEllipsoid body,
  93.                                     final SphericalPolygonsSet zone,
  94.                                     final double samplingStep) {
  95.         this(DEFAULT_MAXCHECK, DEFAULT_THRESHOLD, DEFAULT_MAX_ITER,
  96.              new StopOnIncreasing<FootprintOverlapDetector>(),
  97.              fov, body, zone, samplingStep, sample(body, zone, samplingStep));
  98.     }

  99.     /** Build a new instance.
  100.      * <p>The maximal interval between distance to FOV boundary checks should
  101.      * be smaller than the half duration of the minimal pass to handle,
  102.      * otherwise some short passes could be missed.</p>
  103.      * @param fov sensor field of view
  104.      * @param body body on which the geographic zone is defined
  105.      * @param zone geographic zone to consider
  106.      * @param samplingStep linear step used for sampling the geographic zone (in meters)
  107.      * @since 10.1
  108.      */
  109.     public FootprintOverlapDetector(final FieldOfView fov,
  110.                                     final OneAxisEllipsoid body,
  111.                                     final SphericalPolygonsSet zone,
  112.                                     final double samplingStep) {
  113.         this(DEFAULT_MAXCHECK, DEFAULT_THRESHOLD, DEFAULT_MAX_ITER,
  114.              new StopOnIncreasing<FootprintOverlapDetector>(),
  115.              fov, body, zone, samplingStep, sample(body, zone, samplingStep));
  116.     }

  117.     /** Private constructor with full parameters.
  118.      * <p>
  119.      * This constructor is private as users are expected to use the builder
  120.      * API with the various {@code withXxx()} methods to set up the instance
  121.      * in a readable manner without using a huge amount of parameters.
  122.      * </p>
  123.      * @param maxCheck maximum checking interval (s)
  124.      * @param threshold convergence threshold (s)
  125.      * @param maxIter maximum number of iterations in the event time search
  126.      * @param handler event handler to call at event occurrences
  127.      * @param body body on which the geographic zone is defined
  128.      * @param zone geographic zone to consider
  129.      * @param fov sensor field of view
  130.      * @param sampledZone sampling of the geographic zone
  131.      * @param samplingStep linear step used for sampling the geographic zone (in meters)
  132.      */
  133.     private FootprintOverlapDetector(final double maxCheck, final double threshold,
  134.                                      final int maxIter, final EventHandler<? super FootprintOverlapDetector> handler,
  135.                                      final FieldOfView fov,
  136.                                      final OneAxisEllipsoid body,
  137.                                      final SphericalPolygonsSet zone,
  138.                                      final double samplingStep,
  139.                                      final List<SamplingPoint> sampledZone) {

  140.         super(maxCheck, threshold, maxIter, handler);
  141.         this.fov          = fov;
  142.         this.body         = body;
  143.         this.samplingStep = samplingStep;
  144.         this.zone         = zone;
  145.         this.sampledZone  = sampledZone;

  146.         final EnclosingBall<Sphere2D, S2Point> cap = zone.getEnclosingCap();
  147.         final SinCos sc = FastMath.sinCos(cap.getRadius());
  148.         this.capCenter    = cap.getCenter().getVector();
  149.         this.capCos       = sc.cos();
  150.         this.capSin       = sc.sin();

  151.     }

  152.     /** Sample the region.
  153.      * @param body body on which the geographic zone is defined
  154.      * @param zone geographic zone to consider
  155.      * @param samplingStep  linear step used for sampling the geographic zone (in meters)
  156.      * @return sampling points
  157.      */
  158.     private static List<SamplingPoint> sample(final OneAxisEllipsoid body,
  159.                                               final SphericalPolygonsSet zone,
  160.                                               final double samplingStep) {

  161.         final List<SamplingPoint> sampledZone = new ArrayList<SamplingPoint>();

  162.         // sample the zone boundary
  163.         final List<Vertex> boundary = zone.getBoundaryLoops();
  164.         for (final Vertex loopStart : boundary) {
  165.             int count = 0;
  166.             for (Vertex v = loopStart; count == 0 || v != loopStart; v = v.getOutgoing().getEnd()) {
  167.                 ++count;
  168.                 final Edge edge = v.getOutgoing();
  169.                 final int n = (int) FastMath.ceil(edge.getLength() * body.getEquatorialRadius() / samplingStep);
  170.                 for (int i = 0; i < n; ++i) {
  171.                     final S2Point intermediate = new S2Point(edge.getPointAt(i * edge.getLength() / n));
  172.                     final GeodeticPoint gp = new GeodeticPoint(0.5 * FastMath.PI - intermediate.getPhi(),
  173.                                                                intermediate.getTheta(), 0.0);
  174.                     sampledZone.add(new SamplingPoint(body.transform(gp), gp.getZenith()));
  175.                 }
  176.             }
  177.         }

  178.         // sample the zone interior
  179.         final EllipsoidTessellator tessellator =
  180.                         new EllipsoidTessellator(body, new DivertedSingularityAiming(zone), 1);
  181.         final List<List<GeodeticPoint>> gpSample = tessellator.sample(zone, samplingStep, samplingStep);
  182.         for (final List<GeodeticPoint> list : gpSample) {
  183.             for (final GeodeticPoint gp : list) {
  184.                 sampledZone.add(new SamplingPoint(body.transform(gp), gp.getZenith()));
  185.             }
  186.         }

  187.         return sampledZone;

  188.     }

  189.     /** {@inheritDoc} */
  190.     @Override
  191.     protected FootprintOverlapDetector create(final double newMaxCheck, final double newThreshold,
  192.                                               final int newMaxIter,
  193.                                               final EventHandler<? super FootprintOverlapDetector> newHandler) {
  194.         return new FootprintOverlapDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  195.                                             fov, body, zone, samplingStep, sampledZone);
  196.     }

  197.     /** Get the geographic zone triggering the events.
  198.      * <p>
  199.      * The zone is mapped on the unit sphere
  200.      * </p>
  201.      * @return geographic zone triggering the events
  202.      */
  203.     public SphericalPolygonsSet getZone() {
  204.         return zone;
  205.     }

  206.     /** Get the Field Of View.
  207.      * @return Field Of View
  208.      * @since 10.1
  209.      */
  210.     public FieldOfView getFOV() {
  211.         return fov;
  212.     }

  213.     /** Get the Field Of View.
  214.      * @return Field Of View, if detector has been built from a
  215.      * {@link org.orekit.propagation.events.FieldOfView}, or null of the
  216.      * detector was built from another implementation of {@link FieldOfView}
  217.      * @deprecated as of 10.1, replaced by {@link #getFOV()}
  218.      */
  219.     @Deprecated
  220.     public org.orekit.propagation.events.FieldOfView getFieldOfView() {
  221.         return fov instanceof org.orekit.propagation.events.FieldOfView ?
  222.                (org.orekit.propagation.events.FieldOfView) fov :
  223.                null;
  224.     }

  225.     /** Get the body on which the geographic zone is defined.
  226.      * @return body on which the geographic zone is defined
  227.      */
  228.     public BodyShape getBody() {
  229.         return body;
  230.     }

  231.     /** {@inheritDoc}
  232.      * <p>
  233.      * The g function value is the minimum offset among the region points
  234.      * with respect to the Field Of View boundary. It is positive if all region
  235.      * points are outside of the Field Of View, and negative if at least some
  236.      * of the region points are inside of the Field Of View. The minimum is
  237.      * computed by sampling the region, considering only the points for which
  238.      * the spacecraft is above the horizon. The accuracy of the detection
  239.      * depends on the linear sampling step set at detector construction. If
  240.      * the spacecraft is below horizon for all region points, an arbitrary
  241.      * positive value is returned.
  242.      * </p>
  243.      * <p>
  244.      * As per the previous definition, when the region enters the Field Of
  245.      * View, a decreasing event is generated, and when the region leaves
  246.      * the Field Of View, an increasing event is generated.
  247.      * </p>
  248.      */
  249.     public double g(final SpacecraftState s) {

  250.         // initial arbitrary positive value
  251.         double value = FastMath.PI;

  252.         // get spacecraft position in body frame
  253.         final Vector3D      scBody      = s.getPVCoordinates(body.getBodyFrame()).getPosition();

  254.         // map the point to a sphere
  255.         final GeodeticPoint gp          = body.transform(scBody, body.getBodyFrame(), s.getDate());
  256.         final S2Point       s2p         = new S2Point(gp.getLongitude(), 0.5 * FastMath.PI - gp.getLatitude());

  257.         // for faster computation, we start using only the surrounding cap, to filter out
  258.         // far away points (which correspond to most of the points if the zone is small)
  259.         final Vector3D p   = s2p.getVector();
  260.         final double   dot = Vector3D.dotProduct(p, capCenter);
  261.         if (dot < capCos) {
  262.             // the spacecraft is outside of the cap, look for the closest cap point
  263.             final Vector3D t     = p.subtract(dot, capCenter).normalize();
  264.             final Vector3D close = new Vector3D(capCos, capCenter, capSin, t);
  265.             if (Vector3D.dotProduct(p, close) < -0.01) {
  266.                 // the spacecraft is not visible from the cap edge,
  267.                 // even taking some margin into account for sphere/ellipsoid different shapes
  268.                 // this induces no points in the zone can see the spacecraft,
  269.                 // we can return the arbitrary initial positive value without performing further computation
  270.                 return value;
  271.             }
  272.         }

  273.         // the spacecraft may be visible from some points in the zone, check them all
  274.         final Transform bodyToSc = new Transform(s.getDate(),
  275.                                                  body.getBodyFrame().getTransformTo(s.getFrame(), s.getDate()),
  276.                                                  s.toTransform());
  277.         for (final SamplingPoint point : sampledZone) {
  278.             final Vector3D lineOfSightBody = point.getPosition().subtract(scBody);
  279.             if (Vector3D.dotProduct(lineOfSightBody, point.getZenith()) <= 0) {
  280.                 // spacecraft is above this sample point local horizon
  281.                 // get line of sight in spacecraft frame
  282.                 final double offset = fov.offsetFromBoundary(bodyToSc.transformVector(lineOfSightBody),
  283.                                                              0.0, VisibilityTrigger.VISIBLE_ONLY_WHEN_FULLY_IN_FOV);
  284.                 value = FastMath.min(value, offset);
  285.             }
  286.         }

  287.         return value;

  288.     }

  289.     /** Container for sampling points. */
  290.     private static class SamplingPoint {

  291.         /** Position of the point. */
  292.         private final Vector3D position;

  293.         /** Zenith vector of the point. */
  294.         private final Vector3D zenith;

  295.         /** Simple constructor.
  296.          * @param position position of the point
  297.          * @param zenith zenith vector of the point
  298.          */
  299.         SamplingPoint(final Vector3D position, final Vector3D zenith) {
  300.             this.position = position;
  301.             this.zenith   = zenith;
  302.         }

  303.         /** Get the point position.
  304.          * @return point position
  305.          */
  306.         public Vector3D getPosition() {
  307.             return position;
  308.         }

  309.         /** Get the point zenith vector.
  310.          * @return point zenith vector
  311.          */
  312.         public Vector3D getZenith() {
  313.             return zenith;
  314.         }

  315.     }

  316. }