TabulatedLofOffset.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.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.      * @exception OrekitException if inertialFrame is not a pseudo-inertial frame
  67.      */
  68.     public TabulatedLofOffset(final Frame inertialFrame, final LOFType type,
  69.                               final List<TimeStampedAngularCoordinates> table,
  70.                               final int n, final AngularDerivativesFilter filter)
  71.         throws OrekitException {
  72.         if (!inertialFrame.isPseudoInertial()) {
  73.             throw new OrekitException(OrekitMessages.NON_PSEUDO_INERTIAL_FRAME,
  74.                                       inertialFrame.getName());
  75.         }
  76.         this.inertialFrame = inertialFrame;
  77.         this.type          = type;
  78.         this.table         = new ImmutableTimeStampedCache<TimeStampedAngularCoordinates>(n, table);
  79.         this.filter        = filter;
  80.     }

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

  87.     /** {@inheritDoc} */
  88.     public Attitude getAttitude(final PVCoordinatesProvider pvProv,
  89.                                 final AbsoluteDate date, final Frame frame)
  90.         throws OrekitException {

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

  93.         // interpolate
  94.         final TimeStampedAngularCoordinates interpolated =
  95.                 TimeStampedAngularCoordinates.interpolate(date, filter, sample);

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

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

  102.         // compose with interpolated rotation
  103.         return new Attitude(date, frame,
  104.                             interpolated.addOffset(frameToLof.getAngular()));
  105.     }

  106.     /** {@inheritDoc} */
  107.     public <T extends RealFieldElement<T>> FieldAttitude<T> getAttitude(final FieldPVCoordinatesProvider<T> pvProv,
  108.                                                                         final FieldAbsoluteDate<T> date,
  109.                                                                         final Frame frame)
  110.         throws OrekitException {

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

  117.         // interpolate
  118.         final TimeStampedFieldAngularCoordinates<T> interpolated =
  119.                 TimeStampedFieldAngularCoordinates.interpolate(date, filter, sample);

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

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

  126.         // compose with interpolated rotation
  127.         return new FieldAttitude<>(date, frame,
  128.                                    interpolated.addOffset(frameToLof.getAngular()));
  129.     }

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

  137.     /** Internal class used only for serialization. */
  138.     private static class DataTransferObject implements Serializable {

  139.         /** Serializable UID. */
  140.         private static final long serialVersionUID = 20151211L;

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

  143.         /** Type of Local Orbital Frame. */
  144.         private LOFType type;

  145.         /** Cached attitude table. */
  146.         private final List<TimeStampedAngularCoordinates> list;

  147.         /** Number of attitude to use for interpolation. */
  148.         private final int n;

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

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

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

  178.     }

  179. }