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  
19  
20  import org.hipparchus.CalculusFieldElement;
21  import org.hipparchus.Field;
22  import org.hipparchus.analysis.differentiation.DerivativeStructure;
23  import org.hipparchus.analysis.differentiation.FieldDerivative;
24  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
25  import org.orekit.errors.OrekitException;
26  import org.orekit.errors.OrekitIllegalArgumentException;
27  import org.orekit.errors.OrekitMessages;
28  import org.orekit.frames.FieldStaticTransform;
29  import org.orekit.frames.FieldTransform;
30  import org.orekit.frames.Frame;
31  import org.orekit.time.FieldAbsoluteDate;
32  import org.orekit.time.FieldTimeStamped;
33  
34  /** Field implementation of AbsolutePVCoordinates.
35   * @see AbsolutePVCoordinates
36   * @author Vincent Mouraux
37   * @param <T> type of the field elements
38   */
39  public class FieldAbsolutePVCoordinates<T extends CalculusFieldElement<T>> extends TimeStampedFieldPVCoordinates<T>
40      implements FieldTimeStamped<T>, FieldPVCoordinatesProvider<T> {
41  
42      /** Frame in which are defined the coordinates. */
43      private final Frame frame;
44  
45      /** Build from position, velocity, acceleration.
46       * @param frame the frame in which the coordinates are defined
47       * @param date coordinates date
48       * @param position the position vector (m)
49       * @param velocity the velocity vector (m/s)
50       * @param acceleration the acceleration vector (m/sÂý)
51       */
52      public FieldAbsolutePVCoordinates(final Frame frame, final FieldAbsoluteDate<T> date,
53                                   final FieldVector3D<T> position, final FieldVector3D<T> velocity, final FieldVector3D<T> acceleration) {
54          super(date, position, velocity, acceleration);
55          this.frame = frame;
56      }
57  
58      /** Build from position and velocity. Acceleration is set to zero.
59       * @param frame the frame in which the coordinates are defined
60       * @param date coordinates date
61       * @param position the position vector (m)
62       * @param velocity the velocity vector (m/s)
63       */
64      public FieldAbsolutePVCoordinates(final Frame frame, final FieldAbsoluteDate<T> date,
65                                   final FieldVector3D<T> position,
66                                   final FieldVector3D<T> velocity) {
67          this(frame, date, position, velocity, FieldVector3D.getZero(date.getField()));
68      }
69  
70      /** Build from frame, date and FieldPVA coordinates.
71       * @param frame the frame in which the coordinates are defined
72       * @param date date of the coordinates
73       * @param pva TimeStampedPVCoordinates
74       */
75      public FieldAbsolutePVCoordinates(final Frame frame, final FieldAbsoluteDate<T> date, final FieldPVCoordinates<T> pva) {
76          super(date, pva);
77          this.frame = frame;
78      }
79  
80      /** Build from frame and TimeStampedFieldPVCoordinates.
81       * @param frame the frame in which the coordinates are defined
82       * @param pva TimeStampedFieldPVCoordinates
83       */
84      public FieldAbsolutePVCoordinates(final Frame frame, final TimeStampedFieldPVCoordinates<T> pva) {
85          super(pva.getDate(), pva);
86          this.frame = frame;
87      }
88  
89      /** Build from Field and non-Fielded object.
90       * @param field field
91       * @param pva non-Field AbsolutePVCoordinates
92       */
93      public FieldAbsolutePVCoordinates(final Field<T> field, final AbsolutePVCoordinates pva) {
94          this(pva.getFrame(), new TimeStampedFieldPVCoordinates<>(field, pva));
95      }
96  
97      /** Multiplicative constructor
98       * <p>Build a FieldAbsolutePVCoordinates from another one and a scale factor.</p>
99       * <p>The TimeStampedFieldPVCoordinates built will be a * AbsPva</p>
100      * @param date date of the built coordinates
101      * @param a scale factor
102      * @param AbsPva base (unscaled) FieldAbsolutePVCoordinates
103      */
104     public FieldAbsolutePVCoordinates(final FieldAbsoluteDate<T> date,
105                                  final T a, final FieldAbsolutePVCoordinates<T> AbsPva) {
106         super(date, a, AbsPva);
107         this.frame = AbsPva.frame;
108     }
109 
110     /** Subtractive constructor
111      * <p>Build a relative FieldAbsolutePVCoordinates from a start and an end position.</p>
112      * <p>The FieldAbsolutePVCoordinates built will be end - start.</p>
113      * <p>In case start and end use two different pseudo-inertial frames,
114      * the new FieldAbsolutePVCoordinates arbitrarily be defined in the start frame. </p>
115      * @param date date of the built coordinates
116      * @param start Starting FieldAbsolutePVCoordinates
117      * @param end ending FieldAbsolutePVCoordinates
118      */
119     public FieldAbsolutePVCoordinates(final FieldAbsoluteDate<T> date,
120                                  final FieldAbsolutePVCoordinates<T> start, final FieldAbsolutePVCoordinates<T> end) {
121         super(date, start, end);
122         ensureIdenticalFrames(start, end);
123         this.frame = start.frame;
124     }
125 
126     /** Linear constructor
127      * <p>Build a FieldAbsolutePVCoordinates from two other ones and corresponding scale factors.</p>
128      * <p>The FieldAbsolutePVCoordinates built will be a1 * u1 + a2 * u2</p>
129      * <p>In case the FieldAbsolutePVCoordinates use different pseudo-inertial frames,
130      * the new FieldAbsolutePVCoordinates arbitrarily be defined in the first frame. </p>
131      * @param date date of the built coordinates
132      * @param a1 first scale factor
133      * @param absPv1 first base (unscaled) FieldAbsolutePVCoordinates
134      * @param a2 second scale factor
135      * @param absPv2 second 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         super(date, a1, absPv1.getPVCoordinates(), a2, absPv2.getPVCoordinates());
141         ensureIdenticalFrames(absPv1, absPv2);
142         this.frame = absPv1.getFrame();
143     }
144 
145     /** Linear constructor
146      * <p>Build a FieldAbsolutePVCoordinates from three other ones and corresponding scale factors.</p>
147      * <p>The FieldAbsolutePVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
148      * <p>In case the FieldAbsolutePVCoordinates use different pseudo-inertial frames,
149      * the new FieldAbsolutePVCoordinates arbitrarily be defined in the first frame. </p>
150      * @param date date of the built coordinates
151      * @param a1 first scale factor
152      * @param absPv1 first base (unscaled) FieldAbsolutePVCoordinates
153      * @param a2 second scale factor
154      * @param absPv2 second base (unscaled) FieldAbsolutePVCoordinates
155      * @param a3 third scale factor
156      * @param absPv3 third base (unscaled) FieldAbsolutePVCoordinates
157      */
158     public FieldAbsolutePVCoordinates(final FieldAbsoluteDate<T> date,
159                                  final T a1, final FieldAbsolutePVCoordinates<T> absPv1,
160                                  final T a2, final FieldAbsolutePVCoordinates<T> absPv2,
161                                  final T a3, final FieldAbsolutePVCoordinates<T> absPv3) {
162         super(date, a1, absPv1.getPVCoordinates(), a2, absPv2.getPVCoordinates(),
163                 a3, absPv3.getPVCoordinates());
164         ensureIdenticalFrames(absPv1, absPv2);
165         ensureIdenticalFrames(absPv1, absPv3);
166         this.frame = absPv1.getFrame();
167     }
168 
169     /** Linear constructor
170      * <p>Build a FieldAbsolutePVCoordinates from four other ones and corresponding scale factors.</p>
171      * <p>The FieldAbsolutePVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
172      * <p>In case the FieldAbsolutePVCoordinates use different pseudo-inertial frames,
173      * the new AbsolutePVCoordinates arbitrarily be defined in the first frame. </p>
174      * @param date date of the built coordinates
175      * @param a1 first scale factor
176      * @param absPv1 first base (unscaled) FieldAbsolutePVCoordinates
177      * @param a2 second scale factor
178      * @param absPv2 second base (unscaled) FieldAbsolutePVCoordinates
179      * @param a3 third scale factor
180      * @param absPv3 third base (unscaled) FieldAbsolutePVCoordinates
181      * @param a4 fourth scale factor
182      * @param absPv4 fourth base (unscaled) FieldAbsolutePVCoordinates
183      */
184     public FieldAbsolutePVCoordinates(final FieldAbsoluteDate<T> date,
185                                  final T a1, final FieldAbsolutePVCoordinates<T> absPv1,
186                                  final T a2, final FieldAbsolutePVCoordinates<T> absPv2,
187                                  final T a3, final FieldAbsolutePVCoordinates<T> absPv3,
188                                  final T a4, final FieldAbsolutePVCoordinates<T> absPv4) {
189         super(date, a1, absPv1.getPVCoordinates(), a2, absPv2.getPVCoordinates(),
190                 a3, absPv3.getPVCoordinates(), a4, absPv4.getPVCoordinates());
191         ensureIdenticalFrames(absPv1, absPv2);
192         ensureIdenticalFrames(absPv1, absPv3);
193         ensureIdenticalFrames(absPv1, absPv4);
194         this.frame = absPv1.getFrame();
195     }
196 
197     /** Builds a FieldAbsolutePVCoordinates triplet from  a {@link FieldVector3D}&lt;{@link DerivativeStructure}&gt;.
198      * <p>
199      * The vector components must have time as their only derivation parameter and
200      * have consistent derivation orders.
201      * </p>
202      * @param frame the frame in which the parameters are defined
203      * @param date date of the built coordinates
204      * @param p vector with time-derivatives embedded within the coordinates
205      * @param <U> type of the derivative
206      */
207     public <U extends FieldDerivative<T, U>> FieldAbsolutePVCoordinates(final Frame frame, final FieldAbsoluteDate<T> date,
208                                                                         final FieldVector3D<U> p) {
209         super(date, p);
210         this.frame = frame;
211     }
212 
213     /** Ensure that the frames from two FieldAbsolutePVCoordinates are identical.
214      * @param absPv1 first FieldAbsolutePVCoordinates
215      * @param absPv2 first FieldAbsolutePVCoordinates
216      * @param <T> the type of the field elements
217      * @throws OrekitIllegalArgumentException if frames are different
218      */
219     private static <T extends CalculusFieldElement<T>> void ensureIdenticalFrames(final FieldAbsolutePVCoordinates<T> absPv1, final FieldAbsolutePVCoordinates<T> absPv2)
220         throws OrekitIllegalArgumentException {
221         if (!absPv1.frame.equals(absPv2.frame)) {
222             throw new OrekitIllegalArgumentException(OrekitMessages.INCOMPATIBLE_FRAMES,
223                                                      absPv1.frame.getName(), absPv2.frame.getName());
224         }
225     }
226 
227     /** Get a time-shifted state.
228      * <p>
229      * The state can be slightly shifted to close dates. This shift is based on
230      * a simple Taylor expansion. It is <em>not</em> intended as a replacement for
231      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
232      * for either small time shifts or coarse accuracy.
233      * </p>
234      * @param dt time shift in seconds
235      * @return a new state, shifted with respect to the instance (which is immutable)
236      */
237     @Override
238     public FieldAbsolutePVCoordinates<T> shiftedBy(final T dt) {
239         final TimeStampedFieldPVCoordinates<T> spv = super.shiftedBy(dt);
240         return new FieldAbsolutePVCoordinates<>(frame, spv);
241     }
242 
243     /** Get a time-shifted state.
244      * <p>
245      * The state can be slightly shifted to close dates. This shift is based on
246      * a simple Taylor expansion. It is <em>not</em> intended as a replacement for
247      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
248      * for either small time shifts or coarse accuracy.
249      * </p>
250      * @param dt time shift in seconds
251      * @return a new state, shifted with respect to the instance (which is immutable)
252      */
253     @Override
254     public FieldAbsolutePVCoordinates<T> shiftedBy(final double dt) {
255         final TimeStampedFieldPVCoordinates<T> spv = super.shiftedBy(dt);
256         return new FieldAbsolutePVCoordinates<>(frame, spv);
257     }
258 
259     /** Create a local provider using simply Taylor expansion through {@link #shiftedBy(double)}.
260      * <p>
261      * The time evolution is based on a simple Taylor expansion. It is <em>not</em> intended as a
262      * replacement for proper orbit propagation (it is not even Keplerian!) but should be sufficient
263      * for either small time shifts or coarse accuracy.
264      * </p>
265      * @return provider based on Taylor expansion, for small time shifts around instance date
266      */
267     public FieldPVCoordinatesProvider<T> toTaylorProvider() {
268         return this;
269     }
270 
271     /** Get the frame in which the coordinates are defined.
272      * @return frame in which the coordinates are defined
273      */
274     public Frame getFrame() {
275         return frame;
276     }
277 
278     /** Get the TimeStampedFieldPVCoordinates.
279      * @return TimeStampedFieldPVCoordinates
280      */
281     public TimeStampedFieldPVCoordinates<T> getPVCoordinates() {
282         return this;
283     }
284 
285     /** Get the position in a specified frame.
286      * @param outputFrame frame in which the position coordinates shall be computed
287      * @return position
288      * @see #getPVCoordinates(Frame)
289      * @since 12.0
290      */
291     public FieldVector3D<T> getPosition(final Frame outputFrame) {
292         // If output frame requested is the same as definition frame,
293         // Position vector is returned directly
294         if (outputFrame == frame) {
295             return getPosition();
296         }
297 
298         // Else, position vector is transformed to output frame
299         final FieldStaticTransform<T> t = frame.getStaticTransformTo(outputFrame, getDate());
300         return t.transformPosition(getPosition());
301     }
302 
303     /** Get the TimeStampedFieldPVCoordinates in a specified frame.
304      * @param outputFrame frame in which the position/velocity coordinates shall be computed
305      * @return TimeStampedFieldPVCoordinates
306      * @exception OrekitException if transformation between frames cannot be computed
307      * @see #getPVCoordinates()
308      */
309     public TimeStampedFieldPVCoordinates<T> getPVCoordinates(final Frame outputFrame) {
310         // If output frame requested is the same as definition frame,
311         // PV coordinates are returned directly
312         if (outputFrame == frame) {
313             return getPVCoordinates();
314         }
315 
316         // Else, PV coordinates are transformed to output frame
317         final FieldTransform<T> t = frame.getTransformTo(outputFrame, getDate());
318         return t.transformPVCoordinates(getPVCoordinates());
319     }
320 
321     /** {@inheritDoc} */
322     @Override
323     public FieldVector3D<T> getPosition(final FieldAbsoluteDate<T> otherDate, final Frame outputFrame) {
324         final T duration = otherDate.durationFrom(getDate());
325         final FieldVector3D<T> position = getPosition().add((getVelocity().add(getAcceleration().scalarMultiply(duration.divide(2)))).scalarMultiply(duration));
326 
327         if (outputFrame == frame) {
328             return position;
329         }
330         return frame.getStaticTransformTo(frame, otherDate).transformPosition(position);
331     }
332 
333     /** {@inheritDoc} */
334     @Override
335     public TimeStampedFieldPVCoordinates<T> getPVCoordinates(final FieldAbsoluteDate<T> otherDate, final Frame outputFrame) {
336         return shiftedBy(otherDate.durationFrom(getDate())).getPVCoordinates(outputFrame);
337     }
338 
339     /**
340      * Converts to an AbsolutePVCoordinates instance.
341      * @return AbsolutePVCoordinates with same properties
342      */
343     public AbsolutePVCoordinates toAbsolutePVCoordinates() {
344         return new AbsolutePVCoordinates(frame, this.getDate()
345             .toAbsoluteDate(), this.getPVCoordinates().toPVCoordinates());
346     }
347 }