1   /* Contributed in the public domain.
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.orekit.propagation.FieldSpacecraftState;
21  import org.orekit.propagation.events.handlers.FieldContinueOnEvent;
22  import org.orekit.propagation.events.handlers.FieldEventHandler;
23  import org.orekit.time.FieldAbsoluteDate;
24  
25  /**
26   * An event detector that negates the sign on another event detector's {@link
27   * FieldEventDetector#g(FieldSpacecraftState) g} function.
28   *
29   * @since 12.0
30   * @param <T> type of the field element
31   * @author Evan Ward
32   * @author Luc Maisonobe
33   */
34  public class FieldNegateDetector<T extends CalculusFieldElement<T>>  extends FieldAbstractDetector<FieldNegateDetector<T>, T> {
35  
36      /** the delegate event detector. */
37      private final FieldEventDetector<T> original;
38  
39      /**
40       * Create a new event detector that negates an existing event detector.
41       *
42       * <p> This detector will be initialized with the same {@link
43       * FieldEventDetector#getMaxCheckInterval()}, {@link FieldEventDetector#getThreshold()}, and
44       * {@link FieldEventDetector#getMaxIterationCount()} as {@code original}. Initially this
45       * detector will use the {@link FieldContinueOnEvent} event handler.
46       *
47       * @param original detector.
48       */
49      public FieldNegateDetector(final FieldEventDetector<T> original) {
50          this(original.getMaxCheckInterval(),
51               original.getThreshold(),
52               original.getMaxIterationCount(),
53               new FieldContinueOnEvent<>(),
54               original);
55      }
56  
57      /**
58       * Private constructor.
59       *
60       * @param newMaxCheck  max check interval.
61       * @param newThreshold convergence threshold in seconds.
62       * @param newMaxIter   max iterations.
63       * @param newHandler   event handler.
64       * @param original     event detector.
65       */
66      protected FieldNegateDetector(final FieldAdaptableInterval<T> newMaxCheck,
67                                    final T newThreshold,
68                                    final int newMaxIter,
69                                    final FieldEventHandler<T> newHandler,
70                                    final FieldEventDetector<T> original) {
71          super(newMaxCheck, newThreshold, newMaxIter, newHandler);
72          this.original = original;
73      }
74  
75      /**
76       * Get the delegate event detector.
77       * @return the delegate event detector
78       */
79      public FieldEventDetector<T> getOriginal() {
80          return original;
81      }
82  
83      @Override
84      public void init(final FieldSpacecraftState<T> s0,
85                       final FieldAbsoluteDate<T> t) {
86          super.init(s0, t);
87          original.init(s0, t);
88      }
89  
90      @Override
91      public T g(final FieldSpacecraftState<T> s) {
92          return original.g(s).negate();
93      }
94  
95      @Override
96      protected FieldNegateDetector<T> create(final FieldAdaptableInterval<T> newMaxCheck,
97                                              final T newThreshold,
98                                              final int newMaxIter,
99                                              final FieldEventHandler<T> newHandler) {
100         return new FieldNegateDetector<>(newMaxCheck, newThreshold, newMaxIter, newHandler,
101                                          original);
102     }
103 
104 }