1   /* Copyright 2002-2025 CS GROUP
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    * CS 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  
19  import org.hipparchus.Field;
20  import org.hipparchus.CalculusFieldElement;
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  
28  /** Detector for geographic latitude crossing.
29   * <p>This detector identifies when a spacecraft crosses a fixed
30   * latitude with respect to a central body.</p>
31   * @author Luc Maisonobe
32   * @author Evan Ward
33   * @since 9.3
34   * @param <T> type of the field elements
35   */
36  public class FieldLatitudeCrossingDetector <T extends CalculusFieldElement<T>>
37          extends FieldAbstractDetector<FieldLatitudeCrossingDetector<T>, T> {
38  
39      /** Body on which the latitude is defined. */
40      private final OneAxisEllipsoid body;
41  
42      /** Fixed latitude to be crossed. */
43      private final double latitude;
44  
45      /** Build a new detector.
46       * <p>The new instance uses default values for maximal checking interval
47       * ({@link #DEFAULT_MAX_CHECK}) and convergence threshold ({@link
48       * #DEFAULT_THRESHOLD}).</p>
49       * @param field the type of numbers to use.
50       * @param body body on which the latitude is defined
51       * @param latitude latitude to be crossed
52       */
53      public FieldLatitudeCrossingDetector(final Field<T> field,
54                                           final OneAxisEllipsoid body,
55                                           final double latitude) {
56          this(new FieldEventDetectionSettings<>(field, EventDetectionSettings.getDefaultEventDetectionSettings()),
57                  new FieldStopOnIncreasing<>(), body, latitude);
58      }
59  
60      /** Build a detector.
61       * @param maxCheck maximal checking interval (s)
62       * @param threshold convergence threshold (s)
63       * @param body body on which the latitude is defined
64       * @param latitude latitude to be crossed
65       */
66      public FieldLatitudeCrossingDetector(final T maxCheck,
67                                           final T threshold,
68                                           final OneAxisEllipsoid body,
69                                           final double latitude) {
70          this(new FieldEventDetectionSettings<>(FieldAdaptableInterval.of(maxCheck.getReal()), threshold, DEFAULT_MAX_ITER),
71                  new FieldStopOnIncreasing<>(), body, latitude);
72      }
73  
74      /** Protected constructor with full parameters.
75       * <p>
76       * This constructor is not public as users are expected to use the builder
77       * API with the various {@code withXxx()} methods to set up the instance
78       * in a readable manner without using a huge amount of parameters.
79       * </p>
80       * @param detectionSettings event detection settings
81       * @param handler event handler to call at event occurrences
82       * @param body body on which the latitude is defined
83       * @param latitude latitude to be crossed
84       * @since 13.0
85       */
86      protected FieldLatitudeCrossingDetector(
87              final FieldEventDetectionSettings<T> detectionSettings,
88              final FieldEventHandler<T> handler,
89              final OneAxisEllipsoid body,
90              final double latitude) {
91          super(detectionSettings, handler);
92          this.body     = body;
93          this.latitude = latitude;
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      protected FieldLatitudeCrossingDetector<T> create(
99              final FieldEventDetectionSettings<T> detectionSettings,
100             final FieldEventHandler<T> newHandler) {
101         return new FieldLatitudeCrossingDetector<>(detectionSettings, newHandler, body, latitude);
102     }
103 
104     /** Get the body on which the geographic zone is defined.
105      * @return body on which the geographic zone is defined
106      */
107     public OneAxisEllipsoid getBody() {
108         return body;
109     }
110 
111     /** Get the fixed latitude to be crossed (radians).
112      * @return fixed latitude to be crossed (radians)
113      */
114     public double getLatitude() {
115         return latitude;
116     }
117 
118     /** Compute the value of the detection function.
119      * <p>
120      * The value is the spacecraft latitude minus the fixed latitude to be crossed.
121      * It is positive if the spacecraft is northward and negative if it is southward
122      * with respect to the fixed latitude.
123      * </p>
124      * @param s the current state information: date, kinematics, attitude
125      * @return spacecraft latitude minus the fixed latitude to be crossed
126      */
127     public T g(final FieldSpacecraftState<T> s) {
128 
129         // convert state to geodetic coordinates
130         final FieldGeodeticPoint<T> gp = body.transform(
131                 s.getPosition(),
132                 s.getFrame(),
133                 s.getDate());
134 
135         // latitude difference
136         return gp.getLatitude().subtract(latitude);
137 
138     }
139 
140 }