PolynomialRotation.java

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

  18. import java.util.stream.Stream;

  19. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  20. import org.hipparchus.analysis.polynomials.PolynomialFunction;
  21. import org.hipparchus.geometry.euclidean.threed.FieldRotation;
  22. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  23. import org.hipparchus.geometry.euclidean.threed.Rotation;
  24. import org.hipparchus.geometry.euclidean.threed.RotationConvention;
  25. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  26. import org.hipparchus.util.FastMath;
  27. import org.orekit.errors.OrekitException;
  28. import org.orekit.rugged.errors.RuggedException;
  29. import org.orekit.rugged.utils.DSGenerator;
  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<DerivativeStructure> axisDS;

  51.     /** Rotation angle polynomial and derivatives. */
  52.     private DerivativeStructure[] 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.         try {
  87.             for (int i = 0; i < angleCoeffs.length; ++i) {
  88.                 coefficientsDrivers[i] = new ParameterDriver(name + "[" + i + "]", angleCoeffs[i], SCALE,
  89.                                                              Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
  90.                 coefficientsDrivers[i].addObserver(resettingObserver);
  91.             }
  92.         } catch (OrekitException oe) {
  93.             // this should never happen
  94.             throw RuggedException.createInternalError(oe);
  95.         }
  96.     }

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

  115.     /** {@inheritDoc}
  116.      * @since 2.0
  117.      */
  118.     @Override
  119.     public Stream<ParameterDriver> getParametersDrivers() {
  120.         return Stream.of(coefficientsDrivers);
  121.     }

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

  137.     /** {@inheritDoc} */
  138.     @Override
  139.     public FieldVector3D<DerivativeStructure> transformLOS(final int i, final FieldVector3D<DerivativeStructure> los,
  140.                                                            final AbsoluteDate date, final DSGenerator generator) {

  141.         if (angleDS == null) {
  142.             // lazy evaluation of the rotation
  143.             axisDS = new FieldVector3D<DerivativeStructure>(generator.constant(axis.getX()),
  144.                                                             generator.constant(axis.getY()),
  145.                                                             generator.constant(axis.getZ()));
  146.             angleDS = new DerivativeStructure[coefficientsDrivers.length];
  147.             for (int k = 0; k < angleDS.length; ++k) {
  148.                 angleDS[k] = generator.variable(coefficientsDrivers[k]);
  149.             }
  150.         }
  151.         // evaluate polynomial, with all its partial derivatives
  152.         final double t = date.durationFrom(referenceDate);
  153.         DerivativeStructure alpha = axisDS.getX().getField().getZero();
  154.         for (int k = angleDS.length - 1; k >= 0; --k) {
  155.             alpha = alpha.multiply(t).add(angleDS[k]);
  156.         }

  157.         return new FieldRotation<DerivativeStructure>(axisDS,
  158.                                                       alpha,
  159.                                                       RotationConvention.VECTOR_OPERATOR).applyTo(los);

  160.     }

  161. }