1   /* Copyright 2023-2025 Alberto Ferrero
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    * Alberto Ferrero 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.orekit.bodies.GeodeticPoint;
21  import org.orekit.bodies.OneAxisEllipsoid;
22  import org.orekit.propagation.SpacecraftState;
23  import org.orekit.propagation.events.handlers.EventHandler;
24  import org.orekit.propagation.events.handlers.StopOnDecreasing;
25  
26  
27  /** Detector for geographic longitude crossing.
28   * <p>This detector identifies when a spacecraft crosses a fixed
29   * longitude range with respect to a central body.</p>
30   * @author Alberto Ferrero
31   * @since 12.0
32   */
33  public class LongitudeRangeCrossingDetector extends AbstractDetector<LongitudeRangeCrossingDetector> {
34  
35      /** Body on which the longitude is defined. */
36      private final OneAxisEllipsoid body;
37  
38      /** Fixed longitude to be crossed, lower boundary in radians. */
39      private final double fromLongitude;
40  
41      /** Fixed longitude to be crossed, upper boundary in radians. */
42      private final double toLongitude;
43  
44      /**
45       * Sign, to get reversed inclusion longitude range (lower > upper).
46       */
47      private final double sign;
48  
49      /** Build a new detector.
50       * <p>The new instance uses default values for maximal checking interval
51       * ({@link #DEFAULT_MAX_CHECK}) and convergence threshold ({@link
52       * #DEFAULT_THRESHOLD}).</p>
53       * @param body body on which the longitude is defined
54       * @param fromLongitude longitude to be crossed, lower range boundary
55       * @param toLongitude longitude to be crossed, upper range boundary
56       */
57      public LongitudeRangeCrossingDetector(final OneAxisEllipsoid body, final double fromLongitude, final double toLongitude) {
58          this(DEFAULT_MAX_CHECK, DEFAULT_THRESHOLD, body, fromLongitude, toLongitude);
59      }
60  
61      /** Build a detector.
62       * @param maxCheck maximal checking interval (s)
63       * @param threshold convergence threshold (s)
64       * @param body body on which the longitude is defined
65       * @param fromLongitude longitude to be crossed, lower range boundary
66       * @param toLongitude longitude to be crossed, upper range boundary
67       */
68      public LongitudeRangeCrossingDetector(final double maxCheck, final double threshold,
69                                            final OneAxisEllipsoid body, final double fromLongitude, final double toLongitude) {
70          this(new EventDetectionSettings(maxCheck, threshold, DEFAULT_MAX_ITER), new StopOnDecreasing(),
71               body, fromLongitude, toLongitude);
72      }
73  
74      /** Private constructor with full parameters.
75       * <p>
76       * This constructor is private as users are expected to use the builder
77       * API with the various {@code withXxx()} methods to set up the instance
78       * in a readable manner without using a huge amount of parameters.
79       * </p>
80       * @param detectionSettings event detection settings
81       * @param handler event handler to call at event occurrences
82       * @param body body on which the longitude is defined
83       * @param fromLongitude longitude to be crossed, lower range boundary
84       * @param toLongitude longitude to be crossed, upper range boundary
85       * @since 13.0
86       */
87      protected LongitudeRangeCrossingDetector(final EventDetectionSettings detectionSettings, final EventHandler handler,
88                                               final OneAxisEllipsoid body, final double fromLongitude, final double toLongitude) {
89          super(detectionSettings, handler);
90          this.body     = body;
91          this.fromLongitude = ensureLongitudePositiveContinuity(fromLongitude);
92          this.toLongitude = ensureLongitudePositiveContinuity(toLongitude);
93          this.sign = FastMath.signum(this.toLongitude - this.fromLongitude);
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      protected LongitudeRangeCrossingDetector create(final EventDetectionSettings detectionSettings,
99                                                      final EventHandler newHandler) {
100         return new LongitudeRangeCrossingDetector(detectionSettings, newHandler, body, fromLongitude, toLongitude);
101     }
102 
103     /** Get the body on which the geographic zone is defined.
104      * @return body on which the geographic zone is defined
105      */
106     public OneAxisEllipsoid getBody() {
107         return body;
108     }
109 
110     /** Get the fixed longitude range to be crossed (radians), lower boundary.
111      * @return fixed lower boundary longitude range to be crossed (radians)
112      */
113     public double getFromLongitude() {
114         return getLongitudeOverOriginalRange(fromLongitude);
115     }
116 
117     /** Get the fixed longitude range to be crossed (radians), upper boundary.
118      * @return fixed upper boundary longitude range to be crossed (radians)
119      */
120     public double getToLongitude() {
121         return getLongitudeOverOriginalRange(toLongitude);
122     }
123 
124     /**
125      * Ensure continuity for negative angles, as longitude defined as [-PI, PI], transform negative to positive.
126      * New longitude angle definition from [0, 2 PI].
127      * @param longitude original longitude value
128      * @return positive range longitude
129      */
130     private double ensureLongitudePositiveContinuity(final double longitude) {
131         return longitude < 0 ? longitude + 2 * FastMath.PI : longitude;
132     }
133 
134     /**
135      * Get longitude shifted over the original range [-PI, PI].
136      * @param longitude longitude value to convert
137      * @return original range longitude
138      */
139     private double getLongitudeOverOriginalRange(final double longitude) {
140         return longitude > FastMath.PI ? longitude - 2 * FastMath.PI : longitude;
141     }
142 
143     /** Compute the value of the detection function.
144      * <p>
145      * The value is positive if the spacecraft longitude is inside the longitude range.
146      * The longitude value is reflected from [-PI, +PI] to [0, 2 PI] to ensure continuity.
147      * </p>
148      * @param s the current state information: date, kinematics, attitude
149      * @return positive if spacecraft inside the range
150      */
151     public double g(final SpacecraftState s) {
152 
153         // convert state to geodetic coordinates
154         final GeodeticPoint gp = body.transform(s.getPVCoordinates().getPosition(),
155             s.getFrame(), s.getDate());
156 
157         // point longitude
158         final double longitude = ensureLongitudePositiveContinuity(gp.getLongitude());
159 
160         // inside or outside longitude range
161         return sign * (longitude - fromLongitude) * (toLongitude - longitude);
162 
163     }
164 
165 }