PVCoordinates.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.ArrayList;
  20. import java.util.Collection;
  21. import java.util.List;

  22. import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
  23. import org.apache.commons.math3.geometry.euclidean.threed.FieldVector3D;
  24. import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
  25. import org.apache.commons.math3.util.Pair;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.errors.OrekitMessages;
  28. import org.orekit.time.AbsoluteDate;
  29. import org.orekit.time.TimeShiftable;

  30. /** Simple container for Position/Velocity/Acceleration triplets.
  31.  * <p>
  32.  * The state can be slightly shifted to close dates. This shift is based on
  33.  * a simple quadratic model. It is <em>not</em> intended as a replacement for
  34.  * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  35.  * for either small time shifts or coarse accuracy.
  36.  * </p>
  37.  * <p>
  38.  * This class is the angular counterpart to {@link AngularCoordinates}.
  39.  * </p>
  40.  * <p>Instances of this class are guaranteed to be immutable.</p>
  41.  * @author Fabien Maussion
  42.  * @author Luc Maisonobe
  43.  */
  44. public class PVCoordinates implements TimeShiftable<PVCoordinates>, Serializable {

  45.     /** Fixed position/velocity at origin (both p, v and a are zero vectors). */
  46.     public static final PVCoordinates ZERO = new PVCoordinates(Vector3D.ZERO, Vector3D.ZERO, Vector3D.ZERO);

  47.     /** Serializable UID. */
  48.     private static final long serialVersionUID = 20140407L;

  49.     /** The position. */
  50.     private final Vector3D position;

  51.     /** The velocity. */
  52.     private final Vector3D velocity;

  53.     /** The acceleration. */
  54.     private final Vector3D acceleration;

  55.     /** Simple constructor.
  56.      * <p> Set the Coordinates to default : (0 0 0), (0 0 0), (0 0 0).</p>
  57.      */
  58.     public PVCoordinates() {
  59.         position     = Vector3D.ZERO;
  60.         velocity     = Vector3D.ZERO;
  61.         acceleration = Vector3D.ZERO;
  62.     }

  63.     /** Builds a PVCoordinates triplet with zero acceleration.
  64.      * <p>Acceleration is set to zero</p>
  65.      * @param position the position vector (m)
  66.      * @param velocity the velocity vector (m/s)
  67.      */
  68.     public PVCoordinates(final Vector3D position, final Vector3D velocity) {
  69.         this.position     = position;
  70.         this.velocity     = velocity;
  71.         this.acceleration = Vector3D.ZERO;
  72.     }

  73.     /** Builds a PVCoordinates triplet.
  74.      * @param position the position vector (m)
  75.      * @param velocity the velocity vector (m/s)
  76.      * @param acceleration the acceleration vector (m/s²)
  77.      */
  78.     public PVCoordinates(final Vector3D position, final Vector3D velocity, final Vector3D acceleration) {
  79.         this.position     = position;
  80.         this.velocity     = velocity;
  81.         this.acceleration = acceleration;
  82.     }

  83.     /** Multiplicative constructor.
  84.      * <p>Build a PVCoordinates from another one and a scale factor.</p>
  85.      * <p>The PVCoordinates built will be a * pv</p>
  86.      * @param a scale factor
  87.      * @param pv base (unscaled) PVCoordinates
  88.      */
  89.     public PVCoordinates(final double a, final PVCoordinates pv) {
  90.         position     = new Vector3D(a, pv.position);
  91.         velocity     = new Vector3D(a, pv.velocity);
  92.         acceleration = new Vector3D(a, pv.acceleration);
  93.     }

  94.     /** Subtractive constructor.
  95.      * <p>Build a relative PVCoordinates from a start and an end position.</p>
  96.      * <p>The PVCoordinates built will be end - start.</p>
  97.      * @param start Starting PVCoordinates
  98.      * @param end ending PVCoordinates
  99.      */
  100.     public PVCoordinates(final PVCoordinates start, final PVCoordinates end) {
  101.         this.position     = end.position.subtract(start.position);
  102.         this.velocity     = end.velocity.subtract(start.velocity);
  103.         this.acceleration = end.acceleration.subtract(start.acceleration);
  104.     }

  105.     /** Linear constructor.
  106.      * <p>Build a PVCoordinates from two other ones and corresponding scale factors.</p>
  107.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2</p>
  108.      * @param a1 first scale factor
  109.      * @param pv1 first base (unscaled) PVCoordinates
  110.      * @param a2 second scale factor
  111.      * @param pv2 second base (unscaled) PVCoordinates
  112.      */
  113.     public PVCoordinates(final double a1, final PVCoordinates pv1,
  114.                          final double a2, final PVCoordinates pv2) {
  115.         position     = new Vector3D(a1, pv1.position,     a2, pv2.position);
  116.         velocity     = new Vector3D(a1, pv1.velocity,     a2, pv2.velocity);
  117.         acceleration = new Vector3D(a1, pv1.acceleration, a2, pv2.acceleration);
  118.     }

  119.     /** Linear constructor.
  120.      * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
  121.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
  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 PVCoordinates(final double a1, final PVCoordinates pv1,
  130.                          final double a2, final PVCoordinates pv2,
  131.                          final double a3, final PVCoordinates pv3) {
  132.         position     = new Vector3D(a1, pv1.position,     a2, pv2.position,     a3, pv3.position);
  133.         velocity     = new Vector3D(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity);
  134.         acceleration = new Vector3D(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration);
  135.     }

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

  159.     /** Builds a PVCoordinates triplet from  a {@link FieldVector3D}&lt;{@link DerivativeStructure}&gt;.
  160.      * <p>
  161.      * The vector components must have time as their only derivation parameter and
  162.      * have consistent derivation orders.
  163.      * </p>
  164.      * @param p vector with time-derivatives embedded within the coordinates
  165.      */
  166.     public PVCoordinates(final FieldVector3D<DerivativeStructure> p) {
  167.         position = new Vector3D(p.getX().getReal(), p.getY().getReal(), p.getZ().getReal());
  168.         if (p.getX().getOrder() >= 1) {
  169.             velocity = new Vector3D(p.getX().getPartialDerivative(1),
  170.                                     p.getY().getPartialDerivative(1),
  171.                                     p.getZ().getPartialDerivative(1));
  172.             if (p.getX().getOrder() >= 2) {
  173.                 acceleration = new Vector3D(p.getX().getPartialDerivative(2),
  174.                                             p.getY().getPartialDerivative(2),
  175.                                             p.getZ().getPartialDerivative(2));
  176.             } else {
  177.                 acceleration = Vector3D.ZERO;
  178.             }
  179.         } else {
  180.             velocity     = Vector3D.ZERO;
  181.             acceleration = Vector3D.ZERO;
  182.         }
  183.     }

  184.     /** Transform the instance to a {@link FieldVector3D}&lt;{@link DerivativeStructure}&gt;.
  185.      * <p>
  186.      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
  187.      * to the user-specified order.
  188.      * </p>
  189.      * @param order derivation order for the vector components (must be either 0, 1 or 2)
  190.      * @return vector with time-derivatives embedded within the coordinates
  191.      * @exception OrekitException if the user specified order is too large
  192.      */
  193.     public FieldVector3D<DerivativeStructure> toDerivativeStructureVector(final int order)
  194.         throws OrekitException {

  195.         final DerivativeStructure x;
  196.         final DerivativeStructure y;
  197.         final DerivativeStructure z;
  198.         switch(order) {
  199.             case 0 :
  200.                 x = new DerivativeStructure(1, 0, position.getX());
  201.                 y = new DerivativeStructure(1, 0, position.getY());
  202.                 z = new DerivativeStructure(1, 0, position.getZ());
  203.                 break;
  204.             case 1 :
  205.                 x = new DerivativeStructure(1, 1, position.getX(), velocity.getX());
  206.                 y = new DerivativeStructure(1, 1, position.getY(), velocity.getY());
  207.                 z = new DerivativeStructure(1, 1, position.getZ(), velocity.getZ());
  208.                 break;
  209.             case 2 :
  210.                 x = new DerivativeStructure(1, 2, position.getX(), velocity.getX(), acceleration.getX());
  211.                 y = new DerivativeStructure(1, 2, position.getY(), velocity.getY(), acceleration.getY());
  212.                 z = new DerivativeStructure(1, 2, position.getZ(), velocity.getZ(), acceleration.getZ());
  213.                 break;
  214.             default :
  215.                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
  216.         }

  217.         return new FieldVector3D<DerivativeStructure>(x, y, z);

  218.     }

  219.     /** Estimate velocity between two positions.
  220.      * <p>Estimation is based on a simple fixed velocity translation
  221.      * during the time interval between the two positions.</p>
  222.      * @param start start position
  223.      * @param end end position
  224.      * @param dt time elapsed between the dates of the two positions
  225.      * @return velocity allowing to go from start to end positions
  226.      */
  227.     public static Vector3D estimateVelocity(final Vector3D start, final Vector3D end, final double dt) {
  228.         final double scale = 1.0 / dt;
  229.         return new Vector3D(scale, end, -scale, start);
  230.     }

  231.     /** Get a time-shifted state.
  232.      * <p>
  233.      * The state can be slightly shifted to close dates. This shift is based on
  234.      * a simple Taylor expansion. It is <em>not</em> intended as a replacement for
  235.      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  236.      * for either small time shifts or coarse accuracy.
  237.      * </p>
  238.      * @param dt time shift in seconds
  239.      * @return a new state, shifted with respect to the instance (which is immutable)
  240.      */
  241.     public PVCoordinates shiftedBy(final double dt) {
  242.         return new PVCoordinates(new Vector3D(1, position, dt, velocity, 0.5 * dt * dt, acceleration),
  243.                                  new Vector3D(1, velocity, dt, acceleration),
  244.                                  acceleration);
  245.     }

  246.     /** Interpolate position-velocity.
  247.      * <p>
  248.      * The interpolated instance is created by polynomial Hermite interpolation
  249.      * ensuring velocity remains the exact derivative of position.
  250.      * </p>
  251.      * <p>
  252.      * Note that even if first time derivatives (velocities)
  253.      * from sample can be ignored, the interpolated instance always includes
  254.      * interpolated derivatives. This feature can be used explicitly to
  255.      * compute these derivatives when it would be too complex to compute them
  256.      * from an analytical formula: just compute a few sample points from the
  257.      * explicit formula and set the derivatives to zero in these sample points,
  258.      * then use interpolation to add derivatives consistent with the positions.
  259.      * </p>
  260.      * @param date interpolation date
  261.      * @param useVelocities if true, use sample points velocities,
  262.      * otherwise ignore them and use only positions
  263.      * @param sample sample points on which interpolation should be done
  264.      * @return a new position-velocity, interpolated at specified date
  265.      * @deprecated since 7.0 replaced with {@link TimeStampedPVCoordinates#interpolate(AbsoluteDate, CartesianDerivativesFilter, Collection)}
  266.      */
  267.     @Deprecated
  268.     public static PVCoordinates interpolate(final AbsoluteDate date, final boolean useVelocities,
  269.                                             final Collection<Pair<AbsoluteDate, PVCoordinates>> sample) {
  270.         final List<TimeStampedPVCoordinates> list = new ArrayList<TimeStampedPVCoordinates>(sample.size());
  271.         for (final Pair<AbsoluteDate, PVCoordinates> pair : sample) {
  272.             list.add(new TimeStampedPVCoordinates(pair.getFirst(),
  273.                                                   pair.getSecond().getPosition(),
  274.                                                   pair.getSecond().getVelocity(),
  275.                                                   pair.getSecond().getAcceleration()));
  276.         }
  277.         return TimeStampedPVCoordinates.interpolate(date,
  278.                                                     useVelocities ? CartesianDerivativesFilter.USE_PV : CartesianDerivativesFilter.USE_P,
  279.                                                     list);
  280.     }

  281.     /** Gets the position.
  282.      * @return the position vector (m).
  283.      */
  284.     public Vector3D getPosition() {
  285.         return position;
  286.     }

  287.     /** Gets the velocity.
  288.      * @return the velocity vector (m/s).
  289.      */
  290.     public Vector3D getVelocity() {
  291.         return velocity;
  292.     }

  293.     /** Gets the acceleration.
  294.      * @return the acceleration vector (m/s²).
  295.      */
  296.     public Vector3D getAcceleration() {
  297.         return acceleration;
  298.     }

  299.     /** Gets the momentum.
  300.      * <p>This vector is the p &otimes; v where p is position, v is velocity
  301.      * and &otimes; is cross product. To get the real physical angular momentum
  302.      * you need to multiply this vector by the mass.</p>
  303.      * <p>The returned vector is recomputed each time this method is called, it
  304.      * is not cached.</p>
  305.      * @return a new instance of the momentum vector (m²/s).
  306.      */
  307.     public Vector3D getMomentum() {
  308.         return Vector3D.crossProduct(position, velocity);
  309.     }

  310.     /**
  311.      * Get the angular velocity (spin) of this point as seen from the origin.
  312.      * <p/>
  313.      * The angular velocity vector is parallel to the {@link #getMomentum() angular
  314.      * momentum} and is computed by ω = p &times; v / ||p||²
  315.      *
  316.      * @return the angular velocity vector
  317.      * @see <a href="http://en.wikipedia.org/wiki/Angular_velocity">Angular Velocity on
  318.      *      Wikipedia</a>
  319.      */
  320.     public Vector3D getAngularVelocity() {
  321.         return this.getMomentum().scalarMultiply(1.0 / this.getPosition().getNormSq());
  322.     }

  323.     /** Get the opposite of the instance.
  324.      * @return a new position-velocity which is opposite to the instance
  325.      */
  326.     public PVCoordinates negate() {
  327.         return new PVCoordinates(position.negate(), velocity.negate(), acceleration.negate());
  328.     }

  329.     /** Normalize the position part of the instance.
  330.      * <p>
  331.      * The computed coordinates first component (position) will be a
  332.      * normalized vector, the second component (velocity) will be the
  333.      * derivative of the first component (hence it will generally not
  334.      * be normalized), and the third component (acceleration) will be the
  335.      * derivative of the second component (hence it will generally not
  336.      * be normalized).
  337.      * </p>
  338.      * @return a new instance, with first component normalized and
  339.      * remaining component computed to have consistent derivatives
  340.      */
  341.     public PVCoordinates normalize() {
  342.         final double   inv     = 1.0 / position.getNorm();
  343.         final Vector3D u       = new Vector3D(inv, position);
  344.         final Vector3D v       = new Vector3D(inv, velocity);
  345.         final Vector3D w       = new Vector3D(inv, acceleration);
  346.         final double   uv      = Vector3D.dotProduct(u, v);
  347.         final double   v2      = Vector3D.dotProduct(v, v);
  348.         final double   uw      = Vector3D.dotProduct(u, w);
  349.         final Vector3D uDot    = new Vector3D(1, v, -uv, u);
  350.         final Vector3D uDotDot = new Vector3D(1, w, -2 * uv, v, 3 * uv * uv - v2 - uw, u);
  351.         return new PVCoordinates(u, uDot, uDotDot);
  352.     }

  353.     /** Compute the cross-product of two instances.
  354.      * @param pv1 first instances
  355.      * @param pv2 second instances
  356.      * @return the cross product v1 ^ v2 as a new instance
  357.      */
  358.     public static PVCoordinates crossProduct(final PVCoordinates pv1, final PVCoordinates pv2) {
  359.         final Vector3D p1 = pv1.position;
  360.         final Vector3D v1 = pv1.velocity;
  361.         final Vector3D a1 = pv1.acceleration;
  362.         final Vector3D p2 = pv2.position;
  363.         final Vector3D v2 = pv2.velocity;
  364.         final Vector3D a2 = pv2.acceleration;
  365.         return new PVCoordinates(Vector3D.crossProduct(p1, p2),
  366.                                  new Vector3D(1, Vector3D.crossProduct(p1, v2),
  367.                                               1, Vector3D.crossProduct(v1, p2)),
  368.                                  new Vector3D(1, Vector3D.crossProduct(p1, a2),
  369.                                               2, Vector3D.crossProduct(v1, v2),
  370.                                               1, Vector3D.crossProduct(a1, p2)));
  371.     }

  372.     /** Return a string representation of this position/velocity pair.
  373.      * @return string representation of this position/velocity pair
  374.      */
  375.     public String toString() {
  376.         final String comma = ", ";
  377.         return new StringBuffer().append('{').append("P(").
  378.                 append(position.getX()).append(comma).
  379.                 append(position.getY()).append(comma).
  380.                 append(position.getZ()).append("), V(").
  381.                 append(velocity.getX()).append(comma).
  382.                 append(velocity.getY()).append(comma).
  383.                 append(velocity.getZ()).append("), A(").
  384.                 append(acceleration.getX()).append(comma).
  385.                 append(acceleration.getY()).append(comma).
  386.                 append(acceleration.getZ()).append(")}").toString();
  387.     }

  388.     /** Replace the instance with a data transfer object for serialization.
  389.      * @return data transfer object that will be serialized
  390.      */
  391.     private Object writeReplace() {
  392.         return new DTO(this);
  393.     }

  394.     /** Internal class used only for serialization. */
  395.     private static class DTO implements Serializable {

  396.         /** Serializable UID. */
  397.         private static final long serialVersionUID = 20140723L;

  398.         /** Double values. */
  399.         private double[] d;

  400.         /** Simple constructor.
  401.          * @param pv instance to serialize
  402.          */
  403.         private DTO(final PVCoordinates pv) {
  404.             this.d = new double[] {
  405.                 pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
  406.                 pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ(),
  407.                 pv.getAcceleration().getX(), pv.getAcceleration().getY(), pv.getAcceleration().getZ(),
  408.             };
  409.         }

  410.         /** Replace the deserialized data transfer object with a {@link PVCoordinates}.
  411.          * @return replacement {@link PVCoordinates}
  412.          */
  413.         private Object readResolve() {
  414.             return new PVCoordinates(new Vector3D(d[0], d[1], d[2]),
  415.                                      new Vector3D(d[3], d[4], d[5]),
  416.                                      new Vector3D(d[6], d[7], d[8]));
  417.         }

  418.     }

  419. }