FieldAbsolutePVCoordinates.java

  1. /* Copyright 2002-2025 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.utils;


  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  21. import org.hipparchus.analysis.differentiation.FieldDerivative;
  22. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  23. import org.orekit.errors.OrekitException;
  24. import org.orekit.errors.OrekitIllegalArgumentException;
  25. import org.orekit.errors.OrekitMessages;
  26. import org.orekit.frames.FieldStaticTransform;
  27. import org.orekit.frames.FieldTransform;
  28. import org.orekit.frames.Frame;
  29. import org.orekit.time.FieldAbsoluteDate;
  30. import org.orekit.time.FieldTimeStamped;

  31. /** Field implementation of AbsolutePVCoordinates.
  32.  * @see AbsolutePVCoordinates
  33.  * @author Vincent Mouraux
  34.  * @param <T> type of the field elements
  35.  */
  36. public class FieldAbsolutePVCoordinates<T extends CalculusFieldElement<T>> extends TimeStampedFieldPVCoordinates<T>
  37.     implements FieldTimeStamped<T>, FieldPVCoordinatesProvider<T> {

  38.     /** Frame in which are defined the coordinates. */
  39.     private final Frame frame;

  40.     /** Build from position, velocity, acceleration.
  41.      * @param frame the frame in which the coordinates are defined
  42.      * @param date coordinates date
  43.      * @param position the position vector (m)
  44.      * @param velocity the velocity vector (m/s)
  45.      * @param acceleration the acceleration vector (m/sÂý)
  46.      */
  47.     public FieldAbsolutePVCoordinates(final Frame frame, final FieldAbsoluteDate<T> date,
  48.                                  final FieldVector3D<T> position, final FieldVector3D<T> velocity, final FieldVector3D<T> acceleration) {
  49.         super(date, position, velocity, acceleration);
  50.         this.frame = frame;
  51.     }

  52.     /** Build from position and velocity. Acceleration is set to zero.
  53.      * @param frame the frame in which the coordinates are defined
  54.      * @param date coordinates date
  55.      * @param position the position vector (m)
  56.      * @param velocity the velocity vector (m/s)
  57.      */
  58.     public FieldAbsolutePVCoordinates(final Frame frame, final FieldAbsoluteDate<T> date,
  59.                                  final FieldVector3D<T> position,
  60.                                  final FieldVector3D<T> velocity) {
  61.         this(frame, date, position, velocity, FieldVector3D.getZero(date.getField()));
  62.     }

  63.     /** Build from frame, date and FieldPVA coordinates.
  64.      * @param frame the frame in which the coordinates are defined
  65.      * @param date date of the coordinates
  66.      * @param pva TimeStampedPVCoordinates
  67.      */
  68.     public FieldAbsolutePVCoordinates(final Frame frame, final FieldAbsoluteDate<T> date, final FieldPVCoordinates<T> pva) {
  69.         super(date, pva);
  70.         this.frame = frame;
  71.     }

  72.     /** Build from frame and TimeStampedFieldPVCoordinates.
  73.      * @param frame the frame in which the coordinates are defined
  74.      * @param pva TimeStampedFieldPVCoordinates
  75.      */
  76.     public FieldAbsolutePVCoordinates(final Frame frame, final TimeStampedFieldPVCoordinates<T> pva) {
  77.         super(pva.getDate(), pva);
  78.         this.frame = frame;
  79.     }

  80.     /** Build from Field and non-Fielded object.
  81.      * @param field field
  82.      * @param pva non-Field AbsolutePVCoordinates
  83.      */
  84.     public FieldAbsolutePVCoordinates(final Field<T> field, final AbsolutePVCoordinates pva) {
  85.         this(pva.getFrame(), new TimeStampedFieldPVCoordinates<>(field, pva));
  86.     }

  87.     /** Multiplicative constructor
  88.      * <p>Build a FieldAbsolutePVCoordinates from another one and a scale factor.</p>
  89.      * <p>The TimeStampedFieldPVCoordinates built will be a * AbsPva</p>
  90.      * @param date date of the built coordinates
  91.      * @param a scale factor
  92.      * @param AbsPva base (unscaled) FieldAbsolutePVCoordinates
  93.      */
  94.     public FieldAbsolutePVCoordinates(final FieldAbsoluteDate<T> date,
  95.                                  final T a, final FieldAbsolutePVCoordinates<T> AbsPva) {
  96.         super(date, a, AbsPva);
  97.         this.frame = AbsPva.frame;
  98.     }

  99.     /** Subtractive constructor
  100.      * <p>Build a relative FieldAbsolutePVCoordinates from a start and an end position.</p>
  101.      * <p>The FieldAbsolutePVCoordinates built will be end - start.</p>
  102.      * <p>In case start and end use two different pseudo-inertial frames,
  103.      * the new FieldAbsolutePVCoordinates arbitrarily be defined in the start frame. </p>
  104.      * @param date date of the built coordinates
  105.      * @param start Starting FieldAbsolutePVCoordinates
  106.      * @param end ending FieldAbsolutePVCoordinates
  107.      */
  108.     public FieldAbsolutePVCoordinates(final FieldAbsoluteDate<T> date,
  109.                                  final FieldAbsolutePVCoordinates<T> start, final FieldAbsolutePVCoordinates<T> end) {
  110.         super(date, start, end);
  111.         ensureIdenticalFrames(start, end);
  112.         this.frame = start.frame;
  113.     }

  114.     /** Linear constructor
  115.      * <p>Build a FieldAbsolutePVCoordinates from two other ones and corresponding scale factors.</p>
  116.      * <p>The FieldAbsolutePVCoordinates built will be a1 * u1 + a2 * u2</p>
  117.      * <p>In case the FieldAbsolutePVCoordinates use different pseudo-inertial frames,
  118.      * the new FieldAbsolutePVCoordinates arbitrarily be defined in the first frame. </p>
  119.      * @param date date of the built coordinates
  120.      * @param a1 first scale factor
  121.      * @param absPv1 first base (unscaled) FieldAbsolutePVCoordinates
  122.      * @param a2 second scale factor
  123.      * @param absPv2 second base (unscaled) FieldAbsolutePVCoordinates
  124.      */
  125.     public FieldAbsolutePVCoordinates(final FieldAbsoluteDate<T> date,
  126.                                  final T a1, final FieldAbsolutePVCoordinates<T> absPv1,
  127.                                  final T a2, final FieldAbsolutePVCoordinates<T> absPv2) {
  128.         super(date, a1, absPv1.getPVCoordinates(), a2, absPv2.getPVCoordinates());
  129.         ensureIdenticalFrames(absPv1, absPv2);
  130.         this.frame = absPv1.getFrame();
  131.     }

  132.     /** Linear constructor
  133.      * <p>Build a FieldAbsolutePVCoordinates from three other ones and corresponding scale factors.</p>
  134.      * <p>The FieldAbsolutePVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
  135.      * <p>In case the FieldAbsolutePVCoordinates use different pseudo-inertial frames,
  136.      * the new FieldAbsolutePVCoordinates arbitrarily be defined in the first frame. </p>
  137.      * @param date date of the built coordinates
  138.      * @param a1 first scale factor
  139.      * @param absPv1 first base (unscaled) FieldAbsolutePVCoordinates
  140.      * @param a2 second scale factor
  141.      * @param absPv2 second base (unscaled) FieldAbsolutePVCoordinates
  142.      * @param a3 third scale factor
  143.      * @param absPv3 third base (unscaled) FieldAbsolutePVCoordinates
  144.      */
  145.     public FieldAbsolutePVCoordinates(final FieldAbsoluteDate<T> date,
  146.                                  final T a1, final FieldAbsolutePVCoordinates<T> absPv1,
  147.                                  final T a2, final FieldAbsolutePVCoordinates<T> absPv2,
  148.                                  final T a3, final FieldAbsolutePVCoordinates<T> absPv3) {
  149.         super(date, a1, absPv1.getPVCoordinates(), a2, absPv2.getPVCoordinates(),
  150.                 a3, absPv3.getPVCoordinates());
  151.         ensureIdenticalFrames(absPv1, absPv2);
  152.         ensureIdenticalFrames(absPv1, absPv3);
  153.         this.frame = absPv1.getFrame();
  154.     }

  155.     /** Linear constructor
  156.      * <p>Build a FieldAbsolutePVCoordinates from four other ones and corresponding scale factors.</p>
  157.      * <p>The FieldAbsolutePVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
  158.      * <p>In case the FieldAbsolutePVCoordinates use different pseudo-inertial frames,
  159.      * the new AbsolutePVCoordinates arbitrarily be defined in the first frame. </p>
  160.      * @param date date of the built coordinates
  161.      * @param a1 first scale factor
  162.      * @param absPv1 first base (unscaled) FieldAbsolutePVCoordinates
  163.      * @param a2 second scale factor
  164.      * @param absPv2 second base (unscaled) FieldAbsolutePVCoordinates
  165.      * @param a3 third scale factor
  166.      * @param absPv3 third base (unscaled) FieldAbsolutePVCoordinates
  167.      * @param a4 fourth scale factor
  168.      * @param absPv4 fourth base (unscaled) FieldAbsolutePVCoordinates
  169.      */
  170.     public FieldAbsolutePVCoordinates(final FieldAbsoluteDate<T> date,
  171.                                  final T a1, final FieldAbsolutePVCoordinates<T> absPv1,
  172.                                  final T a2, final FieldAbsolutePVCoordinates<T> absPv2,
  173.                                  final T a3, final FieldAbsolutePVCoordinates<T> absPv3,
  174.                                  final T a4, final FieldAbsolutePVCoordinates<T> absPv4) {
  175.         super(date, a1, absPv1.getPVCoordinates(), a2, absPv2.getPVCoordinates(),
  176.                 a3, absPv3.getPVCoordinates(), a4, absPv4.getPVCoordinates());
  177.         ensureIdenticalFrames(absPv1, absPv2);
  178.         ensureIdenticalFrames(absPv1, absPv3);
  179.         ensureIdenticalFrames(absPv1, absPv4);
  180.         this.frame = absPv1.getFrame();
  181.     }

  182.     /** Builds a FieldAbsolutePVCoordinates triplet from  a {@link FieldVector3D}&lt;{@link DerivativeStructure}&gt;.
  183.      * <p>
  184.      * The vector components must have time as their only derivation parameter and
  185.      * have consistent derivation orders.
  186.      * </p>
  187.      * @param frame the frame in which the parameters are defined
  188.      * @param date date of the built coordinates
  189.      * @param p vector with time-derivatives embedded within the coordinates
  190.      * @param <U> type of the derivative
  191.      */
  192.     public <U extends FieldDerivative<T, U>> FieldAbsolutePVCoordinates(final Frame frame, final FieldAbsoluteDate<T> date,
  193.                                                                         final FieldVector3D<U> p) {
  194.         super(date, p);
  195.         this.frame = frame;
  196.     }

  197.     /** Ensure that the frames from two FieldAbsolutePVCoordinates are identical.
  198.      * @param absPv1 first FieldAbsolutePVCoordinates
  199.      * @param absPv2 first FieldAbsolutePVCoordinates
  200.      * @param <T> the type of the field elements
  201.      * @throws OrekitIllegalArgumentException if frames are different
  202.      */
  203.     private static <T extends CalculusFieldElement<T>> void ensureIdenticalFrames(final FieldAbsolutePVCoordinates<T> absPv1, final FieldAbsolutePVCoordinates<T> absPv2)
  204.         throws OrekitIllegalArgumentException {
  205.         if (!absPv1.frame.equals(absPv2.frame)) {
  206.             throw new OrekitIllegalArgumentException(OrekitMessages.INCOMPATIBLE_FRAMES,
  207.                                                      absPv1.frame.getName(), absPv2.frame.getName());
  208.         }
  209.     }

  210.     /** Get a time-shifted state.
  211.      * <p>
  212.      * The state can be slightly shifted to close dates. This shift is based on
  213.      * a simple Taylor expansion. It is <em>not</em> intended as a replacement for
  214.      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  215.      * for either small time shifts or coarse accuracy.
  216.      * </p>
  217.      * @param dt time shift in seconds
  218.      * @return a new state, shifted with respect to the instance (which is immutable)
  219.      */
  220.     public FieldAbsolutePVCoordinates<T> shiftedBy(final T dt) {
  221.         final TimeStampedFieldPVCoordinates<T> spv = super.shiftedBy(dt);
  222.         return new FieldAbsolutePVCoordinates<>(frame, spv);
  223.     }

  224.     /** Get a time-shifted state.
  225.      * <p>
  226.      * The state can be slightly shifted to close dates. This shift is based on
  227.      * a simple Taylor expansion. It is <em>not</em> intended as a replacement for
  228.      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  229.      * for either small time shifts or coarse accuracy.
  230.      * </p>
  231.      * @param dt time shift in seconds
  232.      * @return a new state, shifted with respect to the instance (which is immutable)
  233.      */
  234.     public FieldAbsolutePVCoordinates<T> shiftedBy(final double dt) {
  235.         final TimeStampedFieldPVCoordinates<T> spv = super.shiftedBy(dt);
  236.         return new FieldAbsolutePVCoordinates<>(frame, spv);
  237.     }

  238.     /** Create a local provider using simply Taylor expansion through {@link #shiftedBy(double)}.
  239.      * <p>
  240.      * The time evolution is based on a simple Taylor expansion. It is <em>not</em> intended as a
  241.      * replacement for proper orbit propagation (it is not even Keplerian!) but should be sufficient
  242.      * for either small time shifts or coarse accuracy.
  243.      * </p>
  244.      * @return provider based on Taylor expansion, for small time shifts around instance date
  245.      */
  246.     public FieldPVCoordinatesProvider<T> toTaylorProvider() {
  247.         return new FieldPVCoordinatesProvider<T>() {
  248.             /** {@inheritDoc} */
  249.             public FieldVector3D<T> getPosition(final FieldAbsoluteDate<T> d,  final Frame f) {
  250.                 final TimeStampedFieldPVCoordinates<T> shifted   = shiftedBy(d.durationFrom(getDate()));
  251.                 final FieldStaticTransform<T>          transform = frame.getStaticTransformTo(f, d);
  252.                 return transform.transformPosition(shifted.getPosition());
  253.             }
  254.             /** {@inheritDoc} */
  255.             public TimeStampedFieldPVCoordinates<T> getPVCoordinates(final FieldAbsoluteDate<T> d,  final Frame f) {
  256.                 final TimeStampedFieldPVCoordinates<T> shifted   = shiftedBy(d.durationFrom(getDate()));
  257.                 final FieldTransform<T>                transform = frame.getTransformTo(f, d);
  258.                 return transform.transformPVCoordinates(shifted);
  259.             }
  260.         };
  261.     }

  262.     /** Get the frame in which the coordinates are defined.
  263.      * @return frame in which the coordinates are defined
  264.      */
  265.     public Frame getFrame() {
  266.         return frame;
  267.     }

  268.     /** Get the TimeStampedFieldPVCoordinates.
  269.      * @return TimeStampedFieldPVCoordinates
  270.      */
  271.     public TimeStampedFieldPVCoordinates<T> getPVCoordinates() {
  272.         return this;
  273.     }

  274.     /** Get the position in a specified frame.
  275.      * @param outputFrame frame in which the position coordinates shall be computed
  276.      * @return position
  277.      * @see #getPVCoordinates(Frame)
  278.      * @since 12.0
  279.      */
  280.     public FieldVector3D<T> getPosition(final Frame outputFrame) {
  281.         // If output frame requested is the same as definition frame,
  282.         // Position vector is returned directly
  283.         if (outputFrame == frame) {
  284.             return getPosition();
  285.         }

  286.         // Else, position vector is transformed to output frame
  287.         final FieldStaticTransform<T> t = frame.getStaticTransformTo(outputFrame, getDate());
  288.         return t.transformPosition(getPosition());
  289.     }

  290.     /** Get the TimeStampedFieldPVCoordinates in a specified frame.
  291.      * @param outputFrame frame in which the position/velocity coordinates shall be computed
  292.      * @return TimeStampedFieldPVCoordinates
  293.      * @exception OrekitException if transformation between frames cannot be computed
  294.      * @see #getPVCoordinates()
  295.      */
  296.     public TimeStampedFieldPVCoordinates<T> getPVCoordinates(final Frame outputFrame) {
  297.         // If output frame requested is the same as definition frame,
  298.         // PV coordinates are returned directly
  299.         if (outputFrame == frame) {
  300.             return getPVCoordinates();
  301.         }

  302.         // Else, PV coordinates are transformed to output frame
  303.         final FieldTransform<T> t = frame.getTransformTo(outputFrame, getDate());
  304.         return t.transformPVCoordinates(getPVCoordinates());
  305.     }

  306.     @Override
  307.     public TimeStampedFieldPVCoordinates<T> getPVCoordinates(final FieldAbsoluteDate<T> otherDate, final Frame outputFrame) {
  308.         return shiftedBy(otherDate.durationFrom(getDate())).getPVCoordinates(outputFrame);
  309.     }

  310.     /**
  311.      * Converts to an AbsolutePVCoordinates instance.
  312.      * @return AbsolutePVCoordinates with same properties
  313.      */
  314.     public AbsolutePVCoordinates toAbsolutePVCoordinates() {
  315.         return new AbsolutePVCoordinates(frame, this.getDate()
  316.             .toAbsoluteDate(), this.getPVCoordinates().toPVCoordinates());
  317.     }
  318. }