AbstractDetector.java

  1. /* Copyright 2002-2025 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.propagation.events;

  18. import org.orekit.errors.OrekitException;
  19. import org.orekit.errors.OrekitMessages;
  20. import org.orekit.propagation.SpacecraftState;
  21. import org.orekit.propagation.events.handlers.EventHandler;
  22. import org.orekit.propagation.events.intervals.AdaptableInterval;
  23. import org.orekit.time.AbsoluteDate;

  24. /** Common parts shared by several orbital events finders.
  25.  * @param <T> type of the detector
  26.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  27.  * @author Luc Maisonobe
  28.  */
  29. public abstract class AbstractDetector<T extends AbstractDetector<T>> implements EventDetector {

  30.     /** Default maximum checking interval (s). */
  31.     public static final double DEFAULT_MAX_CHECK = EventDetectionSettings.DEFAULT_MAX_CHECK;

  32.     /** Default convergence threshold (s). */
  33.     public static final double DEFAULT_THRESHOLD = EventDetectionSettings.DEFAULT_THRESHOLD;

  34.     /** Default maximum number of iterations in the event time search. */
  35.     public static final int DEFAULT_MAX_ITER = EventDetectionSettings.DEFAULT_MAX_ITER;

  36.     /** Detection settings. */
  37.     private final EventDetectionSettings eventDetectionSettings;

  38.     /** Default handler for event overrides. */
  39.     private final EventHandler handler;

  40.     /** Propagation direction. */
  41.     private boolean forward;

  42.     /** Build a new instance.
  43.      * @param maxCheck maximum checking interval, must be strictly positive (s)
  44.      * @param threshold convergence threshold (s)
  45.      * @param maxIter maximum number of iterations in the event time search
  46.      * @param handler event handler to call at event occurrences
  47.      */
  48.     protected AbstractDetector(final double maxCheck, final double threshold, final int maxIter,
  49.                                final EventHandler handler) {
  50.         this(new EventDetectionSettings(maxCheck, threshold, maxIter), handler);
  51.     }

  52.     /** Build a new instance.
  53.      * @param detectionSettings event detection settings
  54.      * @param handler event handler to call at event occurrences
  55.      * @since 12.2
  56.      */
  57.     protected AbstractDetector(final EventDetectionSettings detectionSettings, final EventHandler handler) {
  58.         checkStrictlyPositive(detectionSettings.getThreshold());
  59.         this.eventDetectionSettings = detectionSettings;
  60.         this.handler   = handler;
  61.         this.forward   = true;
  62.     }

  63.     /**
  64.      * Check if propagation is forward or not.
  65.      * @param state initial state
  66.      * @param targetDate target propagation date
  67.      * @return forward flag
  68.      * @since 13.0
  69.      */
  70.     public static boolean checkIfForward(final SpacecraftState state, final AbsoluteDate targetDate) {
  71.         return targetDate.durationFrom(state.getDate()) >= 0.0;
  72.     }

  73.     /** Check value is strictly positive.
  74.      * @param value value to check
  75.      * @exception OrekitException if value is not strictly positive
  76.      * @since 11.2
  77.      */
  78.     private void checkStrictlyPositive(final double value) throws OrekitException {
  79.         if (value <= 0.0) {
  80.             throw new OrekitException(OrekitMessages.NOT_STRICTLY_POSITIVE, value);
  81.         }
  82.     }

  83.     /**
  84.      * {@inheritDoc}
  85.      *
  86.      * <p> This implementation sets the direction of propagation and initializes the event
  87.      * handler. If a subclass overrides this method it should call {@code
  88.      * super.init(s0, t)}.
  89.      */
  90.     @Override
  91.     public void init(final SpacecraftState s0,
  92.                      final AbsoluteDate t) {
  93.         EventDetector.super.init(s0, t);
  94.         forward = checkIfForward(s0, t);
  95.     }

  96.     /** {@inheritDoc} */
  97.     @Override
  98.     public EventDetectionSettings getDetectionSettings() {
  99.         return eventDetectionSettings;
  100.     }

  101.     /**
  102.      * Set up the maximum checking interval.
  103.      * <p>
  104.      * This will override a maximum checking interval if it has been configured previously.
  105.      * </p>
  106.      * @param newMaxCheck maximum checking interval (s)
  107.      * @return a new detector with updated configuration (the instance is not changed)
  108.      * @since 6.1
  109.      */
  110.     public T withMaxCheck(final double newMaxCheck) {
  111.         return withMaxCheck(AdaptableInterval.of(newMaxCheck));
  112.     }

  113.     /**
  114.      * Set up the maximum checking interval.
  115.      * <p>
  116.      * This will override a maximum checking interval if it has been configured previously.
  117.      * </p>
  118.      * @param newMaxCheck maximum checking interval (s)
  119.      * @return a new detector with updated configuration (the instance is not changed)
  120.      * @since 12.0
  121.      */
  122.     public T withMaxCheck(final AdaptableInterval newMaxCheck) {
  123.         return withDetectionSettings(new EventDetectionSettings(newMaxCheck, getThreshold(), getMaxIterationCount()));
  124.     }

  125.     /**
  126.      * Set up the maximum number of iterations in the event time search.
  127.      * <p>
  128.      * This will override a number of iterations if it has been configured previously.
  129.      * </p>
  130.      * @param newMaxIter maximum number of iterations in the event time search
  131.      * @return a new detector with updated configuration (the instance is not changed)
  132.      * @since 6.1
  133.      */
  134.     public T withMaxIter(final int newMaxIter) {
  135.         return withDetectionSettings(new EventDetectionSettings(getMaxCheckInterval(), getThreshold(), newMaxIter));
  136.     }

  137.     /**
  138.      * Set up the convergence threshold.
  139.      * <p>
  140.      * This will override a convergence threshold if it has been configured previously.
  141.      * </p>
  142.      * @param newThreshold convergence threshold (s)
  143.      * @return a new detector with updated configuration (the instance is not changed)
  144.      * @since 6.1
  145.      */
  146.     public T withThreshold(final double newThreshold) {
  147.         return withDetectionSettings(new EventDetectionSettings(getMaxCheckInterval(), newThreshold, getMaxIterationCount()));
  148.     }

  149.     /**
  150.      * Set up the event detection settings.
  151.      * <p>
  152.      * This will override settings previously configured.
  153.      * </p>
  154.      * @param newSettings new event detection settings
  155.      * @return a new detector with updated configuration (the instance is not changed)
  156.      * @since 12.2
  157.      */
  158.     public T withDetectionSettings(final EventDetectionSettings newSettings) {
  159.         return create(new EventDetectionSettings(newSettings.getMaxCheckInterval(), newSettings.getThreshold(), newSettings.getMaxIterationCount()),
  160.                 getHandler());
  161.     }

  162.     /**
  163.      * Set up the event handler to call at event occurrences.
  164.      * <p>
  165.      * This will override a handler if it has been configured previously.
  166.      * </p>
  167.      * @param newHandler event handler to call at event occurrences
  168.      * @return a new detector with updated configuration (the instance is not changed)
  169.      * @since 6.1
  170.      */
  171.     public T withHandler(final EventHandler newHandler) {
  172.         return create(getDetectionSettings(), newHandler);
  173.     }

  174.     /** {@inheritDoc} */
  175.     @Override
  176.     public EventHandler getHandler() {
  177.         return handler;
  178.     }

  179.     /** Build a new instance.
  180.      * @param detectionSettings detection settings
  181.      * @param newHandler event handler to call at event occurrences
  182.      * @return a new instance of the appropriate sub-type
  183.      * @since 12.2
  184.      */
  185.     protected abstract T create(EventDetectionSettings detectionSettings, EventHandler newHandler);

  186.     /** Check if the current propagation is forward or backward.
  187.      * @return true if the current propagation is forward
  188.      * @since 7.2
  189.      */
  190.     public boolean isForward() {
  191.         return forward;
  192.     }

  193. }