PVCoordinates.java

  1. /* Copyright 2002-2017 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 org.hipparchus.analysis.differentiation.DSFactory;
  20. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  21. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  22. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  23. import org.orekit.errors.OrekitException;
  24. import org.orekit.errors.OrekitMessages;
  25. import org.orekit.time.TimeShiftable;

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

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

  43.     /** Serializable UID. */
  44.     private static final long serialVersionUID = 20140407L;

  45.     /** The position. */
  46.     private final Vector3D position;

  47.     /** The velocity. */
  48.     private final Vector3D velocity;

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

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

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

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

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

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

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

  115.     /** Linear constructor.
  116.      * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
  117.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
  118.      * @param a1 first scale factor
  119.      * @param pv1 first base (unscaled) PVCoordinates
  120.      * @param a2 second scale factor
  121.      * @param pv2 second base (unscaled) PVCoordinates
  122.      * @param a3 third scale factor
  123.      * @param pv3 third base (unscaled) PVCoordinates
  124.      */
  125.     public PVCoordinates(final double a1, final PVCoordinates pv1,
  126.                          final double a2, final PVCoordinates pv2,
  127.                          final double a3, final PVCoordinates pv3) {
  128.         position     = new Vector3D(a1, pv1.position,     a2, pv2.position,     a3, pv3.position);
  129.         velocity     = new Vector3D(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity);
  130.         acceleration = new Vector3D(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration);
  131.     }

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

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

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

  191.         final DSFactory factory;
  192.         final DerivativeStructure x;
  193.         final DerivativeStructure y;
  194.         final DerivativeStructure z;
  195.         switch(order) {
  196.             case 0 :
  197.                 factory = new DSFactory(1, order);
  198.                 x = factory.build(position.getX());
  199.                 y = factory.build(position.getY());
  200.                 z = factory.build(position.getZ());
  201.                 break;
  202.             case 1 :
  203.                 factory = new DSFactory(1, order);
  204.                 x = factory.build(position.getX(), velocity.getX());
  205.                 y = factory.build(position.getY(), velocity.getY());
  206.                 z = factory.build(position.getZ(), velocity.getZ());
  207.                 break;
  208.             case 2 :
  209.                 factory = new DSFactory(1, order);
  210.                 x = factory.build(position.getX(), velocity.getX(), acceleration.getX());
  211.                 y = factory.build(position.getY(), velocity.getY(), acceleration.getY());
  212.                 z = factory.build(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<>(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.     /** Gets the position.
  247.      * @return the position vector (m).
  248.      */
  249.     public Vector3D getPosition() {
  250.         return position;
  251.     }

  252.     /** Gets the velocity.
  253.      * @return the velocity vector (m/s).
  254.      */
  255.     public Vector3D getVelocity() {
  256.         return velocity;
  257.     }

  258.     /** Gets the acceleration.
  259.      * @return the acceleration vector (m/s²).
  260.      */
  261.     public Vector3D getAcceleration() {
  262.         return acceleration;
  263.     }

  264.     /** Gets the momentum.
  265.      * <p>This vector is the p &otimes; v where p is position, v is velocity
  266.      * and &otimes; is cross product. To get the real physical angular momentum
  267.      * you need to multiply this vector by the mass.</p>
  268.      * <p>The returned vector is recomputed each time this method is called, it
  269.      * is not cached.</p>
  270.      * @return a new instance of the momentum vector (m²/s).
  271.      */
  272.     public Vector3D getMomentum() {
  273.         return Vector3D.crossProduct(position, velocity);
  274.     }

  275.     /**
  276.      * Get the angular velocity (spin) of this point as seen from the origin.
  277.      *
  278.      * <p> The angular velocity vector is parallel to the {@link #getMomentum()
  279.      * angular momentum} and is computed by ω = p &times; v / ||p||²
  280.      *
  281.      * @return the angular velocity vector
  282.      * @see <a href="http://en.wikipedia.org/wiki/Angular_velocity">Angular Velocity on
  283.      *      Wikipedia</a>
  284.      */
  285.     public Vector3D getAngularVelocity() {
  286.         return this.getMomentum().scalarMultiply(1.0 / this.getPosition().getNormSq());
  287.     }

  288.     /** Get the opposite of the instance.
  289.      * @return a new position-velocity which is opposite to the instance
  290.      */
  291.     public PVCoordinates negate() {
  292.         return new PVCoordinates(position.negate(), velocity.negate(), acceleration.negate());
  293.     }

  294.     /** Normalize the position part of the instance.
  295.      * <p>
  296.      * The computed coordinates first component (position) will be a
  297.      * normalized vector, the second component (velocity) will be the
  298.      * derivative of the first component (hence it will generally not
  299.      * be normalized), and the third component (acceleration) will be the
  300.      * derivative of the second component (hence it will generally not
  301.      * be normalized).
  302.      * </p>
  303.      * @return a new instance, with first component normalized and
  304.      * remaining component computed to have consistent derivatives
  305.      */
  306.     public PVCoordinates normalize() {
  307.         final double   inv     = 1.0 / position.getNorm();
  308.         final Vector3D u       = new Vector3D(inv, position);
  309.         final Vector3D v       = new Vector3D(inv, velocity);
  310.         final Vector3D w       = new Vector3D(inv, acceleration);
  311.         final double   uv      = Vector3D.dotProduct(u, v);
  312.         final double   v2      = Vector3D.dotProduct(v, v);
  313.         final double   uw      = Vector3D.dotProduct(u, w);
  314.         final Vector3D uDot    = new Vector3D(1, v, -uv, u);
  315.         final Vector3D uDotDot = new Vector3D(1, w, -2 * uv, v, 3 * uv * uv - v2 - uw, u);
  316.         return new PVCoordinates(u, uDot, uDotDot);
  317.     }

  318.     /** Compute the cross-product of two instances.
  319.      * @param pv1 first instances
  320.      * @param pv2 second instances
  321.      * @return the cross product v1 ^ v2 as a new instance
  322.      */
  323.     public static PVCoordinates crossProduct(final PVCoordinates pv1, final PVCoordinates pv2) {
  324.         final Vector3D p1 = pv1.position;
  325.         final Vector3D v1 = pv1.velocity;
  326.         final Vector3D a1 = pv1.acceleration;
  327.         final Vector3D p2 = pv2.position;
  328.         final Vector3D v2 = pv2.velocity;
  329.         final Vector3D a2 = pv2.acceleration;
  330.         return new PVCoordinates(Vector3D.crossProduct(p1, p2),
  331.                                  new Vector3D(1, Vector3D.crossProduct(p1, v2),
  332.                                               1, Vector3D.crossProduct(v1, p2)),
  333.                                  new Vector3D(1, Vector3D.crossProduct(p1, a2),
  334.                                               2, Vector3D.crossProduct(v1, v2),
  335.                                               1, Vector3D.crossProduct(a1, p2)));
  336.     }

  337.     /** Return a string representation of this position/velocity pair.
  338.      * @return string representation of this position/velocity pair
  339.      */
  340.     public String toString() {
  341.         final String comma = ", ";
  342.         return new StringBuffer().append('{').append("P(").
  343.                 append(position.getX()).append(comma).
  344.                 append(position.getY()).append(comma).
  345.                 append(position.getZ()).append("), V(").
  346.                 append(velocity.getX()).append(comma).
  347.                 append(velocity.getY()).append(comma).
  348.                 append(velocity.getZ()).append("), A(").
  349.                 append(acceleration.getX()).append(comma).
  350.                 append(acceleration.getY()).append(comma).
  351.                 append(acceleration.getZ()).append(")}").toString();
  352.     }

  353.     /** Replace the instance with a data transfer object for serialization.
  354.      * @return data transfer object that will be serialized
  355.      */
  356.     private Object writeReplace() {
  357.         return new DTO(this);
  358.     }

  359.     /** Internal class used only for serialization. */
  360.     private static class DTO implements Serializable {

  361.         /** Serializable UID. */
  362.         private static final long serialVersionUID = 20140723L;

  363.         /** Double values. */
  364.         private double[] d;

  365.         /** Simple constructor.
  366.          * @param pv instance to serialize
  367.          */
  368.         private DTO(final PVCoordinates pv) {
  369.             this.d = new double[] {
  370.                 pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
  371.                 pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ(),
  372.                 pv.getAcceleration().getX(), pv.getAcceleration().getY(), pv.getAcceleration().getZ(),
  373.             };
  374.         }

  375.         /** Replace the deserialized data transfer object with a {@link PVCoordinates}.
  376.          * @return replacement {@link PVCoordinates}
  377.          */
  378.         private Object readResolve() {
  379.             return new PVCoordinates(new Vector3D(d[0], d[1], d[2]),
  380.                                      new Vector3D(d[3], d[4], d[5]),
  381.                                      new Vector3D(d[6], d[7], d[8]));
  382.         }

  383.     }

  384. }