1 /* Copyright 2002-2026 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 protected 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 an 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 && subInterpolators.getFirst() == this) {
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 /**
172 * Get the number of interpolation points for this instance only i.e., not taking into account sub-interpolators.
173 *
174 * @return required the number of interpolation points for this instance only i.e., not taking into account
175 * sub-interpolators.
176 */
177 public int getInternalNbInterpolationPoints() {
178 return interpolationPoints;
179 }
180
181 /** {@inheritDoc} */
182 public double getExtrapolationThreshold() {
183 return extrapolationThreshold;
184 }
185
186 /**
187 * Add all lowest level sub interpolators to the sub interpolator list.
188 *
189 * @param subInterpolator optional sub interpolator to add
190 * @param subInterpolators list of sub interpolators
191 */
192 protected void addOptionalSubInterpolatorIfDefined(final TimeInterpolator<? extends TimeStamped> subInterpolator,
193 final List<TimeInterpolator<? extends TimeStamped>> subInterpolators) {
194 // Add all lowest level sub interpolators
195 if (subInterpolator != null) {
196 subInterpolators.addAll(subInterpolator.getSubInterpolators());
197 }
198 }
199
200 /**
201 * Interpolate instance from given interpolation data.
202 *
203 * @param interpolationData interpolation data
204 *
205 * @return interpolated instance from given interpolation data.
206 */
207 protected abstract T interpolate(InterpolationData interpolationData);
208
209 /**
210 * Get the time parameter which lies between [0:1] by normalizing the difference between interpolating time and previous
211 * date by the Δt between tabulated values.
212 *
213 * @param interpolatingTime time at which we want to interpolate a value (between previous and next tabulated dates)
214 * @param previousDate previous tabulated value date
215 * @param nextDate next tabulated value date
216 *
217 * @return time parameter which lies between [0:1]
218 */
219 protected double getTimeParameter(final AbsoluteDate interpolatingTime,
220 final AbsoluteDate previousDate,
221 final AbsoluteDate nextDate) {
222 return interpolatingTime.durationFrom(previousDate) / nextDate.getDate().durationFrom(previousDate);
223 }
224
225 /**
226 * Nested class used to store interpolation data.
227 * <p>
228 * It makes the interpolator thread safe.
229 */
230 public class InterpolationData {
231
232 /** Interpolation date. */
233 private final AbsoluteDate interpolationDate;
234
235 /** Neighbor list around interpolation date. */
236 private final List<T> neighborList;
237
238 /**
239 * Constructor.
240 *
241 * @param interpolationDate interpolation date
242 * @param sample time stamped sample
243 */
244 protected InterpolationData(final AbsoluteDate interpolationDate, final Collection<T> sample) {
245 // Handle specific case not handled by the immutable time stamped cache constructor
246 if (sample.isEmpty()) {
247 throw new OrekitIllegalArgumentException(OrekitMessages.NOT_ENOUGH_DATA, 0);
248 }
249
250 // TODO performance: create neighborsList without copying sample.
251 final int nbInterpolationPoints = getNbInterpolationPoints();
252 if (sample.size() == nbInterpolationPoints) {
253 // shortcut for simple case
254 // copy list to make neighborList immutable
255 this.neighborList = Collections.unmodifiableList(new ArrayList<>(sample));
256 } else {
257 // else, select sample.
258
259 // Create immutable time stamped cache
260 final ImmutableTimeStampedCache<T> cachedSamples =
261 new ImmutableTimeStampedCache<>(nbInterpolationPoints, sample);
262
263 // Find neighbors
264 final AbsoluteDate central = AbstractTimeInterpolator.getCentralDate(
265 interpolationDate,
266 cachedSamples,
267 extrapolationThreshold);
268 final Stream<T> neighborsStream = cachedSamples.getNeighbors(central);
269
270 // Convert to unmodifiable list
271 this.neighborList = Collections.unmodifiableList(neighborsStream.collect(Collectors.toList()));
272 }
273
274 // Store interpolation date
275 this.interpolationDate = interpolationDate;
276 }
277
278 /** Get interpolation date.
279 * @return interpolation date
280 */
281 public AbsoluteDate getInterpolationDate() {
282 return interpolationDate;
283 }
284
285 /** Get neighbor list.
286 * @return neighbor list
287 */
288 public List<T> getNeighborList() {
289 return neighborList;
290 }
291
292 }
293 }