1   /* Copyright 2022-2025 Romain Serra
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  
18  package org.orekit.utils;
19  
20  import org.hipparchus.CalculusFieldElement;
21  import org.hipparchus.Field;
22  import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative2;
23  import org.hipparchus.analysis.differentiation.UnivariateDerivative2;
24  import org.hipparchus.analysis.differentiation.UnivariateDerivative2Field;
25  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
26  import org.hipparchus.geometry.euclidean.threed.Vector3D;
27  import org.orekit.frames.Frame;
28  import org.orekit.time.AbsoluteDate;
29  import org.orekit.time.FieldAbsoluteDate;
30  
31  /**
32   * Interface for position providers (including for Field).
33   * Emulates position (and derivatives) vector as a function of time.
34   * @author Romain Serra
35   * @since 12.1
36   */
37  public interface ExtendedPositionProvider extends PVCoordinatesProvider {
38  
39      /** Get the position in the selected frame.
40       * @param date current date
41       * @param frame the frame where to define the position
42       * @param <T> field type
43       * @return position
44       */
45      <T extends CalculusFieldElement<T>> FieldVector3D<T> getPosition(FieldAbsoluteDate<T> date, Frame frame);
46  
47      /** {@inheritDoc} */
48      @Override
49      default TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame frame) {
50          final UnivariateDerivative2Field ud2Field = UnivariateDerivative2Field.getInstance();
51          final UnivariateDerivative2 ud2Shift = new UnivariateDerivative2(0., 1., 0.);
52          final FieldAbsoluteDate<UnivariateDerivative2> fieldDate = new FieldAbsoluteDate<>(ud2Field, date).shiftedBy(ud2Shift);
53          final FieldVector3D<UnivariateDerivative2> ud2Position = getPosition(fieldDate, frame);
54          final Vector3D position = ud2Position.toVector3D();
55          final Vector3D velocity = new Vector3D(ud2Position.getX().getFirstDerivative(), ud2Position.getY().getFirstDerivative(),
56              ud2Position.getZ().getFirstDerivative());
57          final Vector3D acceleration = new Vector3D(ud2Position.getX().getSecondDerivative(), ud2Position.getY().getSecondDerivative(),
58                  ud2Position.getZ().getSecondDerivative());
59          return new TimeStampedPVCoordinates(date, new PVCoordinates(position, velocity, acceleration));
60      }
61  
62      /** Get the position-velocity-acceleration in the selected frame.
63       * @param date current date
64       * @param frame the frame where to define the position
65       * @param <T> field type
66       * @return position-velocity-acceleration vector
67       */
68      default <T extends CalculusFieldElement<T>> TimeStampedFieldPVCoordinates<T> getPVCoordinates(final FieldAbsoluteDate<T> date,
69                                                                                                    final Frame frame) {
70          final FieldAbsoluteDate<FieldUnivariateDerivative2<T>> fud2Date = date.toFUD2Field();
71          final FieldVector3D<FieldUnivariateDerivative2<T>> fud2Position = getPosition(fud2Date, frame);
72          final FieldVector3D<T> position = new FieldVector3D<>(fud2Position.getX().getValue(), fud2Position.getY().getValue(),
73              fud2Position.getZ().getValue());
74          final FieldVector3D<T> velocity = new FieldVector3D<>(fud2Position.getX().getFirstDerivative(), fud2Position.getY().getFirstDerivative(),
75              fud2Position.getZ().getFirstDerivative());
76          final FieldVector3D<T> acceleration = new FieldVector3D<>(fud2Position.getX().getSecondDerivative(), fud2Position.getY().getSecondDerivative(),
77              fud2Position.getZ().getSecondDerivative());
78          return new TimeStampedFieldPVCoordinates<>(date, position, velocity, acceleration);
79      }
80  
81      /** Convert to a {@link FieldPVCoordinatesProvider} with a specific type.
82       * @param <T> the type of the field elements
83       * @param field field for the argument and value
84       * @return converted function
85       */
86      default <T extends CalculusFieldElement<T>> FieldPVCoordinatesProvider<T> toFieldPVCoordinatesProvider(Field<T> field) {
87          return new FieldPVCoordinatesProvider<T>() {
88  
89              @Override
90              public FieldVector3D<T> getPosition(final FieldAbsoluteDate<T> date, final Frame frame) {
91                  return ExtendedPositionProvider.this.getPosition(date, frame);
92              }
93  
94              @Override
95              public TimeStampedFieldPVCoordinates<T> getPVCoordinates(final FieldAbsoluteDate<T> date,
96                                                                       final Frame frame) {
97                  return ExtendedPositionProvider.this.getPVCoordinates(date, frame);
98              }
99          };
100     }
101 
102     /**
103      * Method to convert as {@link ExtendedPVCoordinatesProvider}.
104      * @return converted object
105      * @since 13.0
106      * @deprecated since 13.0. Only there to help transition out.
107      */
108     @Deprecated
109     default ExtendedPVCoordinatesProvider toExtendedPVCoordinatesProvider() {
110         return ExtendedPositionProvider.this::getPVCoordinates;
111     }
112 }