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.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 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 FieldAbstractDetector<FieldLatitudeRangeCrossingDetector<T>, T> {

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

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

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

  48.     /**
  49.      * Sign, to get reversed inclusion latitude 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 latitude is defined
  59.      * @param fromLatitude latitude to be crossed, lower range boundary
  60.      * @param toLatitude   latitude to be crossed, upper range boundary
  61.      */
  62.     public FieldLatitudeRangeCrossingDetector(final Field<T> field,
  63.                                               final OneAxisEllipsoid body,
  64.                                               final double fromLatitude,
  65.                                               final double toLatitude) {
  66.         this(new FieldEventDetectionSettings<>(field, EventDetectionSettings.getDefaultEventDetectionSettings()),
  67.             new FieldStopOnIncreasing<>(),
  68.             body,
  69.             fromLatitude,
  70.             toLatitude);
  71.     }

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

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

  112.     /**
  113.      * {@inheritDoc}
  114.      */
  115.     @Override
  116.     protected FieldLatitudeRangeCrossingDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
  117.                                                            final FieldEventHandler<T> newHandler) {
  118.         return new FieldLatitudeRangeCrossingDetector<>(detectionSettings, newHandler,
  119.             body, fromLatitude, toLatitude);
  120.     }

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

  129.     /**
  130.      * Get the fixed latitude range to be crossed (radians), lower boundary.
  131.      *
  132.      * @return fixed lower boundary latitude range to be crossed (radians)
  133.      */
  134.     public double getFromLatitude() {
  135.         return fromLatitude;
  136.     }

  137.     /**
  138.      * Get the fixed latitude range to be crossed (radians), upper boundary.
  139.      *
  140.      * @return fixed lower boundary latitude range to be crossed (radians)
  141.      */
  142.     public double getToLatitude() {
  143.         return toLatitude;
  144.     }

  145.     /**
  146.      * Compute the value of the detection function.
  147.      * <p>
  148.      * The value is positive if the spacecraft latitude is inside the latitude range.
  149.      * It is positive if the spacecraft is northward to lower boundary range and southward to upper boundary range,
  150.      * with respect to the fixed latitude range.
  151.      * </p>
  152.      *
  153.      * @param s the current state information: date, kinematics, attitude
  154.      * @return positive if spacecraft inside the range
  155.      */
  156.     public T g(final FieldSpacecraftState<T> s) {

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

  160.         // point latitude
  161.         final T latitude = gp.getLatitude();

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

  164.     }

  165. }