FieldBooleanDetector.java

  1. /* Contributed in the public domain.
  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.propagation.events;


  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Collection;
  21. import java.util.Comparator;
  22. import java.util.List;
  23. import java.util.NoSuchElementException;

  24. import org.hipparchus.CalculusFieldElement;
  25. import org.hipparchus.util.FastMath;
  26. import org.orekit.propagation.FieldSpacecraftState;
  27. import org.orekit.propagation.events.handlers.FieldContinueOnEvent;
  28. import org.orekit.propagation.events.handlers.FieldEventHandler;
  29. import org.orekit.time.FieldAbsoluteDate;

  30. /**
  31.  * This class provides AND and OR operations for event detectors. This class treats
  32.  * positive values of the g function as true and negative values as false.
  33.  *
  34.  * <p> One example for an imaging satellite might be to only detect events when a
  35.  * satellite is overhead (elevation &gt; 0) AND when the ground point is sunlit (Sun
  36.  * elevation &gt; 0). Another slightly contrived example using the OR operator would be to
  37.  * detect access to a set of ground stations and only report events when the satellite
  38.  * enters or leaves the field of view of the set, but not hand-offs between the ground
  39.  * stations.
  40.  *
  41.  * <p> For the FieldBooleanDetector is important that the sign of the g function of the
  42.  * underlying event detector is not arbitrary, but has a semantic meaning, e.g. in or out,
  43.  * true or false. This class works well with event detectors that detect entry to or exit
  44.  * from a region, e.g. {@link FieldEclipseDetector}, {@link FieldElevationDetector}, {@link
  45.  * FieldLatitudeCrossingDetector}. Using this detector with detectors that are not based on
  46.  * entry to or exit from a region, e.g. {@link FieldDateDetector}, will likely lead to
  47.  * unexpected results. To apply conditions to this latter type of event detectors a
  48.  * {@link FieldEventEnablingPredicateFilter} is usually more appropriate.
  49.  *
  50.  * @param <T> type of the field elements
  51.  * @since 12.0
  52.  * @author Evan Ward
  53.  * @author luc Luc Maisonobe
  54.  * @see #andCombine(Collection)
  55.  * @see #orCombine(Collection)
  56.  * @see #notCombine(FieldEventDetector)
  57.  * @see EventEnablingPredicateFilter
  58.  * @see EventSlopeFilter
  59.  */
  60. public class FieldBooleanDetector<T extends CalculusFieldElement<T>> extends FieldAbstractDetector<FieldBooleanDetector<T>, T> {

  61.     /** Original detectors: the operands. */
  62.     private final List<FieldEventDetector<T>> detectors;

  63.     /** The composition function. Should be associative for predictable behavior. */
  64.     private final Operator operator;

  65.     /**
  66.      * Private constructor with all the parameters.
  67.      *
  68.      * @param detectors    the operands.
  69.      * @param operator     reduction operator to apply to value of the g function of the
  70.      *                     operands.
  71.      * @param newMaxCheck  max check interval.
  72.      * @param newThreshold convergence threshold in seconds.
  73.      * @param newMaxIter   max iterations.
  74.      * @param newHandler   event handler.
  75.      */
  76.     protected FieldBooleanDetector(final List<FieldEventDetector<T>> detectors,
  77.                                    final Operator operator,
  78.                                    final FieldAdaptableInterval<T> newMaxCheck,
  79.                                    final T newThreshold,
  80.                                    final int newMaxIter,
  81.                                    final FieldEventHandler<T> newHandler) {
  82.         super(newMaxCheck, newThreshold, newMaxIter, newHandler);
  83.         this.detectors = detectors;
  84.         this.operator = operator;
  85.     }

  86.     /**
  87.      * Create a new event detector that is the logical AND of the given event detectors.
  88.      *
  89.      * <p> The created event detector's g function is positive if and only if the g
  90.      * functions of all detectors in {@code detectors} are positive.
  91.      *
  92.      * <p> The starting interval, threshold, and iteration count are set to the most
  93.      * stringent (minimum) of all the {@code detectors}. The event handlers of the
  94.      * underlying {@code detectors} are not used, instead the default handler is {@link
  95.      * FieldContinueOnEvent}.
  96.      *
  97.      * @param <T> type of the field elements
  98.      * @param detectors the operands. Must contain at least one detector.
  99.      * @return a new event detector that is the logical AND of the operands.
  100.      * @throws NoSuchElementException if {@code detectors} is empty.
  101.      * @see FieldBooleanDetector
  102.      * @see #andCombine(Collection)
  103.      * @see #orCombine(FieldEventDetector...)
  104.      * @see #notCombine(FieldEventDetector)
  105.      */
  106.     @SafeVarargs
  107.     public static <T extends CalculusFieldElement<T>> FieldBooleanDetector<T> andCombine(final FieldEventDetector<T>... detectors) {
  108.         return andCombine(Arrays.asList(detectors));
  109.     }

  110.     /**
  111.      * Create a new event detector that is the logical AND of the given event detectors.
  112.      *
  113.      * <p> The created event detector's g function is positive if and only if the g
  114.      * functions of all detectors in {@code detectors} are positive.
  115.      *
  116.      * <p> The starting interval, threshold, and iteration count are set to the most
  117.      * stringent (minimum) of the {@code detectors}. The event handlers of the
  118.      * underlying {@code detectors} are not used, instead the default handler is {@link
  119.      * FieldContinueOnEvent}.
  120.      *
  121.      * @param <T> type of the field elements
  122.      * @param detectors the operands. Must contain at least one detector.
  123.      * @return a new event detector that is the logical AND of the operands.
  124.      * @throws NoSuchElementException if {@code detectors} is empty.
  125.      * @see FieldBooleanDetector
  126.      * @see #andCombine(FieldEventDetector...)
  127.      * @see #orCombine(Collection)
  128.      * @see #notCombine(FieldEventDetector)
  129.      */
  130.     public static <T extends CalculusFieldElement<T>> FieldBooleanDetector<T> andCombine(final Collection<? extends FieldEventDetector<T>> detectors) {

  131.         return new FieldBooleanDetector<>(new ArrayList<>(detectors), // copy for immutability
  132.                                           Operator.AND,
  133.                                           s -> {
  134.                                               double minInterval = Double.POSITIVE_INFINITY;
  135.                                               for (final FieldEventDetector<T> detector : detectors) {
  136.                                                   minInterval = FastMath.min(minInterval, detector.getMaxCheckInterval().currentInterval(s));
  137.                                               }
  138.                                               return minInterval;
  139.                                           },
  140.                                           detectors.stream().map(FieldEventDetector::getThreshold).min(new FieldComparator<>()).get(),
  141.                                           detectors.stream().map(FieldEventDetector::getMaxIterationCount).min(Integer::compareTo).get(),
  142.                                           new FieldContinueOnEvent<>());
  143.     }

  144.     /**
  145.      * Create a new event detector that is the logical OR of the given event detectors.
  146.      *
  147.      * <p> The created event detector's g function is positive if and only if at least
  148.      * one of g functions of the event detectors in {@code detectors} is positive.
  149.      *
  150.      * <p> The starting interval, threshold, and iteration count are set to the most
  151.      * stringent (minimum) of the {@code detectors}. The event handlers of the
  152.      * underlying EventDetectors are not used, instead the default handler is {@link
  153.      * FieldContinueOnEvent}.
  154.      *
  155.      * @param <T> type of the field elements
  156.      * @param detectors the operands. Must contain at least one detector.
  157.      * @return a new event detector that is the logical OR of the operands.
  158.      * @throws NoSuchElementException if {@code detectors} is empty.
  159.      * @see FieldBooleanDetector
  160.      * @see #orCombine(Collection)
  161.      * @see #andCombine(FieldEventDetector...)
  162.      * @see #notCombine(FieldEventDetector)
  163.      */
  164.     @SafeVarargs
  165.     public static <T extends CalculusFieldElement<T>> FieldBooleanDetector<T> orCombine(final FieldEventDetector<T>... detectors) {
  166.         return orCombine(Arrays.asList(detectors));
  167.     }

  168.     /**
  169.      * Create a new event detector that is the logical OR of the given event detectors.
  170.      *
  171.      * <p> The created event detector's g function is positive if and only if at least
  172.      * one of g functions of the event detectors in {@code detectors} is positive.
  173.      *
  174.      * <p> The starting interval, threshold, and iteration count are set to the most
  175.      * stringent (minimum) of the {@code detectors}. The event handlers of the
  176.      * underlying EventDetectors are not used, instead the default handler is {@link
  177.      * FieldContinueOnEvent}.
  178.      *
  179.      * @param <T> type of the field elements
  180.      * @param detectors the operands. Must contain at least one detector.
  181.      * @return a new event detector that is the logical OR of the operands.
  182.      * @throws NoSuchElementException if {@code detectors} is empty.
  183.      * @see FieldBooleanDetector
  184.      * @see #orCombine(FieldEventDetector...)
  185.      * @see #andCombine(Collection)
  186.      * @see #notCombine(FieldEventDetector)
  187.      */
  188.     public static <T extends CalculusFieldElement<T>> FieldBooleanDetector<T> orCombine(final Collection<? extends FieldEventDetector<T>> detectors) {

  189.         return new FieldBooleanDetector<>(new ArrayList<>(detectors), // copy for immutability
  190.                                           Operator.OR,
  191.                                           s -> {
  192.                                               double minInterval = Double.POSITIVE_INFINITY;
  193.                                               for (final FieldEventDetector<T> detector : detectors) {
  194.                                                   minInterval = FastMath.min(minInterval, detector.getMaxCheckInterval().currentInterval(s));
  195.                                               }
  196.                                               return minInterval;
  197.                                           },
  198.                                           detectors.stream().map(FieldEventDetector::getThreshold).min(new FieldComparator<>()).get(),
  199.                                           detectors.stream().map(FieldEventDetector::getMaxIterationCount).min(Integer::compareTo).get(),
  200.                                           new FieldContinueOnEvent<>());
  201.     }

  202.     /**
  203.      * Create a new event detector that negates the g function of another detector.
  204.      *
  205.      * <p> This detector will be initialized with the same {@link
  206.      * FieldEventDetector#getMaxCheckInterval()}, {@link FieldEventDetector#getThreshold()}, and
  207.      * {@link FieldEventDetector#getMaxIterationCount()} as {@code detector}. The event handler
  208.      * of the underlying detector is not used, instead the default handler is {@link
  209.      * FieldContinueOnEvent}.
  210.      *
  211.      * @param <T> type of the field elements
  212.      * @param detector to negate.
  213.      * @return an new event detector whose g function is the same magnitude but opposite
  214.      * sign of {@code detector}.
  215.      * @see #andCombine(Collection)
  216.      * @see #orCombine(Collection)
  217.      * @see FieldBooleanDetector
  218.      */
  219.     public static <T extends CalculusFieldElement<T>> FieldNegateDetector<T> notCombine(final FieldEventDetector<T> detector) {
  220.         return new FieldNegateDetector<>(detector);
  221.     }

  222.     @Override
  223.     public T g(final FieldSpacecraftState<T> s) {
  224.         // can't use stream/lambda here because g(s) throws a checked exception
  225.         // so write out and combine the map and reduce loops
  226.         T ret = s.getDate().getField().getZero().newInstance(Double.NaN); // return value
  227.         boolean first = true;
  228.         for (final FieldEventDetector<T> detector : detectors) {
  229.             if (first) {
  230.                 ret = detector.g(s);
  231.                 first = false;
  232.             } else {
  233.                 ret = operator.combine(ret, detector.g(s));
  234.             }
  235.         }
  236.         // return the result of applying the operator to all operands
  237.         return ret;
  238.     }

  239.     @Override
  240.     protected FieldBooleanDetector<T> create(final FieldAdaptableInterval<T> newMaxCheck,
  241.                                              final T newThreshold,
  242.                                              final int newMaxIter,
  243.                                              final FieldEventHandler<T> newHandler) {
  244.         return new FieldBooleanDetector<>(detectors, operator, newMaxCheck, newThreshold,
  245.                                           newMaxIter, newHandler);
  246.     }

  247.     @Override
  248.     public void init(final FieldSpacecraftState<T> s0,
  249.                      final FieldAbsoluteDate<T> t) {
  250.         super.init(s0, t);
  251.         for (final FieldEventDetector<T> detector : detectors) {
  252.             detector.init(s0, t);
  253.         }
  254.     }

  255.     /**
  256.      * Get the list of original detectors.
  257.      * @return the list of original detectors
  258.      */
  259.     public List<FieldEventDetector<T>> getDetectors() {
  260.         return new ArrayList<>(detectors);
  261.     }

  262.     /** Local class for operator. */
  263.     private enum Operator {

  264.         /** And operator. */
  265.         AND() {

  266.             @Override
  267.             /** {@inheritDoc} */
  268.             public <T extends CalculusFieldElement<T>> T combine(final T g1, final T g2) {
  269.                 return FastMath.min(g1, g2);
  270.             }

  271.         },

  272.         /** Or operator. */
  273.         OR() {

  274.             @Override
  275.             /** {@inheritDoc} */
  276.             public <T extends CalculusFieldElement<T>> T combine(final T g1, final T g2) {
  277.                 return FastMath.max(g1, g2);
  278.             }

  279.         };

  280.         /** Combine two g functions evaluations.
  281.          * @param <T> type of the field elements
  282.          * @param g1 first evaluation
  283.          * @param g2 second evaluation
  284.          * @return combined evaluation
  285.          */
  286.         public abstract <T extends CalculusFieldElement<T>> T combine(T g1, T g2);

  287.     };

  288.     /** Comparator for field elements.
  289.      * @param <T> type of the field elements
  290.      */
  291.     private static class FieldComparator<T extends CalculusFieldElement<T>> implements Comparator<T> {
  292.         public int compare(final T t1, final T t2) {
  293.             return Double.compare(t1.getReal(), t2.getReal());
  294.         }
  295.     }

  296. }