InterpolatingTransformProvider.java

  1. /* Copyright 2002-2019 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.frames;

  18. import java.io.Serializable;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.stream.Collectors;
  23. import java.util.stream.Stream;

  24. import org.hipparchus.Field;
  25. import org.hipparchus.RealFieldElement;
  26. import org.orekit.time.AbsoluteDate;
  27. import org.orekit.time.FieldAbsoluteDate;
  28. import org.orekit.utils.AngularDerivativesFilter;
  29. import org.orekit.utils.CartesianDerivativesFilter;
  30. import org.orekit.utils.GenericTimeStampedCache;

  31. /** Transform provider using thread-safe interpolation on transforms sample.
  32.  * <p>
  33.  * The interpolation is a polynomial Hermite interpolation, which
  34.  * can either use or ignore the derivatives provided by the raw
  35.  * provider. This means that simple raw providers that do not compute
  36.  * derivatives can be used, the derivatives will be added appropriately
  37.  * by the interpolation process.
  38.  * </p>
  39.  * @see GenericTimeStampedCache
  40.  * @see ShiftingTransformProvider
  41.  * @author Luc Maisonobe
  42.  */
  43. public class InterpolatingTransformProvider implements TransformProvider {

  44.     /** Serializable UID. */
  45.     private static final long serialVersionUID = 20140723L;

  46.     /** Provider for raw (non-interpolated) transforms. */
  47.     private final TransformProvider rawProvider;

  48.     /** Filter for Cartesian derivatives to use in interpolation. */
  49.     private final CartesianDerivativesFilter cFilter;

  50.     /** Filter for angular derivatives to use in interpolation. */
  51.     private final AngularDerivativesFilter aFilter;

  52.     /** Grid points time step. */
  53.     private final double step;

  54.     /** Cache for sample points. */
  55.     private final transient GenericTimeStampedCache<Transform> cache;

  56.     /** Field caches for sample points. */
  57.     // we use Object as the value of fieldCaches because despite numerous attempts,
  58.     // we could not find a way to use GenericTimeStampedCache<FieldTransform<? extends RealFieldElement<?>>
  59.     // without the compiler complaining
  60.     private final transient Map<Field<? extends RealFieldElement<?>>, Object> fieldCaches;

  61.     /** Simple constructor.
  62.      * @param rawProvider provider for raw (non-interpolated) transforms
  63.      * @param cFilter filter for derivatives from the sample to use in interpolation
  64.      * @param aFilter filter for derivatives from the sample to use in interpolation
  65.      * @param gridPoints number of interpolation grid points
  66.      * @param step grid points time step
  67.      * @param maxSlots maximum number of independent cached time slots
  68.      * in the {@link GenericTimeStampedCache time-stamped cache}
  69.      * @param maxSpan maximum duration span in seconds of one slot
  70.      * in the {@link GenericTimeStampedCache time-stamped cache}
  71.      * @param newSlotInterval time interval above which a new slot is created
  72.      * in the {@link GenericTimeStampedCache time-stamped cache}
  73.      * @since 9.1
  74.      */
  75.     public InterpolatingTransformProvider(final TransformProvider rawProvider,
  76.                                           final CartesianDerivativesFilter cFilter,
  77.                                           final AngularDerivativesFilter aFilter,
  78.                                           final int gridPoints, final double step,
  79.                                           final int maxSlots, final double maxSpan, final double newSlotInterval) {
  80.         this.rawProvider = rawProvider;
  81.         this.cFilter     = cFilter;
  82.         this.aFilter     = aFilter;
  83.         this.step        = step;
  84.         this.cache       = new GenericTimeStampedCache<Transform>(gridPoints, maxSlots, maxSpan, newSlotInterval,
  85.                                                                   new TransformGenerator(gridPoints,
  86.                                                                                          rawProvider,
  87.                                                                                          step));
  88.         this.fieldCaches = new HashMap<>();
  89.     }

  90.     /** Simple constructor.
  91.      * @param rawProvider provider for raw (non-interpolated) transforms
  92.      * @param cFilter filter for derivatives from the sample to use in interpolation
  93.      * @param aFilter filter for derivatives from the sample to use in interpolation
  94.      * @param earliest was earliest supported date, but is ignored now and can safely be null
  95.      * @param latest was latest supported date, but is ignored now and can safely be null
  96.      * @param gridPoints number of interpolation grid points
  97.      * @param step grid points time step
  98.      * @param maxSlots maximum number of independent cached time slots
  99.      * in the {@link GenericTimeStampedCache time-stamped cache}
  100.      * @param maxSpan maximum duration span in seconds of one slot
  101.      * in the {@link GenericTimeStampedCache time-stamped cache}
  102.      * @param newSlotInterval time interval above which a new slot is created
  103.      * in the {@link GenericTimeStampedCache time-stamped cache}
  104.      * @deprecated as of 9.1, replaced by {@link #InterpolatingTransformProvider(TransformProvider,
  105.      * CartesianDerivativesFilter, AngularDerivativesFilter, int, double, int, double, double)}
  106.      */
  107.     @Deprecated
  108.     public InterpolatingTransformProvider(final TransformProvider rawProvider,
  109.                                           final CartesianDerivativesFilter cFilter,
  110.                                           final AngularDerivativesFilter aFilter,
  111.                                           final AbsoluteDate earliest, final AbsoluteDate latest,
  112.                                           final int gridPoints, final double step,
  113.                                           final int maxSlots, final double maxSpan, final double newSlotInterval) {
  114.         this(rawProvider, cFilter, aFilter, gridPoints, step, maxSlots, maxSpan, newSlotInterval);
  115.     }

  116.     /** Get the underlying provider for raw (non-interpolated) transforms.
  117.      * @return provider for raw (non-interpolated) transforms
  118.      */
  119.     public TransformProvider getRawProvider() {
  120.         return rawProvider;
  121.     }

  122.     /** Get the number of interpolation grid points.
  123.      * @return number of interpolation grid points
  124.      */
  125.     public int getGridPoints() {
  126.         return cache.getNeighborsSize();
  127.     }

  128.     /** Get the grid points time step.
  129.      * @return grid points time step
  130.      */
  131.     public double getStep() {
  132.         return step;
  133.     }

  134.     /** {@inheritDoc} */
  135.     @Override
  136.     public Transform getTransform(final AbsoluteDate date) {
  137.         // retrieve a sample from the thread-safe cache
  138.         final List<Transform> sample = cache.getNeighbors(date).collect(Collectors.toList());

  139.         // interpolate to specified date
  140.         return Transform.interpolate(date, cFilter, aFilter, sample);
  141.     }

  142.     /** {@inheritDoc} */
  143.     @Override
  144.     public <T extends RealFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {
  145.         @SuppressWarnings("unchecked")
  146.         GenericTimeStampedCache<FieldTransform<T>> fieldCache =
  147.             (GenericTimeStampedCache<FieldTransform<T>>) fieldCaches.get(date.getField());
  148.         if (fieldCache == null) {
  149.             fieldCache =
  150.                 new GenericTimeStampedCache<FieldTransform<T>>(cache.getNeighborsSize(),
  151.                                                                cache.getMaxSlots(),
  152.                                                                cache.getMaxSpan(),
  153.                                                                cache.getNewSlotQuantumGap(),
  154.                                                                new FieldTransformGenerator<>(date.getField(),
  155.                                                                                              cache.getNeighborsSize(),
  156.                                                                                              rawProvider,
  157.                                                                                              step));
  158.             fieldCaches.put(date.getField(), fieldCache);
  159.         }

  160.         // retrieve a sample from the thread-safe cache
  161.         final Stream<FieldTransform<T>> sample = fieldCache.getNeighbors(date.toAbsoluteDate());

  162.         // interpolate to specified date
  163.         return FieldTransform.interpolate(date, cFilter, aFilter, sample);
  164.     }

  165.     /** Replace the instance with a data transfer object for serialization.
  166.      * <p>
  167.      * This intermediate class serializes only the data needed for generation,
  168.      * but does <em>not</em> serializes the cache itself (in fact the cache is
  169.      * not serializable).
  170.      * </p>
  171.      * @return data transfer object that will be serialized
  172.      */
  173.     private Object writeReplace() {
  174.         return new DTO(rawProvider, cFilter.getMaxOrder(), aFilter.getMaxOrder(),
  175.                        cache.getNeighborsSize(), step,
  176.                        cache.getMaxSlots(), cache.getMaxSpan(), cache.getNewSlotQuantumGap());
  177.     }

  178.     /** Internal class used only for serialization. */
  179.     private static class DTO implements Serializable {

  180.         /** Serializable UID. */
  181.         private static final long serialVersionUID = 20170823L;

  182.         /** Provider for raw (non-interpolated) transforms. */
  183.         private final TransformProvider rawProvider;

  184.         /** Cartesian derivatives to use in interpolation. */
  185.         private final int cDerivatives;

  186.         /** Angular derivatives to use in interpolation. */
  187.         private final int aDerivatives;

  188.         /** Number of grid points. */
  189.         private final int gridPoints;

  190.         /** Grid points time step. */
  191.         private final double step;

  192.         /** Maximum number of independent cached time slots. */
  193.         private final int maxSlots;

  194.         /** Maximum duration span in seconds of one slot. */
  195.         private final double maxSpan;

  196.         /** Time interval above which a new slot is created. */
  197.         private final double newSlotInterval;

  198.         /** Simple constructor.
  199.          * @param rawProvider provider for raw (non-interpolated) transforms
  200.          * @param cDerivatives derivation order for Cartesian coordinates
  201.          * @param aDerivatives derivation order for angular coordinates
  202.          * @param gridPoints number of interpolation grid points
  203.          * @param step grid points time step
  204.          * @param maxSlots maximum number of independent cached time slots
  205.          * in the {@link GenericTimeStampedCache time-stamped cache}
  206.          * @param maxSpan maximum duration span in seconds of one slot
  207.          * in the {@link GenericTimeStampedCache time-stamped cache}
  208.          * @param newSlotInterval time interval above which a new slot is created
  209.          * in the {@link GenericTimeStampedCache time-stamped cache}
  210.          */
  211.         private DTO(final TransformProvider rawProvider, final int cDerivatives, final int aDerivatives,
  212.                     final int gridPoints, final double step,
  213.                     final int maxSlots, final double maxSpan, final double newSlotInterval) {
  214.             this.rawProvider     = rawProvider;
  215.             this.cDerivatives    = cDerivatives;
  216.             this.aDerivatives    = aDerivatives;
  217.             this.gridPoints      = gridPoints;
  218.             this.step            = step;
  219.             this.maxSlots        = maxSlots;
  220.             this.maxSpan         = maxSpan;
  221.             this.newSlotInterval = newSlotInterval;
  222.         }

  223.         /** Replace the deserialized data transfer object with a {@link InterpolatingTransformProvider}.
  224.          * @return replacement {@link InterpolatingTransformProvider}
  225.          */
  226.         private Object readResolve() {
  227.             // build a new provider, with an empty cache
  228.             return new InterpolatingTransformProvider(rawProvider,
  229.                                                       CartesianDerivativesFilter.getFilter(cDerivatives),
  230.                                                       AngularDerivativesFilter.getFilter(aDerivatives),
  231.                                                       gridPoints, step,
  232.                                                       maxSlots, maxSpan, newSlotInterval);
  233.         }

  234.     }

  235. }