Frame.java

  1. /* Copyright 2002-2020 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 org.hipparchus.RealFieldElement;
  20. import org.orekit.errors.OrekitIllegalArgumentException;
  21. import org.orekit.errors.OrekitMessages;
  22. import org.orekit.time.AbsoluteDate;
  23. import org.orekit.time.FieldAbsoluteDate;


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

  50.     /** Serializable UID. */
  51.     private static final long serialVersionUID = -6981146543760234087L;

  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.         if (this == destination) {
  216.             // shortcut for special case that may be frequent
  217.             return Transform.IDENTITY;
  218.         }

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

  221.         // transform from common to instance
  222.         Transform commonToInstance = Transform.IDENTITY;
  223.         for (Frame frame = this; frame != common; frame = frame.parent) {
  224.             commonToInstance =
  225.                 new Transform(date, frame.transformProvider.getTransform(date), commonToInstance);
  226.         }

  227.         // transform from destination up to common
  228.         Transform commonToDestination = Transform.IDENTITY;
  229.         for (Frame frame = destination; frame != common; frame = frame.parent) {
  230.             commonToDestination =
  231.                 new Transform(date, frame.transformProvider.getTransform(date), commonToDestination);
  232.         }

  233.         // transform from instance to destination via common
  234.         return new Transform(date, commonToInstance.getInverse(), commonToDestination);

  235.     }

  236.     /** Get the transform from the instance to another frame.
  237.      * @param destination destination frame to which we want to transform vectors
  238.      * @param date the date (can be null if it is sure than no date dependent frame is used)
  239.      * @param <T> the type of the field elements
  240.      * @return transform from the instance to the destination frame
  241.      */
  242.     public <T extends RealFieldElement<T>> FieldTransform<T> getTransformTo(final Frame destination, final FieldAbsoluteDate<T> date) {

  243.         if (this == destination) {
  244.             // shortcut for special case that may be frequent
  245.             return FieldTransform.getIdentity(date.getField());
  246.         }

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

  249.         // transform from common to instance
  250.         FieldTransform<T> commonToInstance = FieldTransform.getIdentity(date.getField());
  251.         for (Frame frame = this; frame != common; frame = frame.parent) {
  252.             commonToInstance =
  253.                 new FieldTransform<>(date, frame.transformProvider.getTransform(date), commonToInstance);
  254.         }

  255.         // transform from destination up to common
  256.         FieldTransform<T> commonToDestination = FieldTransform.getIdentity(date.getField());
  257.         for (Frame frame = destination; frame != common; frame = frame.parent) {
  258.             commonToDestination =
  259.                 new FieldTransform<>(date, frame.transformProvider.getTransform(date), commonToDestination);
  260.         }

  261.         // transform from instance to destination via common
  262.         return new FieldTransform<>(date, commonToInstance.getInverse(), commonToDestination);

  263.     }

  264.     /** Get the provider for transform from parent frame to instance.
  265.      * @return provider for transform from parent frame to instance
  266.      */
  267.     public TransformProvider getTransformProvider() {
  268.         return transformProvider;
  269.     }

  270.     /** Find the deepest common ancestor of two frames in the frames tree.
  271.      * @param from origin frame
  272.      * @param to destination frame
  273.      * @return an ancestor frame of both <code>from</code> and <code>to</code>
  274.      */
  275.     private static Frame findCommon(final Frame from, final Frame to) {

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

  279.         // go upward until we find a match
  280.         while (currentF != currentT) {
  281.             currentF = currentF.parent;
  282.             currentT = currentT.parent;
  283.         }

  284.         return currentF;

  285.     }

  286.     /** Determine if a Frame is a child of another one.
  287.      * @param potentialAncestor supposed ancestor frame
  288.      * @return true if the potentialAncestor belongs to the
  289.      * path from instance to the root frame, excluding itself
  290.      */
  291.     public boolean isChildOf(final Frame potentialAncestor) {
  292.         if (depth <= potentialAncestor.depth) {
  293.             return false;
  294.         }
  295.         return getAncestor(depth - potentialAncestor.depth) == potentialAncestor;
  296.     }

  297.     /** Get the unique root frame.
  298.      * @return the unique instance of the root frame
  299.      */
  300.     public static Frame getRoot() {
  301.         return LazyRootHolder.INSTANCE;
  302.     }

  303.     /** Get a new version of the instance, frozen with respect to a reference frame.
  304.      * <p>
  305.      * Freezing a frame consist in computing its position and orientation with respect
  306.      * to another frame at some freezing date and fixing them so they do not depend
  307.      * on time anymore. This means the frozen frame is fixed with respect to the
  308.      * reference frame.
  309.      * </p>
  310.      * <p>
  311.      * One typical use of this method is to compute an inertial launch reference frame
  312.      * by freezing a {@link TopocentricFrame topocentric frame} at launch date
  313.      * with respect to an inertial frame. Another use is to freeze an equinox-related
  314.      * celestial frame at a reference epoch date.
  315.      * </p>
  316.      * <p>
  317.      * Only the frame returned by this method is frozen, the instance by itself
  318.      * is not affected by calling this method and still moves freely.
  319.      * </p>
  320.      * @param reference frame with respect to which the instance will be frozen
  321.      * @param freezingDate freezing date
  322.      * @param frozenName name of the frozen frame
  323.      * @return a frozen version of the instance
  324.      */
  325.     public Frame getFrozenFrame(final Frame reference, final AbsoluteDate freezingDate,
  326.                                 final String frozenName) {
  327.         return new Frame(reference, reference.getTransformTo(this, freezingDate).freeze(),
  328.                          frozenName, reference.isPseudoInertial());
  329.     }

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

  333.     /** Holder for the root frame singleton. */
  334.     private static class LazyRootHolder {

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

  337.             /** Serializable UID. */
  338.             private static final long serialVersionUID = -2654403496396721543L;

  339.             /** Replace the instance with a data transfer object for serialization.
  340.              * <p>
  341.              * This intermediate class serializes nothing.
  342.              * </p>
  343.              * @return data transfer object that will be serialized
  344.              */
  345.             private Object writeReplace() {
  346.                 return new DataTransferObject();
  347.             }

  348.         };

  349.         /** Private constructor.
  350.          * <p>This class is a utility class, it should neither have a public
  351.          * nor a default constructor. This private constructor prevents
  352.          * the compiler from generating one automatically.</p>
  353.          */
  354.         private LazyRootHolder() {
  355.         }

  356.     }

  357.     /** Internal class used only for serialization. */
  358.     private static class DataTransferObject implements Serializable {

  359.         /** Serializable UID. */
  360.         private static final long serialVersionUID = 4067764035816491212L;

  361.         /** Simple constructor.
  362.          */
  363.         private DataTransferObject() {
  364.         }

  365.         /** Replace the deserialized data transfer object with a {@link FactoryManagedFrame}.
  366.          * @return replacement {@link FactoryManagedFrame}
  367.          */
  368.         private Object readResolve() {
  369.             return getRoot();
  370.         }

  371.     }

  372. }