TimeStampedAngularCoordinatesHermiteInterpolator.java

  1. /* Copyright 2002-2025 CS GROUP
  2.  * Licensed to CS GROUP (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.analysis.interpolation.HermiteInterpolator;
  19. import org.hipparchus.geometry.euclidean.threed.Rotation;
  20. import org.hipparchus.geometry.euclidean.threed.RotationConvention;
  21. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  22. import org.hipparchus.util.FastMath;
  23. import org.hipparchus.util.MathArrays;
  24. import org.orekit.errors.OrekitInternalError;
  25. import org.orekit.time.AbsoluteDate;
  26. import org.orekit.time.AbstractTimeInterpolator;

  27. import java.util.List;

  28. /**
  29.  * Class using Hermite interpolator to interpolate time stamped angular coordinates.
  30.  * <p>
  31.  * As this implementation of interpolation is polynomial, it should be used only with small number of interpolation points
  32.  * (about 10-20 points) in order to avoid <a href="http://en.wikipedia.org/wiki/Runge%27s_phenomenon">Runge's phenomenon</a>
  33.  * and numerical problems (including NaN appearing).
  34.  *
  35.  * @author Vincent Cucchietti
  36.  * @author Luc Maisonobe
  37.  * @see HermiteInterpolator
  38.  * @see TimeStampedAngularCoordinates
  39.  */
  40. public class TimeStampedAngularCoordinatesHermiteInterpolator
  41.         extends AbstractTimeInterpolator<TimeStampedAngularCoordinates> {

  42.     /** Filter for derivatives from the sample to use in interpolation. */
  43.     private final AngularDerivativesFilter filter;

  44.     /**
  45.      * Constructor with :
  46.      * <ul>
  47.      *     <li>Default number of interpolation points of {@code DEFAULT_INTERPOLATION_POINTS}</li>
  48.      *     <li>Default extrapolation threshold value ({@code DEFAULT_EXTRAPOLATION_THRESHOLD_SEC} s)</li>
  49.      *     <li>Use of angular and first time derivative for attitude interpolation</li>
  50.      * </ul>
  51.      * As this implementation of interpolation is polynomial, it should be used only with small number of interpolation
  52.      * points (about 10-20 points) in order to avoid <a href="http://en.wikipedia.org/wiki/Runge%27s_phenomenon">Runge's
  53.      * phenomenon</a> and numerical problems (including NaN appearing).
  54.      */
  55.     public TimeStampedAngularCoordinatesHermiteInterpolator() {
  56.         this(DEFAULT_INTERPOLATION_POINTS);
  57.     }

  58.     /**
  59.      * /** Constructor with :
  60.      * <ul>
  61.      *     <li>Default extrapolation threshold value ({@code DEFAULT_EXTRAPOLATION_THRESHOLD_SEC} s)</li>
  62.      *     <li>Use of angular and first time derivative for attitude interpolation</li>
  63.      * </ul>
  64.      * As this implementation of interpolation is polynomial, it should be used only with small number of interpolation
  65.      * points (about 10-20 points) in order to avoid <a href="http://en.wikipedia.org/wiki/Runge%27s_phenomenon">Runge's
  66.      * phenomenon</a> and numerical problems (including NaN appearing).
  67.      *
  68.      * @param interpolationPoints number of interpolation points
  69.      */
  70.     public TimeStampedAngularCoordinatesHermiteInterpolator(final int interpolationPoints) {
  71.         this(interpolationPoints, AngularDerivativesFilter.USE_RR);
  72.     }

  73.     /**
  74.      * Constructor with :
  75.      * <ul>
  76.      *     <li>Default extrapolation threshold value ({@code DEFAULT_EXTRAPOLATION_THRESHOLD_SEC} s)</li>
  77.      * </ul>
  78.      * As this implementation of interpolation is polynomial, it should be used only with small number of interpolation
  79.      * points (about 10-20 points) in order to avoid <a href="http://en.wikipedia.org/wiki/Runge%27s_phenomenon">Runge's
  80.      * phenomenon</a> and numerical problems (including NaN appearing).
  81.      *
  82.      * @param interpolationPoints number of interpolation points
  83.      * @param filter filter for derivatives from the sample to use in interpolation
  84.      */
  85.     public TimeStampedAngularCoordinatesHermiteInterpolator(final int interpolationPoints,
  86.                                                             final AngularDerivativesFilter filter) {
  87.         this(interpolationPoints, DEFAULT_EXTRAPOLATION_THRESHOLD_SEC, filter);
  88.     }

  89.     /**
  90.      * Constructor.
  91.      * <p>
  92.      * As this implementation of interpolation is polynomial, it should be used only with small number of interpolation
  93.      * points (about 10-20 points) in order to avoid <a href="http://en.wikipedia.org/wiki/Runge%27s_phenomenon">Runge's
  94.      * phenomenon</a> and numerical problems (including NaN appearing).
  95.      *
  96.      * @param interpolationPoints number of interpolation points
  97.      * @param extrapolationThreshold extrapolation threshold beyond which the propagation will fail
  98.      * @param filter filter for derivatives from the sample to use in interpolation
  99.      */
  100.     public TimeStampedAngularCoordinatesHermiteInterpolator(final int interpolationPoints,
  101.                                                             final double extrapolationThreshold,
  102.                                                             final AngularDerivativesFilter filter) {
  103.         super(interpolationPoints, extrapolationThreshold);
  104.         this.filter = filter;
  105.     }

  106.     /** Get filter for derivatives from the sample to use in interpolation.
  107.      * @return filter for derivatives from the sample to use in interpolation
  108.      */
  109.     public AngularDerivativesFilter getFilter() {
  110.         return filter;
  111.     }

  112.     /**
  113.      * {@inheritDoc}
  114.      * <p>
  115.      * The interpolated instance is created by polynomial Hermite interpolation on Rodrigues vector ensuring rotation rate
  116.      * remains the exact derivative of rotation.
  117.      * <p>
  118.      * This method is based on Sergei Tanygin's paper <a
  119.      * href="http://www.agi.com/resources/white-papers/attitude-interpolation">Attitude Interpolation</a>, changing the norm
  120.      * of the vector to match the modified Rodrigues vector as described in Malcolm D. Shuster's paper <a
  121.      * href="http://www.ladispe.polito.it/corsi/Meccatronica/02JHCOR/2011-12/Slides/Shuster_Pub_1993h_J_Repsurv_scan.pdf">A
  122.      * Survey of Attitude Representations</a>. This change avoids the singularity at π. There is still a singularity at 2π,
  123.      * which is handled by slightly offsetting all rotations when this singularity is detected. Another change is that the
  124.      * mean linear motion is first removed before interpolation and added back after interpolation. This allows to use
  125.      * interpolation even when the sample covers much more than one turn and even when sample points are separated by more
  126.      * than one turn.
  127.      * </p>
  128.      * <p>
  129.      * Note that even if first and second time derivatives (rotation rates and acceleration) from sample can be ignored, the
  130.      * interpolated instance always includes interpolated derivatives. This feature can be used explicitly to compute these
  131.      * derivatives when it would be too complex to compute them from an analytical formula: just compute a few sample points
  132.      * from the explicit formula and set the derivatives to zero in these sample points, then use interpolation to add
  133.      * derivatives consistent with the rotations.
  134.      */
  135.     @Override
  136.     protected TimeStampedAngularCoordinates interpolate(final InterpolationData interpolationData) {

  137.         // Get date
  138.         final AbsoluteDate date = interpolationData.getInterpolationDate();

  139.         // Get sample
  140.         final List<TimeStampedAngularCoordinates> sample = interpolationData.getNeighborList();

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

  144.         // set up a linear model canceling mean rotation rate
  145.         final Vector3D meanRate;
  146.         Vector3D       sum = Vector3D.ZERO;
  147.         if (filter != AngularDerivativesFilter.USE_R) {
  148.             for (final TimeStampedAngularCoordinates datedAC : sample) {
  149.                 sum = sum.add(datedAC.getRotationRate());
  150.             }
  151.             meanRate = new Vector3D(1.0 / sample.size(), sum);
  152.         }
  153.         else {
  154.             TimeStampedAngularCoordinates previous = null;
  155.             for (final TimeStampedAngularCoordinates datedAC : sample) {
  156.                 if (previous != null) {
  157.                     sum = sum.add(TimeStampedAngularCoordinates.estimateRate(previous.getRotation(), datedAC.getRotation(),
  158.                                                                              datedAC.getDate()
  159.                                                                                     .durationFrom(previous.getDate())));
  160.                 }
  161.                 previous = datedAC;
  162.             }
  163.             meanRate = new Vector3D(1.0 / (sample.size() - 1), sum);
  164.         }
  165.         TimeStampedAngularCoordinates offset =
  166.                 new TimeStampedAngularCoordinates(date, Rotation.IDENTITY, meanRate, Vector3D.ZERO);

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

  169.             // offset adaptation parameters
  170.             restart = false;

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

  173.             // add sample points
  174.             double   sign     = 1.0;
  175.             Rotation previous = Rotation.IDENTITY;

  176.             for (final TimeStampedAngularCoordinates ac : sample) {

  177.                 // remove linear offset from the current coordinates
  178.                 final double                        dt    = ac.getDate().durationFrom(date);
  179.                 final TimeStampedAngularCoordinates fixed = ac.subtractOffset(offset.shiftedBy(dt));

  180.                 // make sure all interpolated points will be on the same branch
  181.                 final double dot = MathArrays.linearCombination(fixed.getRotation().getQ0(), previous.getQ0(),
  182.                                                                 fixed.getRotation().getQ1(), previous.getQ1(),
  183.                                                                 fixed.getRotation().getQ2(), previous.getQ2(),
  184.                                                                 fixed.getRotation().getQ3(), previous.getQ3());
  185.                 sign     = FastMath.copySign(1.0, dot * sign);
  186.                 previous = fixed.getRotation();

  187.                 // check modified Rodrigues vector singularity
  188.                 if (fixed.getRotation().getQ0() * sign < threshold) {
  189.                     // the sample point is close to a modified Rodrigues vector singularity
  190.                     // we need to change the linear offset model to avoid this
  191.                     restart = true;
  192.                     break;
  193.                 }

  194.                 final double[][] rodrigues = fixed.getModifiedRodrigues(sign);
  195.                 switch (filter) {
  196.                     case USE_RRA:
  197.                         // populate sample with rotation, rotation rate and acceleration data
  198.                         interpolator.addSamplePoint(dt, rodrigues[0], rodrigues[1], rodrigues[2]);
  199.                         break;
  200.                     case USE_RR:
  201.                         // populate sample with rotation and rotation rate data
  202.                         interpolator.addSamplePoint(dt, rodrigues[0], rodrigues[1]);
  203.                         break;
  204.                     case USE_R:
  205.                         // populate sample with rotation data only
  206.                         interpolator.addSamplePoint(dt, rodrigues[0]);
  207.                         break;
  208.                     default:
  209.                         // this should never happen
  210.                         throw new OrekitInternalError(null);
  211.                 }
  212.             }

  213.             if (restart) {
  214.                 // interpolation failed, some intermediate rotation was too close to 2π
  215.                 // we need to offset all rotations to avoid the singularity
  216.                 offset = offset.addOffset(new AngularCoordinates(new Rotation(Vector3D.PLUS_I,
  217.                                                                               epsilon,
  218.                                                                               RotationConvention.VECTOR_OPERATOR),
  219.                                                                  Vector3D.ZERO, Vector3D.ZERO));
  220.             }
  221.             else {
  222.                 // interpolation succeeded with the current offset
  223.                 final double[][]         p  = interpolator.derivatives(0.0, 2);
  224.                 final AngularCoordinates ac = AngularCoordinates.createFromModifiedRodrigues(p);
  225.                 return new TimeStampedAngularCoordinates(offset.getDate(),
  226.                                                          ac.getRotation(),
  227.                                                          ac.getRotationRate(),
  228.                                                          ac.getRotationAcceleration()).addOffset(offset);
  229.             }

  230.         }

  231.         // this should never happen
  232.         throw new OrekitInternalError(null);
  233.     }
  234. }