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  
18  package org.orekit.propagation.events.intervals;
19  
20  import org.hipparchus.CalculusFieldElement;
21  import org.hipparchus.util.FastMath;
22  import org.orekit.propagation.FieldSpacecraftState;
23  import org.orekit.propagation.events.FieldEventDetector;
24  
25  /** This interface represents an event checking interval that depends on state.
26  *
27  * @see FieldEventDetector
28  * @author Luc Maisonobe
29  * @since 12.0
30  * @param <T> the type of the field elements
31  */
32  @FunctionalInterface
33  public interface FieldAdaptableInterval<T extends CalculusFieldElement<T>> {
34  
35      /**
36       * Get the current value of maximal time interval between events handler checks.
37       *
38       * @param state     current state
39       * @param isForward direction of propagation
40       * @return current value of maximal time interval between events handler checks (only as a double)
41       */
42      double currentInterval(FieldSpacecraftState<T> state, boolean isForward);
43  
44      /**
45       * Method creating a constant interval provider.
46       * @param <T> field type
47       * @param constantInterval value of constant interval
48       * @return adaptable interval ready to be added to an event detector
49       * @since 12.1
50       */
51      static <T extends CalculusFieldElement<T>> FieldAdaptableInterval<T> of(final double constantInterval) {
52          return (state, isForward) -> constantInterval;
53      }
54  
55      /**
56       * Method creating an interval provider from a non-Field one.
57       * @param <T> field type
58       * @param adaptableInterval non-Field interval
59       * @return adaptable interval ready to be added to an event detector
60       * @since 13.0
61       */
62      static <T extends CalculusFieldElement<T>> FieldAdaptableInterval<T> of(final AdaptableInterval adaptableInterval) {
63          return (state, isForward) -> adaptableInterval.currentInterval(state.toSpacecraftState(), isForward);
64      }
65  
66      /**
67       * Method creating an interval taking the minimum value of all candidates.
68       * @param defaultMaxCheck default value if no intervals is given as inputv
69       * @param adaptableIntervals intervals
70       * @param <T> field type
71       * @return adaptable interval ready to be added to an event detector
72       * @since 13.0
73       */
74      @SafeVarargs
75      static <T extends CalculusFieldElement<T>> FieldAdaptableInterval<T> of(final double defaultMaxCheck,
76                                                                              final FieldAdaptableInterval<T>... adaptableIntervals) {
77          return (state, isForward) -> {
78              double maxCheck = defaultMaxCheck;
79              for (final FieldAdaptableInterval<T> interval : adaptableIntervals) {
80                  maxCheck = FastMath.min(maxCheck, interval.currentInterval(state, isForward));
81              }
82              return maxCheck;
83          };
84      }
85  }
86