FieldKinematicTransform.java

  1. /* Copyright 2022-2024 Romain Serra
  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 org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.geometry.euclidean.threed.FieldRotation;
  21. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  22. import org.orekit.time.AbsoluteDate;
  23. import org.orekit.time.FieldAbsoluteDate;
  24. import org.orekit.utils.FieldPVCoordinates;
  25. import org.orekit.utils.PVCoordinates;
  26. import org.orekit.utils.TimeStampedFieldPVCoordinates;
  27. import org.orekit.utils.TimeStampedPVCoordinates;

  28. /**
  29.  * A transform that only includes translation and rotation as well as their respective rates.
  30.  * It is kinematic in the sense that it cannot transform an acceleration vector.
  31.  *
  32.  * @author Romain Serra
  33.  * @see FieldStaticTransform
  34.  * @see FieldTransform
  35.  * @see KinematicTransform
  36.  * @since 12.1
  37.  */
  38. public interface FieldKinematicTransform<T extends CalculusFieldElement<T>> extends FieldStaticTransform<T> {

  39.     /**
  40.      * Get the identity kinematic transform.
  41.      *
  42.      * @param <T> type of the elements
  43.      * @param field field used by default
  44.      * @return identity transform.
  45.      */
  46.     static <T extends CalculusFieldElement<T>> FieldKinematicTransform<T> getIdentity(final Field<T> field) {
  47.         return FieldTransform.getIdentity(field);
  48.     }

  49.     /** Compute a composite velocity.
  50.      * @param first first applied transform
  51.      * @param second second applied transform
  52.      * @param <T> the type of the field elements
  53.      * @return velocity part of the composite transform
  54.      */
  55.     static <T extends CalculusFieldElement<T>> FieldVector3D<T> compositeVelocity(final FieldKinematicTransform<T> first,
  56.                                                                                   final FieldKinematicTransform<T> second) {

  57.         final FieldVector3D<T> v1 = first.getVelocity();
  58.         final FieldRotation<T> r1 = first.getRotation();
  59.         final FieldVector3D<T> o1 = first.getRotationRate();
  60.         final FieldVector3D<T> p2 = second.getTranslation();
  61.         final FieldVector3D<T> v2 = second.getVelocity();

  62.         final FieldVector3D<T> crossP = FieldVector3D.crossProduct(o1, p2);

  63.         return v1.add(r1.applyInverseTo(v2.add(crossP)));
  64.     }

  65.     /** Compute a composite rotation rate.
  66.      * @param <T> type of the elements
  67.      * @param first first applied transform
  68.      * @param second second applied transform
  69.      * @return rotation rate part of the composite transform
  70.      */
  71.     static <T extends CalculusFieldElement<T>> FieldVector3D<T> compositeRotationRate(final FieldKinematicTransform<T> first,
  72.                                                                                       final FieldKinematicTransform<T> second) {

  73.         final FieldVector3D<T> o1 = first.getRotationRate();
  74.         final FieldRotation<T> r2 = second.getRotation();
  75.         final FieldVector3D<T> o2 = second.getRotationRate();

  76.         return o2.add(r2.applyTo(o1));
  77.     }

  78.     /** Transform {@link PVCoordinates}, without the acceleration vector.
  79.      * @param pv the position-velocity couple to transform.
  80.      * @return transformed position-velocity
  81.      */
  82.     default FieldPVCoordinates<T> transformOnlyPV(final FieldPVCoordinates<T> pv) {
  83.         final FieldVector3D<T> transformedP = transformPosition(pv.getPosition());
  84.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(getRotationRate(), transformedP);
  85.         final FieldVector3D<T> transformedV = getRotation().applyTo(pv.getVelocity().add(getVelocity())).subtract(crossP);
  86.         return new FieldPVCoordinates<>(transformedP, transformedV);
  87.     }

  88.     /** Transform {@link TimeStampedPVCoordinates}, without the acceleration vector.
  89.      * <p>
  90.      * In order to allow the user more flexibility, this method does <em>not</em> check for
  91.      * consistency between the transform {@link #getDate() date} and the time-stamped
  92.      * position-velocity {@link TimeStampedPVCoordinates#getDate() date}. The returned
  93.      * value will always have the same {@link TimeStampedPVCoordinates#getDate() date} as
  94.      * the input argument, regardless of the instance {@link #getDate() date}.
  95.      * </p>
  96.      * @param pv the position-velocity couple to transform.
  97.      * @return transformed position-velocity
  98.      */
  99.     default TimeStampedFieldPVCoordinates<T> transformOnlyPV(final TimeStampedFieldPVCoordinates<T> pv) {
  100.         final FieldVector3D<T> transformedP = transformPosition(pv.getPosition());
  101.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(getRotationRate(), transformedP);
  102.         final FieldVector3D<T> transformedV = getRotation().applyTo(pv.getVelocity().add(getVelocity())).subtract(crossP);
  103.         return new TimeStampedFieldPVCoordinates<>(pv.getDate(), transformedP, transformedV,
  104.                 FieldVector3D.getZero(pv.getDate().getField()));
  105.     }

  106.     /** Get the first time derivative of the translation.
  107.      * @return first time derivative of the translation
  108.      * @see #getTranslation()
  109.      */
  110.     FieldVector3D<T> getVelocity();

  111.     /** Get the first time derivative of the rotation.
  112.      * <p>The norm represents the angular rate.</p>
  113.      * @return First time derivative of the rotation
  114.      * @see #getRotation()
  115.      */
  116.     FieldVector3D<T> getRotationRate();

  117.     /**
  118.      * Get the inverse transform of the instance.
  119.      *
  120.      * @return inverse transform of the instance
  121.      */
  122.     FieldKinematicTransform<T> getInverse();

  123.     /**
  124.      * Build a transform by combining two existing ones.
  125.      * <p>
  126.      * Note that the dates of the two existing transformed are <em>ignored</em>,
  127.      * and the combined transform date is set to the date supplied in this
  128.      * constructor without any attempt to shift the raw transforms. This is a
  129.      * design choice allowing user full control of the combination.
  130.      * </p>
  131.      *
  132.      * @param <T> type of the elements
  133.      * @param date   date of the transform
  134.      * @param first  first transform applied
  135.      * @param second second transform applied
  136.      * @return the newly created kinematic transform that has the same effect as
  137.      * applying {@code first}, then {@code second}.
  138.      * @see #of(FieldAbsoluteDate, FieldPVCoordinates, FieldRotation, FieldVector3D)
  139.      */
  140.     static <T extends CalculusFieldElement<T>> FieldKinematicTransform<T> compose(final FieldAbsoluteDate<T> date,
  141.                                                                                   final FieldKinematicTransform<T> first,
  142.                                                                                   final FieldKinematicTransform<T> second) {
  143.         final FieldVector3D<T> composedTranslation = FieldStaticTransform.compositeTranslation(first, second);
  144.         final FieldVector3D<T> composedTranslationRate = FieldKinematicTransform.compositeVelocity(first, second);
  145.         return of(date, new FieldPVCoordinates<>(composedTranslation, composedTranslationRate),
  146.                 FieldStaticTransform.compositeRotation(first, second),
  147.                 FieldKinematicTransform.compositeRotationRate(first, second));
  148.     }

  149.     /**
  150.      * Create a new kinematic transform from a rotation and zero, constant translation.
  151.      *
  152.      * @param <T> type of the elements
  153.      * @param date     of translation.
  154.      * @param rotation to apply after the translation. That is after translating
  155.      *                 applying this rotation produces positions expressed in
  156.      *                 the new frame.
  157.      * @param rotationRate rate of rotation
  158.      * @return the newly created kinematic transform.
  159.      * @see #of(FieldAbsoluteDate, FieldPVCoordinates, FieldRotation, FieldVector3D)
  160.      */
  161.     static <T extends CalculusFieldElement<T>> FieldKinematicTransform<T> of(final FieldAbsoluteDate<T> date,
  162.                                                                              final FieldRotation<T> rotation,
  163.                                                                              final FieldVector3D<T> rotationRate) {
  164.         return of(date, FieldPVCoordinates.getZero(date.getField()), rotation, rotationRate);
  165.     }

  166.     /**
  167.      * Create a new kinematic transform from a translation and its rate.
  168.      *
  169.      * @param <T> type of the elements
  170.      * @param date        of translation.
  171.      * @param pvCoordinates translation (with rate) to apply, expressed in the old frame. That is, the
  172.      *                    opposite of the coordinates of the new origin in the
  173.      *                    old frame.
  174.      * @return the newly created kinematic transform.
  175.      * @see #of(FieldAbsoluteDate, FieldPVCoordinates, FieldRotation, FieldVector3D)
  176.      */
  177.     static <T extends CalculusFieldElement<T>> FieldKinematicTransform<T> of(final FieldAbsoluteDate<T> date,
  178.                                                                              final FieldPVCoordinates<T> pvCoordinates) {
  179.         final Field<T> field = date.getField();
  180.         return of(date, pvCoordinates, FieldRotation.getIdentity(field), FieldVector3D.getZero(field));
  181.     }

  182.     /**
  183.      * Create a new kinematic transform from a non-Field version.
  184.      *
  185.      * @param <T> type of the elements
  186.      * @param field field.
  187.      * @param kinematicTransform non-Field kinematic transform
  188.      * @return the newly created kinematic transform.
  189.      * @see #of(FieldAbsoluteDate, FieldPVCoordinates, FieldRotation, FieldVector3D)
  190.      */
  191.     static <T extends CalculusFieldElement<T>> FieldKinematicTransform<T> of(final Field<T> field,
  192.                                                                              final KinematicTransform kinematicTransform) {
  193.         final FieldAbsoluteDate<T> date = new FieldAbsoluteDate<>(field, kinematicTransform.getDate());
  194.         final FieldPVCoordinates<T> pvCoordinates = new FieldPVCoordinates<>(field,
  195.             new PVCoordinates(kinematicTransform.getTranslation(), kinematicTransform.getVelocity()));
  196.         final FieldRotation<T> rotation = new FieldRotation<>(field, kinematicTransform.getRotation());
  197.         final FieldVector3D<T> rotationRate = new FieldVector3D<>(field, kinematicTransform.getRotationRate());
  198.         return of(date, pvCoordinates, rotation, rotationRate);
  199.     }

  200.     /**
  201.      * Create a new kinematic transform from a translation and rotation.
  202.      *
  203.      * @param <T> type of the elements
  204.      * @param date        of translation.
  205.      * @param pvCoordinates translation (with rate) to apply, expressed in the old frame. That is, the
  206.      *                    opposite of the coordinates of the new origin in the
  207.      *                    old frame.
  208.      * @param rotation    to apply after the translation. That is after
  209.      *                    translating applying this rotation produces positions
  210.      *                    expressed in the new frame.
  211.      * @param rotationRate rate of rotation
  212.      * @return the newly created kinematic transform.
  213.      * @see #compose(FieldAbsoluteDate, FieldKinematicTransform, FieldKinematicTransform)
  214.      * @see #of(FieldAbsoluteDate, FieldPVCoordinates, FieldRotation, FieldVector3D)
  215.      * @see #of(FieldAbsoluteDate, FieldPVCoordinates, FieldRotation, FieldVector3D)
  216.      */
  217.     static <T extends CalculusFieldElement<T>> FieldKinematicTransform<T> of(final FieldAbsoluteDate<T> date,
  218.                                                                           final FieldPVCoordinates<T> pvCoordinates,
  219.                                                                           final FieldRotation<T> rotation,
  220.                                                                           final FieldVector3D<T> rotationRate) {
  221.         return new FieldKinematicTransform<T>() {

  222.             @Override
  223.             public FieldKinematicTransform<T> getInverse() {
  224.                 final FieldRotation<T> r = getRotation();
  225.                 final FieldVector3D<T> rp = r.applyTo(getTranslation());
  226.                 final FieldVector3D<T> pInv = rp.negate();
  227.                 final FieldVector3D<T> crossP      = FieldVector3D.crossProduct(getRotationRate(), rp);
  228.                 final FieldVector3D<T> vInv        = crossP.subtract(getRotation().applyTo(getVelocity()));
  229.                 final FieldRotation<T> rInv = r.revert();
  230.                 return FieldKinematicTransform.of(date, new FieldPVCoordinates<>(pInv, vInv),
  231.                         rInv, rInv.applyTo(getRotationRate()).negate());
  232.             }

  233.             @Override
  234.             public AbsoluteDate getDate() {
  235.                 return date.toAbsoluteDate();
  236.             }

  237.             @Override
  238.             public FieldAbsoluteDate<T> getFieldDate() {
  239.                 return date;
  240.             }

  241.             @Override
  242.             public FieldVector3D<T> getTranslation() {
  243.                 return pvCoordinates.getPosition();
  244.             }

  245.             @Override
  246.             public FieldRotation<T> getRotation() {
  247.                 return rotation;
  248.             }

  249.             @Override
  250.             public FieldVector3D<T> getVelocity() {
  251.                 return pvCoordinates.getVelocity();
  252.             }

  253.             @Override
  254.             public FieldVector3D<T> getRotationRate() {
  255.                 return rotationRate;
  256.             }
  257.         };
  258.     }

  259. }