LongitudeCrossingDetector.java

  1. /* Copyright 2002-2024 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 org.hipparchus.util.FastMath;
  19. import org.hipparchus.util.MathUtils;
  20. import org.orekit.bodies.GeodeticPoint;
  21. import org.orekit.bodies.OneAxisEllipsoid;
  22. import org.orekit.propagation.SpacecraftState;
  23. import org.orekit.propagation.events.handlers.ContinueOnEvent;
  24. import org.orekit.propagation.events.handlers.EventHandler;
  25. import org.orekit.propagation.events.handlers.StopOnIncreasing;
  26. import org.orekit.time.AbsoluteDate;

  27. /** Detector for geographic longitude crossing.
  28.  * <p>This detector identifies when a spacecraft crosses a fixed
  29.  * longitude with respect to a central body.</p>
  30.  * @author Luc Maisonobe
  31.  * @since 7.1
  32.  */
  33. public class LongitudeCrossingDetector extends AbstractDetector<LongitudeCrossingDetector> {

  34.     /** Body on which the longitude is defined. */
  35.     private OneAxisEllipsoid body;

  36.     /** Fixed longitude to be crossed. */
  37.     private final double longitude;

  38.     /** Filtering detector. */
  39.     private final EventEnablingPredicateFilter filtering;

  40.     /** Build a new detector.
  41.      * <p>The new instance uses default values for maximal checking interval
  42.      * ({@link #DEFAULT_MAXCHECK}) and convergence threshold ({@link
  43.      * #DEFAULT_THRESHOLD}).</p>
  44.      * @param body body on which the longitude is defined
  45.      * @param longitude longitude to be crossed
  46.      */
  47.     public LongitudeCrossingDetector(final OneAxisEllipsoid body, final double longitude) {
  48.         this(DEFAULT_MAXCHECK, DEFAULT_THRESHOLD, body, longitude);
  49.     }

  50.     /** Build a detector.
  51.      * @param maxCheck maximal checking interval (s)
  52.      * @param threshold convergence threshold (s)
  53.      * @param body body on which the longitude is defined
  54.      * @param longitude longitude to be crossed
  55.      */
  56.     public LongitudeCrossingDetector(final double maxCheck, final double threshold,
  57.                                     final OneAxisEllipsoid body, final double longitude) {
  58.         this(AdaptableInterval.of(maxCheck), threshold, DEFAULT_MAX_ITER, new StopOnIncreasing(),
  59.              body, longitude);
  60.     }

  61.     /** Protected constructor with full parameters.
  62.      * <p>
  63.      * This constructor is not public as users are expected to use the builder
  64.      * API with the various {@code withXxx()} methods to set up the instance
  65.      * in a readable manner without using a huge amount of parameters.
  66.      * </p>
  67.      * @param maxCheck maximum checking interval
  68.      * @param threshold convergence threshold (s)
  69.      * @param maxIter maximum number of iterations in the event time search
  70.      * @param handler event handler to call at event occurrences
  71.      * @param body body on which the longitude is defined
  72.      * @param longitude longitude to be crossed
  73.      */
  74.     protected LongitudeCrossingDetector(final AdaptableInterval maxCheck, final double threshold,
  75.                                         final int maxIter, final EventHandler handler,
  76.                                         final OneAxisEllipsoid body, final double longitude) {

  77.         super(maxCheck, threshold, maxIter, handler);

  78.         this.body      = body;
  79.         this.longitude = longitude;

  80.         // we filter out spurious longitude crossings occurring at the antimeridian
  81.         final RawLongitudeCrossingDetector raw = new RawLongitudeCrossingDetector(maxCheck, threshold, maxIter,
  82.                                                                                   new ContinueOnEvent());
  83.         final EnablingPredicate predicate =
  84.             (state, detector, g) -> FastMath.abs(g) < 0.5 * FastMath.PI;
  85.         this.filtering = new EventEnablingPredicateFilter(raw, predicate);

  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     protected LongitudeCrossingDetector create(final AdaptableInterval newMaxCheck, final double newThreshold, final int newMaxIter,
  90.                                                final EventHandler newHandler) {
  91.         return new LongitudeCrossingDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  92.                                              body, longitude);
  93.     }

  94.     /** Get the body on which the geographic zone is defined.
  95.      * @return body on which the geographic zone is defined
  96.      */
  97.     public OneAxisEllipsoid getBody() {
  98.         return body;
  99.     }

  100.     /** Get the fixed longitude to be crossed (radians).
  101.      * @return fixed longitude to be crossed (radians)
  102.      */
  103.     public double getLongitude() {
  104.         return longitude;
  105.     }

  106.     /**  {@inheritDoc} */
  107.     public void init(final SpacecraftState s0, final AbsoluteDate t) {
  108.         filtering.init(s0, t);
  109.     }

  110.     /** Compute the value of the detection function.
  111.      * <p>
  112.      * The value is the longitude difference between the spacecraft and the fixed
  113.      * longitude to be crossed, with some sign tweaks to ensure continuity.
  114.      * These tweaks imply the {@code increasing} flag in events detection becomes
  115.      * irrelevant here! As an example, the longitude of a prograde spacecraft
  116.      * will always increase, but this g function will increase and decrease so it
  117.      * will cross the zero value once per orbit, in increasing and decreasing
  118.      * directions on alternate orbits. If eastwards and westwards crossing have to
  119.      * be distinguished, the velocity direction has to be checked instead of looking
  120.      * at the {@code increasing} flag.
  121.      * </p>
  122.      * @param s the current state information: date, kinematics, attitude
  123.      * @return longitude difference between the spacecraft and the fixed
  124.      * longitude, with some sign tweaks to ensure continuity
  125.      */
  126.     public double g(final SpacecraftState s) {
  127.         return filtering.g(s);
  128.     }

  129.     private class RawLongitudeCrossingDetector extends AbstractDetector<RawLongitudeCrossingDetector> {

  130.         /** Protected constructor with full parameters.
  131.          * <p>
  132.          * This constructor is not public as users are expected to use the builder
  133.          * API with the various {@code withXxx()} methods to set up the instance
  134.          * in a readable manner without using a huge amount of parameters.
  135.          * </p>
  136.          * @param maxCheck maximum checking interval
  137.          * @param threshold convergence threshold (s)
  138.          * @param maxIter maximum number of iterations in the event time search
  139.          * @param handler event handler to call at event occurrences
  140.          */
  141.         protected RawLongitudeCrossingDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
  142.                                                final EventHandler handler) {
  143.             super(maxCheck, threshold, maxIter, handler);
  144.         }

  145.         /** {@inheritDoc} */
  146.         @Override
  147.         protected RawLongitudeCrossingDetector create(final AdaptableInterval newMaxCheck, final double newThreshold,
  148.                                                       final int newMaxIter,
  149.                                                       final EventHandler newHandler) {
  150.             return new RawLongitudeCrossingDetector(newMaxCheck, newThreshold, newMaxIter, newHandler);
  151.         }

  152.         /** Compute the value of the detection function.
  153.          * <p>
  154.          * The value is the longitude difference between the spacecraft and the fixed
  155.          * longitude to be crossed, and it <em>does</em> change sign twice around
  156.          * the central body: once at expected longitude and once at antimeridian.
  157.          * The second sign change is a spurious one and is filtered out by the
  158.          * outer class.
  159.          * </p>
  160.          * @param s the current state information: date, kinematics, attitude
  161.          * @return longitude difference between the spacecraft and the fixed
  162.          * longitude
  163.          */
  164.         public double g(final SpacecraftState s) {

  165.             // convert state to geodetic coordinates
  166.             final GeodeticPoint gp = body.transform(s.getPosition(),
  167.                                                     s.getFrame(), s.getDate());

  168.             // longitude difference
  169.             return MathUtils.normalizeAngle(gp.getLongitude() - longitude, 0.0);

  170.         }

  171.     }

  172. }