FootprintOverlapDetector.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 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.StaticTransform;
  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.      * @since 10.1
  89.      */
  90.     public FootprintOverlapDetector(final FieldOfView fov,
  91.                                     final OneAxisEllipsoid body,
  92.                                     final SphericalPolygonsSet zone,
  93.                                     final double samplingStep) {
  94.         this(EventDetectionSettings.getDefaultEventDetectionSettings(), new StopOnIncreasing(),
  95.              fov, body, zone, samplingStep, sample(body, zone, samplingStep));
  96.     }

  97.     /** Protected constructor with full parameters.
  98.      * <p>
  99.      * This constructor is not public as users are expected to use the builder
  100.      * API with the various {@code withXxx()} methods to set up the instance
  101.      * in a readable manner without using a huge amount of parameters.
  102.      * </p>
  103.      * @param detectionSettings event detection settings
  104.      * @param handler event handler to call at event occurrences
  105.      * @param body body on which the geographic zone is defined
  106.      * @param zone geographic zone to consider
  107.      * @param fov sensor field of view
  108.      * @param sampledZone sampling of the geographic zone
  109.      * @param samplingStep linear step used for sampling the geographic zone (in meters)
  110.      * @since 13.0
  111.      */
  112.     protected FootprintOverlapDetector(final EventDetectionSettings detectionSettings, final EventHandler handler,
  113.                                        final FieldOfView fov,
  114.                                        final OneAxisEllipsoid body,
  115.                                        final SphericalPolygonsSet zone,
  116.                                        final double samplingStep,
  117.                                        final List<SamplingPoint> sampledZone) {

  118.         super(detectionSettings, handler);
  119.         this.fov          = fov;
  120.         this.body         = body;
  121.         this.samplingStep = samplingStep;
  122.         this.zone         = zone;
  123.         this.sampledZone  = sampledZone;

  124.         final EnclosingBall<Sphere2D, S2Point> cap = zone.getEnclosingCap();
  125.         final SinCos sc = FastMath.sinCos(cap.getRadius());
  126.         this.capCenter    = cap.getCenter().getVector();
  127.         this.capCos       = sc.cos();
  128.         this.capSin       = sc.sin();

  129.     }

  130.     /** Sample the region.
  131.      * @param body body on which the geographic zone is defined
  132.      * @param zone geographic zone to consider
  133.      * @param samplingStep  linear step used for sampling the geographic zone (in meters)
  134.      * @return sampling points
  135.      */
  136.     private static List<SamplingPoint> sample(final OneAxisEllipsoid body,
  137.                                               final SphericalPolygonsSet zone,
  138.                                               final double samplingStep) {

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

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

  156.         // sample the zone interior
  157.         final EllipsoidTessellator tessellator =
  158.                         new EllipsoidTessellator(body, new DivertedSingularityAiming(zone), 1);
  159.         final List<List<GeodeticPoint>> gpSample = tessellator.sample(zone, samplingStep, samplingStep);
  160.         for (final List<GeodeticPoint> list : gpSample) {
  161.             for (final GeodeticPoint gp : list) {
  162.                 sampledZone.add(new SamplingPoint(body.transform(gp), gp.getZenith()));
  163.             }
  164.         }

  165.         return sampledZone;

  166.     }

  167.     /** {@inheritDoc} */
  168.     @Override
  169.     protected FootprintOverlapDetector create(final EventDetectionSettings detectionSettings,
  170.                                               final EventHandler newHandler) {
  171.         return new FootprintOverlapDetector(detectionSettings, newHandler,
  172.                                             fov, body, zone, samplingStep, sampledZone);
  173.     }

  174.     /** Get the geographic zone triggering the events.
  175.      * <p>
  176.      * The zone is mapped on the unit sphere
  177.      * </p>
  178.      * @return geographic zone triggering the events
  179.      */
  180.     public SphericalPolygonsSet getZone() {
  181.         return zone;
  182.     }

  183.     /** Get the Field Of View.
  184.      * @return Field Of View
  185.      * @since 10.1
  186.      */
  187.     public FieldOfView getFOV() {
  188.         return fov;
  189.     }

  190.     /** Get the body on which the geographic zone is defined.
  191.      * @return body on which the geographic zone is defined
  192.      */
  193.     public BodyShape getBody() {
  194.         return body;
  195.     }

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

  215.         // initial arbitrary positive value
  216.         double value = FastMath.PI;

  217.         // get spacecraft position in body frame
  218.         final Vector3D      scBody      = s.getPosition(body.getBodyFrame());

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

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

  238.         // the spacecraft may be visible from some points in the zone, check them all
  239.         final StaticTransform bodyToSc = StaticTransform.compose(
  240.                 s.getDate(),
  241.                 body.getBodyFrame().getStaticTransformTo(s.getFrame(), s.getDate()),
  242.                 s.toStaticTransform());
  243.         for (final SamplingPoint point : sampledZone) {
  244.             final Vector3D lineOfSightBody = point.getPosition().subtract(scBody);
  245.             if (Vector3D.dotProduct(lineOfSightBody, point.getZenith()) <= 0) {
  246.                 // spacecraft is above this sample point local horizon
  247.                 // get line of sight in spacecraft frame
  248.                 final double offset = fov.offsetFromBoundary(bodyToSc.transformVector(lineOfSightBody),
  249.                                                              0.0, VisibilityTrigger.VISIBLE_ONLY_WHEN_FULLY_IN_FOV);
  250.                 value = FastMath.min(value, offset);
  251.             }
  252.         }

  253.         return value;

  254.     }

  255.     /** Container for sampling points. */
  256.     private static class SamplingPoint {

  257.         /** Position of the point. */
  258.         private final Vector3D position;

  259.         /** Zenith vector of the point. */
  260.         private final Vector3D zenith;

  261.         /** Simple constructor.
  262.          * @param position position of the point
  263.          * @param zenith zenith vector of the point
  264.          */
  265.         SamplingPoint(final Vector3D position, final Vector3D zenith) {
  266.             this.position = position;
  267.             this.zenith   = zenith;
  268.         }

  269.         /** Get the point position.
  270.          * @return point position
  271.          */
  272.         public Vector3D getPosition() {
  273.             return position;
  274.         }

  275.         /** Get the point zenith vector.
  276.          * @return point zenith vector
  277.          */
  278.         public Vector3D getZenith() {
  279.             return zenith;
  280.         }

  281.     }

  282. }