TabulatedProvider.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.attitudes;

  18. import java.io.NotSerializableException;
  19. import java.io.Serializable;
  20. import java.util.List;
  21. import java.util.stream.Collectors;

  22. import org.hipparchus.RealFieldElement;
  23. import org.orekit.frames.Frame;
  24. import org.orekit.time.AbsoluteDate;
  25. import org.orekit.time.FieldAbsoluteDate;
  26. import org.orekit.utils.AngularDerivativesFilter;
  27. import org.orekit.utils.FieldPVCoordinatesProvider;
  28. import org.orekit.utils.ImmutableTimeStampedCache;
  29. import org.orekit.utils.PVCoordinatesProvider;
  30. import org.orekit.utils.TimeStampedAngularCoordinates;
  31. import org.orekit.utils.TimeStampedFieldAngularCoordinates;


  32. /**
  33.  * This class handles an attitude provider interpolating from a predefined table.
  34.  * <p>Instances of this class are guaranteed to be immutable.</p>
  35.  * @author Luc Maisonobe
  36.  * @see TabulatedLofOffset
  37.  * @since 6.1
  38.  */
  39. public class TabulatedProvider implements AttitudeProvider {


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

  42.     /** Reference frame for tabulated attitudes. */
  43.     private final Frame referenceFrame;

  44.     /** Cached attitude table. */
  45.     private final transient ImmutableTimeStampedCache<TimeStampedAngularCoordinates> table;

  46.     /** Filter for derivatives from the sample to use in interpolation. */
  47.     private final AngularDerivativesFilter filter;

  48.     /** Creates new instance.
  49.      * @param referenceFrame reference frame for tabulated attitudes
  50.      * @param table tabulated attitudes
  51.      * @param n number of attitude to use for interpolation
  52.      * @param filter filter for derivatives from the sample to use in interpolation
  53.      */
  54.     public TabulatedProvider(final Frame referenceFrame, final List<TimeStampedAngularCoordinates> table,
  55.                              final int n, final AngularDerivativesFilter filter) {
  56.         this.referenceFrame  = referenceFrame;
  57.         this.table           = new ImmutableTimeStampedCache<TimeStampedAngularCoordinates>(n, table);
  58.         this.filter          = filter;
  59.     }

  60.     /** {@inheritDoc} */
  61.     public Attitude getAttitude(final PVCoordinatesProvider pvProv,
  62.                                 final AbsoluteDate date, final Frame frame) {

  63.         // get attitudes sample on which interpolation will be performed
  64.         final List<TimeStampedAngularCoordinates> sample = table.getNeighbors(date).collect(Collectors.toList());

  65.         // interpolate
  66.         final TimeStampedAngularCoordinates interpolated =
  67.                 TimeStampedAngularCoordinates.interpolate(date, filter, sample);

  68.         // build the attitude
  69.         return new Attitude(referenceFrame, interpolated);

  70.     }

  71.     /** {@inheritDoc} */
  72.     public <T extends RealFieldElement<T>> FieldAttitude<T> getAttitude(final FieldPVCoordinatesProvider<T> pvProv,
  73.                                                                         final FieldAbsoluteDate<T> date,
  74.                                                                         final Frame frame) {

  75.         // get attitudes sample on which interpolation will be performed
  76.         final List<TimeStampedFieldAngularCoordinates<T>> sample =
  77.                         table.
  78.                         getNeighbors(date.toAbsoluteDate()).
  79.                         map(ac -> new TimeStampedFieldAngularCoordinates<>(date.getField(), ac)).
  80.                         collect(Collectors.toList());

  81.         // interpolate
  82.         final TimeStampedFieldAngularCoordinates<T> interpolated =
  83.                 TimeStampedFieldAngularCoordinates.interpolate(date, filter, sample);

  84.         // build the attitude
  85.         return new FieldAttitude<>(referenceFrame, interpolated);

  86.     }

  87.     /** Replace the instance with a data transfer object for serialization.
  88.      * @return data transfer object that will be serialized
  89.      * @exception NotSerializableException if the state mapper cannot be serialized (typically for DSST propagator)
  90.      */
  91.     private Object writeReplace() throws NotSerializableException {
  92.         return new DataTransferObject(referenceFrame, table.getAll(), table.getNeighborsSize(), filter);
  93.     }

  94.     /** Internal class used only for serialization. */
  95.     private static class DataTransferObject implements Serializable {

  96.         /** Serializable UID. */
  97.         private static final long serialVersionUID = 20140723L;

  98.         /** Reference frame for tabulated attitudes. */
  99.         private final Frame referenceFrame;

  100.         /** Cached attitude table. */
  101.         private final List<TimeStampedAngularCoordinates> list;

  102.         /** Number of attitude to use for interpolation. */
  103.         private final int n;

  104.         /** Filter for derivatives from the sample to use in interpolation. */
  105.         private final AngularDerivativesFilter filter;

  106.         /** Simple constructor.
  107.          * @param referenceFrame reference frame for tabulated attitudes
  108.          * @param list tabulated attitudes
  109.          * @param n number of attitude to use for interpolation
  110.          * @param filter filter for derivatives from the sample to use in interpolation
  111.          */
  112.         DataTransferObject(final Frame referenceFrame, final List<TimeStampedAngularCoordinates> list,
  113.                                   final int n, final AngularDerivativesFilter filter) {
  114.             this.referenceFrame  = referenceFrame;
  115.             this.list            = list;
  116.             this.n               = n;
  117.             this.filter          = filter;
  118.         }

  119.         /** Replace the deserialized data transfer object with a {@link TabulatedProvider}.
  120.          * @return replacement {@link TabulatedProvider}
  121.          */
  122.         private Object readResolve() {
  123.             return new TabulatedProvider(referenceFrame, list, n, filter);
  124.         }

  125.     }

  126. }