ShiftingTransformProvider.java

  1. /* Copyright 2002-2022 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.frames;

  18. import java.io.Serializable;
  19. import java.util.HashMap;
  20. import java.util.Map;

  21. import org.hipparchus.Field;
  22. import org.hipparchus.CalculusFieldElement;
  23. import org.hipparchus.util.FastMath;
  24. import org.orekit.time.AbsoluteDate;
  25. import org.orekit.time.FieldAbsoluteDate;
  26. import org.orekit.utils.AngularDerivativesFilter;
  27. import org.orekit.utils.CartesianDerivativesFilter;
  28. import org.orekit.utils.GenericTimeStampedCache;

  29. /** Transform provider using thread-safe shifts on transforms sample.
  30.  * <p>
  31.  * The shifts take derivatives into account, up to user specified order.
  32.  * </p>
  33.  * @see GenericTimeStampedCache
  34.  * @see InterpolatingTransformProvider
  35.  * @since 7.1
  36.  * @author Luc Maisonobe
  37.  */
  38. public class ShiftingTransformProvider implements TransformProvider {

  39.     /** Serializable UID. */
  40.     private static final long serialVersionUID = 20150601L;

  41.     /** First level cache. */
  42.     private final InterpolatingTransformProvider interpolatingProvider;

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

  45.     /** Field caches for sample points. */
  46.     // we use Object as the value of fieldCaches because despite numerous attempts,
  47.     // we could not find a way to use GenericTimeStampedCache<FieldTransform<? extends CalculusFieldElement<?>>
  48.     // without the compiler complaining
  49.     private final transient Map<Field<? extends CalculusFieldElement<?>>, Object> fieldCaches;

  50.     /** Simple constructor.
  51.      * @param rawProvider provider for raw (non-interpolated) transforms
  52.      * @param cFilter filter for derivatives from the sample to use in interpolation
  53.      * @param aFilter filter for derivatives from the sample to use in interpolation
  54.      * @param gridPoints number of interpolation grid points
  55.      * @param step grid points time step
  56.      * @param maxSlots maximum number of independent cached time slots
  57.      * in the {@link GenericTimeStampedCache time-stamped cache}
  58.      * @param maxSpan maximum duration span in seconds of one slot
  59.      * in the {@link GenericTimeStampedCache time-stamped cache}
  60.      * @param newSlotInterval time interval above which a new slot is created
  61.      * in the {@link GenericTimeStampedCache time-stamped cache}
  62.      * @since 9.1
  63.      */
  64.     public ShiftingTransformProvider(final TransformProvider rawProvider,
  65.                                      final CartesianDerivativesFilter cFilter,
  66.                                      final AngularDerivativesFilter aFilter,
  67.                                      final int gridPoints, final double step,
  68.                                      final int maxSlots, final double maxSpan, final double newSlotInterval) {
  69.         this(new InterpolatingTransformProvider(rawProvider, cFilter, aFilter,
  70.                                                 gridPoints, step, maxSlots, maxSpan, newSlotInterval),
  71.              maxSlots, maxSpan, newSlotInterval);
  72.     }

  73.     /** Simple constructor.
  74.      * @param interpolatingProvider first level cache provider
  75.      * @param maxSlots maximum number of independent cached time slots
  76.      * in the {@link GenericTimeStampedCache time-stamped cache}
  77.      * @param maxSpan maximum duration span in seconds of one slot
  78.      * in the {@link GenericTimeStampedCache time-stamped cache}
  79.      * @param newSlotInterval time interval above which a new slot is created
  80.      * in the {@link GenericTimeStampedCache time-stamped cache}
  81.      */
  82.     private ShiftingTransformProvider(final InterpolatingTransformProvider interpolatingProvider,
  83.                                      final int maxSlots, final double maxSpan, final double newSlotInterval) {
  84.         this.interpolatingProvider = interpolatingProvider;
  85.         this.cache = new GenericTimeStampedCache<Transform>(2, maxSlots, maxSpan, newSlotInterval,
  86.                                                             new TransformGenerator(2,
  87.                                                                                    interpolatingProvider,
  88.                                                                                    interpolatingProvider.getStep()));
  89.         this.fieldCaches = new HashMap<>();
  90.     }

  91.     /** Get the underlying provider for raw (non-interpolated) transforms.
  92.      * @return provider for raw (non-interpolated) transforms
  93.      */
  94.     public TransformProvider getRawProvider() {
  95.         return interpolatingProvider.getRawProvider();
  96.     }

  97.     /** Get the number of interpolation grid points.
  98.      * @return number of interpolation grid points
  99.      */
  100.     public int getGridPoints() {
  101.         return interpolatingProvider.getGridPoints();
  102.     }

  103.     /** Get the grid points time step.
  104.      * @return grid points time step
  105.      */
  106.     public double getStep() {
  107.         return interpolatingProvider.getStep();
  108.     }

  109.     /** {@inheritDoc} */
  110.     public Transform getTransform(final AbsoluteDate date) {
  111.         // retrieve a sample from the thread-safe cache
  112.         final Transform closest = cache.getNeighbors(date).reduce((t0, t1) ->
  113.             FastMath.abs(date.durationFrom(t0.getDate())) < FastMath.abs(date.durationFrom(t1.getDate())) ? t0 : t1
  114.         ).get();
  115.         return closest.shiftedBy(date.durationFrom(closest.getDate()));
  116.     }

  117.     /** {@inheritDoc} */
  118.     public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {
  119.         @SuppressWarnings("unchecked")
  120.         GenericTimeStampedCache<FieldTransform<T>> fieldCache =
  121.             (GenericTimeStampedCache<FieldTransform<T>>) fieldCaches.get(date.getField());
  122.         if (fieldCache == null) {
  123.             fieldCache =
  124.                 new GenericTimeStampedCache<FieldTransform<T>>(cache.getNeighborsSize(),
  125.                                                                cache.getMaxSlots(),
  126.                                                                cache.getMaxSpan(),
  127.                                                                cache.getNewSlotQuantumGap(),
  128.                                                                new FieldTransformGenerator<>(date.getField(),
  129.                                                                                              cache.getNeighborsSize(),
  130.                                                                                              interpolatingProvider,
  131.                                                                                              interpolatingProvider.getStep()));
  132.             fieldCaches.put(date.getField(), fieldCache);
  133.         }

  134.         // retrieve a sample from the thread-safe cache
  135.         final FieldTransform<T> closest = fieldCache.getNeighbors(date.toAbsoluteDate()).reduce((t0, t1) ->
  136.             date.durationFrom(t0.getDate()).abs().getReal() < date.durationFrom(t1.getDate()).abs().getReal() ?
  137.             t0 : t1
  138.         ).get();
  139.         return closest.shiftedBy(date.durationFrom(closest.getDate()));
  140.     }

  141.     /** Replace the instance with a data transfer object for serialization.
  142.      * <p>
  143.      * This intermediate class serializes only the data needed for generation,
  144.      * but does <em>not</em> serializes the cache itself (in fact the cache is
  145.      * not serializable).
  146.      * </p>
  147.      * @return data transfer object that will be serialized
  148.      */
  149.     private Object writeReplace() {
  150.         return new DTO(interpolatingProvider,
  151.                        cache.getMaxSlots(), cache.getMaxSpan(), cache.getNewSlotQuantumGap());
  152.     }

  153.     /** Internal class used only for serialization. */
  154.     private static class DTO implements Serializable {

  155.         /** Serializable UID. */
  156.         private static final long serialVersionUID = 20150601L;

  157.         /** Provider for raw (non-interpolated) transforms. */
  158.         private final InterpolatingTransformProvider interpolatingProvider;

  159.         /** Maximum number of independent cached time slots. */
  160.         private final int maxSlots;

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

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

  165.         /** Simple constructor.
  166.          * @param interpolatingProvider first level cache provider
  167.          * @param maxSlots maximum number of independent cached time slots
  168.          * in the {@link GenericTimeStampedCache time-stamped cache}
  169.          * @param maxSpan maximum duration span in seconds of one slot
  170.          * in the {@link GenericTimeStampedCache time-stamped cache}
  171.          * @param newSlotInterval time interval above which a new slot is created
  172.          * in the {@link GenericTimeStampedCache time-stamped cache}
  173.          */
  174.         private DTO(final InterpolatingTransformProvider interpolatingProvider,
  175.                     final int maxSlots, final double maxSpan, final double newSlotInterval) {
  176.             this.interpolatingProvider = interpolatingProvider;
  177.             this.maxSlots              = maxSlots;
  178.             this.maxSpan               = maxSpan;
  179.             this.newSlotInterval       = newSlotInterval;
  180.         }

  181.         /** Replace the deserialized data transfer object with a {@link ShiftingTransformProvider}.
  182.          * @return replacement {@link ShiftingTransformProvider}
  183.          */
  184.         private Object readResolve() {
  185.             // build a new provider, with an empty cache
  186.             return new ShiftingTransformProvider(interpolatingProvider,
  187.                                                  maxSlots, maxSpan, newSlotInterval);
  188.         }

  189.     }

  190. }