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.geometry.euclidean.threed.Vector3D;
20  import org.hipparchus.ode.events.Action;
21  import org.orekit.bodies.CelestialBodies;
22  import org.orekit.propagation.SpacecraftState;
23  import org.orekit.propagation.events.handlers.EventHandler;
24  import org.orekit.propagation.events.handlers.StopOnDecreasing;
25  import org.orekit.utils.PVCoordinatesProvider;
26  
27  /** Detects when spacecraft comes close to a moving beacon, as seen from a moving observer.
28   * <p>The main use case for this detector is when the observer is in fact a ground
29   * station, modeled as a {@link org.orekit.frames.TopocentricFrame} and when the beacon
30   * is the {@link CelestialBodies#getSun() Sun}, for computing
31   * interferences for the telemetry link. Another similar case is when the beacon is
32   * another spacecraft, for interferences computation.</p>
33   * <p>The default handler behavior is to {@link Action#STOP stop}
34   * propagation when spacecraft enters the proximity zone. This can be changed by calling
35   * {@link #withHandler(EventHandler)} after construction.</p>
36   * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
37   * @author Luc Maisonobe
38   * @since 8.0
39   */
40  public class AngularSeparationDetector extends AbstractDetector<AngularSeparationDetector> {
41  
42      /** Default detection settings. */
43      public static final EventDetectionSettings DEFAULT_SETTINGS = new EventDetectionSettings(60., 1e-3, EventDetectionSettings.DEFAULT_MAX_ITER);
44  
45      /** Beacon at the center of the proximity zone. */
46      private final PVCoordinatesProvider beacon;
47  
48      /** Observer for the spacecraft, that may also see the beacon at the same time if they are too close. */
49      private final PVCoordinatesProvider observer;
50  
51      /** Proximity angle (rad). */
52      private final double proximityAngle;
53  
54      /** Build a new angular separation detector.
55       * @param beacon beacon at the center of the proximity zone
56       * @param observer observer for the spacecraft, that may also see
57       * the beacon at the same time if they are too close to each other
58       * @param proximityAngle proximity angle as seen from observer, at which events are triggered (rad)
59       */
60      public AngularSeparationDetector(final PVCoordinatesProvider beacon,
61                                       final PVCoordinatesProvider observer,
62                                       final double proximityAngle) {
63          this(DEFAULT_SETTINGS, new StopOnDecreasing(), beacon, observer, proximityAngle);
64      }
65  
66      /** Protected constructor with full parameters.
67       * <p>
68       * This constructor is not public as users are expected to use the builder
69       * API with the various {@code withXxx()} methods to set up the instance
70       * in a readable manner without using a huge amount of parameters.
71       * </p>
72       * @param detectionSettings detection settings
73       * @param handler event handler to call at event occurrences
74       * @param beacon beacon at the center of the proximity zone
75       * @param observer observer for the spacecraft, that may also see
76       * the beacon at the same time if they are too close to each other
77       * @param proximityAngle proximity angle as seen from observer, at which events are triggered (rad)
78       * @since 13.0
79       */
80      protected AngularSeparationDetector(final EventDetectionSettings detectionSettings,
81                                          final EventHandler handler,
82                                          final PVCoordinatesProvider beacon,
83                                          final PVCoordinatesProvider observer,
84                                          final double proximityAngle) {
85          super(detectionSettings, handler);
86          this.beacon         = beacon;
87          this.observer       = observer;
88          this.proximityAngle = proximityAngle;
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      protected AngularSeparationDetector create(final EventDetectionSettings detectionSettings, final EventHandler newHandler) {
94          return new AngularSeparationDetector(detectionSettings, newHandler,
95                                               beacon, observer, proximityAngle);
96      }
97  
98      /** Get the beacon at the center of the proximity zone.
99       * @return beacon at the center of the proximity zone
100      */
101     public PVCoordinatesProvider getBeacon() {
102         return beacon;
103     }
104 
105     /** Get the observer for the spacecraft.
106      * @return observer for the spacecraft
107      */
108     public PVCoordinatesProvider getObserver() {
109         return observer;
110     }
111 
112     /** Get the proximity angle (rad).
113      * @return the proximity angle
114      */
115     public double getProximityAngle() {
116         return proximityAngle;
117     }
118 
119     /** Compute the value of the switching function.
120      * <p>
121      * This function measures the angular separation between beacon and spacecraft
122      * as seen from the observer minus the proximity angle. It therefore triggers
123      * decreasing events when the spacecraft enters the proximity zone and increasing
124      * events when it leaves the proximity zone.
125      * </p>
126      * <p>
127      * No shadowing effect is taken into account, so this method is computed and
128      * may trigger events even when the spacecraft is below horizon for an observer
129      * which is a ground station. If such effects must be taken into account the
130      * detector must be associated with a {@link EventEnablingPredicateFilter predicate
131      * filter} where the {@link EnablingPredicate predicate function} is based on elevation.
132      * </p>
133      * @param s the current state information: date, kinematics, attitude
134      * @return value of the switching function
135      */
136     public double g(final SpacecraftState s) {
137         final Vector3D sPosition = s.getPosition();
138         final Vector3D bP = beacon.getPosition(s.getDate(), s.getFrame());
139         final Vector3D oP = observer.getPosition(s.getDate(), s.getFrame());
140         final double separation = Vector3D.angle(sPosition.subtract(oP), bP.subtract(oP));
141         return separation - proximityAngle;
142     }
143 
144 }