FunctionalDetector.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.function.ToDoubleFunction;

  19. import org.orekit.propagation.SpacecraftState;
  20. import org.orekit.propagation.events.handlers.ContinueOnEvent;
  21. import org.orekit.propagation.events.handlers.EventHandler;
  22. import org.orekit.propagation.events.intervals.AdaptableInterval;

  23. /**
  24.  * A detector that implements the {@link #g(SpacecraftState)} function using a lambda that
  25.  * can be set using {@link #withFunction(ToDoubleFunction)}.
  26.  *
  27.  * <p>For example, to create a simple date detector use:
  28.  *
  29.  * <pre>
  30.  * FunctionalDetector d = new FunctionalDetector()
  31.  *     .withGFunction((s) -&gt; s.getDate().durationFrom(triggerDate))
  32.  *     .withMaxCheck(1e10);
  33.  * </pre>
  34.  *
  35.  * @author Evan Ward
  36.  * @since 9.2
  37.  */
  38. public class FunctionalDetector extends AbstractDetector<FunctionalDetector> {

  39.     /** The g function. */
  40.     private final ToDoubleFunction<SpacecraftState> function;

  41.     /**
  42.      * Create an event detector with the default values. These are {@link
  43.      * #DEFAULT_MAX_CHECK}, {@link #DEFAULT_THRESHOLD}, {@link #DEFAULT_MAX_ITER}, {@link
  44.      * ContinueOnEvent}, and a g function that is identically unity.
  45.      */
  46.     public FunctionalDetector() {
  47.         this(EventDetectionSettings.getDefaultEventDetectionSettings(), new ContinueOnEvent(), value -> 1.0);
  48.     }

  49.     /**
  50.      * Private constructor.
  51.      *
  52.      * @param detectionSettings event detection settings
  53.      * @param handler   event handler to call at event occurrences
  54.      * @param function  the switching function.
  55.      * @since 13.0
  56.      */
  57.     protected FunctionalDetector(final EventDetectionSettings detectionSettings,
  58.                                  final EventHandler handler,
  59.                                  final ToDoubleFunction<SpacecraftState> function) {
  60.         super(detectionSettings, handler);
  61.         this.function = function;
  62.     }


  63.     @Override
  64.     public double g(final SpacecraftState s) {
  65.         return function.applyAsDouble(s);
  66.     }

  67.     @Override
  68.     protected FunctionalDetector create(final EventDetectionSettings detectionSettings, final EventHandler newHandler) {

  69.         return new FunctionalDetector(detectionSettings, newHandler, function);
  70.     }

  71.     /**
  72.      * Create a new event detector with a new g function, keeping all other attributes the
  73.      * same. It is recommended to use {@link #withMaxCheck(AdaptableInterval)} and {@link
  74.      * #withThreshold(double)} to set appropriate values for this g function.
  75.      *
  76.      * @param newGFunction the new g function.
  77.      * @return a new detector with the new g function.
  78.      */
  79.     public FunctionalDetector withFunction(final ToDoubleFunction<SpacecraftState> newGFunction) {
  80.         return new FunctionalDetector(getDetectionSettings(), getHandler(), newGFunction);
  81.     }

  82.     /**
  83.      * Get the switching function.
  84.      *
  85.      * @return the function used in {@link #g(SpacecraftState)}.
  86.      */
  87.     public ToDoubleFunction<SpacecraftState> getFunction() {
  88.         return function;
  89.     }

  90. }