Frame.java

  1. /* Copyright 2002-2016 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.orekit.errors.OrekitIllegalArgumentException;
  20. import org.orekit.errors.OrekitException;
  21. import org.orekit.errors.OrekitMessages;
  22. import org.orekit.time.AbsoluteDate;


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

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

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

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

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

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

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

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

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

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

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

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

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

  145.     }

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

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

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

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

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

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

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

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

  206.         return current;

  207.     }

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

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

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

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

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

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

  236.     }

  237.     /** Get the provider for transform from parent frame to instance.
  238.      * @return provider for transform from parent frame to instance
  239.      */
  240.     public TransformProvider getTransformProvider() {
  241.         return transformProvider;
  242.     }

  243.     /** Find the deepest common ancestor of two frames in the frames tree.
  244.      * @param from origin frame
  245.      * @param to destination frame
  246.      * @return an ancestor frame of both <code>from</code> and <code>to</code>
  247.      */
  248.     private static Frame findCommon(final Frame from, final Frame to) {

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

  252.         // go upward until we find a match
  253.         while (currentF != currentT) {
  254.             currentF = currentF.parent;
  255.             currentT = currentT.parent;
  256.         }

  257.         return currentF;

  258.     }

  259.     /** Determine if a Frame is a child of another one.
  260.      * @param potentialAncestor supposed ancestor frame
  261.      * @return true if the potentialAncestor belongs to the
  262.      * path from instance to the root frame, excluding itself
  263.      */
  264.     public boolean isChildOf(final Frame potentialAncestor) {
  265.         if (depth <= potentialAncestor.depth) {
  266.             return false;
  267.         }
  268.         return getAncestor(depth - potentialAncestor.depth) == potentialAncestor;
  269.     }

  270.     /** Get the unique root frame.
  271.      * @return the unique instance of the root frame
  272.      */
  273.     protected static Frame getRoot() {
  274.         return LazyRootHolder.INSTANCE;
  275.     }

  276.     /** Get a new version of the instance, frozen with respect to a reference frame.
  277.      * <p>
  278.      * Freezing a frame consist in computing its position and orientation with respect
  279.      * to another frame at some freezing date and fixing them so they do not depend
  280.      * on time anymore. This means the frozen frame is fixed with respect to the
  281.      * reference frame.
  282.      * </p>
  283.      * <p>
  284.      * One typical use of this method is to compute an inertial launch reference frame
  285.      * by freezing a {@link TopocentricFrame topocentric frame} at launch date
  286.      * with respect to an inertial frame. Another use is to freeze an equinox-related
  287.      * celestial frame at a reference epoch date.
  288.      * </p>
  289.      * <p>
  290.      * Only the frame returned by this method is frozen, the instance by itself
  291.      * is not affected by calling this method and still moves freely.
  292.      * </p>
  293.      * @param reference frame with respect to which the instance will be frozen
  294.      * @param freezingDate freezing date
  295.      * @param frozenName name of the frozen frame
  296.      * @return a frozen version of the instance
  297.      * @exception OrekitException if transform between reference frame and instance
  298.      * cannot be computed at freezing frame
  299.      */
  300.     public Frame getFrozenFrame(final Frame reference, final AbsoluteDate freezingDate,
  301.                                 final String frozenName) throws OrekitException {
  302.         return new Frame(reference, reference.getTransformTo(this, freezingDate).freeze(),
  303.                          frozenName, reference.isPseudoInertial());
  304.     }

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

  308.     /** Holder for the root frame singleton. */
  309.     private static class LazyRootHolder {

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

  312.             /** Serializable UID. */
  313.             private static final long serialVersionUID = -2654403496396721543L;

  314.             /** Replace the instance with a data transfer object for serialization.
  315.              * <p>
  316.              * This intermediate class serializes nothing.
  317.              * </p>
  318.              * @return data transfer object that will be serialized
  319.              */
  320.             private Object writeReplace() {
  321.                 return new DataTransferObject();
  322.             }

  323.         };

  324.         /** Private constructor.
  325.          * <p>This class is a utility class, it should neither have a public
  326.          * nor a default constructor. This private constructor prevents
  327.          * the compiler from generating one automatically.</p>
  328.          */
  329.         private LazyRootHolder() {
  330.         }

  331.     }

  332.     /** Internal class used only for serialization. */
  333.     private static class DataTransferObject implements Serializable {

  334.         /** Serializable UID. */
  335.         private static final long serialVersionUID = 4067764035816491212L;

  336.         /** Simple constructor.
  337.          */
  338.         private DataTransferObject() {
  339.         }

  340.         /** Replace the deserialized data transfer object with a {@link FactoryManagedFrame}.
  341.          * @return replacement {@link FactoryManagedFrame}
  342.          */
  343.         private Object readResolve() {
  344.             return getRoot();
  345.         }

  346.     }

  347. }