1   /* Copyright 2002-2022 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.CalculusFieldElement;
20  import org.hipparchus.geometry.euclidean.threed.FieldRotation;
21  import org.hipparchus.geometry.euclidean.threed.Rotation;
22  import org.hipparchus.geometry.euclidean.threed.RotationConvention;
23  import org.hipparchus.geometry.euclidean.threed.RotationOrder;
24  import org.orekit.errors.OrekitException;
25  import org.orekit.errors.OrekitMessages;
26  import org.orekit.frames.FieldTransform;
27  import org.orekit.frames.Frame;
28  import org.orekit.frames.LOFType;
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   * Attitude law defined by fixed Roll, Pitch and Yaw angles (in any order)
40   * with respect to a local orbital frame.
41  
42   * <p>
43   * The attitude provider is defined as a rotation offset from some local orbital frame.
44   * @author V&eacute;ronique Pommier-Maurussane
45   */
46  public class LofOffset implements AttitudeProvider {
47  
48      /** Type of Local Orbital Frame. */
49      private LOFType type;
50  
51      /** Rotation from local orbital frame.  */
52      private final Rotation offset;
53  
54      /** Inertial frame with respect to which orbit should be computed. */
55      private final Frame inertialFrame;
56  
57      /** Create a LOF-aligned attitude.
58       * <p>
59       * Calling this constructor is equivalent to call
60       * {@code LofOffset(inertialFrame, LOFType, RotationOrder.XYZ, 0, 0, 0)}
61       * </p>
62       * @param inertialFrame inertial frame with respect to which orbit should be computed
63       * @param type type of Local Orbital Frame
64       */
65      public LofOffset(final Frame inertialFrame, final LOFType type) {
66          this(inertialFrame, type, RotationOrder.XYZ, 0, 0, 0);
67      }
68  
69      /** Creates new instance.
70       * <p>
71       * An important thing to note is that the rotation order and angles signs used here
72       * are compliant with an <em>attitude</em> definition, i.e. they correspond to
73       * a frame that rotate in a field of fixed vectors. So to retrieve the angles
74       * provided here from the Hipparchus underlying rotation, one has to either use the
75       * {@link RotationConvention#VECTOR_OPERATOR} and <em>revert</em> the rotation, or
76       * to use {@link RotationConvention#FRAME_TRANSFORM} as in the following code snippet:
77       * </p>
78       * <pre>
79       *   LofOffset law          = new LofOffset(inertial, lofType, order, alpha1, alpha2, alpha3);
80       *   Rotation  offsetAtt    = law.getAttitude(orbit).getRotation();
81       *   Rotation  alignedAtt   = new LofOffset(inertial, lofType).getAttitude(orbit).getRotation();
82       *   Rotation  offsetProper = offsetAtt.compose(alignedAtt.revert(), RotationConvention.VECTOR_OPERATOR);
83       *
84       *   // note the call to revert and the conventions in the following statement
85       *   double[] anglesV = offsetProper.revert().getAngles(order, RotationConvention.VECTOR_OPERATOR);
86       *   System.out.format(Locale.US, "%f == %f%n", alpha1, anglesV[0]);
87       *   System.out.format(Locale.US, "%f == %f%n", alpha2, anglesV[1]);
88       *   System.out.format(Locale.US, "%f == %f%n", alpha3, anglesV[2]);
89       *
90       *   // note the conventions in the following statement
91       *   double[] anglesF = offsetProper.getAngles(order, RotationConvention.FRAME_TRANSFORM);
92       *   System.out.format(Locale.US, "%f == %f%n", alpha1, anglesF[0]);
93       *   System.out.format(Locale.US, "%f == %f%n", alpha2, anglesF[1]);
94       *   System.out.format(Locale.US, "%f == %f%n", alpha3, anglesF[2]);
95       * </pre>
96       * @param inertialFrame inertial frame with respect to which orbit should be computed
97       * @param type type of Local Orbital Frame
98       * @param order order of rotations to use for (alpha1, alpha2, alpha3) composition
99       * @param alpha1 angle of the first elementary rotation
100      * @param alpha2 angle of the second elementary rotation
101      * @param alpha3 angle of the third elementary rotation
102      */
103     public LofOffset(final Frame inertialFrame, final LOFType type,
104                      final RotationOrder order, final double alpha1,
105                      final double alpha2, final double alpha3) {
106         this.type = type;
107         this.offset = new Rotation(order, RotationConvention.VECTOR_OPERATOR, alpha1, alpha2, alpha3).revert();
108         if (!inertialFrame.isPseudoInertial()) {
109             throw new OrekitException(OrekitMessages.NON_PSEUDO_INERTIAL_FRAME,
110                                       inertialFrame.getName());
111         }
112         this.inertialFrame = inertialFrame;
113     }
114 
115 
116     /** {@inheritDoc} */
117     public Attitude getAttitude(final PVCoordinatesProvider pvProv,
118                                 final AbsoluteDate date, final Frame frame) {
119 
120         // construction of the local orbital frame, using PV from inertial frame
121         final PVCoordinates pv = pvProv.getPVCoordinates(date, inertialFrame);
122         final Transform inertialToLof = type.transformFromInertial(date, pv);
123 
124         // take into account the specified start frame (which may not be an inertial one)
125         final Transform frameToInertial = frame.getTransformTo(inertialFrame, date);
126         final Transform frameToLof = new Transform(date, frameToInertial, inertialToLof);
127 
128         // compose with offset rotation
129         return new Attitude(date, frame,
130                             offset.compose(frameToLof.getRotation(), RotationConvention.VECTOR_OPERATOR),
131                             offset.applyTo(frameToLof.getRotationRate()),
132                             offset.applyTo(frameToLof.getRotationAcceleration()));
133 
134     }
135 
136     /** {@inheritDoc} */
137     public <T extends CalculusFieldElement<T>> FieldAttitude<T> getAttitude(final FieldPVCoordinatesProvider<T> pvProv,
138                                                                         final FieldAbsoluteDate<T> date,
139                                                                         final Frame frame) {
140 
141         // construction of the local orbital frame, using PV from inertial frame
142         final FieldPVCoordinates<T> pv = pvProv.getPVCoordinates(date, inertialFrame);
143         final FieldTransform<T> inertialToLof = type.transformFromInertial(date, pv);
144 
145         // take into account the specified start frame (which may not be an inertial one)
146         final FieldTransform<T> frameToInertial = frame.getTransformTo(inertialFrame, date);
147         final FieldTransform<T> frameToLof = new FieldTransform<>(date, frameToInertial, inertialToLof);
148 
149         // compose with offset rotation
150         return new FieldAttitude<>(date, frame,
151                                    frameToLof.getRotation().compose(offset, RotationConvention.FRAME_TRANSFORM),
152                                    FieldRotation.applyTo(offset, frameToLof.getRotationRate()),
153                                    FieldRotation.applyTo(offset, frameToLof.getRotationAcceleration()));
154 
155     }
156 }