TimeStampedAngularCoordinates.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 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.      * @exception OrekitException if the vectors components cannot be converted to
  75.      * {@link DerivativeStructure} with proper order
  76.      */
  77.     public TimeStampedAngularCoordinates(final AbsoluteDate date,
  78.                                          final PVCoordinates u1, final PVCoordinates u2,
  79.                                          final PVCoordinates v1, final PVCoordinates v2,
  80.                                          final double tolerance)
  81.         throws OrekitException {
  82.         super(u1, u2, v1, v2, tolerance);
  83.         this.date = date;
  84.     }

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

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

  92.      * @param date coordinates date
  93.      * @param u origin vector
  94.      * @param v desired image of u by the rotation
  95.      * @exception OrekitException if the vectors components cannot be converted to
  96.      * {@link DerivativeStructure} with proper order
  97.      */
  98.     public TimeStampedAngularCoordinates(final AbsoluteDate date,
  99.                                          final PVCoordinates u, final PVCoordinates v)
  100.         throws OrekitException {
  101.         super(u, v);
  102.         this.date = date;
  103.     }

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

  117.     /** {@inheritDoc} */
  118.     public AbsoluteDate getDate() {
  119.         return date;
  120.     }

  121.     /** Revert a rotation/rotation rate pair.
  122.      * Build a pair which reverse the effect of another pair.
  123.      * @return a new pair whose effect is the reverse of the effect
  124.      * of the instance
  125.      */
  126.     public TimeStampedAngularCoordinates revert() {
  127.         return new TimeStampedAngularCoordinates(date,
  128.                                                  getRotation().revert(),
  129.                                                  getRotation().applyInverseTo(getRotationRate().negate()),
  130.                                                  getRotation().applyInverseTo(getRotationAcceleration().negate()));
  131.     }

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

  146.     }

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

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

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

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

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

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

  266.             // offset adaptation parameters
  267.             restart = false;

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

  270.             // add sample points
  271.             double sign = +1.0;
  272.             Rotation previous = Rotation.IDENTITY;

  273.             for (final TimeStampedAngularCoordinates ac : sample) {

  274.                 // remove linear offset from the current coordinates
  275.                 final double dt = ac.date.durationFrom(date);
  276.                 final TimeStampedAngularCoordinates fixed = ac.subtractOffset(offset.shiftedBy(dt));

  277.                 // make sure all interpolated points will be on the same branch
  278.                 final double dot = MathArrays.linearCombination(fixed.getRotation().getQ0(), previous.getQ0(),
  279.                                                                 fixed.getRotation().getQ1(), previous.getQ1(),
  280.                                                                 fixed.getRotation().getQ2(), previous.getQ2(),
  281.                                                                 fixed.getRotation().getQ3(), previous.getQ3());
  282.                 sign = FastMath.copySign(1.0, dot * sign);
  283.                 previous = fixed.getRotation();

  284.                 // check modified Rodrigues vector singularity
  285.                 if (fixed.getRotation().getQ0() * sign < threshold) {
  286.                     // the sample point is close to a modified Rodrigues vector singularity
  287.                     // we need to change the linear offset model to avoid this
  288.                     restart = true;
  289.                     break;
  290.                 }

  291.                 final double[][] rodrigues = fixed.getModifiedRodrigues(sign);
  292.                 switch (filter) {
  293.                     case USE_RRA:
  294.                         // populate sample with rotation, rotation rate and acceleration data
  295.                         interpolator.addSamplePoint(dt, rodrigues[0], rodrigues[1], rodrigues[2]);
  296.                         break;
  297.                     case USE_RR:
  298.                         // populate sample with rotation and rotation rate data
  299.                         interpolator.addSamplePoint(dt, rodrigues[0], rodrigues[1]);
  300.                         break;
  301.                     case USE_R:
  302.                         // populate sample with rotation data only
  303.                         interpolator.addSamplePoint(dt, rodrigues[0]);
  304.                         break;
  305.                     default :
  306.                         // this should never happen
  307.                         throw new OrekitInternalError(null);
  308.                 }
  309.             }

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

  326.         }

  327.         // this should never happen
  328.         throw new OrekitInternalError(null);

  329.     }

  330. }