1   /* Copyright 2002-2024 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(AdaptableInterval.of(maxCheck), threshold, DEFAULT_MAX_ITER,
77               new StopOnIncreasing(),
78               body, alignAngle);
79      }
80  
81      /** Build a new alignment detector.
82       * <p>The orbit is used only to set an upper bound for the max check interval
83       * to period/3.</p>
84       * @param threshold convergence threshold (s)
85       * @param orbit initial orbit
86       * @param body the body to align
87       * @param alignAngle the alignment angle (rad)
88       */
89      public AlignmentDetector(final double threshold,
90                               final Orbit orbit,
91                               final PVCoordinatesProvider body,
92                               final double alignAngle) {
93          this(orbit.getKeplerianPeriod() / 3, threshold, body, alignAngle);
94      }
95  
96      /** Protected constructor with full parameters.
97       * <p>
98       * This constructor is not public as users are expected to use the builder
99       * API with the various {@code withXxx()} methods to set up the instance
100      * in a readable manner without using a huge amount of parameters.
101      * </p>
102      * @param maxCheck maximum checking interval (s)
103      * @param threshold convergence threshold (s)
104      * @param maxIter maximum number of iterations in the event time search
105      * @param handler event handler to call at event occurrences
106      * @param body the body to align
107      * @param alignAngle the alignment angle (rad)
108      */
109     protected AlignmentDetector(final AdaptableInterval maxCheck, final double threshold,
110                                 final int maxIter, final EventHandler handler,
111                                 final PVCoordinatesProvider body,
112                                 final double alignAngle) {
113         super(maxCheck, threshold, maxIter, handler);
114         final SinCos sc    = FastMath.sinCos(alignAngle);
115         this.body          = body;
116         this.alignAngle    = alignAngle;
117         this.cosAlignAngle = sc.cos();
118         this.sinAlignAngle = sc.sin();
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     protected AlignmentDetector create(final AdaptableInterval newMaxCheck, final double newThreshold,
124                                        final int newMaxIter, final EventHandler newHandler) {
125         return new AlignmentDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
126                                      body, alignAngle);
127     }
128 
129     /** Get the body to align.
130      * @return the body to align
131      */
132     public PVCoordinatesProvider getPVCoordinatesProvider() {
133         return body;
134     }
135 
136     /** Get the alignment angle (rad).
137      * @return the alignment angle
138      */
139     public double getAlignAngle() {
140         return alignAngle;
141     }
142 
143     /** Compute the value of the switching function.
144      * This function measures the difference between the alignment angle and the
145      * angle between the satellite position and the body position projection in the
146      * orbital plane.
147      * @param s the current state information: date, kinematics, attitude
148      * @return value of the switching function
149      */
150     public double g(final SpacecraftState s) {
151         final PVCoordinates pv = s.getPVCoordinates();
152         final Vector3D a  = pv.getPosition().normalize();
153         final Vector3D b  = Vector3D.crossProduct(pv.getMomentum(), a).normalize();
154         final Vector3D x  = new Vector3D(cosAlignAngle, a,  sinAlignAngle, b);
155         final Vector3D y  = new Vector3D(sinAlignAngle, a, -cosAlignAngle, b);
156         final Vector3D pb = body.getPosition(s.getDate(), s.getFrame());
157         final double beta = FastMath.atan2(Vector3D.dotProduct(pb, y), Vector3D.dotProduct(pb, x));
158         final double betm = -FastMath.PI - beta;
159         final double betp =  FastMath.PI - beta;
160         if (beta < betm) {
161             return betm;
162         } else if (beta < betp) {
163             return beta;
164         } else {
165             return betp;
166         }
167     }
168 
169 }