Transformer.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF 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.propagation.events;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.util.FastMath;
  20. import org.hipparchus.util.Precision;


  21. /** Transformer for {@link EventHandler#g(double, double[]) g functions}.
  22.  * <p>This class is heavily based on the class with the same name from the
  23.  * Hipparchus library. The changes performed consist in replacing
  24.  * raw types (double and double arrays) with space dynamics types
  25.  * ({@link AbsoluteDate}, {@link SpacecraftState}).</p>
  26.  * @see EventSlopeFilter
  27.  * @see FilterType
  28.  * @since 6.0
  29.  */
  30. enum Transformer {

  31.     /** Transformer computing transformed = 0.
  32.      * <p>
  33.      * This transformer is used when we initialize the filter, until we get at
  34.      * least one non-zero value to select the proper transformer.
  35.      * </p>
  36.      */
  37.     UNINITIALIZED {
  38.         /**  {@inheritDoc} */
  39.         protected double transformed(final double g) {
  40.             return 0;
  41.         }
  42.         /**  {@inheritDoc} */
  43.         protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
  44.             return g.getField().getZero();
  45.         }
  46.     },

  47.     /** Transformer computing transformed = g.
  48.      * <p>
  49.      * When this transformer is applied, the roots of the original function
  50.      * are preserved, with the same {@code increasing/decreasing} status.
  51.      * </p>
  52.      */
  53.     PLUS {
  54.         /**  {@inheritDoc} */
  55.         protected double transformed(final double g) {
  56.             return g;
  57.         }
  58.         /**  {@inheritDoc} */
  59.         protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
  60.             return g;
  61.         }
  62.     },

  63.     /** Transformer computing transformed = -g.
  64.      * <p>
  65.      * When this transformer is applied, the roots of the original function
  66.      * are preserved, with reversed {@code increasing/decreasing} status.
  67.      * </p>
  68.      */
  69.     MINUS {
  70.         /**  {@inheritDoc} */
  71.         protected double transformed(final double g) {
  72.             return -g;
  73.         }
  74.         /**  {@inheritDoc} */
  75.         protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
  76.             return g.negate();
  77.         }
  78.     },

  79.     /** Transformer computing transformed = min(-{@link Precision#SAFE_MIN}, -g, +g).
  80.      * <p>
  81.      * When this transformer is applied, the transformed function is
  82.      * guaranteed to be always strictly negative (i.e. there are no roots).
  83.      * </p>
  84.      */
  85.     MIN {
  86.         /**  {@inheritDoc} */
  87.         protected double transformed(final double g) {
  88.             return FastMath.min(-Precision.SAFE_MIN, FastMath.min(-g, +g));
  89.         }
  90.         /**  {@inheritDoc} */
  91.         protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
  92.             final T zero = g.getField().getZero();
  93.             return FastMath.min(zero.newInstance(-Precision.SAFE_MIN), FastMath.min(g.negate(), g));
  94.         }
  95.     },

  96.     /** Transformer computing transformed = max(+{@link Precision#SAFE_MIN}, -g, +g).
  97.      * <p>
  98.      * When this transformer is applied, the transformed function is
  99.      * guaranteed to be always strictly positive (i.e. there are no roots).
  100.      * </p>
  101.      */
  102.     MAX {
  103.         /**  {@inheritDoc} */
  104.         protected double transformed(final double g) {
  105.             return FastMath.max(+Precision.SAFE_MIN, FastMath.max(-g, +g));
  106.         }
  107.         /**  {@inheritDoc} */
  108.         protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
  109.             final T zero = g.getField().getZero();
  110.             return FastMath.max(zero.newInstance(+Precision.SAFE_MIN), FastMath.max(g.negate(), g));
  111.         }
  112.     };

  113.     /** Transform value of function g.
  114.      * @param g raw value of function g
  115.      * @return transformed value of function g
  116.      */
  117.     protected abstract double transformed(double g);

  118.     /** Transform value of function g.
  119.      * @param <T> type of the field elements
  120.      * @param g raw value of function g
  121.      * @return transformed value of function g
  122.      * @since 12.0
  123.      */
  124.     protected abstract <T extends CalculusFieldElement<T>> T transformed(T g);

  125. }