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.attitudes;
18  
19  import java.util.List;
20  import java.util.stream.Collectors;
21  
22  import org.hipparchus.CalculusFieldElement;
23  import org.orekit.errors.OrekitException;
24  import org.orekit.errors.OrekitMessages;
25  import org.orekit.frames.FieldTransform;
26  import org.orekit.frames.Frame;
27  import org.orekit.frames.LOF;
28  import org.orekit.frames.Transform;
29  import org.orekit.time.AbsoluteDate;
30  import org.orekit.time.FieldAbsoluteDate;
31  import org.orekit.time.FieldTimeInterpolator;
32  import org.orekit.time.TimeInterpolator;
33  import org.orekit.utils.AngularDerivativesFilter;
34  import org.orekit.utils.FieldPVCoordinates;
35  import org.orekit.utils.FieldPVCoordinatesProvider;
36  import org.orekit.utils.ImmutableTimeStampedCache;
37  import org.orekit.utils.PVCoordinates;
38  import org.orekit.utils.PVCoordinatesProvider;
39  import org.orekit.utils.TimeStampedAngularCoordinates;
40  import org.orekit.utils.TimeStampedAngularCoordinatesHermiteInterpolator;
41  import org.orekit.utils.TimeStampedFieldAngularCoordinates;
42  import org.orekit.utils.TimeStampedFieldAngularCoordinatesHermiteInterpolator;
43  
44  /**
45   * This class handles an attitude provider interpolating from a predefined table
46   * containing offsets from a Local Orbital Frame.
47   * <p>Instances of this class are guaranteed to be immutable.</p>
48   * @see LofOffset
49   * @see TabulatedProvider
50   * @author Luc Maisonobe
51   * @since 7.1
52   */
53  public class TabulatedLofOffset implements BoundedAttitudeProvider {
54  
55      /** Inertial frame with respect to which orbit should be computed. */
56      private final Frame inertialFrame;
57  
58      /** Local Orbital Frame. */
59      private final LOF type;
60  
61      /** Cached attitude table. */
62      private final transient ImmutableTimeStampedCache<? extends TimeStampedAngularCoordinates> table;
63  
64      /** Filter for derivatives from the sample to use in interpolation. */
65      private final AngularDerivativesFilter filter;
66  
67      /** First date of the range. */
68      private final AbsoluteDate minDate;
69  
70      /** Last date of the range. */
71      private final AbsoluteDate maxDate;
72  
73      /** Creates new instance.
74       * <p>
75       * This constructor uses the first and last point samples as the min and max dates.
76       * </p>
77       * @param inertialFrame inertial frame with respect to which orbit should be computed
78       * @param lof local orbital frame
79       * @param table tabulated attitudes
80       * @param n number of attitude to use for interpolation
81       * @param filter filter for derivatives from the sample to use in interpolation
82       */
83      public TabulatedLofOffset(final Frame inertialFrame, final LOF lof,
84                                final List<? extends TimeStampedAngularCoordinates> table,
85                                final int n, final AngularDerivativesFilter filter) {
86          this(inertialFrame, lof, table, n, filter, table.get(0).getDate(), table.get(table.size() - 1).getDate());
87      }
88  
89      /** Creates new instance.
90       * @param inertialFrame inertial frame with respect to which orbit should be computed
91       * @param lof local orbital frame
92       * @param table tabulated attitudes
93       * @param n number of attitude to use for interpolation
94       * @param minDate min date to use
95       * @param maxDate max date to use
96       * @param filter filter for derivatives from the sample to use in interpolation
97       * @since 11.0
98       */
99      public TabulatedLofOffset(final Frame inertialFrame, final LOF lof,
100                               final List<? extends TimeStampedAngularCoordinates> table,
101                               final int n, final AngularDerivativesFilter filter,
102                               final AbsoluteDate minDate, final AbsoluteDate maxDate) {
103         if (!inertialFrame.isPseudoInertial()) {
104             throw new OrekitException(OrekitMessages.NON_PSEUDO_INERTIAL_FRAME,
105                                       inertialFrame.getName());
106         }
107         this.inertialFrame = inertialFrame;
108         this.type          = lof;
109         this.table         = new ImmutableTimeStampedCache<TimeStampedAngularCoordinates>(n, table);
110         this.filter        = filter;
111         this.minDate       = minDate;
112         this.maxDate       = maxDate;
113     }
114 
115     /** Get an unmodifiable view of the tabulated attitudes.
116      * @return unmodifiable view of the tabulated attitudes
117      */
118     public List<? extends TimeStampedAngularCoordinates> getTable() {
119         return table.getAll();
120     }
121 
122     /** {@inheritDoc} */
123     public Attitude getAttitude(final PVCoordinatesProvider pvProv,
124                                 final AbsoluteDate date, final Frame frame) {
125 
126         // get attitudes sample on which interpolation will be performed
127         final List<TimeStampedAngularCoordinates> sample = table.getNeighbors(date).collect(Collectors.toList());
128 
129         // create interpolator
130         final TimeInterpolator<TimeStampedAngularCoordinates> interpolator =
131                 new TimeStampedAngularCoordinatesHermiteInterpolator(sample.size(), filter);
132 
133         // interpolate
134         final TimeStampedAngularCoordinates interpolated = interpolator.interpolate(date, sample);
135 
136         // construction of the local orbital frame, using PV from inertial frame
137         final PVCoordinates pv = pvProv.getPVCoordinates(date, inertialFrame);
138         final Transform inertialToLof = type.transformFromInertial(date, pv);
139 
140         // take into account the specified start frame (which may not be an inertial one)
141         final Transform frameToInertial = frame.getTransformTo(inertialFrame, date);
142         final Transform frameToLof      = new Transform(date, frameToInertial, inertialToLof);
143 
144         // compose with interpolated rotation
145         return new Attitude(date, frame,
146                             interpolated.addOffset(frameToLof.getAngular()));
147     }
148 
149     /** {@inheritDoc} */
150     public <T extends CalculusFieldElement<T>> FieldAttitude<T> getAttitude(final FieldPVCoordinatesProvider<T> pvProv,
151                                                                         final FieldAbsoluteDate<T> date,
152                                                                         final Frame frame) {
153 
154         // get attitudes sample on which interpolation will be performed
155         final List<TimeStampedFieldAngularCoordinates<T>> sample =
156                         table.
157                         getNeighbors(date.toAbsoluteDate()).
158                         map(ac -> new TimeStampedFieldAngularCoordinates<>(date.getField(), ac)).
159                         collect(Collectors.toList());
160 
161         // create interpolator
162         final FieldTimeInterpolator<TimeStampedFieldAngularCoordinates<T>, T> interpolator =
163                 new TimeStampedFieldAngularCoordinatesHermiteInterpolator<>(sample.size(), filter);
164 
165         // interpolate
166         final TimeStampedFieldAngularCoordinates<T> interpolated = interpolator.interpolate(date, sample);
167 
168         // construction of the local orbital frame, using PV from inertial frame
169         final FieldPVCoordinates<T> pv = pvProv.getPVCoordinates(date, inertialFrame);
170         final FieldTransform<T> inertialToLof = type.transformFromInertial(date, pv);
171 
172         // take into account the specified start frame (which may not be an inertial one)
173         final FieldTransform<T> frameToInertial = frame.getTransformTo(inertialFrame, date);
174         final FieldTransform<T> frameToLof      = new FieldTransform<>(date, frameToInertial, inertialToLof);
175 
176         // compose with interpolated rotation
177         return new FieldAttitude<>(date, frame,
178                                    interpolated.addOffset(frameToLof.getAngular()));
179     }
180 
181     /** {@inheritDoc} */
182     public AbsoluteDate getMinDate() {
183         return minDate;
184     }
185 
186     /** {@inheritDoc} */
187     public AbsoluteDate getMaxDate() {
188         return maxDate;
189     }
190 
191 }