🛰️航天仿真算法库 SpaceAST 0.4.0
载入中...
搜索中...
未找到
Interval.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "DoubleRange.hpp"
25#include "AstUtil/Logger.hpp"
26#include "AstUtil/Math.hpp"
27#include <cstddef>
28#include <cmath>
29#include <limits>
30#include <string>
31
32AST_NAMESPACE_BEGIN
33
43{
44public:
46 static Interval Empty();
47
49 static Interval Whole();
50public:
51 double start() const{return start_;}
52 double stop() const{return stop_;}
53 double& start() {return start_;}
54 double& stop() {return stop_;}
55 double duration() const{return stop_ - start_;}
56public:
58 bool isEmpty() const { return !(start_ <= stop_); }
60 bool isPoint() const { return start_ == stop_; }
62 bool isDegenerate() const { return isPoint(); }
64 bool isValid() const { return start_ <= stop_; }
66 bool isNonEmpty() const { return isValid(); }
68 bool contains(double t) const { return start_ <= t && t <= stop_; }
69public:
74 void setBounds(double start, double stop);
75
77 void setEmpty();
78
80 void setWhole();
81public:
89 DoubleRange discretize(double step) const;
90
97 size_t discretizedCount(double step) const;
98
99
104 Interval& unite(const Interval& other);
105
109 Interval united(const Interval& other) const;
110
115 Interval& intersect(const Interval& other);
116
121 Interval intersected(const Interval& other) const;
122
126 bool intersects(const Interval& other) const;
127
129 Interval& operator|=(const Interval& other) { return unite(other); }
130
132 Interval operator|(const Interval& other) const { return united(other); }
133
135 Interval& operator&=(const Interval& other) { return intersect(other); }
136
138 Interval operator&(const Interval& other) const { return intersected(other); }
139public:
140 AST_CORE_API
141 std::string toString() const;
142public:
143 double start_;
144 double stop_;
145};
146
147inline Interval Interval::Empty()
148{
149 return Interval{+(std::numeric_limits<double>::infinity()),
150 -(std::numeric_limits<double>::infinity()) };
151}
152
153inline Interval Interval::Whole()
154{
155 return Interval{-std::numeric_limits<double>::infinity(),
156 +std::numeric_limits<double>::infinity()};
157}
158
159inline void Interval::setBounds(double start, double stop)
160{
161 start_ = start;
162 stop_ = stop;
163}
164
165inline void Interval::setEmpty()
166{
167 start_ = +std::numeric_limits<double>::infinity();
168 stop_ = -std::numeric_limits<double>::infinity();
169}
170
171inline void Interval::setWhole()
172{
173 start_ = -std::numeric_limits<double>::infinity();
174 stop_ = +std::numeric_limits<double>::infinity();
175}
176
177inline Interval& Interval::unite(const Interval &other)
178{
179 start_ = (std::min)(start_, other.start());
180 stop_ = (std::max)(stop_, other.stop());
181 return *this;
182}
183
184inline Interval Interval::united(const Interval &other) const
185{
186 return Interval{(std::min)(start_, other.start()),
187 (std::max)(stop_, other.stop())};
188}
189
190inline Interval& Interval::intersect(const Interval &other)
191{
192 double s = (propagate_nan::max)(start_, other.start_);
193 double t = (propagate_nan::min)(stop_, other.stop());
194 if (!(s <= t)) {
195 *this = Empty(); // 不相交 → 规范空区间(不钳回假点)
196 } else {
197 start_ = s;
198 stop_ = t;
199 }
200 return *this;
201}
202
203inline Interval Interval::intersected(const Interval &other) const
204{
205 double s = (propagate_nan::max)(start_, other.start_);
206 double t = (propagate_nan::min)(stop_, other.stop());
207 if (!(s <= t)) {
208 return Empty(); // 不相交 → 规范空区间
209 }
210 return Interval{s, t};
211}
212
213inline bool Interval::intersects(const Interval &other) const
214{
215 // 与 intersected() 一致:非空交集(含相切产生的点区间)才算相交;NaN → false
216 return (propagate_nan::max)(start_, other.start()) <= (propagate_nan::min)(stop_, other.stop());
217}
218
219inline DoubleRange Interval::discretize(double step) const
220{
221 double dur = duration();
222 if(step > 0 && dur >= 0)
223 {
224 return DoubleRange(start_, stop_, step);
225 }
226 // 如果存在 nan,duration()为 nan,if 条件为false, 返回空范围
227 return DoubleRange(0.0, 0.0, step, 0);
228}
229
230inline size_t Interval::discretizedCount(double step) const
231{
232 double dur = duration();
233 if(step > 0 && dur >= 0)
234 {
235 return aDiscretizedCount(dur, step);
236 }
237 // 如果存在 nan,duration()为 nan,if 条件为false, 返回0
238 return 0;
239}
240
241
244AST_NAMESPACE_END
按步长采样的相对秒范围
按步长采样的相对秒范围(惰性可迭代)
定义 DoubleRange.hpp:43
相对时间区间
定义 Interval.hpp:43
bool isNonEmpty() const
isValid() 的别名
定义 Interval.hpp:66
bool isDegenerate() const
isPoint() 的别名
定义 Interval.hpp:62
Interval operator&(const Interval &other) const
交集(返回副本,等价于 intersected)
定义 Interval.hpp:138
bool isValid() const
是否非空(start_ <= stop_,即点区间或有效区间)
定义 Interval.hpp:64
bool isPoint() const
是否为点区间(start_ == stop_,恰含一个瞬时,非空)
定义 Interval.hpp:60
bool isEmpty() const
是否为空区间(start_ > stop_,弱序否定;反向 / {+∞,-∞} 哨兵 / NaN 边界 → true)
定义 Interval.hpp:58
Interval & operator|=(const Interval &other)
原地并集(等价于 unite)
定义 Interval.hpp:129
Interval operator|(const Interval &other) const
并集(返回副本,等价于 united)
定义 Interval.hpp:132
bool contains(double t) const
是否包含给定瞬时 t(闭区间含端点)
定义 Interval.hpp:68
Interval & operator&=(const Interval &other)
原地交集(等价于 intersect)
定义 Interval.hpp:135
A_ALWAYS_INLINE size_t aDiscretizedCount(double span, double step)
计算闭区间 [0, span] 按 step 步长采样(含两端点)的采样点数
定义 Math.hpp:107
Unit s
秒
定义 Unit.cpp:465