OrphanFrame.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.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;

  21. import org.orekit.errors.OrekitException;
  22. import org.orekit.errors.OrekitMessages;


  23. /** Prototype frame that can be built from leaf to roots and later attached to a tree.
  24.  *
  25.  * <p>Regular {@link Frame} instances can be built only from a parent frame, i.e.
  26.  * the frames tree can be built only from root to leafs. In some cases, it may
  27.  * desirable to build a subset tree and attach it to the main tree after build
  28.  * time, which means the tree is built from leafs to root. This class allows
  29.  * building this subtree.</p>
  30.  * <p>
  31.  * During the build process, the {@link Frame} associated with each {@link OrphanFrame}
  32.  * is not available. It becomes available only once the {@link OrphanFrame} has been
  33.  * attached to the main tree, and at this time it can be used to compute
  34.  * {@link Transform transforms}.
  35.  * </p>
  36.  *
  37.  * @author Luc Maisonobe
  38.  * @since 6.0
  39.  */
  40. public class OrphanFrame {

  41.     /** Instance name. */
  42.     private final String name;

  43.     /** Children of the frame. */
  44.     private final List<OrphanFrame> children;

  45.     /** Parent orphan frame. */
  46.     private OrphanFrame orphanParent;

  47.     /** Provider for transform from parent frame to instance. */
  48.     private TransformProvider provider;

  49.     /** Indicator for pseudo-inertial frames. */
  50.     private boolean pseudoInertial;

  51.     /** Associated frame (available only once attached to the main frames tree). */
  52.     private Frame frame;

  53.     /** Simple constructor.
  54.      * @param name name of the frame
  55.      */
  56.     public OrphanFrame(final String name) {
  57.         children  = new ArrayList<>();
  58.         this.name = name;
  59.     }

  60.     /** Add a child.
  61.      * <p>
  62.      * If a child is added after the instance has been attached, the child and
  63.      * all its tree will be attached immediately too.
  64.      * </p>
  65.      * @param child child to add
  66.      * @param transform transform from instance to child
  67.      * @param isPseudoInertial true if child is considered pseudo-inertial
  68.      * (i.e. suitable for propagating orbit)
  69.      */
  70.     public void addChild(final OrphanFrame child, final Transform transform,
  71.                          final boolean isPseudoInertial) {
  72.         addChild(child, new FixedTransformProvider(transform), isPseudoInertial);
  73.     }

  74.     /** Add a child.
  75.      * <p>
  76.      * If a child is added after the instance has been attached, the child and
  77.      * all its tree will be attached immediately too.
  78.      * </p>
  79.      * @param child child to add
  80.      * @param transformProvider provider for transform from instance to child
  81.      * @param isPseudoInertial true if child is considered pseudo-inertial
  82.      * (i.e. suitable for propagating orbit)
  83.      */
  84.     public void addChild(final OrphanFrame child, final TransformProvider transformProvider,
  85.                          final boolean isPseudoInertial) {

  86.         // safety check
  87.         if (child.orphanParent != null) {
  88.             throw new OrekitException(OrekitMessages.FRAME_ALREADY_ATTACHED,
  89.                                       child.name, child.orphanParent.name);
  90.         }

  91.         children.add(child);
  92.         child.orphanParent   = this;
  93.         child.provider       = transformProvider;
  94.         child.pseudoInertial = isPseudoInertial;

  95.         if (frame != null) {
  96.             // we are attaching a child after having attached the instance,
  97.             // we process the tree immediately
  98.             buildTree();
  99.         }

  100.     }

  101.     /** Attach the instance (and all its children down to leafs) to the main tree.
  102.      * @param parent parent frame to attach to
  103.      * @param transform transform from parent frame to instance
  104.      * @param isPseudoInertial true if frame is considered pseudo-inertial
  105.      * (i.e. suitable for propagating orbit)
  106.      */
  107.     public void attachTo(final Frame parent, final Transform transform,
  108.                          final boolean isPseudoInertial) {
  109.         attachTo(parent, new FixedTransformProvider(transform), isPseudoInertial);
  110.     }

  111.     /** Attach the instance (and all its children down to leafs) to the main tree.
  112.      * @param parent parent frame to attach to
  113.      * @param transformProvider provider for transform from parent frame to instance
  114.      * @param isPseudoInertial true if frame is considered pseudo-inertial
  115.      * (i.e. suitable for propagating orbit)
  116.      */
  117.     public void attachTo(final Frame parent, final TransformProvider transformProvider,
  118.                          final boolean isPseudoInertial) {

  119.         // safety check
  120.         if (orphanParent != null) {
  121.             throw new OrekitException(OrekitMessages.FRAME_ALREADY_ATTACHED,
  122.                                       name, orphanParent.name);
  123.         }

  124.         // set up the attached point
  125.         final OrphanFrame op = new OrphanFrame(parent.getName());
  126.         op.frame = parent;
  127.         op.addChild(this, transformProvider, isPseudoInertial);

  128.     }

  129.     /** Get all children of the instance.
  130.      * @return unmodifiable list of children
  131.      */
  132.     public List<OrphanFrame> getChildren() {
  133.         return Collections.unmodifiableList(children);
  134.     }

  135.     /** Get the associated {@link Frame frame}.
  136.      * @return associated frame
  137.      */
  138.     public Frame getFrame() {

  139.         // safety check
  140.         if (frame == null) {
  141.             throw new OrekitException(OrekitMessages.FRAME_NOT_ATTACHED, name);
  142.         }

  143.         return frame;

  144.     }

  145.     /** Recursively build the frames tree starting at instance, which is already associated.
  146.      */
  147.     private void buildTree() {
  148.         for (final OrphanFrame child : children) {

  149.             if (child.frame == null) {

  150.                 // associate the child with a regular frame
  151.                 child.frame = new Frame(frame, child.provider, child.name, child.pseudoInertial);

  152.                 // recursively build the rest of the tree
  153.                 child.buildTree();

  154.             }

  155.         }
  156.     }

  157.     /** {@inheritDoc} */
  158.     @Override
  159.     public String toString() {
  160.         return this.name;
  161.     }

  162. }