1   /* Copyright 2022-2025 Romain Serra
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.geometry.euclidean.threed.Vector3D;
20  import org.hipparchus.util.FastMath;
21  import org.orekit.propagation.SpacecraftState;
22  import org.orekit.propagation.events.handlers.EventHandler;
23  import org.orekit.utils.PVCoordinatesProvider;
24  
25  /**
26   * Event detector for eclipses from a single, infinitely-distant light source, occulted by a spherical central body.
27   * The shadow region is cylindrical, a model less accurate than a conical one but more computationally-performant.
28   * <p>
29   *     The so-called g function is negative in eclipse, positive otherwise.
30   * </p>
31   * @author Romain Serra
32   * @see EclipseDetector
33   * @since 12.1
34   */
35  public class CylindricalShadowEclipseDetector extends AbstractDetector<CylindricalShadowEclipseDetector> {
36  
37      /** Direction provider for the occulted light source i.e. the Sun (whose shadow is approximated as if the body was infinitely distant). */
38      private final PVCoordinatesProvider sun;
39  
40      /** Radius of central, occulting body (approximated as spherical).
41       * Its center is assumed to be at the origin of the frame linked to the state. */
42      private final double occultingBodyRadius;
43  
44      /**
45       * Constructor.
46       * @param sun light source provider (infinitely distant)
47       * @param occultingBodyRadius occulting body radius
48       * @param eventDetectionSettings detection settings
49       * @param handler event handler
50       * @since 12.2
51       */
52      public CylindricalShadowEclipseDetector(final PVCoordinatesProvider sun,
53                                              final double occultingBodyRadius,
54                                              final EventDetectionSettings eventDetectionSettings,
55                                              final EventHandler handler) {
56          super(eventDetectionSettings, handler);
57          this.sun = sun;
58          this.occultingBodyRadius = FastMath.abs(occultingBodyRadius);
59      }
60  
61      /**
62       * Constructor with default detection settings.
63       * @param sun light source provider
64       * @param occultingBodyRadius occulting body radius
65       * @param handler event handler
66       */
67      public CylindricalShadowEclipseDetector(final PVCoordinatesProvider sun,
68                                              final double occultingBodyRadius, final EventHandler handler) {
69          this(sun, occultingBodyRadius, EventDetectionSettings.getDefaultEventDetectionSettings(), handler);
70      }
71  
72      /**
73       * Getter for occulting body radius.
74       * @return radius
75       */
76      public double getOccultingBodyRadius() {
77          return occultingBodyRadius;
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public double g(final SpacecraftState s) {
83          final Vector3D sunDirection = sun.getPosition(s.getDate(), s.getFrame()).normalize();
84          final Vector3D position = s.getPosition();
85          final double dotProduct = position.dotProduct(sunDirection);
86          if (dotProduct >= 0.) {
87              return position.getNorm() / occultingBodyRadius;
88          } else {
89              final double distanceToCylinderAxis = (position.subtract(sunDirection.scalarMultiply(dotProduct))).getNorm();
90              return distanceToCylinderAxis / occultingBodyRadius - 1.;
91          }
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      protected CylindricalShadowEclipseDetector create(final EventDetectionSettings detectionSettings,
97                                                        final EventHandler newHandler) {
98          return new CylindricalShadowEclipseDetector(sun, occultingBodyRadius, detectionSettings, newHandler);
99      }
100 }