Frame.java

  1. /* Copyright 2002-2022 CS GROUP
  2.  * Licensed to CS GROUP (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.function.BiFunction;
  20. import java.util.function.Function;

  21. import org.hipparchus.CalculusFieldElement;
  22. import org.hipparchus.Field;
  23. import org.orekit.errors.OrekitIllegalArgumentException;
  24. import org.orekit.errors.OrekitMessages;
  25. import org.orekit.time.AbsoluteDate;
  26. import org.orekit.time.FieldAbsoluteDate;


  27. /** Tridimensional references frames class.
  28.  *
  29.  * <h2> Frame Presentation </h2>
  30.  * <p>This class is the base class for all frames in OREKIT. The frames are
  31.  * linked together in a tree with some specific frame chosen as the root of the tree.
  32.  * Each frame is defined by {@link Transform transforms} combining any number
  33.  * of translations and rotations from a reference frame which is its
  34.  * parent frame in the tree structure.</p>
  35.  * <p>When we say a {@link Transform transform} t is <em>from frame<sub>A</sub>
  36.  * to frame<sub>B</sub></em>, we mean that if the coordinates of some absolute
  37.  * vector (say the direction of a distant star for example) has coordinates
  38.  * u<sub>A</sub> in frame<sub>A</sub> and u<sub>B</sub> in frame<sub>B</sub>,
  39.  * then u<sub>B</sub>={@link
  40.  * Transform#transformVector(org.hipparchus.geometry.euclidean.threed.Vector3D)
  41.  * t.transformVector(u<sub>A</sub>)}.
  42.  * <p>The transforms may be constant or varying, depending on the implementation of
  43.  * the {@link TransformProvider transform provider} used to define the frame. For simple
  44.  * fixed transforms, using {@link FixedTransformProvider} is sufficient. For varying
  45.  * transforms (time-dependent or telemetry-based for example), it may be useful to define
  46.  * specific implementations of {@link TransformProvider transform provider}.</p>
  47.  *
  48.  * @author Guylaine Prat
  49.  * @author Luc Maisonobe
  50.  * @author Pascal Parraud
  51.  */
  52. public class Frame implements Serializable {

  53.     /** Serializable UID. */
  54.     private static final long serialVersionUID = -6981146543760234087L;

  55.     /** Parent frame (only the root frame doesn't have a parent). */
  56.     private final Frame parent;

  57.     /** Depth of the frame with respect to tree root. */
  58.     private final int depth;

  59.     /** Provider for transform from parent frame to instance. */
  60.     private final TransformProvider transformProvider;

  61.     /** Instance name. */
  62.     private final String name;

  63.     /** Indicator for pseudo-inertial frames. */
  64.     private final boolean pseudoInertial;

  65.     /** Private constructor used only for the root frame.
  66.      * @param name name of the frame
  67.      * @param pseudoInertial true if frame is considered pseudo-inertial
  68.      * (i.e. suitable for propagating orbit)
  69.      */
  70.     private Frame(final String name, final boolean pseudoInertial) {
  71.         parent              = null;
  72.         depth               = 0;
  73.         transformProvider   = new FixedTransformProvider(Transform.IDENTITY);
  74.         this.name           = name;
  75.         this.pseudoInertial = pseudoInertial;
  76.     }

  77.     /** Build a non-inertial frame from its transform with respect to its parent.
  78.      * <p>calling this constructor is equivalent to call
  79.      * <code>{link {@link #Frame(Frame, Transform, String, boolean)
  80.      * Frame(parent, transform, name, false)}</code>.</p>
  81.      * @param parent parent frame (must be non-null)
  82.      * @param transform transform from parent frame to instance
  83.      * @param name name of the frame
  84.      * @exception IllegalArgumentException if the parent frame is null
  85.      */
  86.     public Frame(final Frame parent, final Transform transform, final String name)
  87.         throws IllegalArgumentException {
  88.         this(parent, transform, name, false);
  89.     }

  90.     /** Build a non-inertial frame from its transform with respect to its parent.
  91.      * <p>calling this constructor is equivalent to call
  92.      * <code>{link {@link #Frame(Frame, Transform, String, boolean)
  93.      * Frame(parent, transform, name, false)}</code>.</p>
  94.      * @param parent parent frame (must be non-null)
  95.      * @param transformProvider provider for transform from parent frame to instance
  96.      * @param name name of the frame
  97.      * @exception IllegalArgumentException if the parent frame is null
  98.      */
  99.     public Frame(final Frame parent, final TransformProvider transformProvider, final String name)
  100.         throws IllegalArgumentException {
  101.         this(parent, transformProvider, name, false);
  102.     }

  103.     /** Build a frame from its transform with respect to its parent.
  104.      * <p>The convention for the transform is that it is from parent
  105.      * frame to instance. This means that the two following frames
  106.      * are similar:</p>
  107.      * <pre>
  108.      * Frame frame1 = new Frame(FramesFactory.getGCRF(), new Transform(t1, t2));
  109.      * Frame frame2 = new Frame(new Frame(FramesFactory.getGCRF(), t1), t2);
  110.      * </pre>
  111.      * @param parent parent frame (must be non-null)
  112.      * @param transform transform from parent frame to instance
  113.      * @param name name of the frame
  114.      * @param pseudoInertial true if frame is considered pseudo-inertial
  115.      * (i.e. suitable for propagating orbit)
  116.      * @exception IllegalArgumentException if the parent frame is null
  117.      */
  118.     public Frame(final Frame parent, final Transform transform, final String name,
  119.                  final boolean pseudoInertial)
  120.         throws IllegalArgumentException {
  121.         this(parent, new FixedTransformProvider(transform), name, pseudoInertial);
  122.     }

  123.     /** Build a frame from its transform with respect to its parent.
  124.      * <p>The convention for the transform is that it is from parent
  125.      * frame to instance. This means that the two following frames
  126.      * are similar:</p>
  127.      * <pre>
  128.      * Frame frame1 = new Frame(FramesFactory.getGCRF(), new Transform(t1, t2));
  129.      * Frame frame2 = new Frame(new Frame(FramesFactory.getGCRF(), t1), t2);
  130.      * </pre>
  131.      * @param parent parent frame (must be non-null)
  132.      * @param transformProvider provider for transform from parent frame to instance
  133.      * @param name name of the frame
  134.      * @param pseudoInertial true if frame is considered pseudo-inertial
  135.      * (i.e. suitable for propagating orbit)
  136.      * @exception IllegalArgumentException if the parent frame is null
  137.      */
  138.     public Frame(final Frame parent, final TransformProvider transformProvider, final String name,
  139.                  final boolean pseudoInertial)
  140.         throws IllegalArgumentException {

  141.         if (parent == null) {
  142.             throw new OrekitIllegalArgumentException(OrekitMessages.NULL_PARENT_FOR_FRAME, name);
  143.         }
  144.         this.parent            = parent;
  145.         this.depth             = parent.depth + 1;
  146.         this.transformProvider = transformProvider;
  147.         this.name              = name;
  148.         this.pseudoInertial    = pseudoInertial;

  149.     }

  150.     /** Get the name.
  151.      * @return the name
  152.      */
  153.     public String getName() {
  154.         return this.name;
  155.     }

  156.     /** Check if the frame is pseudo-inertial.
  157.      * <p>Pseudo-inertial frames are frames that do have a linear motion and
  158.      * either do not rotate or rotate at a very low rate resulting in
  159.      * neglectible inertial forces. This means they are suitable for orbit
  160.      * definition and propagation using Newtonian mechanics. Frames that are
  161.      * <em>not</em> pseudo-inertial are <em>not</em> suitable for orbit
  162.      * definition and propagation.</p>
  163.      * @return true if frame is pseudo-inertial
  164.      */
  165.     public boolean isPseudoInertial() {
  166.         return pseudoInertial;
  167.     }

  168.     /** New definition of the java.util toString() method.
  169.      * @return the name
  170.      */
  171.     public String toString() {
  172.         return this.name;
  173.     }

  174.     /** Get the parent frame.
  175.      * @return parent frame
  176.      */
  177.     public Frame getParent() {
  178.         return parent;
  179.     }

  180.     /** Get the depth of the frame.
  181.      * <p>
  182.      * The depth of a frame is the number of parents frame between
  183.      * it and the frames tree root. It is 0 for the root frame, and
  184.      * the depth of a frame is the depth of its parent frame plus one.
  185.      * </p>
  186.      * @return depth of the frame
  187.      */
  188.     public int getDepth() {
  189.         return depth;
  190.     }

  191.     /** Get the n<sup>th</sup> ancestor of the frame.
  192.      * @param n index of the ancestor (0 is the instance, 1 is its parent,
  193.      * 2 is the parent of its parent...)
  194.      * @return n<sup>th</sup> ancestor of the frame (must be between 0
  195.      * and the depth of the frame)
  196.      * @exception IllegalArgumentException if n is larger than the depth
  197.      * of the instance
  198.      */
  199.     public Frame getAncestor(final int n) throws IllegalArgumentException {

  200.         // safety check
  201.         if (n > depth) {
  202.             throw new OrekitIllegalArgumentException(OrekitMessages.FRAME_NO_NTH_ANCESTOR,
  203.                                                      name, depth, n);
  204.         }

  205.         // go upward to find ancestor
  206.         Frame current = this;
  207.         for (int i = 0; i < n; ++i) {
  208.             current = current.parent;
  209.         }

  210.         return current;

  211.     }

  212.     /** Get the transform from the instance to another frame.
  213.      * @param destination destination frame to which we want to transform vectors
  214.      * @param date the date (can be null if it is sure than no date dependent frame is used)
  215.      * @return transform from the instance to the destination frame
  216.      */
  217.     public Transform getTransformTo(final Frame destination, final AbsoluteDate date) {
  218.         return getTransformTo(
  219.                 destination,
  220.                 Transform.IDENTITY,
  221.                 frame -> frame.getTransformProvider().getTransform(date),
  222.                 (t1, t2) -> new Transform(date, t1, t2),
  223.                 Transform::getInverse);
  224.     }

  225.     /** Get the transform from the instance to another frame.
  226.      * @param destination destination frame to which we want to transform vectors
  227.      * @param date the date (can be null if it is sure than no date dependent frame is used)
  228.      * @param <T> the type of the field elements
  229.      * @return transform from the instance to the destination frame
  230.      */
  231.     public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransformTo(final Frame destination, final FieldAbsoluteDate<T> date) {
  232.         final Field<T> field = date.getField();
  233.         return getTransformTo(
  234.                 destination,
  235.                 FieldTransform.getIdentity(field),
  236.                 frame -> frame.getTransformProvider().getTransform(date),
  237.                 (t1, t2) -> new FieldTransform<>(date, t1, t2),
  238.                 FieldTransform::getInverse);
  239.     }

  240.     /**
  241.      * Get the static portion of the transform from the instance to another
  242.      * frame. The returned transform is static in the sense that it includes
  243.      * translations and rotations, but not rates.
  244.      *
  245.      * <p>This method is often more performant than {@link
  246.      * #getTransformTo(Frame, AbsoluteDate)} when rates are not needed.
  247.      *
  248.      * @param destination destination frame to which we want to transform
  249.      *                    vectors
  250.      * @param date        the date (can be null if it is sure than no date
  251.      *                    dependent frame is used)
  252.      * @return static transform from the instance to the destination frame
  253.      * @since 11.2
  254.      */
  255.     public StaticTransform getStaticTransformTo(final Frame destination,
  256.                                                 final AbsoluteDate date) {
  257.         return getTransformTo(
  258.                 destination,
  259.                 StaticTransform.getIdentity(),
  260.                 frame -> frame.getTransformProvider().getStaticTransform(date),
  261.                 (t1, t2) -> StaticTransform.compose(date, t1, t2),
  262.                 StaticTransform::getInverse);
  263.     }

  264.     /**
  265.      * Generic get transform method that builds the transform from {@code this}
  266.      * to {@code destination}.
  267.      *
  268.      * @param destination  destination frame to which we want to transform
  269.      *                     vectors
  270.      * @param identity     transform of the given type.
  271.      * @param getTransform method to get a transform from a frame.
  272.      * @param compose      method to combine two transforms.
  273.      * @param inverse      method to invert a transform.
  274.      * @param <T>          Type of transform returned.
  275.      * @return composite transform.
  276.      */
  277.     private <T> T getTransformTo(final Frame destination,
  278.                                  final T identity,
  279.                                  final Function<Frame, T> getTransform,
  280.                                  final BiFunction<T, T, T> compose,
  281.                                  final Function<T, T> inverse) {

  282.         if (this == destination) {
  283.             // shortcut for special case that may be frequent
  284.             return identity;
  285.         }

  286.         // common ancestor to both frames in the frames tree
  287.         final Frame common = findCommon(this, destination);

  288.         // transform from common to instance
  289.         T commonToInstance = identity;
  290.         for (Frame frame = this; frame != common; frame = frame.parent) {
  291.             commonToInstance = compose.apply(getTransform.apply(frame), commonToInstance);
  292.         }

  293.         // transform from destination up to common
  294.         T commonToDestination = identity;
  295.         for (Frame frame = destination; frame != common; frame = frame.parent) {
  296.             commonToDestination = compose.apply(getTransform.apply(frame), commonToDestination);
  297.         }

  298.         // transform from instance to destination via common
  299.         return compose.apply(inverse.apply(commonToInstance), commonToDestination);

  300.     }

  301.     /** Get the provider for transform from parent frame to instance.
  302.      * @return provider for transform from parent frame to instance
  303.      */
  304.     public TransformProvider getTransformProvider() {
  305.         return transformProvider;
  306.     }

  307.     /** Find the deepest common ancestor of two frames in the frames tree.
  308.      * @param from origin frame
  309.      * @param to destination frame
  310.      * @return an ancestor frame of both <code>from</code> and <code>to</code>
  311.      */
  312.     private static Frame findCommon(final Frame from, final Frame to) {

  313.         // select deepest frames that could be the common ancestor
  314.         Frame currentF = from.depth > to.depth ? from.getAncestor(from.depth - to.depth) : from;
  315.         Frame currentT = from.depth > to.depth ? to : to.getAncestor(to.depth - from.depth);

  316.         // go upward until we find a match
  317.         while (currentF != currentT) {
  318.             currentF = currentF.parent;
  319.             currentT = currentT.parent;
  320.         }

  321.         return currentF;

  322.     }

  323.     /** Determine if a Frame is a child of another one.
  324.      * @param potentialAncestor supposed ancestor frame
  325.      * @return true if the potentialAncestor belongs to the
  326.      * path from instance to the root frame, excluding itself
  327.      */
  328.     public boolean isChildOf(final Frame potentialAncestor) {
  329.         if (depth <= potentialAncestor.depth) {
  330.             return false;
  331.         }
  332.         return getAncestor(depth - potentialAncestor.depth) == potentialAncestor;
  333.     }

  334.     /** Get the unique root frame.
  335.      * @return the unique instance of the root frame
  336.      */
  337.     public static Frame getRoot() {
  338.         return LazyRootHolder.INSTANCE;
  339.     }

  340.     /** Get a new version of the instance, frozen with respect to a reference frame.
  341.      * <p>
  342.      * Freezing a frame consist in computing its position and orientation with respect
  343.      * to another frame at some freezing date and fixing them so they do not depend
  344.      * on time anymore. This means the frozen frame is fixed with respect to the
  345.      * reference frame.
  346.      * </p>
  347.      * <p>
  348.      * One typical use of this method is to compute an inertial launch reference frame
  349.      * by freezing a {@link TopocentricFrame topocentric frame} at launch date
  350.      * with respect to an inertial frame. Another use is to freeze an equinox-related
  351.      * celestial frame at a reference epoch date.
  352.      * </p>
  353.      * <p>
  354.      * Only the frame returned by this method is frozen, the instance by itself
  355.      * is not affected by calling this method and still moves freely.
  356.      * </p>
  357.      * @param reference frame with respect to which the instance will be frozen
  358.      * @param freezingDate freezing date
  359.      * @param frozenName name of the frozen frame
  360.      * @return a frozen version of the instance
  361.      */
  362.     public Frame getFrozenFrame(final Frame reference, final AbsoluteDate freezingDate,
  363.                                 final String frozenName) {
  364.         return new Frame(reference, reference.getTransformTo(this, freezingDate).freeze(),
  365.                          frozenName, reference.isPseudoInertial());
  366.     }

  367.     // We use the Initialization on demand holder idiom to store
  368.     // the singletons, as it is both thread-safe, efficient (no
  369.     // synchronization) and works with all versions of java.

  370.     /** Holder for the root frame singleton. */
  371.     private static class LazyRootHolder {

  372.         /** Unique instance. */
  373.         private static final Frame INSTANCE = new Frame(Predefined.GCRF.getName(), true) {

  374.             /** Serializable UID. */
  375.             private static final long serialVersionUID = -2654403496396721543L;

  376.             /** Replace the instance with a data transfer object for serialization.
  377.              * <p>
  378.              * This intermediate class serializes nothing.
  379.              * </p>
  380.              * @return data transfer object that will be serialized
  381.              */
  382.             private Object writeReplace() {
  383.                 return new DataTransferObject();
  384.             }

  385.         };

  386.         /** Private constructor.
  387.          * <p>This class is a utility class, it should neither have a public
  388.          * nor a default constructor. This private constructor prevents
  389.          * the compiler from generating one automatically.</p>
  390.          */
  391.         private LazyRootHolder() {
  392.         }

  393.     }

  394.     /** Internal class used only for serialization. */
  395.     private static class DataTransferObject implements Serializable {

  396.         /** Serializable UID. */
  397.         private static final long serialVersionUID = 4067764035816491212L;

  398.         /** Simple constructor.
  399.          */
  400.         private DataTransferObject() {
  401.         }

  402.         /** Replace the deserialized data transfer object with a {@link FactoryManagedFrame}.
  403.          * @return replacement {@link FactoryManagedFrame}
  404.          */
  405.         private Object readResolve() {
  406.             return getRoot();
  407.         }

  408.     }

  409. }