1 /* Copyright 2002-2016 CS Systèmes d'Information
2 * Licensed to CS Systèmes d'Information (CS) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * CS licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.orekit.utils;
18
19 import java.io.Serializable;
20
21 import org.hipparchus.analysis.differentiation.DerivativeStructure;
22 import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
23 import org.hipparchus.geometry.euclidean.threed.Vector3D;
24 import org.orekit.errors.OrekitException;
25 import org.orekit.errors.OrekitMessages;
26 import org.orekit.time.TimeShiftable;
27
28 /** Simple container for Position/Velocity/Acceleration triplets.
29 * <p>
30 * The state can be slightly shifted to close dates. This shift is based on
31 * a simple quadratic model. It is <em>not</em> intended as a replacement for
32 * proper orbit propagation (it is not even Keplerian!) but should be sufficient
33 * for either small time shifts or coarse accuracy.
34 * </p>
35 * <p>
36 * This class is the angular counterpart to {@link AngularCoordinates}.
37 * </p>
38 * <p>Instances of this class are guaranteed to be immutable.</p>
39 * @author Fabien Maussion
40 * @author Luc Maisonobe
41 */
42 public class PVCoordinates implements TimeShiftable<PVCoordinates>, Serializable {
43
44 /** Fixed position/velocity at origin (both p, v and a are zero vectors). */
45 public static final PVCoordinates ZERO = new PVCoordinates(Vector3D.ZERO, Vector3D.ZERO, Vector3D.ZERO);
46
47 /** Serializable UID. */
48 private static final long serialVersionUID = 20140407L;
49
50 /** The position. */
51 private final Vector3D position;
52
53 /** The velocity. */
54 private final Vector3D velocity;
55
56 /** The acceleration. */
57 private final Vector3D acceleration;
58
59 /** Simple constructor.
60 * <p> Set the Coordinates to default : (0 0 0), (0 0 0), (0 0 0).</p>
61 */
62 public PVCoordinates() {
63 position = Vector3D.ZERO;
64 velocity = Vector3D.ZERO;
65 acceleration = Vector3D.ZERO;
66 }
67
68 /** Builds a PVCoordinates triplet with zero acceleration.
69 * <p>Acceleration is set to zero</p>
70 * @param position the position vector (m)
71 * @param velocity the velocity vector (m/s)
72 */
73 public PVCoordinates(final Vector3D position, final Vector3D velocity) {
74 this.position = position;
75 this.velocity = velocity;
76 this.acceleration = Vector3D.ZERO;
77 }
78
79 /** Builds a PVCoordinates triplet.
80 * @param position the position vector (m)
81 * @param velocity the velocity vector (m/s)
82 * @param acceleration the acceleration vector (m/s²)
83 */
84 public PVCoordinates(final Vector3D position, final Vector3D velocity, final Vector3D acceleration) {
85 this.position = position;
86 this.velocity = velocity;
87 this.acceleration = acceleration;
88 }
89
90 /** Multiplicative constructor.
91 * <p>Build a PVCoordinates from another one and a scale factor.</p>
92 * <p>The PVCoordinates built will be a * pv</p>
93 * @param a scale factor
94 * @param pv base (unscaled) PVCoordinates
95 */
96 public PVCoordinates(final double a, final PVCoordinates pv) {
97 position = new Vector3D(a, pv.position);
98 velocity = new Vector3D(a, pv.velocity);
99 acceleration = new Vector3D(a, pv.acceleration);
100 }
101
102 /** Subtractive constructor.
103 * <p>Build a relative PVCoordinates from a start and an end position.</p>
104 * <p>The PVCoordinates built will be end - start.</p>
105 * @param start Starting PVCoordinates
106 * @param end ending PVCoordinates
107 */
108 public PVCoordinates(final PVCoordinates start, final PVCoordinates end) {
109 this.position = end.position.subtract(start.position);
110 this.velocity = end.velocity.subtract(start.velocity);
111 this.acceleration = end.acceleration.subtract(start.acceleration);
112 }
113
114 /** Linear constructor.
115 * <p>Build a PVCoordinates from two other ones and corresponding scale factors.</p>
116 * <p>The PVCoordinates built will be a1 * u1 + a2 * u2</p>
117 * @param a1 first scale factor
118 * @param pv1 first base (unscaled) PVCoordinates
119 * @param a2 second scale factor
120 * @param pv2 second base (unscaled) PVCoordinates
121 */
122 public PVCoordinates(final double a1, final PVCoordinates pv1,
123 final double a2, final PVCoordinates pv2) {
124 position = new Vector3D(a1, pv1.position, a2, pv2.position);
125 velocity = new Vector3D(a1, pv1.velocity, a2, pv2.velocity);
126 acceleration = new Vector3D(a1, pv1.acceleration, a2, pv2.acceleration);
127 }
128
129 /** Linear constructor.
130 * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
131 * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
132 * @param a1 first scale factor
133 * @param pv1 first base (unscaled) PVCoordinates
134 * @param a2 second scale factor
135 * @param pv2 second base (unscaled) PVCoordinates
136 * @param a3 third scale factor
137 * @param pv3 third base (unscaled) PVCoordinates
138 */
139 public PVCoordinates(final double a1, final PVCoordinates pv1,
140 final double a2, final PVCoordinates pv2,
141 final double a3, final PVCoordinates pv3) {
142 position = new Vector3D(a1, pv1.position, a2, pv2.position, a3, pv3.position);
143 velocity = new Vector3D(a1, pv1.velocity, a2, pv2.velocity, a3, pv3.velocity);
144 acceleration = new Vector3D(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration);
145 }
146
147 /** Linear constructor.
148 * <p>Build a PVCoordinates from four other ones and corresponding scale factors.</p>
149 * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
150 * @param a1 first scale factor
151 * @param pv1 first base (unscaled) PVCoordinates
152 * @param a2 second scale factor
153 * @param pv2 second base (unscaled) PVCoordinates
154 * @param a3 third scale factor
155 * @param pv3 third base (unscaled) PVCoordinates
156 * @param a4 fourth scale factor
157 * @param pv4 fourth base (unscaled) PVCoordinates
158 */
159 public PVCoordinates(final double a1, final PVCoordinates pv1,
160 final double a2, final PVCoordinates pv2,
161 final double a3, final PVCoordinates pv3,
162 final double a4, final PVCoordinates pv4) {
163 position = new Vector3D(a1, pv1.position, a2, pv2.position,
164 a3, pv3.position, a4, pv4.position);
165 velocity = new Vector3D(a1, pv1.velocity, a2, pv2.velocity,
166 a3, pv3.velocity, a4, pv4.velocity);
167 acceleration = new Vector3D(a1, pv1.acceleration, a2, pv2.acceleration,
168 a3, pv3.acceleration, a4, pv4.acceleration);
169 }
170
171 /** Builds a PVCoordinates triplet from a {@link FieldVector3D}<{@link DerivativeStructure}>.
172 * <p>
173 * The vector components must have time as their only derivation parameter and
174 * have consistent derivation orders.
175 * </p>
176 * @param p vector with time-derivatives embedded within the coordinates
177 */
178 public PVCoordinates(final FieldVector3D<DerivativeStructure> p) {
179 position = new Vector3D(p.getX().getReal(), p.getY().getReal(), p.getZ().getReal());
180 if (p.getX().getOrder() >= 1) {
181 velocity = new Vector3D(p.getX().getPartialDerivative(1),
182 p.getY().getPartialDerivative(1),
183 p.getZ().getPartialDerivative(1));
184 if (p.getX().getOrder() >= 2) {
185 acceleration = new Vector3D(p.getX().getPartialDerivative(2),
186 p.getY().getPartialDerivative(2),
187 p.getZ().getPartialDerivative(2));
188 } else {
189 acceleration = Vector3D.ZERO;
190 }
191 } else {
192 velocity = Vector3D.ZERO;
193 acceleration = Vector3D.ZERO;
194 }
195 }
196
197 /** Transform the instance to a {@link FieldVector3D}<{@link DerivativeStructure}>.
198 * <p>
199 * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
200 * to the user-specified order.
201 * </p>
202 * @param order derivation order for the vector components (must be either 0, 1 or 2)
203 * @return vector with time-derivatives embedded within the coordinates
204 * @exception OrekitException if the user specified order is too large
205 */
206 public FieldVector3D<DerivativeStructure> toDerivativeStructureVector(final int order)
207 throws OrekitException {
208
209 final DerivativeStructure x;
210 final DerivativeStructure y;
211 final DerivativeStructure z;
212 switch(order) {
213 case 0 :
214 x = new DerivativeStructure(1, 0, position.getX());
215 y = new DerivativeStructure(1, 0, position.getY());
216 z = new DerivativeStructure(1, 0, position.getZ());
217 break;
218 case 1 :
219 x = new DerivativeStructure(1, 1, position.getX(), velocity.getX());
220 y = new DerivativeStructure(1, 1, position.getY(), velocity.getY());
221 z = new DerivativeStructure(1, 1, position.getZ(), velocity.getZ());
222 break;
223 case 2 :
224 x = new DerivativeStructure(1, 2, position.getX(), velocity.getX(), acceleration.getX());
225 y = new DerivativeStructure(1, 2, position.getY(), velocity.getY(), acceleration.getY());
226 z = new DerivativeStructure(1, 2, position.getZ(), velocity.getZ(), acceleration.getZ());
227 break;
228 default :
229 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
230 }
231
232 return new FieldVector3D<DerivativeStructure>(x, y, z);
233
234 }
235
236 /** Estimate velocity between two positions.
237 * <p>Estimation is based on a simple fixed velocity translation
238 * during the time interval between the two positions.</p>
239 * @param start start position
240 * @param end end position
241 * @param dt time elapsed between the dates of the two positions
242 * @return velocity allowing to go from start to end positions
243 */
244 public static Vector3D estimateVelocity(final Vector3D start, final Vector3D end, final double dt) {
245 final double scale = 1.0 / dt;
246 return new Vector3D(scale, end, -scale, start);
247 }
248
249 /** Get a time-shifted state.
250 * <p>
251 * The state can be slightly shifted to close dates. This shift is based on
252 * a simple Taylor expansion. It is <em>not</em> intended as a replacement for
253 * proper orbit propagation (it is not even Keplerian!) but should be sufficient
254 * for either small time shifts or coarse accuracy.
255 * </p>
256 * @param dt time shift in seconds
257 * @return a new state, shifted with respect to the instance (which is immutable)
258 */
259 public PVCoordinates shiftedBy(final double dt) {
260 return new PVCoordinates(new Vector3D(1, position, dt, velocity, 0.5 * dt * dt, acceleration),
261 new Vector3D(1, velocity, dt, acceleration),
262 acceleration);
263 }
264
265 /** Gets the position.
266 * @return the position vector (m).
267 */
268 public Vector3D getPosition() {
269 return position;
270 }
271
272 /** Gets the velocity.
273 * @return the velocity vector (m/s).
274 */
275 public Vector3D getVelocity() {
276 return velocity;
277 }
278
279 /** Gets the acceleration.
280 * @return the acceleration vector (m/s²).
281 */
282 public Vector3D getAcceleration() {
283 return acceleration;
284 }
285
286 /** Gets the momentum.
287 * <p>This vector is the p ⊗ v where p is position, v is velocity
288 * and ⊗ is cross product. To get the real physical angular momentum
289 * you need to multiply this vector by the mass.</p>
290 * <p>The returned vector is recomputed each time this method is called, it
291 * is not cached.</p>
292 * @return a new instance of the momentum vector (m²/s).
293 */
294 public Vector3D getMomentum() {
295 return Vector3D.crossProduct(position, velocity);
296 }
297
298 /**
299 * Get the angular velocity (spin) of this point as seen from the origin.
300 *
301 * <p> The angular velocity vector is parallel to the {@link #getMomentum()
302 * angular momentum} and is computed by ω = p × v / ||p||²
303 *
304 * @return the angular velocity vector
305 * @see <a href="http://en.wikipedia.org/wiki/Angular_velocity">Angular Velocity on
306 * Wikipedia</a>
307 */
308 public Vector3D getAngularVelocity() {
309 return this.getMomentum().scalarMultiply(1.0 / this.getPosition().getNormSq());
310 }
311
312 /** Get the opposite of the instance.
313 * @return a new position-velocity which is opposite to the instance
314 */
315 public PVCoordinates negate() {
316 return new PVCoordinates(position.negate(), velocity.negate(), acceleration.negate());
317 }
318
319 /** Normalize the position part of the instance.
320 * <p>
321 * The computed coordinates first component (position) will be a
322 * normalized vector, the second component (velocity) will be the
323 * derivative of the first component (hence it will generally not
324 * be normalized), and the third component (acceleration) will be the
325 * derivative of the second component (hence it will generally not
326 * be normalized).
327 * </p>
328 * @return a new instance, with first component normalized and
329 * remaining component computed to have consistent derivatives
330 */
331 public PVCoordinates normalize() {
332 final double inv = 1.0 / position.getNorm();
333 final Vector3D u = new Vector3D(inv, position);
334 final Vector3D v = new Vector3D(inv, velocity);
335 final Vector3D w = new Vector3D(inv, acceleration);
336 final double uv = Vector3D.dotProduct(u, v);
337 final double v2 = Vector3D.dotProduct(v, v);
338 final double uw = Vector3D.dotProduct(u, w);
339 final Vector3D uDot = new Vector3D(1, v, -uv, u);
340 final Vector3D uDotDot = new Vector3D(1, w, -2 * uv, v, 3 * uv * uv - v2 - uw, u);
341 return new PVCoordinates(u, uDot, uDotDot);
342 }
343
344 /** Compute the cross-product of two instances.
345 * @param pv1 first instances
346 * @param pv2 second instances
347 * @return the cross product v1 ^ v2 as a new instance
348 */
349 public static PVCoordinates crossProduct(final PVCoordinates pv1, final PVCoordinates pv2) {
350 final Vector3D p1 = pv1.position;
351 final Vector3D v1 = pv1.velocity;
352 final Vector3D a1 = pv1.acceleration;
353 final Vector3D p2 = pv2.position;
354 final Vector3D v2 = pv2.velocity;
355 final Vector3D a2 = pv2.acceleration;
356 return new PVCoordinates(Vector3D.crossProduct(p1, p2),
357 new Vector3D(1, Vector3D.crossProduct(p1, v2),
358 1, Vector3D.crossProduct(v1, p2)),
359 new Vector3D(1, Vector3D.crossProduct(p1, a2),
360 2, Vector3D.crossProduct(v1, v2),
361 1, Vector3D.crossProduct(a1, p2)));
362 }
363
364 /** Return a string representation of this position/velocity pair.
365 * @return string representation of this position/velocity pair
366 */
367 public String toString() {
368 final String comma = ", ";
369 return new StringBuffer().append('{').append("P(").
370 append(position.getX()).append(comma).
371 append(position.getY()).append(comma).
372 append(position.getZ()).append("), V(").
373 append(velocity.getX()).append(comma).
374 append(velocity.getY()).append(comma).
375 append(velocity.getZ()).append("), A(").
376 append(acceleration.getX()).append(comma).
377 append(acceleration.getY()).append(comma).
378 append(acceleration.getZ()).append(")}").toString();
379 }
380
381 /** Replace the instance with a data transfer object for serialization.
382 * @return data transfer object that will be serialized
383 */
384 private Object writeReplace() {
385 return new DTO(this);
386 }
387
388 /** Internal class used only for serialization. */
389 private static class DTO implements Serializable {
390
391 /** Serializable UID. */
392 private static final long serialVersionUID = 20140723L;
393
394 /** Double values. */
395 private double[] d;
396
397 /** Simple constructor.
398 * @param pv instance to serialize
399 */
400 private DTO(final PVCoordinates pv) {
401 this.d = new double[] {
402 pv.getPosition().getX(), pv.getPosition().getY(), pv.getPosition().getZ(),
403 pv.getVelocity().getX(), pv.getVelocity().getY(), pv.getVelocity().getZ(),
404 pv.getAcceleration().getX(), pv.getAcceleration().getY(), pv.getAcceleration().getZ(),
405 };
406 }
407
408 /** Replace the deserialized data transfer object with a {@link PVCoordinates}.
409 * @return replacement {@link PVCoordinates}
410 */
411 private Object readResolve() {
412 return new PVCoordinates(new Vector3D(d[0], d[1], d[2]),
413 new Vector3D(d[3], d[4], d[5]),
414 new Vector3D(d[6], d[7], d[8]));
415 }
416
417 }
418
419 }