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 kinematic portion of the transform from the instance to another
  241.      * frame. The returned transform is kinematic in the sense that it includes
  242.      * translations and rotations, with rates, but cannot transform an acceleration vector.
  243.      *
  244.      * <p>This method is often more performant than {@link
  245.      * #getTransformTo(Frame, AbsoluteDate)} when accelerations 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 kinematic transform from the instance to the destination frame
  252.      * @since 12.1
  253.      */
  254.     public KinematicTransform getKinematicTransformTo(final Frame destination, final AbsoluteDate date) {
  255.         return getTransformTo(
  256.             destination,
  257.             KinematicTransform.getIdentity(),
  258.             frame -> frame.getTransformProvider().getKinematicTransform(date),
  259.             (t1, t2) -> KinematicTransform.compose(date, t1, t2),
  260.             KinematicTransform::getInverse);
  261.     }

  262.     /**
  263.      * Get the static portion of the transform from the instance to another
  264.      * frame. The returned transform is static in the sense that it includes
  265.      * translations and rotations, but not rates.
  266.      *
  267.      * <p>This method is often more performant than {@link
  268.      * #getTransformTo(Frame, AbsoluteDate)} when rates are not needed.
  269.      *
  270.      * @param destination destination frame to which we want to transform
  271.      *                    vectors
  272.      * @param date        the date (can be null if it is sure than no date
  273.      *                    dependent frame is used)
  274.      * @return static transform from the instance to the destination frame
  275.      * @since 11.2
  276.      */
  277.     public StaticTransform getStaticTransformTo(final Frame destination,
  278.                                                 final AbsoluteDate date) {
  279.         return getTransformTo(
  280.                 destination,
  281.                 StaticTransform.getIdentity(),
  282.                 frame -> frame.getTransformProvider().getStaticTransform(date),
  283.                 (t1, t2) -> StaticTransform.compose(date, t1, t2),
  284.                 StaticTransform::getInverse);
  285.     }

  286.     /**
  287.      * Get the static portion of the transform from the instance to another
  288.      * frame. The returned transform is static in the sense that it includes
  289.      * translations and rotations, but not rates.
  290.      *
  291.      * <p>This method is often more performant than {@link
  292.      * #getTransformTo(Frame, FieldAbsoluteDate)} when rates are not needed.
  293.      *
  294.      * <p>A first check is made on the FieldAbsoluteDate because "fielded" transforms have low-performance.<br>
  295.      * The date field is checked with {@link FieldElement#isZero()}.<br>
  296.      * If true, the un-fielded version of the transform computation is used.
  297.      *
  298.      * @param <T>         type of the elements
  299.      * @param destination destination frame to which we want to transform
  300.      *                    vectors
  301.      * @param date        the date (<em>must</em> be non-null, which is a more stringent condition
  302.      *                    than in {@link #getStaticTransformTo(Frame, AbsoluteDate)})
  303.      * @return static transform from the instance to the destination frame
  304.      * @since 12.0
  305.      */
  306.     public <T extends CalculusFieldElement<T>> FieldStaticTransform<T> getStaticTransformTo(final Frame destination,
  307.                                                 final FieldAbsoluteDate<T> date) {
  308.         if (date.hasZeroField()) {
  309.             // If date field is Zero, then use the un-fielded version for performances
  310.             return FieldStaticTransform.of(date, getStaticTransformTo(destination, date.toAbsoluteDate()));

  311.         } else {
  312.             // Use classic fielded function
  313.             return getTransformTo(destination,
  314.                                   FieldStaticTransform.getIdentity(date.getField()),
  315.                                   frame -> frame.getTransformProvider().getStaticTransform(date),
  316.                                   (t1, t2) -> FieldStaticTransform.compose(date, t1, t2),
  317.                                   FieldStaticTransform::getInverse);
  318.         }
  319.     }

  320.     /**
  321.      * Get the kinematic portion of the transform from the instance to another
  322.      * frame. The returned transform is kinematic in the sense that it includes
  323.      * translations and rotations, with rates, but cannot transform an acceleration vector.
  324.      *
  325.      * <p>This method is often more performant than {@link
  326.      * #getTransformTo(Frame, AbsoluteDate)} when accelerations are not needed.
  327.      * @param <T>          Type of transform returned.
  328.      * @param destination destination frame to which we want to transform
  329.      *                    vectors
  330.      * @param date        the date (<em>must</em> be non-null, which is a more stringent condition
  331.      *      *                    than in {@link #getKinematicTransformTo(Frame, AbsoluteDate)})
  332.      * @return kinematic transform from the instance to the destination frame
  333.      * @since 12.1
  334.      */
  335.     public <T extends CalculusFieldElement<T>> FieldKinematicTransform<T> getKinematicTransformTo(final Frame destination,
  336.                                                                                                   final FieldAbsoluteDate<T> date) {
  337.         if (date.hasZeroField()) {
  338.             // If date field is Zero, then use the un-fielded version for performances
  339.             final KinematicTransform kinematicTransform = getKinematicTransformTo(destination, date.toAbsoluteDate());
  340.             return FieldKinematicTransform.of(date.getField(), kinematicTransform);

  341.         } else {
  342.             // Use classic fielded function
  343.             return getTransformTo(destination,
  344.                     FieldKinematicTransform.getIdentity(date.getField()),
  345.                     frame -> frame.getTransformProvider().getKinematicTransform(date),
  346.                     (t1, t2) -> FieldKinematicTransform.compose(date, t1, t2),
  347.                     FieldKinematicTransform::getInverse);
  348.         }
  349.     }

  350.     /**
  351.      * Generic get transform method that builds the transform from {@code this}
  352.      * to {@code destination}.
  353.      *
  354.      * @param destination  destination frame to which we want to transform
  355.      *                     vectors
  356.      * @param identity     transform of the given type.
  357.      * @param getTransform method to get a transform from a frame.
  358.      * @param compose      method to combine two transforms.
  359.      * @param inverse      method to invert a transform.
  360.      * @param <T>          Type of transform returned.
  361.      * @return composite transform.
  362.      */
  363.     private <T> T getTransformTo(final Frame destination,
  364.                                  final T identity,
  365.                                  final Function<Frame, T> getTransform,
  366.                                  final BiFunction<T, T, T> compose,
  367.                                  final Function<T, T> inverse) {

  368.         if (this == destination) {
  369.             // shortcut for special case that may be frequent
  370.             return identity;
  371.         }

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

  374.         // transform from common to instance
  375.         T commonToInstance = identity;
  376.         for (Frame frame = this; frame != common; frame = frame.parent) {
  377.             commonToInstance = compose.apply(getTransform.apply(frame), commonToInstance);
  378.         }

  379.         // transform from destination up to common
  380.         T commonToDestination = identity;
  381.         for (Frame frame = destination; frame != common; frame = frame.parent) {
  382.             commonToDestination = compose.apply(getTransform.apply(frame), commonToDestination);
  383.         }

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

  386.     }

  387.     /** Get the provider for transform from parent frame to instance.
  388.      * @return provider for transform from parent frame to instance
  389.      */
  390.     public TransformProvider getTransformProvider() {
  391.         return transformProvider;
  392.     }

  393.     /** Find the deepest common ancestor of two frames in the frames tree.
  394.      * @param from origin frame
  395.      * @param to destination frame
  396.      * @return an ancestor frame of both <code>from</code> and <code>to</code>
  397.      */
  398.     private static Frame findCommon(final Frame from, final Frame to) {

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

  402.         // go upward until we find a match
  403.         while (currentF != currentT) {
  404.             currentF = currentF.parent;
  405.             currentT = currentT.parent;
  406.         }

  407.         return currentF;

  408.     }

  409.     /** Determine if a Frame is a child of another one.
  410.      * @param potentialAncestor supposed ancestor frame
  411.      * @return true if the potentialAncestor belongs to the
  412.      * path from instance to the root frame, excluding itself
  413.      */
  414.     public boolean isChildOf(final Frame potentialAncestor) {
  415.         if (depth <= potentialAncestor.depth) {
  416.             return false;
  417.         }
  418.         return getAncestor(depth - potentialAncestor.depth) == potentialAncestor;
  419.     }

  420.     /** Get the unique root frame.
  421.      * @return the unique instance of the root frame
  422.      */
  423.     public static Frame getRoot() {
  424.         return LazyRootHolder.INSTANCE;
  425.     }

  426.     /** Get a new version of the instance, frozen with respect to a reference frame.
  427.      * <p>
  428.      * Freezing a frame consist in computing its position and orientation with respect
  429.      * to another frame at some freezing date and fixing them so they do not depend
  430.      * on time anymore. This means the frozen frame is fixed with respect to the
  431.      * reference frame.
  432.      * </p>
  433.      * <p>
  434.      * One typical use of this method is to compute an inertial launch reference frame
  435.      * by freezing a {@link TopocentricFrame topocentric frame} at launch date
  436.      * with respect to an inertial frame. Another use is to freeze an equinox-related
  437.      * celestial frame at a reference epoch date.
  438.      * </p>
  439.      * <p>
  440.      * Only the frame returned by this method is frozen, the instance by itself
  441.      * is not affected by calling this method and still moves freely.
  442.      * </p>
  443.      * @param reference frame with respect to which the instance will be frozen
  444.      * @param freezingDate freezing date
  445.      * @param frozenName name of the frozen frame
  446.      * @return a frozen version of the instance
  447.      */
  448.     public Frame getFrozenFrame(final Frame reference, final AbsoluteDate freezingDate,
  449.                                 final String frozenName) {
  450.         return new Frame(reference, reference.getTransformTo(this, freezingDate).freeze(),
  451.                          frozenName, reference.isPseudoInertial());
  452.     }

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

  456.     /** Holder for the root frame singleton. */
  457.     private static class LazyRootHolder {

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

  460.             /** Serializable UID. */
  461.             private static final long serialVersionUID = -2654403496396721543L;

  462.             /** Replace the instance with a data transfer object for serialization.
  463.              * <p>
  464.              * This intermediate class serializes nothing.
  465.              * </p>
  466.              * @return data transfer object that will be serialized
  467.              */
  468.             private Object writeReplace() {
  469.                 return new DataTransferObject();
  470.             }

  471.         };

  472.         /** Private constructor.
  473.          * <p>This class is a utility class, it should neither have a public
  474.          * nor a default constructor. This private constructor prevents
  475.          * the compiler from generating one automatically.</p>
  476.          */
  477.         private LazyRootHolder() {
  478.         }

  479.     }

  480.     /** Internal class used only for serialization. */
  481.     private static class DataTransferObject implements Serializable {

  482.         /** Serializable UID. */
  483.         private static final long serialVersionUID = 4067764035816491212L;

  484.         /** Simple constructor.
  485.          */
  486.         private DataTransferObject() {
  487.         }

  488.         /** Replace the deserialized data transfer object with a {@link FactoryManagedFrame}.
  489.          * @return replacement {@link FactoryManagedFrame}
  490.          */
  491.         private Object readResolve() {
  492.             return getRoot();
  493.         }

  494.     }

  495. }