1   /* Copyright 2002-2025 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.ode.events.Action;
20  import org.orekit.bodies.OneAxisEllipsoid;
21  import org.orekit.propagation.SpacecraftState;
22  import org.orekit.propagation.events.handlers.EventHandler;
23  import org.orekit.propagation.events.handlers.StopOnIncreasing;
24  import org.orekit.utils.ExtendedPositionProvider;
25  import org.orekit.utils.OccultationEngine;
26  import org.orekit.utils.PVCoordinatesProvider;
27  
28  /** Finder for satellite eclipse related events.
29   * <p>This class finds eclipse events, i.e. satellite within umbra (total
30   * eclipse) or penumbra (partial eclipse).</p>
31   * <p>The occulted body is given through a {@link PVCoordinatesProvider} and its radius in meters. It is modeled as a sphere.
32   * </p>
33   * <p>Since v10.0 the occulting body is a {@link OneAxisEllipsoid}, before it was modeled as a  sphere.
34   * <br>It was changed to precisely model Solar eclipses by the Earth, especially for Low Earth Orbits.
35   * <br>If you want eclipses by a spherical occulting body, set its flattening to 0. when defining its OneAxisEllipsoid model..
36   * </p>
37   * <p>The {@link #withUmbra} or {@link #withPenumbra} methods will tell you if the event is triggered when complete umbra/lighting
38   * is achieved or when entering/living the penumbra zone.
39   * <br>The default behavior is detecting complete umbra/lighting events.
40   * <br>If you want to have both, you'll need to set up two distinct detectors.
41   * </p>
42   * <p>The default implementation behavior is to {@link Action#CONTINUE continue}
43   * propagation when entering the eclipse and to {@link Action#STOP stop} propagation
44   * when exiting the eclipse.
45   * <br>This can be changed by calling {@link #withHandler(EventHandler)} after construction.
46   * </p>
47   * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
48   * @author Pascal Parraud
49   * @author Luc Maisonobe
50   */
51  public class EclipseDetector extends AbstractDetector<EclipseDetector> {
52  
53      /** Occultation engine.
54       * @since 12.0
55       */
56      private final OccultationEngine occultationEngine;
57  
58      /** Umbra, if true, or penumbra, if false, detection flag. */
59      private final boolean totalEclipse;
60  
61      /** Margin to apply to eclipse angle. */
62      private final double margin;
63  
64      /** Build a new eclipse detector.
65       * <p>The new instance is a total eclipse (umbra) detector with default
66       * values for maximal checking interval ({@link #DEFAULT_MAX_CHECK})
67       * and convergence threshold ({@link #DEFAULT_THRESHOLD}).</p>
68       * @param occulted the body to be occulted
69       * @param occultedRadius the radius of the body to be occulted (m)
70       * @param occulting the occulting body
71       * @since 12.0
72       */
73      public EclipseDetector(final ExtendedPositionProvider occulted, final double occultedRadius,
74                             final OneAxisEllipsoid occulting) {
75          this(new OccultationEngine(occulted, occultedRadius, occulting));
76      }
77  
78      /** Build a new eclipse detector.
79       * <p>The new instance is a total eclipse (umbra) detector with default
80       * values for maximal checking interval ({@link #DEFAULT_MAX_CHECK})
81       * and convergence threshold ({@link #DEFAULT_THRESHOLD}).</p>
82       * @param occultationEngine occultation engine
83       * @since 12.0
84       */
85      public EclipseDetector(final OccultationEngine occultationEngine) {
86          this(EventDetectionSettings.getDefaultEventDetectionSettings(), new StopOnIncreasing(),
87               occultationEngine, 0.0, true);
88      }
89  
90      /** Protected constructor with full parameters.
91       * <p>
92       * This constructor is not public as users are expected to use the builder
93       * API with the various {@code withXxx()} methods to set up the instance
94       * in a readable manner without using a huge amount of parameters.
95       * </p>
96       * @param detectionSettings detection settings
97       * @param handler event handler to call at event occurrences
98       * @param occultationEngine occultation engine
99       * @param margin to apply to eclipse angle (rad)
100      * @param totalEclipse umbra (true) or penumbra (false) detection flag
101      * @since 12.2
102      */
103     protected EclipseDetector(final EventDetectionSettings detectionSettings, final EventHandler handler,
104                               final OccultationEngine occultationEngine, final double margin, final boolean totalEclipse) {
105         super(detectionSettings, handler);
106         this.occultationEngine = occultationEngine;
107         this.margin            = margin;
108         this.totalEclipse      = totalEclipse;
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     protected EclipseDetector create(final EventDetectionSettings detectionSettings, final EventHandler newHandler) {
114         return new EclipseDetector(detectionSettings, newHandler, occultationEngine, margin, totalEclipse);
115     }
116 
117     /**
118      * Setup the detector to full umbra detection.
119      * <p>
120      * This will override a penumbra/umbra flag if it has been configured previously.
121      * </p>
122      * @return a new detector with updated configuration (the instance is not changed)
123      * @see #withPenumbra()
124      * @since 6.1
125      */
126     public EclipseDetector withUmbra() {
127         return new EclipseDetector(getDetectionSettings(), getHandler(), occultationEngine, margin, true);
128     }
129 
130     /**
131      * Setup the detector to penumbra detection.
132      * <p>
133      * This will override a penumbra/umbra flag if it has been configured previously.
134      * </p>
135      * @return a new detector with updated configuration (the instance is not changed)
136      * @see #withUmbra()
137      * @since 6.1
138      */
139     public EclipseDetector withPenumbra() {
140         return new EclipseDetector(getDetectionSettings(), getHandler(), occultationEngine, margin, false);
141     }
142 
143     /**
144      * Setup a margin to angle detection.
145      * <p>
146      * A positive margin implies eclipses are "larger" hence entry occurs earlier and exit occurs later
147      * than a detector with 0 margin.
148      * </p>
149      * @param newMargin angular margin to apply to eclipse detection (rad)
150      * @return a new detector with updated configuration (the instance is not changed)
151      * @since 12.0
152      */
153     public EclipseDetector withMargin(final double newMargin) {
154         return new EclipseDetector(getDetectionSettings(), getHandler(), occultationEngine, newMargin, totalEclipse);
155     }
156 
157     /** Get the angular margin used for eclipse detection.
158      * @return angular margin used for eclipse detection (rad)
159      * @since 12.0
160      */
161     public double getMargin() {
162         return margin;
163     }
164 
165     /** Get the occultation engine.
166      * @return occultation engine
167      * @since 12.0
168      */
169     public OccultationEngine getOccultationEngine() {
170         return occultationEngine;
171     }
172 
173     /** Get the total eclipse detection flag.
174      * @return the total eclipse detection flag (true for umbra events detection,
175      * false for penumbra events detection)
176      */
177     public boolean getTotalEclipse() {
178         return totalEclipse;
179     }
180 
181     /** Compute the value of the switching function.
182      * This function becomes negative when entering the region of shadow
183      * and positive when exiting.
184      * @param s the current state information: date, kinematics, attitude
185      * @return value of the switching function
186      */
187     public double g(final SpacecraftState s) {
188         final OccultationEngine.OccultationAngles angles = occultationEngine.angles(s);
189         return totalEclipse ?
190                (angles.getSeparation() - angles.getLimbRadius() + angles.getOccultedApparentRadius() + margin) :
191                (angles.getSeparation() - angles.getLimbRadius() - angles.getOccultedApparentRadius() + margin);
192     }
193 
194 }