FieldLongitudeRangeCrossingDetector.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.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.util.FastMath;
  21. import org.orekit.bodies.FieldGeodeticPoint;
  22. import org.orekit.bodies.OneAxisEllipsoid;
  23. import org.orekit.propagation.FieldSpacecraftState;
  24. import org.orekit.propagation.events.handlers.FieldEventHandler;
  25. import org.orekit.propagation.events.handlers.FieldStopOnIncreasing;
  26. import org.orekit.propagation.events.intervals.FieldAdaptableInterval;


  27. /** Detector for geographic longitude crossing.
  28.  * <p>This detector identifies when a spacecraft crosses a fixed
  29.  * longitude range with respect to a central body.</p>
  30.  * @author Alberto Ferrero
  31.  * @since 12.0
  32.  * @param <T> type of the field elements
  33.  */
  34. public class FieldLongitudeRangeCrossingDetector <T extends CalculusFieldElement<T>>
  35.         extends FieldAbstractDetector<FieldLongitudeRangeCrossingDetector<T>, T> {

  36.     /**
  37.      * Body on which the longitude is defined.
  38.      */
  39.     private final OneAxisEllipsoid body;

  40.     /**
  41.      * Fixed longitude to be crossed, lower boundary in radians.
  42.      */
  43.     private final double fromLongitude;

  44.     /**
  45.      * Fixed longitude to be crossed, upper boundary in radians.
  46.      */
  47.     private final double toLongitude;

  48.     /**
  49.      * Sign, to get reversed inclusion longitude range (lower > upper).
  50.      */
  51.     private final double sign;

  52.     /**
  53.      * Build a new detector.
  54.      * <p>The new instance uses default values for maximal checking interval
  55.      * ({@link #DEFAULT_MAX_CHECK}) and convergence threshold ({@link
  56.      * #DEFAULT_THRESHOLD}).</p>
  57.      * @param field        the type of numbers to use.
  58.      * @param body          body on which the longitude is defined
  59.      * @param fromLongitude longitude to be crossed, lower range boundary
  60.      * @param toLongitude   longitude to be crossed, upper range boundary
  61.      */
  62.     public FieldLongitudeRangeCrossingDetector(final Field<T> field, final OneAxisEllipsoid body,
  63.                                                final double fromLongitude, final double toLongitude) {
  64.         this(new FieldEventDetectionSettings<>(field, EventDetectionSettings.getDefaultEventDetectionSettings()),
  65.             new FieldStopOnIncreasing<>(), body, fromLongitude, toLongitude);
  66.     }

  67.     /**
  68.      * Build a detector.
  69.      *
  70.      * @param maxCheck      maximal checking interval (s)
  71.      * @param threshold     convergence threshold (s)
  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.      */
  76.     public FieldLongitudeRangeCrossingDetector(final T maxCheck, final T threshold,
  77.                                                final OneAxisEllipsoid body, final double fromLongitude, final double toLongitude) {
  78.         this(new FieldEventDetectionSettings<>(FieldAdaptableInterval.of(maxCheck.getReal()), threshold, DEFAULT_MAX_ITER),
  79.             new FieldStopOnIncreasing<>(),
  80.             body,
  81.             fromLongitude,
  82.             toLongitude);
  83.     }

  84.     /**
  85.      * Protected constructor with full parameters.
  86.      * <p>
  87.      * This constructor is private as users are expected to use the builder
  88.      * API with the various {@code withXxx()} methods to set up the instance
  89.      * in a readable manner without using a huge amount of parameters.
  90.      * </p>
  91.      *
  92.      * @param detectionSettings event detection settings
  93.      * @param handler       event handler to call at event occurrences
  94.      * @param body          body on which the longitude is defined
  95.      * @param fromLongitude longitude to be crossed, lower range boundary
  96.      * @param toLongitude   longitude to be crossed, upper range
  97.      * @since 13.0
  98.      */
  99.     protected FieldLongitudeRangeCrossingDetector(final FieldEventDetectionSettings<T> detectionSettings,
  100.                                                   final FieldEventHandler<T> handler,
  101.                                                   final OneAxisEllipsoid body,
  102.                                                   final double fromLongitude,
  103.                                                   final double toLongitude) {
  104.         super(detectionSettings, handler);
  105.         this.body = body;
  106.         this.fromLongitude = ensureLongitudePositiveContinuity(fromLongitude);
  107.         this.toLongitude = ensureLongitudePositiveContinuity(toLongitude);
  108.         this.sign = FastMath.signum(this.toLongitude - this.fromLongitude);
  109.     }

  110.     /**
  111.      * {@inheritDoc}
  112.      */
  113.     @Override
  114.     protected FieldLongitudeRangeCrossingDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
  115.                                                             final FieldEventHandler<T> newHandler) {
  116.         return new FieldLongitudeRangeCrossingDetector<>(detectionSettings, newHandler,
  117.             body, fromLongitude, toLongitude);
  118.     }

  119.     /**
  120.      * Get the body on which the geographic zone is defined.
  121.      *
  122.      * @return body on which the geographic zone is defined
  123.      */
  124.     public OneAxisEllipsoid getBody() {
  125.         return body;
  126.     }

  127.     /** Get the fixed longitude range to be crossed (radians), lower boundary.
  128.      * @return fixed lower boundary longitude range to be crossed (radians)
  129.      */
  130.     public double getFromLongitude() {
  131.         return getLongitudeOverOriginalRange(fromLongitude);
  132.     }

  133.     /** Get the fixed longitude range to be crossed (radians), upper boundary.
  134.      * @return fixed upper boundary longitude range to be crossed (radians)
  135.      */
  136.     public double getToLongitude() {
  137.         return getLongitudeOverOriginalRange(toLongitude);
  138.     }

  139.     /**
  140.      * Ensure continuity for negative angles, as longitude defined as [-PI, PI], transform negative to positive.
  141.      * New longitude angle definition from [0, 2 PI].
  142.      *
  143.      * @param longitude original longitude value
  144.      * @return positive range longitude
  145.      */
  146.     private T ensureFieldLongitudePositiveContinuity(final T longitude) {
  147.         return longitude.getReal() < 0 ? longitude.add(2 * FastMath.PI) : longitude;
  148.     }

  149.     /**
  150.      * Ensure continuity for negative angles, as longitude defined as [-PI, PI], transform negative to positive.
  151.      * New longitude angle definition from [0, 2 PI].
  152.      *
  153.      * @param longitude original longitude value
  154.      * @return positive range longitude
  155.      */
  156.     private double ensureLongitudePositiveContinuity(final double longitude) {
  157.         return longitude < 0 ? longitude + 2 * FastMath.PI : longitude;
  158.     }

  159.     /**
  160.      * Get longitude shifted over the original range [-PI, PI].
  161.      * @param longitude longitude value to convert
  162.      * @return original range longitude
  163.      */
  164.     private double getLongitudeOverOriginalRange(final double longitude) {
  165.         return longitude > FastMath.PI ? longitude - 2 * FastMath.PI : longitude;
  166.     }

  167.     /**
  168.      * Compute the value of the detection function.
  169.      * <p>
  170.      * The value is positive if the spacecraft longitude is inside the longitude range.
  171.      * The longitude value is reflected from [-PI, +PI] to [0, 2 PI] to ensure continuity.
  172.      * </p>
  173.      *
  174.      * @param s the current state information: date, kinematics, attitude
  175.      * @return positive if spacecraft inside the range
  176.      */
  177.     public T g(final FieldSpacecraftState<T> s) {

  178.         // convert state to geodetic coordinates
  179.         final FieldGeodeticPoint<T> gp = body.transform(s.getPVCoordinates().getPosition(),
  180.             s.getFrame(), s.getDate());

  181.         // point longitude
  182.         final T longitude = ensureFieldLongitudePositiveContinuity(gp.getLongitude());

  183.         // inside or outside latitude range
  184.         return longitude.subtract(fromLongitude).multiply(longitude.negate().add(toLongitude)).multiply(sign);

  185.     }

  186. }