1   /* Copyright 2002-2026 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.orbits;
18  
19  import org.hipparchus.analysis.differentiation.UnivariateDerivative2;
20  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
21  import org.hipparchus.geometry.euclidean.threed.Vector3D;
22  import org.hipparchus.linear.MatrixUtils;
23  import org.hipparchus.util.FastMath;
24  import org.orekit.frames.Frame;
25  import org.orekit.frames.KinematicTransform;
26  import org.orekit.time.AbsoluteDate;
27  import org.orekit.time.TimeOffset;
28  import org.orekit.utils.FieldPVCoordinates;
29  import org.orekit.utils.PVCoordinates;
30  import org.orekit.utils.TimeStampedPVCoordinates;
31  
32  
33  /** This class holds Cartesian orbital parameters.
34  
35   * <p>
36   * The parameters used internally are the Cartesian coordinates:
37   *   <ul>
38   *     <li>x</li>
39   *     <li>y</li>
40   *     <li>z</li>
41   *     <li>xDot</li>
42   *     <li>yDot</li>
43   *     <li>zDot</li>
44   *   </ul>
45   * contained in {@link PVCoordinates}.
46   *
47  
48   * <p>
49   * Note that the implementation of this class delegates all non-Cartesian related
50   * computations ({@link #getA()}, {@link #getEquinoctialEx()}, ...) to an underlying
51   * instance of the {@link EquinoctialOrbit} class. This implies that using this class
52   * only for analytical computations which are always based on non-Cartesian
53   * parameters is perfectly possible but somewhat sub-optimal.
54   * </p>
55   * <p>
56   * The instance <code>CartesianOrbit</code> is guaranteed to be immutable.
57   * </p>
58   * @see    Orbit
59   * @see    KeplerianOrbit
60   * @see    CircularOrbit
61   * @see    EquinoctialOrbit
62   * @author Luc Maisonobe
63   * @author Guylaine Prat
64   * @author Fabien Maussion
65   * @author V&eacute;ronique Pommier-Maurussane
66   * @author Andrew Goetz
67   */
68  public class CartesianOrbit extends Orbit {
69  
70      /** 6x6 identity matrix. */
71      private static final double[][] SIX_BY_SIX_IDENTITY = MatrixUtils.createRealIdentityMatrix(6).getData();
72  
73      /** Indicator for non-Keplerian derivatives. */
74      private final boolean hasNonKeplerianAcceleration;
75  
76      /** Underlying equinoctial orbit to which high-level methods are delegated. */
77      private EquinoctialOrbit equinoctial;
78  
79      /** Constructor from Cartesian parameters.
80       *
81       * <p> The acceleration provided in {@code pvCoordinates} is accessible using
82       * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
83       * use {@code mu} and the position to compute the acceleration, including
84       * {@link #shiftedBy(double)} and {@link #getPVCoordinates(AbsoluteDate, Frame)}.
85       *
86       * @param pvaCoordinates the position, velocity and acceleration of the satellite.
87       * @param frame the frame in which the {@link PVCoordinates} are defined
88       * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
89       * @param mu central attraction coefficient (m³/s²)
90       * @exception IllegalArgumentException if frame is not a {@link
91       * Frame#isPseudoInertial pseudo-inertial frame}
92       */
93      public CartesianOrbit(final TimeStampedPVCoordinates pvaCoordinates,
94                            final Frame frame, final double mu)
95          throws IllegalArgumentException {
96          super(pvaCoordinates, frame, mu);
97          hasNonKeplerianAcceleration = hasNonKeplerianAcceleration(pvaCoordinates, mu);
98          equinoctial = null;
99      }
100 
101     /** Constructor from Cartesian parameters.
102      *
103      * <p> The acceleration provided in {@code pvCoordinates} is accessible using
104      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
105      * use {@code mu} and the position to compute the acceleration, including
106      * {@link #shiftedBy(double)} and {@link #getPVCoordinates(AbsoluteDate, Frame)}.
107      *
108      * @param pvaCoordinates the position and velocity of the satellite.
109      * @param frame the frame in which the {@link PVCoordinates} are defined
110      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
111      * @param date date of the orbital parameters
112      * @param mu central attraction coefficient (m³/s²)
113      * @exception IllegalArgumentException if frame is not a {@link
114      * Frame#isPseudoInertial pseudo-inertial frame}
115      */
116     public CartesianOrbit(final PVCoordinates pvaCoordinates, final Frame frame,
117                           final AbsoluteDate date, final double mu)
118         throws IllegalArgumentException {
119         this(new TimeStampedPVCoordinates(date, pvaCoordinates), frame, mu);
120     }
121 
122     /** Constructor from any kind of orbital parameters.
123      * @param op orbital parameters to copy
124      */
125     public CartesianOrbit(final Orbit op) {
126         super(op.getPVCoordinates(), op.getFrame(), op.getMu());
127         hasNonKeplerianAcceleration = op.hasNonKeplerianAcceleration();
128         switch (op) {
129             case EquinoctialOrbit orbit1 -> equinoctial = orbit1;
130             case CartesianOrbit orbit -> equinoctial = orbit.equinoctial;
131             case null, default -> equinoctial = null;
132         }
133     }
134 
135     /** {@inheritDoc} */
136     public OrbitType getType() {
137         return OrbitType.CARTESIAN;
138     }
139 
140     /** {@inheritDoc} */
141     @Override
142     protected Vector3D nonKeplerianAcceleration() {
143         final double norm = getPosition().getNorm();
144         return getPVCoordinates().getAcceleration().add(new Vector3D(getMu() / (norm * norm * norm), getPosition()));
145     }
146 
147     /** Lazy evaluation of equinoctial parameters. */
148     private void initEquinoctial() {
149         if (equinoctial == null) {
150             if (hasNonKeplerianAcceleration()) {
151                 // getPVCoordinates includes accelerations that will be interpreted as derivatives
152                 equinoctial = new EquinoctialOrbit(getPVCoordinates(), getFrame(), getDate(), getMu());
153             } else {
154                 // get rid of Keplerian acceleration so we don't assume
155                 // we have derivatives when in fact we don't have them
156                 equinoctial = new EquinoctialOrbit(new PVCoordinates(getPosition(),
157                                                                      getPVCoordinates().getVelocity()),
158                                                    getFrame(), getDate(), getMu());
159             }
160         }
161     }
162 
163     /** Get the position/velocity with derivatives.
164      * @return position/velocity with derivatives
165      * @since 10.2
166      */
167     private FieldPVCoordinates<UnivariateDerivative2> getPVDerivatives() {
168         // PVA coordinates
169         final PVCoordinates pva = getPVCoordinates();
170         final Vector3D      p   = pva.getPosition();
171         final Vector3D      v   = pva.getVelocity();
172         final Vector3D      a   = pva.getAcceleration();
173         // Field coordinates
174         final FieldVector3D<UnivariateDerivative2> pG = new FieldVector3D<>(new UnivariateDerivative2(p.getX(), v.getX(), a.getX()),
175                                                                new UnivariateDerivative2(p.getY(), v.getY(), a.getY()),
176                                                                new UnivariateDerivative2(p.getZ(), v.getZ(), a.getZ()));
177         final FieldVector3D<UnivariateDerivative2> vG = new FieldVector3D<>(new UnivariateDerivative2(v.getX(), a.getX(), 0.0),
178                                                                new UnivariateDerivative2(v.getY(), a.getY(), 0.0),
179                                                                new UnivariateDerivative2(v.getZ(), a.getZ(), 0.0));
180         return new FieldPVCoordinates<>(pG, vG);
181     }
182 
183     /** {@inheritDoc} */
184     public double getA() {
185         final double r  = getPosition().getNorm();
186         final double V2 = getPVCoordinates().getVelocity().getNorm2Sq();
187         return r / (2 - r * V2 / getMu());
188     }
189 
190     /** {@inheritDoc} */
191     public double getADot() {
192         if (hasNonKeplerianAcceleration) {
193             final FieldPVCoordinates<UnivariateDerivative2> pv = getPVDerivatives();
194             final UnivariateDerivative2 r  = pv.getPosition().getNorm();
195             final UnivariateDerivative2 V2 = pv.getVelocity().getNorm2Sq();
196             final UnivariateDerivative2 a  = r.divide(r.multiply(V2).divide(getMu()).subtract(2).negate());
197             return a.getDerivative(1);
198         } else {
199             return 0.;
200         }
201     }
202 
203     /** {@inheritDoc} */
204     public double getE() {
205         final double muA = getMu() * getA();
206         if (isElliptical()) {
207             // elliptic or circular orbit
208             final Vector3D pvP   = getPosition();
209             final Vector3D pvV   = getPVCoordinates().getVelocity();
210             final double rV2OnMu = pvP.getNorm() * pvV.getNorm2Sq() / getMu();
211             final double eSE     = Vector3D.dotProduct(pvP, pvV) / FastMath.sqrt(muA);
212             final double eCE     = rV2OnMu - 1;
213             return FastMath.sqrt(eCE * eCE + eSE * eSE);
214         } else {
215             // hyperbolic orbit
216             final Vector3D pvM = getPVCoordinates().getMomentum();
217             return FastMath.sqrt(1 - pvM.getNorm2Sq() / muA);
218         }
219     }
220 
221     /** {@inheritDoc} */
222     public double getEDot() {
223         if (hasNonKeplerianAcceleration) {
224             final FieldPVCoordinates<UnivariateDerivative2> pv = getPVDerivatives();
225             final FieldVector3D<UnivariateDerivative2> pvP   = pv.getPosition();
226             final FieldVector3D<UnivariateDerivative2> pvV   = pv.getVelocity();
227             final UnivariateDerivative2 r       = pvP.getNorm();
228             final UnivariateDerivative2 V2      = pvV.getNorm2Sq();
229             final UnivariateDerivative2 rV2OnMu = r.multiply(V2).divide(getMu());
230             final UnivariateDerivative2 a       = r.divide(rV2OnMu.negate().add(2));
231             final UnivariateDerivative2 eSE     = FieldVector3D.dotProduct(pvP, pvV).divide(a.multiply(getMu()).sqrt());
232             final UnivariateDerivative2 eCE     = rV2OnMu.subtract(1);
233             final UnivariateDerivative2 e       = eCE.multiply(eCE).add(eSE.multiply(eSE)).sqrt();
234             return e.getDerivative(1);
235         } else {
236             return 0.;
237         }
238     }
239 
240     /** {@inheritDoc} */
241     public double getI() {
242         return Vector3D.angle(Vector3D.PLUS_K, getPVCoordinates().getMomentum());
243     }
244 
245     /** {@inheritDoc} */
246     public double getIDot() {
247         if (hasNonKeplerianAcceleration) {
248             final FieldPVCoordinates<UnivariateDerivative2> pv = getPVDerivatives();
249             final FieldVector3D<UnivariateDerivative2> momentum =
250                             FieldVector3D.crossProduct(pv.getPosition(), pv.getVelocity());
251             final UnivariateDerivative2 i = FieldVector3D.angle(Vector3D.PLUS_K, momentum);
252             return i.getDerivative(1);
253         } else {
254             return 0.;
255         }
256     }
257 
258     /** {@inheritDoc} */
259     public double getEquinoctialEx() {
260         initEquinoctial();
261         return equinoctial.getEquinoctialEx();
262     }
263 
264     /** {@inheritDoc} */
265     public double getEquinoctialExDot() {
266         initEquinoctial();
267         return equinoctial.getEquinoctialExDot();
268     }
269 
270     /** {@inheritDoc} */
271     public double getEquinoctialEy() {
272         initEquinoctial();
273         return equinoctial.getEquinoctialEy();
274     }
275 
276     /** {@inheritDoc} */
277     public double getEquinoctialEyDot() {
278         initEquinoctial();
279         return equinoctial.getEquinoctialEyDot();
280     }
281 
282     /** {@inheritDoc} */
283     public double getHx() {
284         final Vector3D w = getPVCoordinates().getMomentum().normalize();
285         // Check for equatorial retrograde orbit
286         if ((w.getX() * w.getX() + w.getY() * w.getY()) == 0 && w.getZ() < 0) {
287             return Double.NaN;
288         }
289         return -w.getY() / (1 + w.getZ());
290     }
291 
292     /** {@inheritDoc} */
293     public double getHxDot() {
294         if (hasNonKeplerianAcceleration) {
295             final FieldPVCoordinates<UnivariateDerivative2> pv = getPVDerivatives();
296             final FieldVector3D<UnivariateDerivative2> w =
297                             FieldVector3D.crossProduct(pv.getPosition(), pv.getVelocity()).normalize();
298             // Check for equatorial retrograde orbit
299             final double x = w.getX().getValue();
300             final double y = w.getY().getValue();
301             final double z = w.getZ().getValue();
302             if ((x * x + y * y) == 0 && z < 0) {
303                 return Double.NaN;
304             }
305             final UnivariateDerivative2 hx = w.getY().negate().divide(w.getZ().add(1));
306             return hx.getDerivative(1);
307         } else {
308             return 0.;
309         }
310     }
311 
312     /** {@inheritDoc} */
313     public double getHy() {
314         final Vector3D w = getPVCoordinates().getMomentum().normalize();
315         // Check for equatorial retrograde orbit
316         if ((w.getX() * w.getX() + w.getY() * w.getY()) == 0 && w.getZ() < 0) {
317             return Double.NaN;
318         }
319         return  w.getX() / (1 + w.getZ());
320     }
321 
322     /** {@inheritDoc} */
323     public double getHyDot() {
324         if (hasNonKeplerianAcceleration) {
325             final FieldPVCoordinates<UnivariateDerivative2> pv = getPVDerivatives();
326             final FieldVector3D<UnivariateDerivative2> w =
327                             FieldVector3D.crossProduct(pv.getPosition(), pv.getVelocity()).normalize();
328             // Check for equatorial retrograde orbit
329             final double x = w.getX().getValue();
330             final double y = w.getY().getValue();
331             final double z = w.getZ().getValue();
332             if ((x * x + y * y) == 0 && z < 0) {
333                 return Double.NaN;
334             }
335             final UnivariateDerivative2 hy = w.getX().divide(w.getZ().add(1));
336             return hy.getDerivative(1);
337         } else {
338             return 0.;
339         }
340     }
341 
342     /** {@inheritDoc} */
343     public double getLv() {
344         initEquinoctial();
345         return equinoctial.getLv();
346     }
347 
348     /** {@inheritDoc} */
349     public double getLvDot() {
350         initEquinoctial();
351         return equinoctial.getLvDot();
352     }
353 
354     /** {@inheritDoc} */
355     public double getLE() {
356         initEquinoctial();
357         return equinoctial.getLE();
358     }
359 
360     /** {@inheritDoc} */
361     public double getLEDot() {
362         initEquinoctial();
363         return equinoctial.getLEDot();
364     }
365 
366     /** {@inheritDoc} */
367     public double getLM() {
368         initEquinoctial();
369         return equinoctial.getLM();
370     }
371 
372     /** {@inheritDoc} */
373     public double getLMDot() {
374         initEquinoctial();
375         return equinoctial.getLMDot();
376     }
377 
378     /** {@inheritDoc} */
379     @Override
380     public boolean hasNonKeplerianAcceleration() {
381         return hasNonKeplerianAcceleration;
382     }
383 
384     /** {@inheritDoc} */
385     protected Vector3D initPosition() {
386         // nothing to do here, as the canonical elements are already the Cartesian ones
387         return getPVCoordinates().getPosition();
388     }
389 
390     /** {@inheritDoc} */
391     protected TimeStampedPVCoordinates initPVCoordinates() {
392         // nothing to do here, as the canonical elements are already the Cartesian ones
393         return getPVCoordinates();
394     }
395 
396     /** {@inheritDoc} */
397     @Override
398     public CartesianOrbit inFrame(final Frame inertialFrame) {
399         if (hasNonKeplerianAcceleration()) {
400             return new CartesianOrbit(getPVCoordinates(inertialFrame), inertialFrame, getMu());
401         } else {
402             final KinematicTransform transform = getFrame().getKinematicTransformTo(inertialFrame, getDate());
403             return new CartesianOrbit(transform.transformOnlyPV(getPVCoordinates()), inertialFrame, getDate(), getMu());
404         }
405     }
406 
407     /** {@inheritDoc} */
408     public CartesianOrbit shiftedBy(final double dt) {
409         final PVCoordinates shiftedPV = shiftPV(dt);
410         return new CartesianOrbit(shiftedPV, getFrame(), getDate().shiftedBy(dt), getMu());
411     }
412 
413     /** {@inheritDoc} */
414     public CartesianOrbit shiftedBy(final TimeOffset dt) {
415         final PVCoordinates shiftedPV = shiftPV(dt.toDouble());
416         return new CartesianOrbit(shiftedPV, getFrame(), getDate().shiftedBy(dt), getMu());
417     }
418 
419     /** Compute shifted position and velocity.
420      * @param dt time shift
421      * @return shifted position and velocity
422      */
423     private PVCoordinates shiftPV(final double dt) {
424 
425         final Vector3D pvP = getPosition();
426         final PVCoordinates shiftedPV = KeplerianMotionCartesianUtility.predictPositionVelocity(dt, pvP,
427             getPVCoordinates().getVelocity(), getMu());
428 
429         if (dt != 0. && hasNonKeplerianAcceleration) {
430 
431             return shiftNonKeplerian(shiftedPV, dt);
432 
433         } else {
434             // don't include acceleration,
435             // so the shifted orbit is not considered to have derivatives
436             return shiftedPV;
437         }
438 
439     }
440 
441     @Override
442     protected double[][] computeJacobianMeanWrtCartesian() {
443         return SIX_BY_SIX_IDENTITY;
444     }
445 
446     @Override
447     protected double[][] computeJacobianEccentricWrtCartesian() {
448         return SIX_BY_SIX_IDENTITY;
449     }
450 
451     @Override
452     protected double[][] computeJacobianTrueWrtCartesian() {
453         return SIX_BY_SIX_IDENTITY;
454     }
455 
456     /** {@inheritDoc} */
457     public void addKeplerContribution(final PositionAngleType type, final double gm,
458                                       final double[] pDot) {
459 
460         final PVCoordinates pv = getPVCoordinates();
461 
462         // position derivative is velocity
463         final Vector3D velocity = pv.getVelocity();
464         pDot[0] += velocity.getX();
465         pDot[1] += velocity.getY();
466         pDot[2] += velocity.getZ();
467 
468         // velocity derivative is Newtonian acceleration
469         final Vector3D position = pv.getPosition();
470         final double r2         = position.getNorm2Sq();
471         final double coeff      = -gm / (r2 * FastMath.sqrt(r2));
472         pDot[3] += coeff * position.getX();
473         pDot[4] += coeff * position.getY();
474         pDot[5] += coeff * position.getZ();
475 
476     }
477 
478     /**  Returns a string representation of this Orbit object.
479      * @return a string representation of this object
480      */
481     public String toString() {
482         // use only the six defining elements, like the other Orbit.toString() methods
483         final String comma = ", ";
484         final PVCoordinates pv = getPVCoordinates();
485         final Vector3D p = pv.getPosition();
486         final Vector3D v = pv.getVelocity();
487         return "Cartesian parameters: {P(" +
488                 p.getX() + comma +
489                 p.getY() + comma +
490                 p.getZ() + "), V(" +
491                 v.getX() + comma +
492                 v.getY() + comma +
493                 v.getZ() + ")}";
494     }
495 
496 }