1   /* Copyright 2002-2025 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.hipparchus.util.FastMath;
22  import org.hipparchus.util.SinCos;
23  import org.orekit.orbits.Orbit;
24  import org.orekit.propagation.SpacecraftState;
25  import org.orekit.propagation.events.handlers.EventHandler;
26  import org.orekit.propagation.events.handlers.StopOnIncreasing;
27  import org.orekit.utils.PVCoordinates;
28  import org.orekit.utils.PVCoordinatesProvider;
29  
30  /** Finder for satellite/body alignment events in orbital plane.
31   * <p>This class finds alignment events.</p>
32   * <p>Alignment means the conjunction, with some threshold angle, between the satellite
33   * position and the projection in the orbital plane of some body position.</p>
34   * <p>The default handler behavior is to {@link Action#STOP stop}
35   * propagation when alignment is reached. This can be changed by calling
36   * {@link #withHandler(EventHandler)} after construction.</p>
37   * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
38   * @author Pascal Parraud
39   */
40  public class AlignmentDetector extends AbstractDetector<AlignmentDetector> {
41  
42      /** Body to align. */
43      private final PVCoordinatesProvider body;
44  
45      /** Alignment angle (rad). */
46      private final double alignAngle;
47  
48      /** Cosinus of alignment angle. */
49      private final double cosAlignAngle;
50  
51      /** Sinus of alignment angle. */
52      private final double sinAlignAngle;
53  
54      /** Build a new alignment detector.
55       * <p>The orbit is used only to set an upper bound for the max check interval
56       * to period/3 and to set the convergence threshold according to orbit size.</p>
57       * @param orbit initial orbit
58       * @param body the body to align
59       * @param alignAngle the alignment angle (rad)
60       */
61      public AlignmentDetector(final Orbit orbit,
62                               final PVCoordinatesProvider body,
63                               final double alignAngle) {
64          this(1.0e-13 * orbit.getKeplerianPeriod(), orbit, body, alignAngle);
65      }
66  
67      /** Build a new alignment detector.
68       * @param maxCheck maximum checking interval (s)
69       * @param threshold convergence threshold (s)
70       * @param body the body to align
71       * @param alignAngle the alignment angle (rad)
72       */
73      public AlignmentDetector(final double maxCheck, final double threshold,
74                               final PVCoordinatesProvider body,
75                               final double alignAngle) {
76          this(new EventDetectionSettings(maxCheck, threshold, EventDetectionSettings.DEFAULT_MAX_ITER),
77               new StopOnIncreasing(), body, alignAngle);
78      }
79  
80      /** Build a new alignment detector.
81       * @param detectionSettings detection settings
82       * @param body the body to align
83       * @param alignAngle the alignment angle (rad)
84       * @since 13.0
85       */
86      public AlignmentDetector(final EventDetectionSettings detectionSettings,
87                               final PVCoordinatesProvider body,
88                               final double alignAngle) {
89          this(detectionSettings, new StopOnIncreasing(), body, alignAngle);
90      }
91  
92      /** Build a new alignment detector.
93       * <p>The orbit is used only to set an upper bound for the max check interval
94       * to period/3.</p>
95       * @param threshold convergence threshold (s)
96       * @param orbit initial orbit
97       * @param body the body to align
98       * @param alignAngle the alignment angle (rad)
99       */
100     public AlignmentDetector(final double threshold,
101                              final Orbit orbit,
102                              final PVCoordinatesProvider body,
103                              final double alignAngle) {
104         this(orbit.getKeplerianPeriod() / 3, threshold, body, alignAngle);
105     }
106 
107     /** Protected constructor with full parameters.
108      * <p>
109      * This constructor is not public as users are expected to use the builder
110      * API with the various {@code withXxx()} methods to set up the instance
111      * in a readable manner without using a huge amount of parameters.
112      * </p>
113      * @param detectionSettings detection settings
114      * @param handler event handler to call at event occurrences
115      * @param body the body to align
116      * @param alignAngle the alignment angle (rad)
117      * @since 13.0
118      */
119     protected AlignmentDetector(final EventDetectionSettings detectionSettings, final EventHandler handler,
120                                 final PVCoordinatesProvider body,
121                                 final double alignAngle) {
122         super(detectionSettings, handler);
123         final SinCos sc    = FastMath.sinCos(alignAngle);
124         this.body          = body;
125         this.alignAngle    = alignAngle;
126         this.cosAlignAngle = sc.cos();
127         this.sinAlignAngle = sc.sin();
128     }
129 
130     /** {@inheritDoc} */
131     @Override
132     protected AlignmentDetector create(final EventDetectionSettings detectionSettings, final EventHandler newHandler) {
133         return new AlignmentDetector(detectionSettings, newHandler, body, alignAngle);
134     }
135 
136     /** Get the body to align.
137      * @return the body to align
138      */
139     public PVCoordinatesProvider getPVCoordinatesProvider() {
140         return body;
141     }
142 
143     /** Get the alignment angle (rad).
144      * @return the alignment angle
145      */
146     public double getAlignAngle() {
147         return alignAngle;
148     }
149 
150     /** Compute the value of the switching function.
151      * This function measures the difference between the alignment angle and the
152      * angle between the satellite position and the body position projection in the
153      * orbital plane.
154      * @param s the current state information: date, kinematics, attitude
155      * @return value of the switching function
156      */
157     public double g(final SpacecraftState s) {
158         final PVCoordinates pv = s.getPVCoordinates();
159         final Vector3D a  = pv.getPosition().normalize();
160         final Vector3D b  = Vector3D.crossProduct(pv.getMomentum(), a).normalize();
161         final Vector3D x  = new Vector3D(cosAlignAngle, a,  sinAlignAngle, b);
162         final Vector3D y  = new Vector3D(sinAlignAngle, a, -cosAlignAngle, b);
163         final Vector3D pb = body.getPosition(s.getDate(), s.getFrame());
164         final double beta = FastMath.atan2(Vector3D.dotProduct(pb, y), Vector3D.dotProduct(pb, x));
165         final double betm = -FastMath.PI - beta;
166         final double betp =  FastMath.PI - beta;
167         if (beta < betm) {
168             return betm;
169         } else if (beta < betp) {
170             return beta;
171         } else {
172             return betp;
173         }
174     }
175 
176 }