Frame.java

  1. /* Copyright 2002-2025 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.util.function.BiFunction;
  19. import java.util.function.Function;

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


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

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

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

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

  58.     /** Instance name. */
  59.     private final String name;

  60.     /** Indicator for pseudo-inertial frames. */
  61.     private final boolean pseudoInertial;

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

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

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

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

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

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

  146.     }

  147.     /** Get the name.
  148.      * @return the name
  149.      */
  150.     public String getName() {
  151.         return this.name;
  152.     }

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

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

  171.     /** Get the parent frame.
  172.      * @return parent frame
  173.      */
  174.     public Frame getParent() {
  175.         return parent;
  176.     }

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

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

  197.         // safety check
  198.         if (n > depth) {
  199.             throw new OrekitIllegalArgumentException(OrekitMessages.FRAME_NO_NTH_ANCESTOR,
  200.                                                      name, depth, n);
  201.         }

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

  207.         return current;

  208.     }

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

  222.     /** Get the transform from the instance to another frame.
  223.      * @param destination destination frame to which we want to transform vectors
  224.      * @param date the date (<em>must</em> be non-null, which is a more stringent condition
  225.      *      *                than in {@link #getTransformTo(Frame, FieldAbsoluteDate)})
  226.      * @param <T> the type of the field elements
  227.      * @return transform from the instance to the destination frame
  228.      */
  229.     public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransformTo(final Frame destination,
  230.                                                                                 final FieldAbsoluteDate<T> date) {
  231.         if (date.hasZeroField()) {
  232.             return new FieldTransform<>(date.getField(), getTransformTo(destination, date.toAbsoluteDate()));
  233.         }
  234.         return getTransformTo(destination,
  235.                               FieldTransform.getIdentity(date.getField()),
  236.                               frame -> frame.getTransformProvider().getTransform(date),
  237.                               (t1, t2) -> new FieldTransform<>(date, t1, t2),
  238.                               FieldTransform::getInverse);
  239.     }

  240.     /**
  241.      * Get the kinematic portion of the transform from the instance to another
  242.      * frame. The returned transform is kinematic in the sense that it includes
  243.      * translations and rotations, with rates, but cannot transform an acceleration vector.
  244.      *
  245.      * <p>This method is often more performant than {@link
  246.      * #getTransformTo(Frame, AbsoluteDate)} when accelerations 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 kinematic transform from the instance to the destination frame
  253.      * @since 12.1
  254.      */
  255.     public KinematicTransform getKinematicTransformTo(final Frame destination, final AbsoluteDate date) {
  256.         return getTransformTo(
  257.             destination,
  258.             KinematicTransform.getIdentity(),
  259.             frame -> frame.getTransformProvider().getKinematicTransform(date),
  260.             (t1, t2) -> KinematicTransform.compose(date, t1, t2),
  261.             KinematicTransform::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, AbsoluteDate)} when rates are not needed.
  270.      *
  271.      * @param destination destination frame to which we want to transform
  272.      *                    vectors
  273.      * @param date        the date (can be null if it is sure than no date
  274.      *                    dependent frame is used)
  275.      * @return static transform from the instance to the destination frame
  276.      * @since 11.2
  277.      */
  278.     public StaticTransform getStaticTransformTo(final Frame destination,
  279.                                                 final AbsoluteDate date) {
  280.         return getTransformTo(
  281.                 destination,
  282.                 StaticTransform.getIdentity(),
  283.                 frame -> frame.getTransformProvider().getStaticTransform(date),
  284.                 (t1, t2) -> StaticTransform.compose(date, t1, t2),
  285.                 StaticTransform::getInverse);
  286.     }

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

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

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

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

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

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

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

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

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

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

  387.     }

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

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

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

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

  408.         return currentF;

  409.     }

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

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

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

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

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

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

  461.         /** Private constructor.
  462.          * <p>This class is a utility class, it should neither have a public
  463.          * nor a default constructor. This private constructor prevents
  464.          * the compiler from generating one automatically.</p>
  465.          */
  466.         private LazyRootHolder() {
  467.         }

  468.     }

  469. }