1   /* Copyright 2002-2021 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.OneAxisEllipsoid;
22  import org.orekit.bodies.GeodeticPoint;
23  import org.orekit.propagation.SpacecraftState;
24  import org.orekit.propagation.events.handlers.EventHandler;
25  import org.orekit.propagation.events.handlers.StopOnIncreasing;
26  
27  /** Detector for geographic longitude crossing.
28   * <p>This detector identifies when a spacecraft crosses a fixed
29   * longitude with respect to a central body.</p>
30   * @author Luc Maisonobe
31   * @since 7.1
32   */
33  public class LongitudeCrossingDetector extends AbstractDetector<LongitudeCrossingDetector> {
34  
35      /** Body on which the longitude is defined. */
36      private OneAxisEllipsoid body;
37  
38      /** Fixed longitude to be crossed. */
39      private final double longitude;
40  
41      /** Sign to apply for longitude difference. */
42      private double sign;
43  
44      /** Previous longitude difference. */
45      private double previousDelta;
46  
47      /** Build a new detector.
48       * <p>The new instance uses default values for maximal checking interval
49       * ({@link #DEFAULT_MAXCHECK}) and convergence threshold ({@link
50       * #DEFAULT_THRESHOLD}).</p>
51       * @param body body on which the longitude is defined
52       * @param longitude longitude to be crossed
53       */
54      public LongitudeCrossingDetector(final OneAxisEllipsoid body, final double longitude) {
55          this(DEFAULT_MAXCHECK, DEFAULT_THRESHOLD, body, longitude);
56      }
57  
58      /** Build a detector.
59       * @param maxCheck maximal checking interval (s)
60       * @param threshold convergence threshold (s)
61       * @param body body on which the longitude is defined
62       * @param longitude longitude to be crossed
63       */
64      public LongitudeCrossingDetector(final double maxCheck, final double threshold,
65                                      final OneAxisEllipsoid body, final double longitude) {
66          this(maxCheck, threshold, DEFAULT_MAX_ITER, new StopOnIncreasing<LongitudeCrossingDetector>(),
67               body, longitude);
68      }
69  
70      /** Private constructor with full parameters.
71       * <p>
72       * This constructor is private as users are expected to use the builder
73       * API with the various {@code withXxx()} methods to set up the instance
74       * in a readable manner without using a huge amount of parameters.
75       * </p>
76       * @param maxCheck maximum checking interval (s)
77       * @param threshold convergence threshold (s)
78       * @param maxIter maximum number of iterations in the event time search
79       * @param handler event handler to call at event occurrences
80       * @param body body on which the longitude is defined
81       * @param longitude longitude to be crossed
82       */
83      private LongitudeCrossingDetector(final double maxCheck, final double threshold,
84                                       final int maxIter, final EventHandler<? super LongitudeCrossingDetector> handler,
85                                       final OneAxisEllipsoid body, final double longitude) {
86          super(maxCheck, threshold, maxIter, handler);
87          this.body          = body;
88          this.longitude     = longitude;
89          this.sign          = +1.0;
90          this.previousDelta = Double.NaN;
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      protected LongitudeCrossingDetector create(final double newMaxCheck, final double newThreshold,
96                                                final int newMaxIter,
97                                                final EventHandler<? super LongitudeCrossingDetector> newHandler) {
98          return new LongitudeCrossingDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
99                                            body, longitude);
100     }
101 
102     /** Get the body on which the geographic zone is defined.
103      * @return body on which the geographic zone is defined
104      */
105     public OneAxisEllipsoid getBody() {
106         return body;
107     }
108 
109     /** Get the fixed longitude to be crossed (radians).
110      * @return fixed longitude to be crossed (radians)
111      */
112     public double getLongitude() {
113         return longitude;
114     }
115 
116     /** Compute the value of the detection function.
117      * <p>
118      * The value is the longitude difference between the spacecraft and the fixed
119      * longitude to be crossed, with some sign tweaks to ensure continuity.
120      * These tweaks imply the {@code increasing} flag in events detection becomes
121      * irrelevant here! As an example, the longitude of a prograde spacecraft
122      * will always increase, but this g function will increase and decrease so it
123      * will cross the zero value once per orbit, in increasing and decreasing
124      * directions on alternate orbits. If eastwards and westwards crossing have to
125      * be distinguished, the velocity direction has to be checked instead of looking
126      * at the {@code increasing} flag.
127      * </p>
128      * @param s the current state information: date, kinematics, attitude
129      * @return longitude difference between the spacecraft and the fixed
130      * longitude, with some sign tweaks to ensure continuity
131      */
132     public double g(final SpacecraftState s) {
133 
134         // convert state to geodetic coordinates
135         final GeodeticPoint gp = body.transform(s.getPVCoordinates().getPosition(),
136                                                 s.getFrame(), s.getDate());
137 
138         // longitude difference
139         double delta = MathUtils.normalizeAngle(sign * (gp.getLongitude() - longitude), 0.0);
140 
141         // ensure continuity
142         if (FastMath.abs(delta - previousDelta) > FastMath.PI) {
143             sign  = -sign;
144             delta = MathUtils.normalizeAngle(sign * (gp.getLongitude() - longitude), 0.0);
145         }
146         previousDelta = delta;
147 
148         return delta;
149 
150     }
151 
152 }