FixedRotation.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.analysis.differentiation.Derivative;
  20. import org.hipparchus.geometry.euclidean.threed.FieldRotation;
  21. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  22. import org.hipparchus.geometry.euclidean.threed.Rotation;
  23. import org.hipparchus.geometry.euclidean.threed.RotationConvention;
  24. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  25. import org.hipparchus.util.FastMath;
  26. import org.orekit.rugged.utils.DerivativeGenerator;
  27. import org.orekit.utils.ParameterDriver;
  28. import org.orekit.utils.ParameterObserver;

  29. /** {@link TimeIndependentLOSTransform LOS transform} based on a fixed rotation.
  30.  * @author Luc Maisonobe
  31.  * @see LOSBuilder
  32.  */
  33. public class FixedRotation implements TimeIndependentLOSTransform {

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

  41.     /** Rotation axis. */
  42.     private final Vector3D axis;

  43.     /** Underlying rotation. */
  44.     private Rotation rotation;

  45.     /** Underlying rotation with derivatives. */
  46.     private FieldRotation<?> rDS;

  47.     /** Driver for rotation angle. */
  48.     private final ParameterDriver angleDriver;

  49.     /** Simple constructor.
  50.      * <p>
  51.      * The single parameter is the rotation angle.
  52.      * </p>
  53.      * @param name name of the rotation (used for estimated parameters identification)
  54.      * @param axis rotation axis
  55.      * @param angle rotation angle
  56.      */
  57.     public FixedRotation(final String name, final Vector3D axis, final double angle) {

  58.         this.axis     = axis;
  59.         this.rotation = null;
  60.         this.rDS      = null;
  61.         this.angleDriver = new ParameterDriver(name, angle, SCALE, -2 * FastMath.PI, 2 * FastMath.PI);
  62.         angleDriver.addObserver(new ParameterObserver() {
  63.             @Override
  64.             public void valueChanged(final double previousValue, final ParameterDriver driver) {
  65.                 // reset rotations to null, they will be evaluated lazily if needed
  66.                 rotation = null;
  67.                 rDS      = null;
  68.             }
  69.         });
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     public Stream<ParameterDriver> getParametersDrivers() {
  74.         return Stream.of(angleDriver);
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     public Vector3D transformLOS(final int i, final Vector3D los) {
  79.         if (rotation == null) {
  80.             // lazy evaluation of the rotation
  81.             rotation = new Rotation(axis, angleDriver.getValue(), RotationConvention.VECTOR_OPERATOR);
  82.         }
  83.         return rotation.applyTo(los);
  84.     }

  85.     /** {@inheritDoc} */
  86.     @SuppressWarnings("unchecked")
  87.     @Override
  88.     public <T extends Derivative<T>> FieldVector3D<T> transformLOS(final int i, final FieldVector3D<T> los,
  89.                                                                    final DerivativeGenerator<T> generator) {
  90.         final FieldRotation<T> rD;
  91.         if (rDS == null || !rDS.getQ0().getField().equals(generator.getField())) {

  92.             // lazy evaluation of the rotation
  93.             final FieldVector3D<T> axisDS =
  94.                             new FieldVector3D<>(generator.constant(axis.getX()),
  95.                                                 generator.constant(axis.getY()),
  96.                                                 generator.constant(axis.getZ()));
  97.             final T angleDS = generator.variable(angleDriver);
  98.             rD = new FieldRotation<>(axisDS, angleDS, RotationConvention.VECTOR_OPERATOR);

  99.             // cache evaluated rotation
  100.             rDS = rD;

  101.         } else {
  102.             // reuse cached value
  103.             rD  = (FieldRotation<T>) rDS;
  104.         }

  105.         return rD.applyTo(los);

  106.     }

  107. }