PolynomialRotation.java

  1. /* Copyright 2013-2022 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.rugged.los;

  18. import java.util.stream.Stream;

  19. import org.hipparchus.Field;
  20. import org.hipparchus.analysis.differentiation.Derivative;
  21. import org.hipparchus.analysis.polynomials.PolynomialFunction;
  22. import org.hipparchus.geometry.euclidean.threed.FieldRotation;
  23. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  24. import org.hipparchus.geometry.euclidean.threed.Rotation;
  25. import org.hipparchus.geometry.euclidean.threed.RotationConvention;
  26. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  27. import org.hipparchus.util.FastMath;
  28. import org.hipparchus.util.MathArrays;
  29. import org.orekit.rugged.utils.DerivativeGenerator;
  30. import org.orekit.time.AbsoluteDate;
  31. import org.orekit.utils.ParameterDriver;
  32. import org.orekit.utils.ParameterObserver;

  33. /** {@link LOSTransform LOS transform} based on a rotation with polynomial angle.
  34.  * @author Luc Maisonobe
  35.  * @see LOSBuilder
  36.  */
  37. public class PolynomialRotation implements LOSTransform {

  38.     /** Parameters scaling factor.
  39.      * <p>
  40.      * We use a power of 2 to avoid numeric noise introduction
  41.      * in the multiplications/divisions sequences.
  42.      * </p>
  43.      */
  44.     private final double SCALE = FastMath.scalb(1.0, -20);

  45.     /** Rotation axis. */
  46.     private final Vector3D axis;

  47.     /** Rotation angle polynomial. */
  48.     private PolynomialFunction angle;

  49.     /** Rotation axis and derivatives. */
  50.     private FieldVector3D<?> axisDS;

  51.     /** Rotation angle polynomial and derivatives. */
  52.     private Derivative<?>[] angleDS;

  53.     /** Reference date for polynomial evaluation. */
  54.     private final AbsoluteDate referenceDate;

  55.     /** Drivers for rotation angle polynomial coefficients. */
  56.     private final ParameterDriver[] coefficientsDrivers;

  57.     /** Simple constructor.
  58.      * <p>
  59.      * The angle of the rotation is evaluated as a polynomial in t,
  60.      * where t is the duration in seconds between evaluation date and
  61.      * reference date. The parameters are the polynomial coefficients,
  62.      * with the constant term at index 0.
  63.      * </p>
  64.      * @param name name of the rotation (used for estimated parameters identification)
  65.      * @param axis rotation axis
  66.      * @param referenceDate reference date for the polynomial angle
  67.      * @param angleCoeffs polynomial coefficients of the polynomial angle,
  68.      * with the constant term at index 0
  69.      */
  70.     public PolynomialRotation(final String name,
  71.                               final Vector3D axis,
  72.                               final AbsoluteDate referenceDate,
  73.                               final double... angleCoeffs) {
  74.         this.axis                = axis;
  75.         this.referenceDate       = referenceDate;
  76.         this.coefficientsDrivers = new ParameterDriver[angleCoeffs.length];
  77.         final ParameterObserver resettingObserver = new ParameterObserver() {
  78.             @Override
  79.             public void valueChanged(final double previousValue, final ParameterDriver driver) {
  80.                 // reset rotations to null, they will be evaluated lazily if needed
  81.                 angle   = null;
  82.                 axisDS  = null;
  83.                 angleDS = null;
  84.             }
  85.         };
  86.         for (int i = 0; i < angleCoeffs.length; ++i) {
  87.             coefficientsDrivers[i] = new ParameterDriver(name + "[" + i + "]", angleCoeffs[i], SCALE,
  88.                     Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
  89.             coefficientsDrivers[i].addObserver(resettingObserver);
  90.         }
  91.     }

  92.     /** Simple constructor.
  93.      * <p>
  94.      * The angle of the rotation is evaluated as a polynomial in t,
  95.      * where t is the duration in seconds between evaluation date and
  96.      * reference date. The parameters are the polynomial coefficients,
  97.      * with the constant term at index 0.
  98.      * </p>
  99.      * @param name name of the rotation (used for estimated parameters identification)
  100.      * @param axis rotation axis
  101.      * @param referenceDate reference date for the polynomial angle
  102.      * @param angle polynomial angle
  103.      */
  104.     public PolynomialRotation(final String name,
  105.                               final Vector3D axis,
  106.                               final AbsoluteDate referenceDate,
  107.                               final PolynomialFunction angle) {
  108.         this(name, axis, referenceDate, angle.getCoefficients());
  109.     }

  110.     /** {@inheritDoc}
  111.      * @since 2.0
  112.      */
  113.     @Override
  114.     public Stream<ParameterDriver> getParametersDrivers() {
  115.         return Stream.of(coefficientsDrivers);
  116.     }

  117.     /** {@inheritDoc} */
  118.     @Override
  119.     public Vector3D transformLOS(final int i, final Vector3D los, final AbsoluteDate date) {
  120.         if (angle == null) {
  121.             // lazy evaluation of the rotation
  122.             final double[] coefficients = new double[coefficientsDrivers.length];
  123.             for (int k = 0; k < coefficients.length; ++k) {
  124.                 coefficients[k] = coefficientsDrivers[k].getValue();
  125.             }
  126.             angle = new PolynomialFunction(coefficients);
  127.         }
  128.         return new Rotation(axis,
  129.                             angle.value(date.durationFrom(referenceDate)),
  130.                             RotationConvention.VECTOR_OPERATOR).applyTo(los);
  131.     }

  132.     /** {@inheritDoc} */
  133.     @SuppressWarnings("unchecked")
  134.     @Override
  135.     public <T extends Derivative<T>> FieldVector3D<T> transformLOS(final int i, final FieldVector3D<T> los,
  136.                                                                    final AbsoluteDate date,
  137.                                                                    final DerivativeGenerator<T> generator) {

  138.         final Field<T> field = generator.getField();
  139.         final FieldVector3D<T> axisD;
  140.         final T[] angleD;
  141.         if (axisDS == null || !axisDS.getX().getField().equals(field)) {

  142.             // lazy evaluation of the rotation
  143.             axisD = new FieldVector3D<>(generator.constant(axis.getX()),
  144.                                         generator.constant(axis.getY()),
  145.                                         generator.constant(axis.getZ()));
  146.             angleD = MathArrays.buildArray(field, coefficientsDrivers.length);
  147.             for (int k = 0; k < angleD.length; ++k) {
  148.                 angleD[k] = generator.variable(coefficientsDrivers[k]);
  149.             }

  150.             // cache evaluated rotation parameters
  151.             axisDS  = axisD;
  152.             angleDS = angleD;

  153.         } else {
  154.             // reuse cached values
  155.             axisD  = (FieldVector3D<T>) axisDS;
  156.             angleD = (T[]) angleDS;
  157.         }

  158.         // evaluate polynomial, with all its partial derivatives
  159.         final double t = date.durationFrom(referenceDate);
  160.         T alpha = field.getZero();
  161.         for (int k = angleDS.length - 1; k >= 0; --k) {
  162.             alpha = alpha.multiply(t).add(angleD[k]);
  163.         }

  164.         return new FieldRotation<>(axisD, alpha, RotationConvention.VECTOR_OPERATOR).applyTo(los);

  165.     }

  166. }