FieldLatitudeRangeCrossingDetector.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.BodyShape;
  22. import org.orekit.bodies.FieldGeodeticPoint;
  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 latitude crossing.
  28.  * <p>This detector identifies when a spacecraft crosses a fixed
  29.  * latitude 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 FieldLatitudeRangeCrossingDetector <T extends CalculusFieldElement<T>>
  35.         extends FieldAbstractGeographicalDetector<FieldLatitudeRangeCrossingDetector<T>, T> {

  36.     /**
  37.      * Fixed latitude to be crossed, lower boundary in radians.
  38.      */
  39.     private final double fromLatitude;

  40.     /**
  41.      * Fixed latitude to be crossed, upper boundary in radians.
  42.      */
  43.     private final double toLatitude;

  44.     /**
  45.      * Sign, to get reversed inclusion latitude range (lower > upper).
  46.      */
  47.     private final double sign;

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

  68.     /**
  69.      * Build a detector.
  70.      *
  71.      * @param maxCheck     maximal checking interval (s)
  72.      * @param threshold    convergence threshold (s)
  73.      * @param body         body on which the latitude is defined
  74.      * @param fromLatitude latitude to be crossed, lower range boundary
  75.      * @param toLatitude   latitude to be crossed, upper range boundary
  76.      */
  77.     public FieldLatitudeRangeCrossingDetector(final T maxCheck, final T threshold,
  78.                                               final BodyShape body, final double fromLatitude, final double toLatitude) {
  79.         this(new FieldEventDetectionSettings<>(FieldAdaptableInterval.of(maxCheck.getReal()), threshold, DEFAULT_MAX_ITER),
  80.                 new FieldStopOnIncreasing<>(), body, fromLatitude, toLatitude);
  81.     }

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

  107.     /**
  108.      * {@inheritDoc}
  109.      */
  110.     @Override
  111.     protected FieldLatitudeRangeCrossingDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
  112.                                                            final FieldEventHandler<T> newHandler) {
  113.         return new FieldLatitudeRangeCrossingDetector<>(detectionSettings, newHandler,
  114.             getBodyShape(), fromLatitude, toLatitude);
  115.     }

  116.     /**
  117.      * Get the fixed latitude range to be crossed (radians), lower boundary.
  118.      *
  119.      * @return fixed lower boundary latitude range to be crossed (radians)
  120.      */
  121.     public double getFromLatitude() {
  122.         return fromLatitude;
  123.     }

  124.     /**
  125.      * Get the fixed latitude range to be crossed (radians), upper boundary.
  126.      *
  127.      * @return fixed lower boundary latitude range to be crossed (radians)
  128.      */
  129.     public double getToLatitude() {
  130.         return toLatitude;
  131.     }

  132.     /**
  133.      * Compute the value of the detection function.
  134.      * <p>
  135.      * The value is positive if the spacecraft latitude is inside the latitude range.
  136.      * It is positive if the spacecraft is northward to lower boundary range and southward to upper boundary range,
  137.      * with respect to the fixed latitude range.
  138.      * </p>
  139.      *
  140.      * @param s the current state information: date, kinematics, attitude
  141.      * @return positive if spacecraft inside the range
  142.      */
  143.     public T g(final FieldSpacecraftState<T> s) {

  144.         // convert state to geodetic coordinates
  145.         final FieldGeodeticPoint<T> gp = getBodyShape().transform(s.getPVCoordinates().getPosition(),
  146.             s.getFrame(), s.getDate());

  147.         // point latitude
  148.         final T latitude = gp.getLatitude();

  149.         // inside or outside latitude range
  150.         return latitude.subtract(fromLatitude).multiply(latitude.negate().add(toLatitude)).multiply(sign);

  151.     }

  152. }