1   /* Copyright 2022-2025 Luc Maisonobe
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.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative1;
22  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
23  import org.hipparchus.ode.events.FieldEventSlopeFilter;
24  import org.orekit.frames.FieldStaticTransform;
25  import org.orekit.frames.TopocentricFrame;
26  import org.orekit.propagation.FieldSpacecraftState;
27  import org.orekit.propagation.events.handlers.FieldEventHandler;
28  import org.orekit.propagation.events.handlers.FieldStopOnIncreasing;
29  
30  /** Detector for elevation extremum with respect to a ground point.
31   * <p>This detector identifies when a spacecraft reaches its
32   * extremum elevation with respect to a ground point.</p>
33   * <p>
34   * As in most cases only the elevation maximum is needed and the
35   * minimum is often irrelevant, this detector is often wrapped into
36   * an {@link FieldEventSlopeFilter event slope filter} configured with
37   * {@link FilterType#TRIGGER_ONLY_DECREASING_EVENTS} (i.e. when the
38   * elevation derivative decreases from positive values to negative values,
39   * which correspond to a maximum). Setting up this filter saves some computation
40   * time as the elevation minimum occurrences are not even looked at. It is
41   * however still often necessary to do an additional filtering
42   * </p>
43   * @param <T> type of the field element
44   * @author Luc Maisonobe
45   * @since 12.0
46   */
47  public class FieldElevationExtremumDetector<T extends CalculusFieldElement<T>>
48      extends FieldAbstractTopocentricDetector<FieldElevationExtremumDetector<T>, T> {
49  
50      /** Build a new detector.
51       * <p>The new instance uses default values for maximal checking interval
52       * ({@link #DEFAULT_MAX_CHECK}) and convergence threshold ({@link
53       * #DEFAULT_THRESHOLD}).</p>
54       * @param field field to which elements belong
55       * @param topo topocentric frame centered on ground point
56       */
57      public FieldElevationExtremumDetector(final Field<T> field, final TopocentricFrame topo) {
58          this(field.getZero().newInstance(DEFAULT_MAX_CHECK),
59               field.getZero().newInstance(DEFAULT_THRESHOLD),
60               topo);
61      }
62  
63      /** Build a detector.
64       * @param maxCheck maximal checking interval (s)
65       * @param threshold convergence threshold (s)
66       * @param topo topocentric frame centered on ground point
67       */
68      public FieldElevationExtremumDetector(final T maxCheck, final T threshold,
69                                            final TopocentricFrame topo) {
70          this(new FieldEventDetectionSettings<>(maxCheck.getReal(), threshold, DEFAULT_MAX_ITER), new FieldStopOnIncreasing<>(),
71               topo);
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 topo topocentric frame centered on ground point
83       */
84      protected FieldElevationExtremumDetector(final FieldEventDetectionSettings<T> detectionSettings,
85                                               final FieldEventHandler<T> handler,
86                                               final TopocentricFrame topo) {
87          super(detectionSettings, handler, topo);
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      protected FieldElevationExtremumDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
93                                                         final FieldEventHandler<T> newHandler) {
94          return new FieldElevationExtremumDetector<>(detectionSettings, newHandler, getTopocentricFrame());
95      }
96  
97      /** Get the elevation value.
98       * @param s the current state information: date, kinematics, attitude
99       * @return spacecraft elevation
100      */
101     public T getElevation(final FieldSpacecraftState<T> s) {
102         return getTopocentricFrame().getElevation(s.getPosition(), s.getFrame(), s.getDate());
103     }
104 
105     /** Compute the value of the detection function.
106      * <p>
107      * The value is the spacecraft elevation first time derivative.
108      * </p>
109      * @param s the current state information: date, kinematics, attitude
110      * @return spacecraft elevation first time derivative
111      */
112     public T g(final FieldSpacecraftState<T> s) {
113 
114         // get position, velocity acceleration of spacecraft in topocentric frame
115         final FieldStaticTransform<FieldUnivariateDerivative1<T>> inertToTopo = s.getFrame().getStaticTransformTo(getTopocentricFrame(),
116                 s.getDate().toFUD1Field());
117         final FieldVector3D<FieldUnivariateDerivative1<T>> positionInert = s.getPVCoordinates().toUnivariateDerivative1Vector();
118         final FieldVector3D<FieldUnivariateDerivative1<T>> posTopo = inertToTopo.transformPosition(positionInert);
119 
120         // compute elevation and its first time derivative
121         final FieldUnivariateDerivative1<T> elevation = posTopo.getDelta();
122 
123         // return elevation first time derivative
124         return elevation.getFirstDerivative();
125 
126     }
127 
128 }