1   /* Copyright 2002-2015 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.utils;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.List;
23  
24  import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
25  import org.apache.commons.math3.exception.MathArithmeticException;
26  import org.apache.commons.math3.exception.MathIllegalArgumentException;
27  import org.apache.commons.math3.exception.NumberIsTooLargeException;
28  import org.apache.commons.math3.geometry.euclidean.threed.FieldRotation;
29  import org.apache.commons.math3.geometry.euclidean.threed.Rotation;
30  import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
31  import org.apache.commons.math3.linear.DecompositionSolver;
32  import org.apache.commons.math3.linear.MatrixUtils;
33  import org.apache.commons.math3.linear.QRDecomposition;
34  import org.apache.commons.math3.linear.RealMatrix;
35  import org.apache.commons.math3.linear.RealVector;
36  import org.apache.commons.math3.linear.SingularMatrixException;
37  import org.apache.commons.math3.util.FastMath;
38  import org.apache.commons.math3.util.MathArrays;
39  import org.apache.commons.math3.util.Pair;
40  import org.orekit.errors.OrekitException;
41  import org.orekit.errors.OrekitMessages;
42  import org.orekit.time.AbsoluteDate;
43  import org.orekit.time.TimeShiftable;
44  
45  /** Simple container for rotation/rotation rate/rotation acceleration triplets.
46   * <p>
47   * The state can be slightly shifted to close dates. This shift is based on
48   * an approximate solution of the fixed acceleration motion. It is <em>not</em>
49   * intended as a replacement for proper attitude propagation but should be
50   * sufficient for either small time shifts or coarse accuracy.
51   * </p>
52   * <p>
53   * This class is the angular counterpart to {@link PVCoordinates}.
54   * </p>
55   * <p>Instances of this class are guaranteed to be immutable.</p>
56   * @author Luc Maisonobe
57   */
58  public class AngularCoordinates implements TimeShiftable<AngularCoordinates>, Serializable {
59  
60      /** Fixed orientation parallel with reference frame
61       * (identity rotation, zero rotation rate and acceleration).
62       */
63      public static final AngularCoordinates IDENTITY =
64              new AngularCoordinates(Rotation.IDENTITY, Vector3D.ZERO, Vector3D.ZERO);
65  
66      /** Serializable UID. */
67      private static final long serialVersionUID = 20140414L;
68  
69      /** Rotation. */
70      private final Rotation rotation;
71  
72      /** Rotation rate. */
73      private final Vector3D rotationRate;
74  
75      /** Rotation acceleration. */
76      private final Vector3D rotationAcceleration;
77  
78      /** Simple constructor.
79       * <p> Sets the Coordinates to default : Identity, Ω = (0 0 0), dΩ/dt = (0 0 0).</p>
80       */
81      public AngularCoordinates() {
82          this(Rotation.IDENTITY, Vector3D.ZERO, Vector3D.ZERO);
83      }
84  
85      /** Builds a rotation/rotation rate pair.
86       * @param rotation rotation
87       * @param rotationRate rotation rate Ω (rad/s)
88       */
89      public AngularCoordinates(final Rotation rotation, final Vector3D rotationRate) {
90          this(rotation, rotationRate, Vector3D.ZERO);
91      }
92  
93      /** Builds a rotation/rotation rate/rotation acceleration triplet.
94       * @param rotation rotation
95       * @param rotationRate rotation rate Ω (rad/s)
96       * @param rotationAcceleration rotation acceleration dΩ/dt (rad²/s²)
97       */
98      public AngularCoordinates(final Rotation rotation,
99                                final Vector3D rotationRate, final Vector3D rotationAcceleration) {
100         this.rotation             = rotation;
101         this.rotationRate         = rotationRate;
102         this.rotationAcceleration = rotationAcceleration;
103     }
104 
105     /** Build the rotation that transforms a pair of pv coordinates into another one.
106 
107      * <p><em>WARNING</em>! This method requires much more stringent assumptions on
108      * its parameters than the similar {@link Rotation#Rotation(Vector3D, Vector3D,
109      * Vector3D, Vector3D) constructor} from the {@link Rotation Rotation} class.
110      * As far as the Rotation constructor is concerned, the {@code v₂} vector from
111      * the second pair can be slightly misaligned. The Rotation constructor will
112      * compensate for this misalignment and create a rotation that ensure {@code
113      * v₁ = r(u₁)} and {@code v₂ ∈ plane (r(u₁), r(u₂))}. <em>THIS IS NOT
114      * TRUE ANYMORE IN THIS CLASS</em>! As derivatives are involved and must be
115      * preserved, this constructor works <em>only</em> if the two pairs are fully
116      * consistent, i.e. if a rotation exists that fulfill all the requirements: {@code
117      * v₁ = r(u₁)}, {@code v₂ = r(u₂)}, {@code dv₁/dt = dr(u₁)/dt}, {@code dv₂/dt
118      * = dr(u₂)/dt}, {@code d²v₁/dt² = d²r(u₁)/dt²}, {@code d²v₂/dt² = d²r(u₂)/dt²}.</p>
119      * @param u1 first vector of the origin pair
120      * @param u2 second vector of the origin pair
121      * @param v1 desired image of u1 by the rotation
122      * @param v2 desired image of u2 by the rotation
123      * @param tolerance relative tolerance factor used to check singularities
124      * @exception OrekitException if the vectors are inconsistent for the
125      * rotation to be found (null, aligned, ...)
126      */
127     public AngularCoordinates(final PVCoordinates u1, final PVCoordinates u2,
128                               final PVCoordinates v1, final PVCoordinates v2,
129                               final double tolerance)
130         throws OrekitException {
131 
132         try {
133             // find the initial fixed rotation
134             rotation = new Rotation(u1.getPosition(), u2.getPosition(),
135                                     v1.getPosition(), v2.getPosition());
136 
137             // find rotation rate Ω such that
138             //  Ω ⨯ v₁ = r(dot(u₁)) - dot(v₁)
139             //  Ω ⨯ v₂ = r(dot(u₂)) - dot(v₂)
140             final Vector3D ru1Dot = rotation.applyTo(u1.getVelocity());
141             final Vector3D ru2Dot = rotation.applyTo(u2.getVelocity());
142             rotationRate = inverseCrossProducts(v1.getPosition(), ru1Dot.subtract(v1.getVelocity()),
143                                                 v2.getPosition(), ru2Dot.subtract(v2.getVelocity()),
144                                                 tolerance);
145 
146             // find rotation acceleration dot(Ω) such that
147             // dot(Ω) ⨯ v₁ = r(dotdot(u₁)) - 2 Ω ⨯ dot(v₁) - Ω ⨯  (Ω ⨯ v₁) - dotdot(v₁)
148             // dot(Ω) ⨯ v₂ = r(dotdot(u₂)) - 2 Ω ⨯ dot(v₂) - Ω ⨯  (Ω ⨯ v₂) - dotdot(v₂)
149             final Vector3D ru1DotDot = rotation.applyTo(u1.getAcceleration());
150             final Vector3D oDotv1    = Vector3D.crossProduct(rotationRate, v1.getVelocity());
151             final Vector3D oov1      = Vector3D.crossProduct(rotationRate, Vector3D.crossProduct(rotationRate, v1.getPosition()));
152             final Vector3D c1        = new Vector3D(1, ru1DotDot, -2, oDotv1, -1, oov1, -1, v1.getAcceleration());
153             final Vector3D ru2DotDot = rotation.applyTo(u2.getAcceleration());
154             final Vector3D oDotv2    = Vector3D.crossProduct(rotationRate, v2.getVelocity());
155             final Vector3D oov2      = Vector3D.crossProduct(rotationRate, Vector3D.crossProduct(rotationRate, v2.getPosition()));
156             final Vector3D c2        = new Vector3D(1, ru2DotDot, -2, oDotv2, -1, oov2, -1, v2.getAcceleration());
157             rotationAcceleration     = inverseCrossProducts(v1.getPosition(), c1, v2.getPosition(), c2, tolerance);
158 
159         } catch (MathIllegalArgumentException miae) {
160             throw new OrekitException(miae);
161         } catch (MathArithmeticException mae) {
162             throw new OrekitException(mae);
163         }
164 
165     }
166 
167     /** Build one of the rotations that transform one pv coordinates into another one.
168 
169      * <p>Except for a possible scale factor, if the instance were
170      * applied to the vector u it will produce the vector v. There is an
171      * infinite number of such rotations, this constructor choose the
172      * one with the smallest associated angle (i.e. the one whose axis
173      * is orthogonal to the (u, v) plane). If u and v are collinear, an
174      * arbitrary rotation axis is chosen.</p>
175 
176      * @param u origin vector
177      * @param v desired image of u by the rotation
178      * @exception OrekitException if the vectors components cannot be converted to
179      * {@link DerivativeStructure} with proper order
180      */
181     public AngularCoordinates(final PVCoordinates u, final PVCoordinates v) throws OrekitException {
182         this(new FieldRotation<DerivativeStructure>(u.toDerivativeStructureVector(2),
183                                                     v.toDerivativeStructureVector(2)));
184     }
185 
186     /** Builds a AngularCoordinates from  a {@link FieldRotation}&lt;{@link DerivativeStructure}&gt;.
187      * <p>
188      * The rotation components must have time as their only derivation parameter and
189      * have consistent derivation orders.
190      * </p>
191      * @param r rotation with time-derivatives embedded within the coordinates
192      */
193     public AngularCoordinates(final FieldRotation<DerivativeStructure> r) {
194 
195         final double q0       = r.getQ0().getReal();
196         final double q1       = r.getQ1().getReal();
197         final double q2       = r.getQ2().getReal();
198         final double q3       = r.getQ3().getReal();
199 
200         rotation     = new Rotation(q0, q1, q2, q3, false);
201         if (r.getQ0().getOrder() >= 1) {
202             final double q0Dot    = r.getQ0().getPartialDerivative(1);
203             final double q1Dot    = r.getQ1().getPartialDerivative(1);
204             final double q2Dot    = r.getQ2().getPartialDerivative(1);
205             final double q3Dot    = r.getQ3().getPartialDerivative(1);
206             rotationRate =
207                     new Vector3D(2 * MathArrays.linearCombination(-q1, q0Dot,  q0, q1Dot,  q3, q2Dot, -q2, q3Dot),
208                                  2 * MathArrays.linearCombination(-q2, q0Dot, -q3, q1Dot,  q0, q2Dot,  q1, q3Dot),
209                                  2 * MathArrays.linearCombination(-q3, q0Dot,  q2, q1Dot, -q1, q2Dot,  q0, q3Dot));
210             if (r.getQ0().getOrder() >= 2) {
211                 final double q0DotDot = r.getQ0().getPartialDerivative(2);
212                 final double q1DotDot = r.getQ1().getPartialDerivative(2);
213                 final double q2DotDot = r.getQ2().getPartialDerivative(2);
214                 final double q3DotDot = r.getQ3().getPartialDerivative(2);
215                 rotationAcceleration =
216                         new Vector3D(2 * MathArrays.linearCombination(-q1, q0DotDot,  q0, q1DotDot,  q3, q2DotDot, -q2, q3DotDot),
217                                      2 * MathArrays.linearCombination(-q2, q0DotDot, -q3, q1DotDot,  q0, q2DotDot,  q1, q3DotDot),
218                                      2 * MathArrays.linearCombination(-q3, q0DotDot,  q2, q1DotDot, -q1, q2DotDot,  q0, q3DotDot));
219             } else {
220                 rotationAcceleration = Vector3D.ZERO;
221             }
222         } else {
223             rotationRate         = Vector3D.ZERO;
224             rotationAcceleration = Vector3D.ZERO;
225         }
226 
227     }
228 
229     /** Find a vector from two known cross products.
230      * <p>
231      * We want to find Ω such that: Ω ⨯ v₁ = c₁ and Ω ⨯ v₂ = c₂
232      * </p>
233      * <p>
234      * The first equation (Ω ⨯ v₁ = c₁) will always be fulfilled exactly,
235      * and the second one will be fulfilled if possible.
236      * </p>
237      * @param v1 vector forming the first known cross product
238      * @param c1 know vector for cross product Ω ⨯ v₁
239      * @param v2 vector forming the second known cross product
240      * @param c2 know vector for cross product Ω ⨯ v₂
241      * @param tolerance relative tolerance factor used to check singularities
242      * @return vector Ω such that: Ω ⨯ v₁ = c₁ and Ω ⨯ v₂ = c₂
243      * @exception MathIllegalArgumentException if vectors are inconsistent and
244      * no solution can be found
245      */
246     private static Vector3D inverseCrossProducts(final Vector3D v1, final Vector3D c1,
247                                                  final Vector3D v2, final Vector3D c2,
248                                                  final double tolerance)
249         throws MathIllegalArgumentException {
250 
251         final double v12 = v1.getNormSq();
252         final double v1n = FastMath.sqrt(v12);
253         final double v22 = v2.getNormSq();
254         final double v2n = FastMath.sqrt(v22);
255         final double threshold = tolerance * FastMath.max(v1n, v2n);
256 
257         Vector3D omega;
258 
259         try {
260             // create the over-determined linear system representing the two cross products
261             final RealMatrix m = MatrixUtils.createRealMatrix(6, 3);
262             m.setEntry(0, 1,  v1.getZ());
263             m.setEntry(0, 2, -v1.getY());
264             m.setEntry(1, 0, -v1.getZ());
265             m.setEntry(1, 2,  v1.getX());
266             m.setEntry(2, 0,  v1.getY());
267             m.setEntry(2, 1, -v1.getX());
268             m.setEntry(3, 1,  v2.getZ());
269             m.setEntry(3, 2, -v2.getY());
270             m.setEntry(4, 0, -v2.getZ());
271             m.setEntry(4, 2,  v2.getX());
272             m.setEntry(5, 0,  v2.getY());
273             m.setEntry(5, 1, -v2.getX());
274 
275             final RealVector rhs = MatrixUtils.createRealVector(new double[] {
276                 c1.getX(), c1.getY(), c1.getZ(),
277                 c2.getX(), c2.getY(), c2.getZ()
278             });
279 
280             // find the best solution we can
281             final DecompositionSolver solver = new QRDecomposition(m, threshold).getSolver();
282             final RealVector v = solver.solve(rhs);
283             omega = new Vector3D(v.getEntry(0), v.getEntry(1), v.getEntry(2));
284 
285         } catch (SingularMatrixException sme) {
286 
287             // handle some special cases for which we can compute a solution
288             final double c12 = c1.getNormSq();
289             final double c1n = FastMath.sqrt(c12);
290             final double c22 = c2.getNormSq();
291             final double c2n = FastMath.sqrt(c22);
292 
293             if (c1n <= threshold && c2n <= threshold) {
294                 // simple special case, velocities are cancelled
295                 return Vector3D.ZERO;
296             } else if (v1n <= threshold && c1n >= threshold) {
297                 // this is inconsistent, if v₁ is zero, c₁ must be 0 too
298                 throw new NumberIsTooLargeException(c1n, 0, true);
299             } else if (v2n <= threshold && c2n >= threshold) {
300                 // this is inconsistent, if v₂ is zero, c₂ must be 0 too
301                 throw new NumberIsTooLargeException(c2n, 0, true);
302             } else if (Vector3D.crossProduct(v1, v2).getNorm() <= threshold && v12 > threshold) {
303                 // simple special case, v₂ is redundant with v₁, we just ignore it
304                 // use the simplest Ω: orthogonal to both v₁ and c₁
305                 omega = new Vector3D(1.0 / v12, Vector3D.crossProduct(v1, c1));
306             } else {
307                 throw sme;
308             }
309 
310         }
311 
312         // check results
313         final double d1 = Vector3D.distance(Vector3D.crossProduct(omega, v1), c1);
314         if (d1 > threshold) {
315             throw new NumberIsTooLargeException(d1, 0, true);
316         }
317 
318         final double d2 = Vector3D.distance(Vector3D.crossProduct(omega, v2), c2);
319         if (d2 > threshold) {
320             throw new NumberIsTooLargeException(d2, 0, true);
321         }
322 
323         return omega;
324 
325     }
326 
327     /** Transform the instance to a {@link FieldRotation}&lt;{@link DerivativeStructure}&gt;.
328      * <p>
329      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
330      * to the user-specified order.
331      * </p>
332      * @param order derivation order for the vector components
333      * @return rotation with time-derivatives embedded within the coordinates
334      * @exception OrekitException if the user specified order is too large
335      */
336     public FieldRotation<DerivativeStructure> toDerivativeStructureRotation(final int order)
337         throws OrekitException {
338 
339         // quaternion components
340         final double q0 = rotation.getQ0();
341         final double q1 = rotation.getQ1();
342         final double q2 = rotation.getQ2();
343         final double q3 = rotation.getQ3();
344 
345         // first time-derivatives of the quaternion
346         final double oX    = rotationRate.getX();
347         final double oY    = rotationRate.getY();
348         final double oZ    = rotationRate.getZ();
349         final double q0Dot = 0.5 * MathArrays.linearCombination(-q1, oX, -q2, oY, -q3, oZ);
350         final double q1Dot = 0.5 * MathArrays.linearCombination( q0, oX, -q3, oY,  q2, oZ);
351         final double q2Dot = 0.5 * MathArrays.linearCombination( q3, oX,  q0, oY, -q1, oZ);
352         final double q3Dot = 0.5 * MathArrays.linearCombination(-q2, oX,  q1, oY,  q0, oZ);
353 
354         // second time-derivatives of the quaternion
355         final double oXDot = rotationAcceleration.getX();
356         final double oYDot = rotationAcceleration.getY();
357         final double oZDot = rotationAcceleration.getZ();
358         final double q0DotDot = -0.5 * MathArrays.linearCombination(new double[] {
359             q1, q2,  q3, q1Dot, q2Dot,  q3Dot
360         }, new double[] {
361             oXDot, oYDot, oZDot, oX, oY, oZ
362         });
363         final double q1DotDot =  0.5 * MathArrays.linearCombination(new double[] {
364             q0, q2, -q3, q0Dot, q2Dot, -q3Dot
365         }, new double[] {
366             oXDot, oZDot, oYDot, oX, oZ, oY
367         });
368         final double q2DotDot =  0.5 * MathArrays.linearCombination(new double[] {
369             q0, q3, -q1, q0Dot, q3Dot, -q1Dot
370         }, new double[] {
371             oYDot, oXDot, oZDot, oY, oX, oZ
372         });
373         final double q3DotDot =  0.5 * MathArrays.linearCombination(new double[] {
374             q0, q1, -q2, q0Dot, q1Dot, -q2Dot
375         }, new double[] {
376             oZDot, oYDot, oXDot, oZ, oY, oX
377         });
378 
379         final DerivativeStructure q0DS;
380         final DerivativeStructure q1DS;
381         final DerivativeStructure q2DS;
382         final DerivativeStructure q3DS;
383         switch(order) {
384         case 0 :
385             q0DS = new DerivativeStructure(1, 0, q0);
386             q1DS = new DerivativeStructure(1, 0, q1);
387             q2DS = new DerivativeStructure(1, 0, q2);
388             q3DS = new DerivativeStructure(1, 0, q3);
389             break;
390         case 1 :
391             q0DS = new DerivativeStructure(1, 1, q0, q0Dot);
392             q1DS = new DerivativeStructure(1, 1, q1, q1Dot);
393             q2DS = new DerivativeStructure(1, 1, q2, q2Dot);
394             q3DS = new DerivativeStructure(1, 1, q3, q3Dot);
395             break;
396         case 2 :
397             q0DS = new DerivativeStructure(1, 2, q0, q0Dot, q0DotDot);
398             q1DS = new DerivativeStructure(1, 2, q1, q1Dot, q1DotDot);
399             q2DS = new DerivativeStructure(1, 2, q2, q2Dot, q2DotDot);
400             q3DS = new DerivativeStructure(1, 2, q3, q3Dot, q3DotDot);
401             break;
402         default :
403             throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
404         }
405 
406         return new FieldRotation<DerivativeStructure>(q0DS, q1DS, q2DS, q3DS, false);
407 
408     }
409 
410     /** Estimate rotation rate between two orientations.
411      * <p>Estimation is based on a simple fixed rate rotation
412      * during the time interval between the two orientations.</p>
413      * @param start start orientation
414      * @param end end orientation
415      * @param dt time elapsed between the dates of the two orientations
416      * @return rotation rate allowing to go from start to end orientations
417      */
418     public static Vector3D estimateRate(final Rotation start, final Rotation end, final double dt) {
419         final Rotation evolution = start.applyTo(end.revert());
420         return new Vector3D(evolution.getAngle() / dt, evolution.getAxis());
421     }
422 
423     /** Revert a rotation/rotation rate/ rotation acceleration triplet.
424      * Build a triplet which reverse the effect of another triplet.
425      * @return a new triplet whose effect is the reverse of the effect
426      * of the instance
427      */
428     public AngularCoordinates revert() {
429         return new AngularCoordinates(rotation.revert(),
430                                       rotation.applyInverseTo(rotationRate).negate(),
431                                       rotation.applyInverseTo(rotationAcceleration).negate());
432     }
433 
434     /** Get a time-shifted state.
435      * <p>
436      * The state can be slightly shifted to close dates. This shift is based on
437      * an approximate solution of the fixed acceleration motion. It is <em>not</em>
438      * intended as a replacement for proper attitude propagation but should be
439      * sufficient for either small time shifts or coarse accuracy.
440      * </p>
441      * @param dt time shift in seconds
442      * @return a new state, shifted with respect to the instance (which is immutable)
443      */
444     public AngularCoordinates shiftedBy(final double dt) {
445 
446         // the shiftedBy method is based on a local approximation.
447         // It considers separately the contribution of the constant
448         // rotation, the linear contribution or the rate and the
449         // quadratic contribution of the acceleration. The rate
450         // and acceleration contributions are small rotations as long
451         // as the time shift is small, which is the crux of the algorithm.
452         // Small rotations are almost commutative, so we append these small
453         // contributions one after the other, as if they really occurred
454         // successively, despite this is not what really happens.
455 
456         // compute the linear contribution first, ignoring acceleration
457         // BEWARE: there is really a minus sign here, because if
458         // the target frame rotates in one direction, the vectors in the origin
459         // frame seem to rotate in the opposite direction
460         final double rate = rotationRate.getNorm();
461         final Rotation rateContribution = (rate == 0.0) ? Rotation.IDENTITY : new Rotation(rotationRate, -rate * dt);
462 
463         // append rotation and rate contribution
464         final AngularCoordinates linearPart =
465                 new AngularCoordinates(rateContribution.applyTo(rotation), rotationRate);
466 
467         final double acc  = rotationAcceleration.getNorm();
468         if (acc == 0.0) {
469             // no acceleration, the linear part is sufficient
470             return linearPart;
471         }
472 
473         // compute the quadratic contribution, ignoring initial rotation and rotation rate
474         // BEWARE: there is really a minus sign here, because if
475         // the target frame rotates in one direction, the vectors in the origin
476         // frame seem to rotate in the opposite direction
477         final AngularCoordinates quadraticContribution =
478                 new AngularCoordinates(new Rotation(rotationAcceleration, -0.5 * acc * dt * dt),
479                                        new Vector3D(dt, rotationAcceleration),
480                                        rotationAcceleration);
481 
482         // the quadratic contribution is a small rotation:
483         // its initial angle and angular rate are both zero.
484         // small rotations are almost commutative, so we append the small
485         // quadratic part after the linear part as a simple offset
486         return quadraticContribution.addOffset(linearPart);
487 
488     }
489 
490     /** Get the rotation.
491      * @return the rotation.
492      */
493     public Rotation getRotation() {
494         return rotation;
495     }
496 
497     /** Get the rotation rate.
498      * @return the rotation rate vector Ω (rad/s).
499      */
500     public Vector3D getRotationRate() {
501         return rotationRate;
502     }
503 
504     /** Get the rotation acceleration.
505      * @return the rotation acceleration vector dΩ/dt (rad²/s²).
506      */
507     public Vector3D getRotationAcceleration() {
508         return rotationAcceleration;
509     }
510 
511     /** Add an offset from the instance.
512      * <p>
513      * We consider here that the offset rotation is applied first and the
514      * instance is applied afterward. Note that angular coordinates do <em>not</em>
515      * commute under this operation, i.e. {@code a.addOffset(b)} and {@code
516      * b.addOffset(a)} lead to <em>different</em> results in most cases.
517      * </p>
518      * <p>
519      * The two methods {@link #addOffset(AngularCoordinates) addOffset} and
520      * {@link #subtractOffset(AngularCoordinates) subtractOffset} are designed
521      * so that round trip applications are possible. This means that both {@code
522      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
523      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
524      * </p>
525      * @param offset offset to subtract
526      * @return new instance, with offset subtracted
527      * @see #subtractOffset(AngularCoordinates)
528      */
529     public AngularCoordinates addOffset(final AngularCoordinates offset) {
530         final Vector3D rOmega    = rotation.applyTo(offset.rotationRate);
531         final Vector3D rOmegaDot = rotation.applyTo(offset.rotationAcceleration);
532         return new AngularCoordinates(rotation.applyTo(offset.rotation),
533                                       rotationRate.add(rOmega),
534                                       new Vector3D( 1.0, rotationAcceleration,
535                                                     1.0, rOmegaDot,
536                                                    -1.0, Vector3D.crossProduct(rotationRate, rOmega)));
537     }
538 
539     /** Subtract an offset from the instance.
540      * <p>
541      * We consider here that the offset rotation is applied first and the
542      * instance is applied afterward. Note that angular coordinates do <em>not</em>
543      * commute under this operation, i.e. {@code a.subtractOffset(b)} and {@code
544      * b.subtractOffset(a)} lead to <em>different</em> results in most cases.
545      * </p>
546      * <p>
547      * The two methods {@link #addOffset(AngularCoordinates) addOffset} and
548      * {@link #subtractOffset(AngularCoordinates) subtractOffset} are designed
549      * so that round trip applications are possible. This means that both {@code
550      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
551      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
552      * </p>
553      * @param offset offset to subtract
554      * @return new instance, with offset subtracted
555      * @see #addOffset(AngularCoordinates)
556      */
557     public AngularCoordinates subtractOffset(final AngularCoordinates offset) {
558         return addOffset(offset.revert());
559     }
560 
561     /** Apply the rotation to a pv coordinates.
562      * @param pv vector to apply the rotation to
563      * @return a new pv coordinates which is the image of u by the rotation
564      */
565     public PVCoordinates applyTo(final PVCoordinates pv) {
566 
567         final Vector3D transformedP = rotation.applyTo(pv.getPosition());
568         final Vector3D crossP       = Vector3D.crossProduct(rotationRate, transformedP);
569         final Vector3D transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
570         final Vector3D crossV       = Vector3D.crossProduct(rotationRate, transformedV);
571         final Vector3D crossCrossP  = Vector3D.crossProduct(rotationRate, crossP);
572         final Vector3D crossDotP    = Vector3D.crossProduct(rotationAcceleration, transformedP);
573         final Vector3D transformedA = new Vector3D( 1, rotation.applyTo(pv.getAcceleration()),
574                                                    -2, crossV,
575                                                    -1, crossCrossP,
576                                                    -1, crossDotP);
577 
578         return new PVCoordinates(transformedP, transformedV, transformedA);
579 
580     }
581 
582     /** Apply the rotation to a pv coordinates.
583      * @param pv vector to apply the rotation to
584      * @return a new pv coordinates which is the image of u by the rotation
585      */
586     public TimeStampedPVCoordinates applyTo(final TimeStampedPVCoordinates pv) {
587 
588         final Vector3D transformedP = getRotation().applyTo(pv.getPosition());
589         final Vector3D crossP       = Vector3D.crossProduct(getRotationRate(), transformedP);
590         final Vector3D transformedV = getRotation().applyTo(pv.getVelocity()).subtract(crossP);
591         final Vector3D crossV       = Vector3D.crossProduct(getRotationRate(), transformedV);
592         final Vector3D crossCrossP  = Vector3D.crossProduct(getRotationRate(), crossP);
593         final Vector3D crossDotP    = Vector3D.crossProduct(getRotationAcceleration(), transformedP);
594         final Vector3D transformedA = new Vector3D( 1, getRotation().applyTo(pv.getAcceleration()),
595                                                    -2, crossV,
596                                                    -1, crossCrossP,
597                                                    -1, crossDotP);
598 
599         return new TimeStampedPVCoordinates(pv.getDate(), transformedP, transformedV, transformedA);
600 
601     }
602 
603     /** Interpolate angular coordinates.
604      * <p>
605      * The interpolated instance is created by polynomial Hermite interpolation
606      * on Rodrigues vector ensuring rotation rate remains the exact derivative of rotation.
607      * </p>
608      * <p>
609      * This method is based on Sergei Tanygin's paper <a
610      * href="http://www.agi.com/downloads/resources/white-papers/Attitude-interpolation.pdf">Attitude
611      * Interpolation</a>, changing the norm of the vector to match the modified Rodrigues
612      * vector as described in Malcolm D. Shuster's paper <a
613      * href="http://www.ladispe.polito.it/corsi/Meccatronica/02JHCOR/2011-12/Slides/Shuster_Pub_1993h_J_Repsurv_scan.pdf">A
614      * Survey of Attitude Representations</a>. This change avoids the singularity at π.
615      * There is still a singularity at 2π, which is handled by slightly offsetting all rotations
616      * when this singularity is detected.
617      * </p>
618      * <p>
619      * Note that even if first time derivatives (rotation rates)
620      * from sample can be ignored, the interpolated instance always includes
621      * interpolated derivatives. This feature can be used explicitly to
622      * compute these derivatives when it would be too complex to compute them
623      * from an analytical formula: just compute a few sample points from the
624      * explicit formula and set the derivatives to zero in these sample points,
625      * then use interpolation to add derivatives consistent with the rotations.
626      * </p>
627      * @param date interpolation date
628      * @param useRotationRates if true, use sample points rotation rates,
629      * otherwise ignore them and use only rotations
630      * @param sample sample points on which interpolation should be done
631      * @return a new position-velocity, interpolated at specified date
632      * @exception OrekitException if the number of point is too small for interpolating
633      * @deprecated since 7.0 replaced with {@link TimeStampedAngularCoordinates#interpolate(AbsoluteDate, AngularDerivativesFilter, Collection)}
634      */
635     @Deprecated
636     public static AngularCoordinates interpolate(final AbsoluteDate date, final boolean useRotationRates,
637                                                  final Collection<Pair<AbsoluteDate, AngularCoordinates>> sample)
638         throws OrekitException {
639         final List<TimeStampedAngularCoordinates> list = new ArrayList<TimeStampedAngularCoordinates>(sample.size());
640         for (final Pair<AbsoluteDate, AngularCoordinates> pair : sample) {
641             list.add(new TimeStampedAngularCoordinates(pair.getFirst(),
642                                                        pair.getSecond().getRotation(),
643                                                        pair.getSecond().getRotationRate(),
644                                                        pair.getSecond().getRotationAcceleration()));
645         }
646         return TimeStampedAngularCoordinates.interpolate(date,
647                                                          useRotationRates ? AngularDerivativesFilter.USE_RR : AngularDerivativesFilter.USE_R,
648                                                          list);
649     }
650 
651     /** Convert rotation, rate and acceleration to modified Rodrigues vector and derivatives.
652      * <p>
653      * The modified Rodrigues vector is tan(θ/4) u where θ and u are the
654      * rotation angle and axis respectively.
655      * </p>
656      * @param sign multiplicative sign for quaternion components
657      * @return modified Rodrigues vector and derivatives (vector on row 0, first derivative
658      * on row 1, second derivative on row 2)
659      * @see #createFromModifiedRodrigues(double[][])
660      */
661     public double[][] getModifiedRodrigues(final double sign) {
662 
663         final double q0    = sign * getRotation().getQ0();
664         final double q1    = sign * getRotation().getQ1();
665         final double q2    = sign * getRotation().getQ2();
666         final double q3    = sign * getRotation().getQ3();
667         final double oX    = getRotationRate().getX();
668         final double oY    = getRotationRate().getY();
669         final double oZ    = getRotationRate().getZ();
670         final double oXDot = getRotationAcceleration().getX();
671         final double oYDot = getRotationAcceleration().getY();
672         final double oZDot = getRotationAcceleration().getZ();
673 
674         // first time-derivatives of the quaternion
675         final double q0Dot = 0.5 * MathArrays.linearCombination(-q1, oX, -q2, oY, -q3, oZ);
676         final double q1Dot = 0.5 * MathArrays.linearCombination( q0, oX, -q3, oY,  q2, oZ);
677         final double q2Dot = 0.5 * MathArrays.linearCombination( q3, oX,  q0, oY, -q1, oZ);
678         final double q3Dot = 0.5 * MathArrays.linearCombination(-q2, oX,  q1, oY,  q0, oZ);
679 
680         // second time-derivatives of the quaternion
681         final double q0DotDot = -0.5 * MathArrays.linearCombination(new double[] {
682             q1, q2,  q3, q1Dot, q2Dot,  q3Dot
683         }, new double[] {
684             oXDot, oYDot, oZDot, oX, oY, oZ
685         });
686         final double q1DotDot =  0.5 * MathArrays.linearCombination(new double[] {
687             q0, q2, -q3, q0Dot, q2Dot, -q3Dot
688         }, new double[] {
689             oXDot, oZDot, oYDot, oX, oZ, oY
690         });
691         final double q2DotDot =  0.5 * MathArrays.linearCombination(new double[] {
692             q0, q3, -q1, q0Dot, q3Dot, -q1Dot
693         }, new double[] {
694             oYDot, oXDot, oZDot, oY, oX, oZ
695         });
696         final double q3DotDot =  0.5 * MathArrays.linearCombination(new double[] {
697             q0, q1, -q2, q0Dot, q1Dot, -q2Dot
698         }, new double[] {
699             oZDot, oYDot, oXDot, oZ, oY, oX
700         });
701 
702         // the modified Rodrigues is tan(θ/4) u where θ and u are the rotation angle and axis respectively
703         // this can be rewritten using quaternion components:
704         //      r (q₁ / (1+q₀), q₂ / (1+q₀), q₃ / (1+q₀))
705         // applying the derivation chain rule to previous expression gives rDot and rDotDot
706         final double inv          = 1.0 / (1.0 + q0);
707         final double mTwoInvQ0Dot = -2 * inv * q0Dot;
708 
709         final double r1       = inv * q1;
710         final double r2       = inv * q2;
711         final double r3       = inv * q3;
712 
713         final double mInvR1   = -inv * r1;
714         final double mInvR2   = -inv * r2;
715         final double mInvR3   = -inv * r3;
716 
717         final double r1Dot    = MathArrays.linearCombination(inv, q1Dot, mInvR1, q0Dot);
718         final double r2Dot    = MathArrays.linearCombination(inv, q2Dot, mInvR2, q0Dot);
719         final double r3Dot    = MathArrays.linearCombination(inv, q3Dot, mInvR3, q0Dot);
720 
721         final double r1DotDot = MathArrays.linearCombination(inv, q1DotDot, mTwoInvQ0Dot, r1Dot, mInvR1, q0DotDot);
722         final double r2DotDot = MathArrays.linearCombination(inv, q2DotDot, mTwoInvQ0Dot, r2Dot, mInvR2, q0DotDot);
723         final double r3DotDot = MathArrays.linearCombination(inv, q3DotDot, mTwoInvQ0Dot, r3Dot, mInvR3, q0DotDot);
724 
725         return new double[][] {
726             {
727                 r1,       r2,       r3
728             }, {
729                 r1Dot,    r2Dot,    r3Dot
730             }, {
731                 r1DotDot, r2DotDot, r3DotDot
732             }
733         };
734 
735     }
736 
737     /** Convert a modified Rodrigues vector and derivatives to angular coordinates.
738      * @param r modified Rodrigues vector (with first and second times derivatives)
739      * @return angular coordinates
740      * @see #getModifiedRodrigues(double)
741      */
742     public static AngularCoordinates createFromModifiedRodrigues(final double[][] r) {
743 
744         // rotation
745         final double rSquared = r[0][0] * r[0][0] + r[0][1] * r[0][1] + r[0][2] * r[0][2];
746         final double oPQ0     = 2 / (1 + rSquared);
747         final double q0       = oPQ0 - 1;
748         final double q1       = oPQ0 * r[0][0];
749         final double q2       = oPQ0 * r[0][1];
750         final double q3       = oPQ0 * r[0][2];
751 
752         // rotation rate
753         final double oPQ02    = oPQ0 * oPQ0;
754         final double q0Dot    = -oPQ02 * MathArrays.linearCombination(r[0][0], r[1][0], r[0][1], r[1][1],  r[0][2], r[1][2]);
755         final double q1Dot    = oPQ0 * r[1][0] + r[0][0] * q0Dot;
756         final double q2Dot    = oPQ0 * r[1][1] + r[0][1] * q0Dot;
757         final double q3Dot    = oPQ0 * r[1][2] + r[0][2] * q0Dot;
758         final double oX       = 2 * MathArrays.linearCombination(-q1, q0Dot,  q0, q1Dot,  q3, q2Dot, -q2, q3Dot);
759         final double oY       = 2 * MathArrays.linearCombination(-q2, q0Dot, -q3, q1Dot,  q0, q2Dot,  q1, q3Dot);
760         final double oZ       = 2 * MathArrays.linearCombination(-q3, q0Dot,  q2, q1Dot, -q1, q2Dot,  q0, q3Dot);
761 
762         // rotation acceleration
763         final double q0DotDot = (1 - q0) / oPQ0 * q0Dot * q0Dot -
764                                 oPQ02 * MathArrays.linearCombination(r[0][0], r[2][0], r[0][1], r[2][1], r[0][2], r[2][2]) -
765                                 (q1Dot * q1Dot + q2Dot * q2Dot + q3Dot * q3Dot);
766         final double q1DotDot = MathArrays.linearCombination(oPQ0, r[2][0], 2 * r[1][0], q0Dot, r[0][0], q0DotDot);
767         final double q2DotDot = MathArrays.linearCombination(oPQ0, r[2][1], 2 * r[1][1], q0Dot, r[0][1], q0DotDot);
768         final double q3DotDot = MathArrays.linearCombination(oPQ0, r[2][2], 2 * r[1][2], q0Dot, r[0][2], q0DotDot);
769         final double oXDot    = 2 * MathArrays.linearCombination(-q1, q0DotDot,  q0, q1DotDot,  q3, q2DotDot, -q2, q3DotDot);
770         final double oYDot    = 2 * MathArrays.linearCombination(-q2, q0DotDot, -q3, q1DotDot,  q0, q2DotDot,  q1, q3DotDot);
771         final double oZDot    = 2 * MathArrays.linearCombination(-q3, q0DotDot,  q2, q1DotDot, -q1, q2DotDot,  q0, q3DotDot);
772 
773         return new AngularCoordinates(new Rotation(q0, q1, q2, q3, false),
774                                       new Vector3D(oX, oY, oZ),
775                                       new Vector3D(oXDot, oYDot, oZDot));
776 
777     }
778 
779 }