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
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(EventDetectionSettings.getDefaultEventDetectionSettings(), 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 detectionSettings Detection settings.
102 * @param handler Event handler to call at event occurrences.
103 * @param secondaryPVProvider PVCoordinates provider of the other object with which we want to find out the extremum
104 * approach.
105 * @see EventHandler
106 * @since 13.0
107 */
108 protected ExtremumApproachDetector(final EventDetectionSettings detectionSettings,
109 final EventHandler handler, final PVCoordinatesProvider secondaryPVProvider) {
110 super(detectionSettings, handler);
111 this.secondaryPVProvider = secondaryPVProvider;
112 }
113
114 /**
115 * The {@code g} is positive when the primary object is getting further away from the secondary object and is
116 * negative when it is getting closer to it.
117 *
118 * @param s the current state information: date, kinematics, attitude
119 * @return value of the switching function
120 */
121 public double g(final SpacecraftState s) {
122 final PVCoordinates deltaPV = computeDeltaPV(s);
123 return Vector3D.dotProduct(deltaPV.getPosition(), deltaPV.getVelocity());
124 }
125
126 /**
127 * Compute the relative PV between primary and secondary objects.
128 *
129 * @param s Spacecraft state.
130 *
131 * @return Relative position between primary (=s) and secondaryPVProvider.
132 */
133 public PVCoordinates computeDeltaPV(final SpacecraftState s) {
134 final Vector3D primaryPos = s.getPosition();
135 final Vector3D primaryVel = s.getPVCoordinates().getVelocity();
136
137 final PVCoordinates secondaryPV = secondaryPVProvider.getPVCoordinates(s.getDate(), s.getFrame());
138 final Vector3D secondaryPos = secondaryPV.getPosition();
139 final Vector3D secondaryVel = secondaryPV.getVelocity();
140
141 final Vector3D relativePos = secondaryPos.subtract(primaryPos);
142 final Vector3D relativeVel = secondaryVel.subtract(primaryVel);
143
144 return new PVCoordinates(relativePos, relativeVel);
145 }
146
147 /** {@inheritDoc} */
148 @Override
149 protected ExtremumApproachDetector create(final EventDetectionSettings detectionSettings,
150 final EventHandler newHandler) {
151 return new ExtremumApproachDetector(detectionSettings, newHandler, secondaryPVProvider);
152 }
153
154 /**
155 * Get the secondary position-velocity provider stored in this instance.
156 *
157 * @return the secondary position-velocity provider stored in this instance
158 */
159 public PVCoordinatesProvider getSecondaryPVProvider() {
160 return secondaryPVProvider;
161 }
162 }