LongitudeCrossingDetector.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 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_MAX_CHECK}) 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_MAX_CHECK, 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(new EventDetectionSettings(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 detectionSettings event detection settings
  68.      * @param handler event handler to call at event occurrences
  69.      * @param body body on which the longitude is defined
  70.      * @param longitude longitude to be crossed
  71.      * @since 13.0
  72.      */
  73.     protected LongitudeCrossingDetector(final EventDetectionSettings detectionSettings, final EventHandler handler,
  74.                                         final OneAxisEllipsoid body, final double longitude) {

  75.         super(detectionSettings, handler);

  76.         this.body      = body;
  77.         this.longitude = longitude;

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

  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     protected LongitudeCrossingDetector create(final EventDetectionSettings detectionSettings,
  87.                                                final EventHandler newHandler) {
  88.         return new LongitudeCrossingDetector(detectionSettings, newHandler, body, longitude);
  89.     }

  90.     /** Get the body on which the geographic zone is defined.
  91.      * @return body on which the geographic zone is defined
  92.      */
  93.     public OneAxisEllipsoid getBody() {
  94.         return body;
  95.     }

  96.     /** Get the fixed longitude to be crossed (radians).
  97.      * @return fixed longitude to be crossed (radians)
  98.      */
  99.     public double getLongitude() {
  100.         return longitude;
  101.     }

  102.     /**  {@inheritDoc} */
  103.     @Override
  104.     public void init(final SpacecraftState s0, final AbsoluteDate t) {
  105.         super.init(s0, t);
  106.         filtering.init(s0, t);
  107.     }

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

  127.     private class RawLongitudeCrossingDetector extends AbstractDetector<RawLongitudeCrossingDetector> {

  128.         /** Protected constructor with full parameters.
  129.          * <p>
  130.          * This constructor is not public as users are expected to use the builder
  131.          * API with the various {@code withXxx()} methods to set up the instance
  132.          * in a readable manner without using a huge amount of parameters.
  133.          * </p>
  134.          * @param detectionSettings event detection settings
  135.          * @param handler event handler to call at event occurrences
  136.          */
  137.         protected RawLongitudeCrossingDetector(final EventDetectionSettings detectionSettings,
  138.                                                final EventHandler handler) {
  139.             super(detectionSettings, handler);
  140.         }

  141.         /** {@inheritDoc} */
  142.         @Override
  143.         protected RawLongitudeCrossingDetector create(final EventDetectionSettings detectionSettings,
  144.                                                       final EventHandler newHandler) {
  145.             return new RawLongitudeCrossingDetector(detectionSettings, newHandler);
  146.         }

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

  160.             // convert state to geodetic coordinates
  161.             final GeodeticPoint gp = body.transform(s.getPosition(),
  162.                                                     s.getFrame(), s.getDate());

  163.             // longitude difference
  164.             return MathUtils.normalizeAngle(gp.getLongitude() - longitude, 0.0);

  165.         }

  166.     }

  167. }