🛰️航天仿真算法库 SpaceAST 0.3.1
载入中...
搜索中...
未找到
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
31AST_NAMESPACE_BEGIN
32
42{
43public:
45 static Interval Empty();
46
48 static Interval Whole();
49public:
50 double start() const{return start_;}
51 double stop() const{return stop_;}
52 double& start() {return start_;}
53 double& stop() {return stop_;}
54 double duration() const{return stop_ - start_;}
55public:
57 bool isEmpty() const { return !(start_ <= stop_); }
59 bool isPoint() const { return start_ == stop_; }
61 bool isDegenerate() const { return isPoint(); }
63 bool isValid() const { return start_ <= stop_; }
65 bool isNonEmpty() const { return isValid(); }
67 bool contains(double t) const { return start_ <= t && t <= stop_; }
68public:
73 void setBounds(double start, double stop);
74
76 void setEmpty();
77
79 void setWhole();
80public:
88 DoubleRange discretize(double step) const;
89
96 size_t discretizedCount(double step) const;
97
98
103 Interval& unite(const Interval& other);
104
108 Interval united(const Interval& other) const;
109
114 Interval& intersect(const Interval& other);
115
120 Interval intersected(const Interval& other) const;
121
125 bool intersects(const Interval& other) const;
126
128 Interval& operator|=(const Interval& other) { return unite(other); }
129
131 Interval operator|(const Interval& other) const { return united(other); }
132
134 Interval& operator&=(const Interval& other) { return intersect(other); }
135
137 Interval operator&(const Interval& other) const { return intersected(other); }
138public:
139 double start_;
140 double stop_;
141};
142
143inline Interval Interval::Empty()
144{
145 return Interval{+(std::numeric_limits<double>::infinity()),
146 -(std::numeric_limits<double>::infinity()) };
147}
148
149inline Interval Interval::Whole()
150{
151 return Interval{-std::numeric_limits<double>::infinity(),
152 +std::numeric_limits<double>::infinity()};
153}
154
155inline void Interval::setBounds(double start, double stop)
156{
157 start_ = start;
158 stop_ = stop;
159}
160
161inline void Interval::setEmpty()
162{
163 start_ = +std::numeric_limits<double>::infinity();
164 stop_ = -std::numeric_limits<double>::infinity();
165}
166
167inline void Interval::setWhole()
168{
169 start_ = -std::numeric_limits<double>::infinity();
170 stop_ = +std::numeric_limits<double>::infinity();
171}
172
173inline Interval& Interval::unite(const Interval &other)
174{
175 start_ = (std::min)(start_, other.start());
176 stop_ = (std::max)(stop_, other.stop());
177 return *this;
178}
179
180inline Interval Interval::united(const Interval &other) const
181{
182 return Interval{(std::min)(start_, other.start()),
183 (std::max)(stop_, other.stop())};
184}
185
186inline Interval& Interval::intersect(const Interval &other)
187{
188 double s = (propagate_nan::max)(start_, other.start_);
189 double t = (propagate_nan::min)(stop_, other.stop());
190 if (!(s <= t)) {
191 *this = Empty(); // 不相交 → 规范空区间(不钳回假点)
192 } else {
193 start_ = s;
194 stop_ = t;
195 }
196 return *this;
197}
198
199inline Interval Interval::intersected(const Interval &other) const
200{
201 double s = (propagate_nan::max)(start_, other.start_);
202 double t = (propagate_nan::min)(stop_, other.stop());
203 if (!(s <= t)) {
204 return Empty(); // 不相交 → 规范空区间
205 }
206 return Interval{s, t};
207}
208
209inline bool Interval::intersects(const Interval &other) const
210{
211 // 与 intersected() 一致:非空交集(含相切产生的点区间)才算相交;NaN → false
212 return (propagate_nan::max)(start_, other.start()) <= (propagate_nan::min)(stop_, other.stop());
213}
214
215inline DoubleRange Interval::discretize(double step) const
216{
217 double dur = duration();
218 if(step > 0 && dur >= 0)
219 {
220 return DoubleRange(start_, stop_, step);
221 }
222 // 如果存在 nan,duration()为 nan,if 条件为false, 返回空范围
223 return DoubleRange(0.0, 0.0, step, 0);
224}
225
226inline size_t Interval::discretizedCount(double step) const
227{
228 double dur = duration();
229 if(step > 0 && dur >= 0)
230 {
231 return aDiscretizedCount(dur, step);
232 }
233 // 如果存在 nan,duration()为 nan,if 条件为false, 返回0
234 return 0;
235}
236
237
240AST_NAMESPACE_END
按步长采样的相对秒范围
按步长采样的相对秒范围(惰性可迭代)
定义 DoubleRange.hpp:43
相对时间区间
定义 Interval.hpp:42
bool isNonEmpty() const
isValid() 的别名
定义 Interval.hpp:65
bool isDegenerate() const
isPoint() 的别名
定义 Interval.hpp:61
Interval operator&(const Interval &other) const
交集(返回副本,等价于 intersected)
定义 Interval.hpp:137
bool isValid() const
是否非空(start_ <= stop_,即点区间或有效区间)
定义 Interval.hpp:63
bool isPoint() const
是否为点区间(start_ == stop_,恰含一个瞬时,非空)
定义 Interval.hpp:59
bool isEmpty() const
是否为空区间(start_ > stop_,弱序否定;反向 / {+∞,-∞} 哨兵 / NaN 边界 → true)
定义 Interval.hpp:57
Interval & operator|=(const Interval &other)
原地并集(等价于 unite)
定义 Interval.hpp:128
Interval operator|(const Interval &other) const
并集(返回副本,等价于 united)
定义 Interval.hpp:131
bool contains(double t) const
是否包含给定瞬时 t(闭区间含端点)
定义 Interval.hpp:67
Interval & operator&=(const Interval &other)
原地交集(等价于 intersect)
定义 Interval.hpp:134
A_ALWAYS_INLINE size_t aDiscretizedCount(double span, double step)
计算闭区间 [0, span] 按 step 步长采样(含两端点)的采样点数
定义 Math.hpp:107
Unit s
秒
定义 Unit.cpp:465