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