LongitudeRangeCrossingDetector.java

  1. /* Copyright 2023-2025 Alberto Ferrero
  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.  * Alberto Ferrero 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.orekit.bodies.GeodeticPoint;
  20. import org.orekit.bodies.OneAxisEllipsoid;
  21. import org.orekit.propagation.SpacecraftState;
  22. import org.orekit.propagation.events.handlers.EventHandler;
  23. import org.orekit.propagation.events.handlers.StopOnDecreasing;


  24. /** Detector for geographic longitude crossing.
  25.  * <p>This detector identifies when a spacecraft crosses a fixed
  26.  * longitude range with respect to a central body.</p>
  27.  * @author Alberto Ferrero
  28.  * @since 12.0
  29.  */
  30. public class LongitudeRangeCrossingDetector extends AbstractDetector<LongitudeRangeCrossingDetector> {

  31.     /** Body on which the longitude is defined. */
  32.     private final OneAxisEllipsoid body;

  33.     /** Fixed longitude to be crossed, lower boundary in radians. */
  34.     private final double fromLongitude;

  35.     /** Fixed longitude to be crossed, upper boundary in radians. */
  36.     private final double toLongitude;

  37.     /**
  38.      * Sign, to get reversed inclusion longitude range (lower > upper).
  39.      */
  40.     private final double sign;

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

  52.     /** Build a detector.
  53.      * @param maxCheck maximal checking interval (s)
  54.      * @param threshold convergence threshold (s)
  55.      * @param body body on which the longitude is defined
  56.      * @param fromLongitude longitude to be crossed, lower range boundary
  57.      * @param toLongitude longitude to be crossed, upper range boundary
  58.      */
  59.     public LongitudeRangeCrossingDetector(final double maxCheck, final double threshold,
  60.                                           final OneAxisEllipsoid body, final double fromLongitude, final double toLongitude) {
  61.         this(new EventDetectionSettings(maxCheck, threshold, DEFAULT_MAX_ITER), new StopOnDecreasing(),
  62.              body, fromLongitude, toLongitude);
  63.     }

  64.     /** Private constructor with full parameters.
  65.      * <p>
  66.      * This constructor is private as users are expected to use the builder
  67.      * API with the various {@code withXxx()} methods to set up the instance
  68.      * in a readable manner without using a huge amount of parameters.
  69.      * </p>
  70.      * @param detectionSettings event detection settings
  71.      * @param handler event handler to call at event occurrences
  72.      * @param body body on which the longitude is defined
  73.      * @param fromLongitude longitude to be crossed, lower range boundary
  74.      * @param toLongitude longitude to be crossed, upper range boundary
  75.      * @since 13.0
  76.      */
  77.     protected LongitudeRangeCrossingDetector(final EventDetectionSettings detectionSettings, final EventHandler handler,
  78.                                              final OneAxisEllipsoid body, final double fromLongitude, final double toLongitude) {
  79.         super(detectionSettings, handler);
  80.         this.body     = body;
  81.         this.fromLongitude = ensureLongitudePositiveContinuity(fromLongitude);
  82.         this.toLongitude = ensureLongitudePositiveContinuity(toLongitude);
  83.         this.sign = FastMath.signum(this.toLongitude - this.fromLongitude);
  84.     }

  85.     /** {@inheritDoc} */
  86.     @Override
  87.     protected LongitudeRangeCrossingDetector create(final EventDetectionSettings detectionSettings,
  88.                                                     final EventHandler newHandler) {
  89.         return new LongitudeRangeCrossingDetector(detectionSettings, newHandler, body, fromLongitude, toLongitude);
  90.     }

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

  97.     /** Get the fixed longitude range to be crossed (radians), lower boundary.
  98.      * @return fixed lower boundary longitude range to be crossed (radians)
  99.      */
  100.     public double getFromLongitude() {
  101.         return getLongitudeOverOriginalRange(fromLongitude);
  102.     }

  103.     /** Get the fixed longitude range to be crossed (radians), upper boundary.
  104.      * @return fixed upper boundary longitude range to be crossed (radians)
  105.      */
  106.     public double getToLongitude() {
  107.         return getLongitudeOverOriginalRange(toLongitude);
  108.     }

  109.     /**
  110.      * Ensure continuity for negative angles, as longitude defined as [-PI, PI], transform negative to positive.
  111.      * New longitude angle definition from [0, 2 PI].
  112.      * @param longitude original longitude value
  113.      * @return positive range longitude
  114.      */
  115.     private double ensureLongitudePositiveContinuity(final double longitude) {
  116.         return longitude < 0 ? longitude + 2 * FastMath.PI : longitude;
  117.     }

  118.     /**
  119.      * Get longitude shifted over the original range [-PI, PI].
  120.      * @param longitude longitude value to convert
  121.      * @return original range longitude
  122.      */
  123.     private double getLongitudeOverOriginalRange(final double longitude) {
  124.         return longitude > FastMath.PI ? longitude - 2 * FastMath.PI : longitude;
  125.     }

  126.     /** Compute the value of the detection function.
  127.      * <p>
  128.      * The value is positive if the spacecraft longitude is inside the longitude range.
  129.      * The longitude value is reflected from [-PI, +PI] to [0, 2 PI] to ensure continuity.
  130.      * </p>
  131.      * @param s the current state information: date, kinematics, attitude
  132.      * @return positive if spacecraft inside the range
  133.      */
  134.     public double g(final SpacecraftState s) {

  135.         // convert state to geodetic coordinates
  136.         final GeodeticPoint gp = body.transform(s.getPVCoordinates().getPosition(),
  137.             s.getFrame(), s.getDate());

  138.         // point longitude
  139.         final double longitude = ensureLongitudePositiveContinuity(gp.getLongitude());

  140.         // inside or outside longitude range
  141.         return sign * (longitude - fromLongitude) * (toLongitude - longitude);

  142.     }

  143. }