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.hipparchus.geometry.euclidean.threed.FieldVector3D;
22  import org.hipparchus.ode.events.Action;
23  import org.hipparchus.util.FastMath;
24  import org.orekit.frames.FieldStaticTransform;
25  import org.orekit.frames.TopocentricFrame;
26  import org.orekit.models.AtmosphericRefractionModel;
27  import org.orekit.propagation.FieldSpacecraftState;
28  import org.orekit.propagation.events.handlers.FieldEventHandler;
29  import org.orekit.propagation.events.handlers.FieldStopOnDecreasing;
30  import org.orekit.utils.ElevationMask;
31  
32  
33  /**
34   * Finder for satellite raising/setting events that allows for the
35   * setting of azimuth and/or elevation bounds or a ground azimuth/elevation
36   * mask input. Each calculation be configured to use atmospheric refraction
37   * as well.
38   * <p>The default implementation behavior is to {@link Action#CONTINUE continue}
39   * propagation at raising and to {@link Action#STOP stop} propagation
40   * at setting. This can be changed by calling
41   * {@link #withHandler(FieldEventHandler)} after construction.</p>
42   * @author Hank Grabowski
43   * @param <T> type of the field elements
44   */
45  public class FieldElevationDetector<T extends CalculusFieldElement<T>> extends FieldAbstractDetector<FieldElevationDetector<T>, T> {
46  
47      /** Elevation mask used for calculations, if defined. */
48      private final ElevationMask elevationMask;
49  
50      /** Minimum elevation value used if mask is not defined. */
51      private final double minElevation;
52  
53      /** Atmospheric Model used for calculations, if defined. */
54      private final AtmosphericRefractionModel refractionModel;
55  
56      /** Topocentric frame in which elevation should be evaluated. */
57      private final TopocentricFrame topo;
58  
59      /**
60       * Creates an instance of Elevation detector based on passed in topocentric frame
61       * and the minimum elevation angle.
62       * <p>
63       * uses default values for maximal checking interval ({@link #DEFAULT_MAX_CHECK})
64       * and convergence threshold ({@link #DEFAULT_THRESHOLD}).</p>
65       * @param field type of the elements
66       * @param topo reference to a topocentric model
67       * @see #withConstantElevation(double)
68       * @see #withElevationMask(ElevationMask)
69       * @see #withRefraction(AtmosphericRefractionModel)
70       */
71      public FieldElevationDetector(final Field<T> field, final TopocentricFrame topo) {
72          this(new FieldEventDetectionSettings<>(field, EventDetectionSettings.getDefaultEventDetectionSettings()),
73               new FieldStopOnDecreasing<>(),
74               0.0, null, null, topo);
75      }
76  
77      /**
78       * Creates an instance of Elevation detector based on passed in topocentric frame
79       * and overrides of default maximal checking interval and convergence threshold values.
80       * @param maxCheck maximum checking interval (s)
81       * @param threshold maximum divergence threshold (s)
82       * @param topo reference to a topocentric model
83       * @see #withConstantElevation(double)
84       * @see #withElevationMask(ElevationMask)
85       * @see #withRefraction(AtmosphericRefractionModel)
86       */
87      public FieldElevationDetector(final T maxCheck, final T threshold, final TopocentricFrame topo) {
88          this(new FieldEventDetectionSettings<>(maxCheck.getReal(), threshold, DEFAULT_MAX_ITER),
89               new FieldStopOnDecreasing<>(), 0.0, null, null, topo);
90      }
91  
92      /** Protected constructor with full parameters.
93       * <p>
94       * This constructor is not public as users are expected to use the builder
95       * API with the various {@code withXxx()} methods to set up the instance
96       * in a readable manner without using a huge amount of parameters.
97       * </p>
98       * @param detectionSettings detection settings
99       * @param handler event handler to call at event occurrences
100      * @param minElevation minimum elevation in radians (rad)
101      * @param mask reference to elevation mask
102      * @param refractionModel reference to refraction model
103      * @param topo reference to a topocentric model
104      * @since 12.2
105      */
106     protected FieldElevationDetector(final FieldEventDetectionSettings<T> detectionSettings, final FieldEventHandler<T> handler,
107                                      final double minElevation, final ElevationMask mask,
108                                      final AtmosphericRefractionModel refractionModel,
109                                      final TopocentricFrame topo) {
110         super(detectionSettings, handler);
111         this.minElevation    = minElevation;
112         this.elevationMask   = mask;
113         this.refractionModel = refractionModel;
114         this.topo            = topo;
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     protected FieldElevationDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
120                                                final FieldEventHandler<T> newHandler) {
121         return new FieldElevationDetector<>(detectionSettings, newHandler,
122                                             minElevation, elevationMask, refractionModel, topo);
123     }
124 
125     /**
126      * Returns the currently configured elevation mask.
127      * @return elevation mask
128      * (null if instance has been configured with {@link #withConstantElevation(double)}
129      * @see #withElevationMask(ElevationMask)
130      */
131     public ElevationMask getElevationMask() {
132         return this.elevationMask;
133     }
134 
135     /**
136      * Returns the currently configured minimum valid elevation value.
137      * @return minimum elevation value
138      * ({@code Double.NaN} if instance has been configured with {@link #withElevationMask(ElevationMask)}
139      * @see #withConstantElevation(double)
140      */
141     public double getMinElevation() {
142         return this.minElevation;
143     }
144 
145     /**
146      * Returns the currently configured refraction model.
147      * @return refraction model
148      * @see #withRefraction(AtmosphericRefractionModel)
149      */
150     public AtmosphericRefractionModel getRefractionModel() {
151         return this.refractionModel;
152     }
153 
154     /**
155      * Returns the currently configured topocentric frame definitions.
156      * @return topocentric frame definition
157      */
158     public TopocentricFrame getTopocentricFrame() {
159         return this.topo;
160     }
161 
162     /** Compute the value of the switching function.
163      * This function measures the difference between the current elevation
164      * (and azimuth if necessary) and the reference mask or minimum value.
165      * @param s the current state information: date, kinematics, attitude
166      * @return value of the switching function
167      */
168     @Override
169     public T g(final FieldSpacecraftState<T> s) {
170 
171         final FieldStaticTransform<T> t = s.getFrame().getStaticTransformTo(topo, s.getDate());
172         final FieldVector3D<T> extPointTopo = t.transformPosition(s.getPosition());
173         final T trueElevation = extPointTopo.getDelta();
174 
175         final T calculatedElevation;
176         if (refractionModel != null) {
177             calculatedElevation = trueElevation.add(refractionModel.getRefraction(trueElevation.getReal()));
178         } else {
179             calculatedElevation = trueElevation;
180         }
181 
182         if (elevationMask != null) {
183             final double azimuth = FastMath.atan2(extPointTopo.getY().getReal(), extPointTopo.getX().getReal());
184             return calculatedElevation.subtract(elevationMask.getElevation(azimuth));
185         } else {
186             return calculatedElevation.subtract(minElevation);
187         }
188 
189     }
190 
191     /**
192      * Setup the minimum elevation for detection.
193      * <p>
194      * This will override an elevation mask if it has been configured as such previously.
195      * </p>
196      * @param newMinElevation minimum elevation for visibility in radians (rad)
197      * @return a new detector with updated configuration (the instance is not changed)
198      * @see #getMinElevation()
199      * @since 6.1
200      */
201     public FieldElevationDetector<T> withConstantElevation(final double newMinElevation) {
202         return new FieldElevationDetector<>(getDetectionSettings(), getHandler(),
203                                             newMinElevation, null, refractionModel, topo);
204     }
205 
206     /**
207      * Setup the elevation mask for detection using the passed in mask object.
208      * @param newElevationMask elevation mask to use for the computation
209      * @return a new detector with updated configuration (the instance is not changed)
210      * @since 6.1
211      * @see #getElevationMask()
212      */
213     public FieldElevationDetector<T> withElevationMask(final ElevationMask newElevationMask) {
214         return new FieldElevationDetector<>(getDetectionSettings(), getHandler(),
215                                             Double.NaN, newElevationMask, refractionModel, topo);
216     }
217 
218     /**
219      * Setup the elevation detector to use an atmospheric refraction model in its
220      * calculations.
221      * <p>
222      * To disable the refraction when copying an existing elevation
223      * detector, call this method with a null argument.
224      * </p>
225      * @param newRefractionModel refraction model to use for the computation
226      * @return a new detector with updated configuration (the instance is not changed)
227      * @since 6.1
228      * @see #getRefractionModel()
229      */
230     public FieldElevationDetector<T> withRefraction(final AtmosphericRefractionModel newRefractionModel) {
231         return new FieldElevationDetector<>(getDetectionSettings(), getHandler(),
232                                             minElevation, elevationMask, newRefractionModel, topo);
233     }
234 
235 }