FootprintOverlapDetector.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 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.      * @exception OrekitException if the geographic zone cannot be sampled
  94.      */
  95.     public FootprintOverlapDetector(final FieldOfView fov,
  96.                                     final OneAxisEllipsoid body,
  97.                                     final SphericalPolygonsSet zone,
  98.                                     final double samplingStep)
  99.         throws OrekitException {
  100.         this(DEFAULT_MAXCHECK, DEFAULT_THRESHOLD, DEFAULT_MAX_ITER,
  101.              new StopOnIncreasing<FootprintOverlapDetector>(),
  102.              fov, body, zone, samplingStep, sample(body, zone, samplingStep));
  103.     }

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

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

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

  137.     }

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

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

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

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

  175.         return sampledZone;

  176.     }

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

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

  194.     /** Get the Field Of View.
  195.      * @return Field Of View
  196.      */
  197.     public FieldOfView getFieldOfView() {
  198.         return fov;
  199.     }

  200.     /** Get the body on which the geographic zone is defined.
  201.      * @return body on which the geographic zone is defined
  202.      */
  203.     public BodyShape getBody() {
  204.         return body;
  205.     }

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

  225.         // initial arbitrary positive value
  226.         double value = FastMath.PI;

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

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

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

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

  261.         return value;

  262.     }

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

  269.     /** Internal class used only for serialization. */
  270.     private static class DTO implements Serializable {

  271.         /** Serializable UID. */
  272.         private static final long serialVersionUID = 20150112L;

  273.         /** Max check interval. */
  274.         private final double maxCheck;

  275.         /** Convergence threshold. */
  276.         private final double threshold;

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

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

  281.         /** Field Of View. */
  282.         private final FieldOfView fov;

  283.         /** Proxy for geographic zone. */
  284.         private final SphericalPolygonsSetTransferObject zone;

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

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

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

  313.     }

  314.     /** Container for sampling points. */
  315.     private static class SamplingPoint {

  316.         /** Position of the point. */
  317.         private final Vector3D position;

  318.         /** Zenith vector of the point. */
  319.         private final Vector3D zenith;

  320.         /** Simple constructor.
  321.          * @param position position of the point
  322.          * @param zenith zenith vector of the point
  323.          */
  324.         SamplingPoint(final Vector3D position, final Vector3D zenith) {
  325.             this.position = position;
  326.             this.zenith   = zenith;
  327.         }

  328.         /** Get the point position.
  329.          * @return point position
  330.          */
  331.         public Vector3D getPosition() {
  332.             return position;
  333.         }

  334.         /** Get the point zenith vector.
  335.          * @return point zenith vector
  336.          */
  337.         public Vector3D getZenith() {
  338.             return zenith;
  339.         }

  340.     }

  341. }