🛰️航天仿真算法库 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 this->setStartStop(start, stop);
77 }
78
83 TimeInterval(const TimePoint& epoch, double start, double stop)
84 {
85 this->setStartStop(epoch, start, stop);
86 }
87
89 const TimePoint& start() const{return reinterpret_cast<const TimePoint&>(*this);}
90
92 const TimePoint& getStart() const{return reinterpret_cast<const TimePoint&>(*this);}
93
95 TimePoint stop() const{return {epoch_, stop_};}
96
98 TimePoint getStop() const{return {epoch_, stop_};}
99
100
104 void setStartStop(const TimePoint& start, const TimePoint& stop){
105 epoch_ = start.integerPart();
106 start_ = start.fractionalPart();
107 stop_ = stop.fractionalPart() + (stop.integerPart() - start.integerPart());
108 }
109
114 void setStartStop(const TimePoint& epoch, double start, double stop){
115 epoch_ = epoch.integerPart();
116 start_ = epoch.fractionalPart() + start;
117 stop_ = epoch.fractionalPart() + stop;
118 }
119
122 {
123 epoch_ = 0;
124 start_ = -std::numeric_limits<double>::infinity();
125 stop_ = +std::numeric_limits<double>::infinity();
126 }
129 void setZero()
130 {
131 epoch_ = 0;
132 start_ = 0.0;
133 stop_ = 0.0;
134 }
135public:
137 double duration() const{return stop_ - start_;}
138
139public:
141 std::string toString() const{
142 std::string strStart, strEnd;
143 aTimeIntervalFormat(*this, strStart, strEnd);
144 return strStart + " - " + strEnd;
145 }
146public:
152 AST_CORE_API
153 errc_t discrete(const TimePoint& epoch, double step, std::vector<double>& times) const;
154
159 AST_CORE_API
160 errc_t discrete(double step, std::vector<TimePoint>& times) const;
161
162 class DiscreteTimePointRange;
163 class DiscreteEpochSecondRange;
164
168 DiscreteTimePointRange discrete(double step) const;
169
174 DiscreteEpochSecondRange discrete(const TimePoint& epoch, double step) const;
175public:
180 errc_t merge(const TimeInterval& other);
181private:
182 int64_t epoch_{0};
183 double start_{0};
184 double stop_{0};
185};
186
187
190public:
191 DiscreteTimePointRange(const TimeInterval& interval, double step, size_t n)
192 : interval_(interval), step_(step), n_(n) {}
193
194 class iterator {
195 public:
196 using iterator_category = std::input_iterator_tag;
197 using value_type = TimePoint;
198 using difference_type = ptrdiff_t;
199 using pointer = const TimePoint*;
200 using reference = const TimePoint&;
201
202 iterator() = default;
203 iterator(const DiscreteTimePointRange* range, size_t idx)
204 : range_(range), idx_(idx), value_() {}
205
206 reference operator*() const {
207 if (idx_ == range_->n_ - 1) {
208 value_ = range_->interval_.stop();
209 } else {
210 value_ = range_->interval_.start() + range_->step_ * idx_;
211 }
212 return value_;
213 }
214
215 iterator& operator++() { ++idx_; return *this; }
216 iterator operator++(int) { auto tmp = *this; ++*this; return tmp; }
217
218 bool operator==(const iterator& other) const { return idx_ == other.idx_; }
219 bool operator!=(const iterator& other) const { return !(*this == other); }
220
221 private:
222 const DiscreteTimePointRange* range_ = nullptr;
223 size_t idx_ = 0;
224 mutable TimePoint value_{};
225 };
226
227 iterator begin() const { return iterator(this, 0); }
228 iterator end() const { return iterator(this, n_); }
229 size_t size() const { return n_; }
230
231private:
232 TimeInterval interval_;
233 double step_;
234 size_t n_;
235};
236
239public:
240 DiscreteEpochSecondRange(double offset, double step, double stopOffset, size_t n)
241 : offset_(offset), step_(step), stopOffset_(stopOffset), n_(n) {}
242
243 class iterator {
244 public:
245 using iterator_category = std::input_iterator_tag;
246 using value_type = double;
247 using difference_type = ptrdiff_t;
248 using pointer = const double*;
249 using reference = const double&;
250
251 iterator() = default;
252 iterator(const DiscreteEpochSecondRange* range, size_t idx)
253 : range_(range), idx_(idx), value_() {}
254
255 reference operator*() const {
256 if (idx_ == range_->n_ - 1) {
257 value_ = range_->stopOffset_;
258 } else {
259 value_ = range_->offset_ + range_->step_ * idx_;
260 }
261 return value_;
262 }
263
264 iterator& operator++() { ++idx_; return *this; }
265 iterator operator++(int) { auto tmp = *this; ++*this; return tmp; }
266
267 bool operator==(const iterator& other) const { return idx_ == other.idx_; }
268 bool operator!=(const iterator& other) const { return !(*this == other); }
269
270 private:
271 const DiscreteEpochSecondRange* range_ = nullptr;
272 size_t idx_ = 0;
273 mutable double value_{0.0};
274 };
275
276 iterator begin() const { return iterator(this, 0); }
277 iterator end() const { return iterator(this, n_); }
278 size_t size() const { return n_; }
279private:
280 double offset_;
281 double step_;
282 double stopOffset_;
283 size_t n_;
284};
285
286
287inline TimeInterval::DiscreteTimePointRange TimeInterval::discrete(double step) const
288{
289 double dur = duration();
290 if (step <= 0.0 || dur <= 0.0) {
291 return DiscreteTimePointRange(*this, step, 0);
292 }
293 size_t n = static_cast<size_t>(std::ceil(dur / step));
294 return DiscreteTimePointRange(*this, step, n);
295}
296
297inline TimeInterval::DiscreteEpochSecondRange TimeInterval::discrete(const TimePoint& epoch, double step) const
298{
299 double dur = duration();
300 if (step <= 0.0 || dur <= 0.0) {
301 return DiscreteEpochSecondRange(0.0, step, 0.0, 0);
302 }
303 size_t n = static_cast<size_t>(std::ceil(dur / step));
304 double offset = getStart() - epoch;
305 double stopOffset = getStop() - epoch;
306 return DiscreteEpochSecondRange(offset, step, stopOffset, n);
307}
308
309inline errc_t TimeInterval::merge(const TimeInterval &other)
310{
311 const TimePoint& thisStart = start();
312 const TimePoint& otherStart = other.start();
313 TimePoint thisStop = stop();
314 TimePoint otherStop = other.stop();
315
316 if (thisStart.durationFrom(otherStop) > 0 || otherStart.durationFrom(thisStop) > 0)
317 {
318 aError("merge time interval failed, no overlap");
319 return eErrorInvalidParam;
320 }
321
322 const TimePoint& mergedStart = (thisStart.durationFrom(otherStart) <= 0) ? thisStart : otherStart;
323 const TimePoint& mergedStop = (thisStop.durationFrom(otherStop) >= 0) ? thisStop : otherStop;
324
325 setStartStop(mergedStart, mergedStop);
326 return eNoError;
327}
328
333AST_NAMESPACE_END
绝对时间点
离散化历元秒范围
定义 TimeInterval.hpp:238
离散化时间点范围
定义 TimeInterval.hpp:189
时间区间
定义 TimeInterval.hpp:60
void setInfinite()
设置时间区间为无限时间区间
定义 TimeInterval.hpp:121
TimePoint getStop() const
时间区间的结束时间点
定义 TimeInterval.hpp:98
double duration() const
时间区间的持续时间(秒)
定义 TimeInterval.hpp:137
TimeInterval(const TimePoint &epoch, double start, double stop)
构造函数
定义 TimeInterval.hpp:83
const TimePoint & getStart() const
时间区间的开始时间点
定义 TimeInterval.hpp:92
void setZero()
设置时间区间为零时间区间
定义 TimeInterval.hpp:129
void setStartStop(const TimePoint &epoch, double start, double stop)
设置时间区间的开始时间点和结束时间点
定义 TimeInterval.hpp:114
const TimePoint & start() const
时间区间的开始时间点
定义 TimeInterval.hpp:89
std::string toString() const
将时间区间转换为字符串
定义 TimeInterval.hpp:141
TimePoint stop() const
时间区间的结束时间点
定义 TimeInterval.hpp:95
TimeInterval(const TimePoint &start, const TimePoint &stop)
构造函数
定义 TimeInterval.hpp:75
void setStartStop(const TimePoint &start, const TimePoint &stop)
设置时间区间的开始时间点和结束时间点
定义 TimeInterval.hpp:104
绝对时间点
定义 TimePoint.hpp:106
double fractionalPart() const
时间点的小数秒数部分
定义 TimePoint.hpp:176
double durationFrom(const TimePoint &other) const
计算与另一个时间点的时间差(秒数)
定义 TimePoint.hpp:224
int64_t integerPart() const
时间点的整数秒数部分
定义 TimePoint.hpp:173
errc_t aTimeIntervalFormat(const TimeInterval &interval, std::string &strStart, std::string &strStop)
将时间区间格式化为字符串
定义 TimeInterval.cpp:26
errc_t aTimeIntervalParse(StringView strStart, StringView strStop, TimeInterval &interval)
从字符串解析时间区间
定义 TimeInterval.cpp:36