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