ShiftingTransformProvider.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.apache.commons.math3.util.FastMath;
  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitExceptionWrapper;
  24. import org.orekit.time.AbsoluteDate;
  25. import org.orekit.utils.AngularDerivativesFilter;
  26. import org.orekit.utils.CartesianDerivativesFilter;
  27. import org.orekit.utils.GenericTimeStampedCache;
  28. import org.orekit.utils.TimeStampedGenerator;

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

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

  86.     /** Get the underlying provider for raw (non-interpolated) transforms.
  87.      * @return provider for raw (non-interpolated) transforms
  88.      */
  89.     public TransformProvider getRawProvider() {
  90.         return interpolatingProvider.getRawProvider();
  91.     }

  92.     /** Get the number of interpolation grid points.
  93.      * @return number of interpolation grid points
  94.      */
  95.     public int getGridPoints() {
  96.         return interpolatingProvider.getGridPoints();
  97.     }

  98.     /** Get the grid points time step.
  99.      * @return grid points time step
  100.      */
  101.     public double getStep() {
  102.         return interpolatingProvider.getStep();
  103.     }

  104.     /** {@inheritDoc} */
  105.     public Transform getTransform(final AbsoluteDate date) throws OrekitException {
  106.         try {

  107.             // retrieve a sample from the thread-safe cache
  108.             final List<Transform> sample = cache.getNeighbors(date);
  109.             final double dt0 = date.durationFrom(sample.get(0).getDate());
  110.             final double dt1 = date.durationFrom(sample.get(1).getDate());
  111.             if (FastMath.abs(dt0) < FastMath.abs(dt1)) {
  112.                 return sample.get(0).shiftedBy(dt0);
  113.             } else {
  114.                 return sample.get(1).shiftedBy(dt1);
  115.             }

  116.         } catch (OrekitExceptionWrapper oew) {
  117.             // something went wrong while generating the sample,
  118.             // we just forward the exception up
  119.             throw oew.getException();
  120.         }
  121.     }

  122.     /** Replace the instance with a data transfer object for serialization.
  123.      * <p>
  124.      * This intermediate class serializes only the data needed for generation,
  125.      * but does <em>not</em> serializes the cache itself (in fact the cache is
  126.      * not serializable).
  127.      * </p>
  128.      * @return data transfer object that will be serialized
  129.      */
  130.     private Object writeReplace() {
  131.         return new DTO(interpolatingProvider,
  132.                        cache.getMaxSlots(), cache.getMaxSpan(), cache.getNewSlotQuantumGap());
  133.     }

  134.     /** Internal class used only for serialization. */
  135.     private static class DTO implements Serializable {

  136.         /** Serializable UID. */
  137.         private static final long serialVersionUID = 20150601L;

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

  140.         /** Maximum number of independent cached time slots. */
  141.         private final int maxSlots;

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

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

  146.         /** Simple constructor.
  147.          * @param interpolatingProvider first level cache provider
  148.          * @param maxSlots maximum number of independent cached time slots
  149.          * in the {@link GenericTimeStampedCache time-stamped cache}
  150.          * @param maxSpan maximum duration span in seconds of one slot
  151.          * in the {@link GenericTimeStampedCache time-stamped cache}
  152.          * @param newSlotInterval time interval above which a new slot is created
  153.          * in the {@link GenericTimeStampedCache time-stamped cache}
  154.          */
  155.         private DTO(final InterpolatingTransformProvider interpolatingProvider,
  156.                     final int maxSlots, final double maxSpan, final double newSlotInterval) {
  157.             this.interpolatingProvider = interpolatingProvider;
  158.             this.maxSlots              = maxSlots;
  159.             this.maxSpan               = maxSpan;
  160.             this.newSlotInterval       = newSlotInterval;
  161.         }

  162.         /** Replace the deserialized data transfer object with a {@link ShiftingTransformProvider}.
  163.          * @return replacement {@link ShiftingTransformProvider}
  164.          */
  165.         private Object readResolve() {
  166.             // build a new provider, with an empty cache
  167.             return new ShiftingTransformProvider(interpolatingProvider,
  168.                                                  maxSlots, maxSpan, newSlotInterval);
  169.         }

  170.     }

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

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

  175.             try {
  176.                 final List<Transform> generated = new ArrayList<Transform>();

  177.                 if (existing == null) {

  178.                     // no prior existing transforms, just generate a first set
  179.                     for (int i = 0; i < cache.getNeighborsSize(); ++i) {
  180.                         generated.add(interpolatingProvider.getTransform(date.shiftedBy(i * interpolatingProvider.getStep())));
  181.                     }

  182.                 } else {

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

  185.                     AbsoluteDate t = existing.getDate();
  186.                     if (date.compareTo(t) > 0) {
  187.                         // forward generation
  188.                         do {
  189.                             t = t.shiftedBy(interpolatingProvider.getStep());
  190.                             generated.add(generated.size(), interpolatingProvider.getTransform(t));
  191.                         } while (t.compareTo(date) <= 0);
  192.                     } else {
  193.                         // backward generation
  194.                         do {
  195.                             t = t.shiftedBy(-interpolatingProvider.getStep());
  196.                             generated.add(0, interpolatingProvider.getTransform(t));
  197.                         } while (t.compareTo(date) >= 0);
  198.                     }
  199.                 }

  200.                 // return the generated transforms
  201.                 return generated;
  202.             } catch (OrekitException oe) {
  203.                 throw new OrekitExceptionWrapper(oe);
  204.             }

  205.         }

  206.     }

  207. }