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