AngularCoordinates.java

  1. /* Copyright 2002-2016 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. import java.io.Serializable;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.List;

  22. import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
  23. import org.apache.commons.math3.exception.MathArithmeticException;
  24. import org.apache.commons.math3.exception.MathIllegalArgumentException;
  25. import org.apache.commons.math3.exception.NumberIsTooLargeException;
  26. import org.apache.commons.math3.geometry.euclidean.threed.FieldRotation;
  27. import org.apache.commons.math3.geometry.euclidean.threed.Rotation;
  28. import org.apache.commons.math3.geometry.euclidean.threed.RotationConvention;
  29. import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
  30. import org.apache.commons.math3.linear.DecompositionSolver;
  31. import org.apache.commons.math3.linear.MatrixUtils;
  32. import org.apache.commons.math3.linear.QRDecomposition;
  33. import org.apache.commons.math3.linear.RealMatrix;
  34. import org.apache.commons.math3.linear.RealVector;
  35. import org.apache.commons.math3.linear.SingularMatrixException;
  36. import org.apache.commons.math3.util.FastMath;
  37. import org.apache.commons.math3.util.MathArrays;
  38. import org.apache.commons.math3.util.Pair;
  39. import org.orekit.errors.OrekitException;
  40. import org.orekit.errors.OrekitMessages;
  41. import org.orekit.time.AbsoluteDate;
  42. import org.orekit.time.TimeShiftable;

  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.     /** Fixed orientation parallel with reference frame
  58.      * (identity rotation, zero rotation rate and acceleration).
  59.      */
  60.     public static final AngularCoordinates IDENTITY =
  61.             new AngularCoordinates(Rotation.IDENTITY, Vector3D.ZERO, Vector3D.ZERO);

  62.     /** Serializable UID. */
  63.     private static final long serialVersionUID = 20140414L;

  64.     /** Rotation. */
  65.     private final Rotation rotation;

  66.     /** Rotation rate. */
  67.     private final Vector3D rotationRate;

  68.     /** Rotation acceleration. */
  69.     private final Vector3D rotationAcceleration;

  70.     /** Simple constructor.
  71.      * <p> Sets the Coordinates to default : Identity, Ω = (0 0 0), dΩ/dt = (0 0 0).</p>
  72.      */
  73.     public AngularCoordinates() {
  74.         this(Rotation.IDENTITY, Vector3D.ZERO, Vector3D.ZERO);
  75.     }

  76.     /** Builds a rotation/rotation rate pair.
  77.      * @param rotation rotation
  78.      * @param rotationRate rotation rate Ω (rad/s)
  79.      */
  80.     public AngularCoordinates(final Rotation rotation, final Vector3D rotationRate) {
  81.         this(rotation, rotationRate, Vector3D.ZERO);
  82.     }

  83.     /** Builds a rotation/rotation rate/rotation acceleration triplet.
  84.      * @param rotation rotation
  85.      * @param rotationRate rotation rate Ω (rad/s)
  86.      * @param rotationAcceleration rotation acceleration dΩ/dt (rad²/s²)
  87.      */
  88.     public AngularCoordinates(final Rotation rotation,
  89.                               final Vector3D rotationRate, final Vector3D rotationAcceleration) {
  90.         this.rotation             = rotation;
  91.         this.rotationRate         = rotationRate;
  92.         this.rotationAcceleration = rotationAcceleration;
  93.     }

  94.     /** Build the rotation that transforms a pair of pv coordinates into another one.

  95.      * <p><em>WARNING</em>! This method requires much more stringent assumptions on
  96.      * its parameters than the similar {@link Rotation#Rotation(Vector3D, Vector3D,
  97.      * Vector3D, Vector3D) constructor} from the {@link Rotation Rotation} class.
  98.      * As far as the Rotation constructor is concerned, the {@code v₂} vector from
  99.      * the second pair can be slightly misaligned. The Rotation constructor will
  100.      * compensate for this misalignment and create a rotation that ensure {@code
  101.      * v₁ = r(u₁)} and {@code v₂ ∈ plane (r(u₁), r(u₂))}. <em>THIS IS NOT
  102.      * TRUE ANYMORE IN THIS CLASS</em>! As derivatives are involved and must be
  103.      * preserved, this constructor works <em>only</em> if the two pairs are fully
  104.      * consistent, i.e. if a rotation exists that fulfill all the requirements: {@code
  105.      * v₁ = r(u₁)}, {@code v₂ = r(u₂)}, {@code dv₁/dt = dr(u₁)/dt}, {@code dv₂/dt
  106.      * = dr(u₂)/dt}, {@code d²v₁/dt² = d²r(u₁)/dt²}, {@code d²v₂/dt² = d²r(u₂)/dt²}.</p>
  107.      * @param u1 first vector of the origin pair
  108.      * @param u2 second vector of the origin pair
  109.      * @param v1 desired image of u1 by the rotation
  110.      * @param v2 desired image of u2 by the rotation
  111.      * @param tolerance relative tolerance factor used to check singularities
  112.      * @exception OrekitException if the vectors are inconsistent for the
  113.      * rotation to be found (null, aligned, ...)
  114.      */
  115.     public AngularCoordinates(final PVCoordinates u1, final PVCoordinates u2,
  116.                               final PVCoordinates v1, final PVCoordinates v2,
  117.                               final double tolerance)
  118.         throws OrekitException {

  119.         try {
  120.             // find the initial fixed rotation
  121.             rotation = new Rotation(u1.getPosition(), u2.getPosition(),
  122.                                     v1.getPosition(), v2.getPosition());

  123.             // find rotation rate Ω such that
  124.             //  Ω ⨯ v₁ = r(dot(u₁)) - dot(v₁)
  125.             //  Ω ⨯ v₂ = r(dot(u₂)) - dot(v₂)
  126.             final Vector3D ru1Dot = rotation.applyTo(u1.getVelocity());
  127.             final Vector3D ru2Dot = rotation.applyTo(u2.getVelocity());
  128.             rotationRate = inverseCrossProducts(v1.getPosition(), ru1Dot.subtract(v1.getVelocity()),
  129.                                                 v2.getPosition(), ru2Dot.subtract(v2.getVelocity()),
  130.                                                 tolerance);

  131.             // find rotation acceleration dot(Ω) such that
  132.             // dot(Ω) ⨯ v₁ = r(dotdot(u₁)) - 2 Ω ⨯ dot(v₁) - Ω ⨯  (Ω ⨯ v₁) - dotdot(v₁)
  133.             // dot(Ω) ⨯ v₂ = r(dotdot(u₂)) - 2 Ω ⨯ dot(v₂) - Ω ⨯  (Ω ⨯ v₂) - dotdot(v₂)
  134.             final Vector3D ru1DotDot = rotation.applyTo(u1.getAcceleration());
  135.             final Vector3D oDotv1    = Vector3D.crossProduct(rotationRate, v1.getVelocity());
  136.             final Vector3D oov1      = Vector3D.crossProduct(rotationRate, Vector3D.crossProduct(rotationRate, v1.getPosition()));
  137.             final Vector3D c1        = new Vector3D(1, ru1DotDot, -2, oDotv1, -1, oov1, -1, v1.getAcceleration());
  138.             final Vector3D ru2DotDot = rotation.applyTo(u2.getAcceleration());
  139.             final Vector3D oDotv2    = Vector3D.crossProduct(rotationRate, v2.getVelocity());
  140.             final Vector3D oov2      = Vector3D.crossProduct(rotationRate, Vector3D.crossProduct(rotationRate, v2.getPosition()));
  141.             final Vector3D c2        = new Vector3D(1, ru2DotDot, -2, oDotv2, -1, oov2, -1, v2.getAcceleration());
  142.             rotationAcceleration     = inverseCrossProducts(v1.getPosition(), c1, v2.getPosition(), c2, tolerance);

  143.         } catch (MathIllegalArgumentException miae) {
  144.             throw new OrekitException(miae);
  145.         } catch (MathArithmeticException mae) {
  146.             throw new OrekitException(mae);
  147.         }

  148.     }

  149.     /** Build one of the rotations that transform one pv coordinates into another one.

  150.      * <p>Except for a possible scale factor, if the instance were
  151.      * applied to the vector u it will produce the vector v. There is an
  152.      * infinite number of such rotations, this constructor choose the
  153.      * one with the smallest associated angle (i.e. the one whose axis
  154.      * is orthogonal to the (u, v) plane). If u and v are collinear, an
  155.      * arbitrary rotation axis is chosen.</p>

  156.      * @param u origin vector
  157.      * @param v desired image of u by the rotation
  158.      * @exception OrekitException if the vectors components cannot be converted to
  159.      * {@link DerivativeStructure} with proper order
  160.      */
  161.     public AngularCoordinates(final PVCoordinates u, final PVCoordinates v) throws OrekitException {
  162.         this(new FieldRotation<DerivativeStructure>(u.toDerivativeStructureVector(2),
  163.                                                     v.toDerivativeStructureVector(2)));
  164.     }

  165.     /** Builds a AngularCoordinates from  a {@link FieldRotation}&lt;{@link DerivativeStructure}&gt;.
  166.      * <p>
  167.      * The rotation components must have time as their only derivation parameter and
  168.      * have consistent derivation orders.
  169.      * </p>
  170.      * @param r rotation with time-derivatives embedded within the coordinates
  171.      */
  172.     public AngularCoordinates(final FieldRotation<DerivativeStructure> r) {

  173.         final double q0       = r.getQ0().getReal();
  174.         final double q1       = r.getQ1().getReal();
  175.         final double q2       = r.getQ2().getReal();
  176.         final double q3       = r.getQ3().getReal();

  177.         rotation     = new Rotation(q0, q1, q2, q3, false);
  178.         if (r.getQ0().getOrder() >= 1) {
  179.             final double q0Dot    = r.getQ0().getPartialDerivative(1);
  180.             final double q1Dot    = r.getQ1().getPartialDerivative(1);
  181.             final double q2Dot    = r.getQ2().getPartialDerivative(1);
  182.             final double q3Dot    = r.getQ3().getPartialDerivative(1);
  183.             rotationRate =
  184.                     new Vector3D(2 * MathArrays.linearCombination(-q1, q0Dot,  q0, q1Dot,  q3, q2Dot, -q2, q3Dot),
  185.                                  2 * MathArrays.linearCombination(-q2, q0Dot, -q3, q1Dot,  q0, q2Dot,  q1, q3Dot),
  186.                                  2 * MathArrays.linearCombination(-q3, q0Dot,  q2, q1Dot, -q1, q2Dot,  q0, q3Dot));
  187.             if (r.getQ0().getOrder() >= 2) {
  188.                 final double q0DotDot = r.getQ0().getPartialDerivative(2);
  189.                 final double q1DotDot = r.getQ1().getPartialDerivative(2);
  190.                 final double q2DotDot = r.getQ2().getPartialDerivative(2);
  191.                 final double q3DotDot = r.getQ3().getPartialDerivative(2);
  192.                 rotationAcceleration =
  193.                         new Vector3D(2 * MathArrays.linearCombination(-q1, q0DotDot,  q0, q1DotDot,  q3, q2DotDot, -q2, q3DotDot),
  194.                                      2 * MathArrays.linearCombination(-q2, q0DotDot, -q3, q1DotDot,  q0, q2DotDot,  q1, q3DotDot),
  195.                                      2 * MathArrays.linearCombination(-q3, q0DotDot,  q2, q1DotDot, -q1, q2DotDot,  q0, q3DotDot));
  196.             } else {
  197.                 rotationAcceleration = Vector3D.ZERO;
  198.             }
  199.         } else {
  200.             rotationRate         = Vector3D.ZERO;
  201.             rotationAcceleration = Vector3D.ZERO;
  202.         }

  203.     }

  204.     /** Find a vector from two known cross products.
  205.      * <p>
  206.      * We want to find Ω such that: Ω ⨯ v₁ = c₁ and Ω ⨯ v₂ = c₂
  207.      * </p>
  208.      * <p>
  209.      * The first equation (Ω ⨯ v₁ = c₁) will always be fulfilled exactly,
  210.      * and the second one will be fulfilled if possible.
  211.      * </p>
  212.      * @param v1 vector forming the first known cross product
  213.      * @param c1 know vector for cross product Ω ⨯ v₁
  214.      * @param v2 vector forming the second known cross product
  215.      * @param c2 know vector for cross product Ω ⨯ v₂
  216.      * @param tolerance relative tolerance factor used to check singularities
  217.      * @return vector Ω such that: Ω ⨯ v₁ = c₁ and Ω ⨯ v₂ = c₂
  218.      * @exception MathIllegalArgumentException if vectors are inconsistent and
  219.      * no solution can be found
  220.      */
  221.     private static Vector3D inverseCrossProducts(final Vector3D v1, final Vector3D c1,
  222.                                                  final Vector3D v2, final Vector3D c2,
  223.                                                  final double tolerance)
  224.         throws MathIllegalArgumentException {

  225.         final double v12 = v1.getNormSq();
  226.         final double v1n = FastMath.sqrt(v12);
  227.         final double v22 = v2.getNormSq();
  228.         final double v2n = FastMath.sqrt(v22);
  229.         final double threshold = tolerance * FastMath.max(v1n, v2n);

  230.         Vector3D omega;

  231.         try {
  232.             // create the over-determined linear system representing the two cross products
  233.             final RealMatrix m = MatrixUtils.createRealMatrix(6, 3);
  234.             m.setEntry(0, 1,  v1.getZ());
  235.             m.setEntry(0, 2, -v1.getY());
  236.             m.setEntry(1, 0, -v1.getZ());
  237.             m.setEntry(1, 2,  v1.getX());
  238.             m.setEntry(2, 0,  v1.getY());
  239.             m.setEntry(2, 1, -v1.getX());
  240.             m.setEntry(3, 1,  v2.getZ());
  241.             m.setEntry(3, 2, -v2.getY());
  242.             m.setEntry(4, 0, -v2.getZ());
  243.             m.setEntry(4, 2,  v2.getX());
  244.             m.setEntry(5, 0,  v2.getY());
  245.             m.setEntry(5, 1, -v2.getX());

  246.             final RealVector rhs = MatrixUtils.createRealVector(new double[] {
  247.                 c1.getX(), c1.getY(), c1.getZ(),
  248.                 c2.getX(), c2.getY(), c2.getZ()
  249.             });

  250.             // find the best solution we can
  251.             final DecompositionSolver solver = new QRDecomposition(m, threshold).getSolver();
  252.             final RealVector v = solver.solve(rhs);
  253.             omega = new Vector3D(v.getEntry(0), v.getEntry(1), v.getEntry(2));

  254.         } catch (SingularMatrixException sme) {

  255.             // handle some special cases for which we can compute a solution
  256.             final double c12 = c1.getNormSq();
  257.             final double c1n = FastMath.sqrt(c12);
  258.             final double c22 = c2.getNormSq();
  259.             final double c2n = FastMath.sqrt(c22);

  260.             if (c1n <= threshold && c2n <= threshold) {
  261.                 // simple special case, velocities are cancelled
  262.                 return Vector3D.ZERO;
  263.             } else if (v1n <= threshold && c1n >= threshold) {
  264.                 // this is inconsistent, if v₁ is zero, c₁ must be 0 too
  265.                 throw new NumberIsTooLargeException(c1n, 0, true);
  266.             } else if (v2n <= threshold && c2n >= threshold) {
  267.                 // this is inconsistent, if v₂ is zero, c₂ must be 0 too
  268.                 throw new NumberIsTooLargeException(c2n, 0, true);
  269.             } else if (Vector3D.crossProduct(v1, v2).getNorm() <= threshold && v12 > threshold) {
  270.                 // simple special case, v₂ is redundant with v₁, we just ignore it
  271.                 // use the simplest Ω: orthogonal to both v₁ and c₁
  272.                 omega = new Vector3D(1.0 / v12, Vector3D.crossProduct(v1, c1));
  273.             } else {
  274.                 throw sme;
  275.             }

  276.         }

  277.         // check results
  278.         final double d1 = Vector3D.distance(Vector3D.crossProduct(omega, v1), c1);
  279.         if (d1 > threshold) {
  280.             throw new NumberIsTooLargeException(d1, 0, true);
  281.         }

  282.         final double d2 = Vector3D.distance(Vector3D.crossProduct(omega, v2), c2);
  283.         if (d2 > threshold) {
  284.             throw new NumberIsTooLargeException(d2, 0, true);
  285.         }

  286.         return omega;

  287.     }

  288.     /** Transform the instance to a {@link FieldRotation}&lt;{@link DerivativeStructure}&gt;.
  289.      * <p>
  290.      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
  291.      * to the user-specified order.
  292.      * </p>
  293.      * @param order derivation order for the vector components
  294.      * @return rotation with time-derivatives embedded within the coordinates
  295.      * @exception OrekitException if the user specified order is too large
  296.      */
  297.     public FieldRotation<DerivativeStructure> toDerivativeStructureRotation(final int order)
  298.         throws OrekitException {

  299.         // quaternion components
  300.         final double q0 = rotation.getQ0();
  301.         final double q1 = rotation.getQ1();
  302.         final double q2 = rotation.getQ2();
  303.         final double q3 = rotation.getQ3();

  304.         // first time-derivatives of the quaternion
  305.         final double oX    = rotationRate.getX();
  306.         final double oY    = rotationRate.getY();
  307.         final double oZ    = rotationRate.getZ();
  308.         final double q0Dot = 0.5 * MathArrays.linearCombination(-q1, oX, -q2, oY, -q3, oZ);
  309.         final double q1Dot = 0.5 * MathArrays.linearCombination( q0, oX, -q3, oY,  q2, oZ);
  310.         final double q2Dot = 0.5 * MathArrays.linearCombination( q3, oX,  q0, oY, -q1, oZ);
  311.         final double q3Dot = 0.5 * MathArrays.linearCombination(-q2, oX,  q1, oY,  q0, oZ);

  312.         // second time-derivatives of the quaternion
  313.         final double oXDot = rotationAcceleration.getX();
  314.         final double oYDot = rotationAcceleration.getY();
  315.         final double oZDot = rotationAcceleration.getZ();
  316.         final double q0DotDot = -0.5 * MathArrays.linearCombination(new double[] {
  317.             q1, q2,  q3, q1Dot, q2Dot,  q3Dot
  318.         }, new double[] {
  319.             oXDot, oYDot, oZDot, oX, oY, oZ
  320.         });
  321.         final double q1DotDot =  0.5 * MathArrays.linearCombination(new double[] {
  322.             q0, q2, -q3, q0Dot, q2Dot, -q3Dot
  323.         }, new double[] {
  324.             oXDot, oZDot, oYDot, oX, oZ, oY
  325.         });
  326.         final double q2DotDot =  0.5 * MathArrays.linearCombination(new double[] {
  327.             q0, q3, -q1, q0Dot, q3Dot, -q1Dot
  328.         }, new double[] {
  329.             oYDot, oXDot, oZDot, oY, oX, oZ
  330.         });
  331.         final double q3DotDot =  0.5 * MathArrays.linearCombination(new double[] {
  332.             q0, q1, -q2, q0Dot, q1Dot, -q2Dot
  333.         }, new double[] {
  334.             oZDot, oYDot, oXDot, oZ, oY, oX
  335.         });

  336.         final DerivativeStructure q0DS;
  337.         final DerivativeStructure q1DS;
  338.         final DerivativeStructure q2DS;
  339.         final DerivativeStructure q3DS;
  340.         switch(order) {
  341.             case 0 :
  342.                 q0DS = new DerivativeStructure(1, 0, q0);
  343.                 q1DS = new DerivativeStructure(1, 0, q1);
  344.                 q2DS = new DerivativeStructure(1, 0, q2);
  345.                 q3DS = new DerivativeStructure(1, 0, q3);
  346.                 break;
  347.             case 1 :
  348.                 q0DS = new DerivativeStructure(1, 1, q0, q0Dot);
  349.                 q1DS = new DerivativeStructure(1, 1, q1, q1Dot);
  350.                 q2DS = new DerivativeStructure(1, 1, q2, q2Dot);
  351.                 q3DS = new DerivativeStructure(1, 1, q3, q3Dot);
  352.                 break;
  353.             case 2 :
  354.                 q0DS = new DerivativeStructure(1, 2, q0, q0Dot, q0DotDot);
  355.                 q1DS = new DerivativeStructure(1, 2, q1, q1Dot, q1DotDot);
  356.                 q2DS = new DerivativeStructure(1, 2, q2, q2Dot, q2DotDot);
  357.                 q3DS = new DerivativeStructure(1, 2, q3, q3Dot, q3DotDot);
  358.                 break;
  359.             default :
  360.                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
  361.         }

  362.         return new FieldRotation<DerivativeStructure>(q0DS, q1DS, q2DS, q3DS, false);

  363.     }

  364.     /** Estimate rotation rate between two orientations.
  365.      * <p>Estimation is based on a simple fixed rate rotation
  366.      * during the time interval between the two orientations.</p>
  367.      * @param start start orientation
  368.      * @param end end orientation
  369.      * @param dt time elapsed between the dates of the two orientations
  370.      * @return rotation rate allowing to go from start to end orientations
  371.      */
  372.     public static Vector3D estimateRate(final Rotation start, final Rotation end, final double dt) {
  373.         final Rotation evolution = start.compose(end.revert(), RotationConvention.VECTOR_OPERATOR);
  374.         return new Vector3D(evolution.getAngle() / dt, evolution.getAxis(RotationConvention.VECTOR_OPERATOR));
  375.     }

  376.     /** Revert a rotation/rotation rate/ rotation acceleration triplet.
  377.      * Build a triplet which reverse the effect of another triplet.
  378.      * @return a new triplet whose effect is the reverse of the effect
  379.      * of the instance
  380.      */
  381.     public AngularCoordinates revert() {
  382.         return new AngularCoordinates(rotation.revert(),
  383.                                       rotation.applyInverseTo(rotationRate).negate(),
  384.                                       rotation.applyInverseTo(rotationAcceleration).negate());
  385.     }

  386.     /** Get a time-shifted state.
  387.      * <p>
  388.      * The state can be slightly shifted to close dates. This shift is based on
  389.      * an approximate solution of the fixed acceleration motion. It is <em>not</em>
  390.      * intended as a replacement for proper attitude propagation but should be
  391.      * sufficient for either small time shifts or coarse accuracy.
  392.      * </p>
  393.      * @param dt time shift in seconds
  394.      * @return a new state, shifted with respect to the instance (which is immutable)
  395.      */
  396.     public AngularCoordinates shiftedBy(final double dt) {

  397.         // the shiftedBy method is based on a local approximation.
  398.         // It considers separately the contribution of the constant
  399.         // rotation, the linear contribution or the rate and the
  400.         // quadratic contribution of the acceleration. The rate
  401.         // and acceleration contributions are small rotations as long
  402.         // as the time shift is small, which is the crux of the algorithm.
  403.         // Small rotations are almost commutative, so we append these small
  404.         // contributions one after the other, as if they really occurred
  405.         // successively, despite this is not what really happens.

  406.         // compute the linear contribution first, ignoring acceleration
  407.         // BEWARE: there is really a minus sign here, because if
  408.         // the target frame rotates in one direction, the vectors in the origin
  409.         // frame seem to rotate in the opposite direction
  410.         final double rate = rotationRate.getNorm();
  411.         final Rotation rateContribution = (rate == 0.0) ?
  412.                                           Rotation.IDENTITY :
  413.                                           new Rotation(rotationRate, rate * dt, RotationConvention.FRAME_TRANSFORM);

  414.         // append rotation and rate contribution
  415.         final AngularCoordinates linearPart =
  416.                 new AngularCoordinates(rateContribution.compose(rotation, RotationConvention.VECTOR_OPERATOR), rotationRate);

  417.         final double acc  = rotationAcceleration.getNorm();
  418.         if (acc == 0.0) {
  419.             // no acceleration, the linear part is sufficient
  420.             return linearPart;
  421.         }

  422.         // compute the quadratic contribution, ignoring initial rotation and rotation rate
  423.         // BEWARE: there is really a minus sign here, because if
  424.         // the target frame rotates in one direction, the vectors in the origin
  425.         // frame seem to rotate in the opposite direction
  426.         final AngularCoordinates quadraticContribution =
  427.                 new AngularCoordinates(new Rotation(rotationAcceleration,
  428.                                                     0.5 * acc * dt * dt,
  429.                                                     RotationConvention.FRAME_TRANSFORM),
  430.                                        new Vector3D(dt, rotationAcceleration),
  431.                                        rotationAcceleration);

  432.         // the quadratic contribution is a small rotation:
  433.         // its initial angle and angular rate are both zero.
  434.         // small rotations are almost commutative, so we append the small
  435.         // quadratic part after the linear part as a simple offset
  436.         return quadraticContribution.addOffset(linearPart);

  437.     }

  438.     /** Get the rotation.
  439.      * @return the rotation.
  440.      */
  441.     public Rotation getRotation() {
  442.         return rotation;
  443.     }

  444.     /** Get the rotation rate.
  445.      * @return the rotation rate vector Ω (rad/s).
  446.      */
  447.     public Vector3D getRotationRate() {
  448.         return rotationRate;
  449.     }

  450.     /** Get the rotation acceleration.
  451.      * @return the rotation acceleration vector dΩ/dt (rad²/s²).
  452.      */
  453.     public Vector3D getRotationAcceleration() {
  454.         return rotationAcceleration;
  455.     }

  456.     /** Add an offset from the instance.
  457.      * <p>
  458.      * We consider here that the offset rotation is applied first and the
  459.      * instance is applied afterward. Note that angular coordinates do <em>not</em>
  460.      * commute under this operation, i.e. {@code a.addOffset(b)} and {@code
  461.      * b.addOffset(a)} lead to <em>different</em> results in most cases.
  462.      * </p>
  463.      * <p>
  464.      * The two methods {@link #addOffset(AngularCoordinates) addOffset} and
  465.      * {@link #subtractOffset(AngularCoordinates) subtractOffset} are designed
  466.      * so that round trip applications are possible. This means that both {@code
  467.      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
  468.      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
  469.      * </p>
  470.      * @param offset offset to subtract
  471.      * @return new instance, with offset subtracted
  472.      * @see #subtractOffset(AngularCoordinates)
  473.      */
  474.     public AngularCoordinates addOffset(final AngularCoordinates offset) {
  475.         final Vector3D rOmega    = rotation.applyTo(offset.rotationRate);
  476.         final Vector3D rOmegaDot = rotation.applyTo(offset.rotationAcceleration);
  477.         return new AngularCoordinates(rotation.compose(offset.rotation, RotationConvention.VECTOR_OPERATOR),
  478.                                       rotationRate.add(rOmega),
  479.                                       new Vector3D( 1.0, rotationAcceleration,
  480.                                                     1.0, rOmegaDot,
  481.                                                    -1.0, Vector3D.crossProduct(rotationRate, rOmega)));
  482.     }

  483.     /** Subtract an offset from the instance.
  484.      * <p>
  485.      * We consider here that the offset rotation is applied first and the
  486.      * instance is applied afterward. Note that angular coordinates do <em>not</em>
  487.      * commute under this operation, i.e. {@code a.subtractOffset(b)} and {@code
  488.      * b.subtractOffset(a)} lead to <em>different</em> results in most cases.
  489.      * </p>
  490.      * <p>
  491.      * The two methods {@link #addOffset(AngularCoordinates) addOffset} and
  492.      * {@link #subtractOffset(AngularCoordinates) subtractOffset} are designed
  493.      * so that round trip applications are possible. This means that both {@code
  494.      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
  495.      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
  496.      * </p>
  497.      * @param offset offset to subtract
  498.      * @return new instance, with offset subtracted
  499.      * @see #addOffset(AngularCoordinates)
  500.      */
  501.     public AngularCoordinates subtractOffset(final AngularCoordinates offset) {
  502.         return addOffset(offset.revert());
  503.     }

  504.     /** Apply the rotation to a pv coordinates.
  505.      * @param pv vector to apply the rotation to
  506.      * @return a new pv coordinates which is the image of u by the rotation
  507.      */
  508.     public PVCoordinates applyTo(final PVCoordinates pv) {

  509.         final Vector3D transformedP = rotation.applyTo(pv.getPosition());
  510.         final Vector3D crossP       = Vector3D.crossProduct(rotationRate, transformedP);
  511.         final Vector3D transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  512.         final Vector3D crossV       = Vector3D.crossProduct(rotationRate, transformedV);
  513.         final Vector3D crossCrossP  = Vector3D.crossProduct(rotationRate, crossP);
  514.         final Vector3D crossDotP    = Vector3D.crossProduct(rotationAcceleration, transformedP);
  515.         final Vector3D transformedA = new Vector3D( 1, rotation.applyTo(pv.getAcceleration()),
  516.                                                    -2, crossV,
  517.                                                    -1, crossCrossP,
  518.                                                    -1, crossDotP);

  519.         return new PVCoordinates(transformedP, transformedV, transformedA);

  520.     }

  521.     /** Apply the rotation to a pv coordinates.
  522.      * @param pv vector to apply the rotation to
  523.      * @return a new pv coordinates which is the image of u by the rotation
  524.      */
  525.     public TimeStampedPVCoordinates applyTo(final TimeStampedPVCoordinates pv) {

  526.         final Vector3D transformedP = getRotation().applyTo(pv.getPosition());
  527.         final Vector3D crossP       = Vector3D.crossProduct(getRotationRate(), transformedP);
  528.         final Vector3D transformedV = getRotation().applyTo(pv.getVelocity()).subtract(crossP);
  529.         final Vector3D crossV       = Vector3D.crossProduct(getRotationRate(), transformedV);
  530.         final Vector3D crossCrossP  = Vector3D.crossProduct(getRotationRate(), crossP);
  531.         final Vector3D crossDotP    = Vector3D.crossProduct(getRotationAcceleration(), transformedP);
  532.         final Vector3D transformedA = new Vector3D( 1, getRotation().applyTo(pv.getAcceleration()),
  533.                                                    -2, crossV,
  534.                                                    -1, crossCrossP,
  535.                                                    -1, crossDotP);

  536.         return new TimeStampedPVCoordinates(pv.getDate(), transformedP, transformedV, transformedA);

  537.     }

  538.     /** Interpolate angular coordinates.
  539.      * <p>
  540.      * The interpolated instance is created by polynomial Hermite interpolation
  541.      * on Rodrigues vector ensuring rotation rate remains the exact derivative of rotation.
  542.      * </p>
  543.      * <p>
  544.      * This method is based on Sergei Tanygin's paper <a
  545.      * href="http://www.agi.com/downloads/resources/white-papers/Attitude-interpolation.pdf">Attitude
  546.      * Interpolation</a>, changing the norm of the vector to match the modified Rodrigues
  547.      * vector as described in Malcolm D. Shuster's paper <a
  548.      * href="http://www.ladispe.polito.it/corsi/Meccatronica/02JHCOR/2011-12/Slides/Shuster_Pub_1993h_J_Repsurv_scan.pdf">A
  549.      * Survey of Attitude Representations</a>. This change avoids the singularity at π.
  550.      * There is still a singularity at 2π, which is handled by slightly offsetting all rotations
  551.      * when this singularity is detected.
  552.      * </p>
  553.      * <p>
  554.      * Note that even if first time derivatives (rotation rates)
  555.      * from sample can be ignored, the interpolated instance always includes
  556.      * interpolated derivatives. This feature can be used explicitly to
  557.      * compute these derivatives when it would be too complex to compute them
  558.      * from an analytical formula: just compute a few sample points from the
  559.      * explicit formula and set the derivatives to zero in these sample points,
  560.      * then use interpolation to add derivatives consistent with the rotations.
  561.      * </p>
  562.      * @param date interpolation date
  563.      * @param useRotationRates if true, use sample points rotation rates,
  564.      * otherwise ignore them and use only rotations
  565.      * @param sample sample points on which interpolation should be done
  566.      * @return a new position-velocity, interpolated at specified date
  567.      * @exception OrekitException if the number of point is too small for interpolating
  568.      * @deprecated since 7.0 replaced with {@link TimeStampedAngularCoordinates#interpolate(AbsoluteDate, AngularDerivativesFilter, Collection)}
  569.      */
  570.     @Deprecated
  571.     public static AngularCoordinates interpolate(final AbsoluteDate date, final boolean useRotationRates,
  572.                                                  final Collection<Pair<AbsoluteDate, AngularCoordinates>> sample)
  573.         throws OrekitException {
  574.         final List<TimeStampedAngularCoordinates> list = new ArrayList<TimeStampedAngularCoordinates>(sample.size());
  575.         for (final Pair<AbsoluteDate, AngularCoordinates> pair : sample) {
  576.             list.add(new TimeStampedAngularCoordinates(pair.getFirst(),
  577.                                                        pair.getSecond().getRotation(),
  578.                                                        pair.getSecond().getRotationRate(),
  579.                                                        pair.getSecond().getRotationAcceleration()));
  580.         }
  581.         return TimeStampedAngularCoordinates.interpolate(date,
  582.                                                          useRotationRates ? AngularDerivativesFilter.USE_RR : AngularDerivativesFilter.USE_R,
  583.                                                          list);
  584.     }

  585.     /** Convert rotation, rate and acceleration to modified Rodrigues vector and derivatives.
  586.      * <p>
  587.      * The modified Rodrigues vector is tan(θ/4) u where θ and u are the
  588.      * rotation angle and axis respectively.
  589.      * </p>
  590.      * @param sign multiplicative sign for quaternion components
  591.      * @return modified Rodrigues vector and derivatives (vector on row 0, first derivative
  592.      * on row 1, second derivative on row 2)
  593.      * @see #createFromModifiedRodrigues(double[][])
  594.      */
  595.     public double[][] getModifiedRodrigues(final double sign) {

  596.         final double q0    = sign * getRotation().getQ0();
  597.         final double q1    = sign * getRotation().getQ1();
  598.         final double q2    = sign * getRotation().getQ2();
  599.         final double q3    = sign * getRotation().getQ3();
  600.         final double oX    = getRotationRate().getX();
  601.         final double oY    = getRotationRate().getY();
  602.         final double oZ    = getRotationRate().getZ();
  603.         final double oXDot = getRotationAcceleration().getX();
  604.         final double oYDot = getRotationAcceleration().getY();
  605.         final double oZDot = getRotationAcceleration().getZ();

  606.         // first time-derivatives of the quaternion
  607.         final double q0Dot = 0.5 * MathArrays.linearCombination(-q1, oX, -q2, oY, -q3, oZ);
  608.         final double q1Dot = 0.5 * MathArrays.linearCombination( q0, oX, -q3, oY,  q2, oZ);
  609.         final double q2Dot = 0.5 * MathArrays.linearCombination( q3, oX,  q0, oY, -q1, oZ);
  610.         final double q3Dot = 0.5 * MathArrays.linearCombination(-q2, oX,  q1, oY,  q0, oZ);

  611.         // second time-derivatives of the quaternion
  612.         final double q0DotDot = -0.5 * MathArrays.linearCombination(new double[] {
  613.             q1, q2,  q3, q1Dot, q2Dot,  q3Dot
  614.         }, new double[] {
  615.             oXDot, oYDot, oZDot, oX, oY, oZ
  616.         });
  617.         final double q1DotDot =  0.5 * MathArrays.linearCombination(new double[] {
  618.             q0, q2, -q3, q0Dot, q2Dot, -q3Dot
  619.         }, new double[] {
  620.             oXDot, oZDot, oYDot, oX, oZ, oY
  621.         });
  622.         final double q2DotDot =  0.5 * MathArrays.linearCombination(new double[] {
  623.             q0, q3, -q1, q0Dot, q3Dot, -q1Dot
  624.         }, new double[] {
  625.             oYDot, oXDot, oZDot, oY, oX, oZ
  626.         });
  627.         final double q3DotDot =  0.5 * MathArrays.linearCombination(new double[] {
  628.             q0, q1, -q2, q0Dot, q1Dot, -q2Dot
  629.         }, new double[] {
  630.             oZDot, oYDot, oXDot, oZ, oY, oX
  631.         });

  632.         // the modified Rodrigues is tan(θ/4) u where θ and u are the rotation angle and axis respectively
  633.         // this can be rewritten using quaternion components:
  634.         //      r (q₁ / (1+q₀), q₂ / (1+q₀), q₃ / (1+q₀))
  635.         // applying the derivation chain rule to previous expression gives rDot and rDotDot
  636.         final double inv          = 1.0 / (1.0 + q0);
  637.         final double mTwoInvQ0Dot = -2 * inv * q0Dot;

  638.         final double r1       = inv * q1;
  639.         final double r2       = inv * q2;
  640.         final double r3       = inv * q3;

  641.         final double mInvR1   = -inv * r1;
  642.         final double mInvR2   = -inv * r2;
  643.         final double mInvR3   = -inv * r3;

  644.         final double r1Dot    = MathArrays.linearCombination(inv, q1Dot, mInvR1, q0Dot);
  645.         final double r2Dot    = MathArrays.linearCombination(inv, q2Dot, mInvR2, q0Dot);
  646.         final double r3Dot    = MathArrays.linearCombination(inv, q3Dot, mInvR3, q0Dot);

  647.         final double r1DotDot = MathArrays.linearCombination(inv, q1DotDot, mTwoInvQ0Dot, r1Dot, mInvR1, q0DotDot);
  648.         final double r2DotDot = MathArrays.linearCombination(inv, q2DotDot, mTwoInvQ0Dot, r2Dot, mInvR2, q0DotDot);
  649.         final double r3DotDot = MathArrays.linearCombination(inv, q3DotDot, mTwoInvQ0Dot, r3Dot, mInvR3, q0DotDot);

  650.         return new double[][] {
  651.             {
  652.                 r1,       r2,       r3
  653.             }, {
  654.                 r1Dot,    r2Dot,    r3Dot
  655.             }, {
  656.                 r1DotDot, r2DotDot, r3DotDot
  657.             }
  658.         };

  659.     }

  660.     /** Convert a modified Rodrigues vector and derivatives to angular coordinates.
  661.      * @param r modified Rodrigues vector (with first and second times derivatives)
  662.      * @return angular coordinates
  663.      * @see #getModifiedRodrigues(double)
  664.      */
  665.     public static AngularCoordinates createFromModifiedRodrigues(final double[][] r) {

  666.         // rotation
  667.         final double rSquared = r[0][0] * r[0][0] + r[0][1] * r[0][1] + r[0][2] * r[0][2];
  668.         final double oPQ0     = 2 / (1 + rSquared);
  669.         final double q0       = oPQ0 - 1;
  670.         final double q1       = oPQ0 * r[0][0];
  671.         final double q2       = oPQ0 * r[0][1];
  672.         final double q3       = oPQ0 * r[0][2];

  673.         // rotation rate
  674.         final double oPQ02    = oPQ0 * oPQ0;
  675.         final double q0Dot    = -oPQ02 * MathArrays.linearCombination(r[0][0], r[1][0], r[0][1], r[1][1],  r[0][2], r[1][2]);
  676.         final double q1Dot    = oPQ0 * r[1][0] + r[0][0] * q0Dot;
  677.         final double q2Dot    = oPQ0 * r[1][1] + r[0][1] * q0Dot;
  678.         final double q3Dot    = oPQ0 * r[1][2] + r[0][2] * q0Dot;
  679.         final double oX       = 2 * MathArrays.linearCombination(-q1, q0Dot,  q0, q1Dot,  q3, q2Dot, -q2, q3Dot);
  680.         final double oY       = 2 * MathArrays.linearCombination(-q2, q0Dot, -q3, q1Dot,  q0, q2Dot,  q1, q3Dot);
  681.         final double oZ       = 2 * MathArrays.linearCombination(-q3, q0Dot,  q2, q1Dot, -q1, q2Dot,  q0, q3Dot);

  682.         // rotation acceleration
  683.         final double q0DotDot = (1 - q0) / oPQ0 * q0Dot * q0Dot -
  684.                                 oPQ02 * MathArrays.linearCombination(r[0][0], r[2][0], r[0][1], r[2][1], r[0][2], r[2][2]) -
  685.                                 (q1Dot * q1Dot + q2Dot * q2Dot + q3Dot * q3Dot);
  686.         final double q1DotDot = MathArrays.linearCombination(oPQ0, r[2][0], 2 * r[1][0], q0Dot, r[0][0], q0DotDot);
  687.         final double q2DotDot = MathArrays.linearCombination(oPQ0, r[2][1], 2 * r[1][1], q0Dot, r[0][1], q0DotDot);
  688.         final double q3DotDot = MathArrays.linearCombination(oPQ0, r[2][2], 2 * r[1][2], q0Dot, r[0][2], q0DotDot);
  689.         final double oXDot    = 2 * MathArrays.linearCombination(-q1, q0DotDot,  q0, q1DotDot,  q3, q2DotDot, -q2, q3DotDot);
  690.         final double oYDot    = 2 * MathArrays.linearCombination(-q2, q0DotDot, -q3, q1DotDot,  q0, q2DotDot,  q1, q3DotDot);
  691.         final double oZDot    = 2 * MathArrays.linearCombination(-q3, q0DotDot,  q2, q1DotDot, -q1, q2DotDot,  q0, q3DotDot);

  692.         return new AngularCoordinates(new Rotation(q0, q1, q2, q3, false),
  693.                                       new Vector3D(oX, oY, oZ),
  694.                                       new Vector3D(oXDot, oYDot, oZDot));

  695.     }

  696. }