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.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_MAXCHECK}) 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_MAXCHECK, 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(AdaptableInterval.of(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 maxCheck maximum checking interval (s)
81       * @param threshold convergence threshold (s)
82       * @param maxIter maximum number of iterations in the event time search
83       * @param handler event handler to call at event occurrences
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      protected LongitudeRangeCrossingDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
89                                               final EventHandler handler,
90                                               final OneAxisEllipsoid body, final double fromLongitude, final double toLongitude) {
91          super(maxCheck, threshold, maxIter, handler);
92          this.body     = body;
93          this.fromLongitude = ensureLongitudePositiveContinuity(fromLongitude);
94          this.toLongitude = ensureLongitudePositiveContinuity(toLongitude);
95          this.sign = FastMath.signum(this.toLongitude - this.fromLongitude);
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     protected LongitudeRangeCrossingDetector create(final AdaptableInterval newMaxCheck,
101                                                     final double newThreshold,
102                                                     final int newMaxIter,
103                                                     final EventHandler newHandler) {
104         return new LongitudeRangeCrossingDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
105                                           body, fromLongitude, toLongitude);
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 range to be crossed (radians), lower boundary.
116      * @return fixed lower boundary longitude range to be crossed (radians)
117      */
118     public double getFromLongitude() {
119         return getLongitudeOverOriginalRange(fromLongitude);
120     }
121 
122     /** Get the fixed longitude range to be crossed (radians), upper boundary.
123      * @return fixed upper boundary longitude range to be crossed (radians)
124      */
125     public double getToLongitude() {
126         return getLongitudeOverOriginalRange(toLongitude);
127     }
128 
129     /**
130      * Ensure continuity for negative angles, as longitude defined as [-PI, PI], transform negative to positive.
131      * New longitude angle definition from [0, 2 PI].
132      * @param longitude original longitude value
133      * @return positive range longitude
134      */
135     private double ensureLongitudePositiveContinuity(final double longitude) {
136         return longitude < 0 ? longitude + 2 * FastMath.PI : longitude;
137     }
138 
139     /**
140      * Get longitude shifted over the original range [-PI, PI].
141      * @param longitude longitude value to convert
142      * @return original range longitude
143      */
144     private double getLongitudeOverOriginalRange(final double longitude) {
145         return longitude > FastMath.PI ? longitude - 2 * FastMath.PI : longitude;
146     }
147 
148     /** Compute the value of the detection function.
149      * <p>
150      * The value is positive if the spacecraft longitude is inside the longitude range.
151      * The longitude value is reflected from [-PI, +PI] to [0, 2 PI] to ensure continuity.
152      * </p>
153      * @param s the current state information: date, kinematics, attitude
154      * @return positive if spacecraft inside the range
155      */
156     public double g(final SpacecraftState s) {
157 
158         // convert state to geodetic coordinates
159         final GeodeticPoint gp = body.transform(s.getPVCoordinates().getPosition(),
160             s.getFrame(), s.getDate());
161 
162         // point longitude
163         final double longitude = ensureLongitudePositiveContinuity(gp.getLongitude());
164 
165         // inside or outside longitude range
166         return sign * (longitude - fromLongitude) * (toLongitude - longitude);
167 
168     }
169 
170 }