TimeStampedPVCoordinates.java

  1. /* Copyright 2002-2024 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 java.io.Serializable;

  19. import org.hipparchus.analysis.differentiation.Derivative;
  20. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  21. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  22. import org.hipparchus.util.FastMath;
  23. import org.orekit.annotation.DefaultDataContext;
  24. import org.orekit.data.DataContext;
  25. import org.orekit.frames.Frame;
  26. import org.orekit.frames.StaticTransform;
  27. import org.orekit.frames.Transform;
  28. import org.orekit.time.AbsoluteDate;
  29. import org.orekit.time.TimeScale;
  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 Derivative}&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.      * @param <U> type of the derivative
  169.      */
  170.     public <U extends Derivative<U>> TimeStampedPVCoordinates(final AbsoluteDate date, final FieldVector3D<U> 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 Vector3D getPosition(final AbsoluteDate d,  final Frame f) {
  206.                 final TimeStampedPVCoordinates shifted   = shiftedBy(d.durationFrom(getDate()));
  207.                 final StaticTransform          transform = instanceFrame.getStaticTransformTo(f, d);
  208.                 return transform.transformPosition(shifted.getPosition());
  209.             }
  210.             /** {@inheritDoc} */
  211.             public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate d,  final Frame f) {
  212.                 final TimeStampedPVCoordinates shifted   = shiftedBy(d.durationFrom(date));
  213.                 final Transform                transform = instanceFrame.getTransformTo(f, d);
  214.                 return transform.transformPVCoordinates(shifted);
  215.             }
  216.         };
  217.     }

  218.     /** Return a string representation of this date, position, velocity, and acceleration.
  219.      *
  220.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  221.      *
  222.      * @return string representation of this.
  223.      */
  224.     @Override
  225.     @DefaultDataContext
  226.     public String toString() {
  227.         return toString(DataContext.getDefault().getTimeScales().getUTC());
  228.     }

  229.     /**
  230.      * Return a string representation of this date, position, velocity, and acceleration.
  231.      *
  232.      * @param utc time scale used to print the date.
  233.      * @return string representation of this.
  234.      */
  235.     public String toString(final TimeScale utc) {
  236.         final String comma = ", ";
  237.         return new StringBuilder().append('{').
  238.                                   append(date.toString(utc)).append(", P(").
  239.                                   append(getPosition().getX()).append(comma).
  240.                                   append(getPosition().getY()).append(comma).
  241.                                   append(getPosition().getZ()).append("), V(").
  242.                                   append(getVelocity().getX()).append(comma).
  243.                                   append(getVelocity().getY()).append(comma).
  244.                                   append(getVelocity().getZ()).append("), A(").
  245.                                   append(getAcceleration().getX()).append(comma).
  246.                                   append(getAcceleration().getY()).append(comma).
  247.                                   append(getAcceleration().getZ()).append(")}").toString();
  248.     }

  249.     /** Replace the instance with a data transfer object for serialization.
  250.      * @return data transfer object that will be serialized
  251.      */
  252.     @DefaultDataContext
  253.     private Object writeReplace() {
  254.         return new DTO(this);
  255.     }

  256.     /** Internal class used only for serialization. */
  257.     @DefaultDataContext
  258.     private static class DTO implements Serializable {

  259.         /** Serializable UID. */
  260.         private static final long serialVersionUID = 20140723L;

  261.         /** Double values. */
  262.         private double[] d;

  263.         /** Simple constructor.
  264.          * @param pv instance to serialize
  265.          */
  266.         private DTO(final TimeStampedPVCoordinates pv) {

  267.             // decompose date
  268.             final AbsoluteDate j2000Epoch =
  269.                     DataContext.getDefault().getTimeScales().getJ2000Epoch();
  270.             final double epoch  = FastMath.floor(pv.getDate().durationFrom(j2000Epoch));
  271.             final double offset = pv.getDate().durationFrom(j2000Epoch.shiftedBy(epoch));

  272.             this.d = new double[] {
  273.                 epoch, offset,
  274.                 pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
  275.                 pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ(),
  276.                 pv.getAcceleration().getX(), pv.getAcceleration().getY(), pv.getAcceleration().getZ()
  277.             };

  278.         }

  279.         /** Replace the deserialized data transfer object with a {@link TimeStampedPVCoordinates}.
  280.          * @return replacement {@link TimeStampedPVCoordinates}
  281.          */
  282.         private Object readResolve() {
  283.             final AbsoluteDate j2000Epoch =
  284.                     DataContext.getDefault().getTimeScales().getJ2000Epoch();
  285.             return new TimeStampedPVCoordinates(j2000Epoch.shiftedBy(d[0]).shiftedBy(d[1]),
  286.                                                 new Vector3D(d[2], d[3], d[ 4]),
  287.                                                 new Vector3D(d[5], d[6], d[ 7]),
  288.                                                 new Vector3D(d[8], d[9], d[10]));
  289.         }

  290.     }

  291. }