FieldAngularCoordinates.java

  1. /* Copyright 2002-2019 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.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.      */
  102.     public FieldAngularCoordinates(final FieldPVCoordinates<T> u1, final FieldPVCoordinates<T> u2,
  103.                                    final FieldPVCoordinates<T> v1, final FieldPVCoordinates<T> v2,
  104.                                    final double tolerance) {

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

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


  114.             rotationRate = inverseCrossProducts(v1.getPosition(), ru1Dot.subtract(v1.getVelocity()),
  115.                                                 v2.getPosition(), ru2Dot.subtract(v2.getVelocity()),
  116.                                                 tolerance);


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

  129.         } catch (MathIllegalArgumentException miae) {
  130.             throw new OrekitException(miae);
  131.         }

  132.     }

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

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

  151.         final T q0       = r.getQ0().getValue();
  152.         final T q1       = r.getQ1().getValue();
  153.         final T q2       = r.getQ2().getValue();
  154.         final T q3       = r.getQ3().getValue();

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

  187.     }

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

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

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

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

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

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

  258.         } catch (MathIllegalArgumentException miae) {
  259.             if (miae.getSpecifier() == LocalizedCoreFormats.SINGULAR_MATRIX) {

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

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

  294.         return omega;

  295.     }

  296.     /** Transform the instance to a {@link FieldRotation}&lt;{@link FieldDerivativeStructure}&gt;.
  297.      * <p>
  298.      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
  299.      * to the user-specified order.
  300.      * </p>
  301.      * @param order derivation order for the vector components
  302.      * @return rotation with time-derivatives embedded within the coordinates
  303.           * @since 9.2
  304.      */
  305.     public FieldRotation<FieldDerivativeStructure<T>> toDerivativeStructureRotation(final int order) {

  306.         // quaternion components
  307.         final T q0 = rotation.getQ0();
  308.         final T q1 = rotation.getQ1();
  309.         final T q2 = rotation.getQ2();
  310.         final T q3 = rotation.getQ3();

  311.         // first time-derivatives of the quaternion
  312.         final T oX    = rotationRate.getX();
  313.         final T oY    = rotationRate.getY();
  314.         final T oZ    = rotationRate.getZ();
  315.         final T q0Dot = q0.linearCombination(q1.negate(), oX, q2.negate(), oY, q3.negate(), oZ).multiply(0.5);
  316.         final T q1Dot = q0.linearCombination(q0,          oX, q3.negate(), oY, q2,          oZ).multiply(0.5);
  317.         final T q2Dot = q0.linearCombination(q3,          oX, q0,          oY, q1.negate(), oZ).multiply(0.5);
  318.         final T q3Dot = q0.linearCombination(q2.negate(), oX, q1,          oY, q0,          oZ).multiply(0.5);

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

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

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

  363.     }

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

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

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

  416.     /**
  417.      * Revert a rotation / rotation rate / rotation acceleration triplet.
  418.      *
  419.      * <p> Build a triplet which reverse the effect of another triplet.
  420.      *
  421.      * @return a new triplet whose effect is the reverse of the effect
  422.      * of the instance
  423.      */
  424.     public FieldAngularCoordinates<T> revert() {
  425.         return new FieldAngularCoordinates<>(rotation.revert(),
  426.                                              rotation.applyInverseTo(rotationRate.negate()),
  427.                                              rotation.applyInverseTo(rotationAcceleration.negate()));
  428.     }

  429.     /** Get a time-shifted state.
  430.      * <p>
  431.      * The state can be slightly shifted to close dates. This shift is based on
  432.      * a simple quadratic model. It is <em>not</em> intended as a replacement for
  433.      * proper attitude propagation but should be sufficient for either small
  434.      * time shifts or coarse accuracy.
  435.      * </p>
  436.      * @param dt time shift in seconds
  437.      * @return a new state, shifted with respect to the instance (which is immutable)
  438.      */
  439.     public FieldAngularCoordinates<T> shiftedBy(final double dt) {
  440.         return shiftedBy(rotation.getQ0().getField().getZero().add(dt));
  441.     }

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

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

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

  474.         // append rotation and rate contribution
  475.         final FieldAngularCoordinates<T> linearPart =
  476.                 new FieldAngularCoordinates<>(rateContribution.compose(rotation, RotationConvention.VECTOR_OPERATOR),
  477.                                               rotationRate);

  478.         final T acc  = rotationAcceleration.getNorm();
  479.         if (acc.getReal() == 0.0) {
  480.             // no acceleration, the linear part is sufficient
  481.             return linearPart;
  482.         }

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

  493.         // the quadratic contribution is a small rotation:
  494.         // its initial angle and angular rate are both zero.
  495.         // small rotations are almost commutative, so we append the small
  496.         // quadratic part after the linear part as a simple offset
  497.         return quadraticContribution.addOffset(linearPart);

  498.     }

  499.     /** Get the rotation.
  500.      * @return the rotation.
  501.      */
  502.     public FieldRotation<T> getRotation() {
  503.         return rotation;
  504.     }

  505.     /** Get the rotation rate.
  506.      * @return the rotation rate vector (rad/s).
  507.      */
  508.     public FieldVector3D<T> getRotationRate() {
  509.         return rotationRate;
  510.     }

  511.     /** Get the rotation acceleration.
  512.      * @return the rotation acceleration vector dΩ/dt (rad²/s²).
  513.      */
  514.     public FieldVector3D<T> getRotationAcceleration() {
  515.         return rotationAcceleration;
  516.     }

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

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

  565.     /** Convert to a regular angular coordinates.
  566.      * @return a regular angular coordinates
  567.      */
  568.     public AngularCoordinates toAngularCoordinates() {
  569.         return new AngularCoordinates(rotation.toRotation(), rotationRate.toVector3D(),
  570.                                       rotationAcceleration.toVector3D());
  571.     }

  572.     /** Apply the rotation to a pv coordinates.
  573.      * @param pv vector to apply the rotation to
  574.      * @return a new pv coordinates which is the image of u by the rotation
  575.      */
  576.     public FieldPVCoordinates<T> applyTo(final PVCoordinates pv) {

  577.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  578.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  579.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  580.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  581.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  582.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  583.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  584.                                                                   -2, crossV,
  585.                                                                   -1, crossCrossP,
  586.                                                                   -1, crossDotP);

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

  588.     }

  589.     /** Apply the rotation to a pv coordinates.
  590.      * @param pv vector to apply the rotation to
  591.      * @return a new pv coordinates which is the image of u by the rotation
  592.      */
  593.     public TimeStampedFieldPVCoordinates<T> applyTo(final TimeStampedPVCoordinates pv) {

  594.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  595.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  596.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  597.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  598.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  599.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  600.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  601.                                                                   -2, crossV,
  602.                                                                   -1, crossCrossP,
  603.                                                                   -1, crossDotP);

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

  605.     }

  606.     /** Apply the rotation to a pv coordinates.
  607.      * @param pv vector to apply the rotation to
  608.      * @return a new pv coordinates which is the image of u by the rotation
  609.      * @since 9.0
  610.      */
  611.     public FieldPVCoordinates<T> applyTo(final FieldPVCoordinates<T> pv) {

  612.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  613.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  614.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  615.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  616.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  617.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  618.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  619.                                                                   -2, crossV,
  620.                                                                   -1, crossCrossP,
  621.                                                                   -1, crossDotP);

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

  623.     }

  624.     /** Apply the rotation to a pv coordinates.
  625.      * @param pv vector to apply the rotation to
  626.      * @return a new pv coordinates which is the image of u by the rotation
  627.      * @since 9.0
  628.      */
  629.     public TimeStampedFieldPVCoordinates<T> applyTo(final TimeStampedFieldPVCoordinates<T> pv) {

  630.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  631.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  632.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  633.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  634.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  635.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  636.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  637.                                                                   -2, crossV,
  638.                                                                   -1, crossCrossP,
  639.                                                                   -1, crossDotP);

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

  641.     }

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

  654.         final T q0    = getRotation().getQ0().multiply(sign);
  655.         final T q1    = getRotation().getQ1().multiply(sign);
  656.         final T q2    = getRotation().getQ2().multiply(sign);
  657.         final T q3    = getRotation().getQ3().multiply(sign);
  658.         final T oX    = getRotationRate().getX();
  659.         final T oY    = getRotationRate().getY();
  660.         final T oZ    = getRotationRate().getZ();
  661.         final T oXDot = getRotationAcceleration().getX();
  662.         final T oYDot = getRotationAcceleration().getY();
  663.         final T oZDot = getRotationAcceleration().getZ();

  664.         // first time-derivatives of the quaternion
  665.         final T q0Dot = q0.linearCombination(q1.negate(), oX, q2.negate(), oY, q3.negate(), oZ).multiply(0.5);
  666.         final T q1Dot = q0.linearCombination( q0, oX, q3.negate(), oY,  q2, oZ).multiply(0.5);
  667.         final T q2Dot = q0.linearCombination( q3, oX,  q0, oY, q1.negate(), oZ).multiply(0.5);
  668.         final T q3Dot = q0.linearCombination(q2.negate(), oX,  q1, oY,  q0, oZ).multiply(0.5);

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

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

  688.         final T r1       = inv.multiply(q1);
  689.         final T r2       = inv.multiply(q2);
  690.         final T r3       = inv.multiply(q3);

  691.         final T mInvR1   = inv.multiply(r1).negate();
  692.         final T mInvR2   = inv.multiply(r2).negate();
  693.         final T mInvR3   = inv.multiply(r3).negate();

  694.         final T r1Dot    = q0.linearCombination(inv, q1Dot, mInvR1, q0Dot);
  695.         final T r2Dot    = q0.linearCombination(inv, q2Dot, mInvR2, q0Dot);
  696.         final T r3Dot    = q0.linearCombination(inv, q3Dot, mInvR3, q0Dot);

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

  700.         final T[][] rodrigues = MathArrays.buildArray(q0.getField(), 3, 3);
  701.         rodrigues[0][0] = r1;
  702.         rodrigues[0][1] = r2;
  703.         rodrigues[0][2] = r3;
  704.         rodrigues[1][0] = r1Dot;
  705.         rodrigues[1][1] = r2Dot;
  706.         rodrigues[1][2] = r3Dot;
  707.         rodrigues[2][0] = r1DotDot;
  708.         rodrigues[2][1] = r2DotDot;
  709.         rodrigues[2][2] = r3DotDot;
  710.         return rodrigues;

  711.     }

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

  732.         final T[] a = MathArrays.buildArray(a1.getField(), 6);
  733.         a[0] = a1;
  734.         a[1] = a2;
  735.         a[2] = a3;
  736.         a[3] = a4;
  737.         a[4] = a5;
  738.         a[5] = a6;

  739.         final T[] b = MathArrays.buildArray(b1.getField(), 6);
  740.         b[0] = b1;
  741.         b[1] = b2;
  742.         b[2] = b3;
  743.         b[3] = b4;
  744.         b[4] = b5;
  745.         b[5] = b6;

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

  747.     }

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

  756.         // rotation
  757.         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]));
  758.         final T oPQ0     = rSquared.add(1).reciprocal().multiply(2);
  759.         final T q0       = oPQ0.subtract(1);
  760.         final T q1       = oPQ0.multiply(r[0][0]);
  761.         final T q2       = oPQ0.multiply(r[0][1]);
  762.         final T q3       = oPQ0.multiply(r[0][2]);

  763.         // rotation rate
  764.         final T oPQ02    = oPQ0.multiply(oPQ0);
  765.         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();
  766.         final T q1Dot    = oPQ0.multiply(r[1][0]).add(r[0][0].multiply(q0Dot));
  767.         final T q2Dot    = oPQ0.multiply(r[1][1]).add(r[0][1].multiply(q0Dot));
  768.         final T q3Dot    = oPQ0.multiply(r[1][2]).add(r[0][2].multiply(q0Dot));
  769.         final T oX       = q0.linearCombination(q1.negate(), q0Dot,  q0, q1Dot,  q3, q2Dot, q2.negate(), q3Dot).multiply(2);
  770.         final T oY       = q0.linearCombination(q2.negate(), q0Dot, q3.negate(), q1Dot,  q0, q2Dot,  q1, q3Dot).multiply(2);
  771.         final T oZ       = q0.linearCombination(q3.negate(), q0Dot,  q2, q1Dot, q1.negate(), q2Dot,  q0, q3Dot).multiply(2);

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

  782.         return new FieldAngularCoordinates<>(new FieldRotation<>(q0, q1, q2, q3, false),
  783.                                              new FieldVector3D<>(oX, oY, oZ),
  784.                                              new FieldVector3D<>(oXDot, oYDot, oZDot));

  785.     }

  786. }