🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
TimeInterval.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "TimePoint.hpp"
25#include "TimePointRange.hpp"
26#include "DoubleRange.hpp"
27#include "Interval.hpp"
28#include "AstUtil/Logger.hpp"
29#include <string>
30#include <limits>
31#include <vector>
32#include <cmath>
33
34AST_NAMESPACE_BEGIN
35
36
37
44class TimeInterval;
45
51AST_CORE_CAPI errc_t aTimeIntervalFormat(const TimeInterval& interval, std::string& strStart, std::string& strEnd, int precision = kTimePointDefaultFormatPrecision);
52
53
58AST_CORE_CAPI errc_t aTimeIntervalParse(StringView strStart, StringView strEnd, TimeInterval& interval);
59
60
64{
65public:
66 static TimeInterval Parse(StringView strStart, StringView strEnd)
67 {
68 TimeInterval interval;
69 aTimeIntervalParse(strStart, strEnd, interval);
70 return interval;
71 }
72
75 return TimeInterval(TimePoint{0, +std::numeric_limits<double>::infinity()},
76 TimePoint{0, -std::numeric_limits<double>::infinity()});
77 }
78
79public:
81 : start_{}
82 , stop_{}
83 {}
84 ~TimeInterval() = default;
85
89 TimeInterval(const TimePoint& start, const TimePoint& stop)
90 : start_(start), stop_(stop) {}
91
96 TimeInterval(const TimePoint& epoch, double start, double stop)
97 : start_(epoch + start), stop_(epoch + stop) {}
98
100 const TimePoint& start() const{return start_;}
101
103 const TimePoint& getStart() const{return start_;}
104
106 void setStart(const TimePoint& start){start_ = start;}
107
109 const TimePoint& stop() const{return stop_;}
110
112 const TimePoint& getStop() const{return stop_;}
113
115 void setStop(const TimePoint& stop){stop_ = stop;}
116
120 void setBounds(const TimePoint& start, const TimePoint& stop){
121 start_ = start;
122 stop_ = stop;
123 }
124
129 void setBounds(const TimePoint& epoch, double start, double stop){
130 start_ = epoch + start;
131 stop_ = epoch + stop;
132 }
133
137 void setBounds(const TimePoint& epoch, const Interval& interval){
138 start_ = epoch + interval.start();
139 stop_ = epoch + interval.stop();
140 }
141
151 Interval toInterval(const TimePoint& epoch) const
152 {
153 return Interval{getStart() - epoch, getStop() - epoch};
154 }
155
157 void setWhole()
158 {
159 start_ = {0, -std::numeric_limits<double>::infinity()};
160 stop_ = {0, +std::numeric_limits<double>::infinity()};
161 }
163 void setEmpty()
164 {
165 start_ = {0, +std::numeric_limits<double>::infinity()};
166 stop_ = {0, -std::numeric_limits<double>::infinity()};
167 }
168public:
170 double duration() const{return stop_ - start_;}
171
173 bool isEmpty() const { return !(start_ <= stop_); }
175 bool isPoint() const { return start_ == stop_; }
177 bool isDegenerate() const { return isPoint(); }
179 bool isValid() const { return start_ <= stop_; }
181 bool isNonEmpty() const { return isValid(); }
183 bool contains(const TimePoint& t) const { return start_ <= t && t <= stop_; }
184
185public:
189 std::string toString(int precision = kTimePointDefaultFormatPrecision) const{
190 std::string strStart, strEnd;
191 aTimeIntervalFormat(*this, strStart, strEnd, precision);
192 return strStart + " - " + strEnd;
193 }
194public:
200 AST_CORE_API
201 errc_t discretize(const TimePoint& epoch, double step, std::vector<double>& times) const;
202
207 AST_CORE_API
208 errc_t discretize(double step, std::vector<TimePoint>& times) const;
209
214 AST_CORE_API
215 errc_t discretize(double step, TimeList& times) const;
216
220 TimePointRange discretize(double step) const;
221
226 DoubleRange discretize(const TimePoint& epoch, double step) const;
227
234 size_t discretizedCount(double step) const
235 {
236 double dur = duration();
237 if (step > 0 && dur >= 0)
238 {
239 return static_cast<size_t>(std::ceil(dur / step)) + 1;
240 }
241 // 如果存在nan,则duration()为nan,if 条件不成立,返回0
242 return 0;
243 }
244public:
249 AST_CORE_API
250 TimeInterval& unite(const TimeInterval& other);
251
255 AST_CORE_API
256 TimeInterval united(const TimeInterval& other) const;
257
262 AST_CORE_API
263 TimeInterval& intersect(const TimeInterval& other);
264
269 AST_CORE_API
270 TimeInterval intersected(const TimeInterval& other) const;
271
275 AST_CORE_API
276 bool intersects(const TimeInterval& other) const;
277
279 TimeInterval& operator|=(const TimeInterval& other) { return unite(other); }
280
282 TimeInterval operator|(const TimeInterval& other) const { return united(other); }
283
285 TimeInterval& operator&=(const TimeInterval& other) { return intersect(other); }
286
288 TimeInterval operator&(const TimeInterval& other) const { return intersected(other); }
289private:
290 TimePoint start_;
291 TimePoint stop_;
292};
293
294
295inline TimePointRange TimeInterval::discretize(double step) const
296{
297 double dur = duration();
298 if (step > 0 && dur >= 0)
299 {
300 size_t n = static_cast<size_t>(std::ceil(dur / step)) + 1;
301 return TimePointRange(start_, stop_, step, n);
302 }
303 return TimePointRange(start_, stop_, step, 0);
304}
305
306inline DoubleRange TimeInterval::discretize(const TimePoint& epoch, double step) const
307{
308 double dur = duration();
309 if (step > 0 && dur >= 0)
310 {
311 size_t n = static_cast<size_t>(std::ceil(dur / step)) + 1;
312 double start = getStart() - epoch;
313 double stop = getStop() - epoch;
314 return DoubleRange(start, stop, step, n);
315 }
316
317 return DoubleRange(0.0, 0.0, step, 0);
318}
319
324AST_NAMESPACE_END
按步长采样的相对秒范围
按步长采样的时间点范围
绝对时间点
按步长采样的相对秒范围(惰性可迭代)
定义 DoubleRange.hpp:41
相对时间区间
定义 Interval.hpp:42
时间区间
定义 TimeInterval.hpp:64
bool isValid() const
是否非空(start_ <= stop_,即点区间或有效区间)
定义 TimeInterval.hpp:179
Interval toInterval(const TimePoint &epoch) const
将绝对时间区间转换为相对于给定基准历元的相对时间区间
定义 TimeInterval.hpp:151
double duration() const
时间区间的持续时间(秒)
定义 TimeInterval.hpp:170
TimeInterval & operator|=(const TimeInterval &other)
原地并集(等价于 unite)
定义 TimeInterval.hpp:279
bool isNonEmpty() const
isValid() 的别名
定义 TimeInterval.hpp:181
static TimeInterval Empty()
空区间哨兵({+∞, -∞},IEEE 1788 惯例)
定义 TimeInterval.hpp:74
TimeInterval(const TimePoint &epoch, double start, double stop)
构造函数
定义 TimeInterval.hpp:96
const TimePoint & getStart() const
时间区间的开始时间点
定义 TimeInterval.hpp:103
void setBounds(const TimePoint &epoch, const Interval &interval)
设置时间区间的开始时间点和结束时间点
定义 TimeInterval.hpp:137
void setBounds(const TimePoint &epoch, double start, double stop)
设置时间区间的开始时间点和结束时间点
定义 TimeInterval.hpp:129
bool isDegenerate() const
isPoint() 的别名
定义 TimeInterval.hpp:177
TimeInterval operator&(const TimeInterval &other) const
交集(返回副本,等价于 intersected)
定义 TimeInterval.hpp:288
void setEmpty()
设置时间区间为空区间
定义 TimeInterval.hpp:163
void setStart(const TimePoint &start)
设置时间区间的开始时间点
定义 TimeInterval.hpp:106
bool isEmpty() const
是否为空区间(start_ > stop_,弱序否定;反向 / {+∞,-∞} 哨兵 / NaN → true)
定义 TimeInterval.hpp:173
bool contains(const TimePoint &t) const
是否包含绝对时间点 t(闭区间含端点)
定义 TimeInterval.hpp:183
size_t discretizedCount(double step) const
计算离散化采样点数量
定义 TimeInterval.hpp:234
bool isPoint() const
是否为点区间(start_ == stop_,恰含一个瞬时,非空)
定义 TimeInterval.hpp:175
const TimePoint & stop() const
时间区间的结束时间点
定义 TimeInterval.hpp:109
const TimePoint & start() const
时间区间的开始时间点
定义 TimeInterval.hpp:100
void setBounds(const TimePoint &start, const TimePoint &stop)
设置时间区间的开始时间点和结束时间点
定义 TimeInterval.hpp:120
std::string toString(int precision=kTimePointDefaultFormatPrecision) const
将时间区间转换为字符串
定义 TimeInterval.hpp:189
const TimePoint & getStop() const
时间区间的结束时间点
定义 TimeInterval.hpp:112
TimeInterval operator|(const TimeInterval &other) const
并集(返回副本,等价于 united)
定义 TimeInterval.hpp:282
TimeInterval(const TimePoint &start, const TimePoint &stop)
构造函数
定义 TimeInterval.hpp:89
TimeInterval & operator&=(const TimeInterval &other)
原地交集(等价于 intersect)
定义 TimeInterval.hpp:285
void setWhole()
设置时间区间为全区间
定义 TimeInterval.hpp:157
void setStop(const TimePoint &stop)
设置时间区间的结束时间点
定义 TimeInterval.hpp:115
时间点列表
定义 TimeList.hpp:49
按步长采样的时间点范围(惰性可迭代)
定义 TimePointRange.hpp:41
绝对时间点
定义 TimePoint.hpp:108
errc_t aTimeIntervalParse(StringView strStart, StringView strStop, TimeInterval &interval)
从字符串解析时间区间
定义 TimeInterval.cpp:37
errc_t aTimeIntervalFormat(const TimeInterval &interval, std::string &strStart, std::string &strStop, int precision)
将时间区间格式化为字符串
定义 TimeInterval.cpp:27