1   /* Copyright 2002-2019 CS Systèmes d'Information
2    * Licensed to CS Systèmes d'Information (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 java.io.Serializable;
20  import java.util.List;
21  import java.util.stream.Collectors;
22  import java.util.stream.Stream;
23  
24  import org.hipparchus.geometry.euclidean.threed.Rotation;
25  import org.hipparchus.geometry.euclidean.threed.RotationConvention;
26  import org.hipparchus.geometry.euclidean.threed.Vector3D;
27  import org.orekit.frames.Frame;
28  import org.orekit.frames.Transform;
29  import org.orekit.time.AbsoluteDate;
30  import org.orekit.time.TimeInterpolable;
31  import org.orekit.time.TimeShiftable;
32  import org.orekit.time.TimeStamped;
33  import org.orekit.utils.AngularCoordinates;
34  import org.orekit.utils.AngularDerivativesFilter;
35  import org.orekit.utils.TimeStampedAngularCoordinates;
36  
37  
38  /** This class handles attitude definition at a given date.
39  
40   * <p>This class represents the rotation between a reference frame and
41   * the satellite frame, as well as the spin of the satellite (axis and
42   * rotation rate).</p>
43   * <p>
44   * The state can be slightly shifted to close dates. This shift is based on
45   * a linear extrapolation for attitude taking the spin rate into account.
46   * It is <em>not</em> intended as a replacement for proper attitude propagation
47   * but should be sufficient for either small time shifts or coarse accuracy.
48   * </p>
49   * <p>The instance <code>Attitude</code> is guaranteed to be immutable.</p>
50   * @see     org.orekit.orbits.Orbit
51   * @see AttitudeProvider
52   * @author V&eacute;ronique Pommier-Maurussane
53   */
54  
55  public class Attitude
56      implements TimeStamped, TimeShiftable<Attitude>, TimeInterpolable<Attitude>, Serializable {
57  
58      /** Serializable UID. */
59      private static final long serialVersionUID = 20140611L;
60  
61      /** Reference frame. */
62      private final Frame referenceFrame;
63  
64       /** Attitude and spin.  */
65      private final TimeStampedAngularCoordinates orientation;
66  
67      /** Creates a new instance.
68       * @param referenceFrame reference frame from which attitude is defined
69       * @param orientation complete orientation between reference frame and satellite frame,
70       * including rotation rate
71       */
72      public Attitude(final Frame referenceFrame, final TimeStampedAngularCoordinates orientation) {
73          this.referenceFrame = referenceFrame;
74          this.orientation    = orientation;
75      }
76  
77      /** Creates a new instance.
78       * @param date date at which attitude is defined
79       * @param referenceFrame reference frame from which attitude is defined
80       * @param orientation complete orientation between reference frame and satellite frame,
81       * including rotation rate
82       */
83      public Attitude(final AbsoluteDate date, final Frame referenceFrame,
84                      final AngularCoordinates orientation) {
85          this(referenceFrame,
86               new TimeStampedAngularCoordinates(date,
87                                                 orientation.getRotation(),
88                                                 orientation.getRotationRate(),
89                                                 orientation.getRotationAcceleration()));
90      }
91  
92      /** Creates a new instance.
93       * @param date date at which attitude is defined
94       * @param referenceFrame reference frame from which attitude is defined
95       * @param attitude rotation between reference frame and satellite frame
96       * @param spin satellite spin (axis and velocity, in <strong>satellite</strong> frame)
97       * @param acceleration satellite rotation acceleration (in <strong>satellite</strong> frame)
98       */
99      public Attitude(final AbsoluteDate date, final Frame referenceFrame,
100                     final Rotation attitude, final Vector3D spin, final Vector3D acceleration) {
101         this(referenceFrame, new TimeStampedAngularCoordinates(date, attitude, spin, acceleration));
102     }
103 
104     /** Get a time-shifted attitude.
105      * <p>
106      * The state can be slightly shifted to close dates. This shift is based on
107      * a linear extrapolation for attitude taking the spin rate into account.
108      * It is <em>not</em> intended as a replacement for proper attitude propagation
109      * but should be sufficient for either small time shifts or coarse accuracy.
110      * </p>
111      * @param dt time shift in seconds
112      * @return a new attitude, shifted with respect to the instance (which is immutable)
113      */
114     public Attitude shiftedBy(final double dt) {
115         return new Attitude(referenceFrame, orientation.shiftedBy(dt));
116     }
117 
118     /** Get a similar attitude with a specific reference frame.
119      * <p>
120      * If the instance reference frame is already the specified one, the instance
121      * itself is returned without any object creation. Otherwise, a new instance
122      * will be created with the specified reference frame. In this case, the
123      * required intermediate rotation and spin between the specified and the
124      * original reference frame will be inserted.
125      * </p>
126      * @param newReferenceFrame desired reference frame for attitude
127      * @return an attitude that has the same orientation and motion as the instance,
128      * but guaranteed to have the specified reference frame
129      */
130     public Attitude withReferenceFrame(final Frame newReferenceFrame) {
131 
132         if (newReferenceFrame == referenceFrame) {
133             // simple case, the instance is already compliant
134             return this;
135         }
136 
137         // we have to take an intermediate rotation into account
138         final Transform t = newReferenceFrame.getTransformTo(referenceFrame, orientation.getDate());
139         return new Attitude(orientation.getDate(), newReferenceFrame,
140                             orientation.getRotation().compose(t.getRotation(), RotationConvention.VECTOR_OPERATOR),
141                             orientation.getRotationRate().add(orientation.getRotation().applyTo(t.getRotationRate())),
142                             orientation.getRotationAcceleration().add(orientation.getRotation().applyTo(t.getRotationAcceleration())));
143 
144     }
145 
146     /** Get the date of attitude parameters.
147      * @return date of the attitude parameters
148      */
149     public AbsoluteDate getDate() {
150         return orientation.getDate();
151     }
152 
153     /** Get the reference frame.
154      * @return referenceFrame reference frame from which attitude is defined.
155      */
156     public Frame getReferenceFrame() {
157         return referenceFrame;
158     }
159 
160     /** Get the complete orientation including spin.
161      * @return complete orientation including spin
162      * @see #getRotation()
163      * @see #getSpin()
164      */
165     public TimeStampedAngularCoordinates getOrientation() {
166         return orientation;
167     }
168 
169     /** Get the attitude rotation.
170      * @return attitude satellite rotation from reference frame.
171      * @see #getOrientation()
172      * @see #getSpin()
173      */
174     public Rotation getRotation() {
175         return orientation.getRotation();
176     }
177 
178     /** Get the satellite spin.
179      * <p>The spin vector is defined in <strong>satellite</strong> frame.</p>
180      * @return spin satellite spin (axis and velocity).
181      * @see #getOrientation()
182      * @see #getRotation()
183      */
184     public Vector3D getSpin() {
185         return orientation.getRotationRate();
186     }
187 
188     /** Get the satellite rotation acceleration.
189      * <p>The rotation acceleration. vector is defined in <strong>satellite</strong> frame.</p>
190      * @return rotation acceleration
191      * @see #getOrientation()
192      * @see #getRotation()
193      */
194     public Vector3D getRotationAcceleration() {
195         return orientation.getRotationAcceleration();
196     }
197 
198     /** {@inheritDoc}
199      * <p>
200      * The interpolated instance is created by polynomial Hermite interpolation
201      * on Rodrigues vector ensuring rotation rate remains the exact derivative of rotation.
202      * </p>
203      * <p>
204      * As this implementation of interpolation is polynomial, it should be used only
205      * with small samples (about 10-20 points) in order to avoid <a
206      * href="http://en.wikipedia.org/wiki/Runge%27s_phenomenon">Runge's phenomenon</a>
207      * and numerical problems (including NaN appearing).
208      * </p>
209      */
210     public Attitude interpolate(final AbsoluteDate interpolationDate, final Stream<Attitude> sample) {
211         final List<TimeStampedAngularCoordinates> datedPV =
212              sample.map(attitude -> attitude.orientation).collect(Collectors.toList());
213         final TimeStampedAngularCoordinates interpolated =
214                 TimeStampedAngularCoordinates.interpolate(interpolationDate, AngularDerivativesFilter.USE_RR, datedPV);
215         return new Attitude(referenceFrame, interpolated);
216     }
217 
218 }