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  
18  package org.orekit.propagation.events;
19  
20  import org.hipparchus.CalculusFieldElement;
21  import org.hipparchus.Field;
22  import org.orekit.propagation.FieldSpacecraftState;
23  import org.orekit.propagation.events.handlers.FieldContinueOnEvent;
24  import org.orekit.propagation.events.handlers.FieldEventHandler;
25  import org.orekit.propagation.events.intervals.DateDetectionAdaptableIntervalFactory;
26  import org.orekit.propagation.events.intervals.FieldAdaptableInterval;
27  import org.orekit.time.FieldAbsoluteDate;
28  import org.orekit.time.TimeInterval;
29  
30  
31  /**
32   * Detector for time intervals. Positive whenever the date is inside, negative otherwise.
33   *
34   * @author Romain Serra
35   * @since 13.1
36   * @see TimeInterval
37   * @see TimeIntervalDetector
38   */
39  public class FieldTimeIntervalDetector<T extends CalculusFieldElement<T>>
40          extends FieldAbstractDetector<FieldTimeIntervalDetector<T>, T> {
41  
42      /** Time interval for detection. */
43      private final TimeInterval timeInterval;
44  
45      /**
46       * Constructor with default detection settings and handler.
47       * @param field field
48       * @param timeInterval time interval
49       */
50      public FieldTimeIntervalDetector(final Field<T> field, final TimeInterval timeInterval) {
51          this(getDefaultDetectionSettings(field, timeInterval), new FieldContinueOnEvent<>(), timeInterval);
52      }
53  
54      /**
55       * Constructor.
56       * @param detectionSettings event detection settings
57       * @param handler event handler
58       * @param timeInterval time interval
59       */
60      public FieldTimeIntervalDetector(final FieldEventDetectionSettings<T> detectionSettings,
61                                       final FieldEventHandler<T> handler,
62                                       final TimeInterval timeInterval) {
63          super(detectionSettings, handler);
64          this.timeInterval = timeInterval;
65      }
66  
67      /**
68       * Get the default detection settings.
69       * @param field field
70       * @param timeInterval time interval
71       * @return default detection settings
72       * @param <W> field type
73       */
74      private static <W extends CalculusFieldElement<W>> FieldEventDetectionSettings<W> getDefaultDetectionSettings(final Field<W> field,
75                                                                                                                    final TimeInterval timeInterval) {
76          final FieldAbsoluteDate<W> fieldStartDate = new FieldAbsoluteDate<>(field, timeInterval.getStartDate());
77          final FieldAbsoluteDate<W> fieldEndDate = new FieldAbsoluteDate<>(field, timeInterval.getEndDate());
78          final FieldAdaptableInterval<W> adaptableInterval = DateDetectionAdaptableIntervalFactory
79                  .getDatesDetectionFieldInterval(fieldStartDate, fieldEndDate);
80          final W fieldThreshold = field.getZero().newInstance(DateDetector.DEFAULT_THRESHOLD);
81          return new FieldEventDetectionSettings<>(adaptableInterval, fieldThreshold, DEFAULT_MAX_ITER);
82      }
83  
84      /**
85       * Getter for the time interval.
86       * @return interval
87       */
88      public TimeInterval getTimeInterval() {
89          return timeInterval;
90      }
91  
92      @Override
93      protected FieldTimeIntervalDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
94                                                   final FieldEventHandler<T> newHandler) {
95          return new FieldTimeIntervalDetector<>(detectionSettings, newHandler, timeInterval);
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public boolean dependsOnTimeOnly() {
101         return true;
102     }
103 
104     @Override
105     public T g(final FieldSpacecraftState<T> s) {
106         final FieldAbsoluteDate<T> date = s.getDate();
107         return (date.durationFrom(timeInterval.getStartDate())).multiply(date.durationFrom(timeInterval.getEndDate())).negate();
108     }
109 }