LongitudeRangeCrossingDetector.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.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_MAXCHECK}) 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_MAXCHECK, 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(AdaptableInterval.of(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 maxCheck maximum checking interval (s)
  71.      * @param threshold convergence threshold (s)
  72.      * @param maxIter maximum number of iterations in the event time search
  73.      * @param handler event handler to call at event occurrences
  74.      * @param body body on which the longitude is defined
  75.      * @param fromLongitude longitude to be crossed, lower range boundary
  76.      * @param toLongitude longitude to be crossed, upper range boundary
  77.      */
  78.     protected LongitudeRangeCrossingDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
  79.                                              final EventHandler handler,
  80.                                              final OneAxisEllipsoid body, final double fromLongitude, final double toLongitude) {
  81.         super(maxCheck, threshold, maxIter, handler);
  82.         this.body     = body;
  83.         this.fromLongitude = ensureLongitudePositiveContinuity(fromLongitude);
  84.         this.toLongitude = ensureLongitudePositiveContinuity(toLongitude);
  85.         this.sign = FastMath.signum(this.toLongitude - this.fromLongitude);
  86.     }

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

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

  102.     /** Get the fixed longitude range to be crossed (radians), lower boundary.
  103.      * @return fixed lower boundary longitude range to be crossed (radians)
  104.      */
  105.     public double getFromLongitude() {
  106.         return getLongitudeOverOriginalRange(fromLongitude);
  107.     }

  108.     /** Get the fixed longitude range to be crossed (radians), upper boundary.
  109.      * @return fixed upper boundary longitude range to be crossed (radians)
  110.      */
  111.     public double getToLongitude() {
  112.         return getLongitudeOverOriginalRange(toLongitude);
  113.     }

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

  123.     /**
  124.      * Get longitude shifted over the original range [-PI, PI].
  125.      * @param longitude longitude value to convert
  126.      * @return original range longitude
  127.      */
  128.     private double getLongitudeOverOriginalRange(final double longitude) {
  129.         return longitude > FastMath.PI ? longitude - 2 * FastMath.PI : longitude;
  130.     }

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

  140.         // convert state to geodetic coordinates
  141.         final GeodeticPoint gp = body.transform(s.getPVCoordinates().getPosition(),
  142.             s.getFrame(), s.getDate());

  143.         // point longitude
  144.         final double longitude = ensureLongitudePositiveContinuity(gp.getLongitude());

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

  147.     }

  148. }