🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
TimeInterval.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "TimePoint.hpp"
25#include "AstUtil/Logger.hpp"
26#include <string>
27#include <limits>
28#include <vector>
29#include <cmath>
30
31AST_NAMESPACE_BEGIN
32
33
34
41class TimeInterval;
42
47AST_CORE_CAPI errc_t aTimeIntervalFormat(const TimeInterval& interval, std::string& strStart, std::string& strEnd);
48
49
54AST_CORE_CAPI errc_t aTimeIntervalParse(StringView strStart, StringView strEnd, TimeInterval& interval);
55
56
60{
61public:
62 static TimeInterval Parse(StringView strStart, StringView strEnd)
63 {
64 TimeInterval interval;
65 aTimeIntervalParse(strStart, strEnd, interval);
66 return interval;
67 }
68
69 TimeInterval() = default;
70 ~TimeInterval() = default;
71
75 TimeInterval(const TimePoint& start, const TimePoint& stop)
76 : start_(start), stop_(stop) {}
77
82 TimeInterval(const TimePoint& epoch, double start, double stop)
83 : start_(epoch + start), stop_(epoch + stop) {}
84
86 const TimePoint& start() const{return start_;}
87
89 const TimePoint& getStart() const{return start_;}
90
92 void setStart(const TimePoint& start){start_ = start;}
93
95 const TimePoint& stop() const{return stop_;}
96
98 const TimePoint& getStop() const{return stop_;}
99
101 void setStop(const TimePoint& stop){stop_ = stop;}
102
106 void setStartStop(const TimePoint& start, const TimePoint& stop){
107 start_ = start;
108 stop_ = stop;
109 }
110
115 void setStartStop(const TimePoint& epoch, double start, double stop){
116 start_ = epoch + start;
117 stop_ = epoch + stop;
118 }
119
122 {
123 start_ = {0, -std::numeric_limits<double>::infinity()};
124 stop_ = {0, +std::numeric_limits<double>::infinity()};
125 }
128 void setZero()
129 {
130 start_ = {0, 0.0};
131 stop_ = {0, 0.0};
132 }
133public:
135 double duration() const{return stop_ - start_;}
136
137public:
139 std::string toString() const{
140 std::string strStart, strEnd;
141 aTimeIntervalFormat(*this, strStart, strEnd);
142 return strStart + " - " + strEnd;
143 }
144public:
150 AST_CORE_API
151 errc_t discrete(const TimePoint& epoch, double step, std::vector<double>& times) const;
152
157 AST_CORE_API
158 errc_t discrete(double step, std::vector<TimePoint>& times) const;
159
164 AST_CORE_API
165 errc_t discrete(double step, TimeList& times) const;
166
167 class DiscreteTimePointRange;
168 class DiscreteEpochSecondRange;
169
173 DiscreteTimePointRange discrete(double step) const;
174
179 DiscreteEpochSecondRange discrete(const TimePoint& epoch, double step) const;
180public:
185 errc_t merge(const TimeInterval& other);
186private:
187 TimePoint start_;
188 TimePoint stop_;
189};
190
191
194public:
195 DiscreteTimePointRange(const TimeInterval& interval, double step, size_t n)
196 : interval_(interval), step_(step), n_(n) {}
197
198 class iterator {
199 public:
200 using iterator_category = std::input_iterator_tag;
201 using value_type = TimePoint;
202 using difference_type = ptrdiff_t;
203 using pointer = const TimePoint*;
204 using reference = const TimePoint&;
205
206 iterator() = default;
207 iterator(const DiscreteTimePointRange* range, size_t idx)
208 : range_(range), idx_(idx), value_() {}
209
210 reference operator*() const {
211 if (idx_ == range_->n_ - 1) {
212 value_ = range_->interval_.stop();
213 } else {
214 value_ = range_->interval_.start() + range_->step_ * idx_;
215 }
216 return value_;
217 }
218
219 iterator& operator++() { ++idx_; return *this; }
220 iterator operator++(int) { auto tmp = *this; ++*this; return tmp; }
221
222 bool operator==(const iterator& other) const { return idx_ == other.idx_; }
223 bool operator!=(const iterator& other) const { return !(*this == other); }
224
225 private:
226 const DiscreteTimePointRange* range_ = nullptr;
227 size_t idx_ = 0;
228 mutable TimePoint value_{};
229 };
230
231 iterator begin() const { return iterator(this, 0); }
232 iterator end() const { return iterator(this, n_); }
233 size_t size() const { return n_; }
234
235private:
236 TimeInterval interval_;
237 double step_;
238 size_t n_;
239};
240
243public:
244 DiscreteEpochSecondRange(double offset, double step, double stopOffset, size_t n)
245 : offset_(offset), step_(step), stopOffset_(stopOffset), n_(n) {}
246
247 class iterator {
248 public:
249 using iterator_category = std::input_iterator_tag;
250 using value_type = double;
251 using difference_type = ptrdiff_t;
252 using pointer = const double*;
253 using reference = const double&;
254
255 iterator() = default;
256 iterator(const DiscreteEpochSecondRange* range, size_t idx)
257 : range_(range), idx_(idx), value_() {}
258
259 reference operator*() const {
260 if (idx_ == range_->n_ - 1) {
261 value_ = range_->stopOffset_;
262 } else {
263 value_ = range_->offset_ + range_->step_ * idx_;
264 }
265 return value_;
266 }
267
268 iterator& operator++() { ++idx_; return *this; }
269 iterator operator++(int) { auto tmp = *this; ++*this; return tmp; }
270
271 bool operator==(const iterator& other) const { return idx_ == other.idx_; }
272 bool operator!=(const iterator& other) const { return !(*this == other); }
273
274 private:
275 const DiscreteEpochSecondRange* range_ = nullptr;
276 size_t idx_ = 0;
277 mutable double value_{0.0};
278 };
279
280 iterator begin() const { return iterator(this, 0); }
281 iterator end() const { return iterator(this, n_); }
282 size_t size() const { return n_; }
283private:
284 double offset_;
285 double step_;
286 double stopOffset_;
287 size_t n_;
288};
289
290
291inline TimeInterval::DiscreteTimePointRange TimeInterval::discrete(double step) const
292{
293 double dur = duration();
294 if (step <= 0.0 || dur <= 0.0) {
295 return DiscreteTimePointRange(*this, step, 0);
296 }
297 size_t n = static_cast<size_t>(std::ceil(dur / step)) + 1;
298 return DiscreteTimePointRange(*this, step, n);
299}
300
301inline TimeInterval::DiscreteEpochSecondRange TimeInterval::discrete(const TimePoint& epoch, double step) const
302{
303 double dur = duration();
304 if (step <= 0.0 || dur <= 0.0) {
305 return DiscreteEpochSecondRange(0.0, step, 0.0, 0);
306 }
307 size_t n = static_cast<size_t>(std::ceil(dur / step)) + 1;
308 double offset = getStart() - epoch;
309 double stopOffset = getStop() - epoch;
310 return DiscreteEpochSecondRange(offset, step, stopOffset, n);
311}
312
313inline errc_t TimeInterval::merge(const TimeInterval &other)
314{
315 const TimePoint& thisStart = start();
316 const TimePoint& otherStart = other.start();
317 TimePoint thisStop = stop();
318 TimePoint otherStop = other.stop();
319
320 if (thisStart.durationFrom(otherStop) > 0 || otherStart.durationFrom(thisStop) > 0)
321 {
322 aError("merge time interval failed, no overlap");
323 return eErrorInvalidParam;
324 }
325
326 const TimePoint& mergedStart = (thisStart.durationFrom(otherStart) <= 0) ? thisStart : otherStart;
327 const TimePoint& mergedStop = (thisStop.durationFrom(otherStop) >= 0) ? thisStop : otherStop;
328
329 setStartStop(mergedStart, mergedStop);
330 return eNoError;
331}
332
337AST_NAMESPACE_END
绝对时间点
离散化历元秒范围
定义 TimeInterval.hpp:242
离散化时间点范围
定义 TimeInterval.hpp:193
时间区间
定义 TimeInterval.hpp:60
void setInfinite()
设置时间区间为无限时间区间
定义 TimeInterval.hpp:121
double duration() const
时间区间的持续时间(秒)
定义 TimeInterval.hpp:135
TimeInterval(const TimePoint &epoch, double start, double stop)
构造函数
定义 TimeInterval.hpp:82
const TimePoint & getStart() const
时间区间的开始时间点
定义 TimeInterval.hpp:89
void setZero()
设置时间区间为零时间区间
定义 TimeInterval.hpp:128
void setStart(const TimePoint &start)
设置时间区间的开始时间点
定义 TimeInterval.hpp:92
void setStartStop(const TimePoint &epoch, double start, double stop)
设置时间区间的开始时间点和结束时间点
定义 TimeInterval.hpp:115
const TimePoint & stop() const
时间区间的结束时间点
定义 TimeInterval.hpp:95
const TimePoint & start() const
时间区间的开始时间点
定义 TimeInterval.hpp:86
std::string toString() const
将时间区间转换为字符串
定义 TimeInterval.hpp:139
const TimePoint & getStop() const
时间区间的结束时间点
定义 TimeInterval.hpp:98
TimeInterval(const TimePoint &start, const TimePoint &stop)
构造函数
定义 TimeInterval.hpp:75
void setStop(const TimePoint &stop)
设置时间区间的结束时间点
定义 TimeInterval.hpp:101
void setStartStop(const TimePoint &start, const TimePoint &stop)
设置时间区间的开始时间点和结束时间点
定义 TimeInterval.hpp:106
时间点列表
定义 TimeList.hpp:49
绝对时间点
定义 TimePoint.hpp:107
double durationFrom(const TimePoint &other) const
计算与另一个时间点的时间差(秒数)
定义 TimePoint.hpp:236
errc_t aTimeIntervalFormat(const TimeInterval &interval, std::string &strStart, std::string &strStop)
将时间区间格式化为字符串
定义 TimeInterval.cpp:27
errc_t aTimeIntervalParse(StringView strStart, StringView strStop, TimeInterval &interval)
从字符串解析时间区间
定义 TimeInterval.cpp:37