1   /* Copyright 2002-2025 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_MAX_CHECK}) 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_MAX_CHECK, 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(new EventDetectionSettings(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 detectionSettings event detection settings
76       * @param handler event handler to call at event occurrences
77       * @param body body on which the longitude is defined
78       * @param longitude longitude to be crossed
79       * @since 13.0
80       */
81      protected LongitudeCrossingDetector(final EventDetectionSettings detectionSettings, final EventHandler handler,
82                                          final OneAxisEllipsoid body, final double longitude) {
83  
84          super(detectionSettings, handler);
85  
86          this.body      = body;
87          this.longitude = longitude;
88  
89          // we filter out spurious longitude crossings occurring at the antimeridian
90          final RawLongitudeCrossingDetector raw = new RawLongitudeCrossingDetector(detectionSettings, new ContinueOnEvent());
91          final EnablingPredicate predicate =
92              (state, detector, g) -> FastMath.abs(g) < 0.5 * FastMath.PI;
93          this.filtering = new EventEnablingPredicateFilter(raw, predicate);
94  
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      protected LongitudeCrossingDetector create(final EventDetectionSettings detectionSettings,
100                                                final EventHandler newHandler) {
101         return new LongitudeCrossingDetector(detectionSettings, newHandler, body, longitude);
102     }
103 
104     /** Get the body on which the geographic zone is defined.
105      * @return body on which the geographic zone is defined
106      */
107     public OneAxisEllipsoid getBody() {
108         return body;
109     }
110 
111     /** Get the fixed longitude to be crossed (radians).
112      * @return fixed longitude to be crossed (radians)
113      */
114     public double getLongitude() {
115         return longitude;
116     }
117 
118     /**  {@inheritDoc} */
119     @Override
120     public void init(final SpacecraftState s0, final AbsoluteDate t) {
121         super.init(s0, t);
122         filtering.init(s0, t);
123     }
124 
125     /** Compute the value of the detection function.
126      * <p>
127      * The value is the longitude difference between the spacecraft and the fixed
128      * longitude to be crossed, with some sign tweaks to ensure continuity.
129      * These tweaks imply the {@code increasing} flag in events detection becomes
130      * irrelevant here! As an example, the longitude of a prograde spacecraft
131      * will always increase, but this g function will increase and decrease so it
132      * will cross the zero value once per orbit, in increasing and decreasing
133      * directions on alternate orbits. If eastwards and westwards crossing have to
134      * be distinguished, the velocity direction has to be checked instead of looking
135      * at the {@code increasing} flag.
136      * </p>
137      * @param s the current state information: date, kinematics, attitude
138      * @return longitude difference between the spacecraft and the fixed
139      * longitude, with some sign tweaks to ensure continuity
140      */
141     public double g(final SpacecraftState s) {
142         return filtering.g(s);
143     }
144 
145     private class RawLongitudeCrossingDetector extends AbstractDetector<RawLongitudeCrossingDetector> {
146 
147         /** Protected constructor with full parameters.
148          * <p>
149          * This constructor is not public as users are expected to use the builder
150          * API with the various {@code withXxx()} methods to set up the instance
151          * in a readable manner without using a huge amount of parameters.
152          * </p>
153          * @param detectionSettings event detection settings
154          * @param handler event handler to call at event occurrences
155          */
156         protected RawLongitudeCrossingDetector(final EventDetectionSettings detectionSettings,
157                                                final EventHandler handler) {
158             super(detectionSettings, handler);
159         }
160 
161         /** {@inheritDoc} */
162         @Override
163         protected RawLongitudeCrossingDetector create(final EventDetectionSettings detectionSettings,
164                                                       final EventHandler newHandler) {
165             return new RawLongitudeCrossingDetector(detectionSettings, newHandler);
166         }
167 
168         /** Compute the value of the detection function.
169          * <p>
170          * The value is the longitude difference between the spacecraft and the fixed
171          * longitude to be crossed, and it <em>does</em> change sign twice around
172          * the central body: once at expected longitude and once at antimeridian.
173          * The second sign change is a spurious one and is filtered out by the
174          * outer class.
175          * </p>
176          * @param s the current state information: date, kinematics, attitude
177          * @return longitude difference between the spacecraft and the fixed
178          * longitude
179          */
180         public double g(final SpacecraftState s) {
181 
182             // convert state to geodetic coordinates
183             final GeodeticPoint gp = body.transform(s.getPosition(),
184                                                     s.getFrame(), s.getDate());
185 
186             // longitude difference
187             return MathUtils.normalizeAngle(gp.getLongitude() - longitude, 0.0);
188 
189         }
190 
191     }
192 
193 }