1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.propagation.events;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22
23 import org.hipparchus.ode.events.Action;
24 import org.orekit.errors.OrekitIllegalArgumentException;
25 import org.orekit.errors.OrekitMessages;
26 import org.orekit.propagation.SpacecraftState;
27 import org.orekit.propagation.events.handlers.EventHandler;
28 import org.orekit.propagation.events.handlers.StopOnEvent;
29 import org.orekit.time.AbsoluteDate;
30 import org.orekit.time.TimeStamped;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class DateDetector extends AbstractDetector<DateDetector> implements TimeStamped {
48
49
50
51
52 public static final double DEFAULT_MAX_CHECK = 1.0e10;
53
54
55
56
57 public static final double DEFAULT_MIN_GAP = 1.0;
58
59
60
61
62 public static final double DEFAULT_THRESHOLD = 1.0e-13;
63
64
65
66
67 private final double minGap;
68
69
70 private AbsoluteDate gDate;
71
72
73 private final ArrayList<EventDate> eventDateList;
74
75
76 private int currentIndex;
77
78
79
80
81
82
83
84
85 public DateDetector(final TimeStamped... dates) {
86 this(AdaptableInterval.of(DEFAULT_MAX_CHECK), DEFAULT_THRESHOLD, DEFAULT_MAX_ITER, new StopOnEvent(), DEFAULT_MIN_GAP, dates);
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 protected DateDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
104 final EventHandler handler, final double minGap, final TimeStamped... dates) {
105 super(maxCheck, threshold, maxIter, handler);
106 this.currentIndex = -1;
107 this.gDate = null;
108 this.eventDateList = new ArrayList<DateDetector.EventDate>(dates.length);
109 for (final TimeStamped ts : dates) {
110 addEventDate(ts.getDate());
111 }
112 this.minGap = minGap;
113 }
114
115
116
117
118
119
120
121 public DateDetector withMinGap(final double newMinGap) {
122 return new DateDetector(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(),
123 getHandler(), newMinGap,
124 eventDateList.toArray(new EventDate[eventDateList.size()]));
125 }
126
127
128 @Override
129 protected DateDetector create(final AdaptableInterval newMaxCheck, final double newThreshold,
130 final int newMaxIter, final EventHandler newHandler) {
131 return new DateDetector(newMaxCheck, newThreshold, newMaxIter, newHandler, minGap,
132 eventDateList.toArray(new EventDate[eventDateList.size()]));
133 }
134
135
136
137
138
139 public List<TimeStamped> getDates() {
140 return Collections.unmodifiableList(eventDateList);
141 }
142
143
144
145
146
147
148 public double g(final SpacecraftState s) {
149 gDate = s.getDate();
150 if (currentIndex < 0) {
151 return -1.0;
152 } else {
153 final EventDate event = getClosest(gDate);
154 return event.isgIncrease() ? gDate.durationFrom(event.getDate()) : event.getDate().durationFrom(gDate);
155 }
156 }
157
158
159
160
161 public AbsoluteDate getDate() {
162 return currentIndex < 0 ? null : eventDateList.get(currentIndex).getDate();
163 }
164
165
166
167
168
169
170
171
172
173
174
175 public void addEventDate(final AbsoluteDate target) throws IllegalArgumentException {
176 final boolean increasing;
177 if (currentIndex < 0) {
178 increasing = (gDate == null) ? true : target.durationFrom(gDate) > 0.0;
179 currentIndex = 0;
180 eventDateList.add(new EventDate(target, increasing));
181 } else {
182 final int lastIndex = eventDateList.size() - 1;
183 final AbsoluteDate firstDate = eventDateList.get(0).getDate();
184 final AbsoluteDate lastDate = eventDateList.get(lastIndex).getDate();
185 if (firstDate.durationFrom(target) > minGap) {
186 increasing = !eventDateList.get(0).isgIncrease();
187 eventDateList.add(0, new EventDate(target, increasing));
188 currentIndex++;
189 } else if (target.durationFrom(lastDate) > minGap) {
190 increasing = !eventDateList.get(lastIndex).isgIncrease();
191 eventDateList.add(new EventDate(target, increasing));
192 } else {
193 throw new OrekitIllegalArgumentException(OrekitMessages.EVENT_DATE_TOO_CLOSE,
194 target,
195 firstDate,
196 lastDate,
197 minGap,
198 firstDate.durationFrom(target),
199 target.durationFrom(lastDate));
200 }
201 }
202 }
203
204
205
206
207
208 private EventDate getClosest(final AbsoluteDate target) {
209 final double dt = target.durationFrom(eventDateList.get(currentIndex).getDate());
210 if (dt < 0.0 && currentIndex > 0) {
211 boolean found = false;
212 while (currentIndex > 0 && !found) {
213 if (target.durationFrom(eventDateList.get(currentIndex - 1).getDate()) < eventDateList.get(currentIndex).getDate().durationFrom(target)) {
214 currentIndex--;
215 } else {
216 found = true;
217 }
218 }
219 } else if (dt > 0.0 && currentIndex < eventDateList.size() - 1) {
220 final int maxIndex = eventDateList.size() - 1;
221 boolean found = false;
222 while (currentIndex < maxIndex && !found) {
223 if (target.durationFrom(eventDateList.get(currentIndex + 1).getDate()) > eventDateList.get(currentIndex).getDate().durationFrom(target)) {
224 currentIndex++;
225 } else {
226 found = true;
227 }
228 }
229 }
230 return eventDateList.get(currentIndex);
231 }
232
233
234 private static class EventDate implements TimeStamped {
235
236
237 private final AbsoluteDate eventDate;
238
239
240 private final boolean gIncrease;
241
242
243
244
245
246 EventDate(final AbsoluteDate date, final boolean increase) {
247 this.eventDate = date;
248 this.gIncrease = increase;
249 }
250
251
252
253
254 public AbsoluteDate getDate() {
255 return eventDate;
256 }
257
258
259
260
261 public boolean isgIncrease() {
262 return gIncrease;
263 }
264
265 }
266
267 }