TimeStampedAngularCoordinates.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 java.util.Collection;

  19. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  20. import org.hipparchus.analysis.interpolation.HermiteInterpolator;
  21. import org.hipparchus.geometry.euclidean.threed.FieldRotation;
  22. import org.hipparchus.geometry.euclidean.threed.Rotation;
  23. import org.hipparchus.geometry.euclidean.threed.RotationConvention;
  24. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  25. import org.hipparchus.util.FastMath;
  26. import org.hipparchus.util.MathArrays;
  27. import org.orekit.errors.OrekitException;
  28. import org.orekit.errors.OrekitInternalError;
  29. import org.orekit.errors.OrekitMessages;
  30. import org.orekit.time.AbsoluteDate;
  31. import org.orekit.time.TimeStamped;

  32. /** {@link TimeStamped time-stamped} version of {@link AngularCoordinates}.
  33.  * <p>Instances of this class are guaranteed to be immutable.</p>
  34.  * @author Luc Maisonobe
  35.  * @since 7.0
  36.  */
  37. public class TimeStampedAngularCoordinates extends AngularCoordinates implements TimeStamped {

  38.     /** Serializable UID. */
  39.     private static final long serialVersionUID = 20140723L;

  40.     /** The date. */
  41.     private final AbsoluteDate date;

  42.     /** Builds a rotation/rotation rate pair.
  43.      * @param date coordinates date
  44.      * @param rotation rotation
  45.      * @param rotationRate rotation rate Ω (rad/s)
  46.      * @param rotationAcceleration rotation acceleration dΩ/dt (rad²/s²)
  47.      */
  48.     public TimeStampedAngularCoordinates(final AbsoluteDate date,
  49.                                          final Rotation rotation,
  50.                                          final Vector3D rotationRate,
  51.                                          final Vector3D rotationAcceleration) {
  52.         super(rotation, rotationRate, rotationAcceleration);
  53.         this.date = date;
  54.     }

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

  56.      * <p><em>WARNING</em>! This method requires much more stringent assumptions on
  57.      * its parameters than the similar {@link Rotation#Rotation(Vector3D, Vector3D,
  58.      * Vector3D, Vector3D) constructor} from the {@link Rotation Rotation} class.
  59.      * As far as the Rotation constructor is concerned, the {@code v₂} vector from
  60.      * the second pair can be slightly misaligned. The Rotation constructor will
  61.      * compensate for this misalignment and create a rotation that ensure {@code
  62.      * v₁ = r(u₁)} and {@code v₂ ∈ plane (r(u₁), r(u₂))}. <em>THIS IS NOT
  63.      * TRUE ANYMORE IN THIS CLASS</em>! As derivatives are involved and must be
  64.      * preserved, this constructor works <em>only</em> if the two pairs are fully
  65.      * consistent, i.e. if a rotation exists that fulfill all the requirements: {@code
  66.      * v₁ = r(u₁)}, {@code v₂ = r(u₂)}, {@code dv₁/dt = dr(u₁)/dt}, {@code dv₂/dt
  67.      * = dr(u₂)/dt}, {@code d²v₁/dt² = d²r(u₁)/dt²}, {@code d²v₂/dt² = d²r(u₂)/dt²}.</p>

  68.      * @param date coordinates date
  69.      * @param u1 first vector of the origin pair
  70.      * @param u2 second vector of the origin pair
  71.      * @param v1 desired image of u1 by the rotation
  72.      * @param v2 desired image of u2 by the rotation
  73.      * @param tolerance relative tolerance factor used to check singularities
  74.      */
  75.     public TimeStampedAngularCoordinates(final AbsoluteDate date,
  76.                                          final PVCoordinates u1, final PVCoordinates u2,
  77.                                          final PVCoordinates v1, final PVCoordinates v2,
  78.                                          final double tolerance) {
  79.         super(u1, u2, v1, v2, tolerance);
  80.         this.date = date;
  81.     }

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

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

  89.      * @param date coordinates date
  90.      * @param u origin vector
  91.      * @param v desired image of u by the rotation
  92.      */
  93.     public TimeStampedAngularCoordinates(final AbsoluteDate date,
  94.                                          final PVCoordinates u, final PVCoordinates v) {
  95.         super(u, v);
  96.         this.date = date;
  97.     }

  98.     /** Builds a TimeStampedAngularCoordinates from  a {@link FieldRotation}&lt;{@link DerivativeStructure}&gt;.
  99.      * <p>
  100.      * The rotation components must have time as their only derivation parameter and
  101.      * have consistent derivation orders.
  102.      * </p>
  103.      * @param date coordinates date
  104.      * @param r rotation with time-derivatives embedded within the coordinates
  105.      */
  106.     public TimeStampedAngularCoordinates(final AbsoluteDate date,
  107.                                          final FieldRotation<DerivativeStructure> r) {
  108.         super(r);
  109.         this.date = date;
  110.     }

  111.     /** {@inheritDoc} */
  112.     public AbsoluteDate getDate() {
  113.         return date;
  114.     }

  115.     /** Revert a rotation/rotation rate pair.
  116.      * Build a pair which reverse the effect of another pair.
  117.      * @return a new pair whose effect is the reverse of the effect
  118.      * of the instance
  119.      */
  120.     public TimeStampedAngularCoordinates revert() {
  121.         return new TimeStampedAngularCoordinates(date,
  122.                                                  getRotation().revert(),
  123.                                                  getRotation().applyInverseTo(getRotationRate().negate()),
  124.                                                  getRotation().applyInverseTo(getRotationAcceleration().negate()));
  125.     }

  126.     /** Get a time-shifted state.
  127.      * <p>
  128.      * The state can be slightly shifted to close dates. This shift is based on
  129.      * a simple linear model. It is <em>not</em> intended as a replacement for
  130.      * proper attitude propagation but should be sufficient for either small
  131.      * time shifts or coarse accuracy.
  132.      * </p>
  133.      * @param dt time shift in seconds
  134.      * @return a new state, shifted with respect to the instance (which is immutable)
  135.      */
  136.     public TimeStampedAngularCoordinates shiftedBy(final double dt) {
  137.         final AngularCoordinates sac = super.shiftedBy(dt);
  138.         return new TimeStampedAngularCoordinates(date.shiftedBy(dt),
  139.                                                  sac.getRotation(), sac.getRotationRate(), sac.getRotationAcceleration());

  140.     }

  141.     /** Add an offset from the instance.
  142.      * <p>
  143.      * We consider here that the offset rotation is applied first and the
  144.      * instance is applied afterward. Note that angular coordinates do <em>not</em>
  145.      * commute under this operation, i.e. {@code a.addOffset(b)} and {@code
  146.      * b.addOffset(a)} lead to <em>different</em> results in most cases.
  147.      * </p>
  148.      * <p>
  149.      * The two methods {@link #addOffset(AngularCoordinates) addOffset} and
  150.      * {@link #subtractOffset(AngularCoordinates) subtractOffset} are designed
  151.      * so that round trip applications are possible. This means that both {@code
  152.      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
  153.      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
  154.      * </p>
  155.      * @param offset offset to subtract
  156.      * @return new instance, with offset subtracted
  157.      * @see #subtractOffset(AngularCoordinates)
  158.      */
  159.     @Override
  160.     public TimeStampedAngularCoordinates addOffset(final AngularCoordinates offset) {
  161.         final Vector3D rOmega    = getRotation().applyTo(offset.getRotationRate());
  162.         final Vector3D rOmegaDot = getRotation().applyTo(offset.getRotationAcceleration());
  163.         return new TimeStampedAngularCoordinates(date,
  164.                                                  getRotation().compose(offset.getRotation(), RotationConvention.VECTOR_OPERATOR),
  165.                                                  getRotationRate().add(rOmega),
  166.                                                  new Vector3D( 1.0, getRotationAcceleration(),
  167.                                                                1.0, rOmegaDot,
  168.                                                               -1.0, Vector3D.crossProduct(getRotationRate(), rOmega)));
  169.     }

  170.     /** Subtract an offset from the instance.
  171.      * <p>
  172.      * We consider here that the offset rotation is applied first and the
  173.      * instance is applied afterward. Note that angular coordinates do <em>not</em>
  174.      * commute under this operation, i.e. {@code a.subtractOffset(b)} and {@code
  175.      * b.subtractOffset(a)} lead to <em>different</em> results in most cases.
  176.      * </p>
  177.      * <p>
  178.      * The two methods {@link #addOffset(AngularCoordinates) addOffset} and
  179.      * {@link #subtractOffset(AngularCoordinates) subtractOffset} are designed
  180.      * so that round trip applications are possible. This means that both {@code
  181.      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
  182.      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
  183.      * </p>
  184.      * @param offset offset to subtract
  185.      * @return new instance, with offset subtracted
  186.      * @see #addOffset(AngularCoordinates)
  187.      */
  188.     @Override
  189.     public TimeStampedAngularCoordinates subtractOffset(final AngularCoordinates offset) {
  190.         return addOffset(offset.revert());
  191.     }

  192.     /** Interpolate angular coordinates.
  193.      * <p>
  194.      * The interpolated instance is created by polynomial Hermite interpolation
  195.      * on Rodrigues vector ensuring rotation rate remains the exact derivative of rotation.
  196.      * </p>
  197.      * <p>
  198.      * This method is based on Sergei Tanygin's paper <a
  199.      * href="http://www.agi.com/downloads/resources/white-papers/Attitude-interpolation.pdf">Attitude
  200.      * Interpolation</a>, changing the norm of the vector to match the modified Rodrigues
  201.      * vector as described in Malcolm D. Shuster's paper <a
  202.      * href="http://www.ladispe.polito.it/corsi/Meccatronica/02JHCOR/2011-12/Slides/Shuster_Pub_1993h_J_Repsurv_scan.pdf">A
  203.      * Survey of Attitude Representations</a>. This change avoids the singularity at π.
  204.      * There is still a singularity at 2π, which is handled by slightly offsetting all rotations
  205.      * when this singularity is detected. Another change is that the mean linear motion
  206.      * is first removed before interpolation and added back after interpolation. This allows
  207.      * to use interpolation even when the sample covers much more than one turn and even
  208.      * when sample points are separated by more than one turn.
  209.      * </p>
  210.      * <p>
  211.      * Note that even if first and second time derivatives (rotation rates and acceleration)
  212.      * from sample can be ignored, the interpolated instance always includes
  213.      * interpolated derivatives. This feature can be used explicitly to
  214.      * compute these derivatives when it would be too complex to compute them
  215.      * from an analytical formula: just compute a few sample points from the
  216.      * explicit formula and set the derivatives to zero in these sample points,
  217.      * then use interpolation to add derivatives consistent with the rotations.
  218.      * </p>
  219.      * @param date interpolation date
  220.      * @param filter filter for derivatives from the sample to use in interpolation
  221.      * @param sample sample points on which interpolation should be done
  222.      * @return a new position-velocity, interpolated at specified date
  223.      */
  224.     public static TimeStampedAngularCoordinates interpolate(final AbsoluteDate date,
  225.                                                             final AngularDerivativesFilter filter,
  226.                                                             final Collection<TimeStampedAngularCoordinates> sample) {

  227.         // set up safety elements for 2π singularity avoidance
  228.         final double epsilon   = 2 * FastMath.PI / sample.size();
  229.         final double threshold = FastMath.min(-(1.0 - 1.0e-4), -FastMath.cos(epsilon / 4));

  230.         // set up a linear model canceling mean rotation rate
  231.         final Vector3D meanRate;
  232.         if (filter != AngularDerivativesFilter.USE_R) {
  233.             Vector3D sum = Vector3D.ZERO;
  234.             for (final TimeStampedAngularCoordinates datedAC : sample) {
  235.                 sum = sum.add(datedAC.getRotationRate());
  236.             }
  237.             meanRate = new Vector3D(1.0 / sample.size(), sum);
  238.         } else {
  239.             if (sample.size() < 2) {
  240.                 throw new OrekitException(OrekitMessages.NOT_ENOUGH_DATA_FOR_INTERPOLATION,
  241.                                           sample.size());
  242.             }
  243.             Vector3D sum = Vector3D.ZERO;
  244.             TimeStampedAngularCoordinates previous = null;
  245.             for (final TimeStampedAngularCoordinates datedAC : sample) {
  246.                 if (previous != null) {
  247.                     sum = sum.add(estimateRate(previous.getRotation(), datedAC.getRotation(),
  248.                                                datedAC.date.durationFrom(previous.date)));
  249.                 }
  250.                 previous = datedAC;
  251.             }
  252.             meanRate = new Vector3D(1.0 / (sample.size() - 1), sum);
  253.         }
  254.         TimeStampedAngularCoordinates offset =
  255.             new TimeStampedAngularCoordinates(date, Rotation.IDENTITY, meanRate, Vector3D.ZERO);

  256.         boolean restart = true;
  257.         for (int i = 0; restart && i < sample.size() + 2; ++i) {

  258.             // offset adaptation parameters
  259.             restart = false;

  260.             // set up an interpolator taking derivatives into account
  261.             final HermiteInterpolator interpolator = new HermiteInterpolator();

  262.             // add sample points
  263.             double sign = +1.0;
  264.             Rotation previous = Rotation.IDENTITY;

  265.             for (final TimeStampedAngularCoordinates ac : sample) {

  266.                 // remove linear offset from the current coordinates
  267.                 final double dt = ac.date.durationFrom(date);
  268.                 final TimeStampedAngularCoordinates fixed = ac.subtractOffset(offset.shiftedBy(dt));

  269.                 // make sure all interpolated points will be on the same branch
  270.                 final double dot = MathArrays.linearCombination(fixed.getRotation().getQ0(), previous.getQ0(),
  271.                                                                 fixed.getRotation().getQ1(), previous.getQ1(),
  272.                                                                 fixed.getRotation().getQ2(), previous.getQ2(),
  273.                                                                 fixed.getRotation().getQ3(), previous.getQ3());
  274.                 sign = FastMath.copySign(1.0, dot * sign);
  275.                 previous = fixed.getRotation();

  276.                 // check modified Rodrigues vector singularity
  277.                 if (fixed.getRotation().getQ0() * sign < threshold) {
  278.                     // the sample point is close to a modified Rodrigues vector singularity
  279.                     // we need to change the linear offset model to avoid this
  280.                     restart = true;
  281.                     break;
  282.                 }

  283.                 final double[][] rodrigues = fixed.getModifiedRodrigues(sign);
  284.                 switch (filter) {
  285.                     case USE_RRA:
  286.                         // populate sample with rotation, rotation rate and acceleration data
  287.                         interpolator.addSamplePoint(dt, rodrigues[0], rodrigues[1], rodrigues[2]);
  288.                         break;
  289.                     case USE_RR:
  290.                         // populate sample with rotation and rotation rate data
  291.                         interpolator.addSamplePoint(dt, rodrigues[0], rodrigues[1]);
  292.                         break;
  293.                     case USE_R:
  294.                         // populate sample with rotation data only
  295.                         interpolator.addSamplePoint(dt, rodrigues[0]);
  296.                         break;
  297.                     default :
  298.                         // this should never happen
  299.                         throw new OrekitInternalError(null);
  300.                 }
  301.             }

  302.             if (restart) {
  303.                 // interpolation failed, some intermediate rotation was too close to 2π
  304.                 // we need to offset all rotations to avoid the singularity
  305.                 offset = offset.addOffset(new AngularCoordinates(new Rotation(Vector3D.PLUS_I,
  306.                                                                               epsilon,
  307.                                                                               RotationConvention.VECTOR_OPERATOR),
  308.                                                                  Vector3D.ZERO, Vector3D.ZERO));
  309.             } else {
  310.                 // interpolation succeeded with the current offset
  311.                 final double[][] p = interpolator.derivatives(0.0, 2);
  312.                 final AngularCoordinates ac = createFromModifiedRodrigues(p);
  313.                 return new TimeStampedAngularCoordinates(offset.getDate(),
  314.                                                          ac.getRotation(),
  315.                                                          ac.getRotationRate(),
  316.                                                          ac.getRotationAcceleration()).addOffset(offset);
  317.             }

  318.         }

  319.         // this should never happen
  320.         throw new OrekitInternalError(null);

  321.     }

  322. }