Frame.java

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


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

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

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

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

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

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

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

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

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

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

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

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

  136.         if (parent == null) {
  137.             throw OrekitException.createIllegalArgumentException(OrekitMessages.NULL_PARENT_FOR_FRAME,
  138.                                                                  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.      * @deprecated as of 6.0, replaced by {@link #isPseudoInertial()}
  161.      */
  162.     @Deprecated
  163.     public boolean isQuasiInertial() {
  164.         return isPseudoInertial();
  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 OrekitException.createIllegalArgumentException(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.     /** Get the transform from the instance to another frame.
  223.      * @param destination destination frame to which we want to transform vectors
  224.      * @param date the date (can be null if it is sure than no date dependent frame is used)
  225.      * @return transform from the instance to the destination frame
  226.      * @exception OrekitException if some frame specific error occurs
  227.      */
  228.     public Transform getTransformTo(final Frame destination, final AbsoluteDate date)
  229.         throws OrekitException {

  230.         if (this == destination) {
  231.             // shortcut for special case that may be frequent
  232.             return Transform.IDENTITY;
  233.         }

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

  236.         // transform from common to instance
  237.         Transform commonToInstance = Transform.IDENTITY;
  238.         for (Frame frame = this; frame != common; frame = frame.parent) {
  239.             commonToInstance =
  240.                 new Transform(date, frame.transformProvider.getTransform(date), commonToInstance);
  241.         }

  242.         // transform from destination up to common
  243.         Transform commonToDestination = Transform.IDENTITY;
  244.         for (Frame frame = destination; frame != common; frame = frame.parent) {
  245.             commonToDestination =
  246.                 new Transform(date, frame.transformProvider.getTransform(date), commonToDestination);
  247.         }

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

  250.     }

  251.     /** Get the provider for transform from parent frame to instance.
  252.      * @return provider for transform from parent frame to instance
  253.      */
  254.     public TransformProvider getTransformProvider() {
  255.         return transformProvider;
  256.     }

  257.     /** Find the deepest common ancestor of two frames in the frames tree.
  258.      * @param from origin frame
  259.      * @param to destination frame
  260.      * @return an ancestor frame of both <code>from</code> and <code>to</code>
  261.      */
  262.     private static Frame findCommon(final Frame from, final Frame to) {

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

  266.         // go upward until we find a match
  267.         while (currentF != currentT) {
  268.             currentF = currentF.parent;
  269.             currentT = currentT.parent;
  270.         }

  271.         return currentF;

  272.     }

  273.     /** Determine if a Frame is a child of another one.
  274.      * @param potentialAncestor supposed ancestor frame
  275.      * @return true if the potentialAncestor belongs to the
  276.      * path from instance to the root frame, excluding itself
  277.      */
  278.     public boolean isChildOf(final Frame potentialAncestor) {
  279.         if (depth <= potentialAncestor.depth) {
  280.             return false;
  281.         }
  282.         return getAncestor(depth - potentialAncestor.depth) == potentialAncestor;
  283.     }

  284.     /** Get the unique root frame.
  285.      * @return the unique instance of the root frame
  286.      */
  287.     protected static Frame getRoot() {
  288.         return LazyRootHolder.INSTANCE;
  289.     }

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

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

  322.     /** Holder for the root frame singleton. */
  323.     private static class LazyRootHolder {

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

  326.             /** Serializable UID. */
  327.             private static final long serialVersionUID = -2654403496396721543L;

  328.             /** Replace the instance with a data transfer object for serialization.
  329.              * <p>
  330.              * This intermediate class serializes nothing.
  331.              * </p>
  332.              * @return data transfer object that will be serialized
  333.              */
  334.             private Object writeReplace() {
  335.                 return new DataTransferObject();
  336.             }

  337.         };

  338.         /** Private constructor.
  339.          * <p>This class is a utility class, it should neither have a public
  340.          * nor a default constructor. This private constructor prevents
  341.          * the compiler from generating one automatically.</p>
  342.          */
  343.         private LazyRootHolder() {
  344.         }

  345.     }

  346.     /** Internal class used only for serialization. */
  347.     private static class DataTransferObject implements Serializable {

  348.         /** Serializable UID. */
  349.         private static final long serialVersionUID = 4067764035816491212L;

  350.         /** Simple constructor.
  351.          */
  352.         private DataTransferObject() {
  353.         }

  354.         /** Replace the deserialized data transfer object with a {@link FactoryManagedFrame}.
  355.          * @return replacement {@link FactoryManagedFrame}
  356.          */
  357.         private Object readResolve() {
  358.             return getRoot();
  359.         }

  360.     }

  361. }