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.CalculusFieldElement;
21  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
22  import org.hipparchus.ode.events.Action;
23  import org.orekit.propagation.FieldSpacecraftState;
24  import org.orekit.propagation.events.handlers.FieldEventHandler;
25  import org.orekit.propagation.events.handlers.FieldStopOnEvent;
26  import org.orekit.utils.FieldPVCoordinatesProvider;
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(org.orekit.propagation.events.handlers.FieldEventHandler)} after construction.
33   * </p>
34   * <p>
35   * As this detector needs two objects (moving relative to each other), it embeds one
36   * {@link org.orekit.utils.FieldPVCoordinatesProvider 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 org.orekit.utils.FieldPVCoordinatesProvider 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>
41   * For efficiency reason during the event search loop, it is recommended to have the secondary provider be an analytical
42   * propagator or an ephemeris. A numerical propagator as a secondary propagator works but is expected to be
43   * computationally costly.
44   * </p>
45   *
46   * @see org.orekit.propagation.FieldPropagator#addEventDetector(FieldEventDetector)
47   * @author Romain Serra
48   * @since 12.1
49   */
50  public class FieldRelativeDistanceDetector<T extends CalculusFieldElement<T>>
51          extends FieldAbstractDetector<FieldRelativeDistanceDetector<T>, T> {
52  
53      /**
54       * PVCoordinates provider of the other object used to define relative distance.
55       */
56      private final FieldPVCoordinatesProvider<T> secondaryPVProvider;
57  
58      /** Relative distance value triggering detection. */
59      private final T distanceThreshold;
60  
61      /**
62       * Constructor with default values.
63       * <p>
64       * By default, the implemented behavior is to {@link Action#STOP stop} propagation at detection.
65       * </p>
66       *
67       * @param secondaryPVProvider PVCoordinates provider of the other object defining relative distance.
68       * @param distanceThreshold Relative distance threshold for event detection
69       */
70      public FieldRelativeDistanceDetector(final FieldPVCoordinatesProvider<T> secondaryPVProvider,
71                                           final T distanceThreshold) {
72          this(new FieldEventDetectionSettings<>(distanceThreshold.getField(), EventDetectionSettings.getDefaultEventDetectionSettings()),
73                  new FieldStopOnEvent<>(), secondaryPVProvider, distanceThreshold);
74      }
75  
76      /**
77       * Constructor.
78       * <p>
79       * This constructor is to be used if the user wants to change the default behavior of the detector.
80       * </p>
81       *
82       * @param detectionSettings   Detection settings.
83       * @param handler             Event handler to call at event occurrences.
84       * @param secondaryPVProvider PVCoordinates provider of the other object defining relative distance.
85       * @param distanceThreshold Relative distance threshold for event detection
86       * @see FieldEventHandler
87       * @since 12.2
88       */
89      protected FieldRelativeDistanceDetector(final FieldEventDetectionSettings<T> detectionSettings,
90                                              final FieldEventHandler<T> handler, final FieldPVCoordinatesProvider<T> secondaryPVProvider,
91                                              final T distanceThreshold) {
92          super(detectionSettings, handler);
93          this.secondaryPVProvider = secondaryPVProvider;
94          this.distanceThreshold = distanceThreshold;
95      }
96  
97      /**
98       * The {@code g} is positive when the relative distance is larger or equal than the threshold,
99       * non-positive otherwise.
100      *
101      * @param s the current state information: date, kinematics, attitude
102      * @return value of the switching function
103      */
104     @Override
105     public T g(final FieldSpacecraftState<T> s) {
106         final FieldVector3D<T> secondaryPosition = getSecondaryPVProvider().getPosition(s.getDate(), s.getFrame());
107         final T relativeDistance = s.getPosition().subtract(secondaryPosition).getNorm();
108         return relativeDistance.subtract(distanceThreshold);
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     protected FieldRelativeDistanceDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
114                                                       final FieldEventHandler<T> newHandler) {
115         return new FieldRelativeDistanceDetector<>(detectionSettings, newHandler, secondaryPVProvider,
116                 distanceThreshold);
117     }
118 
119     /**
120      * Get the secondary position-velocity provider stored in this instance.
121      *
122      * @return the secondary position-velocity provider stored in this instance
123      */
124     public FieldPVCoordinatesProvider<T> getSecondaryPVProvider() {
125         return secondaryPVProvider;
126     }
127 
128     /**
129      * Get the relative distance threshold.
130      *
131      * @return threshold triggering detection
132      */
133     public T getDistanceThreshold() {
134         return distanceThreshold;
135     }
136 }