1   /* Copyright 2022-2026 Romain Serra
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.functions;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.orekit.propagation.FieldSpacecraftState;
21  import org.orekit.propagation.SpacecraftState;
22  
23  /** Event function modifier.
24   * Default implementations simply wrap the base function.
25   * @author Romain Serra
26   * @see EventFunction
27   * @since 14.0
28   */
29  public interface EventFunctionModifier extends EventFunction {
30  
31      /** Get the base event function.
32       * @return base event function
33       */
34      EventFunction getBaseFunction();
35  
36      @Override
37      default double value(final SpacecraftState state) {
38          return getBaseFunction().value(state);
39      }
40  
41      @Override
42      default <T extends CalculusFieldElement<T>> T value(final FieldSpacecraftState<T> fieldState) {
43          return getBaseFunction().value(fieldState);
44      }
45  
46      @Override
47      default boolean dependsOnTimeOnly() {
48          return getBaseFunction().dependsOnTimeOnly();
49      }
50  
51      @Override
52      default boolean dependsOnMainVariablesOnly() {
53          return getBaseFunction().dependsOnMainVariablesOnly();
54      }
55  
56      /** Create a modifier that adds a real increment to the event function value.
57       * @param eventFunction base event function
58       * @param increment real increment to add
59       * @return modifier wrapping the base function
60       */
61      static EventFunctionModifier addReal(final EventFunction eventFunction, final double increment) {
62          return new EventFunctionModifier() {
63              @Override
64              public EventFunction getBaseFunction() {
65                  return eventFunction;
66              }
67  
68              @Override
69              public double value(final SpacecraftState state) {
70                  return eventFunction.value(state) + increment;
71              }
72  
73              @Override
74              public <T extends CalculusFieldElement<T>> T value(final FieldSpacecraftState<T> fieldState) {
75                  return eventFunction.value(fieldState).add(increment);
76              }
77          };
78      }
79  
80      /** Create a modifier that adds a field element increment to the event function value.
81       * @param <S> type of the field element increment
82       * @param eventFunction base event function
83       * @param increment field element increment to add
84       * @return modifier wrapping the base function
85       */
86      static <S extends CalculusFieldElement<S>> EventFunctionModifier addFieldValue(final EventFunction eventFunction,
87                                                                                     final S increment) {
88          return new EventFunctionModifier() {
89              @Override
90              public EventFunction getBaseFunction() {
91                  return eventFunction;
92              }
93  
94              @Override
95              public double value(final SpacecraftState state) {
96                  return eventFunction.value(state) + increment.getReal();
97              }
98  
99              @Override
100             @SuppressWarnings("unchecked")
101             public <T extends CalculusFieldElement<T>> T value(final FieldSpacecraftState<T> fieldState) {
102                 final T g = eventFunction.value(fieldState);
103                 if (g.getField().equals(increment.getField())) {
104                     return (T) ((S) g).add(increment);
105                 } else {
106                     return g.add(increment.getReal());
107                 }
108             }
109         };
110     }
111 }