FieldAngularCoordinates.java

  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. import org.hipparchus.Field;
  19. import org.hipparchus.RealFieldElement;
  20. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  21. import org.hipparchus.analysis.differentiation.FDSFactory;
  22. import org.hipparchus.analysis.differentiation.FieldDerivativeStructure;
  23. import org.hipparchus.exception.LocalizedCoreFormats;
  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.geometry.euclidean.threed.FieldRotation;
  26. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  27. import org.hipparchus.geometry.euclidean.threed.RotationConvention;
  28. import org.hipparchus.linear.FieldDecompositionSolver;
  29. import org.hipparchus.linear.FieldMatrix;
  30. import org.hipparchus.linear.FieldQRDecomposition;
  31. import org.hipparchus.linear.FieldVector;
  32. import org.hipparchus.linear.MatrixUtils;
  33. import org.hipparchus.util.MathArrays;
  34. import org.orekit.errors.OrekitException;
  35. import org.orekit.errors.OrekitMessages;

  36. /** Simple container for rotation / rotation rate pairs, using {@link
  37.  * RealFieldElement}.
  38.  * <p>
  39.  * The state can be slightly shifted to close dates. This shift is based on
  40.  * a simple quadratic model. It is <em>not</em> intended as a replacement for
  41.  * proper attitude propagation but should be sufficient for either small
  42.  * time shifts or coarse accuracy.
  43.  * </p>
  44.  * <p>
  45.  * This class is the angular counterpart to {@link FieldPVCoordinates}.
  46.  * </p>
  47.  * <p>Instances of this class are guaranteed to be immutable.</p>
  48.  * @param <T> the type of the field elements
  49.  * @author Luc Maisonobe
  50.  * @since 6.0
  51.  * @see AngularCoordinates
  52.  */
  53. public class FieldAngularCoordinates<T extends RealFieldElement<T>> {


  54.     /** rotation. */
  55.     private final FieldRotation<T> rotation;

  56.     /** rotation rate. */
  57.     private final FieldVector3D<T> rotationRate;

  58.     /** rotation acceleration. */
  59.     private final FieldVector3D<T> rotationAcceleration;

  60.     /** Builds a rotation/rotation rate pair.
  61.      * @param rotation rotation
  62.      * @param rotationRate rotation rate Ω (rad/s)
  63.      */
  64.     public FieldAngularCoordinates(final FieldRotation<T> rotation,
  65.                                    final FieldVector3D<T> rotationRate) {
  66.         this(rotation, rotationRate,
  67.              new FieldVector3D<>(rotation.getQ0().getField().getZero(),
  68.                                  rotation.getQ0().getField().getZero(),
  69.                                  rotation.getQ0().getField().getZero()));
  70.     }

  71.     /** Builds a rotation / rotation rate / rotation acceleration triplet.
  72.      * @param rotation i.e. the orientation of the vehicle
  73.      * @param rotationRate rotation rate rate Ω, i.e. the spin vector (rad/s)
  74.      * @param rotationAcceleration angular acceleration vector dΩ/dt (rad²/s²)
  75.      */
  76.     public FieldAngularCoordinates(final FieldRotation<T> rotation,
  77.                                    final FieldVector3D<T> rotationRate,
  78.                                    final FieldVector3D<T> rotationAcceleration) {
  79.         this.rotation             = rotation;
  80.         this.rotationRate         = rotationRate;
  81.         this.rotationAcceleration = rotationAcceleration;
  82.     }

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

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

  108.         try {
  109.             // find the initial fixed rotation
  110.             rotation = new FieldRotation<>(u1.getPosition(), u2.getPosition(),
  111.                                            v1.getPosition(), v2.getPosition());

  112.             // find rotation rate Ω such that
  113.             //  Ω ⨯ v₁ = r(dot(u₁)) - dot(v₁)
  114.             //  Ω ⨯ v₂ = r(dot(u₂)) - dot(v₂)
  115.             final FieldVector3D<T> ru1Dot = rotation.applyTo(u1.getVelocity());
  116.             final FieldVector3D<T> ru2Dot = rotation.applyTo(u2.getVelocity());


  117.             rotationRate = inverseCrossProducts(v1.getPosition(), ru1Dot.subtract(v1.getVelocity()),
  118.                                                 v2.getPosition(), ru2Dot.subtract(v2.getVelocity()),
  119.                                                 tolerance);


  120.             // find rotation acceleration dot(Ω) such that
  121.             // dot(Ω) ⨯ v₁ = r(dotdot(u₁)) - 2 Ω ⨯ dot(v₁) - Ω ⨯  (Ω ⨯ v₁) - dotdot(v₁)
  122.             // dot(Ω) ⨯ v₂ = r(dotdot(u₂)) - 2 Ω ⨯ dot(v₂) - Ω ⨯  (Ω ⨯ v₂) - dotdot(v₂)
  123.             final FieldVector3D<T> ru1DotDot = rotation.applyTo(u1.getAcceleration());
  124.             final FieldVector3D<T> oDotv1    = FieldVector3D.crossProduct(rotationRate, v1.getVelocity());
  125.             final FieldVector3D<T> oov1      = FieldVector3D.crossProduct(rotationRate, rotationRate.crossProduct(v1.getPosition()));
  126.             final FieldVector3D<T> c1        = new FieldVector3D<>(1, ru1DotDot, -2, oDotv1, -1, oov1, -1, v1.getAcceleration());
  127.             final FieldVector3D<T> ru2DotDot = rotation.applyTo(u2.getAcceleration());
  128.             final FieldVector3D<T> oDotv2    = FieldVector3D.crossProduct(rotationRate, v2.getVelocity());
  129.             final FieldVector3D<T> oov2      = FieldVector3D.crossProduct(rotationRate, rotationRate.crossProduct( v2.getPosition()));
  130.             final FieldVector3D<T> c2        = new FieldVector3D<>(1, ru2DotDot, -2, oDotv2, -1, oov2, -1, v2.getAcceleration());
  131.             rotationAcceleration     = inverseCrossProducts(v1.getPosition(), c1, v2.getPosition(), c2, tolerance);

  132.         } catch (MathIllegalArgumentException miae) {
  133.             throw new OrekitException(miae);
  134.         }

  135.     }

  136.     /** Builds a FieldAngularCoordinates from a field and a regular AngularCoordinates.
  137.      * @param field field for the components
  138.      * @param ang AngularCoordinates to convert
  139.      */
  140.     public FieldAngularCoordinates(final Field<T> field, final AngularCoordinates ang) {
  141.         this.rotation             = new FieldRotation<>(field, ang.getRotation());
  142.         this.rotationRate         = new FieldVector3D<>(field, ang.getRotationRate());
  143.         this.rotationAcceleration = new FieldVector3D<>(field, ang.getRotationAcceleration());
  144.     }

  145.     /** Builds a FieldAngularCoordinates from  a {@link FieldRotation}&lt;{@link FieldDerivativeStructure}&gt;.
  146.      * <p>
  147.      * The rotation components must have time as their only derivation parameter and
  148.      * have consistent derivation orders.
  149.      * </p>
  150.      * @param r rotation with time-derivatives embedded within the coordinates
  151.      * @since 9.2
  152.      */
  153.     public FieldAngularCoordinates(final FieldRotation<FieldDerivativeStructure<T>> r) {

  154.         final T q0       = r.getQ0().getValue();
  155.         final T q1       = r.getQ1().getValue();
  156.         final T q2       = r.getQ2().getValue();
  157.         final T q3       = r.getQ3().getValue();

  158.         rotation     = new FieldRotation<>(q0, q1, q2, q3, false);
  159.         if (r.getQ0().getOrder() >= 1) {
  160.             final T q0Dot    = r.getQ0().getPartialDerivative(1);
  161.             final T q1Dot    = r.getQ1().getPartialDerivative(1);
  162.             final T q2Dot    = r.getQ2().getPartialDerivative(1);
  163.             final T q3Dot    = r.getQ3().getPartialDerivative(1);
  164.             rotationRate =
  165.                     new FieldVector3D<>(q0.linearCombination(q1.negate(), q0Dot, q0,          q1Dot,
  166.                                                              q3,          q2Dot, q2.negate(), q3Dot).multiply(2),
  167.                                         q0.linearCombination(q2.negate(), q0Dot, q3.negate(), q1Dot,
  168.                                                              q0,          q2Dot, q1,          q3Dot).multiply(2),
  169.                                         q0.linearCombination(q3.negate(), q0Dot, q2,          q1Dot,
  170.                                                              q1.negate(), q2Dot, q0,          q3Dot).multiply(2));
  171.             if (r.getQ0().getOrder() >= 2) {
  172.                 final T q0DotDot = r.getQ0().getPartialDerivative(2);
  173.                 final T q1DotDot = r.getQ1().getPartialDerivative(2);
  174.                 final T q2DotDot = r.getQ2().getPartialDerivative(2);
  175.                 final T q3DotDot = r.getQ3().getPartialDerivative(2);
  176.                 rotationAcceleration =
  177.                         new FieldVector3D<>(q0.linearCombination(q1.negate(), q0DotDot, q0,          q1DotDot,
  178.                                                                  q3,          q2DotDot, q2.negate(), q3DotDot).multiply(2),
  179.                                             q0.linearCombination(q2.negate(), q0DotDot, q3.negate(), q1DotDot,
  180.                                                                  q0,          q2DotDot, q1,          q3DotDot).multiply(2),
  181.                                             q0.linearCombination(q3.negate(), q0DotDot, q2,          q1DotDot,
  182.                                                                  q1.negate(), q2DotDot, q0,          q3DotDot).multiply(2));
  183.             } else {
  184.                 rotationAcceleration = FieldVector3D.getZero(q0.getField());
  185.             }
  186.         } else {
  187.             rotationRate         = FieldVector3D.getZero(q0.getField());
  188.             rotationAcceleration = FieldVector3D.getZero(q0.getField());
  189.         }

  190.     }

  191.     /** Fixed orientation parallel with reference frame
  192.      * (identity rotation, zero rotation rate and acceleration).
  193.      * @param field field for the components
  194.      * @param <T> the type of the field elements
  195.      * @return a new fixed orientation parallel with reference frame
  196.      */
  197.     public static <T extends RealFieldElement<T>> FieldAngularCoordinates<T> getIdentity(final Field<T> field) {
  198.         return new FieldAngularCoordinates<>(field, AngularCoordinates.IDENTITY);
  199.     }

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

  222.         final T v12 = v1.getNormSq();
  223.         final T v1n = v12.sqrt();
  224.         final T v22 = v2.getNormSq();
  225.         final T v2n = v22.sqrt();
  226.         final T threshold;
  227.         if (v1n.getReal() >= v2n.getReal()) {
  228.             threshold = v1n.multiply(tolerance);
  229.         }
  230.         else {
  231.             threshold = v2n.multiply(tolerance);
  232.         }
  233.         FieldVector3D<T> omega = null;

  234.         try {
  235.             // create the over-determined linear system representing the two cross products
  236.             final FieldMatrix<T> m = MatrixUtils.createFieldMatrix(v12.getField(), 6, 3);
  237.             m.setEntry(0, 1, v1.getZ());
  238.             m.setEntry(0, 2, v1.getY().negate());
  239.             m.setEntry(1, 0, v1.getZ().negate());
  240.             m.setEntry(1, 2, v1.getX());
  241.             m.setEntry(2, 0, v1.getY());
  242.             m.setEntry(2, 1, v1.getX().negate());
  243.             m.setEntry(3, 1, v2.getZ());
  244.             m.setEntry(3, 2, v2.getY().negate());
  245.             m.setEntry(4, 0, v2.getZ().negate());
  246.             m.setEntry(4, 2, v2.getX());
  247.             m.setEntry(5, 0, v2.getY());
  248.             m.setEntry(5, 1, v2.getX().negate());

  249.             final T[] kk = MathArrays.buildArray(v2n.getField(), 6);
  250.             kk[0] = c1.getX();
  251.             kk[1] = c1.getY();
  252.             kk[2] = c1.getZ();
  253.             kk[3] = c2.getX();
  254.             kk[4] = c2.getY();
  255.             kk[5] = c2.getZ();
  256.             final FieldVector<T> rhs = MatrixUtils.createFieldVector(kk);

  257.             // find the best solution we can
  258.             final FieldDecompositionSolver<T> solver = new FieldQRDecomposition<>(m).getSolver();
  259.             final FieldVector<T> v = solver.solve(rhs);
  260.             omega = new FieldVector3D<>(v.getEntry(0), v.getEntry(1), v.getEntry(2));

  261.         } catch (MathIllegalArgumentException miae) {
  262.             if (miae.getSpecifier() == LocalizedCoreFormats.SINGULAR_MATRIX) {

  263.                 // handle some special cases for which we can compute a solution
  264.                 final T c12 = c1.getNormSq();
  265.                 final T c1n = c12.sqrt();
  266.                 final T c22 = c2.getNormSq();
  267.                 final T c2n = c22.sqrt();
  268.                 if (c1n.getReal() <= threshold.getReal() && c2n.getReal() <= threshold.getReal()) {
  269.                     // simple special case, velocities are cancelled
  270.                     return new FieldVector3D<>(v12.getField().getZero(), v12.getField().getZero(), v12.getField().getZero());
  271.                 } else if (v1n.getReal() <= threshold.getReal() && c1n.getReal() >= threshold.getReal()) {
  272.                     // this is inconsistent, if v₁ is zero, c₁ must be 0 too
  273.                     throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE,
  274.                                                            c1n.getReal(), 0, true);
  275.                 } else if (v2n.getReal() <= threshold.getReal() && c2n.getReal() >= threshold.getReal()) {
  276.                     // this is inconsistent, if v₂ is zero, c₂ must be 0 too
  277.                     throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE,
  278.                                                            c2n.getReal(), 0, true);
  279.                 } else if (v1.crossProduct(v1).getNorm().getReal() <= threshold.getReal() && v12.getReal() > threshold.getReal()) {
  280.                     // simple special case, v₂ is redundant with v₁, we just ignore it
  281.                     // use the simplest Ω: orthogonal to both v₁ and c₁
  282.                     omega = new FieldVector3D<>(v12.reciprocal(), v1.crossProduct(c1));
  283.                 }
  284.             } else {
  285.                 throw miae;
  286.             }
  287.         }
  288.         // check results
  289.         final T d1 = FieldVector3D.distance(omega.crossProduct(v1), c1);
  290.         if (d1.getReal() > threshold.getReal()) {
  291.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, 0, true);
  292.         }

  293.         final T d2 = FieldVector3D.distance(omega.crossProduct(v2), c2);
  294.         if (d2.getReal() > threshold.getReal()) {
  295.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, 0, true);
  296.         }

  297.         return omega;

  298.     }

  299.     /** Transform the instance to a {@link FieldRotation}&lt;{@link FieldDerivativeStructure}&gt;.
  300.      * <p>
  301.      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
  302.      * to the user-specified order.
  303.      * </p>
  304.      * @param order derivation order for the vector components
  305.      * @return rotation with time-derivatives embedded within the coordinates
  306.      * @exception OrekitException if the user specified order is too large
  307.      * @since 9.2
  308.      */
  309.     public FieldRotation<FieldDerivativeStructure<T>> toDerivativeStructureRotation(final int order)
  310.         throws OrekitException {

  311.         // quaternion components
  312.         final T q0 = rotation.getQ0();
  313.         final T q1 = rotation.getQ1();
  314.         final T q2 = rotation.getQ2();
  315.         final T q3 = rotation.getQ3();

  316.         // first time-derivatives of the quaternion
  317.         final T oX    = rotationRate.getX();
  318.         final T oY    = rotationRate.getY();
  319.         final T oZ    = rotationRate.getZ();
  320.         final T q0Dot = q0.linearCombination(q1.negate(), oX, q2.negate(), oY, q3.negate(), oZ).multiply(0.5);
  321.         final T q1Dot = q0.linearCombination(q0,          oX, q3.negate(), oY, q2,          oZ).multiply(0.5);
  322.         final T q2Dot = q0.linearCombination(q3,          oX, q0,          oY, q1.negate(), oZ).multiply(0.5);
  323.         final T q3Dot = q0.linearCombination(q2.negate(), oX, q1,          oY, q0,          oZ).multiply(0.5);

  324.         // second time-derivatives of the quaternion
  325.         final T oXDot = rotationAcceleration.getX();
  326.         final T oYDot = rotationAcceleration.getY();
  327.         final T oZDot = rotationAcceleration.getZ();
  328.         final T q0DotDot = q0.linearCombination(array6(q1, q2,  q3, q1Dot, q2Dot,  q3Dot),
  329.                                                 array6(oXDot, oYDot, oZDot, oX, oY, oZ)).
  330.                            multiply(-0.5);
  331.         final T q1DotDot = q0.linearCombination(array6(q0, q2, q3.negate(), q0Dot, q2Dot, q3Dot.negate()),
  332.                                                 array6(oXDot, oZDot, oYDot, oX, oZ, oY)).multiply(0.5);
  333.         final T q2DotDot =  q0.linearCombination(array6(q0, q3, q1.negate(), q0Dot, q3Dot, q1Dot.negate()),
  334.                                                  array6(oYDot, oXDot, oZDot, oY, oX, oZ)).multiply(0.5);
  335.         final T q3DotDot =  q0.linearCombination(array6(q0, q1, q2.negate(), q0Dot, q1Dot, q2Dot.negate()),
  336.                                                  array6(oZDot, oYDot, oXDot, oZ, oY, oX)).multiply(0.5);

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

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

  368.     }

  369.     /** Build an arry of 6 elements.
  370.      * @param e1 first element
  371.      * @param e2 second element
  372.      * @param e3 third element
  373.      * @param e4 fourth element
  374.      * @param e5 fifth element
  375.      * @param e6 sixth element
  376.      * @return a new array
  377.      * @since 9.2
  378.      */
  379.     private T[] array6(final T e1, final T e2, final T e3, final T e4, final T e5, final T e6) {
  380.         final T[] array = MathArrays.buildArray(e1.getField(), 6);
  381.         array[0] = e1;
  382.         array[1] = e2;
  383.         array[2] = e3;
  384.         array[3] = e4;
  385.         array[4] = e5;
  386.         array[5] = e6;
  387.         return array;
  388.     }

  389.     /** Estimate rotation rate between two orientations.
  390.      * <p>Estimation is based on a simple fixed rate rotation
  391.      * during the time interval between the two orientations.</p>
  392.      * @param start start orientation
  393.      * @param end end orientation
  394.      * @param dt time elapsed between the dates of the two orientations
  395.      * @param <T> the type of the field elements
  396.      * @return rotation rate allowing to go from start to end orientations
  397.      */
  398.     public static <T extends RealFieldElement<T>>
  399.         FieldVector3D<T> estimateRate(final FieldRotation<T> start,
  400.                                       final FieldRotation<T> end,
  401.                                       final double dt) {
  402.         return estimateRate(start, end, start.getQ0().getField().getZero().add(dt));
  403.     }

  404.     /** Estimate rotation rate between two orientations.
  405.      * <p>Estimation is based on a simple fixed rate rotation
  406.      * during the time interval between the two orientations.</p>
  407.      * @param start start orientation
  408.      * @param end end orientation
  409.      * @param dt time elapsed between the dates of the two orientations
  410.      * @param <T> the type of the field elements
  411.      * @return rotation rate allowing to go from start to end orientations
  412.      */
  413.     public static <T extends RealFieldElement<T>>
  414.         FieldVector3D<T> estimateRate(final FieldRotation<T> start,
  415.                                       final FieldRotation<T> end,
  416.                                       final T dt) {
  417.         final FieldRotation<T> evolution = start.compose(end.revert(), RotationConvention.VECTOR_OPERATOR);
  418.         return new FieldVector3D<>(evolution.getAngle().divide(dt),
  419.                                    evolution.getAxis(RotationConvention.VECTOR_OPERATOR));
  420.     }

  421.     /**
  422.      * Revert a rotation / rotation rate / rotation acceleration triplet.
  423.      *
  424.      * <p> Build a triplet which reverse the effect of another triplet.
  425.      *
  426.      * @return a new triplet whose effect is the reverse of the effect
  427.      * of the instance
  428.      */
  429.     public FieldAngularCoordinates<T> revert() {
  430.         return new FieldAngularCoordinates<>(rotation.revert(),
  431.                                              rotation.applyInverseTo(rotationRate.negate()),
  432.                                              rotation.applyInverseTo(rotationAcceleration.negate()));
  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.      * a simple quadratic model. It is <em>not</em> intended as a replacement for
  438.      * proper attitude propagation but should be sufficient for either small
  439.      * 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 FieldAngularCoordinates<T> shiftedBy(final double dt) {
  445.         return shiftedBy(rotation.getQ0().getField().getZero().add(dt));
  446.     }

  447.     /** Get a time-shifted state.
  448.      * <p>
  449.      * The state can be slightly shifted to close dates. This shift is based on
  450.      * a simple quadratic model. It is <em>not</em> intended as a replacement for
  451.      * proper attitude propagation but should be sufficient for either small
  452.      * time shifts or coarse accuracy.
  453.      * </p>
  454.      * @param dt time shift in seconds
  455.      * @return a new state, shifted with respect to the instance (which is immutable)
  456.      */
  457.     public FieldAngularCoordinates<T> shiftedBy(final T dt) {

  458.         // the shiftedBy method is based on a local approximation.
  459.         // It considers separately the contribution of the constant
  460.         // rotation, the linear contribution or the rate and the
  461.         // quadratic contribution of the acceleration. The rate
  462.         // and acceleration contributions are small rotations as long
  463.         // as the time shift is small, which is the crux of the algorithm.
  464.         // Small rotations are almost commutative, so we append these small
  465.         // contributions one after the other, as if they really occurred
  466.         // successively, despite this is not what really happens.

  467.         // compute the linear contribution first, ignoring acceleration
  468.         // BEWARE: there is really a minus sign here, because if
  469.         // the target frame rotates in one direction, the vectors in the origin
  470.         // frame seem to rotate in the opposite direction
  471.         final T rate = rotationRate.getNorm();
  472.         final T zero = rate.getField().getZero();
  473.         final T one  = rate.getField().getOne();
  474.         final FieldRotation<T> rateContribution = (rate.getReal() == 0.0) ?
  475.                                                   new FieldRotation<>(one, zero, zero, zero, false) :
  476.                                                   new FieldRotation<>(rotationRate,
  477.                                                                       rate.multiply(dt),
  478.                                                                       RotationConvention.FRAME_TRANSFORM);

  479.         // append rotation and rate contribution
  480.         final FieldAngularCoordinates<T> linearPart =
  481.                 new FieldAngularCoordinates<>(rateContribution.compose(rotation, RotationConvention.VECTOR_OPERATOR),
  482.                                               rotationRate);

  483.         final T acc  = rotationAcceleration.getNorm();
  484.         if (acc.getReal() == 0.0) {
  485.             // no acceleration, the linear part is sufficient
  486.             return linearPart;
  487.         }

  488.         // compute the quadratic contribution, ignoring initial rotation and rotation rate
  489.         // BEWARE: there is really a minus sign here, because if
  490.         // the target frame rotates in one direction, the vectors in the origin
  491.         // frame seem to rotate in the opposite direction
  492.         final FieldAngularCoordinates<T> quadraticContribution =
  493.                 new FieldAngularCoordinates<>(new FieldRotation<>(rotationAcceleration,
  494.                                                                   acc.multiply(dt.multiply(0.5).multiply(dt)),
  495.                                                                   RotationConvention.FRAME_TRANSFORM),
  496.                                               new FieldVector3D<>(dt, rotationAcceleration),
  497.                                               rotationAcceleration);

  498.         // the quadratic contribution is a small rotation:
  499.         // its initial angle and angular rate are both zero.
  500.         // small rotations are almost commutative, so we append the small
  501.         // quadratic part after the linear part as a simple offset
  502.         return quadraticContribution.addOffset(linearPart);

  503.     }

  504.     /** Get the rotation.
  505.      * @return the rotation.
  506.      */
  507.     public FieldRotation<T> getRotation() {
  508.         return rotation;
  509.     }

  510.     /** Get the rotation rate.
  511.      * @return the rotation rate vector (rad/s).
  512.      */
  513.     public FieldVector3D<T> getRotationRate() {
  514.         return rotationRate;
  515.     }

  516.     /** Get the rotation acceleration.
  517.      * @return the rotation acceleration vector dΩ/dt (rad²/s²).
  518.      */
  519.     public FieldVector3D<T> getRotationAcceleration() {
  520.         return rotationAcceleration;
  521.     }

  522.     /** Add an offset from the instance.
  523.      * <p>
  524.      * We consider here that the offset rotation is applied first and the
  525.      * instance is applied afterward. Note that angular coordinates do <em>not</em>
  526.      * commute under this operation, i.e. {@code a.addOffset(b)} and {@code
  527.      * b.addOffset(a)} lead to <em>different</em> results in most cases.
  528.      * </p>
  529.      * <p>
  530.      * The two methods {@link #addOffset(FieldAngularCoordinates) addOffset} and
  531.      * {@link #subtractOffset(FieldAngularCoordinates) subtractOffset} are designed
  532.      * so that round trip applications are possible. This means that both {@code
  533.      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
  534.      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
  535.      * </p>
  536.      * @param offset offset to subtract
  537.      * @return new instance, with offset subtracted
  538.      * @see #subtractOffset(FieldAngularCoordinates)
  539.      */
  540.     public FieldAngularCoordinates<T> addOffset(final FieldAngularCoordinates<T> offset) {
  541.         final FieldVector3D<T> rOmega    = rotation.applyTo(offset.rotationRate);
  542.         final FieldVector3D<T> rOmegaDot = rotation.applyTo(offset.rotationAcceleration);
  543.         return new FieldAngularCoordinates<>(rotation.compose(offset.rotation, RotationConvention.VECTOR_OPERATOR),
  544.                                              rotationRate.add(rOmega),
  545.                                              new FieldVector3D<>( 1.0, rotationAcceleration,
  546.                                                                   1.0, rOmegaDot,
  547.                                                                  -1.0, FieldVector3D.crossProduct(rotationRate, rOmega)));
  548.     }

  549.     /** Subtract an offset from the instance.
  550.      * <p>
  551.      * We consider here that the offset Rotation is applied first and the
  552.      * instance is applied afterward. Note that angular coordinates do <em>not</em>
  553.      * commute under this operation, i.e. {@code a.subtractOffset(b)} and {@code
  554.      * b.subtractOffset(a)} lead to <em>different</em> results in most cases.
  555.      * </p>
  556.      * <p>
  557.      * The two methods {@link #addOffset(FieldAngularCoordinates) addOffset} and
  558.      * {@link #subtractOffset(FieldAngularCoordinates) subtractOffset} are designed
  559.      * so that round trip applications are possible. This means that both {@code
  560.      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
  561.      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
  562.      * </p>
  563.      * @param offset offset to subtract
  564.      * @return new instance, with offset subtracted
  565.      * @see #addOffset(FieldAngularCoordinates)
  566.      */
  567.     public FieldAngularCoordinates<T> subtractOffset(final FieldAngularCoordinates<T> offset) {
  568.         return addOffset(offset.revert());
  569.     }

  570.     /** Convert to a regular angular coordinates.
  571.      * @return a regular angular coordinates
  572.      */
  573.     public AngularCoordinates toAngularCoordinates() {
  574.         return new AngularCoordinates(rotation.toRotation(), rotationRate.toVector3D(),
  575.                                       rotationAcceleration.toVector3D());
  576.     }

  577.     /** Apply the rotation to a pv coordinates.
  578.      * @param pv vector to apply the rotation to
  579.      * @return a new pv coordinates which is the image of u by the rotation
  580.      */
  581.     public FieldPVCoordinates<T> applyTo(final PVCoordinates pv) {

  582.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  583.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  584.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  585.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  586.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  587.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  588.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  589.                                                                   -2, crossV,
  590.                                                                   -1, crossCrossP,
  591.                                                                   -1, crossDotP);

  592.         return new FieldPVCoordinates<>(transformedP, transformedV, transformedA);

  593.     }

  594.     /** Apply the rotation to a pv coordinates.
  595.      * @param pv vector to apply the rotation to
  596.      * @return a new pv coordinates which is the image of u by the rotation
  597.      */
  598.     public TimeStampedFieldPVCoordinates<T> applyTo(final TimeStampedPVCoordinates pv) {

  599.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  600.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  601.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  602.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  603.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  604.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  605.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  606.                                                                   -2, crossV,
  607.                                                                   -1, crossCrossP,
  608.                                                                   -1, crossDotP);

  609.         return new TimeStampedFieldPVCoordinates<>(pv.getDate(), transformedP, transformedV, transformedA);

  610.     }

  611.     /** Apply the rotation to a pv coordinates.
  612.      * @param pv vector to apply the rotation to
  613.      * @return a new pv coordinates which is the image of u by the rotation
  614.      * @since 9.0
  615.      */
  616.     public FieldPVCoordinates<T> applyTo(final FieldPVCoordinates<T> pv) {

  617.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  618.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  619.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  620.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  621.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  622.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  623.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  624.                                                                   -2, crossV,
  625.                                                                   -1, crossCrossP,
  626.                                                                   -1, crossDotP);

  627.         return new FieldPVCoordinates<>(transformedP, transformedV, transformedA);

  628.     }

  629.     /** Apply the rotation to a pv coordinates.
  630.      * @param pv vector to apply the rotation to
  631.      * @return a new pv coordinates which is the image of u by the rotation
  632.      * @since 9.0
  633.      */
  634.     public TimeStampedFieldPVCoordinates<T> applyTo(final TimeStampedFieldPVCoordinates<T> pv) {

  635.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  636.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  637.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  638.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  639.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  640.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  641.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  642.                                                                   -2, crossV,
  643.                                                                   -1, crossCrossP,
  644.                                                                   -1, crossDotP);

  645.         return new TimeStampedFieldPVCoordinates<>(pv.getDate(), transformedP, transformedV, transformedA);

  646.     }

  647.     /** Convert rotation, rate and acceleration to modified Rodrigues vector and derivatives.
  648.      * <p>
  649.      * The modified Rodrigues vector is tan(θ/4) u where θ and u are the
  650.      * rotation angle and axis respectively.
  651.      * </p>
  652.      * @param sign multiplicative sign for quaternion components
  653.      * @return modified Rodrigues vector and derivatives (vector on row 0, first derivative
  654.      * on row 1, second derivative on row 2)
  655.      * @see #createFromModifiedRodrigues(RealFieldElement[][])
  656.      * @since 9.0
  657.      */
  658.     public T[][] getModifiedRodrigues(final double sign) {

  659.         final T q0    = getRotation().getQ0().multiply(sign);
  660.         final T q1    = getRotation().getQ1().multiply(sign);
  661.         final T q2    = getRotation().getQ2().multiply(sign);
  662.         final T q3    = getRotation().getQ3().multiply(sign);
  663.         final T oX    = getRotationRate().getX();
  664.         final T oY    = getRotationRate().getY();
  665.         final T oZ    = getRotationRate().getZ();
  666.         final T oXDot = getRotationAcceleration().getX();
  667.         final T oYDot = getRotationAcceleration().getY();
  668.         final T oZDot = getRotationAcceleration().getZ();

  669.         // first time-derivatives of the quaternion
  670.         final T q0Dot = q0.linearCombination(q1.negate(), oX, q2.negate(), oY, q3.negate(), oZ).multiply(0.5);
  671.         final T q1Dot = q0.linearCombination( q0, oX, q3.negate(), oY,  q2, oZ).multiply(0.5);
  672.         final T q2Dot = q0.linearCombination( q3, oX,  q0, oY, q1.negate(), oZ).multiply(0.5);
  673.         final T q3Dot = q0.linearCombination(q2.negate(), oX,  q1, oY,  q0, oZ).multiply(0.5);

  674.         // second time-derivatives of the quaternion
  675.         final T q0DotDot = linearCombination(q1, oXDot, q2, oYDot, q3, oZDot,
  676.                                              q1Dot, oX, q2Dot, oY, q3Dot, oZ).
  677.                            multiply(-0.5);
  678.         final T q1DotDot = linearCombination(q0, oXDot, q2, oZDot, q3.negate(), oYDot,
  679.                                              q0Dot, oX, q2Dot, oZ, q3Dot.negate(), oY).
  680.                            multiply(0.5);
  681.         final T q2DotDot = linearCombination(q0, oYDot, q3, oXDot, q1.negate(), oZDot,
  682.                                              q0Dot, oY, q3Dot, oX, q1Dot.negate(), oZ).
  683.                            multiply(0.5);
  684.         final T q3DotDot = linearCombination(q0, oZDot, q1, oYDot, q2.negate(), oXDot,
  685.                                              q0Dot, oZ, q1Dot, oY, q2Dot.negate(), oX).
  686.                            multiply(0.5);

  687.         // the modified Rodrigues is tan(θ/4) u where θ and u are the rotation angle and axis respectively
  688.         // this can be rewritten using quaternion components:
  689.         //      r (q₁ / (1+q₀), q₂ / (1+q₀), q₃ / (1+q₀))
  690.         // applying the derivation chain rule to previous expression gives rDot and rDotDot
  691.         final T inv          = q0.add(1).reciprocal();
  692.         final T mTwoInvQ0Dot = inv.multiply(q0Dot).multiply(-2);

  693.         final T r1       = inv.multiply(q1);
  694.         final T r2       = inv.multiply(q2);
  695.         final T r3       = inv.multiply(q3);

  696.         final T mInvR1   = inv.multiply(r1).negate();
  697.         final T mInvR2   = inv.multiply(r2).negate();
  698.         final T mInvR3   = inv.multiply(r3).negate();

  699.         final T r1Dot    = q0.linearCombination(inv, q1Dot, mInvR1, q0Dot);
  700.         final T r2Dot    = q0.linearCombination(inv, q2Dot, mInvR2, q0Dot);
  701.         final T r3Dot    = q0.linearCombination(inv, q3Dot, mInvR3, q0Dot);

  702.         final T r1DotDot = q0.linearCombination(inv, q1DotDot, mTwoInvQ0Dot, r1Dot, mInvR1, q0DotDot);
  703.         final T r2DotDot = q0.linearCombination(inv, q2DotDot, mTwoInvQ0Dot, r2Dot, mInvR2, q0DotDot);
  704.         final T r3DotDot = q0.linearCombination(inv, q3DotDot, mTwoInvQ0Dot, r3Dot, mInvR3, q0DotDot);

  705.         final T[][] rodrigues = MathArrays.buildArray(q0.getField(), 3, 3);
  706.         rodrigues[0][0] = r1;
  707.         rodrigues[0][1] = r2;
  708.         rodrigues[0][2] = r3;
  709.         rodrigues[1][0] = r1Dot;
  710.         rodrigues[1][1] = r2Dot;
  711.         rodrigues[1][2] = r3Dot;
  712.         rodrigues[2][0] = r1DotDot;
  713.         rodrigues[2][1] = r2DotDot;
  714.         rodrigues[2][2] = r3DotDot;
  715.         return rodrigues;

  716.     }

  717.     /**
  718.      * Compute a linear combination.
  719.      * @param a1 first factor of the first term
  720.      * @param b1 second factor of the first term
  721.      * @param a2 first factor of the second term
  722.      * @param b2 second factor of the second term
  723.      * @param a3 first factor of the third term
  724.      * @param b3 second factor of the third term
  725.      * @param a4 first factor of the fourth term
  726.      * @param b4 second factor of the fourth term
  727.      * @param a5 first factor of the fifth term
  728.      * @param b5 second factor of the fifth term
  729.      * @param a6 first factor of the sixth term
  730.      * @param b6 second factor of the sicth term
  731.      * @return a<sub>1</sub>&times;b<sub>1</sub> + a<sub>2</sub>&times;b<sub>2</sub> +
  732.      * a<sub>3</sub>&times;b<sub>3</sub> + a<sub>4</sub>&times;b<sub>4</sub> +
  733.      * a<sub>5</sub>&times;b<sub>5</sub> + a<sub>6</sub>&times;b<sub>6</sub>
  734.      */
  735.     private T linearCombination(final T a1, final T b1, final T a2, final T b2, final T a3, final T b3,
  736.                                 final T a4, final T b4, final T a5, final T b5, final T a6, final T b6) {

  737.         final T[] a = MathArrays.buildArray(a1.getField(), 6);
  738.         a[0] = a1;
  739.         a[1] = a2;
  740.         a[2] = a3;
  741.         a[3] = a4;
  742.         a[4] = a5;
  743.         a[5] = a6;

  744.         final T[] b = MathArrays.buildArray(b1.getField(), 6);
  745.         b[0] = b1;
  746.         b[1] = b2;
  747.         b[2] = b3;
  748.         b[3] = b4;
  749.         b[4] = b5;
  750.         b[5] = b6;

  751.         return a1.linearCombination(a, b);

  752.     }

  753.     /** Convert a modified Rodrigues vector and derivatives to angular coordinates.
  754.      * @param r modified Rodrigues vector (with first and second times derivatives)
  755.      * @param <T> the type of the field elements
  756.      * @return angular coordinates
  757.      * @see #getModifiedRodrigues(double)
  758.      * @since 9.0
  759.      */
  760.     public static <T extends RealFieldElement<T>>  FieldAngularCoordinates<T> createFromModifiedRodrigues(final T[][] r) {

  761.         // rotation
  762.         final T rSquared = r[0][0].multiply(r[0][0]).add(r[0][1].multiply(r[0][1])).add(r[0][2].multiply(r[0][2]));
  763.         final T oPQ0     = rSquared.add(1).reciprocal().multiply(2);
  764.         final T q0       = oPQ0.subtract(1);
  765.         final T q1       = oPQ0.multiply(r[0][0]);
  766.         final T q2       = oPQ0.multiply(r[0][1]);
  767.         final T q3       = oPQ0.multiply(r[0][2]);

  768.         // rotation rate
  769.         final T oPQ02    = oPQ0.multiply(oPQ0);
  770.         final T q0Dot    = oPQ02.multiply(q0.linearCombination(r[0][0], r[1][0], r[0][1], r[1][1],  r[0][2], r[1][2])).negate();
  771.         final T q1Dot    = oPQ0.multiply(r[1][0]).add(r[0][0].multiply(q0Dot));
  772.         final T q2Dot    = oPQ0.multiply(r[1][1]).add(r[0][1].multiply(q0Dot));
  773.         final T q3Dot    = oPQ0.multiply(r[1][2]).add(r[0][2].multiply(q0Dot));
  774.         final T oX       = q0.linearCombination(q1.negate(), q0Dot,  q0, q1Dot,  q3, q2Dot, q2.negate(), q3Dot).multiply(2);
  775.         final T oY       = q0.linearCombination(q2.negate(), q0Dot, q3.negate(), q1Dot,  q0, q2Dot,  q1, q3Dot).multiply(2);
  776.         final T oZ       = q0.linearCombination(q3.negate(), q0Dot,  q2, q1Dot, q1.negate(), q2Dot,  q0, q3Dot).multiply(2);

  777.         // rotation acceleration
  778.         final T q0DotDot = q0.subtract(1).negate().divide(oPQ0).multiply(q0Dot).multiply(q0Dot).
  779.                            subtract(oPQ02.multiply(q0.linearCombination(r[0][0], r[2][0], r[0][1], r[2][1], r[0][2], r[2][2]))).
  780.                            subtract(q1Dot.multiply(q1Dot).add(q2Dot.multiply(q2Dot)).add(q3Dot.multiply(q3Dot)));
  781.         final T q1DotDot = q0.linearCombination(oPQ0, r[2][0], r[1][0].add(r[1][0]), q0Dot, r[0][0], q0DotDot);
  782.         final T q2DotDot = q0.linearCombination(oPQ0, r[2][1], r[1][1].add(r[1][1]), q0Dot, r[0][1], q0DotDot);
  783.         final T q3DotDot = q0.linearCombination(oPQ0, r[2][2], r[1][2].add(r[1][2]), q0Dot, r[0][2], q0DotDot);
  784.         final T oXDot    = q0.linearCombination(q1.negate(), q0DotDot,  q0, q1DotDot,  q3, q2DotDot, q2.negate(), q3DotDot).multiply(2);
  785.         final T oYDot    = q0.linearCombination(q2.negate(), q0DotDot, q3.negate(), q1DotDot,  q0, q2DotDot,  q1, q3DotDot).multiply(2);
  786.         final T oZDot    = q0.linearCombination(q3.negate(), q0DotDot,  q2, q1DotDot, q1.negate(), q2DotDot,  q0, q3DotDot).multiply(2);

  787.         return new FieldAngularCoordinates<>(new FieldRotation<>(q0, q1, q2, q3, false),
  788.                                              new FieldVector3D<>(oX, oY, oZ),
  789.                                              new FieldVector3D<>(oXDot, oYDot, oZDot));

  790.     }

  791. }