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.FieldKinematicTransform;
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  import org.orekit.utils.TimeStampedFieldPVCoordinates;
30  
31  /** Detector for elevation extremum with respect to a ground point.
32   * <p>This detector identifies when a spacecraft reaches its
33   * extremum elevation with respect to a ground point.</p>
34   * <p>
35   * As in most cases only the elevation maximum is needed and the
36   * minimum is often irrelevant, this detector is often wrapped into
37   * an {@link FieldEventSlopeFilter event slope filter} configured with
38   * {@link FilterType#TRIGGER_ONLY_DECREASING_EVENTS} (i.e. when the
39   * elevation derivative decreases from positive values to negative values,
40   * which correspond to a maximum). Setting up this filter saves some computation
41   * time as the elevation minimum occurrences are not even looked at. It is
42   * however still often necessary to do an additional filtering
43   * </p>
44   * @param <T> type of the field element
45   * @author Luc Maisonobe
46   * @since 12.0
47   */
48  public class FieldElevationExtremumDetector<T extends CalculusFieldElement<T>>
49      extends FieldAbstractDetector<FieldElevationExtremumDetector<T>, T> {
50  
51      /** Topocentric frame in which elevation should be evaluated. */
52      private final TopocentricFrame topo;
53  
54      /** Build a new detector.
55       * <p>The new instance uses default values for maximal checking interval
56       * ({@link #DEFAULT_MAX_CHECK}) and convergence threshold ({@link
57       * #DEFAULT_THRESHOLD}).</p>
58       * @param field field to which elements belong
59       * @param topo topocentric frame centered on ground point
60       */
61      public FieldElevationExtremumDetector(final Field<T> field, final TopocentricFrame topo) {
62          this(field.getZero().newInstance(DEFAULT_MAX_CHECK),
63               field.getZero().newInstance(DEFAULT_THRESHOLD),
64               topo);
65      }
66  
67      /** Build a detector.
68       * @param maxCheck maximal checking interval (s)
69       * @param threshold convergence threshold (s)
70       * @param topo topocentric frame centered on ground point
71       */
72      public FieldElevationExtremumDetector(final T maxCheck, final T threshold,
73                                            final TopocentricFrame topo) {
74          this(new FieldEventDetectionSettings<>(maxCheck.getReal(), threshold, DEFAULT_MAX_ITER), new FieldStopOnIncreasing<>(),
75               topo);
76      }
77  
78      /** Protected constructor with full parameters.
79       * <p>
80       * This constructor is not public as users are expected to use the builder
81       * API with the various {@code withXxx()} methods to set up the instance
82       * in a readable manner without using a huge amount of parameters.
83       * </p>
84       * @param detectionSettings event detection settings
85       * @param handler event handler to call at event occurrences
86       * @param topo topocentric frame centered on ground point
87       */
88      protected FieldElevationExtremumDetector(final FieldEventDetectionSettings<T> detectionSettings,
89                                               final FieldEventHandler<T> handler,
90                                               final TopocentricFrame topo) {
91          super(detectionSettings, handler);
92          this.topo = topo;
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      protected FieldElevationExtremumDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
98                                                         final FieldEventHandler<T> newHandler) {
99          return new FieldElevationExtremumDetector<>(detectionSettings, newHandler, topo);
100     }
101 
102     /**
103      * Returns the topocentric frame centered on ground point.
104      * @return topocentric frame centered on ground point
105      */
106     public TopocentricFrame getTopocentricFrame() {
107         return this.topo;
108     }
109 
110     /** Get the elevation value.
111      * @param s the current state information: date, kinematics, attitude
112      * @return spacecraft elevation
113      */
114     public T getElevation(final FieldSpacecraftState<T> s) {
115         return topo.getElevation(s.getPosition(), s.getFrame(), s.getDate());
116     }
117 
118     /** Compute the value of the detection function.
119      * <p>
120      * The value is the spacecraft elevation first time derivative.
121      * </p>
122      * @param s the current state information: date, kinematics, attitude
123      * @return spacecraft elevation first time derivative
124      */
125     public T g(final FieldSpacecraftState<T> s) {
126 
127         // get position, velocity acceleration of spacecraft in topocentric frame
128         final FieldKinematicTransform<T> inertToTopo = s.getFrame().getKinematicTransformTo(topo, s.getDate());
129         final TimeStampedFieldPVCoordinates<T> pvTopo = inertToTopo.transformOnlyPV(s.getPVCoordinates());
130 
131         // convert the coordinates to UnivariateDerivative1 based vector
132         // instead of having vector position, then vector velocity then vector acceleration
133         // we get one vector and each coordinate is a DerivativeStructure containing
134         // value, first time derivative (we don't need second time derivative here)
135         final FieldVector3D<FieldUnivariateDerivative1<T>> pvDS = pvTopo.toUnivariateDerivative1Vector();
136 
137         // compute elevation and its first time derivative
138         final FieldUnivariateDerivative1<T> elevation = pvDS.getZ().divide(pvDS.getNorm()).asin();
139 
140         // return elevation first time derivative
141         return elevation.getDerivative(1);
142 
143     }
144 
145 }