🛰️航天仿真算法库 SpaceAST 0.3.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
81 {
82 return TimeInterval(TimePoint{0, -std::numeric_limits<double>::infinity()},
83 TimePoint{0, +std::numeric_limits<double>::infinity()});
84 }
85
86public:
88 : start_{}
89 , stop_{}
90 {}
91 ~TimeInterval() = default;
92
96 TimeInterval(const TimePoint& start, const TimePoint& stop)
97 : start_(start), stop_(stop) {}
98
103 TimeInterval(const TimePoint& epoch, double start, double stop)
104 : start_(epoch + start), stop_(epoch + stop) {}
105
107 const TimePoint& start() const{return start_;}
108
110 const TimePoint& getStart() const{return start_;}
111
113 void setStart(const TimePoint& start){start_ = start;}
114
116 const TimePoint& stop() const{return stop_;}
117
119 const TimePoint& getStop() const{return stop_;}
120
122 void setStop(const TimePoint& stop){stop_ = stop;}
123
130 void setBounds(TimePoint start, TimePoint stop){
131 start_ = start;
132 stop_ = stop;
133 }
134
141 void setBounds(TimePoint epoch, double start, double stop){
142 start_ = epoch + start;
143 stop_ = epoch + stop;
144 }
145
150 void setBounds(TimePoint epoch, const Interval& interval){
151 start_ = epoch + interval.start();
152 stop_ = epoch + interval.stop();
153 }
154
164 Interval toInterval(const TimePoint& epoch) const
165 {
166 return Interval{getStart() - epoch, getStop() - epoch};
167 }
168
170 void setWhole()
171 {
172 start_ = {0, -std::numeric_limits<double>::infinity()};
173 stop_ = {0, +std::numeric_limits<double>::infinity()};
174 }
176 void setEmpty()
177 {
178 start_ = {0, +std::numeric_limits<double>::infinity()};
179 stop_ = {0, -std::numeric_limits<double>::infinity()};
180 }
181public:
183 double duration() const{return stop_ - start_;}
184
186 bool isEmpty() const { return !(start_ <= stop_); }
188 bool isPoint() const { return start_ == stop_; }
190 bool isDegenerate() const { return isPoint(); }
192 bool isValid() const { return start_ <= stop_; }
194 bool isNonEmpty() const { return isValid(); }
196 bool contains(const TimePoint& t) const { return start_ <= t && t <= stop_; }
198 bool contains(const TimeInterval& interval) const { return start_ <= interval.start() && interval.stop() <= stop_; }
200 bool isWhole() const{return start_.isInf() && stop_.isInf();}
202 bool isInf() const {return start_.isInf() || stop_.isInf();}
203public:
207 std::string toString(int precision = kTimePointDefaultFormatPrecision) const{
208 std::string strStart, strEnd;
209 aTimeIntervalFormat(*this, strStart, strEnd, precision);
210 return strStart + " - " + strEnd;
211 }
212public:
218 AST_CORE_API
219 errc_t discretize(const TimePoint& epoch, double step, std::vector<double>& times) const;
220
225 AST_CORE_API
226 errc_t discretize(double step, std::vector<TimePoint>& times) const;
227
232 AST_CORE_API
233 errc_t discretize(double step, TimeList& times) const;
234
238 TimePointRange discretize(double step) const;
239
244 DoubleRange discretize(const TimePoint& epoch, double step) const;
245
252 size_t discretizedCount(double step) const
253 {
254 double dur = duration();
255 if (step > 0 && dur >= 0)
256 {
257 return aDiscretizedCount(dur, step);
258 }
259 // 如果存在nan,则duration()为nan,if 条件不成立,返回0
260 return 0;
261 }
262public:
267 AST_CORE_API
268 TimeInterval& unite(const TimeInterval& other);
269
273 AST_CORE_API
274 TimeInterval united(const TimeInterval& other) const;
275
280 AST_CORE_API
281 TimeInterval& intersect(const TimeInterval& other);
282
287 AST_CORE_API
288 TimeInterval intersected(const TimeInterval& other) const;
289
293 AST_CORE_API
294 bool intersects(const TimeInterval& other) const;
295
297 TimeInterval& operator|=(const TimeInterval& other) { return unite(other); }
298
300 TimeInterval operator|(const TimeInterval& other) const { return united(other); }
301
303 TimeInterval& operator&=(const TimeInterval& other) { return intersect(other); }
304
306 TimeInterval operator&(const TimeInterval& other) const { return intersected(other); }
307private:
308 TimePoint start_;
309 TimePoint stop_;
310};
311
312
313inline TimePointRange TimeInterval::discretize(double step) const
314{
315 double dur = duration();
316 if (step > 0 && dur >= 0)
317 {
318 size_t n = aDiscretizedCount(dur, step);
319 return TimePointRange(start_, stop_, step, n);
320 }
321 return TimePointRange(start_, stop_, step, 0);
322}
323
324inline DoubleRange TimeInterval::discretize(const TimePoint& epoch, double step) const
325{
326 double dur = duration();
327 if (step > 0 && dur >= 0)
328 {
329 double start = getStart() - epoch;
330 double stop = getStop() - epoch;
331 return DoubleRange(start, stop, step);
332 }
333
334 return DoubleRange(0.0, 0.0, step, 0);
335}
336
337#ifndef SWIG
338
339A_ALWAYS_INLINE bool operator==(const TimeInterval& lhs, const TimeInterval& rhs)
340{
341 return lhs.start() == rhs.start() && lhs.stop() == rhs.stop();
342}
343
344A_ALWAYS_INLINE bool operator!=(const TimeInterval& lhs, const TimeInterval& rhs)
345{
346 return !(lhs == rhs);
347}
348
349#endif
350
355AST_NAMESPACE_END
按步长采样的相对秒范围
按步长采样的时间点范围
绝对时间点
按步长采样的相对秒范围(惰性可迭代)
定义 DoubleRange.hpp:43
相对时间区间
定义 Interval.hpp:42
时间区间
定义 TimeInterval.hpp:64
bool isValid() const
是否非空(start_ <= stop_,即点区间或有效区间)
定义 TimeInterval.hpp:192
Interval toInterval(const TimePoint &epoch) const
将绝对时间区间转换为相对于给定基准历元的相对时间区间
定义 TimeInterval.hpp:164
double duration() const
时间区间的持续时间(秒)
定义 TimeInterval.hpp:183
TimeInterval & operator|=(const TimeInterval &other)
原地并集(等价于 unite)
定义 TimeInterval.hpp:297
bool isNonEmpty() const
isValid() 的别名
定义 TimeInterval.hpp:194
static TimeInterval Empty()
空区间哨兵({+∞, -∞},IEEE 1788 惯例)
定义 TimeInterval.hpp:74
TimeInterval(const TimePoint &epoch, double start, double stop)
构造函数
定义 TimeInterval.hpp:103
static TimeInterval Whole()
时间区间全集({-∞, +∞})
定义 TimeInterval.hpp:80
const TimePoint & getStart() const
时间区间的开始时间点
定义 TimeInterval.hpp:110
void setBounds(TimePoint epoch, const Interval &interval)
设置时间区间的开始时间点和结束时间点
定义 TimeInterval.hpp:150
void setBounds(TimePoint start, TimePoint stop)
设置时间区间的开始时间点和结束时间点
定义 TimeInterval.hpp:130
bool isDegenerate() const
isPoint() 的别名
定义 TimeInterval.hpp:190
TimeInterval operator&(const TimeInterval &other) const
交集(返回副本,等价于 intersected)
定义 TimeInterval.hpp:306
void setEmpty()
设置时间区间为空区间
定义 TimeInterval.hpp:176
void setStart(const TimePoint &start)
设置时间区间的开始时间点
定义 TimeInterval.hpp:113
bool isEmpty() const
是否为空区间(start_ > stop_,弱序否定;反向 / {+∞,-∞} 哨兵 / NaN → true)
定义 TimeInterval.hpp:186
bool contains(const TimePoint &t) const
是否包含绝对时间点 t(闭区间含端点)
定义 TimeInterval.hpp:196
size_t discretizedCount(double step) const
计算离散化采样点数量
定义 TimeInterval.hpp:252
bool isPoint() const
是否为点区间(start_ == stop_,恰含一个瞬时,非空)
定义 TimeInterval.hpp:188
const TimePoint & stop() const
时间区间的结束时间点
定义 TimeInterval.hpp:116
void setBounds(TimePoint epoch, double start, double stop)
设置时间区间的开始时间点和结束时间点
定义 TimeInterval.hpp:141
const TimePoint & start() const
时间区间的开始时间点
定义 TimeInterval.hpp:107
std::string toString(int precision=kTimePointDefaultFormatPrecision) const
将时间区间转换为字符串
定义 TimeInterval.hpp:207
const TimePoint & getStop() const
时间区间的结束时间点
定义 TimeInterval.hpp:119
TimeInterval operator|(const TimeInterval &other) const
并集(返回副本,等价于 united)
定义 TimeInterval.hpp:300
bool isInf() const
是否有区间至少一个端点为无穷大/无穷小
定义 TimeInterval.hpp:202
TimeInterval(const TimePoint &start, const TimePoint &stop)
构造函数
定义 TimeInterval.hpp:96
TimeInterval & operator&=(const TimeInterval &other)
原地交集(等价于 intersect)
定义 TimeInterval.hpp:303
void setWhole()
设置时间区间为全区间
定义 TimeInterval.hpp:170
void setStop(const TimePoint &stop)
设置时间区间的结束时间点
定义 TimeInterval.hpp:122
bool isWhole() const
是否为全区间(start_ == -∞, stop_ == +∞)
定义 TimeInterval.hpp:200
bool contains(const TimeInterval &interval) const
是否包含时间区间 interval(闭区间含端点)
定义 TimeInterval.hpp:198
时间点列表
定义 TimeList.hpp:49
按步长采样的时间点范围(惰性可迭代)
定义 TimePointRange.hpp:43
绝对时间点
定义 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
A_ALWAYS_INLINE size_t aDiscretizedCount(double span, double step)
计算闭区间 [0, span] 按 step 步长采样(含两端点)的采样点数
定义 Math.hpp:107