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