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.time;
18  
19  import org.hipparchus.util.FastMath;
20  import org.orekit.errors.OrekitIllegalArgumentException;
21  import org.orekit.errors.OrekitInternalError;
22  import org.orekit.errors.OrekitMessages;
23  import org.orekit.utils.ImmutableTimeStampedCache;
24  
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Collections;
28  import java.util.List;
29  import java.util.Optional;
30  import java.util.stream.Collectors;
31  import java.util.stream.Stream;
32  
33  /**
34   * Abstract class for time interpolator.
35   *
36   * @param <T> interpolated time stamped type
37   *
38   * @author Vincent Cucchietti
39   */
40  public abstract class AbstractTimeInterpolator<T extends TimeStamped> implements TimeInterpolator<T> {
41  
42      /** Default extrapolation time threshold: 1ms. */
43      public static final double DEFAULT_EXTRAPOLATION_THRESHOLD_SEC = 1e-3;
44  
45      /** Default number of interpolation points. */
46      public static final int DEFAULT_INTERPOLATION_POINTS = 2;
47  
48      /** The extrapolation threshold beyond which the propagation will fail. */
49      private final double extrapolationThreshold;
50  
51      /** Neighbor size. */
52      private final int interpolationPoints;
53  
54      /**
55       * Constructor.
56       *
57       * @param interpolationPoints number of interpolation points
58       * @param extrapolationThreshold extrapolation threshold beyond which the propagation will fail
59       */
60      public AbstractTimeInterpolator(final int interpolationPoints, final double extrapolationThreshold) {
61          this.interpolationPoints    = interpolationPoints;
62          this.extrapolationThreshold = extrapolationThreshold;
63      }
64  
65      /**
66       * Method checking if given interpolator is compatible with given sample size.
67       *
68       * @param interpolator interpolator
69       * @param sampleSize sample size
70       */
71      public static void checkInterpolatorCompatibilityWithSampleSize(
72              final TimeInterpolator<? extends TimeStamped> interpolator,
73              final int sampleSize) {
74  
75          // Retrieve all sub-interpolators (or a singleton list with given interpolator if there are no sub-interpolators)
76          final List<TimeInterpolator<? extends TimeStamped>> subInterpolators = interpolator.getSubInterpolators();
77          for (final TimeInterpolator<? extends TimeStamped> subInterpolator : subInterpolators) {
78              if (sampleSize < subInterpolator.getNbInterpolationPoints()) {
79                  throw new OrekitIllegalArgumentException(OrekitMessages.NOT_ENOUGH_DATA, sampleSize);
80              }
81          }
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public T interpolate(final AbsoluteDate interpolationDate, final Stream<T> sample) {
87          return interpolate(interpolationDate, sample.collect(Collectors.toList()));
88      }
89  
90      /** {@inheritDoc}. */
91      @Override
92      public T interpolate(final AbsoluteDate interpolationDate, final Collection<T> sample) {
93          final InterpolationData interpolationData = new InterpolationData(interpolationDate, sample);
94          return interpolate(interpolationData);
95      }
96  
97      /**
98       * Get the central date to use to find neighbors while taking into account extrapolation threshold.
99       *
100      * @param date interpolation date
101      * @param cachedSamples cached samples
102      * @param threshold extrapolation threshold
103      * @param <T> type of element
104      *
105      * @return central date to use to find neighbors
106      * @since 12.0.1
107      */
108     public static <T extends TimeStamped> AbsoluteDate getCentralDate(final AbsoluteDate date,
109                                                                       final ImmutableTimeStampedCache<T> cachedSamples,
110                                                                       final double threshold) {
111         final AbsoluteDate minDate = cachedSamples.getEarliest().getDate();
112         final AbsoluteDate maxDate = cachedSamples.getLatest().getDate();
113         return getCentralDate(date, minDate, maxDate, threshold);
114     }
115 
116     /**
117      * Get the central date to use to find neighbors while taking into account extrapolation threshold.
118      *
119      * @param date interpolation date
120      * @param minDate earliest date in the sample.
121      * @param maxDate latest date in the sample.
122      * @param threshold extrapolation threshold
123      *
124      * @return central date to use to find neighbors
125      * @since 12.0.1
126      */
127     public static AbsoluteDate getCentralDate(final AbsoluteDate date,
128                                               final AbsoluteDate minDate,
129                                               final AbsoluteDate maxDate,
130                                               final double threshold) {
131         final AbsoluteDate central;
132 
133         if (date.compareTo(minDate) < 0 && FastMath.abs(date.durationFrom(minDate)) <= threshold) {
134             // avoid TimeStampedCacheException as we are still within the tolerance before minDate
135             central = minDate;
136         } else if (date.compareTo(maxDate) > 0 && FastMath.abs(date.durationFrom(maxDate)) <= threshold) {
137             // avoid TimeStampedCacheException as we are still within the tolerance after maxDate
138             central = maxDate;
139         } else {
140             central = date;
141         }
142 
143         return central;
144     }
145 
146     /** {@inheritDoc} */
147     public List<TimeInterpolator<? extends TimeStamped>> getSubInterpolators() {
148         return Collections.singletonList(this);
149     }
150 
151     /** {@inheritDoc} */
152     public int getNbInterpolationPoints() {
153         final List<TimeInterpolator<? extends TimeStamped>> subInterpolators = getSubInterpolators();
154         // In case the interpolator does not have sub interpolators
155         if (subInterpolators.size() == 1) {
156             return interpolationPoints;
157         }
158         // Otherwise find maximum number of interpolation points among sub interpolators
159         else {
160             final Optional<Integer> optionalMaxNbInterpolationPoints =
161                     subInterpolators.stream().map(TimeInterpolator::getNbInterpolationPoints).max(Integer::compareTo);
162             if (optionalMaxNbInterpolationPoints.isPresent()) {
163                 return optionalMaxNbInterpolationPoints.get();
164             } else {
165                 // This should never happen
166                 throw new OrekitInternalError(null);
167             }
168         }
169     }
170 
171     /** {@inheritDoc} */
172     public double getExtrapolationThreshold() {
173         return extrapolationThreshold;
174     }
175 
176     /**
177      * Add all lowest level sub interpolators to the sub interpolator list.
178      *
179      * @param subInterpolator optional sub interpolator to add
180      * @param subInterpolators list of sub interpolators
181      */
182     protected void addOptionalSubInterpolatorIfDefined(final TimeInterpolator<? extends TimeStamped> subInterpolator,
183                                                        final List<TimeInterpolator<? extends TimeStamped>> subInterpolators) {
184         // Add all lowest level sub interpolators
185         if (subInterpolator != null) {
186             subInterpolators.addAll(subInterpolator.getSubInterpolators());
187         }
188     }
189 
190     /**
191      * Interpolate instance from given interpolation data.
192      *
193      * @param interpolationData interpolation data
194      *
195      * @return interpolated instance from given interpolation data.
196      */
197     protected abstract T interpolate(InterpolationData interpolationData);
198 
199     /**
200      * Get the time parameter which lies between [0:1] by normalizing the difference between interpolating time and previous
201      * date by the Δt between tabulated values.
202      *
203      * @param interpolatingTime time at which we want to interpolate a value (between previous and next tabulated dates)
204      * @param previousDate previous tabulated value date
205      * @param nextDate next tabulated value date
206      *
207      * @return time parameter which lies between [0:1]
208      */
209     protected double getTimeParameter(final AbsoluteDate interpolatingTime,
210                                       final AbsoluteDate previousDate,
211                                       final AbsoluteDate nextDate) {
212         return interpolatingTime.durationFrom(previousDate) / nextDate.getDate().durationFrom(previousDate);
213     }
214 
215     /**
216      * Nested class used to store interpolation data.
217      * <p>
218      * It makes the interpolator thread safe.
219      */
220     public class InterpolationData {
221 
222         /** Interpolation date. */
223         private final AbsoluteDate interpolationDate;
224 
225         /** Neighbor list around interpolation date. */
226         private final List<T> neighborList;
227 
228         /**
229          * Constructor.
230          *
231          * @param interpolationDate interpolation date
232          * @param sample time stamped sample
233          */
234         protected InterpolationData(final AbsoluteDate interpolationDate, final Collection<T> sample) {
235             // Handle specific case that is not handled by the immutable time stamped cache constructor
236             if (sample.isEmpty()) {
237                 throw new OrekitIllegalArgumentException(OrekitMessages.NOT_ENOUGH_DATA, 0);
238             }
239 
240             // TODO performance: create neighborsList without copying sample.
241             if (sample.size() == interpolationPoints) {
242                 // shortcut for simple case
243                 // copy list to make neighborList immutable
244                 this.neighborList = Collections.unmodifiableList(new ArrayList<>(sample));
245             } else {
246                 // else, select sample.
247 
248                 // Create immutable time stamped cache
249                 final ImmutableTimeStampedCache<T> cachedSamples =
250                         new ImmutableTimeStampedCache<>(interpolationPoints, sample);
251 
252                 // Find neighbors
253                 final AbsoluteDate central = AbstractTimeInterpolator.getCentralDate(
254                         interpolationDate,
255                         cachedSamples,
256                         extrapolationThreshold);
257                 final Stream<T> neighborsStream = cachedSamples.getNeighbors(central);
258 
259                 // Convert to unmodifiable list
260                 this.neighborList = Collections.unmodifiableList(neighborsStream.collect(Collectors.toList()));
261             }
262 
263             // Store interpolation date
264             this.interpolationDate = interpolationDate;
265         }
266 
267         /** Get interpolation date.
268          * @return interpolation date
269          */
270         public AbsoluteDate getInterpolationDate() {
271             return interpolationDate;
272         }
273 
274         /** Get neighbor list.
275          * @return neighbor list
276          */
277         public List<T> getNeighborList() {
278             return neighborList;
279         }
280 
281     }
282 }