TimeStampedPVCoordinates.java

  1. /* Copyright 2002-2019 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.io.Serializable;
  19. import java.util.Collection;
  20. import java.util.stream.Stream;

  21. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  22. import org.hipparchus.analysis.interpolation.HermiteInterpolator;
  23. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  24. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  25. import org.hipparchus.util.FastMath;
  26. import org.orekit.errors.OrekitInternalError;
  27. import org.orekit.frames.Frame;
  28. import org.orekit.frames.Transform;
  29. import org.orekit.time.AbsoluteDate;
  30. import org.orekit.time.TimeStamped;

  31. /** {@link TimeStamped time-stamped} version of {@link PVCoordinates}.
  32.  * <p>Instances of this class are guaranteed to be immutable.</p>
  33.  * @author Luc Maisonobe
  34.  * @since 7.0
  35.  */
  36. public class TimeStampedPVCoordinates extends PVCoordinates implements TimeStamped {

  37.     /** Serializable UID. */
  38.     private static final long serialVersionUID = 20140723L;

  39.     /** The date. */
  40.     private final AbsoluteDate date;

  41.     /** Builds a TimeStampedPVCoordinates pair.
  42.      * @param date coordinates date
  43.      * @param position the position vector (m)
  44.      * @param velocity the velocity vector (m/s)
  45.      * @param acceleration the acceleration vector (m/s²)
  46.      */
  47.     public TimeStampedPVCoordinates(final AbsoluteDate date,
  48.                                     final Vector3D position, final Vector3D velocity, final Vector3D acceleration) {
  49.         super(position, velocity, acceleration);
  50.         this.date = date;
  51.     }

  52.     /**
  53.      * Build from position and velocity. Acceleration is set to zero.
  54.      *
  55.      * @param date coordinates date
  56.      * @param position the position vector (m)
  57.      * @param velocity the velocity vector (m/s)
  58.      */
  59.     public TimeStampedPVCoordinates(final AbsoluteDate date,
  60.                                     final Vector3D position,
  61.                                     final Vector3D velocity) {
  62.         this(date, position, velocity, Vector3D.ZERO);
  63.     }

  64.     /**
  65.      * Build from position velocity acceleration coordinates.
  66.      *
  67.      * @param date coordinates date
  68.      * @param pv position velocity, and acceleration coordinates, in meters and seconds.
  69.      */
  70.     public TimeStampedPVCoordinates(final AbsoluteDate date, final PVCoordinates pv) {
  71.         this(date, pv.getPosition(), pv.getVelocity(), pv.getAcceleration());
  72.     }

  73.     /** Multiplicative constructor
  74.      * <p>Build a TimeStampedPVCoordinates from another one and a scale factor.</p>
  75.      * <p>The TimeStampedPVCoordinates built will be a * pv</p>
  76.      * @param date date of the built coordinates
  77.      * @param a scale factor
  78.      * @param pv base (unscaled) PVCoordinates
  79.      */
  80.     public TimeStampedPVCoordinates(final AbsoluteDate date,
  81.                                     final double a, final PVCoordinates pv) {
  82.         super(new Vector3D(a, pv.getPosition()),
  83.               new Vector3D(a, pv.getVelocity()),
  84.               new Vector3D(a, pv.getAcceleration()));
  85.         this.date = date;
  86.     }

  87.     /** Subtractive constructor
  88.      * <p>Build a relative TimeStampedPVCoordinates from a start and an end position.</p>
  89.      * <p>The TimeStampedPVCoordinates built will be end - start.</p>
  90.      * @param date date of the built coordinates
  91.      * @param start Starting PVCoordinates
  92.      * @param end ending PVCoordinates
  93.      */
  94.     public TimeStampedPVCoordinates(final AbsoluteDate date,
  95.                                     final PVCoordinates start, final PVCoordinates end) {
  96.         super(end.getPosition().subtract(start.getPosition()),
  97.               end.getVelocity().subtract(start.getVelocity()),
  98.               end.getAcceleration().subtract(start.getAcceleration()));
  99.         this.date = date;
  100.     }

  101.     /** Linear constructor
  102.      * <p>Build a TimeStampedPVCoordinates from two other ones and corresponding scale factors.</p>
  103.      * <p>The TimeStampedPVCoordinates built will be a1 * u1 + a2 * u2</p>
  104.      * @param date date of the built coordinates
  105.      * @param a1 first scale factor
  106.      * @param pv1 first base (unscaled) PVCoordinates
  107.      * @param a2 second scale factor
  108.      * @param pv2 second base (unscaled) PVCoordinates
  109.      */
  110.     public TimeStampedPVCoordinates(final AbsoluteDate date,
  111.                                     final double a1, final PVCoordinates pv1,
  112.                                     final double a2, final PVCoordinates pv2) {
  113.         super(new Vector3D(a1, pv1.getPosition(),     a2, pv2.getPosition()),
  114.               new Vector3D(a1, pv1.getVelocity(),     a2, pv2.getVelocity()),
  115.               new Vector3D(a1, pv1.getAcceleration(), a2, pv2.getAcceleration()));
  116.         this.date = date;
  117.     }

  118.     /** Linear constructor
  119.      * <p>Build a TimeStampedPVCoordinates from three other ones and corresponding scale factors.</p>
  120.      * <p>The TimeStampedPVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
  121.      * @param date date of the built coordinates
  122.      * @param a1 first scale factor
  123.      * @param pv1 first base (unscaled) PVCoordinates
  124.      * @param a2 second scale factor
  125.      * @param pv2 second base (unscaled) PVCoordinates
  126.      * @param a3 third scale factor
  127.      * @param pv3 third base (unscaled) PVCoordinates
  128.      */
  129.     public TimeStampedPVCoordinates(final AbsoluteDate date,
  130.                                     final double a1, final PVCoordinates pv1,
  131.                                     final double a2, final PVCoordinates pv2,
  132.                                     final double a3, final PVCoordinates pv3) {
  133.         super(new Vector3D(a1, pv1.getPosition(),     a2, pv2.getPosition(),     a3, pv3.getPosition()),
  134.               new Vector3D(a1, pv1.getVelocity(),     a2, pv2.getVelocity(),     a3, pv3.getVelocity()),
  135.               new Vector3D(a1, pv1.getAcceleration(), a2, pv2.getAcceleration(), a3, pv3.getAcceleration()));
  136.         this.date = date;
  137.     }

  138.     /** Linear constructor
  139.      * <p>Build a TimeStampedPVCoordinates from four other ones and corresponding scale factors.</p>
  140.      * <p>The TimeStampedPVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
  141.      * @param date date of the built coordinates
  142.      * @param a1 first scale factor
  143.      * @param pv1 first base (unscaled) PVCoordinates
  144.      * @param a2 second scale factor
  145.      * @param pv2 second base (unscaled) PVCoordinates
  146.      * @param a3 third scale factor
  147.      * @param pv3 third base (unscaled) PVCoordinates
  148.      * @param a4 fourth scale factor
  149.      * @param pv4 fourth base (unscaled) PVCoordinates
  150.      */
  151.     public TimeStampedPVCoordinates(final AbsoluteDate date,
  152.                                     final double a1, final PVCoordinates pv1,
  153.                                     final double a2, final PVCoordinates pv2,
  154.                                     final double a3, final PVCoordinates pv3,
  155.                                     final double a4, final PVCoordinates pv4) {
  156.         super(new Vector3D(a1, pv1.getPosition(),     a2, pv2.getPosition(),     a3, pv3.getPosition(),     a4, pv4.getPosition()),
  157.               new Vector3D(a1, pv1.getVelocity(),     a2, pv2.getVelocity(),     a3, pv3.getVelocity(),     a4, pv4.getVelocity()),
  158.               new Vector3D(a1, pv1.getAcceleration(), a2, pv2.getAcceleration(), a3, pv3.getAcceleration(), a4, pv4.getAcceleration()));
  159.         this.date = date;
  160.     }

  161.     /** Builds a TimeStampedPVCoordinates triplet from  a {@link FieldVector3D}&lt;{@link DerivativeStructure}&gt;.
  162.      * <p>
  163.      * The vector components must have time as their only derivation parameter and
  164.      * have consistent derivation orders.
  165.      * </p>
  166.      * @param date date of the built coordinates
  167.      * @param p vector with time-derivatives embedded within the coordinates
  168.      */
  169.     public TimeStampedPVCoordinates(final AbsoluteDate date,
  170.                                     final FieldVector3D<DerivativeStructure> p) {
  171.         super(p);
  172.         this.date = date;
  173.     }

  174.     /** {@inheritDoc} */
  175.     public AbsoluteDate getDate() {
  176.         return date;
  177.     }

  178.     /** Get a time-shifted state.
  179.      * <p>
  180.      * The state can be slightly shifted to close dates. This shift is based on
  181.      * a simple Taylor expansion. It is <em>not</em> intended as a replacement for
  182.      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  183.      * for either small time shifts or coarse accuracy.
  184.      * </p>
  185.      * @param dt time shift in seconds
  186.      * @return a new state, shifted with respect to the instance (which is immutable)
  187.      */
  188.     public TimeStampedPVCoordinates shiftedBy(final double dt) {
  189.         final PVCoordinates spv = super.shiftedBy(dt);
  190.         return new TimeStampedPVCoordinates(date.shiftedBy(dt),
  191.                                             spv.getPosition(), spv.getVelocity(), spv.getAcceleration());
  192.     }

  193.     /** Create a local provider using simply Taylor expansion through {@link #shiftedBy(double)}.
  194.      * <p>
  195.      * The time evolution is based on a simple Taylor expansion. It is <em>not</em> intended as a
  196.      * replacement for proper orbit propagation (it is not even Keplerian!) but should be sufficient
  197.      * for either small time shifts or coarse accuracy.
  198.      * </p>
  199.      * @param instanceFrame frame in which the instance is defined
  200.      * @return provider based on Taylor expansion, for small time shifts around instance date
  201.      */
  202.     public PVCoordinatesProvider toTaylorProvider(final Frame instanceFrame) {
  203.         return new PVCoordinatesProvider() {
  204.             /** {@inheritDoc} */
  205.             public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate d,  final Frame f) {
  206.                 final TimeStampedPVCoordinates shifted   = shiftedBy(d.durationFrom(date));
  207.                 final Transform                transform = instanceFrame.getTransformTo(f, d);
  208.                 return transform.transformPVCoordinates(shifted);
  209.             }
  210.         };
  211.     }

  212.     /** Interpolate position-velocity.
  213.      * <p>
  214.      * The interpolated instance is created by polynomial Hermite interpolation
  215.      * ensuring velocity remains the exact derivative of position.
  216.      * </p>
  217.      * <p>
  218.      * Note that even if first time derivatives (velocities)
  219.      * from sample can be ignored, the interpolated instance always includes
  220.      * interpolated derivatives. This feature can be used explicitly to
  221.      * compute these derivatives when it would be too complex to compute them
  222.      * from an analytical formula: just compute a few sample points from the
  223.      * explicit formula and set the derivatives to zero in these sample points,
  224.      * then use interpolation to add derivatives consistent with the positions.
  225.      * </p>
  226.      * @param date interpolation date
  227.      * @param filter filter for derivatives from the sample to use in interpolation
  228.      * @param sample sample points on which interpolation should be done
  229.      * @return a new position-velocity, interpolated at specified date
  230.      */
  231.     public static TimeStampedPVCoordinates interpolate(final AbsoluteDate date,
  232.                                                        final CartesianDerivativesFilter filter,
  233.                                                        final Collection<TimeStampedPVCoordinates> sample) {
  234.         return interpolate(date, filter, sample.stream());
  235.     }

  236.     /** Interpolate position-velocity.
  237.      * <p>
  238.      * The interpolated instance is created by polynomial Hermite interpolation
  239.      * ensuring velocity remains the exact derivative of position.
  240.      * </p>
  241.      * <p>
  242.      * Note that even if first time derivatives (velocities)
  243.      * from sample can be ignored, the interpolated instance always includes
  244.      * interpolated derivatives. This feature can be used explicitly to
  245.      * compute these derivatives when it would be too complex to compute them
  246.      * from an analytical formula: just compute a few sample points from the
  247.      * explicit formula and set the derivatives to zero in these sample points,
  248.      * then use interpolation to add derivatives consistent with the positions.
  249.      * </p>
  250.      * @param date interpolation date
  251.      * @param filter filter for derivatives from the sample to use in interpolation
  252.      * @param sample sample points on which interpolation should be done
  253.      * @return a new position-velocity, interpolated at specified date
  254.      * @since 9.0
  255.      */
  256.     public static TimeStampedPVCoordinates interpolate(final AbsoluteDate date,
  257.                                                        final CartesianDerivativesFilter filter,
  258.                                                        final Stream<TimeStampedPVCoordinates> sample) {

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

  261.         // add sample points
  262.         switch (filter) {
  263.             case USE_P :
  264.                 // populate sample with position data, ignoring velocity
  265.                 sample.forEach(pv -> {
  266.                     final Vector3D position = pv.getPosition();
  267.                     interpolator.addSamplePoint(pv.getDate().durationFrom(date),
  268.                                                 position.toArray());
  269.                 });
  270.                 break;
  271.             case USE_PV :
  272.                 // populate sample with position and velocity data
  273.                 sample.forEach(pv -> {
  274.                     final Vector3D position = pv.getPosition();
  275.                     final Vector3D velocity = pv.getVelocity();
  276.                     interpolator.addSamplePoint(pv.getDate().durationFrom(date),
  277.                                                 position.toArray(), velocity.toArray());
  278.                 });
  279.                 break;
  280.             case USE_PVA :
  281.                 // populate sample with position, velocity and acceleration data
  282.                 sample.forEach(pv -> {
  283.                     final Vector3D position     = pv.getPosition();
  284.                     final Vector3D velocity     = pv.getVelocity();
  285.                     final Vector3D acceleration = pv.getAcceleration();
  286.                     interpolator.addSamplePoint(pv.getDate().durationFrom(date),
  287.                                                 position.toArray(), velocity.toArray(), acceleration.toArray());
  288.                 });
  289.                 break;
  290.             default :
  291.                 // this should never happen
  292.                 throw new OrekitInternalError(null);
  293.         }

  294.         // interpolate
  295.         final double[][] p = interpolator.derivatives(0.0, 2);

  296.         // build a new interpolated instance
  297.         return new TimeStampedPVCoordinates(date, new Vector3D(p[0]), new Vector3D(p[1]), new Vector3D(p[2]));

  298.     }

  299.     /** Return a string representation of this position/velocity pair.
  300.      * @return string representation of this position/velocity pair
  301.      */
  302.     public String toString() {
  303.         final String comma = ", ";
  304.         return new StringBuffer().append('{').append(date).append(", P(").
  305.                                   append(getPosition().getX()).append(comma).
  306.                                   append(getPosition().getY()).append(comma).
  307.                                   append(getPosition().getZ()).append("), V(").
  308.                                   append(getVelocity().getX()).append(comma).
  309.                                   append(getVelocity().getY()).append(comma).
  310.                                   append(getVelocity().getZ()).append("), A(").
  311.                                   append(getAcceleration().getX()).append(comma).
  312.                                   append(getAcceleration().getY()).append(comma).
  313.                                   append(getAcceleration().getZ()).append(")}").toString();
  314.     }

  315.     /** Replace the instance with a data transfer object for serialization.
  316.      * @return data transfer object that will be serialized
  317.      */
  318.     private Object writeReplace() {
  319.         return new DTO(this);
  320.     }

  321.     /** Internal class used only for serialization. */
  322.     private static class DTO implements Serializable {

  323.         /** Serializable UID. */
  324.         private static final long serialVersionUID = 20140723L;

  325.         /** Double values. */
  326.         private double[] d;

  327.         /** Simple constructor.
  328.          * @param pv instance to serialize
  329.          */
  330.         private DTO(final TimeStampedPVCoordinates pv) {

  331.             // decompose date
  332.             final double epoch  = FastMath.floor(pv.getDate().durationFrom(AbsoluteDate.J2000_EPOCH));
  333.             final double offset = pv.getDate().durationFrom(AbsoluteDate.J2000_EPOCH.shiftedBy(epoch));

  334.             this.d = new double[] {
  335.                 epoch, offset,
  336.                 pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
  337.                 pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ(),
  338.                 pv.getAcceleration().getX(), pv.getAcceleration().getY(), pv.getAcceleration().getZ()
  339.             };

  340.         }

  341.         /** Replace the deserialized data transfer object with a {@link TimeStampedPVCoordinates}.
  342.          * @return replacement {@link TimeStampedPVCoordinates}
  343.          */
  344.         private Object readResolve() {
  345.             return new TimeStampedPVCoordinates(AbsoluteDate.J2000_EPOCH.shiftedBy(d[0]).shiftedBy(d[1]),
  346.                                                 new Vector3D(d[2], d[3], d[ 4]),
  347.                                                 new Vector3D(d[5], d[6], d[ 7]),
  348.                                                 new Vector3D(d[8], d[9], d[10]));
  349.         }

  350.     }

  351. }