TimeStampedPVCoordinates.java

  1. /* Copyright 2002-2016 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 org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
  21. import org.apache.commons.math3.analysis.interpolation.HermiteInterpolator;
  22. import org.apache.commons.math3.geometry.euclidean.threed.FieldVector3D;
  23. import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
  24. import org.apache.commons.math3.util.FastMath;
  25. import org.orekit.errors.OrekitException;
  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.                 throws OrekitException {
  207.                 final TimeStampedPVCoordinates shifted   = shiftedBy(d.durationFrom(date));
  208.                 final Transform                transform = instanceFrame.getTransformTo(f, d);
  209.                 return transform.transformPVCoordinates(shifted);
  210.             }
  211.         };
  212.     }

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

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

  237.         // add sample points
  238.         switch (filter) {
  239.             case USE_P :
  240.                 // populate sample with position data, ignoring velocity
  241.                 for (final TimeStampedPVCoordinates pv : sample) {
  242.                     final Vector3D position = pv.getPosition();
  243.                     interpolator.addSamplePoint(pv.getDate().durationFrom(date),
  244.                                                 new double[] {
  245.                                                               position.getX(), position.getY(), position.getZ()
  246.                                                 });
  247.                 }
  248.                 break;
  249.             case USE_PV :
  250.                 // populate sample with position and velocity data
  251.                 for (final TimeStampedPVCoordinates pv : sample) {
  252.                     final Vector3D position = pv.getPosition();
  253.                     final Vector3D velocity = pv.getVelocity();
  254.                     interpolator.addSamplePoint(pv.getDate().durationFrom(date),
  255.                                                 new double[] {
  256.                                                     position.getX(), position.getY(), position.getZ()
  257.                                                 }, new double[] {
  258.                                                     velocity.getX(), velocity.getY(), velocity.getZ()
  259.                                                 });
  260.                 }
  261.                 break;
  262.             case USE_PVA :
  263.                 // populate sample with position, velocity and acceleration data
  264.                 for (final TimeStampedPVCoordinates pv : sample) {
  265.                     final Vector3D position     = pv.getPosition();
  266.                     final Vector3D velocity     = pv.getVelocity();
  267.                     final Vector3D acceleration = pv.getAcceleration();
  268.                     interpolator.addSamplePoint(pv.getDate().durationFrom(date),
  269.                                                 new double[] {
  270.                                                     position.getX(),     position.getY(),     position.getZ()
  271.                                                 }, new double[] {
  272.                                                     velocity.getX(),     velocity.getY(),     velocity.getZ()
  273.                                                 }, new double[] {
  274.                                                     acceleration.getX(), acceleration.getY(), acceleration.getZ()
  275.                                                 });
  276.                 }
  277.                 break;
  278.             default :
  279.                 // this should never happen
  280.                 throw new OrekitInternalError(null);
  281.         }

  282.         // interpolate
  283.         final DerivativeStructure zero = new DerivativeStructure(1, 2, 0, 0.0);
  284.         final DerivativeStructure[] p  = interpolator.value(zero);

  285.         // build a new interpolated instance
  286.         return new TimeStampedPVCoordinates(date,
  287.                                             new Vector3D(p[0].getValue(),
  288.                                                          p[1].getValue(),
  289.                                                          p[2].getValue()),
  290.                                             new Vector3D(p[0].getPartialDerivative(1),
  291.                                                          p[1].getPartialDerivative(1),
  292.                                                          p[2].getPartialDerivative(1)),
  293.                                             new Vector3D(p[0].getPartialDerivative(2),
  294.                                                          p[1].getPartialDerivative(2),
  295.                                                          p[2].getPartialDerivative(2)));

  296.     }

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

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

  319.     /** Internal class used only for serialization. */
  320.     private static class DTO implements Serializable {

  321.         /** Serializable UID. */
  322.         private static final long serialVersionUID = 20140723L;

  323.         /** Double values. */
  324.         private double[] d;

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

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

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

  338.         }

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

  348.     }

  349. }