TimeStampedPVCoordinates.java

  1. /* Copyright 2002-2018 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.OrekitException;
  27. import org.orekit.errors.OrekitInternalError;
  28. import org.orekit.frames.Frame;
  29. import org.orekit.frames.Transform;
  30. import org.orekit.time.AbsoluteDate;
  31. import org.orekit.time.TimeStamped;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  300.     }

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

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

  323.     /** Internal class used only for serialization. */
  324.     private static class DTO implements Serializable {

  325.         /** Serializable UID. */
  326.         private static final long serialVersionUID = 20140723L;

  327.         /** Double values. */
  328.         private double[] d;

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

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

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

  342.         }

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

  352.     }

  353. }