FieldLongitudeRangeCrossingDetector.java

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

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

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

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

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

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

  70.     /**
  71.      * Build a detector.
  72.      *
  73.      * @param maxCheck      maximal checking interval (s)
  74.      * @param threshold     convergence threshold (s)
  75.      * @param body          body on which the longitude is defined
  76.      * @param fromLongitude longitude to be crossed, lower range boundary
  77.      * @param toLongitude   longitude to be crossed, upper range boundary
  78.      */
  79.     public FieldLongitudeRangeCrossingDetector(final T maxCheck, final T threshold,
  80.                                                final OneAxisEllipsoid body, final double fromLongitude, final double toLongitude) {
  81.         this(FieldAdaptableInterval.of(maxCheck.getReal()),
  82.             threshold,
  83.             DEFAULT_MAX_ITER,
  84.             new FieldStopOnIncreasing<>(),
  85.             body,
  86.             fromLongitude,
  87.             toLongitude);
  88.     }

  89.     /**
  90.      * Private constructor with full parameters.
  91.      * <p>
  92.      * This constructor is private as users are expected to use the builder
  93.      * API with the various {@code withXxx()} methods to set up the instance
  94.      * in a readable manner without using a huge amount of parameters.
  95.      * </p>
  96.      *
  97.      * @param maxCheck      maximum checking interval (s)
  98.      * @param threshold     convergence threshold (s)
  99.      * @param maxIter       maximum number of iterations in the event time search
  100.      * @param handler       event handler to call at event occurrences
  101.      * @param body          body on which the longitude is defined
  102.      * @param fromLongitude longitude to be crossed, lower range boundary
  103.      * @param toLongitude   longitude to be crossed, upper range boundary
  104.      */
  105.     protected FieldLongitudeRangeCrossingDetector(final FieldAdaptableInterval<T> maxCheck,
  106.                                                   final T threshold,
  107.                                                   final int maxIter,
  108.                                                   final FieldEventHandler<T> handler,
  109.                                                   final OneAxisEllipsoid body,
  110.                                                   final double fromLongitude,
  111.                                                   final double toLongitude) {
  112.         super(maxCheck, threshold, maxIter, handler);
  113.         this.body = body;
  114.         this.fromLongitude = ensureLongitudePositiveContinuity(fromLongitude);
  115.         this.toLongitude = ensureLongitudePositiveContinuity(toLongitude);
  116.         this.sign = FastMath.signum(this.toLongitude - this.fromLongitude);
  117.     }

  118.     /**
  119.      * {@inheritDoc}
  120.      */
  121.     @Override
  122.     protected FieldLongitudeRangeCrossingDetector<T> create(final FieldAdaptableInterval<T> newMaxCheck,
  123.                                                             final T newThreshold,
  124.                                                             final int newMaxIter,
  125.                                                             final FieldEventHandler<T> newHandler) {
  126.         return new FieldLongitudeRangeCrossingDetector<T>(newMaxCheck, newThreshold, newMaxIter, newHandler,
  127.             body, fromLongitude, toLongitude);
  128.     }

  129.     /**
  130.      * Get the body on which the geographic zone is defined.
  131.      *
  132.      * @return body on which the geographic zone is defined
  133.      */
  134.     public OneAxisEllipsoid getBody() {
  135.         return body;
  136.     }

  137.     /** Get the fixed longitude range to be crossed (radians), lower boundary.
  138.      * @return fixed lower boundary longitude range to be crossed (radians)
  139.      */
  140.     public double getFromLongitude() {
  141.         return getLongitudeOverOriginalRange(fromLongitude);
  142.     }

  143.     /** Get the fixed longitude range to be crossed (radians), upper boundary.
  144.      * @return fixed upper boundary longitude range to be crossed (radians)
  145.      */
  146.     public double getToLongitude() {
  147.         return getLongitudeOverOriginalRange(toLongitude);
  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 T ensureFieldLongitudePositiveContinuity(final T longitude) {
  157.         return longitude.getReal() < 0 ? longitude.add(2 * FastMath.PI) : longitude;
  158.     }

  159.     /**
  160.      * Ensure continuity for negative angles, as longitude defined as [-PI, PI], transform negative to positive.
  161.      * New longitude angle definition from [0, 2 PI].
  162.      *
  163.      * @param longitude original longitude value
  164.      * @return positive range longitude
  165.      */
  166.     private double ensureLongitudePositiveContinuity(final double longitude) {
  167.         return longitude < 0 ? longitude + 2 * FastMath.PI : longitude;
  168.     }

  169.     /**
  170.      * Get longitude shifted over the original range [-PI, PI].
  171.      * @param longitude longitude value to convert
  172.      * @return original range longitude
  173.      */
  174.     private double getLongitudeOverOriginalRange(final double longitude) {
  175.         return longitude > FastMath.PI ? longitude - 2 * FastMath.PI : longitude;
  176.     }

  177.     /**
  178.      * Compute the value of the detection function.
  179.      * <p>
  180.      * The value is positive if the spacecraft longitude is inside the longitude range.
  181.      * The longitude value is reflected from [-PI, +PI] to [0, 2 PI] to ensure continuity.
  182.      * </p>
  183.      *
  184.      * @param s the current state information: date, kinematics, attitude
  185.      * @return positive if spacecraft inside the range
  186.      */
  187.     public T g(final FieldSpacecraftState<T> s) {

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

  191.         // point longitude
  192.         final T longitude = ensureFieldLongitudePositiveContinuity(gp.getLongitude());

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

  195.     }

  196. }