FieldTimeIntervalDetector.java

  1. /* Copyright 2022-2025 Romain Serra
  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.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.orekit.propagation.FieldSpacecraftState;
  21. import org.orekit.propagation.events.handlers.FieldContinueOnEvent;
  22. import org.orekit.propagation.events.handlers.FieldEventHandler;
  23. import org.orekit.propagation.events.intervals.DateDetectionAdaptableIntervalFactory;
  24. import org.orekit.propagation.events.intervals.FieldAdaptableInterval;
  25. import org.orekit.time.FieldAbsoluteDate;
  26. import org.orekit.time.TimeInterval;


  27. /**
  28.  * Detector for time intervals. Positive whenever the date is inside, negative otherwise.
  29.  *
  30.  * @author Romain Serra
  31.  * @since 13.1
  32.  * @see TimeInterval
  33.  * @see TimeIntervalDetector
  34.  */
  35. public class FieldTimeIntervalDetector<T extends CalculusFieldElement<T>>
  36.         extends FieldAbstractDetector<FieldTimeIntervalDetector<T>, T> {

  37.     /** Time interval for detection. */
  38.     private final TimeInterval timeInterval;

  39.     /**
  40.      * Constructor with default detection settings and handler.
  41.      * @param field field
  42.      * @param timeInterval time interval
  43.      */
  44.     public FieldTimeIntervalDetector(final Field<T> field, final TimeInterval timeInterval) {
  45.         this(getDefaultDetectionSettings(field, timeInterval), new FieldContinueOnEvent<>(), timeInterval);
  46.     }

  47.     /**
  48.      * Constructor.
  49.      * @param detectionSettings event detection settings
  50.      * @param handler event handler
  51.      * @param timeInterval time interval
  52.      */
  53.     public FieldTimeIntervalDetector(final FieldEventDetectionSettings<T> detectionSettings,
  54.                                      final FieldEventHandler<T> handler,
  55.                                      final TimeInterval timeInterval) {
  56.         super(detectionSettings, handler);
  57.         this.timeInterval = timeInterval;
  58.     }

  59.     /**
  60.      * Get the default detection settings.
  61.      * @param field field
  62.      * @param timeInterval time interval
  63.      * @return default detection settings
  64.      * @param <W> field type
  65.      */
  66.     private static <W extends CalculusFieldElement<W>> FieldEventDetectionSettings<W> getDefaultDetectionSettings(final Field<W> field,
  67.                                                                                                                   final TimeInterval timeInterval) {
  68.         final FieldAbsoluteDate<W> fieldStartDate = new FieldAbsoluteDate<>(field, timeInterval.getStartDate());
  69.         final FieldAbsoluteDate<W> fieldEndDate = new FieldAbsoluteDate<>(field, timeInterval.getEndDate());
  70.         final FieldAdaptableInterval<W> adaptableInterval = DateDetectionAdaptableIntervalFactory
  71.                 .getDatesDetectionFieldInterval(fieldStartDate, fieldEndDate);
  72.         final W fieldThreshold = field.getZero().newInstance(DateDetector.DEFAULT_THRESHOLD);
  73.         return new FieldEventDetectionSettings<>(adaptableInterval, fieldThreshold, DEFAULT_MAX_ITER);
  74.     }

  75.     /**
  76.      * Getter for the time interval.
  77.      * @return interval
  78.      */
  79.     public TimeInterval getTimeInterval() {
  80.         return timeInterval;
  81.     }

  82.     @Override
  83.     protected FieldTimeIntervalDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
  84.                                                  final FieldEventHandler<T> newHandler) {
  85.         return new FieldTimeIntervalDetector<>(detectionSettings, newHandler, timeInterval);
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public boolean dependsOnTimeOnly() {
  90.         return true;
  91.     }

  92.     @Override
  93.     public T g(final FieldSpacecraftState<T> s) {
  94.         final FieldAbsoluteDate<T> date = s.getDate();
  95.         return (date.durationFrom(timeInterval.getStartDate())).multiply(date.durationFrom(timeInterval.getEndDate())).negate();
  96.     }
  97. }