1   /* Copyright 2002-2020 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 two moving objects come close to each other, as seen from spacecraft.
29   * <p>The main use case for this detector is when the primary object is in fact a ground
30   * station, modeled as a {@link org.orekit.frames.TopocentricFrame} and when the secondary
31   * is the {@link CelestialBodies#getSun() Sun}, for computing
32   * optical reflections.</p>
33   * <p>The default handler behavior is to {@link Action#STOP stop}
34   * propagation when objects enter 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   * @author Thomas Paulet
39   * @since 11.0
40   */
41  public class AngularSeparationFromSatelliteDetector extends AbstractDetector<AngularSeparationFromSatelliteDetector> {
42  
43      /** Primary object, at the center of the proximity zone. */
44      private final PVCoordinatesProvider primaryObject;
45  
46      /** Secondary object, that may come close to the primary, as seen from the spacecraft . */
47      private final PVCoordinatesProvider secondaryObject;
48  
49      /** Proximity angle (rad). */
50      private final double proximityAngle;
51  
52      /** Build a new angular detachment detector.
53       * @param primaryObject primaryObject, at the center of the proximity zone
54       * @param secondaryObject secondaryObject, that may come close to
55       *        the primaryObject as seen from the spacecraft
56       * @param proximityAngle proximity angle as seen from spacecraft, at which events are triggered (rad)
57       */
58      public AngularSeparationFromSatelliteDetector(final PVCoordinatesProvider primaryObject,
59                                                    final PVCoordinatesProvider secondaryObject,
60                                                    final double proximityAngle) {
61          this(AdaptableInterval.of(DEFAULT_MAXCHECK), DEFAULT_THRESHOLD, DEFAULT_MAX_ITER, new StopOnDecreasing(),
62               primaryObject, secondaryObject, 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 maxCheck maximum checking interval
72       * @param threshold convergence threshold (s)
73       * @param maxIter maximum number of iterations in the event time search
74       * @param handler event handler to call at event occurrences
75       * @param primaryObject primaryObject at the center of the proximity zone
76       * @param secondaryObject secondaryObject, that may come close to
77       *        the primaryObject as seen from the spacecraft
78       * @param proximityAngle proximity angle as seen from secondaryObject, at which events are triggered (rad)
79       */
80      protected AngularSeparationFromSatelliteDetector(final AdaptableInterval maxCheck, final double threshold,
81                                                       final int maxIter,
82                                                       final EventHandler handler,
83                                                       final PVCoordinatesProvider primaryObject,
84                                                       final PVCoordinatesProvider secondaryObject,
85                                                       final double proximityAngle) {
86          super(maxCheck, threshold, maxIter, handler);
87          this.primaryObject         = primaryObject;
88          this.secondaryObject       = secondaryObject;
89          this.proximityAngle = proximityAngle;
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      protected AngularSeparationFromSatelliteDetector create(final AdaptableInterval newMaxCheck, final double newThreshold,
95                                                              final int newMaxIter, final EventHandler newHandler) {
96          return new AngularSeparationFromSatelliteDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
97                                               primaryObject, secondaryObject, proximityAngle);
98      }
99  
100     /** Get the primaryObject, at the center of the proximity zone.
101      * @return primaryObject
102      */
103     public PVCoordinatesProvider getPrimaryObject() {
104         return primaryObject;
105     }
106 
107     /** Get the secondaryObject.
108      * @return secondaryObject
109      */
110     public PVCoordinatesProvider getSecondaryObject() {
111         return secondaryObject;
112     }
113 
114     /** Get the proximity angle (rad).
115      * @return the proximity angle
116      */
117     public double getProximityAngle() {
118         return proximityAngle;
119     }
120 
121     /** Compute the value of the switching function.
122      * <p>
123      * This function measures the angular separation between primary and secondary objects
124      * as seen from the spacecraft minus the proximity angle. It therefore triggers
125      * decreasing events when the secondary object enters the proximity zone and increasing
126      * events when it leaves the proximity zone.
127      * </p>
128      * <p>
129      * No shadowing effect is taken into account, so this method is computed and
130      * may trigger events even when the secondary object is behind the primary.
131      * If such effects must be taken into account the
132      * detector must be associated with a {@link EventEnablingPredicateFilter predicate
133      * filter} where the {@link EnablingPredicate predicate function} is based on eclipse conditions.
134      * </p>
135      * @param s the current state information: date, kinematics, attitude
136      * @return value of the switching function
137      */
138     public double g(final SpacecraftState s) {
139         final PVCoordinates sPV = s.getPVCoordinates();
140         final Vector3D primaryPos   = primaryObject  .getPosition(s.getDate(), s.getFrame());
141         final Vector3D secondaryPos = secondaryObject.getPosition(s.getDate(), s.getFrame());
142         final double separation = Vector3D.angle(primaryPos.subtract(sPV.getPosition()),
143                                                  secondaryPos.subtract(sPV.getPosition()));
144         return separation - proximityAngle;
145     }
146 
147 }