1   /* Copyright 2002-2021 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  package org.orekit.propagation.events;
18  
19  import org.hipparchus.geometry.euclidean.threed.Vector3D;
20  import org.hipparchus.ode.events.Action;
21  import org.orekit.orbits.Orbit;
22  import org.orekit.propagation.SpacecraftState;
23  import org.orekit.propagation.events.handlers.EventHandler;
24  import org.orekit.propagation.events.handlers.StopOnIncreasing;
25  import org.orekit.utils.PVCoordinates;
26  
27  /** Finder for apside crossing events.
28   * <p>This class finds apside crossing events (i.e. apogee or perigee crossing).</p>
29   * <p>The default implementation behavior is to {@link Action#CONTINUE continue}
30   * propagation at apogee crossing and to {@link Action#STOP stop} propagation
31   * at perigee crossing. This can be changed by calling
32   * {@link #withHandler(EventHandler)} after construction.</p>
33   * <p>Beware that apside detection will fail for almost circular orbits. If
34   * for example an apside detector is used to trigger an {@link
35   * org.orekit.forces.maneuvers.ImpulseManeuver ImpulseManeuver} and the maneuver
36   * change the orbit shape to circular, then the detector may completely fail just
37   * after the maneuver has been performed!</p>
38   * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
39   * @author Luc Maisonobe
40   */
41  public class ApsideDetector extends AbstractDetector<ApsideDetector> {
42  
43      /** Build a new instance.
44       * <p>The orbit is used only to set an upper bound for the
45       * max check interval to period/3 and to set the convergence
46       * threshold according to orbit size</p>
47       * @param orbit initial orbit
48       */
49      public ApsideDetector(final Orbit orbit) {
50          this(1.0e-13 * orbit.getKeplerianPeriod(), orbit);
51      }
52  
53      /** Build a new instance.
54       * <p>The orbit is used only to set an upper bound for the
55       * max check interval to period/3</p>
56       * @param threshold convergence threshold (s)
57       * @param orbit initial orbit
58       */
59      public ApsideDetector(final double threshold, final Orbit orbit) {
60          super(orbit.getKeplerianPeriod() / 3, threshold,
61                DEFAULT_MAX_ITER, new StopOnIncreasing<ApsideDetector>());
62      }
63  
64      /** Private constructor with full parameters.
65       * <p>
66       * This constructor is private as users are expected to use the builder
67       * API with the various {@code withXxx()} methods to set up the instance
68       * in a readable manner without using a huge amount of parameters.
69       * </p>
70       * @param maxCheck maximum checking interval (s)
71       * @param threshold convergence threshold (s)
72       * @param maxIter maximum number of iterations in the event time search
73       * @param handler event handler to call at event occurrences
74       * @since 6.1
75       */
76      private ApsideDetector(final double maxCheck, final double threshold,
77                             final int maxIter, final EventHandler<? super ApsideDetector> handler) {
78          super(maxCheck, threshold, maxIter, handler);
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      protected ApsideDetector create(final double newMaxCheck, final double newThreshold,
84                                      final int newMaxIter, final EventHandler<? super ApsideDetector> newHandler) {
85          return new ApsideDetector(newMaxCheck, newThreshold, newMaxIter, newHandler);
86      }
87  
88      /** Compute the value of the switching function.
89       * This function computes the dot product of the 2 vectors : position.velocity.
90       * @param s the current state information: date, kinematics, attitude
91       * @return value of the switching function
92       */
93      public double g(final SpacecraftState s) {
94          final PVCoordinates pv = s.getPVCoordinates();
95          return Vector3D.dotProduct(pv.getPosition(), pv.getVelocity());
96      }
97  
98  }