FieldAbsolutePVCoordinates.java

  1. /* Copyright 2002-2024 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.analysis.differentiation.DerivativeStructure;
  20. import org.hipparchus.analysis.differentiation.FieldDerivative;
  21. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitIllegalArgumentException;
  24. import org.orekit.errors.OrekitMessages;
  25. import org.orekit.frames.FieldStaticTransform;
  26. import org.orekit.frames.FieldTransform;
  27. import org.orekit.frames.Frame;
  28. import org.orekit.time.FieldAbsoluteDate;
  29. import org.orekit.time.FieldTimeStamped;

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

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

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

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

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

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

  79.     /** Multiplicative constructor
  80.      * <p>Build a FieldAbsolutePVCoordinates from another one and a scale factor.</p>
  81.      * <p>The TimeStampedFieldPVCoordinates built will be a * AbsPva</p>
  82.      * @param date date of the built coordinates
  83.      * @param a scale factor
  84.      * @param AbsPva base (unscaled) FieldAbsolutePVCoordinates
  85.      */
  86.     public FieldAbsolutePVCoordinates(final FieldAbsoluteDate<T> date,
  87.                                  final T a, final FieldAbsolutePVCoordinates<T> AbsPva) {
  88.         super(date, a, AbsPva);
  89.         this.frame = AbsPva.frame;
  90.     }

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

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

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

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

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

  189.     /** Ensure that the frames from two FieldAbsolutePVCoordinates are identical.
  190.      * @param absPv1 first FieldAbsolutePVCoordinates
  191.      * @param absPv2 first FieldAbsolutePVCoordinates
  192.      * @param <T> the type of the field elements
  193.      * @throws OrekitIllegalArgumentException if frames are different
  194.      */
  195.     private static <T extends CalculusFieldElement<T>> void ensureIdenticalFrames(final FieldAbsolutePVCoordinates<T> absPv1, final FieldAbsolutePVCoordinates<T> absPv2)
  196.         throws OrekitIllegalArgumentException {
  197.         if (!absPv1.frame.equals(absPv2.frame)) {
  198.             throw new OrekitIllegalArgumentException(OrekitMessages.INCOMPATIBLE_FRAMES,
  199.                                                      absPv1.frame.getName(), absPv2.frame.getName());
  200.         }
  201.     }

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

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

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

  254.     /** Get the frame in which the coordinates are defined.
  255.      * @return frame in which the coordinates are defined
  256.      */
  257.     public Frame getFrame() {
  258.         return frame;
  259.     }

  260.     /** Get the TimeStampedFieldPVCoordinates.
  261.      * @return TimeStampedFieldPVCoordinates
  262.      */
  263.     public TimeStampedFieldPVCoordinates<T> getPVCoordinates() {
  264.         return this;
  265.     }

  266.     /** Get the position in a specified frame.
  267.      * @param outputFrame frame in which the position coordinates shall be computed
  268.      * @return position
  269.      * @see #getPVCoordinates(Frame)
  270.      * @since 12.0
  271.      */
  272.     public FieldVector3D<T> getPosition(final Frame outputFrame) {
  273.         // If output frame requested is the same as definition frame,
  274.         // Position vector is returned directly
  275.         if (outputFrame == frame) {
  276.             return getPosition();
  277.         }

  278.         // Else, position vector is transformed to output frame
  279.         final FieldStaticTransform<T> t = frame.getStaticTransformTo(outputFrame, getDate());
  280.         return t.transformPosition(getPosition());
  281.     }

  282.     /** Get the TimeStampedFieldPVCoordinates in a specified frame.
  283.      * @param outputFrame frame in which the position/velocity coordinates shall be computed
  284.      * @return TimeStampedFieldPVCoordinates
  285.      * @exception OrekitException if transformation between frames cannot be computed
  286.      * @see #getPVCoordinates()
  287.      */
  288.     public TimeStampedFieldPVCoordinates<T> getPVCoordinates(final Frame outputFrame) {
  289.         // If output frame requested is the same as definition frame,
  290.         // PV coordinates are returned directly
  291.         if (outputFrame == frame) {
  292.             return getPVCoordinates();
  293.         }

  294.         // Else, PV coordinates are transformed to output frame
  295.         final FieldTransform<T> t = frame.getTransformTo(outputFrame, getDate());
  296.         return t.transformPVCoordinates(getPVCoordinates());
  297.     }

  298.     @Override
  299.     public TimeStampedFieldPVCoordinates<T> getPVCoordinates(final FieldAbsoluteDate<T> otherDate, final Frame outputFrame) {
  300.         return shiftedBy(otherDate.durationFrom(getDate())).getPVCoordinates(outputFrame);
  301.     }

  302.     /**
  303.      * Converts to an AbsolutePVCoordinates instance.
  304.      * @return AbsolutePVCoordinates with same properties
  305.      */
  306.     public AbsolutePVCoordinates toAbsolutePVCoordinates() {
  307.         return new AbsolutePVCoordinates(frame, this.getDate()
  308.             .toAbsoluteDate(), this.getPVCoordinates().toPVCoordinates());
  309.     }
  310. }