TimeStampedPVCoordinatesHermiteInterpolator.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.Vector3D;
  20. import org.orekit.errors.OrekitInternalError;
  21. import org.orekit.time.AbsoluteDate;
  22. import org.orekit.time.AbstractTimeInterpolator;

  23. import java.util.stream.Stream;

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

  37.     /** Filter for derivatives from the sample to use in interpolation. */
  38.     private final CartesianDerivativesFilter filter;

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

  53.     /**
  54.      * Constructor with :
  55.      * <ul>
  56.      *     <li>Default extrapolation threshold value ({@code DEFAULT_EXTRAPOLATION_THRESHOLD_SEC} s)</li>
  57.      *     <li>Use of position and both time derivatives for attitude interpolation</li>
  58.      * </ul>
  59.      * As this implementation of interpolation is polynomial, it should be used only with small number of interpolation
  60.      * points (about 10-20 points) in order to avoid <a href="http://en.wikipedia.org/wiki/Runge%27s_phenomenon">Runge's
  61.      * phenomenon</a> and numerical problems (including NaN appearing).
  62.      *
  63.      * @param interpolationPoints number of interpolation points
  64.      */
  65.     public TimeStampedPVCoordinatesHermiteInterpolator(final int interpolationPoints) {
  66.         this(interpolationPoints, CartesianDerivativesFilter.USE_PVA);
  67.     }

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

  82.         this(interpolationPoints, DEFAULT_EXTRAPOLATION_THRESHOLD_SEC, filter);
  83.     }

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

  101.     /** Get filter for derivatives from the sample to use in interpolation.
  102.      * @return filter for derivatives from the sample to use in interpolation
  103.      */
  104.     public CartesianDerivativesFilter getFilter() {
  105.         return filter;
  106.     }

  107.     /**
  108.      * {@inheritDoc}
  109.      * <p>
  110.      * The interpolated instance is created by polynomial Hermite interpolation ensuring velocity remains the exact
  111.      * derivative of position.
  112.      * <p>
  113.      * Note that even if first time derivatives (velocities) from sample can be ignored, the interpolated instance always
  114.      * includes interpolated derivatives. This feature can be used explicitly to compute these derivatives when it would be
  115.      * too complex to compute them from an analytical formula: just compute a few sample points from the explicit formula and
  116.      * set the derivatives to zero in these sample points, then use interpolation to add derivatives consistent with the
  117.      * positions.
  118.      */
  119.     @Override
  120.     protected TimeStampedPVCoordinates interpolate(final InterpolationData interpolationData) {

  121.         // Get date
  122.         final AbsoluteDate date = interpolationData.getInterpolationDate();

  123.         // Convert sample to stream
  124.         final Stream<TimeStampedPVCoordinates> sample = interpolationData.getNeighborList().stream();

  125.         // Set up an interpolator taking derivatives into account
  126.         final HermiteInterpolator interpolator = new HermiteInterpolator();

  127.         // Add sample points
  128.         switch (filter) {
  129.             case USE_P:
  130.                 // populate sample with position data, ignoring velocity
  131.                 sample.forEach(pv -> {
  132.                     final Vector3D position = pv.getPosition();
  133.                     interpolator.addSamplePoint(pv.getDate().durationFrom(date),
  134.                                                 position.toArray());
  135.                 });
  136.                 break;
  137.             case USE_PV:
  138.                 // populate sample with position and velocity data
  139.                 sample.forEach(pv -> {
  140.                     final Vector3D position = pv.getPosition();
  141.                     final Vector3D velocity = pv.getVelocity();
  142.                     interpolator.addSamplePoint(pv.getDate().durationFrom(date),
  143.                                                 position.toArray(), velocity.toArray());
  144.                 });
  145.                 break;
  146.             case USE_PVA:
  147.                 // populate sample with position, velocity and acceleration data
  148.                 sample.forEach(pv -> {
  149.                     final Vector3D position     = pv.getPosition();
  150.                     final Vector3D velocity     = pv.getVelocity();
  151.                     final Vector3D acceleration = pv.getAcceleration();
  152.                     interpolator.addSamplePoint(pv.getDate().durationFrom(date),
  153.                                                 position.toArray(), velocity.toArray(), acceleration.toArray());
  154.                 });
  155.                 break;
  156.             default:
  157.                 // this should never happen
  158.                 throw new OrekitInternalError(null);
  159.         }

  160.         // Interpolate
  161.         final double[][] pva = interpolator.derivatives(0.0, 2);

  162.         // Build a new interpolated instance
  163.         return new TimeStampedPVCoordinates(date, new Vector3D(pva[0]), new Vector3D(pva[1]), new Vector3D(pva[2]));
  164.     }
  165. }