PVCoordinates.java

  1. /* Copyright 2002-2025 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 org.hipparchus.analysis.differentiation.DSFactory;
  19. import org.hipparchus.analysis.differentiation.Derivative;
  20. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  21. import org.hipparchus.analysis.differentiation.UnivariateDerivative1;
  22. import org.hipparchus.analysis.differentiation.UnivariateDerivative2;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  25. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  26. import org.hipparchus.util.Blendable;
  27. import org.hipparchus.util.FastMath;
  28. import org.orekit.errors.OrekitException;
  29. import org.orekit.errors.OrekitMessages;
  30. import org.orekit.time.TimeShiftable;

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

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

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

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

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

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

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

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

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

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

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

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

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

  158.     /** Builds a PVCoordinates triplet from  a {@link FieldVector3D}&lt;{@link Derivative}&gt;.
  159.      * <p>
  160.      * The vector components must have time as their only derivation parameter and
  161.      * have consistent derivation orders.
  162.      * </p>
  163.      * @param p vector with time-derivatives embedded within the coordinates
  164.      * @param <U> type of the derivative
  165.      */
  166.     public <U extends Derivative<U>> PVCoordinates(final FieldVector3D<U> 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.     /**
  185.      * Builds PV coordinates with the givne position, zero velocity, and zero
  186.      * acceleration.
  187.      *
  188.      * @param position position vector (m)
  189.      */
  190.     public PVCoordinates(final Vector3D position) {
  191.         this(position, Vector3D.ZERO);
  192.     }

  193.     /** Transform the instance to a {@link FieldVector3D}&lt;{@link DerivativeStructure}&gt;.
  194.      * <p>
  195.      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
  196.      * to the user-specified order.
  197.      * </p>
  198.      * @param order derivation order for the vector components (must be either 0, 1 or 2)
  199.      * @return vector with time-derivatives embedded within the coordinates
  200.      */
  201.     public FieldVector3D<DerivativeStructure> toDerivativeStructureVector(final int order) {

  202.         final DSFactory factory;
  203.         final DerivativeStructure x;
  204.         final DerivativeStructure y;
  205.         final DerivativeStructure z;
  206.         switch (order) {
  207.             case 0 :
  208.                 factory = new DSFactory(1, order);
  209.                 x = factory.build(position.getX());
  210.                 y = factory.build(position.getY());
  211.                 z = factory.build(position.getZ());
  212.                 break;
  213.             case 1 :
  214.                 factory = new DSFactory(1, order);
  215.                 x = factory.build(position.getX(), velocity.getX());
  216.                 y = factory.build(position.getY(), velocity.getY());
  217.                 z = factory.build(position.getZ(), velocity.getZ());
  218.                 break;
  219.             case 2 :
  220.                 factory = new DSFactory(1, order);
  221.                 x = factory.build(position.getX(), velocity.getX(), acceleration.getX());
  222.                 y = factory.build(position.getY(), velocity.getY(), acceleration.getY());
  223.                 z = factory.build(position.getZ(), velocity.getZ(), acceleration.getZ());
  224.                 break;
  225.             default :
  226.                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
  227.         }

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

  229.     }

  230.     /** Transform the instance to a {@link FieldVector3D}&lt;{@link UnivariateDerivative1}&gt;.
  231.      * <p>
  232.      * The {@link UnivariateDerivative1} coordinates correspond to time-derivatives up
  233.      * to the order 1.
  234.      * </p>
  235.      * @return vector with time-derivatives embedded within the coordinates
  236.      * @see #toUnivariateDerivative2Vector()
  237.      * @since 10.2
  238.      */
  239.     public FieldVector3D<UnivariateDerivative1> toUnivariateDerivative1Vector() {

  240.         final UnivariateDerivative1 x = new UnivariateDerivative1(position.getX(), velocity.getX());
  241.         final UnivariateDerivative1 y = new UnivariateDerivative1(position.getY(), velocity.getY());
  242.         final UnivariateDerivative1 z = new UnivariateDerivative1(position.getZ(), velocity.getZ());

  243.         return new FieldVector3D<>(x, y, z);
  244.     }

  245.     /** Transform the instance to a {@link FieldVector3D}&lt;{@link UnivariateDerivative2}&gt;.
  246.      * <p>
  247.      * The {@link UnivariateDerivative2} coordinates correspond to time-derivatives up
  248.      * to the order 2.
  249.      * </p>
  250.      * @return vector with time-derivatives embedded within the coordinates
  251.      * @see #toUnivariateDerivative1Vector()
  252.      * @since 10.2
  253.      */
  254.     public FieldVector3D<UnivariateDerivative2> toUnivariateDerivative2Vector() {

  255.         final UnivariateDerivative2 x = new UnivariateDerivative2(position.getX(), velocity.getX(), acceleration.getX());
  256.         final UnivariateDerivative2 y = new UnivariateDerivative2(position.getY(), velocity.getY(), acceleration.getY());
  257.         final UnivariateDerivative2 z = new UnivariateDerivative2(position.getZ(), velocity.getZ(), acceleration.getZ());

  258.         return new FieldVector3D<>(x, y, z);
  259.     }

  260.     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link DerivativeStructure}&gt;.
  261.      * <p>
  262.      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
  263.      * to the user-specified order. As both the instance components {@link #getPosition() position},
  264.      * {@link #getVelocity() velocity} and {@link #getAcceleration() acceleration} and the
  265.      * {@link DerivativeStructure#getPartialDerivative(int...) derivatives} of the components
  266.      * holds time-derivatives, there are several ways to retrieve these derivatives. If for example
  267.      * the {@code order} is set to 2, then both {@code pv.getPosition().getX().getPartialDerivative(2)},
  268.      * {@code pv.getVelocity().getX().getPartialDerivative(1)} and
  269.      * {@code pv.getAcceleration().getX().getValue()} return the exact same value.
  270.      * </p>
  271.      * <p>
  272.      * If derivation order is 1, the first derivative of acceleration will be computed as a
  273.      * Keplerian-only jerk. If derivation order is 2, the second derivative of velocity (which
  274.      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
  275.      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
  276.      * </p>
  277.      * @param order derivation order for the vector components (must be either 0, 1 or 2)
  278.      * @return pv coordinates with time-derivatives embedded within the coordinates
  279.      * @since 9.2
  280.      */
  281.     public FieldPVCoordinates<DerivativeStructure> toDerivativeStructurePV(final int order) {

  282.         final DSFactory factory;
  283.         final DerivativeStructure x0;
  284.         final DerivativeStructure y0;
  285.         final DerivativeStructure z0;
  286.         final DerivativeStructure x1;
  287.         final DerivativeStructure y1;
  288.         final DerivativeStructure z1;
  289.         final DerivativeStructure x2;
  290.         final DerivativeStructure y2;
  291.         final DerivativeStructure z2;
  292.         switch (order) {
  293.             case 0 :
  294.                 factory = new DSFactory(1, order);
  295.                 x0 = factory.build(position.getX());
  296.                 y0 = factory.build(position.getY());
  297.                 z0 = factory.build(position.getZ());
  298.                 x1 = factory.build(velocity.getX());
  299.                 y1 = factory.build(velocity.getY());
  300.                 z1 = factory.build(velocity.getZ());
  301.                 x2 = factory.build(acceleration.getX());
  302.                 y2 = factory.build(acceleration.getY());
  303.                 z2 = factory.build(acceleration.getZ());
  304.                 break;
  305.             case 1 : {
  306.                 factory = new DSFactory(1, order);
  307.                 final double   r2            = position.getNormSq();
  308.                 final double   r             = FastMath.sqrt(r2);
  309.                 final double   pvOr2         = Vector3D.dotProduct(position, velocity) / r2;
  310.                 final double   a             = acceleration.getNorm();
  311.                 final double   aOr           = a / r;
  312.                 final Vector3D keplerianJerk = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
  313.                 x0 = factory.build(position.getX(),     velocity.getX());
  314.                 y0 = factory.build(position.getY(),     velocity.getY());
  315.                 z0 = factory.build(position.getZ(),     velocity.getZ());
  316.                 x1 = factory.build(velocity.getX(),     acceleration.getX());
  317.                 y1 = factory.build(velocity.getY(),     acceleration.getY());
  318.                 z1 = factory.build(velocity.getZ(),     acceleration.getZ());
  319.                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX());
  320.                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY());
  321.                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ());
  322.                 break;
  323.             }
  324.             case 2 : {
  325.                 factory = new DSFactory(1, order);
  326.                 final double   r2              = position.getNormSq();
  327.                 final double   r               = FastMath.sqrt(r2);
  328.                 final double   pvOr2           = Vector3D.dotProduct(position, velocity) / r2;
  329.                 final double   a               = acceleration.getNorm();
  330.                 final double   aOr             = a / r;
  331.                 final Vector3D keplerianJerk   = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
  332.                 final double   v2              = velocity.getNormSq();
  333.                 final double   pa              = Vector3D.dotProduct(position, acceleration);
  334.                 final double   aj              = Vector3D.dotProduct(acceleration, keplerianJerk);
  335.                 final Vector3D keplerianJounce = new Vector3D(-3 * (v2 + pa) / r2 + 15 * pvOr2 * pvOr2 - aOr, acceleration,
  336.                                                               4 * aOr * pvOr2 - aj / (a * r), velocity);
  337.                 x0 = factory.build(position.getX(),     velocity.getX(),      acceleration.getX());
  338.                 y0 = factory.build(position.getY(),     velocity.getY(),      acceleration.getY());
  339.                 z0 = factory.build(position.getZ(),     velocity.getZ(),      acceleration.getZ());
  340.                 x1 = factory.build(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
  341.                 y1 = factory.build(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
  342.                 z1 = factory.build(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
  343.                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
  344.                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
  345.                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());
  346.                 break;
  347.             }
  348.             default :
  349.                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
  350.         }

  351.         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
  352.                                         new FieldVector3D<>(x1, y1, z1),
  353.                                         new FieldVector3D<>(x2, y2, z2));

  354.     }

  355.     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link UnivariateDerivative1}&gt;.
  356.      * <p>
  357.      * The {@link UnivariateDerivative1} coordinates correspond to time-derivatives up
  358.      * to the order 1.
  359.      * The first derivative of acceleration will be computed as a Keplerian-only jerk.
  360.      * </p>
  361.      * @return pv coordinates with time-derivatives embedded within the coordinates
  362.      * @since 10.2
  363.      */
  364.     public FieldPVCoordinates<UnivariateDerivative1> toUnivariateDerivative1PV() {

  365.         final double   r2            = position.getNormSq();
  366.         final double   r             = FastMath.sqrt(r2);
  367.         final double   pvOr2         = Vector3D.dotProduct(position, velocity) / r2;
  368.         final double   a             = acceleration.getNorm();
  369.         final double   aOr           = a / r;
  370.         final Vector3D keplerianJerk = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);

  371.         final UnivariateDerivative1 x0 = new UnivariateDerivative1(position.getX(),     velocity.getX());
  372.         final UnivariateDerivative1 y0 = new UnivariateDerivative1(position.getY(),     velocity.getY());
  373.         final UnivariateDerivative1 z0 = new UnivariateDerivative1(position.getZ(),     velocity.getZ());
  374.         final UnivariateDerivative1 x1 = new UnivariateDerivative1(velocity.getX(),     acceleration.getX());
  375.         final UnivariateDerivative1 y1 = new UnivariateDerivative1(velocity.getY(),     acceleration.getY());
  376.         final UnivariateDerivative1 z1 = new UnivariateDerivative1(velocity.getZ(),     acceleration.getZ());
  377.         final UnivariateDerivative1 x2 = new UnivariateDerivative1(acceleration.getX(), keplerianJerk.getX());
  378.         final UnivariateDerivative1 y2 = new UnivariateDerivative1(acceleration.getY(), keplerianJerk.getY());
  379.         final UnivariateDerivative1 z2 = new UnivariateDerivative1(acceleration.getZ(), keplerianJerk.getZ());

  380.         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
  381.                                         new FieldVector3D<>(x1, y1, z1),
  382.                                         new FieldVector3D<>(x2, y2, z2));

  383.     }

  384.     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link UnivariateDerivative2}&gt;.
  385.      * <p>
  386.      * The {@link UnivariateDerivative2} coordinates correspond to time-derivatives up
  387.      * to the order 2.
  388.      * As derivation order is 2, the second derivative of velocity (which
  389.      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
  390.      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
  391.      * </p>
  392.      * @return pv coordinates with time-derivatives embedded within the coordinates
  393.      * @since 10.2
  394.      */
  395.     public FieldPVCoordinates<UnivariateDerivative2> toUnivariateDerivative2PV() {

  396.         final double   r2              = position.getNormSq();
  397.         final double   r               = FastMath.sqrt(r2);
  398.         final double   pvOr2           = Vector3D.dotProduct(position, velocity) / r2;
  399.         final double   a               = acceleration.getNorm();
  400.         final double   aOr             = a / r;
  401.         final Vector3D keplerianJerk   = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
  402.         final double   v2              = velocity.getNormSq();
  403.         final double   pa              = Vector3D.dotProduct(position, acceleration);
  404.         final double   aj              = Vector3D.dotProduct(acceleration, keplerianJerk);
  405.         final Vector3D keplerianJounce = new Vector3D(-3 * (v2 + pa) / r2 + 15 * pvOr2 * pvOr2 - aOr, acceleration,
  406.                                                       4 * aOr * pvOr2 - aj / (a * r), velocity);

  407.         final UnivariateDerivative2 x0 = new UnivariateDerivative2(position.getX(),     velocity.getX(),      acceleration.getX());
  408.         final UnivariateDerivative2 y0 = new UnivariateDerivative2(position.getY(),     velocity.getY(),      acceleration.getY());
  409.         final UnivariateDerivative2 z0 = new UnivariateDerivative2(position.getZ(),     velocity.getZ(),      acceleration.getZ());
  410.         final UnivariateDerivative2 x1 = new UnivariateDerivative2(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
  411.         final UnivariateDerivative2 y1 = new UnivariateDerivative2(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
  412.         final UnivariateDerivative2 z1 = new UnivariateDerivative2(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
  413.         final UnivariateDerivative2 x2 = new UnivariateDerivative2(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
  414.         final UnivariateDerivative2 y2 = new UnivariateDerivative2(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
  415.         final UnivariateDerivative2 z2 = new UnivariateDerivative2(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());

  416.         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
  417.                                         new FieldVector3D<>(x1, y1, z1),
  418.                                         new FieldVector3D<>(x2, y2, z2));

  419.     }

  420.     /** Estimate velocity between two positions.
  421.      * <p>Estimation is based on a simple fixed velocity translation
  422.      * during the time interval between the two positions.</p>
  423.      * @param start start position
  424.      * @param end end position
  425.      * @param dt time elapsed between the dates of the two positions
  426.      * @return velocity allowing to go from start to end positions
  427.      */
  428.     public static Vector3D estimateVelocity(final Vector3D start, final Vector3D end, final double dt) {
  429.         final double scale = 1.0 / dt;
  430.         return new Vector3D(scale, end, -scale, start);
  431.     }

  432.     /** Get a time-shifted state.
  433.      * <p>
  434.      * The state can be slightly shifted to close dates. This shift is based on
  435.      * a simple Taylor expansion. It is <em>not</em> intended as a replacement for
  436.      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  437.      * for either small time shifts or coarse accuracy.
  438.      * </p>
  439.      * @param dt time shift in seconds
  440.      * @return a new state, shifted with respect to the instance (which is immutable)
  441.      */
  442.     public PVCoordinates shiftedBy(final double dt) {
  443.         return new PVCoordinates(positionShiftedBy(dt),
  444.                                  new Vector3D(1, velocity, dt, acceleration),
  445.                                  acceleration);
  446.     }

  447.     /**
  448.      * Get a time-shifted position. Same as {@link #shiftedBy(double)} except
  449.      * that only the sifted position is returned.
  450.      * <p>
  451.      * The state can be slightly shifted to close dates. This shift is based on
  452.      * a simple Taylor expansion. It is <em>not</em> intended as a replacement
  453.      * for proper orbit propagation (it is not even Keplerian!) but should be
  454.      * sufficient for either small time shifts or coarse accuracy.
  455.      * </p>
  456.      *
  457.      * @param dt time shift in seconds
  458.      * @return a new state, shifted with respect to the instance (which is
  459.      * immutable)
  460.      */
  461.     public Vector3D positionShiftedBy(final double dt) {
  462.         return new Vector3D(1, position, dt, velocity, 0.5 * dt * dt, acceleration);
  463.     }

  464.     /** Gets the position.
  465.      * @return the position vector (m).
  466.      */
  467.     public Vector3D getPosition() {
  468.         return position;
  469.     }

  470.     /** Gets the velocity.
  471.      * @return the velocity vector (m/s).
  472.      */
  473.     public Vector3D getVelocity() {
  474.         return velocity;
  475.     }

  476.     /** Gets the acceleration.
  477.      * @return the acceleration vector (m/s²).
  478.      */
  479.     public Vector3D getAcceleration() {
  480.         return acceleration;
  481.     }

  482.     /** Gets the momentum.
  483.      * <p>This vector is the p &otimes; v where p is position, v is velocity
  484.      * and &otimes; is cross product. To get the real physical angular momentum
  485.      * you need to multiply this vector by the mass.</p>
  486.      * <p>The returned vector is recomputed each time this method is called, it
  487.      * is not cached.</p>
  488.      * @return a new instance of the momentum vector (m²/s).
  489.      */
  490.     public Vector3D getMomentum() {
  491.         return Vector3D.crossProduct(position, velocity);
  492.     }

  493.     /**
  494.      * Get the angular velocity (spin) of this point as seen from the origin.
  495.      *
  496.      * <p> The angular velocity vector is parallel to the {@link #getMomentum()
  497.      * angular momentum} and is computed by ω = p &times; v / ||p||²
  498.      *
  499.      * @return the angular velocity vector
  500.      * @see <a href="http://en.wikipedia.org/wiki/Angular_velocity">Angular Velocity on
  501.      *      Wikipedia</a>
  502.      */
  503.     public Vector3D getAngularVelocity() {
  504.         return this.getMomentum().scalarMultiply(1.0 / this.getPosition().getNormSq());
  505.     }

  506.     /** Get the opposite of the instance.
  507.      * @return a new position-velocity which is opposite to the instance
  508.      */
  509.     public PVCoordinates negate() {
  510.         return new PVCoordinates(position.negate(), velocity.negate(), acceleration.negate());
  511.     }

  512.     /** Normalize the position part of the instance.
  513.      * <p>
  514.      * The computed coordinates first component (position) will be a
  515.      * normalized vector, the second component (velocity) will be the
  516.      * derivative of the first component (hence it will generally not
  517.      * be normalized), and the third component (acceleration) will be the
  518.      * derivative of the second component (hence it will generally not
  519.      * be normalized).
  520.      * </p>
  521.      * @return a new instance, with first component normalized and
  522.      * remaining component computed to have consistent derivatives
  523.      */
  524.     public PVCoordinates normalize() {
  525.         final double   inv     = 1.0 / position.getNorm();
  526.         final Vector3D u       = new Vector3D(inv, position);
  527.         final Vector3D v       = new Vector3D(inv, velocity);
  528.         final Vector3D w       = new Vector3D(inv, acceleration);
  529.         final double   uv      = Vector3D.dotProduct(u, v);
  530.         final double   v2      = Vector3D.dotProduct(v, v);
  531.         final double   uw      = Vector3D.dotProduct(u, w);
  532.         final Vector3D uDot    = new Vector3D(1, v, -uv, u);
  533.         final Vector3D uDotDot = new Vector3D(1, w, -2 * uv, v, 3 * uv * uv - v2 - uw, u);
  534.         return new PVCoordinates(u, uDot, uDotDot);
  535.     }

  536.     /** Compute the cross-product of two instances.
  537.      * @param pv1 first instances
  538.      * @param pv2 second instances
  539.      * @return the cross product v1 ^ v2 as a new instance
  540.      */
  541.     public static PVCoordinates crossProduct(final PVCoordinates pv1, final PVCoordinates pv2) {
  542.         final Vector3D p1 = pv1.position;
  543.         final Vector3D v1 = pv1.velocity;
  544.         final Vector3D a1 = pv1.acceleration;
  545.         final Vector3D p2 = pv2.position;
  546.         final Vector3D v2 = pv2.velocity;
  547.         final Vector3D a2 = pv2.acceleration;
  548.         return new PVCoordinates(Vector3D.crossProduct(p1, p2),
  549.                                  new Vector3D(1, Vector3D.crossProduct(p1, v2),
  550.                                               1, Vector3D.crossProduct(v1, p2)),
  551.                                  new Vector3D(1, Vector3D.crossProduct(p1, a2),
  552.                                               2, Vector3D.crossProduct(v1, v2),
  553.                                               1, Vector3D.crossProduct(a1, p2)));
  554.     }

  555.     /** Return a string representation of this position/velocity pair.
  556.      * @return string representation of this position/velocity pair
  557.      */
  558.     public String toString() {
  559.         final String comma = ", ";
  560.         return new StringBuilder().append('{').append("P(").
  561.                 append(position.getX()).append(comma).
  562.                 append(position.getY()).append(comma).
  563.                 append(position.getZ()).append("), V(").
  564.                 append(velocity.getX()).append(comma).
  565.                 append(velocity.getY()).append(comma).
  566.                 append(velocity.getZ()).append("), A(").
  567.                 append(acceleration.getX()).append(comma).
  568.                 append(acceleration.getY()).append(comma).
  569.                 append(acceleration.getZ()).append(")}").toString();
  570.     }

  571.     /** {@inheritDoc} */
  572.     @Override
  573.     public PVCoordinates blendArithmeticallyWith(final PVCoordinates other, final double blendingValue)
  574.             throws MathIllegalArgumentException {
  575.         final Vector3D blendedPosition     = position.blendArithmeticallyWith(other.position, blendingValue);
  576.         final Vector3D blendedVelocity     = velocity.blendArithmeticallyWith(other.velocity, blendingValue);
  577.         final Vector3D blendedAcceleration = acceleration.blendArithmeticallyWith(other.acceleration, blendingValue);

  578.         return new PVCoordinates(blendedPosition, blendedVelocity, blendedAcceleration);
  579.     }

  580. }