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.Map;
  19. import java.util.concurrent.ConcurrentHashMap;
  20. import java.util.function.BiFunction;
  21. import java.util.function.Function;

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


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

  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.     /** Cache for transforms with peer frame.
  66.      * @since 13.0.3
  67.      */
  68.     private CachedTransformProvider peerCache;

  69.     /** Cache for transforms with peer frame.
  70.      * @since 13.0.3
  71.      */
  72.     private Map<Field<? extends CalculusFieldElement<?>>, FieldCachedTransformProvider<?>> peerFieldCache;

  73.     /** Private constructor used only for the root frame.
  74.      * @param name name of the frame
  75.      * @param pseudoInertial true if frame is considered pseudo-inertial
  76.      * (i.e. suitable for propagating orbit)
  77.      */
  78.     private Frame(final String name, final boolean pseudoInertial) {
  79.         parent              = null;
  80.         depth               = 0;
  81.         transformProvider   = new FixedTransformProvider(Transform.IDENTITY);
  82.         this.name           = name;
  83.         this.pseudoInertial = pseudoInertial;
  84.         this.peerCache      = null;
  85.         this.peerFieldCache = null;
  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 transform 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 Transform transform, final String name)
  97.         throws IllegalArgumentException {
  98.         this(parent, transform, name, false);
  99.     }

  100.     /** Build a non-inertial frame from its transform with respect to its parent.
  101.      * <p>calling this constructor is equivalent to call
  102.      * <code>{link {@link #Frame(Frame, Transform, String, boolean)
  103.      * Frame(parent, transform, name, false)}</code>.</p>
  104.      * @param parent parent frame (must be non-null)
  105.      * @param transformProvider provider for transform from parent frame to instance
  106.      * @param name name of the frame
  107.      * @exception IllegalArgumentException if the parent frame is null
  108.      */
  109.     public Frame(final Frame parent, final TransformProvider transformProvider, final String name)
  110.         throws IllegalArgumentException {
  111.         this(parent, transformProvider, name, false);
  112.     }

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

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

  151.         if (parent == null) {
  152.             throw new OrekitIllegalArgumentException(OrekitMessages.NULL_PARENT_FOR_FRAME, name);
  153.         }
  154.         this.parent            = parent;
  155.         this.depth             = parent.depth + 1;
  156.         this.transformProvider = transformProvider;
  157.         this.name              = name;
  158.         this.pseudoInertial    = pseudoInertial;

  159.     }

  160.     /** Get the name.
  161.      * @return the name
  162.      */
  163.     public String getName() {
  164.         return this.name;
  165.     }

  166.     /** Check if the frame is pseudo-inertial.
  167.      * <p>Pseudo-inertial frames are frames that do have a linear motion and
  168.      * either do not rotate or rotate at a very low rate resulting in
  169.      * neglectible inertial forces. This means they are suitable for orbit
  170.      * definition and propagation using Newtonian mechanics. Frames that are
  171.      * <em>not</em> pseudo-inertial are <em>not</em> suitable for orbit
  172.      * definition and propagation.</p>
  173.      * @return true if frame is pseudo-inertial
  174.      */
  175.     public boolean isPseudoInertial() {
  176.         return pseudoInertial;
  177.     }

  178.     /** New definition of the java.util toString() method.
  179.      * @return the name
  180.      */
  181.     public String toString() {
  182.         return this.name;
  183.     }

  184.     /** Get the parent frame.
  185.      * @return parent frame
  186.      */
  187.     public Frame getParent() {
  188.         return parent;
  189.     }

  190.     /** Get the depth of the frame.
  191.      * <p>
  192.      * The depth of a frame is the number of parents frame between
  193.      * it and the frames tree root. It is 0 for the root frame, and
  194.      * the depth of a frame is the depth of its parent frame plus one.
  195.      * </p>
  196.      * @return depth of the frame
  197.      */
  198.     public int getDepth() {
  199.         return depth;
  200.     }

  201.     /** Get the n<sup>th</sup> ancestor of the frame.
  202.      * @param n index of the ancestor (0 is the instance, 1 is its parent,
  203.      * 2 is the parent of its parent...)
  204.      * @return n<sup>th</sup> ancestor of the frame (must be between 0
  205.      * and the depth of the frame)
  206.      * @exception IllegalArgumentException if n is larger than the depth
  207.      * of the instance
  208.      */
  209.     public Frame getAncestor(final int n) throws IllegalArgumentException {

  210.         // safety check
  211.         if (n > depth) {
  212.             throw new OrekitIllegalArgumentException(OrekitMessages.FRAME_NO_NTH_ANCESTOR,
  213.                                                      name, depth, n);
  214.         }

  215.         // go upward to find ancestor
  216.         Frame current = this;
  217.         for (int i = 0; i < n; ++i) {
  218.             current = current.parent;
  219.         }

  220.         return current;

  221.     }

  222.     /** Associate this frame with a peer, caching transforms.
  223.      * <p>
  224.      * The cache is a LRU cache (Least Recently Used), so entries remain in
  225.      * the cache if they are used frequently, and only older entries
  226.      * that have not been accessed for a while will be expunged.
  227.      * </p>
  228.      * <p>
  229.      * If a peer was already associated with this frame, it will be overridden.
  230.      * </p>
  231.      * <p>
  232.      * Peering is unidirectional, i.e. if frameA is peered with frameB,
  233.      * then frameB may be peered with another frameC or no frame at all.
  234.      * This allows several frames to be peered with a pivot one (typically
  235.      * Earth frame and many topocentric frames all peered with one inertial frame).
  236.      * </p>
  237.      * @param peer peer frame
  238.      * @param cacheSize number of transforms kept in the date-based cache
  239.      * @since 13.0.3
  240.      */
  241.     public void setPeerCaching(final Frame peer, final int cacheSize) {

  242.         // caching for regular dates
  243.         peerCache = createCache(peer, cacheSize);

  244.         // caching for field dates
  245.         peerFieldCache = new ConcurrentHashMap<>();

  246.     }

  247.     /** Get the peer associated to this frame.
  248.      * @return peer associated with this frame, null if not peered at all
  249.      * @since 13.0.3
  250.      */
  251.     public Frame getPeer() {
  252.         return peerCache == null ? null : peerCache.getDestination();
  253.     }

  254.     /** Create cache.
  255.      * @param peer peer frame
  256.      * @param cacheSize number of transforms kept in the date-based cache
  257.      * @return built cache
  258.      * @since 13.0.3
  259.      */
  260.     private CachedTransformProvider createCache(final Frame peer, final int cacheSize) {
  261.         final Function<AbsoluteDate, Transform> fullGenerator =
  262.                 date -> getTransformTo(peer,
  263.                                        Transform.IDENTITY,
  264.                                        frame -> frame.getTransformProvider().getTransform(date),
  265.                                        (t1, t2) -> new Transform(date, t1, t2),
  266.                                        Transform::getInverse);
  267.         final Function<AbsoluteDate, KinematicTransform> kinematicGenerator =
  268.                 date -> getTransformTo(peer,
  269.                                        KinematicTransform.getIdentity(),
  270.                                        frame -> frame.getTransformProvider().getTransform(date),
  271.                                        (t1, t2) -> KinematicTransform.compose(date, t1, t2),
  272.                                        KinematicTransform::getInverse);
  273.         final Function<AbsoluteDate, StaticTransform> staticGenerator =
  274.                 date -> getTransformTo(peer,
  275.                                        StaticTransform.getIdentity(),
  276.                                        frame -> frame.getTransformProvider().getTransform(date),
  277.                                        (t1, t2) -> StaticTransform.compose(date, t1, t2),
  278.                                        StaticTransform::getInverse);
  279.         return new CachedTransformProvider(this, peer,
  280.                                            fullGenerator, kinematicGenerator, staticGenerator,
  281.                                            cacheSize);
  282.     }

  283.     /** Create field cache.
  284.      * @param <T> type of the field elements
  285.      * @param peer peer frame
  286.      * @param field field elements belong to
  287.      * @return built cache
  288.      * @since 13.0.3
  289.      */
  290.     private <T extends CalculusFieldElement<T>> FieldCachedTransformProvider<T>
  291.         createCache(final Frame peer, final Field<T> field) {
  292.         final Function<FieldAbsoluteDate<T>, FieldTransform<T>> fullGenerator =
  293.                 d -> getTransformTo(peer,
  294.                                     FieldTransform.getIdentity(field),
  295.                                     frame -> frame.getTransformProvider().getTransform(d),
  296.                                     (FieldTransform<T> t1, FieldTransform<T> t2) -> new FieldTransform<>(d, t1, t2),
  297.                                     FieldTransform::getInverse);
  298.         final Function<FieldAbsoluteDate<T>, FieldKinematicTransform<T>> kinematicGenerator =
  299.                 d -> getTransformTo(peer,
  300.                                     FieldKinematicTransform.getIdentity(field),
  301.                                     frame -> frame.getTransformProvider().getTransform(d),
  302.                                     (t1, t2) -> FieldKinematicTransform.compose(d, t1, t2),
  303.                                     FieldKinematicTransform::getInverse);
  304.         final Function<FieldAbsoluteDate<T>, FieldStaticTransform<T>> staticGenerator =
  305.                 d -> getTransformTo(peer,
  306.                                     FieldStaticTransform.getIdentity(field),
  307.                                     frame -> frame.getTransformProvider().getTransform(d),
  308.                                     (t1, t2) -> FieldStaticTransform.compose(d, t1, t2),
  309.                                     FieldStaticTransform::getInverse);
  310.         return new FieldCachedTransformProvider<>(this, peer,
  311.                                                   fullGenerator, kinematicGenerator, staticGenerator,
  312.                                                   peerCache.getCacheSize());
  313.     }

  314.     /** Get the transform from the instance to another frame.
  315.      * @param destination destination frame to which we want to transform vectors
  316.      * @param date the date (can be null if it is certain that no date dependent frame is used)
  317.      * @return transform from the instance to the destination frame
  318.      */
  319.     public Transform getTransformTo(final Frame destination, final AbsoluteDate date) {
  320.         if (peerCache != null && peerCache.getDestination() == destination) {
  321.             // this is our peer, we must cache the transform
  322.             return peerCache.getTransform(date);
  323.         } else {
  324.             // not our peer, just compute the transform and forget about it
  325.             return getTransformTo(
  326.                     destination,
  327.                     Transform.IDENTITY,
  328.                     frame -> frame.getTransformProvider().getTransform(date),
  329.                     (t1, t2) -> new Transform(date, t1, t2),
  330.                     Transform::getInverse);
  331.         }
  332.     }

  333.     /** Get the transform from the instance to another frame.
  334.      * @param destination destination frame to which we want to transform vectors
  335.      * @param date        the date (<em>must</em> be non-null, which is a more stringent condition
  336.      *                    than in {@link #getTransformTo(Frame, FieldAbsoluteDate)})
  337.      * @param <T> the type of the field elements
  338.      * @return transform from the instance to the destination frame
  339.      */
  340.     public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransformTo(final Frame destination,
  341.                                                                                 final FieldAbsoluteDate<T> date) {
  342.         if (peerCache != null && peerCache.getDestination() == destination) {
  343.             // this is our peer, we must cache the transform
  344.             @SuppressWarnings("unchedked")
  345.             final FieldCachedTransformProvider<T> cache =
  346.                     (FieldCachedTransformProvider<T>) peerFieldCache.computeIfAbsent(date.getField(),
  347.                                                                                      field -> createCache(destination, date.getField()));
  348.             return cache.getTransform(date);
  349.         } else {
  350.             // not our peer, just compute the transform and forget about it
  351.             if (date.hasZeroField()) {
  352.                 return new FieldTransform<>(date.getField(), getTransformTo(destination, date.toAbsoluteDate()));
  353.             }

  354.             return getTransformTo(destination,
  355.                                   FieldTransform.getIdentity(date.getField()),
  356.                                   frame -> frame.getTransformProvider().getTransform(date),
  357.                                   (t1, t2) -> new FieldTransform<>(date, t1, t2),
  358.                                   FieldTransform::getInverse);
  359.         }
  360.     }

  361.     /**
  362.      * Get the kinematic portion of the transform from the instance to another
  363.      * frame. The returned transform is kinematic in the sense that it includes
  364.      * translations and rotations, with rates, but cannot transform an acceleration vector.
  365.      *
  366.      * <p>This method is often more performant than {@link
  367.      * #getTransformTo(Frame, AbsoluteDate)} when accelerations are not needed.
  368.      *
  369.      * @param destination destination frame to which we want to transform
  370.      *                    vectors
  371.      * @param date        the date (can be null if it is sure than no date
  372.      *                    dependent frame is used)
  373.      * @return kinematic transform from the instance to the destination frame
  374.      * @since 12.1
  375.      */
  376.     public KinematicTransform getKinematicTransformTo(final Frame destination, final AbsoluteDate date) {
  377.         if (peerCache != null && peerCache.getDestination() == destination) {
  378.             // this is our peer, we must cache the transform
  379.             return peerCache.getKinematicTransform(date);
  380.         } else {
  381.             // not our peer, just compute the transform and forget about it
  382.             return getTransformTo(
  383.                     destination,
  384.                     KinematicTransform.getIdentity(),
  385.                     frame -> frame.getTransformProvider().getKinematicTransform(date),
  386.                     (t1, t2) -> KinematicTransform.compose(date, t1, t2),
  387.                     KinematicTransform::getInverse);
  388.         }
  389.     }

  390.     /**
  391.      * Get the static portion of the transform from the instance to another
  392.      * frame. The returned transform is static in the sense that it includes
  393.      * translations and rotations, but not rates.
  394.      *
  395.      * <p>This method is often more performant than {@link
  396.      * #getTransformTo(Frame, AbsoluteDate)} when rates are not needed.
  397.      *
  398.      * @param destination destination frame to which we want to transform
  399.      *                    vectors
  400.      * @param date        the date (can be null if it is sure than no date
  401.      *                    dependent frame is used)
  402.      * @return static transform from the instance to the destination frame
  403.      * @since 11.2
  404.      */
  405.     public StaticTransform getStaticTransformTo(final Frame destination,
  406.                                                 final AbsoluteDate date) {
  407.         if (peerCache != null && peerCache.getDestination() == destination) {
  408.             // this is our peer, we must cache the transform
  409.             return peerCache.getStaticTransform(date);
  410.         }
  411.         else {
  412.             // not our peer, just compute the transform and forget about it
  413.             return getTransformTo(
  414.                     destination,
  415.                     StaticTransform.getIdentity(),
  416.                     frame -> frame.getTransformProvider().getStaticTransform(date),
  417.                     (t1, t2) -> StaticTransform.compose(date, t1, t2),
  418.                     StaticTransform::getInverse);
  419.         }
  420.     }

  421.     /**
  422.      * Get the static portion of the transform from the instance to another
  423.      * frame. The returned transform is static in the sense that it includes
  424.      * translations and rotations, but not rates.
  425.      *
  426.      * <p>This method is often more performant than {@link
  427.      * #getTransformTo(Frame, FieldAbsoluteDate)} when rates are not needed.
  428.      *
  429.      * <p>A first check is made on the FieldAbsoluteDate because "fielded" transforms have low-performance.<br>
  430.      * The date field is checked with {@link FieldElement#isZero()}.<br>
  431.      * If true, the un-fielded version of the transform computation is used.
  432.      *
  433.      * @param <T>         type of the elements
  434.      * @param destination destination frame to which we want to transform
  435.      *                    vectors
  436.      * @param date        the date (<em>must</em> be non-null, which is a more stringent condition
  437.      *                    than in {@link #getStaticTransformTo(Frame, AbsoluteDate)})
  438.      * @return static transform from the instance to the destination frame
  439.      * @since 12.0
  440.      */
  441.     public <T extends CalculusFieldElement<T>> FieldStaticTransform<T> getStaticTransformTo(final Frame destination,
  442.                                                 final FieldAbsoluteDate<T> date) {
  443.         if (peerCache != null && peerCache.getDestination() == destination) {
  444.             // this is our peer, we must cache the transform
  445.             @SuppressWarnings("unchedked")
  446.             final FieldCachedTransformProvider<T> cache =
  447.                     (FieldCachedTransformProvider<T>) peerFieldCache.computeIfAbsent(date.getField(),
  448.                                                                                      field -> createCache(destination, date.getField()));
  449.             return cache.getStaticTransform(date);
  450.         } else {
  451.             // not our peer, just compute the transform and forget about it
  452.             if (date.hasZeroField()) {
  453.                 // If date field is Zero, then use the un-fielded version for performances
  454.                 return FieldStaticTransform.of(date, getStaticTransformTo(destination, date.toAbsoluteDate()));

  455.             }
  456.             else {
  457.                 // Use classic fielded function
  458.                 return getTransformTo(destination,
  459.                                       FieldStaticTransform.getIdentity(date.getField()),
  460.                                       frame -> frame.getTransformProvider().getStaticTransform(date),
  461.                                       (t1, t2) -> FieldStaticTransform.compose(date, t1, t2),
  462.                                       FieldStaticTransform::getInverse);
  463.             }
  464.         }
  465.     }

  466.     /**
  467.      * Get the kinematic portion of the transform from the instance to another
  468.      * frame. The returned transform is kinematic in the sense that it includes
  469.      * translations and rotations, with rates, but cannot transform an acceleration vector.
  470.      *
  471.      * <p>This method is often more performant than {@link
  472.      * #getTransformTo(Frame, AbsoluteDate)} when accelerations are not needed.
  473.      * @param <T>          Type of transform returned.
  474.      * @param destination destination frame to which we want to transform
  475.      *                    vectors
  476.      * @param date        the date (<em>must</em> be non-null, which is a more stringent condition
  477.      *      *                    than in {@link #getKinematicTransformTo(Frame, AbsoluteDate)})
  478.      * @return kinematic transform from the instance to the destination frame
  479.      * @since 12.1
  480.      */
  481.     public <T extends CalculusFieldElement<T>> FieldKinematicTransform<T> getKinematicTransformTo(final Frame destination,
  482.                                                                                                   final FieldAbsoluteDate<T> date) {
  483.         if (peerCache != null && peerCache.getDestination() == destination) {
  484.             // this is our peer, we must cache the transform
  485.             @SuppressWarnings("unchedked")
  486.             final FieldCachedTransformProvider<T> cache =
  487.                     (FieldCachedTransformProvider<T>) peerFieldCache.computeIfAbsent(date.getField(),
  488.                                                                                      field -> createCache(destination, date.getField()));
  489.             return cache.getKinematicTransform(date);
  490.         }
  491.         else {
  492.             // not our peer, just compute the transform and forget about it
  493.             if (date.hasZeroField()) {
  494.                 // If date field is Zero, then use the un-fielded version for performances
  495.                 final KinematicTransform kinematicTransform = getKinematicTransformTo(destination, date.toAbsoluteDate());
  496.                 return FieldKinematicTransform.of(date.getField(), kinematicTransform);

  497.             }
  498.             else {
  499.                 // Use classic fielded function
  500.                 return getTransformTo(destination,
  501.                                       FieldKinematicTransform.getIdentity(date.getField()),
  502.                                       frame -> frame.getTransformProvider().getKinematicTransform(date),
  503.                                       (t1, t2) -> FieldKinematicTransform.compose(date, t1, t2),
  504.                                       FieldKinematicTransform::getInverse);
  505.             }
  506.         }
  507.     }

  508.     /**
  509.      * Generic get transform method that builds the transform from {@code this}
  510.      * to {@code destination}.
  511.      *
  512.      * @param destination  destination frame to which we want to transform
  513.      *                     vectors
  514.      * @param identity     transform of the given type.
  515.      * @param getTransform method to get a transform from a frame.
  516.      * @param compose      method to combine two transforms.
  517.      * @param inverse      method to invert a transform.
  518.      * @param <T>          Type of transform returned.
  519.      * @return composite transform.
  520.      */
  521.     private <T> T getTransformTo(final Frame destination,
  522.                                  final T identity,
  523.                                  final Function<Frame, T> getTransform,
  524.                                  final BiFunction<T, T, T> compose,
  525.                                  final Function<T, T> inverse) {

  526.         if (this == destination) {
  527.             // shortcut for special case that may be frequent
  528.             return identity;
  529.         }

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

  532.         // transform from common to instance
  533.         T commonToInstance = identity;
  534.         for (Frame frame = this; frame != common; frame = frame.parent) {
  535.             commonToInstance = compose.apply(getTransform.apply(frame), commonToInstance);
  536.         }

  537.         // transform from destination up to common
  538.         T commonToDestination = identity;
  539.         for (Frame frame = destination; frame != common; frame = frame.parent) {
  540.             commonToDestination = compose.apply(getTransform.apply(frame), commonToDestination);
  541.         }

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

  544.     }

  545.     /** Get the provider for transform from parent frame to instance.
  546.      * @return provider for transform from parent frame to instance
  547.      */
  548.     public TransformProvider getTransformProvider() {
  549.         return transformProvider;
  550.     }

  551.     /** Find the deepest common ancestor of two frames in the frames tree.
  552.      * @param from origin frame
  553.      * @param to destination frame
  554.      * @return an ancestor frame of both <code>from</code> and <code>to</code>
  555.      */
  556.     private static Frame findCommon(final Frame from, final Frame to) {

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

  560.         // go upward until we find a match
  561.         while (currentF != currentT) {
  562.             currentF = currentF.parent;
  563.             currentT = currentT.parent;
  564.         }

  565.         return currentF;

  566.     }

  567.     /** Determine if a Frame is a child of another one.
  568.      * @param potentialAncestor supposed ancestor frame
  569.      * @return true if the potentialAncestor belongs to the
  570.      * path from instance to the root frame, excluding itself
  571.      */
  572.     public boolean isChildOf(final Frame potentialAncestor) {
  573.         if (depth <= potentialAncestor.depth) {
  574.             return false;
  575.         }
  576.         return getAncestor(depth - potentialAncestor.depth) == potentialAncestor;
  577.     }

  578.     /** Get the unique root frame.
  579.      * @return the unique instance of the root frame
  580.      */
  581.     public static Frame getRoot() {
  582.         return LazyRootHolder.INSTANCE;
  583.     }

  584.     /** Get a new version of the instance, frozen with respect to a reference frame.
  585.      * <p>
  586.      * Freezing a frame consist in computing its position and orientation with respect
  587.      * to another frame at some freezing date and fixing them so they do not depend
  588.      * on time anymore. This means the frozen frame is fixed with respect to the
  589.      * reference frame.
  590.      * </p>
  591.      * <p>
  592.      * One typical use of this method is to compute an inertial launch reference frame
  593.      * by freezing a {@link TopocentricFrame topocentric frame} at launch date
  594.      * with respect to an inertial frame. Another use is to freeze an equinox-related
  595.      * celestial frame at a reference epoch date.
  596.      * </p>
  597.      * <p>
  598.      * Only the frame returned by this method is frozen, the instance by itself
  599.      * is not affected by calling this method and still moves freely.
  600.      * </p>
  601.      * @param reference frame with respect to which the instance will be frozen
  602.      * @param freezingDate freezing date
  603.      * @param frozenName name of the frozen frame
  604.      * @return a frozen version of the instance
  605.      */
  606.     public Frame getFrozenFrame(final Frame reference, final AbsoluteDate freezingDate,
  607.                                 final String frozenName) {
  608.         return new Frame(reference, reference.getTransformTo(this, freezingDate).freeze(),
  609.                          frozenName, reference.isPseudoInertial());
  610.     }

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

  614.     /** Holder for the root frame singleton. */
  615.     private static class LazyRootHolder {

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

  618.         /** Private constructor.
  619.          * <p>This class is a utility class, it should neither have a public
  620.          * nor a default constructor. This private constructor prevents
  621.          * the compiler from generating one automatically.</p>
  622.          */
  623.         private LazyRootHolder() {
  624.         }

  625.     }

  626. }