🛰️航天仿真算法库 SpaceAST 0.0.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();
46public:
47 double start() const{return start_;}
48 double stop() const{return stop_;}
49 double& start() {return start_;}
50 double& stop() {return stop_;}
51 double duration() const{return stop_ - start_;}
52public:
54 bool isEmpty() const { return !(start_ <= stop_); }
56 bool isPoint() const { return start_ == stop_; }
58 bool isDegenerate() const { return isPoint(); }
60 bool isValid() const { return start_ <= stop_; }
62 bool isNonEmpty() const { return isValid(); }
64 bool contains(double t) const { return start_ <= t && t <= stop_; }
65public:
70 void setBounds(double start, double stop);
71
73 void setEmpty();
74
76 void setWhole();
77public:
85 DoubleRange discretize(double step) const;
86
93 size_t discretizedCount(double step) const;
94
95
100 Interval& unite(const Interval& other);
101
105 Interval united(const Interval& other) const;
106
111 Interval& intersect(const Interval& other);
112
117 Interval intersected(const Interval& other) const;
118
122 bool intersects(const Interval& other) const;
123
125 Interval& operator|=(const Interval& other) { return unite(other); }
126
128 Interval operator|(const Interval& other) const { return united(other); }
129
131 Interval& operator&=(const Interval& other) { return intersect(other); }
132
134 Interval operator&(const Interval& other) const { return intersected(other); }
135public:
136 double start_;
137 double stop_;
138};
139
140inline Interval Interval::Empty()
141{
142 return Interval{+(std::numeric_limits<double>::infinity()),
143 -(std::numeric_limits<double>::infinity()) };
144}
145
146inline void Interval::setBounds(double start, double stop)
147{
148 start_ = start;
149 stop_ = stop;
150}
151
152inline void Interval::setEmpty()
153{
154 start_ = +std::numeric_limits<double>::infinity();
155 stop_ = -std::numeric_limits<double>::infinity();
156}
157
158inline void Interval::setWhole()
159{
160 start_ = -std::numeric_limits<double>::infinity();
161 stop_ = +std::numeric_limits<double>::infinity();
162}
163
164inline Interval& Interval::unite(const Interval &other)
165{
166 start_ = (std::min)(start_, other.start());
167 stop_ = (std::max)(stop_, other.stop());
168 return *this;
169}
170
171inline Interval Interval::united(const Interval &other) const
172{
173 return Interval{(std::min)(start_, other.start()),
174 (std::max)(stop_, other.stop())};
175}
176
177inline Interval& Interval::intersect(const Interval &other)
178{
179 double s = (propagate_nan::max)(start_, other.start_);
180 double t = (propagate_nan::min)(stop_, other.stop());
181 if (!(s <= t)) {
182 *this = Empty(); // 不相交 → 规范空区间(不钳回假点)
183 } else {
184 start_ = s;
185 stop_ = t;
186 }
187 return *this;
188}
189
190inline Interval Interval::intersected(const Interval &other) const
191{
192 double s = (propagate_nan::max)(start_, other.start_);
193 double t = (propagate_nan::min)(stop_, other.stop());
194 if (!(s <= t)) {
195 return Empty(); // 不相交 → 规范空区间
196 }
197 return Interval{s, t};
198}
199
200inline bool Interval::intersects(const Interval &other) const
201{
202 // 与 intersected() 一致:非空交集(含相切产生的点区间)才算相交;NaN → false
203 return (propagate_nan::max)(start_, other.start()) <= (propagate_nan::min)(stop_, other.stop());
204}
205
206inline DoubleRange Interval::discretize(double step) const
207{
208 double dur = duration();
209 if(step > 0 && dur >= 0)
210 {
211 size_t n = static_cast<size_t>(std::ceil(dur / step)) + 1;
212 return DoubleRange(start_, stop_, step, n);
213 }
214 // 如果存在 nan,duration()为 nan,if 条件为false, 返回空范围
215 return DoubleRange(0.0, 0.0, step, 0);
216}
217
218inline size_t Interval::discretizedCount(double step) const
219{
220 double dur = duration();
221 if(step > 0 && dur >= 0)
222 {
223 return static_cast<size_t>(std::ceil(dur / step)) + 1;
224 }
225 // 如果存在 nan,duration()为 nan,if 条件为false, 返回0
226 return 0;
227}
228
229
232AST_NAMESPACE_END
按步长采样的相对秒范围
按步长采样的相对秒范围(惰性可迭代)
定义 DoubleRange.hpp:41
相对时间区间
定义 Interval.hpp:42
bool isNonEmpty() const
isValid() 的别名
定义 Interval.hpp:62
bool isDegenerate() const
isPoint() 的别名
定义 Interval.hpp:58
Interval operator&(const Interval &other) const
交集(返回副本,等价于 intersected)
定义 Interval.hpp:134
bool isValid() const
是否非空(start_ <= stop_,即点区间或有效区间)
定义 Interval.hpp:60
bool isPoint() const
是否为点区间(start_ == stop_,恰含一个瞬时,非空)
定义 Interval.hpp:56
bool isEmpty() const
是否为空区间(start_ > stop_,弱序否定;反向 / {+∞,-∞} 哨兵 / NaN 边界 → true)
定义 Interval.hpp:54
Interval & operator|=(const Interval &other)
原地并集(等价于 unite)
定义 Interval.hpp:125
Interval operator|(const Interval &other) const
并集(返回副本,等价于 united)
定义 Interval.hpp:128
bool contains(double t) const
是否包含给定瞬时 t(闭区间含端点)
定义 Interval.hpp:64
Interval & operator&=(const Interval &other)
原地交集(等价于 intersect)
定义 Interval.hpp:131
Unit s
定义 Unit.cpp:465