1   /* Copyright 2022-2024 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 maxCheck maximum check for event detection
49       * @param threshold threshold for event detection
50       * @param maxIter maximum iteration for event detection
51       * @param handler event handler
52       */
53      public CylindricalShadowEclipseDetector(final PVCoordinatesProvider sun,
54                                              final double occultingBodyRadius,
55                                              final AdaptableInterval maxCheck, final double threshold,
56                                              final int maxIter, final EventHandler handler) {
57          super(maxCheck, threshold, maxIter, handler);
58          this.sun = sun;
59          this.occultingBodyRadius = FastMath.abs(occultingBodyRadius);
60      }
61  
62      /**
63       * Constructor with default detection settings.
64       * @param sun light source provider
65       * @param occultingBodyRadius occulting body radius
66       * @param handler event handler
67       */
68      public CylindricalShadowEclipseDetector(final PVCoordinatesProvider sun,
69                                              final double occultingBodyRadius, final EventHandler handler) {
70          this(sun, occultingBodyRadius, AdaptableInterval.of(DEFAULT_MAXCHECK), DEFAULT_THRESHOLD, DEFAULT_MAX_ITER, handler);
71      }
72  
73      /**
74       * Getter for occulting body radius.
75       * @return radius
76       */
77      public double getOccultingBodyRadius() {
78          return occultingBodyRadius;
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      public double g(final SpacecraftState s) {
84          final Vector3D sunDirection = sun.getPosition(s.getDate(), s.getFrame()).normalize();
85          final Vector3D position = s.getPosition();
86          final double dotProduct = position.dotProduct(sunDirection);
87          if (dotProduct >= 0.) {
88              return position.getNorm() / occultingBodyRadius;
89          } else {
90              final double distanceToCylinderAxis = (position.subtract(sunDirection.scalarMultiply(dotProduct))).getNorm();
91              return distanceToCylinderAxis / occultingBodyRadius - 1.;
92          }
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      protected CylindricalShadowEclipseDetector create(final AdaptableInterval newMaxCheck, final double newThreshold,
98                                                        final int newMaxIter, final EventHandler newHandler) {
99          return new CylindricalShadowEclipseDetector(sun, occultingBodyRadius, newMaxCheck, newThreshold, newMaxIter, newHandler);
100     }
101 }