FootprintOverlapDetector.java

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

  21. import org.hipparchus.geometry.enclosing.EnclosingBall;
  22. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  23. import org.hipparchus.geometry.spherical.twod.Edge;
  24. import org.hipparchus.geometry.spherical.twod.S2Point;
  25. import org.hipparchus.geometry.spherical.twod.Sphere2D;
  26. import org.hipparchus.geometry.spherical.twod.SphericalPolygonsSet;
  27. import org.hipparchus.geometry.spherical.twod.Vertex;
  28. import org.hipparchus.util.FastMath;
  29. import org.orekit.bodies.BodyShape;
  30. import org.orekit.bodies.GeodeticPoint;
  31. import org.orekit.bodies.OneAxisEllipsoid;
  32. import org.orekit.errors.OrekitException;
  33. import org.orekit.errors.OrekitInternalError;
  34. import org.orekit.frames.Transform;
  35. import org.orekit.models.earth.tessellation.ConstantAzimuthAiming;
  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. import org.orekit.utils.SphericalPolygonsSetTransferObject;

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

  67.     /** Serializable UID. */
  68.     private static final long serialVersionUID = 20150112L;

  69.     /** Field of view. */
  70.     private final transient FieldOfView fov;

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

  73.     /** Geographic zone to consider. */
  74.     private final transient SphericalPolygonsSet zone;

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

  77.     /** Sampling of the geographic zone. */
  78.     private final transient List<SamplingPoint> sampledZone;

  79.     /** Center of the spherical cap surrounding the zone. */
  80.     private final transient Vector3D capCenter;

  81.     /** Cosine of the radius of the spherical cap surrounding the zone. */
  82.     private final transient double capCos;

  83.     /** Sine of the radius of the spherical cap surrounding the zone. */
  84.     private final transient double capSin;

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

  102.     /** Private constructor with full parameters.
  103.      * <p>
  104.      * This constructor is private as users are expected to use the builder
  105.      * API with the various {@code withXxx()} methods to set up the instance
  106.      * in a readable manner without using a huge amount of parameters.
  107.      * </p>
  108.      * @param maxCheck maximum checking interval (s)
  109.      * @param threshold convergence threshold (s)
  110.      * @param maxIter maximum number of iterations in the event time search
  111.      * @param handler event handler to call at event occurrences
  112.      * @param body body on which the geographic zone is defined
  113.      * @param zone geographic zone to consider
  114.      * @param fov sensor field of view
  115.      * @param sampledZone sampling of the geographic zone
  116.      * @param samplingStep linear step used for sampling the geographic zone (in meters)
  117.      */
  118.     private FootprintOverlapDetector(final double maxCheck, final double threshold,
  119.                                      final int maxIter, final EventHandler<? super FootprintOverlapDetector> handler,
  120.                                      final FieldOfView fov,
  121.                                      final OneAxisEllipsoid body,
  122.                                      final SphericalPolygonsSet zone,
  123.                                      final double samplingStep,
  124.                                      final List<SamplingPoint> sampledZone) {

  125.         super(maxCheck, threshold, maxIter, handler);
  126.         this.fov          = fov;
  127.         this.body         = body;
  128.         this.samplingStep = samplingStep;
  129.         this.zone         = zone;
  130.         this.sampledZone  = sampledZone;

  131.         final EnclosingBall<Sphere2D, S2Point> cap = zone.getEnclosingCap();
  132.         this.capCenter    = cap.getCenter().getVector();
  133.         this.capCos       = FastMath.cos(cap.getRadius());
  134.         this.capSin       = FastMath.sin(cap.getRadius());

  135.     }

  136.     /** Sample the region.
  137.      * @param body body on which the geographic zone is defined
  138.      * @param zone geographic zone to consider
  139.      * @param samplingStep  linear step used for sampling the geographic zone (in meters)
  140.      * @return sampling points
  141.      */
  142.     private static List<SamplingPoint> sample(final OneAxisEllipsoid body,
  143.                                               final SphericalPolygonsSet zone,
  144.                                               final double samplingStep) {

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

  146.         // sample the zone boundary
  147.         final List<Vertex> boundary = zone.getBoundaryLoops();
  148.         for (final Vertex loopStart : boundary) {
  149.             int count = 0;
  150.             for (Vertex v = loopStart; count == 0 || v != loopStart; v = v.getOutgoing().getEnd()) {
  151.                 ++count;
  152.                 final Edge edge = v.getOutgoing();
  153.                 final int n = (int) FastMath.ceil(edge.getLength() * body.getEquatorialRadius() / samplingStep);
  154.                 for (int i = 0; i < n; ++i) {
  155.                     final S2Point intermediate = new S2Point(edge.getPointAt(i * edge.getLength() / n));
  156.                     final GeodeticPoint gp = new GeodeticPoint(0.5 * FastMath.PI - intermediate.getPhi(),
  157.                                                                intermediate.getTheta(), 0.0);
  158.                     sampledZone.add(new SamplingPoint(body.transform(gp), gp.getZenith()));
  159.                 }
  160.             }
  161.         }

  162.         // sample the zone interior
  163.         final EllipsoidTessellator tessellator =
  164.                         new EllipsoidTessellator(body, new ConstantAzimuthAiming(body, 0.0), 4);
  165.         final List<List<GeodeticPoint>> gpSample = tessellator.sample(zone, samplingStep, samplingStep);
  166.         for (final List<GeodeticPoint> list : gpSample) {
  167.             for (final GeodeticPoint gp : list) {
  168.                 sampledZone.add(new SamplingPoint(body.transform(gp), gp.getZenith()));
  169.             }
  170.         }

  171.         return sampledZone;

  172.     }

  173.     /** {@inheritDoc} */
  174.     @Override
  175.     protected FootprintOverlapDetector create(final double newMaxCheck, final double newThreshold,
  176.                                               final int newMaxIter,
  177.                                               final EventHandler<? super FootprintOverlapDetector> newHandler) {
  178.         return new FootprintOverlapDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  179.                                             fov, body, zone, samplingStep, sampledZone);
  180.     }

  181.     /** Get the geographic zone triggering the events.
  182.      * <p>
  183.      * The zone is mapped on the unit sphere
  184.      * </p>
  185.      * @return geographic zone triggering the events
  186.      */
  187.     public SphericalPolygonsSet getZone() {
  188.         return zone;
  189.     }

  190.     /** Get the Field Of View.
  191.      * @return Field Of View
  192.      */
  193.     public FieldOfView getFieldOfView() {
  194.         return fov;
  195.     }

  196.     /** Get the body on which the geographic zone is defined.
  197.      * @return body on which the geographic zone is defined
  198.      */
  199.     public BodyShape getBody() {
  200.         return body;
  201.     }

  202.     /** {@inheritDoc}
  203.      * <p>
  204.      * The g function value is the minimum offset among the region points
  205.      * with respect to the Field Of View boundary. It is positive if all region
  206.      * points are outside of the Field Of View, and negative if at least some
  207.      * of the region points are inside of the Field Of View. The minimum is
  208.      * computed by sampling the region, considering only the points for which
  209.      * the spacecraft is above the horizon. The accuracy of the detection
  210.      * depends on the linear sampling step set at detector construction. If
  211.      * the spacecraft is below horizon for all region points, an arbitrary
  212.      * positive value is returned.
  213.      * </p>
  214.      * <p>
  215.      * As per the previous definition, when the region enters the Field Of
  216.      * View, a decreasing event is generated, and when the region leaves
  217.      * the Field Of View, an increasing event is generated.
  218.      * </p>
  219.      */
  220.     public double g(final SpacecraftState s) {

  221.         // initial arbitrary positive value
  222.         double value = FastMath.PI;

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

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

  228.         // for faster computation, we start using only the surrounding cap, to filter out
  229.         // far away points (which correspond to most of the points if the zone is small)
  230.         final Vector3D p   = s2p.getVector();
  231.         final double   dot = Vector3D.dotProduct(p, capCenter);
  232.         if (dot < capCos) {
  233.             // the spacecraft is outside of the cap, look for the closest cap point
  234.             final Vector3D t     = p.subtract(dot, capCenter).normalize();
  235.             final Vector3D close = new Vector3D(capCos, capCenter, capSin, t);
  236.             if (Vector3D.dotProduct(p, close) < -0.01) {
  237.                 // the spacecraft is not visible from the cap edge,
  238.                 // even taking some margin into account for sphere/ellipsoid different shapes
  239.                 // this induces no points in the zone can see the spacecraft,
  240.                 // we can return the arbitrary initial positive value without performing further computation
  241.                 return value;
  242.             }
  243.         }

  244.         // the spacecraft may be visible from some points in the zone, check them all
  245.         final Transform bodyToSc = new Transform(s.getDate(),
  246.                                                  body.getBodyFrame().getTransformTo(s.getFrame(), s.getDate()),
  247.                                                  s.toTransform());
  248.         for (final SamplingPoint point : sampledZone) {
  249.             final Vector3D lineOfSightBody = point.getPosition().subtract(scBody);
  250.             if (Vector3D.dotProduct(lineOfSightBody, point.getZenith()) <= 0) {
  251.                 // spacecraft is above this sample point local horizon
  252.                 // get line of sight in spacecraft frame
  253.                 final double offset = fov.offsetFromBoundary(bodyToSc.transformVector(lineOfSightBody));
  254.                 value = FastMath.min(value, offset);
  255.             }
  256.         }

  257.         return value;

  258.     }

  259.     /** Replace the instance with a data transfer object for serialization.
  260.      * @return data transfer object that will be serialized
  261.      */
  262.     private Object writeReplace() {
  263.         return new DTO(this);
  264.     }

  265.     /** Internal class used only for serialization. */
  266.     private static class DTO implements Serializable {

  267.         /** Serializable UID. */
  268.         private static final long serialVersionUID = 20150112L;

  269.         /** Max check interval. */
  270.         private final double maxCheck;

  271.         /** Convergence threshold. */
  272.         private final double threshold;

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

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

  277.         /** Field Of View. */
  278.         private final FieldOfView fov;

  279.         /** Proxy for geographic zone. */
  280.         private final SphericalPolygonsSetTransferObject zone;

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

  283.         /** Simple constructor.
  284.          * @param detector instance to serialize
  285.          */
  286.         private DTO(final FootprintOverlapDetector detector) {
  287.             this.maxCheck     = detector.getMaxCheckInterval();
  288.             this.threshold    = detector.getThreshold();
  289.             this.maxIter      = detector.getMaxIterationCount();
  290.             this.fov          = detector.fov;
  291.             this.body         = detector.body;
  292.             this.zone         = new SphericalPolygonsSetTransferObject(detector.zone);
  293.             this.samplingStep = detector.samplingStep;
  294.         }

  295.         /** Replace the deserialized data transfer object with a {@link FootprintOverlapDetector}.
  296.          * @return replacement {@link FootprintOverlapDetector}
  297.          */
  298.         private Object readResolve() {
  299.             try {
  300.                 return new FootprintOverlapDetector(fov, body, zone.rebuildZone(), samplingStep).
  301.                                 withMaxCheck(maxCheck).
  302.                                 withThreshold(threshold).
  303.                                 withMaxIter(maxIter);
  304.             } catch (OrekitException oe) {
  305.                 // this should never happen as the region as already been sampled before serialization
  306.                 throw new OrekitInternalError(oe);
  307.             }
  308.         }

  309.     }

  310.     /** Container for sampling points. */
  311.     private static class SamplingPoint {

  312.         /** Position of the point. */
  313.         private final Vector3D position;

  314.         /** Zenith vector of the point. */
  315.         private final Vector3D zenith;

  316.         /** Simple constructor.
  317.          * @param position position of the point
  318.          * @param zenith zenith vector of the point
  319.          */
  320.         SamplingPoint(final Vector3D position, final Vector3D zenith) {
  321.             this.position = position;
  322.             this.zenith   = zenith;
  323.         }

  324.         /** Get the point position.
  325.          * @return point position
  326.          */
  327.         public Vector3D getPosition() {
  328.             return position;
  329.         }

  330.         /** Get the point zenith vector.
  331.          * @return point zenith vector
  332.          */
  333.         public Vector3D getZenith() {
  334.             return zenith;
  335.         }

  336.     }

  337. }