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.attitudes;
18  
19  import org.hipparchus.Field;
20  import org.hipparchus.CalculusFieldElement;
21  import org.hipparchus.geometry.euclidean.threed.FieldRotation;
22  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
23  import org.hipparchus.geometry.euclidean.threed.Rotation;
24  import org.hipparchus.geometry.euclidean.threed.Vector3D;
25  import org.orekit.frames.FieldStaticTransform;
26  import org.orekit.frames.FieldTransform;
27  import org.orekit.frames.Frame;
28  import org.orekit.frames.StaticTransform;
29  import org.orekit.frames.Transform;
30  import org.orekit.time.AbsoluteDate;
31  import org.orekit.utils.ExtendedPositionProvider;
32  import org.orekit.time.FieldAbsoluteDate;
33  import org.orekit.utils.FieldPVCoordinates;
34  import org.orekit.utils.FieldPVCoordinatesProvider;
35  import org.orekit.utils.PVCoordinates;
36  import org.orekit.utils.PVCoordinatesProvider;
37  
38  
39  /**
40   * This class handles a celestial body pointed attitude provider.
41   * <p>The celestial body pointed law is defined by two main elements:
42   * <ul>
43   *   <li>a celestial body towards which some satellite axis is exactly aimed</li>
44   *   <li>a phasing reference defining the rotation around the pointing axis</li>
45   * </ul>
46   *
47   * <p>
48   * The celestial body implicitly defines two of the three degrees of freedom
49   * and the phasing reference defines the remaining degree of freedom. This definition
50   * can be represented as first aligning exactly the satellite pointing axis to
51   * the current direction of the celestial body, and then to find the rotation
52   * around this axis such that the satellite phasing axis is in the half-plane
53   * defined by a cut line on the pointing axis and containing the celestial
54   * phasing reference.
55   * </p>
56   * <p>
57   * In order for this definition to work, the user must ensure that the phasing
58   * reference is <strong>never</strong> aligned with the pointing reference.
59   * Since the pointed body moves as the date changes, this should be ensured
60   * regardless of the date. A simple way to do this for Sun, Moon or any planet
61   * pointing is to choose a phasing reference far from the ecliptic plane. Using
62   * <code>Vector3D.PLUS_K</code>, the equatorial pole, is perfect in these cases.
63   * </p>
64   * <p>Instances of this class are guaranteed to be immutable.</p>
65   * @author Luc Maisonobe
66   */
67  public class CelestialBodyPointed implements AttitudeProvider {
68  
69      /** Frame in which {@link #phasingCel} is defined. */
70      private final Frame celestialFrame;
71  
72      /** Celestial body to point at. */
73      private final ExtendedPositionProvider pointedBody;
74  
75      /** Phasing reference, in celestial frame. */
76      private final Vector3D phasingCel;
77  
78      /** Satellite axis aiming at the pointed body, in satellite frame. */
79      private final Vector3D pointingSat;
80  
81      /** Phasing reference, in satellite frame. */
82      private final Vector3D phasingSat;
83  
84      /** Creates new instance.
85       * @param celestialFrame frame in which <code>phasingCel</code> is defined
86       * @param pointedBody celestial body to point at
87       * @param phasingCel phasing reference, in celestial frame
88       * @param pointingSat satellite vector defining the pointing direction
89       * @param phasingSat phasing reference, in satellite frame
90       */
91      public CelestialBodyPointed(final Frame celestialFrame,
92                                  final ExtendedPositionProvider pointedBody,
93                                  final Vector3D phasingCel,
94                                  final Vector3D pointingSat,
95                                  final Vector3D phasingSat) {
96          this.celestialFrame = celestialFrame;
97          this.pointedBody    = pointedBody;
98          this.phasingCel     = phasingCel;
99          this.pointingSat    = pointingSat;
100         this.phasingSat     = phasingSat;
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public Attitude getAttitude(final PVCoordinatesProvider pvProv,
106                                 final AbsoluteDate date, final Frame frame) {
107 
108         final PVCoordinates satPV = pvProv.getPVCoordinates(date, celestialFrame);
109 
110         // compute celestial references at the specified date
111         final PVCoordinates bodyPV    = pointedBody.getPVCoordinates(date, celestialFrame);
112         final PVCoordinates pointing  = new PVCoordinates(satPV, bodyPV);
113         final Vector3D      pointingP = pointing.getPosition();
114         final double        r2        = Vector3D.dotProduct(pointingP, pointingP);
115 
116         // evaluate instant rotation axis due to sat and body motion only (no phasing yet)
117         final Vector3D rotAxisCel = new Vector3D(1 / r2, Vector3D.crossProduct(pointingP, pointing.getVelocity()));
118 
119         // fix instant rotation to take phasing constraint into account
120         // (adding a rotation around pointing axis ensuring the motion of the phasing axis
121         //  is constrained in the pointing-phasing plane)
122         final Vector3D v1    = Vector3D.crossProduct(rotAxisCel, phasingCel);
123         final Vector3D v2    = Vector3D.crossProduct(pointingP,  phasingCel);
124         final double   compensation = -Vector3D.dotProduct(v1, v2) / v2.getNormSq();
125         final Vector3D phasedRotAxisCel = new Vector3D(1.0, rotAxisCel, compensation, pointingP);
126 
127         // compute transform from celestial frame to satellite frame
128         final Rotation celToSatRotation =
129             new Rotation(pointingP, phasingCel, pointingSat, phasingSat);
130 
131         // build transform combining rotation and instant rotation axis
132         Transform transform = new Transform(date, celToSatRotation, celToSatRotation.applyTo(phasedRotAxisCel));
133         if (frame != celestialFrame) {
134             // prepend transform from specified frame to celestial frame
135             transform = new Transform(date, frame.getTransformTo(celestialFrame, date), transform);
136         }
137 
138         // build the attitude
139         return new Attitude(date, frame, transform.getRotation(), transform.getRotationRate(), transform.getRotationAcceleration());
140 
141     }
142 
143     /** {@inheritDoc} */
144     @Override
145     public Rotation getAttitudeRotation(final PVCoordinatesProvider pvProv,
146                                         final AbsoluteDate date,
147                                         final Frame frame) {
148         final Vector3D satPosition = pvProv.getPosition(date, celestialFrame);
149 
150         // compute celestial references at the specified date
151         final Vector3D bodyPosition    = pointedBody.getPosition(date, celestialFrame);
152         final Vector3D      pointingP  = bodyPosition.subtract(satPosition);
153 
154         // compute static transform from celestial frame to satellite frame
155         final Rotation celToSatRotation = new Rotation(pointingP, phasingCel, pointingSat, phasingSat);
156         StaticTransform staticTransform = StaticTransform.of(date, celToSatRotation);
157 
158         if (frame != celestialFrame) {
159             // prepend static transform from specified frame to celestial frame
160             staticTransform = StaticTransform.compose(date, frame.getStaticTransformTo(celestialFrame, date), staticTransform);
161         }
162         return staticTransform.getRotation();
163     }
164 
165     /** {@inheritDoc} */
166     @Override
167     public <T extends CalculusFieldElement<T>> FieldAttitude<T> getAttitude(final FieldPVCoordinatesProvider<T> pvProv,
168                                                                             final FieldAbsoluteDate<T> date,
169                                                                             final Frame frame) {
170 
171         final Field<T> field = date.getField();
172         final FieldPVCoordinates<T> satPV = pvProv.getPVCoordinates(date, celestialFrame);
173 
174         // compute celestial references at the specified date
175         final FieldPVCoordinates<T> bodyPV    = pointedBody.getPVCoordinates(date, celestialFrame);
176         final FieldPVCoordinates<T> pointing  = new FieldPVCoordinates<>(satPV, bodyPV);
177         final FieldVector3D<T>      pointingP = pointing.getPosition();
178         final T                     r2        = FieldVector3D.dotProduct(pointingP, pointingP);
179 
180         // evaluate instant rotation axis due to sat and body motion only (no phasing yet)
181         final FieldVector3D<T> rotAxisCel =
182             new FieldVector3D<>(r2.reciprocal(), FieldVector3D.crossProduct(pointingP, pointing.getVelocity()));
183 
184         // fix instant rotation to take phasing constraint into account
185         // (adding a rotation around pointing axis ensuring the motion of the phasing axis
186         //  is constrained in the pointing-phasing plane)
187         final FieldVector3D<T> v1           = FieldVector3D.crossProduct(rotAxisCel, phasingCel);
188         final FieldVector3D<T> v2           = FieldVector3D.crossProduct(pointingP,  phasingCel);
189         final T                compensation = FieldVector3D.dotProduct(v1, v2).negate().divide(v2.getNormSq());
190         final FieldVector3D<T> phasedRotAxisCel = new FieldVector3D<>(field.getOne(), rotAxisCel, compensation, pointingP);
191 
192         // compute transform from celestial frame to satellite frame
193         final FieldRotation<T> celToSatRotation =
194             new FieldRotation<>(pointingP, new FieldVector3D<>(field, phasingCel),
195                             new FieldVector3D<>(field, pointingSat), new FieldVector3D<>(field, phasingSat));
196 
197         // build transform combining rotation and instant rotation axis
198         FieldTransform<T> transform = new FieldTransform<>(date, celToSatRotation, celToSatRotation.applyTo(phasedRotAxisCel));
199         if (frame != celestialFrame) {
200             // prepend transform from specified frame to celestial frame
201             transform = new FieldTransform<>(date, frame.getTransformTo(celestialFrame, date), transform);
202         }
203 
204         // build the attitude
205         return new FieldAttitude<>(date, frame,
206                         transform.getRotation(), transform.getRotationRate(), transform.getRotationAcceleration());
207 
208     }
209 
210     /** {@inheritDoc} */
211     @Override
212     public <T extends CalculusFieldElement<T>> FieldRotation<T> getAttitudeRotation(final FieldPVCoordinatesProvider<T> pvProv,
213                                                                                     final FieldAbsoluteDate<T> date,
214                                                                                     final Frame frame) {
215         final Field<T> field = date.getField();
216         final FieldVector3D<T> satPosition = pvProv.getPosition(date, celestialFrame);
217 
218         // compute celestial references at the specified date
219         final FieldVector3D<T> bodyPosition    = pointedBody.getPosition(date, celestialFrame);
220         final FieldVector3D<T>      pointingP = bodyPosition.subtract(satPosition);
221 
222         // compute rotation from celestial frame to satellite frame
223         final FieldRotation<T> celToSatRotation =
224                 new FieldRotation<>(pointingP, new FieldVector3D<>(field, phasingCel),
225                         new FieldVector3D<>(field, pointingSat), new FieldVector3D<>(field, phasingSat));
226 
227         // build static transform combining rotation and instant rotation axis
228         FieldStaticTransform<T> staticTransform = FieldStaticTransform.of(date, celToSatRotation);
229         if (frame != celestialFrame) {
230             // prepend static transform from specified frame to celestial frame
231             staticTransform = FieldStaticTransform.compose(date, frame.getStaticTransformTo(celestialFrame, date), staticTransform);
232         }
233         return staticTransform.getRotation();
234     }
235 }