FieldAbsolutePVCoordinatesHermiteInterpolator.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.analysis.interpolation.FieldHermiteInterpolator;
  20. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  21. import org.orekit.errors.OrekitInternalError;
  22. import org.orekit.frames.Frame;
  23. import org.orekit.time.AbstractFieldTimeInterpolator;
  24. import org.orekit.time.FieldAbsoluteDate;

  25. import java.util.List;

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

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

  43.     /** Output frame for the interpolated instance. */
  44.     private final Frame outputFrame;

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

  61.     /**
  62.      * Constructor with :
  63.      * <ul>
  64.      *     <li>Default extrapolation threshold value ({@code DEFAULT_EXTRAPOLATION_THRESHOLD_SEC} s)</li>
  65.      *     <li>Use of position and two time derivatives during 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.      * @param outputFrame frame for the interpolated instance
  73.      */
  74.     public FieldAbsolutePVCoordinatesHermiteInterpolator(final int interpolationPoints, final Frame outputFrame) {
  75.         this(interpolationPoints, outputFrame, CartesianDerivativesFilter.USE_PVA);
  76.     }

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

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

  112.     /** Get filter for derivatives from the sample to use in interpolation.
  113.      * @return filter for derivatives from the sample to use in interpolation
  114.      */
  115.     public CartesianDerivativesFilter getFilter() {
  116.         return filter;
  117.     }

  118.     /** Get output frame for the interpolated instance.
  119.      * @return output frame for the interpolated instance
  120.      */
  121.     public Frame getOutputFrame() {
  122.         return outputFrame;
  123.     }

  124.     /**
  125.      * {@inheritDoc}
  126.      * <p>
  127.      * The interpolated instance is created by polynomial Hermite interpolation ensuring velocity remains the exact
  128.      * derivative of position.
  129.      * <p>
  130.      * Note that even if first time derivatives (velocities) from sample can be ignored, the interpolated instance always
  131.      * includes interpolated derivatives. This feature can be used explicitly to compute these derivatives when it would be
  132.      * too complex to compute them from an analytical formula: just compute a few sample points from the explicit formula and
  133.      * set the derivatives to zero in these sample points, then use interpolation to add derivatives consistent with the
  134.      * positions.
  135.      */
  136.     @Override
  137.     protected FieldAbsolutePVCoordinates<KK> interpolate(final InterpolationData interpolationData) {

  138.         // Get interpolation date
  139.         final FieldAbsoluteDate<KK> date = interpolationData.getInterpolationDate();

  140.         // Get sample
  141.         final List<FieldAbsolutePVCoordinates<KK>> sample = interpolationData.getNeighborList();

  142.         // Set up an interpolator taking derivatives into account
  143.         final FieldHermiteInterpolator<KK> interpolator = new FieldHermiteInterpolator<>();

  144.         // Add sample points
  145.         switch (filter) {
  146.             case USE_P:
  147.                 // Populate sample with position data, ignoring velocity
  148.                 sample.forEach(pv -> {
  149.                     final FieldVector3D<KK> position = pv.getPosition();
  150.                     interpolator.addSamplePoint(pv.getDate().durationFrom(date),
  151.                                                 position.toArray());
  152.                 });
  153.                 break;
  154.             case USE_PV:
  155.                 // Populate sample with position and velocity data
  156.                 sample.forEach(pv -> {
  157.                     final FieldVector3D<KK> position = pv.getPosition();
  158.                     final FieldVector3D<KK> velocity = pv.getVelocity();
  159.                     interpolator.addSamplePoint(pv.getDate().durationFrom(date),
  160.                                                 position.toArray(), velocity.toArray());
  161.                 });
  162.                 break;
  163.             case USE_PVA:
  164.                 // Populate sample with position, velocity and acceleration data
  165.                 sample.forEach(pv -> {
  166.                     final FieldVector3D<KK> position     = pv.getPosition();
  167.                     final FieldVector3D<KK> velocity     = pv.getVelocity();
  168.                     final FieldVector3D<KK> acceleration = pv.getAcceleration();
  169.                     interpolator.addSamplePoint(pv.getDate().durationFrom(date),
  170.                                                 position.toArray(), velocity.toArray(), acceleration.toArray());
  171.                 });
  172.                 break;
  173.             default:
  174.                 // this should never happen
  175.                 throw new OrekitInternalError(null);
  176.         }

  177.         // interpolate
  178.         final KK     zero = interpolationData.getZero();
  179.         final KK[][] p    = interpolator.derivatives(zero, 2);

  180.         // build a new interpolated instance
  181.         return new FieldAbsolutePVCoordinates<>(outputFrame, date, new FieldVector3D<>(p[0]), new FieldVector3D<>(p[1]),
  182.                                                 new FieldVector3D<>(p[2]));
  183.     }
  184. }