1   /* Copyright 2002-2024 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.util.FastMath;
20  import org.hipparchus.util.MathUtils;
21  import org.orekit.bodies.GeodeticPoint;
22  import org.orekit.bodies.OneAxisEllipsoid;
23  import org.orekit.propagation.SpacecraftState;
24  import org.orekit.propagation.events.handlers.ContinueOnEvent;
25  import org.orekit.propagation.events.handlers.EventHandler;
26  import org.orekit.propagation.events.handlers.StopOnIncreasing;
27  import org.orekit.time.AbsoluteDate;
28  
29  /** Detector for geographic longitude crossing.
30   * <p>This detector identifies when a spacecraft crosses a fixed
31   * longitude with respect to a central body.</p>
32   * @author Luc Maisonobe
33   * @since 7.1
34   */
35  public class LongitudeCrossingDetector extends AbstractDetector<LongitudeCrossingDetector> {
36  
37      /** Body on which the longitude is defined. */
38      private OneAxisEllipsoid body;
39  
40      /** Fixed longitude to be crossed. */
41      private final double longitude;
42  
43      /** Filtering detector. */
44      private final EventEnablingPredicateFilter filtering;
45  
46      /** Build a new detector.
47       * <p>The new instance uses default values for maximal checking interval
48       * ({@link #DEFAULT_MAXCHECK}) and convergence threshold ({@link
49       * #DEFAULT_THRESHOLD}).</p>
50       * @param body body on which the longitude is defined
51       * @param longitude longitude to be crossed
52       */
53      public LongitudeCrossingDetector(final OneAxisEllipsoid body, final double longitude) {
54          this(DEFAULT_MAXCHECK, DEFAULT_THRESHOLD, body, longitude);
55      }
56  
57      /** Build a detector.
58       * @param maxCheck maximal checking interval (s)
59       * @param threshold convergence threshold (s)
60       * @param body body on which the longitude is defined
61       * @param longitude longitude to be crossed
62       */
63      public LongitudeCrossingDetector(final double maxCheck, final double threshold,
64                                      final OneAxisEllipsoid body, final double longitude) {
65          this(AdaptableInterval.of(maxCheck), threshold, DEFAULT_MAX_ITER, new StopOnIncreasing(),
66               body, longitude);
67      }
68  
69      /** Protected constructor with full parameters.
70       * <p>
71       * This constructor is not public as users are expected to use the builder
72       * API with the various {@code withXxx()} methods to set up the instance
73       * in a readable manner without using a huge amount of parameters.
74       * </p>
75       * @param maxCheck maximum checking interval
76       * @param threshold convergence threshold (s)
77       * @param maxIter maximum number of iterations in the event time search
78       * @param handler event handler to call at event occurrences
79       * @param body body on which the longitude is defined
80       * @param longitude longitude to be crossed
81       */
82      protected LongitudeCrossingDetector(final AdaptableInterval maxCheck, final double threshold,
83                                          final int maxIter, final EventHandler handler,
84                                          final OneAxisEllipsoid body, final double longitude) {
85  
86          super(maxCheck, threshold, maxIter, handler);
87  
88          this.body      = body;
89          this.longitude = longitude;
90  
91          // we filter out spurious longitude crossings occurring at the antimeridian
92          final RawLongitudeCrossingDetector raw = new RawLongitudeCrossingDetector(maxCheck, threshold, maxIter,
93                                                                                    new ContinueOnEvent());
94          final EnablingPredicate predicate =
95              (state, detector, g) -> FastMath.abs(g) < 0.5 * FastMath.PI;
96          this.filtering = new EventEnablingPredicateFilter(raw, predicate);
97  
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     protected LongitudeCrossingDetector create(final AdaptableInterval newMaxCheck, final double newThreshold, final int newMaxIter,
103                                                final EventHandler newHandler) {
104         return new LongitudeCrossingDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
105                                              body, longitude);
106     }
107 
108     /** Get the body on which the geographic zone is defined.
109      * @return body on which the geographic zone is defined
110      */
111     public OneAxisEllipsoid getBody() {
112         return body;
113     }
114 
115     /** Get the fixed longitude to be crossed (radians).
116      * @return fixed longitude to be crossed (radians)
117      */
118     public double getLongitude() {
119         return longitude;
120     }
121 
122     /**  {@inheritDoc} */
123     public void init(final SpacecraftState s0, final AbsoluteDate t) {
124         filtering.init(s0, t);
125     }
126 
127     /** Compute the value of the detection function.
128      * <p>
129      * The value is the longitude difference between the spacecraft and the fixed
130      * longitude to be crossed, with some sign tweaks to ensure continuity.
131      * These tweaks imply the {@code increasing} flag in events detection becomes
132      * irrelevant here! As an example, the longitude of a prograde spacecraft
133      * will always increase, but this g function will increase and decrease so it
134      * will cross the zero value once per orbit, in increasing and decreasing
135      * directions on alternate orbits. If eastwards and westwards crossing have to
136      * be distinguished, the velocity direction has to be checked instead of looking
137      * at the {@code increasing} flag.
138      * </p>
139      * @param s the current state information: date, kinematics, attitude
140      * @return longitude difference between the spacecraft and the fixed
141      * longitude, with some sign tweaks to ensure continuity
142      */
143     public double g(final SpacecraftState s) {
144         return filtering.g(s);
145     }
146 
147     private class RawLongitudeCrossingDetector extends AbstractDetector<RawLongitudeCrossingDetector> {
148 
149         /** Protected constructor with full parameters.
150          * <p>
151          * This constructor is not public as users are expected to use the builder
152          * API with the various {@code withXxx()} methods to set up the instance
153          * in a readable manner without using a huge amount of parameters.
154          * </p>
155          * @param maxCheck maximum checking interval
156          * @param threshold convergence threshold (s)
157          * @param maxIter maximum number of iterations in the event time search
158          * @param handler event handler to call at event occurrences
159          */
160         protected RawLongitudeCrossingDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
161                                                final EventHandler handler) {
162             super(maxCheck, threshold, maxIter, handler);
163         }
164 
165         /** {@inheritDoc} */
166         @Override
167         protected RawLongitudeCrossingDetector create(final AdaptableInterval newMaxCheck, final double newThreshold,
168                                                       final int newMaxIter,
169                                                       final EventHandler newHandler) {
170             return new RawLongitudeCrossingDetector(newMaxCheck, newThreshold, newMaxIter, newHandler);
171         }
172 
173         /** Compute the value of the detection function.
174          * <p>
175          * The value is the longitude difference between the spacecraft and the fixed
176          * longitude to be crossed, and it <em>does</em> change sign twice around
177          * the central body: once at expected longitude and once at antimeridian.
178          * The second sign change is a spurious one and is filtered out by the
179          * outer class.
180          * </p>
181          * @param s the current state information: date, kinematics, attitude
182          * @return longitude difference between the spacecraft and the fixed
183          * longitude
184          */
185         public double g(final SpacecraftState s) {
186 
187             // convert state to geodetic coordinates
188             final GeodeticPoint gp = body.transform(s.getPosition(),
189                                                     s.getFrame(), s.getDate());
190 
191             // longitude difference
192             return MathUtils.normalizeAngle(gp.getLongitude() - longitude, 0.0);
193 
194         }
195 
196     }
197 
198 }