1   /* Copyright 2002-2024 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  
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.StopOnIncreasing;
26  import org.orekit.utils.PVCoordinates;
27  import org.orekit.utils.PVCoordinatesProvider;
28  
29  /**
30   * Finder for extremum approach events.
31   * <p>
32   * This class finds extremum approach events (i.e. closest or farthest approach).
33   * </p>
34   * <p>
35   * The default implementation behavior is to {@link Action#CONTINUE continue} propagation at farthest approach and to
36   * {@link Action#STOP stop} propagation at closest approach. This can be changed by calling
37   * {@link #withHandler(EventHandler)} after construction (go to the end of the documentation to see an example).
38   * </p>
39   * <p>
40   * As this detector needs two objects (moving relative to each other), it embeds one
41   * {@link PVCoordinatesProvider coordinates provider} for the secondary object and is registered as an event detector in
42   * the propagator of the primary object. The secondary object  {@link PVCoordinatesProvider coordinates provider} will
43   * therefore be driven by this detector (and hence by the propagator in which this detector is registered).
44   * </p>
45   * <p><b>
46   * In order to avoid infinite recursion, care must be taken to have the secondary object provider being <em>completely
47   * independent</em> from anything else. In particular, if the provider is a propagator, it should <em>not</em> be run
48   * together in a {@link PropagatorsParallelizer propagators parallelizer} with the propagator this detector is
49   * registered in. It is fine however to configure two separate propagators PsA and PsB with similar settings for the
50   * secondary object and one propagator Pm for the primary object and then use Psa in this detector registered within Pm
51   * while Pm and Psb are run in the context of a {@link PropagatorsParallelizer propagators parallelizer}.
52   * </b></p>
53   * <p>
54   * For efficiency reason during the event search loop, it is recommended to have the secondary provider be an analytical
55   * propagator or an ephemeris. A numerical propagator as a secondary propagator works but is expected to be
56   * computationally costly.
57   * </p>
58   * <p>
59   * Also, it is possible to detect solely one type of event using an {@link EventSlopeFilter event slope filter}. For
60   * example in order to only detect closest approach, one should type the following :
61   * </p>
62   * <pre>{@code
63   * ExtremumApproachDetector extremumApproachDetector = new ExtremumApproachDetector(secondaryPVProvider);
64   * EventDetector closeApproachDetector = new EventSlopeFilter<ExtremumApproachDetector>(extremumApproachDetector,FilterType.TRIGGER_ONLY_INCREASING_EVENTS);
65   *  }
66   * </pre>
67   *
68   * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
69   * @see EventSlopeFilter
70   * @see FilterType
71   * @author Vincent Cucchietti
72   * @since 11.3
73   */
74  public class ExtremumApproachDetector extends AbstractDetector<ExtremumApproachDetector> {
75  
76      /**
77       * PVCoordinates provider of the other object with which we want to find out the extremum approach.
78       */
79      private final PVCoordinatesProvider secondaryPVProvider;
80  
81      /**
82       * Constructor with default values.
83       * <p>
84       * By default, the implemented behavior is to {@link Action#CONTINUE continue} propagation at farthest approach and
85       * to {@link Action#STOP stop} propagation at closest approach.
86       * </p>
87       *
88       * @param secondaryPVProvider PVCoordinates provider of the other object with which we want to find out the extremum
89       *                            approach.
90       */
91      public ExtremumApproachDetector(final PVCoordinatesProvider secondaryPVProvider) {
92          this(AdaptableInterval.of(DEFAULT_MAXCHECK), DEFAULT_THRESHOLD, DEFAULT_MAX_ITER, new StopOnIncreasing(), secondaryPVProvider);
93      }
94  
95      /**
96       * Constructor.
97       * <p>
98       * This constructor is to be used if the user wants to change the default behavior of the detector.
99       * </p>
100      *
101      * @param maxCheck            Maximum checking interval.
102      * @param threshold           Convergence threshold (s).
103      * @param maxIter             Maximum number of iterations in the event time search.
104      * @param handler             Event handler to call at event occurrences.
105      * @param secondaryPVProvider PVCoordinates provider of the other object with which we want to find out the extremum
106      *                            approach.
107      * @see EventHandler
108      */
109     protected ExtremumApproachDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
110                                        final EventHandler handler, final PVCoordinatesProvider secondaryPVProvider) {
111         super(maxCheck, threshold, maxIter, handler);
112         this.secondaryPVProvider = secondaryPVProvider;
113     }
114 
115     /**
116      * The {@code g} is positive when the primary object is getting further away from the secondary object and is
117      * negative when it is getting closer to it.
118      *
119      * @param s the current state information: date, kinematics, attitude
120      * @return value of the switching function
121      */
122     public double g(final SpacecraftState s) {
123         final PVCoordinates deltaPV = computeDeltaPV(s);
124         return Vector3D.dotProduct(deltaPV.getPosition(), deltaPV.getVelocity());
125     }
126 
127     /**
128      * Compute the relative PV between primary and secondary objects.
129      *
130      * @param s Spacecraft state.
131      *
132      * @return Relative position between primary (=s) and secondaryPVProvider.
133      *
134      * @deprecated The output type of this method shall be modified in the future to improve code efficiency (though it will
135      * still give access to the relative position and velocity)
136      */
137     @Deprecated
138     public PVCoordinates computeDeltaPV(final SpacecraftState s) {
139         final Vector3D primaryPos = s.getPosition();
140         final Vector3D primaryVel = s.getPVCoordinates().getVelocity();
141 
142         final PVCoordinates secondaryPV  = secondaryPVProvider.getPVCoordinates(s.getDate(), s.getFrame());
143         final Vector3D      secondaryPos = secondaryPV.getPosition();
144         final Vector3D      secondaryVel = secondaryPV.getVelocity();
145 
146         final Vector3D relativePos = secondaryPos.subtract(primaryPos);
147         final Vector3D relativeVel  = secondaryVel.subtract(primaryVel);
148 
149         return new PVCoordinates(relativePos, relativeVel);
150     }
151 
152     /** {@inheritDoc} */
153     @Override
154     protected ExtremumApproachDetector create(final AdaptableInterval newMaxCheck, final double newThreshold, final int newMaxIter,
155                                               final EventHandler newHandler) {
156         return new ExtremumApproachDetector(newMaxCheck, newThreshold, newMaxIter, newHandler, secondaryPVProvider);
157     }
158 
159     /**
160      * Get the secondary position-velocity provider stored in this instance.
161      *
162      * @return the secondary position-velocity provider stored in this instance
163      */
164     public PVCoordinatesProvider getSecondaryPVProvider() {
165         return secondaryPVProvider;
166     }
167 }