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