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