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