Frame.java

  1. /* Copyright 2002-2024 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.FieldElement;
  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 (<em>must</em> be non-null, which is a more stringent condition
  228.      *      *                than in {@link #getTransformTo(Frame, FieldAbsoluteDate)})
  229.      * @param <T> the type of the field elements
  230.      * @return transform from the instance to the destination frame
  231.      */
  232.     public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransformTo(final Frame destination, final FieldAbsoluteDate<T> date) {

  233.         return getTransformTo(destination,
  234.                               FieldTransform.getIdentity(date.getField()),
  235.                               frame -> frame.getTransformProvider().getTransform(date),
  236.                               (t1, t2) -> new FieldTransform<>(date, t1, t2),
  237.                               FieldTransform::getInverse);
  238.     }

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

  263.     /**
  264.      * Get the static portion of the transform from the instance to another
  265.      * frame. The returned transform is static in the sense that it includes
  266.      * translations and rotations, but not rates.
  267.      *
  268.      * <p>This method is often more performant than {@link
  269.      * #getTransformTo(Frame, FieldAbsoluteDate)} when rates are not needed.
  270.      *
  271.      * <p>A first check is made on the FieldAbsoluteDate because "fielded" transforms have low-performance.<br>
  272.      * The date field is checked with {@link FieldElement#isZero()}.<br>
  273.      * If true, the un-fielded version of the transform computation is used.
  274.      *
  275.      * @param <T>         type of the elements
  276.      * @param destination destination frame to which we want to transform
  277.      *                    vectors
  278.      * @param date        the date (<em>must</em> be non-null, which is a more stringent condition
  279.      *                    than in {@link #getStaticTransformTo(Frame, AbsoluteDate)})
  280.      * @return static transform from the instance to the destination frame
  281.      * @since 12.0
  282.      */
  283.     public <T extends CalculusFieldElement<T>> FieldStaticTransform<T> getStaticTransformTo(final Frame destination,
  284.                                                 final FieldAbsoluteDate<T> date) {
  285.         if (date.hasZeroField()) {
  286.             // If date field is Zero, then use the un-fielded version for performances
  287.             return FieldStaticTransform.of(date, getStaticTransformTo(destination, date.toAbsoluteDate()));

  288.         } else {
  289.             // Use classic fielded function
  290.             return getTransformTo(destination,
  291.                                   FieldStaticTransform.getIdentity(date.getField()),
  292.                                   frame -> frame.getTransformProvider().getStaticTransform(date),
  293.                                   (t1, t2) -> FieldStaticTransform.compose(date, t1, t2),
  294.                                   FieldStaticTransform::getInverse);
  295.         }
  296.     }

  297.     /**
  298.      * Generic get transform method that builds the transform from {@code this}
  299.      * to {@code destination}.
  300.      *
  301.      * @param destination  destination frame to which we want to transform
  302.      *                     vectors
  303.      * @param identity     transform of the given type.
  304.      * @param getTransform method to get a transform from a frame.
  305.      * @param compose      method to combine two transforms.
  306.      * @param inverse      method to invert a transform.
  307.      * @param <T>          Type of transform returned.
  308.      * @return composite transform.
  309.      */
  310.     private <T> T getTransformTo(final Frame destination,
  311.                                  final T identity,
  312.                                  final Function<Frame, T> getTransform,
  313.                                  final BiFunction<T, T, T> compose,
  314.                                  final Function<T, T> inverse) {

  315.         if (this == destination) {
  316.             // shortcut for special case that may be frequent
  317.             return identity;
  318.         }

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

  321.         // transform from common to instance
  322.         T commonToInstance = identity;
  323.         for (Frame frame = this; frame != common; frame = frame.parent) {
  324.             commonToInstance = compose.apply(getTransform.apply(frame), commonToInstance);
  325.         }

  326.         // transform from destination up to common
  327.         T commonToDestination = identity;
  328.         for (Frame frame = destination; frame != common; frame = frame.parent) {
  329.             commonToDestination = compose.apply(getTransform.apply(frame), commonToDestination);
  330.         }

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

  333.     }

  334.     /** Get the provider for transform from parent frame to instance.
  335.      * @return provider for transform from parent frame to instance
  336.      */
  337.     public TransformProvider getTransformProvider() {
  338.         return transformProvider;
  339.     }

  340.     /** Find the deepest common ancestor of two frames in the frames tree.
  341.      * @param from origin frame
  342.      * @param to destination frame
  343.      * @return an ancestor frame of both <code>from</code> and <code>to</code>
  344.      */
  345.     private static Frame findCommon(final Frame from, final Frame to) {

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

  349.         // go upward until we find a match
  350.         while (currentF != currentT) {
  351.             currentF = currentF.parent;
  352.             currentT = currentT.parent;
  353.         }

  354.         return currentF;

  355.     }

  356.     /** Determine if a Frame is a child of another one.
  357.      * @param potentialAncestor supposed ancestor frame
  358.      * @return true if the potentialAncestor belongs to the
  359.      * path from instance to the root frame, excluding itself
  360.      */
  361.     public boolean isChildOf(final Frame potentialAncestor) {
  362.         if (depth <= potentialAncestor.depth) {
  363.             return false;
  364.         }
  365.         return getAncestor(depth - potentialAncestor.depth) == potentialAncestor;
  366.     }

  367.     /** Get the unique root frame.
  368.      * @return the unique instance of the root frame
  369.      */
  370.     public static Frame getRoot() {
  371.         return LazyRootHolder.INSTANCE;
  372.     }

  373.     /** Get a new version of the instance, frozen with respect to a reference frame.
  374.      * <p>
  375.      * Freezing a frame consist in computing its position and orientation with respect
  376.      * to another frame at some freezing date and fixing them so they do not depend
  377.      * on time anymore. This means the frozen frame is fixed with respect to the
  378.      * reference frame.
  379.      * </p>
  380.      * <p>
  381.      * One typical use of this method is to compute an inertial launch reference frame
  382.      * by freezing a {@link TopocentricFrame topocentric frame} at launch date
  383.      * with respect to an inertial frame. Another use is to freeze an equinox-related
  384.      * celestial frame at a reference epoch date.
  385.      * </p>
  386.      * <p>
  387.      * Only the frame returned by this method is frozen, the instance by itself
  388.      * is not affected by calling this method and still moves freely.
  389.      * </p>
  390.      * @param reference frame with respect to which the instance will be frozen
  391.      * @param freezingDate freezing date
  392.      * @param frozenName name of the frozen frame
  393.      * @return a frozen version of the instance
  394.      */
  395.     public Frame getFrozenFrame(final Frame reference, final AbsoluteDate freezingDate,
  396.                                 final String frozenName) {
  397.         return new Frame(reference, reference.getTransformTo(this, freezingDate).freeze(),
  398.                          frozenName, reference.isPseudoInertial());
  399.     }

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

  403.     /** Holder for the root frame singleton. */
  404.     private static class LazyRootHolder {

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

  407.             /** Serializable UID. */
  408.             private static final long serialVersionUID = -2654403496396721543L;

  409.             /** Replace the instance with a data transfer object for serialization.
  410.              * <p>
  411.              * This intermediate class serializes nothing.
  412.              * </p>
  413.              * @return data transfer object that will be serialized
  414.              */
  415.             private Object writeReplace() {
  416.                 return new DataTransferObject();
  417.             }

  418.         };

  419.         /** Private constructor.
  420.          * <p>This class is a utility class, it should neither have a public
  421.          * nor a default constructor. This private constructor prevents
  422.          * the compiler from generating one automatically.</p>
  423.          */
  424.         private LazyRootHolder() {
  425.         }

  426.     }

  427.     /** Internal class used only for serialization. */
  428.     private static class DataTransferObject implements Serializable {

  429.         /** Serializable UID. */
  430.         private static final long serialVersionUID = 4067764035816491212L;

  431.         /** Simple constructor.
  432.          */
  433.         private DataTransferObject() {
  434.         }

  435.         /** Replace the deserialized data transfer object with a {@link FactoryManagedFrame}.
  436.          * @return replacement {@link FactoryManagedFrame}
  437.          */
  438.         private Object readResolve() {
  439.             return getRoot();
  440.         }

  441.     }

  442. }