TimeStampedFieldAngularCoordinatesHermiteInterpolator.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.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.analysis.interpolation.FieldHermiteInterpolator;
  21. import org.hipparchus.geometry.euclidean.threed.FieldRotation;
  22. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  23. import org.hipparchus.geometry.euclidean.threed.RotationConvention;
  24. import org.hipparchus.util.FastMath;
  25. import org.orekit.errors.OrekitInternalError;
  26. import org.orekit.time.AbstractFieldTimeInterpolator;
  27. import org.orekit.time.FieldAbsoluteDate;

  28. import java.util.List;

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

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

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

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

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

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

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

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

  140.         // Get interpolation date
  141.         final FieldAbsoluteDate<KK> interpolationDate = interpolationData.getInterpolationDate();

  142.         // Get sample
  143.         final List<TimeStampedFieldAngularCoordinates<KK>> sample = interpolationData.getNeighborList();

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

  147.         // set up a linear model canceling mean rotation rate
  148.         final Field<KK> field = interpolationData.getField();
  149.         final FieldVector3D<KK> meanRate;
  150.         FieldVector3D<KK>       sum = FieldVector3D.getZero(field);
  151.         if (filter != AngularDerivativesFilter.USE_R) {
  152.             for (final TimeStampedFieldAngularCoordinates<KK> datedAC : sample) {
  153.                 sum = sum.add(datedAC.getRotationRate());
  154.             }
  155.             meanRate = new FieldVector3D<>(1.0 / sample.size(), sum);
  156.         }
  157.         else {
  158.             TimeStampedFieldAngularCoordinates<KK> previous = null;
  159.             for (final TimeStampedFieldAngularCoordinates<KK> datedAC : sample) {
  160.                 if (previous != null) {
  161.                     sum = sum.add(TimeStampedFieldAngularCoordinates.estimateRate(previous.getRotation(),
  162.                                                                                   datedAC.getRotation(),
  163.                                                                                   datedAC.getDate()
  164.                                                                                          .durationFrom(previous.getDate())));
  165.                 }
  166.                 previous = datedAC;
  167.             }
  168.             meanRate = new FieldVector3D<>(1.0 / (sample.size() - 1), sum);
  169.         }
  170.         TimeStampedFieldAngularCoordinates<KK> offset =
  171.                 new TimeStampedFieldAngularCoordinates<>(interpolationDate, FieldRotation.getIdentity(field),
  172.                                                          meanRate, FieldVector3D.getZero(field));

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

  175.             // offset adaptation parameters
  176.             restart = false;

  177.             // set up an interpolator taking derivatives into account
  178.             final FieldHermiteInterpolator<KK> interpolator = new FieldHermiteInterpolator<>();

  179.             // add sample points
  180.             final KK          one      = interpolationData.getOne();
  181.             double            sign     = 1.0;
  182.             FieldRotation<KK> previous = FieldRotation.getIdentity(field);

  183.             for (final TimeStampedFieldAngularCoordinates<KK> ac : sample) {

  184.                 // remove linear offset from the current coordinates
  185.                 final KK                                     dt    = ac.getDate().durationFrom(interpolationDate);
  186.                 final TimeStampedFieldAngularCoordinates<KK> fixed = ac.subtractOffset(offset.shiftedBy(dt));

  187.                 // make sure all interpolated points will be on the same branch
  188.                 final double dot = one.linearCombination(fixed.getRotation().getQ0(), previous.getQ0(),
  189.                                                          fixed.getRotation().getQ1(), previous.getQ1(),
  190.                                                          fixed.getRotation().getQ2(), previous.getQ2(),
  191.                                                          fixed.getRotation().getQ3(), previous.getQ3()).getReal();
  192.                 sign     = FastMath.copySign(1.0, dot * sign);
  193.                 previous = fixed.getRotation();

  194.                 // check modified Rodrigues vector singularity
  195.                 if (fixed.getRotation().getQ0().getReal() * sign < threshold) {
  196.                     // the sample point is close to a modified Rodrigues vector singularity
  197.                     // we need to change the linear offset model to avoid this
  198.                     restart = true;
  199.                     break;
  200.                 }

  201.                 final KK[][] rodrigues = fixed.getModifiedRodrigues(sign);
  202.                 switch (filter) {
  203.                     case USE_RRA:
  204.                         // populate sample with rotation, rotation rate and acceleration data
  205.                         interpolator.addSamplePoint(dt, rodrigues[0], rodrigues[1], rodrigues[2]);
  206.                         break;
  207.                     case USE_RR:
  208.                         // populate sample with rotation and rotation rate data
  209.                         interpolator.addSamplePoint(dt, rodrigues[0], rodrigues[1]);
  210.                         break;
  211.                     case USE_R:
  212.                         // populate sample with rotation data only
  213.                         interpolator.addSamplePoint(dt, rodrigues[0]);
  214.                         break;
  215.                     default:
  216.                         // this should never happen
  217.                         throw new OrekitInternalError(null);
  218.                 }
  219.             }

  220.             if (restart) {
  221.                 // interpolation failed, some intermediate rotation was too close to 2π
  222.                 // we need to offset all rotations to avoid the singularity
  223.                 offset = offset.addOffset(
  224.                         new FieldAngularCoordinates<>(new FieldRotation<>(FieldVector3D.getPlusI(field),
  225.                                                                           one.newInstance(epsilon),
  226.                                                                           RotationConvention.VECTOR_OPERATOR),
  227.                                                       FieldVector3D.getZero(field), FieldVector3D.getZero(field)));
  228.             } else {
  229.                 // interpolation succeeded with the current offset
  230.                 final KK                          zero = interpolationData.getZero();
  231.                 final KK[][]                      p    = interpolator.derivatives(zero, 2);
  232.                 final FieldAngularCoordinates<KK> ac   = FieldAngularCoordinates.createFromModifiedRodrigues(p);
  233.                 return new TimeStampedFieldAngularCoordinates<>(offset.getDate(),
  234.                                                                 ac.getRotation(),
  235.                                                                 ac.getRotationRate(),
  236.                                                                 ac.getRotationAcceleration()).addOffset(offset);
  237.             }

  238.         }

  239.         // this should never happen
  240.         throw new OrekitInternalError(null);
  241.     }
  242. }