1   /* Copyright 2002-2021 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     /** Transform the instance to a {@link FieldVector3D}&lt;{@link DerivativeStructure}&gt;.
204      * <p>
205      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
206      * to the user-specified order.
207      * </p>
208      * @param order derivation order for the vector components (must be either 0, 1 or 2)
209      * @return vector with time-derivatives embedded within the coordinates
210      */
211     public FieldVector3D<DerivativeStructure> toDerivativeStructureVector(final int order) {
212 
213         final DSFactory factory;
214         final DerivativeStructure x;
215         final DerivativeStructure y;
216         final DerivativeStructure z;
217         switch(order) {
218             case 0 :
219                 factory = new DSFactory(1, order);
220                 x = factory.build(position.getX());
221                 y = factory.build(position.getY());
222                 z = factory.build(position.getZ());
223                 break;
224             case 1 :
225                 factory = new DSFactory(1, order);
226                 x = factory.build(position.getX(), velocity.getX());
227                 y = factory.build(position.getY(), velocity.getY());
228                 z = factory.build(position.getZ(), velocity.getZ());
229                 break;
230             case 2 :
231                 factory = new DSFactory(1, order);
232                 x = factory.build(position.getX(), velocity.getX(), acceleration.getX());
233                 y = factory.build(position.getY(), velocity.getY(), acceleration.getY());
234                 z = factory.build(position.getZ(), velocity.getZ(), acceleration.getZ());
235                 break;
236             default :
237                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
238         }
239 
240         return new FieldVector3D<>(x, y, z);
241 
242     }
243 
244     /** Transform the instance to a {@link FieldVector3D}&lt;{@link UnivariateDerivative1}&gt;.
245      * <p>
246      * The {@link UnivariateDerivative1} coordinates correspond to time-derivatives up
247      * to the order 1.
248      * </p>
249      * @return vector with time-derivatives embedded within the coordinates
250      * @see #toUnivariateDerivative2Vector()
251      * @since 10.2
252      */
253     public FieldVector3D<UnivariateDerivative1> toUnivariateDerivative1Vector() {
254 
255         final UnivariateDerivative1 x = new UnivariateDerivative1(position.getX(), velocity.getX());
256         final UnivariateDerivative1 y = new UnivariateDerivative1(position.getY(), velocity.getY());
257         final UnivariateDerivative1 z = new UnivariateDerivative1(position.getZ(), velocity.getZ());
258 
259         return new FieldVector3D<>(x, y, z);
260     }
261 
262     /** Transform the instance to a {@link FieldVector3D}&lt;{@link UnivariateDerivative2}&gt;.
263      * <p>
264      * The {@link UnivariateDerivative2} coordinates correspond to time-derivatives up
265      * to the order 2.
266      * </p>
267      * @return vector with time-derivatives embedded within the coordinates
268      * @see #toUnivariateDerivative1Vector()
269      * @since 10.2
270      */
271     public FieldVector3D<UnivariateDerivative2> toUnivariateDerivative2Vector() {
272 
273         final UnivariateDerivative2 x = new UnivariateDerivative2(position.getX(), velocity.getX(), acceleration.getX());
274         final UnivariateDerivative2 y = new UnivariateDerivative2(position.getY(), velocity.getY(), acceleration.getY());
275         final UnivariateDerivative2 z = new UnivariateDerivative2(position.getZ(), velocity.getZ(), acceleration.getZ());
276 
277         return new FieldVector3D<>(x, y, z);
278     }
279 
280     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link DerivativeStructure}&gt;.
281      * <p>
282      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
283      * to the user-specified order. As both the instance components {@link #getPosition() position},
284      * {@link #getVelocity() velocity} and {@link #getAcceleration() acceleration} and the
285      * {@link DerivativeStructure#getPartialDerivative(int...) derivatives} of the components
286      * holds time-derivatives, there are several ways to retrieve these derivatives. If for example
287      * the {@code order} is set to 2, then both {@code pv.getPosition().getX().getPartialDerivative(2)},
288      * {@code pv.getVelocity().getX().getPartialDerivative(1)} and
289      * {@code pv.getAcceleration().getX().getValue()} return the exact same value.
290      * </p>
291      * <p>
292      * If derivation order is 1, the first derivative of acceleration will be computed as a
293      * Keplerian-only jerk. If derivation order is 2, the second derivative of velocity (which
294      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
295      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
296      * </p>
297      * @param order derivation order for the vector components (must be either 0, 1 or 2)
298      * @return pv coordinates with time-derivatives embedded within the coordinates
299      * @since 9.2
300      */
301     public FieldPVCoordinates<DerivativeStructure> toDerivativeStructurePV(final int order) {
302 
303         final DSFactory factory;
304         final DerivativeStructure x0;
305         final DerivativeStructure y0;
306         final DerivativeStructure z0;
307         final DerivativeStructure x1;
308         final DerivativeStructure y1;
309         final DerivativeStructure z1;
310         final DerivativeStructure x2;
311         final DerivativeStructure y2;
312         final DerivativeStructure z2;
313         switch(order) {
314             case 0 :
315                 factory = new DSFactory(1, order);
316                 x0 = factory.build(position.getX());
317                 y0 = factory.build(position.getY());
318                 z0 = factory.build(position.getZ());
319                 x1 = factory.build(velocity.getX());
320                 y1 = factory.build(velocity.getY());
321                 z1 = factory.build(velocity.getZ());
322                 x2 = factory.build(acceleration.getX());
323                 y2 = factory.build(acceleration.getY());
324                 z2 = factory.build(acceleration.getZ());
325                 break;
326             case 1 : {
327                 factory = new DSFactory(1, order);
328                 final double   r2            = position.getNormSq();
329                 final double   r             = FastMath.sqrt(r2);
330                 final double   pvOr2         = Vector3D.dotProduct(position, velocity) / r2;
331                 final double   a             = acceleration.getNorm();
332                 final double   aOr           = a / r;
333                 final Vector3D keplerianJerk = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
334                 x0 = factory.build(position.getX(),     velocity.getX());
335                 y0 = factory.build(position.getY(),     velocity.getY());
336                 z0 = factory.build(position.getZ(),     velocity.getZ());
337                 x1 = factory.build(velocity.getX(),     acceleration.getX());
338                 y1 = factory.build(velocity.getY(),     acceleration.getY());
339                 z1 = factory.build(velocity.getZ(),     acceleration.getZ());
340                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX());
341                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY());
342                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ());
343                 break;
344             }
345             case 2 : {
346                 factory = new DSFactory(1, order);
347                 final double   r2              = position.getNormSq();
348                 final double   r               = FastMath.sqrt(r2);
349                 final double   pvOr2           = Vector3D.dotProduct(position, velocity) / r2;
350                 final double   a               = acceleration.getNorm();
351                 final double   aOr             = a / r;
352                 final Vector3D keplerianJerk   = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
353                 final double   v2              = velocity.getNormSq();
354                 final double   pa              = Vector3D.dotProduct(position, acceleration);
355                 final double   aj              = Vector3D.dotProduct(acceleration, keplerianJerk);
356                 final Vector3D keplerianJounce = new Vector3D(-3 * (v2 + pa) / r2 + 15 * pvOr2 * pvOr2 - aOr, acceleration,
357                                                               4 * aOr * pvOr2 - aj / (a * r), velocity);
358                 x0 = factory.build(position.getX(),     velocity.getX(),      acceleration.getX());
359                 y0 = factory.build(position.getY(),     velocity.getY(),      acceleration.getY());
360                 z0 = factory.build(position.getZ(),     velocity.getZ(),      acceleration.getZ());
361                 x1 = factory.build(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
362                 y1 = factory.build(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
363                 z1 = factory.build(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
364                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
365                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
366                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());
367                 break;
368             }
369             default :
370                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
371         }
372 
373         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
374                                         new FieldVector3D<>(x1, y1, z1),
375                                         new FieldVector3D<>(x2, y2, z2));
376 
377     }
378 
379     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link UnivariateDerivative1}&gt;.
380      * <p>
381      * The {@link UnivariateDerivative1} coordinates correspond to time-derivatives up
382      * to the order 1.
383      * The first derivative of acceleration will be computed as a Keplerian-only jerk.
384      * </p>
385      * @return pv coordinates with time-derivatives embedded within the coordinates
386      * @since 10.2
387      */
388     public FieldPVCoordinates<UnivariateDerivative1> toUnivariateDerivative1PV() {
389 
390         final double   r2            = position.getNormSq();
391         final double   r             = FastMath.sqrt(r2);
392         final double   pvOr2         = Vector3D.dotProduct(position, velocity) / r2;
393         final double   a             = acceleration.getNorm();
394         final double   aOr           = a / r;
395         final Vector3D keplerianJerk = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
396 
397         final UnivariateDerivative1 x0 = new UnivariateDerivative1(position.getX(),     velocity.getX());
398         final UnivariateDerivative1 y0 = new UnivariateDerivative1(position.getY(),     velocity.getY());
399         final UnivariateDerivative1 z0 = new UnivariateDerivative1(position.getZ(),     velocity.getZ());
400         final UnivariateDerivative1 x1 = new UnivariateDerivative1(velocity.getX(),     acceleration.getX());
401         final UnivariateDerivative1 y1 = new UnivariateDerivative1(velocity.getY(),     acceleration.getY());
402         final UnivariateDerivative1 z1 = new UnivariateDerivative1(velocity.getZ(),     acceleration.getZ());
403         final UnivariateDerivative1 x2 = new UnivariateDerivative1(acceleration.getX(), keplerianJerk.getX());
404         final UnivariateDerivative1 y2 = new UnivariateDerivative1(acceleration.getY(), keplerianJerk.getY());
405         final UnivariateDerivative1 z2 = new UnivariateDerivative1(acceleration.getZ(), keplerianJerk.getZ());
406 
407         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
408                                         new FieldVector3D<>(x1, y1, z1),
409                                         new FieldVector3D<>(x2, y2, z2));
410 
411     }
412 
413     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link UnivariateDerivative2}&gt;.
414      * <p>
415      * The {@link UnivariateDerivative2} coordinates correspond to time-derivatives up
416      * to the order 2.
417      * As derivation order is 2, the second derivative of velocity (which
418      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
419      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
420      * </p>
421      * @return pv coordinates with time-derivatives embedded within the coordinates
422      * @since 10.2
423      */
424     public FieldPVCoordinates<UnivariateDerivative2> toUnivariateDerivative2PV() {
425 
426         final double   r2              = position.getNormSq();
427         final double   r               = FastMath.sqrt(r2);
428         final double   pvOr2           = Vector3D.dotProduct(position, velocity) / r2;
429         final double   a               = acceleration.getNorm();
430         final double   aOr             = a / r;
431         final Vector3D keplerianJerk   = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
432         final double   v2              = velocity.getNormSq();
433         final double   pa              = Vector3D.dotProduct(position, acceleration);
434         final double   aj              = Vector3D.dotProduct(acceleration, keplerianJerk);
435         final Vector3D keplerianJounce = new Vector3D(-3 * (v2 + pa) / r2 + 15 * pvOr2 * pvOr2 - aOr, acceleration,
436                                                       4 * aOr * pvOr2 - aj / (a * r), velocity);
437 
438         final UnivariateDerivative2 x0 = new UnivariateDerivative2(position.getX(),     velocity.getX(),      acceleration.getX());
439         final UnivariateDerivative2 y0 = new UnivariateDerivative2(position.getY(),     velocity.getY(),      acceleration.getY());
440         final UnivariateDerivative2 z0 = new UnivariateDerivative2(position.getZ(),     velocity.getZ(),      acceleration.getZ());
441         final UnivariateDerivative2 x1 = new UnivariateDerivative2(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
442         final UnivariateDerivative2 y1 = new UnivariateDerivative2(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
443         final UnivariateDerivative2 z1 = new UnivariateDerivative2(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
444         final UnivariateDerivative2 x2 = new UnivariateDerivative2(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
445         final UnivariateDerivative2 y2 = new UnivariateDerivative2(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
446         final UnivariateDerivative2 z2 = new UnivariateDerivative2(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());
447 
448         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
449                                         new FieldVector3D<>(x1, y1, z1),
450                                         new FieldVector3D<>(x2, y2, z2));
451 
452     }
453 
454     /** Estimate velocity between two positions.
455      * <p>Estimation is based on a simple fixed velocity translation
456      * during the time interval between the two positions.</p>
457      * @param start start position
458      * @param end end position
459      * @param dt time elapsed between the dates of the two positions
460      * @return velocity allowing to go from start to end positions
461      */
462     public static Vector3D estimateVelocity(final Vector3D start, final Vector3D end, final double dt) {
463         final double scale = 1.0 / dt;
464         return new Vector3D(scale, end, -scale, start);
465     }
466 
467     /** Get a time-shifted state.
468      * <p>
469      * The state can be slightly shifted to close dates. This shift is based on
470      * a simple Taylor expansion. It is <em>not</em> intended as a replacement for
471      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
472      * for either small time shifts or coarse accuracy.
473      * </p>
474      * @param dt time shift in seconds
475      * @return a new state, shifted with respect to the instance (which is immutable)
476      */
477     public PVCoordinates shiftedBy(final double dt) {
478         return new PVCoordinates(new Vector3D(1, position, dt, velocity, 0.5 * dt * dt, acceleration),
479                                  new Vector3D(1, velocity, dt, acceleration),
480                                  acceleration);
481     }
482 
483     /** Gets the position.
484      * @return the position vector (m).
485      */
486     public Vector3D getPosition() {
487         return position;
488     }
489 
490     /** Gets the velocity.
491      * @return the velocity vector (m/s).
492      */
493     public Vector3D getVelocity() {
494         return velocity;
495     }
496 
497     /** Gets the acceleration.
498      * @return the acceleration vector (m/s²).
499      */
500     public Vector3D getAcceleration() {
501         return acceleration;
502     }
503 
504     /** Gets the momentum.
505      * <p>This vector is the p &otimes; v where p is position, v is velocity
506      * and &otimes; is cross product. To get the real physical angular momentum
507      * you need to multiply this vector by the mass.</p>
508      * <p>The returned vector is recomputed each time this method is called, it
509      * is not cached.</p>
510      * @return a new instance of the momentum vector (m²/s).
511      */
512     public Vector3D getMomentum() {
513         return Vector3D.crossProduct(position, velocity);
514     }
515 
516     /**
517      * Get the angular velocity (spin) of this point as seen from the origin.
518      *
519      * <p> The angular velocity vector is parallel to the {@link #getMomentum()
520      * angular momentum} and is computed by ω = p &times; v / ||p||²
521      *
522      * @return the angular velocity vector
523      * @see <a href="http://en.wikipedia.org/wiki/Angular_velocity">Angular Velocity on
524      *      Wikipedia</a>
525      */
526     public Vector3D getAngularVelocity() {
527         return this.getMomentum().scalarMultiply(1.0 / this.getPosition().getNormSq());
528     }
529 
530     /** Get the opposite of the instance.
531      * @return a new position-velocity which is opposite to the instance
532      */
533     public PVCoordinates negate() {
534         return new PVCoordinates(position.negate(), velocity.negate(), acceleration.negate());
535     }
536 
537     /** Normalize the position part of the instance.
538      * <p>
539      * The computed coordinates first component (position) will be a
540      * normalized vector, the second component (velocity) will be the
541      * derivative of the first component (hence it will generally not
542      * be normalized), and the third component (acceleration) will be the
543      * derivative of the second component (hence it will generally not
544      * be normalized).
545      * </p>
546      * @return a new instance, with first component normalized and
547      * remaining component computed to have consistent derivatives
548      */
549     public PVCoordinates normalize() {
550         final double   inv     = 1.0 / position.getNorm();
551         final Vector3D u       = new Vector3D(inv, position);
552         final Vector3D v       = new Vector3D(inv, velocity);
553         final Vector3D w       = new Vector3D(inv, acceleration);
554         final double   uv      = Vector3D.dotProduct(u, v);
555         final double   v2      = Vector3D.dotProduct(v, v);
556         final double   uw      = Vector3D.dotProduct(u, w);
557         final Vector3D uDot    = new Vector3D(1, v, -uv, u);
558         final Vector3D uDotDot = new Vector3D(1, w, -2 * uv, v, 3 * uv * uv - v2 - uw, u);
559         return new PVCoordinates(u, uDot, uDotDot);
560     }
561 
562     /** Compute the cross-product of two instances.
563      * @param pv1 first instances
564      * @param pv2 second instances
565      * @return the cross product v1 ^ v2 as a new instance
566      */
567     public static PVCoordinates crossProduct(final PVCoordinates pv1, final PVCoordinates pv2) {
568         final Vector3D p1 = pv1.position;
569         final Vector3D v1 = pv1.velocity;
570         final Vector3D a1 = pv1.acceleration;
571         final Vector3D p2 = pv2.position;
572         final Vector3D v2 = pv2.velocity;
573         final Vector3D a2 = pv2.acceleration;
574         return new PVCoordinates(Vector3D.crossProduct(p1, p2),
575                                  new Vector3D(1, Vector3D.crossProduct(p1, v2),
576                                               1, Vector3D.crossProduct(v1, p2)),
577                                  new Vector3D(1, Vector3D.crossProduct(p1, a2),
578                                               2, Vector3D.crossProduct(v1, v2),
579                                               1, Vector3D.crossProduct(a1, p2)));
580     }
581 
582     /** Return a string representation of this position/velocity pair.
583      * @return string representation of this position/velocity pair
584      */
585     public String toString() {
586         final String comma = ", ";
587         return new StringBuffer().append('{').append("P(").
588                 append(position.getX()).append(comma).
589                 append(position.getY()).append(comma).
590                 append(position.getZ()).append("), V(").
591                 append(velocity.getX()).append(comma).
592                 append(velocity.getY()).append(comma).
593                 append(velocity.getZ()).append("), A(").
594                 append(acceleration.getX()).append(comma).
595                 append(acceleration.getY()).append(comma).
596                 append(acceleration.getZ()).append(")}").toString();
597     }
598 
599     /** Replace the instance with a data transfer object for serialization.
600      * @return data transfer object that will be serialized
601      */
602     private Object writeReplace() {
603         return new DTO(this);
604     }
605 
606     /** Internal class used only for serialization. */
607     private static class DTO implements Serializable {
608 
609         /** Serializable UID. */
610         private static final long serialVersionUID = 20140723L;
611 
612         /** Double values. */
613         private double[] d;
614 
615         /** Simple constructor.
616          * @param pv instance to serialize
617          */
618         private DTO(final PVCoordinates pv) {
619             this.d = new double[] {
620                 pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
621                 pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ(),
622                 pv.getAcceleration().getX(), pv.getAcceleration().getY(), pv.getAcceleration().getZ(),
623             };
624         }
625 
626         /** Replace the deserialized data transfer object with a {@link PVCoordinates}.
627          * @return replacement {@link PVCoordinates}
628          */
629         private Object readResolve() {
630             return new PVCoordinates(new Vector3D(d[0], d[1], d[2]),
631                                      new Vector3D(d[3], d[4], d[5]),
632                                      new Vector3D(d[6], d[7], d[8]));
633         }
634 
635     }
636 
637 }