1   /* Copyright 2022-2026 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   * @param <T> type of the field elements
47   * @see org.orekit.propagation.FieldPropagator#addEventDetector(FieldEventDetector)
48   * @author Romain Serra
49   * @since 12.1
50   */
51  public class FieldRelativeDistanceDetector<T extends CalculusFieldElement<T>>
52          extends FieldAbstractDetector<FieldRelativeDistanceDetector<T>, T> {
53  
54      /**
55       * PVCoordinates provider of the other object used to define relative distance.
56       */
57      private final FieldPVCoordinatesProvider<T> secondaryPVProvider;
58  
59      /** Relative distance value triggering detection. */
60      private final T distanceThreshold;
61  
62      /**
63       * Constructor with default values.
64       * <p>
65       * By default, the implemented behavior is to {@link Action#STOP stop} propagation at detection.
66       * </p>
67       *
68       * @param secondaryPVProvider PVCoordinates provider of the other object defining relative distance.
69       * @param distanceThreshold Relative distance threshold for event detection
70       */
71      public FieldRelativeDistanceDetector(final FieldPVCoordinatesProvider<T> secondaryPVProvider,
72                                           final T distanceThreshold) {
73          this(new FieldEventDetectionSettings<>(distanceThreshold.getField(), EventDetectionSettings.getDefaultEventDetectionSettings()),
74                  new FieldStopOnEvent<>(), secondaryPVProvider, distanceThreshold);
75      }
76  
77      /**
78       * Constructor.
79       * <p>
80       * This constructor is to be used if the user wants to change the default behavior of the detector.
81       * </p>
82       *
83       * @param detectionSettings   Detection settings.
84       * @param handler             Event handler to call at event occurrences.
85       * @param secondaryPVProvider PVCoordinates provider of the other object defining relative distance.
86       * @param distanceThreshold Relative distance threshold for event detection
87       * @see FieldEventHandler
88       * @since 12.2
89       */
90      protected FieldRelativeDistanceDetector(final FieldEventDetectionSettings<T> detectionSettings,
91                                              final FieldEventHandler<T> handler, final FieldPVCoordinatesProvider<T> secondaryPVProvider,
92                                              final T distanceThreshold) {
93          super(detectionSettings, handler);
94          this.secondaryPVProvider = secondaryPVProvider;
95          this.distanceThreshold = distanceThreshold;
96      }
97  
98      /**
99       * The {@code g} is positive when the relative distance is larger or equal than the threshold,
100      * non-positive otherwise.
101      *
102      * @param s the current state information: date, kinematics, attitude
103      * @return value of the switching function
104      */
105     @Override
106     public T g(final FieldSpacecraftState<T> s) {
107         final FieldVector3D<T> secondaryPosition = getSecondaryPVProvider().getPosition(s.getDate(), s.getFrame());
108         final T relativeDistance = s.getPosition().subtract(secondaryPosition).getNorm();
109         return relativeDistance.subtract(distanceThreshold);
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     protected FieldRelativeDistanceDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
115                                                       final FieldEventHandler<T> newHandler) {
116         return new FieldRelativeDistanceDetector<>(detectionSettings, newHandler, secondaryPVProvider,
117                 distanceThreshold);
118     }
119 
120     /**
121      * Get the secondary position-velocity provider stored in this instance.
122      *
123      * @return the secondary position-velocity provider stored in this instance
124      */
125     public FieldPVCoordinatesProvider<T> getSecondaryPVProvider() {
126         return secondaryPVProvider;
127     }
128 
129     /**
130      * Get the relative distance threshold.
131      *
132      * @return threshold triggering detection
133      */
134     public T getDistanceThreshold() {
135         return distanceThreshold;
136     }
137 }