Frame.java

  1. /* Copyright 2002-2018 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.OrekitException;
  21. import org.orekit.errors.OrekitIllegalArgumentException;
  22. import org.orekit.errors.OrekitMessages;
  23. import org.orekit.time.AbsoluteDate;
  24. import org.orekit.time.FieldAbsoluteDate;


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

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

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

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

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

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

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

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

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

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

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

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

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

  147.     }

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

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

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

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

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

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

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

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

  208.         return current;

  209.     }

  210.     /** Get the transform from the instance to another frame.
  211.      * @param destination destination frame to which we want to transform vectors
  212.      * @param date the date (can be null if it is sure than no date dependent frame is used)
  213.      * @return transform from the instance to the destination frame
  214.      * @exception OrekitException if some frame specific error occurs
  215.      */
  216.     public Transform getTransformTo(final Frame destination, final AbsoluteDate date)
  217.         throws OrekitException {

  218.         if (this == destination) {
  219.             // shortcut for special case that may be frequent
  220.             return Transform.IDENTITY;
  221.         }

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

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

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

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

  238.     }

  239.     /** Get the transform from the instance to another frame.
  240.      * @param destination destination frame to which we want to transform vectors
  241.      * @param date the date (can be null if it is sure than no date dependent frame is used)
  242.      * @param <T> the type of the field elements
  243.      * @return transform from the instance to the destination frame
  244.      * @exception OrekitException if some frame specific error occurs
  245.      */
  246.     public <T extends RealFieldElement<T>> FieldTransform<T> getTransformTo(final Frame destination, final FieldAbsoluteDate<T> date)
  247.         throws OrekitException {

  248.         if (this == destination) {
  249.             // shortcut for special case that may be frequent
  250.             return FieldTransform.getIdentity(date.getField());
  251.         }

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

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

  260.         // transform from destination up to common
  261.         FieldTransform<T> commonToDestination = FieldTransform.getIdentity(date.getField());
  262.         for (Frame frame = destination; frame != common; frame = frame.parent) {
  263.             commonToDestination =
  264.                 new FieldTransform<>(date, frame.transformProvider.getTransform(date), commonToDestination);
  265.         }

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

  268.     }

  269.     /** Get the provider for transform from parent frame to instance.
  270.      * @return provider for transform from parent frame to instance
  271.      */
  272.     public TransformProvider getTransformProvider() {
  273.         return transformProvider;
  274.     }

  275.     /** Find the deepest common ancestor of two frames in the frames tree.
  276.      * @param from origin frame
  277.      * @param to destination frame
  278.      * @return an ancestor frame of both <code>from</code> and <code>to</code>
  279.      */
  280.     private static Frame findCommon(final Frame from, final Frame to) {

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

  284.         // go upward until we find a match
  285.         while (currentF != currentT) {
  286.             currentF = currentF.parent;
  287.             currentT = currentT.parent;
  288.         }

  289.         return currentF;

  290.     }

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

  302.     /** Get the unique root frame.
  303.      * @return the unique instance of the root frame
  304.      */
  305.     protected static Frame getRoot() {
  306.         return LazyRootHolder.INSTANCE;
  307.     }

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

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

  340.     /** Holder for the root frame singleton. */
  341.     private static class LazyRootHolder {

  342.         /** Unique instance. */
  343.         private static final Frame INSTANCE = new Frame("GCRF", true) {

  344.             /** Serializable UID. */
  345.             private static final long serialVersionUID = -2654403496396721543L;

  346.             /** Replace the instance with a data transfer object for serialization.
  347.              * <p>
  348.              * This intermediate class serializes nothing.
  349.              * </p>
  350.              * @return data transfer object that will be serialized
  351.              */
  352.             private Object writeReplace() {
  353.                 return new DataTransferObject();
  354.             }

  355.         };

  356.         /** Private constructor.
  357.          * <p>This class is a utility class, it should neither have a public
  358.          * nor a default constructor. This private constructor prevents
  359.          * the compiler from generating one automatically.</p>
  360.          */
  361.         private LazyRootHolder() {
  362.         }

  363.     }

  364.     /** Internal class used only for serialization. */
  365.     private static class DataTransferObject implements Serializable {

  366.         /** Serializable UID. */
  367.         private static final long serialVersionUID = 4067764035816491212L;

  368.         /** Simple constructor.
  369.          */
  370.         private DataTransferObject() {
  371.         }

  372.         /** Replace the deserialized data transfer object with a {@link FactoryManagedFrame}.
  373.          * @return replacement {@link FactoryManagedFrame}
  374.          */
  375.         private Object readResolve() {
  376.             return getRoot();
  377.         }

  378.     }

  379. }