TabulatedProvider.java

  1. /* Copyright 2002-2018 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.errors.OrekitException;
  24. import org.orekit.frames.Frame;
  25. import org.orekit.time.AbsoluteDate;
  26. import org.orekit.time.FieldAbsoluteDate;
  27. import org.orekit.utils.AngularDerivativesFilter;
  28. import org.orekit.utils.FieldPVCoordinatesProvider;
  29. import org.orekit.utils.ImmutableTimeStampedCache;
  30. import org.orekit.utils.PVCoordinatesProvider;
  31. import org.orekit.utils.TimeStampedAngularCoordinates;
  32. import org.orekit.utils.TimeStampedFieldAngularCoordinates;


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


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

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

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

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

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

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

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

  67.         // interpolate
  68.         final TimeStampedAngularCoordinates interpolated =
  69.                 TimeStampedAngularCoordinates.interpolate(date, filter, sample);

  70.         // build the attitude
  71.         return new Attitude(referenceFrame, interpolated);

  72.     }

  73.     /** {@inheritDoc} */
  74.     public <T extends RealFieldElement<T>> FieldAttitude<T> getAttitude(final FieldPVCoordinatesProvider<T> pvProv,
  75.                                                                         final FieldAbsoluteDate<T> date,
  76.                                                                         final Frame frame)
  77.         throws OrekitException {

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

  84.         // interpolate
  85.         final TimeStampedFieldAngularCoordinates<T> interpolated =
  86.                 TimeStampedFieldAngularCoordinates.interpolate(date, filter, sample);

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

  89.     }

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

  97.     /** Internal class used only for serialization. */
  98.     private static class DataTransferObject implements Serializable {

  99.         /** Serializable UID. */
  100.         private static final long serialVersionUID = 20140723L;

  101.         /** Reference frame for tabulated attitudes. */
  102.         private final Frame referenceFrame;

  103.         /** Cached attitude table. */
  104.         private final List<TimeStampedAngularCoordinates> list;

  105.         /** Number of attitude to use for interpolation. */
  106.         private final int n;

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

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

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

  128.     }

  129. }