TabulatedLofOffset.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.errors.OrekitException;
  24. import org.orekit.errors.OrekitInternalError;
  25. import org.orekit.errors.OrekitMessages;
  26. import org.orekit.frames.FieldTransform;
  27. import org.orekit.frames.Frame;
  28. import org.orekit.frames.LOFType;
  29. import org.orekit.frames.Transform;
  30. import org.orekit.time.AbsoluteDate;
  31. import org.orekit.time.FieldAbsoluteDate;
  32. import org.orekit.utils.AngularDerivativesFilter;
  33. import org.orekit.utils.FieldPVCoordinates;
  34. import org.orekit.utils.FieldPVCoordinatesProvider;
  35. import org.orekit.utils.ImmutableTimeStampedCache;
  36. import org.orekit.utils.PVCoordinates;
  37. import org.orekit.utils.PVCoordinatesProvider;
  38. import org.orekit.utils.TimeStampedAngularCoordinates;
  39. import org.orekit.utils.TimeStampedFieldAngularCoordinates;

  40. /**
  41.  * This class handles an attitude provider interpolating from a predefined table
  42.  * containing offsets from a Local Orbital Frame.
  43.  * <p>Instances of this class are guaranteed to be immutable.</p>
  44.  * @see LofOffset
  45.  * @see TabulatedProvider
  46.  * @author Luc Maisonobe
  47.  * @since 7.1
  48.  */
  49. public class TabulatedLofOffset implements AttitudeProvider {


  50.     /** Serializable UID. */
  51.     private static final long serialVersionUID = 20151211L;

  52.     /** Inertial frame with respect to which orbit should be computed. */
  53.     private final Frame inertialFrame;

  54.     /** Type of Local Orbital Frame. */
  55.     private LOFType type;

  56.     /** Cached attitude table. */
  57.     private final transient ImmutableTimeStampedCache<TimeStampedAngularCoordinates> table;

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

  60.     /** Creates new instance.
  61.      * @param inertialFrame inertial frame with respect to which orbit should be computed
  62.      * @param type type of Local Orbital Frame
  63.      * @param table tabulated attitudes
  64.      * @param n number of attitude to use for interpolation
  65.      * @param filter filter for derivatives from the sample to use in interpolation
  66.      */
  67.     public TabulatedLofOffset(final Frame inertialFrame, final LOFType type,
  68.                               final List<TimeStampedAngularCoordinates> table,
  69.                               final int n, final AngularDerivativesFilter filter) {
  70.         if (!inertialFrame.isPseudoInertial()) {
  71.             throw new OrekitException(OrekitMessages.NON_PSEUDO_INERTIAL_FRAME,
  72.                                       inertialFrame.getName());
  73.         }
  74.         this.inertialFrame = inertialFrame;
  75.         this.type          = type;
  76.         this.table         = new ImmutableTimeStampedCache<TimeStampedAngularCoordinates>(n, table);
  77.         this.filter        = filter;
  78.     }

  79.     /** Get an unmodifiable view of the tabulated attitudes.
  80.      * @return unmodifiable view of the tabulated attitudes
  81.      */
  82.     public List<TimeStampedAngularCoordinates> getTable() {
  83.         return table.getAll();
  84.     }

  85.     /** {@inheritDoc} */
  86.     public Attitude getAttitude(final PVCoordinatesProvider pvProv,
  87.                                 final AbsoluteDate date, final Frame frame) {

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

  90.         // interpolate
  91.         final TimeStampedAngularCoordinates interpolated =
  92.                 TimeStampedAngularCoordinates.interpolate(date, filter, sample);

  93.         // construction of the local orbital frame, using PV from inertial frame
  94.         final PVCoordinates pv = pvProv.getPVCoordinates(date, inertialFrame);
  95.         final Transform inertialToLof = type.transformFromInertial(date, pv);

  96.         // take into account the specified start frame (which may not be an inertial one)
  97.         final Transform frameToInertial = frame.getTransformTo(inertialFrame, date);
  98.         final Transform frameToLof      = new Transform(date, frameToInertial, inertialToLof);

  99.         // compose with interpolated rotation
  100.         return new Attitude(date, frame,
  101.                             interpolated.addOffset(frameToLof.getAngular()));
  102.     }

  103.     /** {@inheritDoc} */
  104.     public <T extends RealFieldElement<T>> FieldAttitude<T> getAttitude(final FieldPVCoordinatesProvider<T> pvProv,
  105.                                                                         final FieldAbsoluteDate<T> date,
  106.                                                                         final Frame frame) {

  107.         // get attitudes sample on which interpolation will be performed
  108.         final List<TimeStampedFieldAngularCoordinates<T>> sample =
  109.                         table.
  110.                         getNeighbors(date.toAbsoluteDate()).
  111.                         map(ac -> new TimeStampedFieldAngularCoordinates<>(date.getField(), ac)).
  112.                         collect(Collectors.toList());

  113.         // interpolate
  114.         final TimeStampedFieldAngularCoordinates<T> interpolated =
  115.                 TimeStampedFieldAngularCoordinates.interpolate(date, filter, sample);

  116.         // construction of the local orbital frame, using PV from inertial frame
  117.         final FieldPVCoordinates<T> pv = pvProv.getPVCoordinates(date, inertialFrame);
  118.         final FieldTransform<T> inertialToLof = type.transformFromInertial(date, pv);

  119.         // take into account the specified start frame (which may not be an inertial one)
  120.         final FieldTransform<T> frameToInertial = frame.getTransformTo(inertialFrame, date);
  121.         final FieldTransform<T> frameToLof      = new FieldTransform<>(date, frameToInertial, inertialToLof);

  122.         // compose with interpolated rotation
  123.         return new FieldAttitude<>(date, frame,
  124.                                    interpolated.addOffset(frameToLof.getAngular()));
  125.     }

  126.     /** Replace the instance with a data transfer object for serialization.
  127.      * @return data transfer object that will be serialized
  128.      * @exception NotSerializableException if the state mapper cannot be serialized (typically for DSST propagator)
  129.      */
  130.     private Object writeReplace() throws NotSerializableException {
  131.         return new DataTransferObject(inertialFrame, type, table.getAll(), table.getNeighborsSize(), filter);
  132.     }

  133.     /** Internal class used only for serialization. */
  134.     private static class DataTransferObject implements Serializable {

  135.         /** Serializable UID. */
  136.         private static final long serialVersionUID = 20151211L;

  137.         /** Inertial frame with respect to which orbit should be computed. */
  138.         private final Frame inertialFrame;

  139.         /** Type of Local Orbital Frame. */
  140.         private LOFType type;

  141.         /** Cached attitude table. */
  142.         private final List<TimeStampedAngularCoordinates> list;

  143.         /** Number of attitude to use for interpolation. */
  144.         private final int n;

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

  147.         /** Simple constructor.
  148.          * @param inertialFrame inertial frame with respect to which orbit should be computed
  149.          * @param type type of Local Orbital Frame
  150.          * @param list tabulated attitudes
  151.          * @param n number of attitude to use for interpolation
  152.          * @param filter filter for derivatives from the sample to use in interpolation
  153.          */
  154.         DataTransferObject(final Frame inertialFrame, final LOFType type,
  155.                            final List<TimeStampedAngularCoordinates> list,
  156.                            final int n, final AngularDerivativesFilter filter) {
  157.             this.inertialFrame = inertialFrame;
  158.             this.type          = type;
  159.             this.list          = list;
  160.             this.n             = n;
  161.             this.filter        = filter;
  162.         }

  163.         /** Replace the deserialized data transfer object with a {@link TabulatedLofOffset}.
  164.          * @return replacement {@link TabulatedLofOffset}
  165.          */
  166.         private Object readResolve() {
  167.             try {
  168.                 return new TabulatedLofOffset(inertialFrame, type, list, n, filter);
  169.             } catch (OrekitException oe) {
  170.                 // this should never happen
  171.                 throw new OrekitInternalError(oe);
  172.             }
  173.         }

  174.     }

  175. }