1   /* Copyright 2002-2022 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  
19  import java.io.Serializable;
20  
21  import org.hipparchus.analysis.differentiation.DSFactory;
22  import org.hipparchus.analysis.differentiation.Derivative;
23  import org.hipparchus.analysis.differentiation.DerivativeStructure;
24  import org.hipparchus.analysis.differentiation.UnivariateDerivative1;
25  import org.hipparchus.analysis.differentiation.UnivariateDerivative2;
26  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
27  import org.hipparchus.geometry.euclidean.threed.Vector3D;
28  import org.hipparchus.util.FastMath;
29  import org.orekit.errors.OrekitException;
30  import org.orekit.errors.OrekitMessages;
31  import org.orekit.time.TimeShiftable;
32  
33  /** Simple container for Position/Velocity/Acceleration triplets.
34   * <p>
35   * The state can be slightly shifted to close dates. This shift is based on
36   * a simple quadratic model. It is <em>not</em> intended as a replacement for
37   * proper orbit propagation (it is not even Keplerian!) but should be sufficient
38   * for either small time shifts or coarse accuracy.
39   * </p>
40   * <p>
41   * This class is the angular counterpart to {@link AngularCoordinates}.
42   * </p>
43   * <p>Instances of this class are guaranteed to be immutable.</p>
44   * @author Fabien Maussion
45   * @author Luc Maisonobe
46   */
47  public class PVCoordinates implements TimeShiftable<PVCoordinates>, Serializable {
48  
49      /** Fixed position/velocity at origin (both p, v and a are zero vectors). */
50      public static final PVCoordinates ZERO = new PVCoordinates(Vector3D.ZERO, Vector3D.ZERO, Vector3D.ZERO);
51  
52      /** Serializable UID. */
53      private static final long serialVersionUID = 20140407L;
54  
55      /** The position. */
56      private final Vector3D position;
57  
58      /** The velocity. */
59      private final Vector3D velocity;
60  
61      /** The acceleration. */
62      private final Vector3D acceleration;
63  
64      /** Simple constructor.
65       * <p> Set the Coordinates to default : (0 0 0), (0 0 0), (0 0 0).</p>
66       */
67      public PVCoordinates() {
68          position     = Vector3D.ZERO;
69          velocity     = Vector3D.ZERO;
70          acceleration = Vector3D.ZERO;
71      }
72  
73      /** Builds a PVCoordinates triplet with zero acceleration.
74       * <p>Acceleration is set to zero</p>
75       * @param position the position vector (m)
76       * @param velocity the velocity vector (m/s)
77       */
78      public PVCoordinates(final Vector3D position, final Vector3D velocity) {
79          this.position     = position;
80          this.velocity     = velocity;
81          this.acceleration = Vector3D.ZERO;
82      }
83  
84      /** Builds a PVCoordinates triplet.
85       * @param position the position vector (m)
86       * @param velocity the velocity vector (m/s)
87       * @param acceleration the acceleration vector (m/s²)
88       */
89      public PVCoordinates(final Vector3D position, final Vector3D velocity, final Vector3D acceleration) {
90          this.position     = position;
91          this.velocity     = velocity;
92          this.acceleration = acceleration;
93      }
94  
95      /** Multiplicative constructor.
96       * <p>Build a PVCoordinates from another one and a scale factor.</p>
97       * <p>The PVCoordinates built will be a * pv</p>
98       * @param a scale factor
99       * @param pv base (unscaled) PVCoordinates
100      */
101     public PVCoordinates(final double a, final PVCoordinates pv) {
102         position     = new Vector3D(a, pv.position);
103         velocity     = new Vector3D(a, pv.velocity);
104         acceleration = new Vector3D(a, pv.acceleration);
105     }
106 
107     /** Subtractive constructor.
108      * <p>Build a relative PVCoordinates from a start and an end position.</p>
109      * <p>The PVCoordinates built will be end - start.</p>
110      * @param start Starting PVCoordinates
111      * @param end ending PVCoordinates
112      */
113     public PVCoordinates(final PVCoordinates start, final PVCoordinates end) {
114         this.position     = end.position.subtract(start.position);
115         this.velocity     = end.velocity.subtract(start.velocity);
116         this.acceleration = end.acceleration.subtract(start.acceleration);
117     }
118 
119     /** Linear constructor.
120      * <p>Build a PVCoordinates from two other ones and corresponding scale factors.</p>
121      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2</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      */
127     public PVCoordinates(final double a1, final PVCoordinates pv1,
128                          final double a2, final PVCoordinates pv2) {
129         position     = new Vector3D(a1, pv1.position,     a2, pv2.position);
130         velocity     = new Vector3D(a1, pv1.velocity,     a2, pv2.velocity);
131         acceleration = new Vector3D(a1, pv1.acceleration, a2, pv2.acceleration);
132     }
133 
134     /** Linear constructor.
135      * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
136      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
137      * @param a1 first scale factor
138      * @param pv1 first base (unscaled) PVCoordinates
139      * @param a2 second scale factor
140      * @param pv2 second base (unscaled) PVCoordinates
141      * @param a3 third scale factor
142      * @param pv3 third 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         position     = new Vector3D(a1, pv1.position,     a2, pv2.position,     a3, pv3.position);
148         velocity     = new Vector3D(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity);
149         acceleration = new Vector3D(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration);
150     }
151 
152     /** Linear constructor.
153      * <p>Build a PVCoordinates from four other ones and corresponding scale factors.</p>
154      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
155      * @param a1 first scale factor
156      * @param pv1 first base (unscaled) PVCoordinates
157      * @param a2 second scale factor
158      * @param pv2 second base (unscaled) PVCoordinates
159      * @param a3 third scale factor
160      * @param pv3 third base (unscaled) PVCoordinates
161      * @param a4 fourth scale factor
162      * @param pv4 fourth base (unscaled) PVCoordinates
163      */
164     public PVCoordinates(final double a1, final PVCoordinates pv1,
165                          final double a2, final PVCoordinates pv2,
166                          final double a3, final PVCoordinates pv3,
167                          final double a4, final PVCoordinates pv4) {
168         position     = new Vector3D(a1, pv1.position,     a2, pv2.position,
169                                     a3, pv3.position,     a4, pv4.position);
170         velocity     = new Vector3D(a1, pv1.velocity,     a2, pv2.velocity,
171                                     a3, pv3.velocity,     a4, pv4.velocity);
172         acceleration = new Vector3D(a1, pv1.acceleration, a2, pv2.acceleration,
173                                     a3, pv3.acceleration, a4, pv4.acceleration);
174     }
175 
176     /** Builds a PVCoordinates triplet from  a {@link FieldVector3D}&lt;{@link Derivative}&gt;.
177      * <p>
178      * The vector components must have time as their only derivation parameter and
179      * have consistent derivation orders.
180      * </p>
181      * @param p vector with time-derivatives embedded within the coordinates
182      * @param <U> type of the derivative
183      */
184     public <U extends Derivative<U>> PVCoordinates(final FieldVector3D<U> p) {
185         position = new Vector3D(p.getX().getReal(), p.getY().getReal(), p.getZ().getReal());
186         if (p.getX().getOrder() >= 1) {
187             velocity = new Vector3D(p.getX().getPartialDerivative(1),
188                                     p.getY().getPartialDerivative(1),
189                                     p.getZ().getPartialDerivative(1));
190             if (p.getX().getOrder() >= 2) {
191                 acceleration = new Vector3D(p.getX().getPartialDerivative(2),
192                                             p.getY().getPartialDerivative(2),
193                                             p.getZ().getPartialDerivative(2));
194             } else {
195                 acceleration = Vector3D.ZERO;
196             }
197         } else {
198             velocity     = Vector3D.ZERO;
199             acceleration = Vector3D.ZERO;
200         }
201     }
202 
203     /**
204      * Builds PV coordinates with the givne position, zero velocity, and zero
205      * acceleration.
206      *
207      * @param position position vector (m)
208      */
209     public PVCoordinates(final Vector3D position) {
210         this(position, Vector3D.ZERO);
211     }
212 
213     /** Transform the instance to a {@link FieldVector3D}&lt;{@link DerivativeStructure}&gt;.
214      * <p>
215      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
216      * to the user-specified order.
217      * </p>
218      * @param order derivation order for the vector components (must be either 0, 1 or 2)
219      * @return vector with time-derivatives embedded within the coordinates
220      */
221     public FieldVector3D<DerivativeStructure> toDerivativeStructureVector(final int order) {
222 
223         final DSFactory factory;
224         final DerivativeStructure x;
225         final DerivativeStructure y;
226         final DerivativeStructure z;
227         switch (order) {
228             case 0 :
229                 factory = new DSFactory(1, order);
230                 x = factory.build(position.getX());
231                 y = factory.build(position.getY());
232                 z = factory.build(position.getZ());
233                 break;
234             case 1 :
235                 factory = new DSFactory(1, order);
236                 x = factory.build(position.getX(), velocity.getX());
237                 y = factory.build(position.getY(), velocity.getY());
238                 z = factory.build(position.getZ(), velocity.getZ());
239                 break;
240             case 2 :
241                 factory = new DSFactory(1, order);
242                 x = factory.build(position.getX(), velocity.getX(), acceleration.getX());
243                 y = factory.build(position.getY(), velocity.getY(), acceleration.getY());
244                 z = factory.build(position.getZ(), velocity.getZ(), acceleration.getZ());
245                 break;
246             default :
247                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
248         }
249 
250         return new FieldVector3D<>(x, y, z);
251 
252     }
253 
254     /** Transform the instance to a {@link FieldVector3D}&lt;{@link UnivariateDerivative1}&gt;.
255      * <p>
256      * The {@link UnivariateDerivative1} coordinates correspond to time-derivatives up
257      * to the order 1.
258      * </p>
259      * @return vector with time-derivatives embedded within the coordinates
260      * @see #toUnivariateDerivative2Vector()
261      * @since 10.2
262      */
263     public FieldVector3D<UnivariateDerivative1> toUnivariateDerivative1Vector() {
264 
265         final UnivariateDerivative1 x = new UnivariateDerivative1(position.getX(), velocity.getX());
266         final UnivariateDerivative1 y = new UnivariateDerivative1(position.getY(), velocity.getY());
267         final UnivariateDerivative1 z = new UnivariateDerivative1(position.getZ(), velocity.getZ());
268 
269         return new FieldVector3D<>(x, y, z);
270     }
271 
272     /** Transform the instance to a {@link FieldVector3D}&lt;{@link UnivariateDerivative2}&gt;.
273      * <p>
274      * The {@link UnivariateDerivative2} coordinates correspond to time-derivatives up
275      * to the order 2.
276      * </p>
277      * @return vector with time-derivatives embedded within the coordinates
278      * @see #toUnivariateDerivative1Vector()
279      * @since 10.2
280      */
281     public FieldVector3D<UnivariateDerivative2> toUnivariateDerivative2Vector() {
282 
283         final UnivariateDerivative2 x = new UnivariateDerivative2(position.getX(), velocity.getX(), acceleration.getX());
284         final UnivariateDerivative2 y = new UnivariateDerivative2(position.getY(), velocity.getY(), acceleration.getY());
285         final UnivariateDerivative2 z = new UnivariateDerivative2(position.getZ(), velocity.getZ(), acceleration.getZ());
286 
287         return new FieldVector3D<>(x, y, z);
288     }
289 
290     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link DerivativeStructure}&gt;.
291      * <p>
292      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
293      * to the user-specified order. As both the instance components {@link #getPosition() position},
294      * {@link #getVelocity() velocity} and {@link #getAcceleration() acceleration} and the
295      * {@link DerivativeStructure#getPartialDerivative(int...) derivatives} of the components
296      * holds time-derivatives, there are several ways to retrieve these derivatives. If for example
297      * the {@code order} is set to 2, then both {@code pv.getPosition().getX().getPartialDerivative(2)},
298      * {@code pv.getVelocity().getX().getPartialDerivative(1)} and
299      * {@code pv.getAcceleration().getX().getValue()} return the exact same value.
300      * </p>
301      * <p>
302      * If derivation order is 1, the first derivative of acceleration will be computed as a
303      * Keplerian-only jerk. If derivation order is 2, the second derivative of velocity (which
304      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
305      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
306      * </p>
307      * @param order derivation order for the vector components (must be either 0, 1 or 2)
308      * @return pv coordinates with time-derivatives embedded within the coordinates
309      * @since 9.2
310      */
311     public FieldPVCoordinates<DerivativeStructure> toDerivativeStructurePV(final int order) {
312 
313         final DSFactory factory;
314         final DerivativeStructure x0;
315         final DerivativeStructure y0;
316         final DerivativeStructure z0;
317         final DerivativeStructure x1;
318         final DerivativeStructure y1;
319         final DerivativeStructure z1;
320         final DerivativeStructure x2;
321         final DerivativeStructure y2;
322         final DerivativeStructure z2;
323         switch (order) {
324             case 0 :
325                 factory = new DSFactory(1, order);
326                 x0 = factory.build(position.getX());
327                 y0 = factory.build(position.getY());
328                 z0 = factory.build(position.getZ());
329                 x1 = factory.build(velocity.getX());
330                 y1 = factory.build(velocity.getY());
331                 z1 = factory.build(velocity.getZ());
332                 x2 = factory.build(acceleration.getX());
333                 y2 = factory.build(acceleration.getY());
334                 z2 = factory.build(acceleration.getZ());
335                 break;
336             case 1 : {
337                 factory = new DSFactory(1, order);
338                 final double   r2            = position.getNormSq();
339                 final double   r             = FastMath.sqrt(r2);
340                 final double   pvOr2         = Vector3D.dotProduct(position, velocity) / r2;
341                 final double   a             = acceleration.getNorm();
342                 final double   aOr           = a / r;
343                 final Vector3D keplerianJerk = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
344                 x0 = factory.build(position.getX(),     velocity.getX());
345                 y0 = factory.build(position.getY(),     velocity.getY());
346                 z0 = factory.build(position.getZ(),     velocity.getZ());
347                 x1 = factory.build(velocity.getX(),     acceleration.getX());
348                 y1 = factory.build(velocity.getY(),     acceleration.getY());
349                 z1 = factory.build(velocity.getZ(),     acceleration.getZ());
350                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX());
351                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY());
352                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ());
353                 break;
354             }
355             case 2 : {
356                 factory = new DSFactory(1, order);
357                 final double   r2              = position.getNormSq();
358                 final double   r               = FastMath.sqrt(r2);
359                 final double   pvOr2           = Vector3D.dotProduct(position, velocity) / r2;
360                 final double   a               = acceleration.getNorm();
361                 final double   aOr             = a / r;
362                 final Vector3D keplerianJerk   = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
363                 final double   v2              = velocity.getNormSq();
364                 final double   pa              = Vector3D.dotProduct(position, acceleration);
365                 final double   aj              = Vector3D.dotProduct(acceleration, keplerianJerk);
366                 final Vector3D keplerianJounce = new Vector3D(-3 * (v2 + pa) / r2 + 15 * pvOr2 * pvOr2 - aOr, acceleration,
367                                                               4 * aOr * pvOr2 - aj / (a * r), velocity);
368                 x0 = factory.build(position.getX(),     velocity.getX(),      acceleration.getX());
369                 y0 = factory.build(position.getY(),     velocity.getY(),      acceleration.getY());
370                 z0 = factory.build(position.getZ(),     velocity.getZ(),      acceleration.getZ());
371                 x1 = factory.build(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
372                 y1 = factory.build(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
373                 z1 = factory.build(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
374                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
375                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
376                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());
377                 break;
378             }
379             default :
380                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
381         }
382 
383         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
384                                         new FieldVector3D<>(x1, y1, z1),
385                                         new FieldVector3D<>(x2, y2, z2));
386 
387     }
388 
389     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link UnivariateDerivative1}&gt;.
390      * <p>
391      * The {@link UnivariateDerivative1} coordinates correspond to time-derivatives up
392      * to the order 1.
393      * The first derivative of acceleration will be computed as a Keplerian-only jerk.
394      * </p>
395      * @return pv coordinates with time-derivatives embedded within the coordinates
396      * @since 10.2
397      */
398     public FieldPVCoordinates<UnivariateDerivative1> toUnivariateDerivative1PV() {
399 
400         final double   r2            = position.getNormSq();
401         final double   r             = FastMath.sqrt(r2);
402         final double   pvOr2         = Vector3D.dotProduct(position, velocity) / r2;
403         final double   a             = acceleration.getNorm();
404         final double   aOr           = a / r;
405         final Vector3D keplerianJerk = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
406 
407         final UnivariateDerivative1 x0 = new UnivariateDerivative1(position.getX(),     velocity.getX());
408         final UnivariateDerivative1 y0 = new UnivariateDerivative1(position.getY(),     velocity.getY());
409         final UnivariateDerivative1 z0 = new UnivariateDerivative1(position.getZ(),     velocity.getZ());
410         final UnivariateDerivative1 x1 = new UnivariateDerivative1(velocity.getX(),     acceleration.getX());
411         final UnivariateDerivative1 y1 = new UnivariateDerivative1(velocity.getY(),     acceleration.getY());
412         final UnivariateDerivative1 z1 = new UnivariateDerivative1(velocity.getZ(),     acceleration.getZ());
413         final UnivariateDerivative1 x2 = new UnivariateDerivative1(acceleration.getX(), keplerianJerk.getX());
414         final UnivariateDerivative1 y2 = new UnivariateDerivative1(acceleration.getY(), keplerianJerk.getY());
415         final UnivariateDerivative1 z2 = new UnivariateDerivative1(acceleration.getZ(), keplerianJerk.getZ());
416 
417         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
418                                         new FieldVector3D<>(x1, y1, z1),
419                                         new FieldVector3D<>(x2, y2, z2));
420 
421     }
422 
423     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link UnivariateDerivative2}&gt;.
424      * <p>
425      * The {@link UnivariateDerivative2} coordinates correspond to time-derivatives up
426      * to the order 2.
427      * As derivation order is 2, the second derivative of velocity (which
428      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
429      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
430      * </p>
431      * @return pv coordinates with time-derivatives embedded within the coordinates
432      * @since 10.2
433      */
434     public FieldPVCoordinates<UnivariateDerivative2> toUnivariateDerivative2PV() {
435 
436         final double   r2              = position.getNormSq();
437         final double   r               = FastMath.sqrt(r2);
438         final double   pvOr2           = Vector3D.dotProduct(position, velocity) / r2;
439         final double   a               = acceleration.getNorm();
440         final double   aOr             = a / r;
441         final Vector3D keplerianJerk   = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
442         final double   v2              = velocity.getNormSq();
443         final double   pa              = Vector3D.dotProduct(position, acceleration);
444         final double   aj              = Vector3D.dotProduct(acceleration, keplerianJerk);
445         final Vector3D keplerianJounce = new Vector3D(-3 * (v2 + pa) / r2 + 15 * pvOr2 * pvOr2 - aOr, acceleration,
446                                                       4 * aOr * pvOr2 - aj / (a * r), velocity);
447 
448         final UnivariateDerivative2 x0 = new UnivariateDerivative2(position.getX(),     velocity.getX(),      acceleration.getX());
449         final UnivariateDerivative2 y0 = new UnivariateDerivative2(position.getY(),     velocity.getY(),      acceleration.getY());
450         final UnivariateDerivative2 z0 = new UnivariateDerivative2(position.getZ(),     velocity.getZ(),      acceleration.getZ());
451         final UnivariateDerivative2 x1 = new UnivariateDerivative2(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
452         final UnivariateDerivative2 y1 = new UnivariateDerivative2(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
453         final UnivariateDerivative2 z1 = new UnivariateDerivative2(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
454         final UnivariateDerivative2 x2 = new UnivariateDerivative2(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
455         final UnivariateDerivative2 y2 = new UnivariateDerivative2(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
456         final UnivariateDerivative2 z2 = new UnivariateDerivative2(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());
457 
458         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
459                                         new FieldVector3D<>(x1, y1, z1),
460                                         new FieldVector3D<>(x2, y2, z2));
461 
462     }
463 
464     /** Estimate velocity between two positions.
465      * <p>Estimation is based on a simple fixed velocity translation
466      * during the time interval between the two positions.</p>
467      * @param start start position
468      * @param end end position
469      * @param dt time elapsed between the dates of the two positions
470      * @return velocity allowing to go from start to end positions
471      */
472     public static Vector3D estimateVelocity(final Vector3D start, final Vector3D end, final double dt) {
473         final double scale = 1.0 / dt;
474         return new Vector3D(scale, end, -scale, start);
475     }
476 
477     /** Get a time-shifted state.
478      * <p>
479      * The state can be slightly shifted to close dates. This shift is based on
480      * a simple Taylor expansion. It is <em>not</em> intended as a replacement for
481      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
482      * for either small time shifts or coarse accuracy.
483      * </p>
484      * @param dt time shift in seconds
485      * @return a new state, shifted with respect to the instance (which is immutable)
486      */
487     public PVCoordinates shiftedBy(final double dt) {
488         return new PVCoordinates(positionShiftedBy(dt),
489                                  new Vector3D(1, velocity, dt, acceleration),
490                                  acceleration);
491     }
492 
493     /**
494      * Get a time-shifted position. Same as {@link #shiftedBy(double)} except
495      * that only the sifted position is returned.
496      * <p>
497      * The state can be slightly shifted to close dates. This shift is based on
498      * a simple Taylor expansion. It is <em>not</em> intended as a replacement
499      * for proper orbit propagation (it is not even Keplerian!) but should be
500      * sufficient for either small time shifts or coarse accuracy.
501      * </p>
502      *
503      * @param dt time shift in seconds
504      * @return a new state, shifted with respect to the instance (which is
505      * immutable)
506      */
507     public Vector3D positionShiftedBy(final double dt) {
508         return new Vector3D(1, position, dt, velocity, 0.5 * dt * dt, acceleration);
509     }
510 
511     /** Gets the position.
512      * @return the position vector (m).
513      */
514     public Vector3D getPosition() {
515         return position;
516     }
517 
518     /** Gets the velocity.
519      * @return the velocity vector (m/s).
520      */
521     public Vector3D getVelocity() {
522         return velocity;
523     }
524 
525     /** Gets the acceleration.
526      * @return the acceleration vector (m/s²).
527      */
528     public Vector3D getAcceleration() {
529         return acceleration;
530     }
531 
532     /** Gets the momentum.
533      * <p>This vector is the p &otimes; v where p is position, v is velocity
534      * and &otimes; is cross product. To get the real physical angular momentum
535      * you need to multiply this vector by the mass.</p>
536      * <p>The returned vector is recomputed each time this method is called, it
537      * is not cached.</p>
538      * @return a new instance of the momentum vector (m²/s).
539      */
540     public Vector3D getMomentum() {
541         return Vector3D.crossProduct(position, velocity);
542     }
543 
544     /**
545      * Get the angular velocity (spin) of this point as seen from the origin.
546      *
547      * <p> The angular velocity vector is parallel to the {@link #getMomentum()
548      * angular momentum} and is computed by ω = p &times; v / ||p||²
549      *
550      * @return the angular velocity vector
551      * @see <a href="http://en.wikipedia.org/wiki/Angular_velocity">Angular Velocity on
552      *      Wikipedia</a>
553      */
554     public Vector3D getAngularVelocity() {
555         return this.getMomentum().scalarMultiply(1.0 / this.getPosition().getNormSq());
556     }
557 
558     /** Get the opposite of the instance.
559      * @return a new position-velocity which is opposite to the instance
560      */
561     public PVCoordinates negate() {
562         return new PVCoordinates(position.negate(), velocity.negate(), acceleration.negate());
563     }
564 
565     /** Normalize the position part of the instance.
566      * <p>
567      * The computed coordinates first component (position) will be a
568      * normalized vector, the second component (velocity) will be the
569      * derivative of the first component (hence it will generally not
570      * be normalized), and the third component (acceleration) will be the
571      * derivative of the second component (hence it will generally not
572      * be normalized).
573      * </p>
574      * @return a new instance, with first component normalized and
575      * remaining component computed to have consistent derivatives
576      */
577     public PVCoordinates normalize() {
578         final double   inv     = 1.0 / position.getNorm();
579         final Vector3D u       = new Vector3D(inv, position);
580         final Vector3D v       = new Vector3D(inv, velocity);
581         final Vector3D w       = new Vector3D(inv, acceleration);
582         final double   uv      = Vector3D.dotProduct(u, v);
583         final double   v2      = Vector3D.dotProduct(v, v);
584         final double   uw      = Vector3D.dotProduct(u, w);
585         final Vector3D uDot    = new Vector3D(1, v, -uv, u);
586         final Vector3D uDotDot = new Vector3D(1, w, -2 * uv, v, 3 * uv * uv - v2 - uw, u);
587         return new PVCoordinates(u, uDot, uDotDot);
588     }
589 
590     /** Compute the cross-product of two instances.
591      * @param pv1 first instances
592      * @param pv2 second instances
593      * @return the cross product v1 ^ v2 as a new instance
594      */
595     public static PVCoordinates crossProduct(final PVCoordinates pv1, final PVCoordinates pv2) {
596         final Vector3D p1 = pv1.position;
597         final Vector3D v1 = pv1.velocity;
598         final Vector3D a1 = pv1.acceleration;
599         final Vector3D p2 = pv2.position;
600         final Vector3D v2 = pv2.velocity;
601         final Vector3D a2 = pv2.acceleration;
602         return new PVCoordinates(Vector3D.crossProduct(p1, p2),
603                                  new Vector3D(1, Vector3D.crossProduct(p1, v2),
604                                               1, Vector3D.crossProduct(v1, p2)),
605                                  new Vector3D(1, Vector3D.crossProduct(p1, a2),
606                                               2, Vector3D.crossProduct(v1, v2),
607                                               1, Vector3D.crossProduct(a1, p2)));
608     }
609 
610     /** Return a string representation of this position/velocity pair.
611      * @return string representation of this position/velocity pair
612      */
613     public String toString() {
614         final String comma = ", ";
615         return new StringBuilder().append('{').append("P(").
616                 append(position.getX()).append(comma).
617                 append(position.getY()).append(comma).
618                 append(position.getZ()).append("), V(").
619                 append(velocity.getX()).append(comma).
620                 append(velocity.getY()).append(comma).
621                 append(velocity.getZ()).append("), A(").
622                 append(acceleration.getX()).append(comma).
623                 append(acceleration.getY()).append(comma).
624                 append(acceleration.getZ()).append(")}").toString();
625     }
626 
627     /** Replace the instance with a data transfer object for serialization.
628      * @return data transfer object that will be serialized
629      */
630     private Object writeReplace() {
631         return new DTO(this);
632     }
633 
634     /** Internal class used only for serialization. */
635     private static class DTO implements Serializable {
636 
637         /** Serializable UID. */
638         private static final long serialVersionUID = 20140723L;
639 
640         /** Double values. */
641         private double[] d;
642 
643         /** Simple constructor.
644          * @param pv instance to serialize
645          */
646         private DTO(final PVCoordinates pv) {
647             this.d = new double[] {
648                 pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
649                 pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ(),
650                 pv.getAcceleration().getX(), pv.getAcceleration().getY(), pv.getAcceleration().getZ(),
651             };
652         }
653 
654         /** Replace the deserialized data transfer object with a {@link PVCoordinates}.
655          * @return replacement {@link PVCoordinates}
656          */
657         private Object readResolve() {
658             return new PVCoordinates(new Vector3D(d[0], d[1], d[2]),
659                                      new Vector3D(d[3], d[4], d[5]),
660                                      new Vector3D(d[6], d[7], d[8]));
661         }
662 
663     }
664 
665 }