1   /* Copyright 2002-2021 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.ode.events.Action;
20  import org.orekit.propagation.SpacecraftState;
21  import org.orekit.propagation.events.handlers.EventHandler;
22  import org.orekit.time.AbsoluteDate;
23  
24  /** Common parts shared by several orbital events finders.
25   * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
26   * @author Luc Maisonobe
27   */
28  public abstract class AbstractDetector<T extends AbstractDetector<T>> implements EventDetector {
29  
30      /** Default maximum checking interval (s). */
31      public static final double DEFAULT_MAXCHECK = 600;
32  
33      /** Default convergence threshold (s). */
34      public static final double DEFAULT_THRESHOLD = 1.e-6;
35  
36      /** Default maximum number of iterations in the event time search. */
37      public static final int DEFAULT_MAX_ITER = 100;
38  
39      /** Max check interval. */
40      private final double maxCheck;
41  
42      /** Convergence threshold. */
43      private final double threshold;
44  
45      /** Maximum number of iterations in the event time search. */
46      private final int maxIter;
47  
48      /** Default handler for event overrides. */
49      private final EventHandler<? super T> handler;
50  
51      /** Propagation direction. */
52      private boolean forward;
53  
54      /** Build a new instance.
55       * @param maxCheck maximum checking interval (s)
56       * @param threshold convergence threshold (s)
57       * @param maxIter maximum number of iterations in the event time search
58       * @param handler event handler to call at event occurrences
59       */
60      protected AbstractDetector(final double maxCheck, final double threshold, final int maxIter,
61                                 final EventHandler<? super T> handler) {
62          this.maxCheck  = maxCheck;
63          this.threshold = threshold;
64          this.maxIter   = maxIter;
65          this.handler   = handler;
66          this.forward   = true;
67      }
68  
69      /**
70       * {@inheritDoc}
71       *
72       * <p> This implementation sets the direction of propagation and initializes the event
73       * handler. If a subclass overrides this method it should call {@code
74       * super.init(s0, t)}.
75       */
76      @SuppressWarnings("unchecked")
77      public void init(final SpacecraftState s0,
78                       final AbsoluteDate t) {
79          forward = t.durationFrom(s0.getDate()) >= 0.0;
80          getHandler().init(s0, t, (T) this);
81      }
82  
83      /** {@inheritDoc} */
84      public abstract double g(SpacecraftState s);
85  
86      /** {@inheritDoc} */
87      public double getMaxCheckInterval() {
88          return maxCheck;
89      }
90  
91      /** {@inheritDoc} */
92      public int getMaxIterationCount() {
93          return maxIter;
94      }
95  
96      /** {@inheritDoc} */
97      public double getThreshold() {
98          return threshold;
99      }
100 
101     /**
102      * Setup the maximum checking interval.
103      * <p>
104      * This will override a maximum checking interval if it has been configured previously.
105      * </p>
106      * @param newMaxCheck maximum checking interval (s)
107      * @return a new detector with updated configuration (the instance is not changed)
108      * @since 6.1
109      */
110     public T withMaxCheck(final double newMaxCheck) {
111         return create(newMaxCheck, getThreshold(), getMaxIterationCount(), getHandler());
112     }
113 
114     /**
115      * Setup the maximum number of iterations in the event time search.
116      * <p>
117      * This will override a number of iterations if it has been configured previously.
118      * </p>
119      * @param newMaxIter maximum number of iterations in the event time search
120      * @return a new detector with updated configuration (the instance is not changed)
121      * @since 6.1
122      */
123     public T withMaxIter(final int newMaxIter) {
124         return create(getMaxCheckInterval(), getThreshold(), newMaxIter,  getHandler());
125     }
126 
127     /**
128      * Setup the convergence threshold.
129      * <p>
130      * This will override a convergence threshold if it has been configured previously.
131      * </p>
132      * @param newThreshold convergence threshold (s)
133      * @return a new detector with updated configuration (the instance is not changed)
134      * @since 6.1
135      */
136     public T withThreshold(final double newThreshold) {
137         return create(getMaxCheckInterval(), newThreshold, getMaxIterationCount(),  getHandler());
138     }
139 
140     /**
141      * Setup the event handler to call at event occurrences.
142      * <p>
143      * This will override a handler if it has been configured previously.
144      * </p>
145      * @param newHandler event handler to call at event occurrences
146      * @return a new detector with updated configuration (the instance is not changed)
147      * @since 6.1
148      */
149     public T withHandler(final EventHandler<? super T> newHandler) {
150         return create(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), newHandler);
151     }
152 
153     /** Get the handler.
154      * @return event handler to call at event occurrences
155      */
156     public EventHandler<? super T> getHandler() {
157         return handler;
158     }
159 
160     /** {@inheritDoc} */
161     public Action eventOccurred(final SpacecraftState s, final boolean increasing) {
162         @SuppressWarnings("unchecked")
163         final Action whatNext = getHandler().eventOccurred(s, (T) this, increasing);
164         return whatNext;
165     }
166 
167     /** {@inheritDoc} */
168     public SpacecraftState resetState(final SpacecraftState oldState) {
169         @SuppressWarnings("unchecked")
170         final SpacecraftState newState = getHandler().resetState((T) this, oldState);
171         return newState;
172     }
173 
174     /** Build a new instance.
175      * @param newMaxCheck maximum checking interval (s)
176      * @param newThreshold convergence threshold (s)
177      * @param newMaxIter maximum number of iterations in the event time search
178      * @param newHandler event handler to call at event occurrences
179      * @return a new instance of the appropriate sub-type
180      */
181     protected abstract T create(double newMaxCheck, double newThreshold,
182                                 int newMaxIter, EventHandler<? super T> newHandler);
183 
184     /** Check if the current propagation is forward or backward.
185      * @return true if the current propagation is forward
186      * @since 7.2
187      */
188     public boolean isForward() {
189         return forward;
190     }
191 
192 }