1 /* Copyright 2022-2025 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(EventDetectionSettings.getDefaultEventDetectionSettings(), 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 detectionSettings Detection settings
90 * @param handler Event handler to call at event occurrences.
91 * @param secondaryPVProvider PVCoordinates provider of the other object defining relative distance.
92 * @param distanceThreshold Relative distance threshold for event detection
93 * @see EventHandler
94 * @since 12.2
95 */
96 protected RelativeDistanceDetector(final EventDetectionSettings detectionSettings,
97 final EventHandler handler, final PVCoordinatesProvider secondaryPVProvider,
98 final double distanceThreshold) {
99 super(detectionSettings, handler);
100 this.secondaryPVProvider = secondaryPVProvider;
101 this.distanceThreshold = distanceThreshold;
102 }
103
104 /**
105 * The {@code g} is positive when the relative distance is larger or equal than the threshold,
106 * non-positive otherwise.
107 *
108 * @param s the current state information: date, kinematics, attitude
109 * @return value of the switching function
110 */
111 public double g(final SpacecraftState s) {
112 final Vector3D secondaryPosition = getSecondaryPVProvider().getPosition(s.getDate(), s.getFrame());
113 final double relativeDistance = s.getPosition().subtract(secondaryPosition).getNorm();
114 return relativeDistance - distanceThreshold;
115 }
116
117 /** {@inheritDoc} */
118 @Override
119 protected RelativeDistanceDetector create(final EventDetectionSettings detectionSettings,
120 final EventHandler newHandler) {
121 return new RelativeDistanceDetector(detectionSettings, newHandler, secondaryPVProvider, distanceThreshold);
122 }
123
124 /**
125 * Get the secondary position-velocity provider stored in this instance.
126 *
127 * @return the secondary position-velocity provider stored in this instance
128 */
129 public PVCoordinatesProvider getSecondaryPVProvider() {
130 return secondaryPVProvider;
131 }
132
133 /**
134 * Get the relative distance threshold.
135 *
136 * @return threshold triggering detection
137 */
138 public double getDistanceThreshold() {
139 return distanceThreshold;
140 }
141 }