🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
TimeInterval.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "TimePoint.hpp"
25#include <string>
26#include <limits>
27#include <vector>
28#include <cmath>
29
30AST_NAMESPACE_BEGIN
31
32
33
40class TimeInterval;
41
46AST_CORE_CAPI errc_t aTimeIntervalFormat(const TimeInterval& interval, std::string& strStart, std::string& strEnd);
47
48
53AST_CORE_CAPI errc_t aTimeIntervalParse(StringView strStart, StringView strEnd, TimeInterval& interval);
54
55
59{
60public:
61 TimeInterval() = default;
62 ~TimeInterval() = default;
63
67 TimeInterval(const TimePoint& start, const TimePoint& stop){
68 this->setStartStop(start, stop);
69 }
70
75 TimeInterval(const TimePoint& epoch, double start, double stop)
76 {
77 this->setStartStop(epoch, start, stop);
78 }
79
81 const TimePoint& start() const{return reinterpret_cast<const TimePoint&>(*this);}
82
84 const TimePoint& getStart() const{return reinterpret_cast<const TimePoint&>(*this);}
85
87 TimePoint stop() const{return {epoch_, stop_};}
88
90 TimePoint getStop() const{return {epoch_, stop_};}
91
92
96 void setStartStop(const TimePoint& start, const TimePoint& stop){
97 epoch_ = start.integerPart();
98 start_ = start.fractionalPart();
99 stop_ = stop.fractionalPart() + (stop.integerPart() - start.integerPart());
100 }
101
106 void setStartStop(const TimePoint& epoch, double start, double stop){
107 epoch_ = epoch.integerPart();
108 start_ = epoch.fractionalPart() + start;
109 stop_ = epoch.fractionalPart() + stop;
110 }
111
114 {
115 epoch_ = 0;
116 start_ = -std::numeric_limits<double>::infinity();
117 stop_ = +std::numeric_limits<double>::infinity();
118 }
119public:
121 double duration() const{return stop_ - start_;}
122
123public:
125 std::string toString() const{
126 std::string strStart, strEnd;
127 aTimeIntervalFormat(*this, strStart, strEnd);
128 return strStart + " - " + strEnd;
129 }
130public:
136 AST_CORE_API
137 errc_t discrete(const TimePoint& epoch, double step, std::vector<double>& times) const;
138
143 AST_CORE_API
144 errc_t discrete(double step, std::vector<TimePoint>& times) const;
145
146 class DiscreteTimePointRange;
147 class DiscreteEpochSecondRange;
148
152 DiscreteTimePointRange discrete(double step) const;
153
158 DiscreteEpochSecondRange discrete(const TimePoint& epoch, double step) const;
159
160protected:
161 int64_t epoch_;
162 double start_;
163 double stop_;
164};
165
166
169public:
170 DiscreteTimePointRange(const TimeInterval& interval, double step, size_t n)
171 : interval_(interval), step_(step), n_(n) {}
172
173 class iterator {
174 public:
175 using iterator_category = std::input_iterator_tag;
176 using value_type = TimePoint;
177 using difference_type = ptrdiff_t;
178 using pointer = const TimePoint*;
179 using reference = const TimePoint&;
180
181 iterator() = default;
182 iterator(const DiscreteTimePointRange* range, size_t idx)
183 : range_(range), idx_(idx) {}
184
185 reference operator*() const {
186 if (idx_ == range_->n_ - 1) {
187 value_ = range_->interval_.stop();
188 } else {
189 value_ = range_->interval_.start() + range_->step_ * idx_;
190 }
191 return value_;
192 }
193
194 iterator& operator++() { ++idx_; return *this; }
195 iterator operator++(int) { auto tmp = *this; ++*this; return tmp; }
196
197 bool operator==(const iterator& other) const { return idx_ == other.idx_; }
198 bool operator!=(const iterator& other) const { return !(*this == other); }
199
200 private:
201 const DiscreteTimePointRange* range_ = nullptr;
202 size_t idx_ = 0;
203 mutable TimePoint value_;
204 };
205
206 iterator begin() const { return iterator(this, 0); }
207 iterator end() const { return iterator(this, n_); }
208 size_t size() const { return n_; }
209
210private:
211 TimeInterval interval_;
212 double step_;
213 size_t n_;
214};
215
218public:
219 DiscreteEpochSecondRange(double offset, double step, double stopOffset, size_t n)
220 : offset_(offset), step_(step), stopOffset_(stopOffset), n_(n) {}
221
222 class iterator {
223 public:
224 using iterator_category = std::input_iterator_tag;
225 using value_type = double;
226 using difference_type = ptrdiff_t;
227 using pointer = const double*;
228 using reference = const double&;
229
230 iterator() = default;
231 iterator(const DiscreteEpochSecondRange* range, size_t idx)
232 : range_(range), idx_(idx) {}
233
234 reference operator*() const {
235 if (idx_ == range_->n_ - 1) {
236 value_ = range_->stopOffset_;
237 } else {
238 value_ = range_->offset_ + range_->step_ * idx_;
239 }
240 return value_;
241 }
242
243 iterator& operator++() { ++idx_; return *this; }
244 iterator operator++(int) { auto tmp = *this; ++*this; return tmp; }
245
246 bool operator==(const iterator& other) const { return idx_ == other.idx_; }
247 bool operator!=(const iterator& other) const { return !(*this == other); }
248
249 private:
250 const DiscreteEpochSecondRange* range_ = nullptr;
251 size_t idx_ = 0;
252 mutable double value_;
253 };
254
255 iterator begin() const { return iterator(this, 0); }
256 iterator end() const { return iterator(this, n_); }
257 size_t size() const { return n_; }
258private:
259 double offset_;
260 double step_;
261 double stopOffset_;
262 size_t n_;
263};
264
265
266inline TimeInterval::DiscreteTimePointRange TimeInterval::discrete(double step) const
267{
268 double dur = duration();
269 if (step <= 0.0 || dur <= 0.0) {
270 return DiscreteTimePointRange(*this, step, 0);
271 }
272 size_t n = static_cast<size_t>(std::ceil(dur / step));
273 return DiscreteTimePointRange(*this, step, n);
274}
275
276inline TimeInterval::DiscreteEpochSecondRange TimeInterval::discrete(const TimePoint& epoch, double step) const
277{
278 double dur = duration();
279 if (step <= 0.0 || dur <= 0.0) {
280 return DiscreteEpochSecondRange(0.0, step, 0.0, 0);
281 }
282 size_t n = static_cast<size_t>(std::ceil(dur / step));
283 double offset = getStart() - epoch;
284 double stopOffset = getStop() - epoch;
285 return DiscreteEpochSecondRange(offset, step, stopOffset, n);
286}
287
292AST_NAMESPACE_END
绝对时间点
离散化历元秒范围
定义 TimeInterval.hpp:217
离散化时间点范围
定义 TimeInterval.hpp:168
时间区间
定义 TimeInterval.hpp:59
void setInfinite()
设置时间区间为无限时间区间
定义 TimeInterval.hpp:113
double stop_
相对结束时间(s)
定义 TimeInterval.hpp:163
TimePoint getStop() const
时间区间的结束时间点
定义 TimeInterval.hpp:90
double duration() const
时间区间的持续时间(秒)
定义 TimeInterval.hpp:121
TimeInterval(const TimePoint &epoch, double start, double stop)
构造函数
定义 TimeInterval.hpp:75
const TimePoint & getStart() const
时间区间的开始时间点
定义 TimeInterval.hpp:84
int64_t epoch_
时间区间的基准时间点(秒,从J2000.0 TAI 开始)
定义 TimeInterval.hpp:161
void setStartStop(const TimePoint &epoch, double start, double stop)
设置时间区间的开始时间点和结束时间点
定义 TimeInterval.hpp:106
const TimePoint & start() const
时间区间的开始时间点
定义 TimeInterval.hpp:81
std::string toString() const
将时间区间转换为字符串
定义 TimeInterval.hpp:125
double start_
相对开始时间(s)
定义 TimeInterval.hpp:162
TimePoint stop() const
时间区间的结束时间点
定义 TimeInterval.hpp:87
TimeInterval(const TimePoint &start, const TimePoint &stop)
构造函数
定义 TimeInterval.hpp:67
void setStartStop(const TimePoint &start, const TimePoint &stop)
设置时间区间的开始时间点和结束时间点
定义 TimeInterval.hpp:96
绝对时间点
定义 TimePoint.hpp:106
double fractionalPart() const
时间点的小数秒数部分
定义 TimePoint.hpp:163
int64_t integerPart() const
时间点的整数秒数部分
定义 TimePoint.hpp:160
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