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  package org.orekit.propagation.events;
18  
19  import org.hipparchus.analysis.differentiation.UnivariateDerivative1;
20  import org.hipparchus.analysis.differentiation.UnivariateDerivative1Field;
21  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
22  import org.orekit.propagation.SpacecraftState;
23  import org.orekit.propagation.events.handlers.EventHandler;
24  import org.orekit.time.FieldAbsoluteDate;
25  import org.orekit.utils.ExtendedPositionProvider;
26  import org.orekit.utils.FieldPVCoordinates;
27  
28  /** Detector of local extrema with angular separation.
29   * @author Romain Serra
30   * @see AngularSeparationDetector
31   * @since 13.1
32   */
33  public class ExtremumAngularSeparationDetector extends AbstractDetector<ExtremumAngularSeparationDetector> {
34  
35      /** Beacon at the center of the proximity zone. */
36      private final ExtendedPositionProvider beacon;
37  
38      /** Observer for the spacecraft, that may also see the beacon at the same time if they are too close. */
39      private final ExtendedPositionProvider observer;
40  
41      /** Protected constructor with full parameters.
42       * @param detectionSettings detection settings
43       * @param handler event handler to call at event occurrences
44       * @param beacon beacon at the center of the proximity zone
45       * @param observer observer for the spacecraft, that may also see
46       * the beacon at the same time if they are too close to each other
47       */
48      public ExtremumAngularSeparationDetector(final EventDetectionSettings detectionSettings,
49                                               final EventHandler handler,
50                                               final ExtendedPositionProvider beacon,
51                                                  final ExtendedPositionProvider observer) {
52          super(detectionSettings, handler);
53          this.beacon         = beacon;
54          this.observer       = observer;
55      }
56  
57      /** Get the beacon at the center of the proximity zone.
58       * @return beacon at the center of the proximity zone
59       */
60      public ExtendedPositionProvider getBeacon() {
61          return beacon;
62      }
63  
64      /** Get the observer for the spacecraft.
65       * @return observer for the spacecraft
66       */
67      public ExtendedPositionProvider getObserver() {
68          return observer;
69      }
70  
71      @Override
72      protected ExtremumAngularSeparationDetector create(final EventDetectionSettings detectionSettings,
73                                                         final EventHandler newHandler) {
74          return new ExtremumAngularSeparationDetector(detectionSettings, newHandler, beacon, observer);
75      }
76  
77      @Override
78      public double g(final SpacecraftState s) {
79          final FieldPVCoordinates<UnivariateDerivative1> pv = s.getPVCoordinates().toUnivariateDerivative1PV();
80          final UnivariateDerivative1 dt = new UnivariateDerivative1(0., 1.);
81          final FieldAbsoluteDate<UnivariateDerivative1> fieldDate = new FieldAbsoluteDate<>(UnivariateDerivative1Field.getInstance(),
82                  s.getDate()).shiftedBy(dt);
83          final FieldVector3D<UnivariateDerivative1> bP = beacon.getPosition(fieldDate, s.getFrame());
84          final FieldVector3D<UnivariateDerivative1> oP = observer.getPosition(fieldDate, s.getFrame());
85          final UnivariateDerivative1 separation = FieldVector3D.angle(pv.getPosition().subtract(oP), bP.subtract(oP));
86          return separation.getFirstDerivative();
87      }
88  }