InterpolatingTransformProvider.java

  1. /* Copyright 2002-2016 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.ArrayList;
  20. import java.util.List;

  21. import org.orekit.errors.OrekitException;
  22. import org.orekit.errors.OrekitExceptionWrapper;
  23. import org.orekit.time.AbsoluteDate;
  24. import org.orekit.utils.AngularDerivativesFilter;
  25. import org.orekit.utils.CartesianDerivativesFilter;
  26. import org.orekit.utils.GenericTimeStampedCache;
  27. import org.orekit.utils.TimeStampedGenerator;

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

  41.     /** Serializable UID. */
  42.     private static final long serialVersionUID = 20140723L;

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

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

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

  49.     /** Earliest supported date. */
  50.     private final AbsoluteDate earliest;

  51.     /** Latest supported date. */
  52.     private final AbsoluteDate latest;

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

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

  57.     /** Simple constructor.
  58.      * @param rawProvider provider for raw (non-interpolated) transforms
  59.      * @param useVelocities if true, use sample transforms velocities,
  60.      * otherwise ignore them and use only positions
  61.      * @param useRotationRates if true, use sample points rotation rates,
  62.      * otherwise ignore them and use only rotations
  63.      * @param earliest earliest supported date
  64.      * @param latest latest supported date
  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.      * @deprecated as of 7.0, replaced with {@link #InterpolatingTransformProvider(TransformProvider,
  74.      * CartesianDerivativesFilter, AngularDerivativesFilter, AbsoluteDate, AbsoluteDate,
  75.      * int, double, int, double, double)}
  76.      */
  77.     @Deprecated
  78.     public InterpolatingTransformProvider(final TransformProvider rawProvider,
  79.                                           final boolean useVelocities, final boolean useRotationRates,
  80.                                           final AbsoluteDate earliest, final AbsoluteDate latest,
  81.                                           final int gridPoints, final double step,
  82.                                           final int maxSlots, final double maxSpan, final double newSlotInterval) {
  83.         this(rawProvider,
  84.              useVelocities ? CartesianDerivativesFilter.USE_PV : CartesianDerivativesFilter.USE_P,
  85.              useRotationRates ? AngularDerivativesFilter.USE_RR : AngularDerivativesFilter.USE_R,
  86.              earliest, latest, gridPoints, step, maxSlots, maxSpan, newSlotInterval);
  87.     }

  88.     /** Simple constructor.
  89.      * @param rawProvider provider for raw (non-interpolated) transforms
  90.      * @param cFilter filter for derivatives from the sample to use in interpolation
  91.      * @param aFilter filter for derivatives from the sample to use in interpolation
  92.      * @param earliest earliest supported date
  93.      * @param latest latest supported date
  94.      * @param gridPoints number of interpolation grid points
  95.      * @param step grid points time step
  96.      * @param maxSlots maximum number of independent cached time slots
  97.      * in the {@link GenericTimeStampedCache time-stamped cache}
  98.      * @param maxSpan maximum duration span in seconds of one slot
  99.      * in the {@link GenericTimeStampedCache time-stamped cache}
  100.      * @param newSlotInterval time interval above which a new slot is created
  101.      * in the {@link GenericTimeStampedCache time-stamped cache}
  102.      */
  103.     public InterpolatingTransformProvider(final TransformProvider rawProvider,
  104.                                           final CartesianDerivativesFilter cFilter,
  105.                                           final AngularDerivativesFilter aFilter,
  106.                                           final AbsoluteDate earliest, final AbsoluteDate latest,
  107.                                           final int gridPoints, final double step,
  108.                                           final int maxSlots, final double maxSpan, final double newSlotInterval) {
  109.         this.rawProvider = rawProvider;
  110.         this.cFilter     = cFilter;
  111.         this.aFilter     = aFilter;
  112.         this.earliest    = earliest;
  113.         this.latest      = latest;
  114.         this.step        = step;
  115.         this.cache       = new GenericTimeStampedCache<Transform>(gridPoints, maxSlots, maxSpan, newSlotInterval,
  116.                                                                   new Generator(), Transform.class);
  117.     }

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

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

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

  136.     /** {@inheritDoc} */
  137.     public Transform getTransform(final AbsoluteDate date) throws OrekitException {
  138.         try {

  139.             // retrieve a sample from the thread-safe cache
  140.             final List<Transform> sample = cache.getNeighbors(date);

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

  143.         } catch (OrekitExceptionWrapper oew) {
  144.             // something went wrong while generating the sample,
  145.             // we just forward the exception up
  146.             throw oew.getException();
  147.         }
  148.     }

  149.     /** Replace the instance with a data transfer object for serialization.
  150.      * <p>
  151.      * This intermediate class serializes only the data needed for generation,
  152.      * but does <em>not</em> serializes the cache itself (in fact the cache is
  153.      * not serializable).
  154.      * </p>
  155.      * @return data transfer object that will be serialized
  156.      */
  157.     private Object writeReplace() {
  158.         return new DTO(rawProvider, cFilter.getMaxOrder(), aFilter.getMaxOrder(),
  159.                        earliest, latest, cache.getNeighborsSize(), step,
  160.                        cache.getMaxSlots(), cache.getMaxSpan(), cache.getNewSlotQuantumGap());
  161.     }

  162.     /** Internal class used only for serialization. */
  163.     private static class DTO implements Serializable {

  164.         /** Serializable UID. */
  165.         private static final long serialVersionUID = 20140723L;

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

  168.         /** Cartesian derivatives to use in interpolation. */
  169.         private final int cDerivatives;

  170.         /** Angular derivatives to use in interpolation. */
  171.         private final int aDerivatives;

  172.         /** Earliest supported date. */
  173.         private final AbsoluteDate earliest;

  174.         /** Latest supported date. */
  175.         private final AbsoluteDate latest;

  176.         /** Number of grid points. */
  177.         private final int gridPoints;

  178.         /** Grid points time step. */
  179.         private final double step;

  180.         /** Maximum number of independent cached time slots. */
  181.         private final int maxSlots;

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

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

  186.         /** Simple constructor.
  187.          * @param rawProvider provider for raw (non-interpolated) transforms
  188.          * @param cDerivatives derivation order for Cartesian coordinates
  189.          * @param aDerivatives derivation order for angular coordinates
  190.          * @param earliest earliest supported date
  191.          * @param latest latest supported date
  192.          * @param gridPoints number of interpolation grid points
  193.          * @param step grid points time step
  194.          * @param maxSlots maximum number of independent cached time slots
  195.          * in the {@link GenericTimeStampedCache time-stamped cache}
  196.          * @param maxSpan maximum duration span in seconds of one slot
  197.          * in the {@link GenericTimeStampedCache time-stamped cache}
  198.          * @param newSlotInterval time interval above which a new slot is created
  199.          * in the {@link GenericTimeStampedCache time-stamped cache}
  200.          */
  201.         private DTO(final TransformProvider rawProvider, final int cDerivatives, final int aDerivatives,
  202.                     final AbsoluteDate earliest, final AbsoluteDate latest,
  203.                     final int gridPoints, final double step,
  204.                     final int maxSlots, final double maxSpan, final double newSlotInterval) {
  205.             this.rawProvider      = rawProvider;
  206.             this.cDerivatives     = cDerivatives;
  207.             this.aDerivatives     = aDerivatives;
  208.             this.earliest         = earliest;
  209.             this.latest           = latest;
  210.             this.gridPoints       = gridPoints;
  211.             this.step             = step;
  212.             this.maxSlots         = maxSlots;
  213.             this.maxSpan          = maxSpan;
  214.             this.newSlotInterval  = newSlotInterval;
  215.         }

  216.         /** Replace the deserialized data transfer object with a {@link InterpolatingTransformProvider}.
  217.          * @return replacement {@link InterpolatingTransformProvider}
  218.          */
  219.         private Object readResolve() {
  220.             // build a new provider, with an empty cache
  221.             return new InterpolatingTransformProvider(rawProvider,
  222.                                                       CartesianDerivativesFilter.getFilter(cDerivatives),
  223.                                                       AngularDerivativesFilter.getFilter(aDerivatives),
  224.                                                       earliest, latest, gridPoints, step,
  225.                                                       maxSlots, maxSpan, newSlotInterval);
  226.         }

  227.     }

  228.     /** Local generator for thread-safe cache. */
  229.     private class Generator implements TimeStampedGenerator<Transform> {

  230.         /** {@inheritDoc} */
  231.         public List<Transform> generate(final Transform existing, final AbsoluteDate date) {

  232.             try {
  233.                 final List<Transform> generated = new ArrayList<Transform>();

  234.                 if (existing == null) {

  235.                     // no prior existing transforms, just generate a first set
  236.                     for (int i = 0; i < cache.getNeighborsSize(); ++i) {
  237.                         generated.add(rawProvider.getTransform(date.shiftedBy(i * step)));
  238.                     }

  239.                 } else {

  240.                     // some transforms have already been generated
  241.                     // add the missing ones up to specified date

  242.                     AbsoluteDate t = existing.getDate();
  243.                     if (date.compareTo(t) > 0) {
  244.                         // forward generation
  245.                         do {
  246.                             t = t.shiftedBy(step);
  247.                             generated.add(generated.size(), rawProvider.getTransform(t));
  248.                         } while (t.compareTo(date) <= 0);
  249.                     } else {
  250.                         // backward generation
  251.                         do {
  252.                             t = t.shiftedBy(-step);
  253.                             generated.add(0, rawProvider.getTransform(t));
  254.                         } while (t.compareTo(date) >= 0);
  255.                     }
  256.                 }

  257.                 // return the generated transforms
  258.                 return generated;
  259.             } catch (OrekitException oe) {
  260.                 throw new OrekitExceptionWrapper(oe);
  261.             }

  262.         }

  263.     }

  264. }