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