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  
18  package org.orekit.propagation.events;
19  
20  import org.hipparchus.geometry.euclidean.threed.Vector3D;
21  import org.hipparchus.ode.events.Action;
22  import org.orekit.propagation.PropagatorsParallelizer;
23  import org.orekit.propagation.SpacecraftState;
24  import org.orekit.propagation.events.handlers.EventHandler;
25  import org.orekit.propagation.events.handlers.StopOnEvent;
26  import org.orekit.utils.PVCoordinatesProvider;
27  
28  /**
29   * Detector of specific value for the distance relative to another trajectory (using the Euclidean norm).
30   * <p>
31   * The default implementation behavior is to {@link Action#STOP stop} propagation.
32   * This can be changed by calling {@link #withHandler(EventHandler)} after construction.
33   * </p>
34   * <p>
35   * As this detector needs two objects (moving relative to each other), it embeds one
36   * {@link PVCoordinatesProvider coordinates provider} for the secondary object and is registered as an event detector in
37   * the propagator of the primary object. The secondary object {@link PVCoordinatesProvider coordinates provider} will
38   * therefore be driven by this detector (and hence by the propagator in which this detector is registered).
39   * </p>
40   * <p><b>
41   * In order to avoid infinite recursion, care must be taken to have the secondary object provider being <em>completely
42   * independent</em> from anything else. In particular, if the provider is a propagator, it should <em>not</em> be run
43   * together in a {@link PropagatorsParallelizer propagators parallelizer} with the propagator this detector is
44   * registered in. It is fine however to configure two separate propagators PsA and PsB with similar settings for the
45   * secondary object and one propagator Pm for the primary object and then use Psa in this detector registered within Pm
46   * while Pm and Psb are run in the context of a {@link PropagatorsParallelizer propagators parallelizer}.
47   * </b></p>
48   * <p>
49   * For efficiency reason during the event search loop, it is recommended to have the secondary provider be an analytical
50   * propagator or an ephemeris. A numerical propagator as a secondary propagator works but is expected to be
51   * computationally costly.
52   * </p>
53   *
54   * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
55   * @author Romain Serra
56   * @since 12.1
57   */
58  public class RelativeDistanceDetector extends AbstractDetector<RelativeDistanceDetector> {
59  
60      /**
61       * PVCoordinates provider of the other object used to define relative distance.
62       */
63      private final PVCoordinatesProvider secondaryPVProvider;
64  
65      /** Relative distance value triggering detection. */
66      private final double distanceThreshold;
67  
68      /**
69       * Constructor with default values.
70       * <p>
71       * By default, the implemented behavior is to {@link Action#STOP stop} propagation at detection.
72       * </p>
73       *
74       * @param secondaryPVProvider PVCoordinates provider of the other object defining relative distance.
75       * @param distanceThreshold Relative distance threshold for event detection
76       */
77      public RelativeDistanceDetector(final PVCoordinatesProvider secondaryPVProvider,
78                                      final double distanceThreshold) {
79          this(AdaptableInterval.of(DEFAULT_MAXCHECK), DEFAULT_THRESHOLD, DEFAULT_MAX_ITER, new StopOnEvent(), secondaryPVProvider,
80                  distanceThreshold);
81      }
82  
83      /**
84       * Constructor.
85       * <p>
86       * This constructor is to be used if the user wants to change the default behavior of the detector.
87       * </p>
88       *
89       * @param maxCheck            Maximum checking interval.
90       * @param threshold           Convergence threshold (s).
91       * @param maxIter             Maximum number of iterations in the event time search.
92       * @param handler             Event handler to call at event occurrences.
93       * @param secondaryPVProvider PVCoordinates provider of the other object defining relative distance.
94       * @param distanceThreshold Relative distance threshold for event detection
95       * @see EventHandler
96       */
97      protected RelativeDistanceDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
98                                         final EventHandler handler, final PVCoordinatesProvider secondaryPVProvider,
99                                         final double distanceThreshold) {
100         super(maxCheck, threshold, maxIter, handler);
101         this.secondaryPVProvider = secondaryPVProvider;
102         this.distanceThreshold = distanceThreshold;
103     }
104 
105     /**
106      * The {@code g} is positive when the relative distance is larger or equal than the threshold,
107      * non-positive otherwise.
108      *
109      * @param s the current state information: date, kinematics, attitude
110      * @return value of the switching function
111      */
112     public double g(final SpacecraftState s) {
113         final Vector3D secondaryPosition = getSecondaryPVProvider().getPosition(s.getDate(), s.getFrame());
114         final double relativeDistance = s.getPosition().subtract(secondaryPosition).getNorm();
115         return relativeDistance - distanceThreshold;
116     }
117 
118     /** {@inheritDoc} */
119     @Override
120     protected RelativeDistanceDetector create(final AdaptableInterval newMaxCheck, final double newThreshold,
121                                               final int newMaxIter, final EventHandler newHandler) {
122         return new RelativeDistanceDetector(newMaxCheck, newThreshold, newMaxIter, newHandler, secondaryPVProvider,
123                 distanceThreshold);
124     }
125 
126     /**
127      * Get the secondary position-velocity provider stored in this instance.
128      *
129      * @return the secondary position-velocity provider stored in this instance
130      */
131     public PVCoordinatesProvider getSecondaryPVProvider() {
132         return secondaryPVProvider;
133     }
134 
135     /**
136      * Get the relative distance threshold.
137      *
138      * @return threshold triggering detection
139      */
140     public double getDistanceThreshold() {
141         return distanceThreshold;
142     }
143 }