BIMHome v1.0.0
BIMHome接口文档说明
BoundBox2D.h
浏览该文件的文档.
1/************************************************************************
2* @file BoundBox2d.h
3*
4* @brief 二维包围盒类
5*
6* @details 二维包围盒类
7*
8* @author lirf
9*
10* @version 1.0
11*
12* @date 2025-6-12
13*
14* @license 北京华科软科技有限公司
15*
16*************************************************************************/
17
18#ifndef BIMHOMEBASE_BOUNDBOX2D_H
19#define BIMHOMEBASE_BOUNDBOX2D_H
20#ifndef BH_GLOBAL_H
21#include <BHGlobal.h>
22#endif
23#include <cmath>
24#include <cfloat>
25#include <stdexcept>
26#include "Base/Vector2D.h"
27#ifndef F_PI
28# define F_PI 3.1415926f
29#endif
30
31#ifndef D_PI
32# define D_PI 3.141592653589793
33#endif
34
35#ifndef FLOAT_MAX
36# define FLOAT_MAX 3.402823466E+38F
37#endif
38
39#ifndef FLOAT_MIN
40# define FLOAT_MIN 1.175494351E-38F
41#endif
42
43#ifndef DOUBLE_MAX
44# define DOUBLE_MAX 1.7976931348623157E+308 /* "double" 最大值 */
45#endif
46
47#ifndef DOUBLE_MIN
48# define DOUBLE_MIN 2.2250738585072014E-308 /* "double" 最小值 */
49#endif
50
51namespace Base
52{
53 class Vector2d;
54 class BoundBox2d;
55 class LineSegment2D;
56 class Polygon2d;
57
62 class BaseExport BoundBox2d
63 {
64 public:
69 inline BoundBox2d();
70
76 inline BoundBox2d(const BoundBox2d& rclBB);
77
86 inline BoundBox2d(double fX1, double fY1, double fX2, double fY2);
87
93 inline bool IsValid();
94
102 inline bool IsEqual(const BoundBox2d& rclBB, double tolerance) const;
103
110 inline BoundBox2d& operator= (const BoundBox2d& rclBB);
111
118 inline bool operator== (const BoundBox2d& rclBB) const;
119
125 inline double Width() const;
126
132 inline double Height() const;
133
140 inline bool Contains(const Base::Vector2d& v) const;
141
149 inline bool Contains(const Vector2d& v, double tolerance) const;
150
156 inline Vector2d GetCenter() const;
157
162 inline void SetVoid();
163
169 inline void Add(const Vector2d& v);
170
177 bool Intersect(const LineSegment2D& rclLine) const;
178
185 bool Intersect(const BoundBox2d& rclBB) const;
186
193 bool Intersect(const Polygon2d& rclPoly) const;
194
195 public:
196 double MinX;
197 double MinY;
198 double MaxX;
199 double MaxY;
200 };
202 {
203 MinX = MinY = DOUBLE_MAX;
204 MaxX = MaxY = -DOUBLE_MAX;
205 }
206
208 : MinX(rclBB.MinX),
209 MinY(rclBB.MinY),
210 MaxX(rclBB.MaxX),
211 MaxY(rclBB.MaxY)
212 {
213 }
214
215 inline BoundBox2d::BoundBox2d(double fX1, double fY1, double fX2, double fY2)
216 {
217 MinX = std::min<double>(fX1, fX2);
218 MaxX = std::max<double>(fX1, fX2);
219 MinY = std::min<double>(fY1, fY2);
220 MaxY = std::max<double>(fY1, fY2);
221 }
222
224 {
225 return (MaxX >= MinX) && (MaxY >= MinY);
226 }
227
228 inline bool BoundBox2d::IsEqual(const BoundBox2d& b, double tolerance) const
229 {
230 return Vector2d(MinX, MinY).IsEqual(Vector2d(b.MinX, b.MinY), tolerance) &&
231 Vector2d(MaxX, MaxY).IsEqual(Vector2d(b.MaxX, b.MaxY), tolerance);
232 }
233
235 {
236 MinX = rclBB.MinX;
237 MinY = rclBB.MinY;
238 MaxX = rclBB.MaxX;
239 MaxY = rclBB.MaxY;
240 return *this;
241 }
242
243 inline bool BoundBox2d::operator== (const BoundBox2d& rclBB) const
244 {
245 return (MinX == rclBB.MinX) &&
246 (MinY == rclBB.MinY) &&
247 (MaxX == rclBB.MaxX) &&
248 (MaxY == rclBB.MaxY);
249 }
250
251 inline double BoundBox2d::Width() const
252 {
253 return MaxX - MinX;
254 }
255
256 inline double BoundBox2d::Height() const
257 {
258 return MaxY - MinY;
259 }
260
261 inline bool BoundBox2d::Contains(const Vector2d& v) const
262 {
263 return v.x >= MinX && v.x <= MaxX
264 && v.y >= MinY && v.y <= MaxY;
265 }
266
267 inline bool BoundBox2d::Contains(const Vector2d& v, double tolerance) const
268 {
269 return v.x >= MinX - tolerance && v.x <= MaxX + tolerance
270 && v.y >= MinY - tolerance && v.y <= MaxY + tolerance;
271 }
272
274 {
275 return Vector2d((MinX + MaxX) * 0.5, (MinY + MaxY) * 0.5);
276 }
277
279 {
280 MinX = MinY = DOUBLE_MAX;
281 MaxX = MaxY = -DOUBLE_MAX;
282 }
283
284 inline void BoundBox2d::Add(const Vector2d& v)
285 {
286 MinX = std::min<double>(MinX, v.x);
287 MinY = std::min<double>(MinY, v.y);
288 MaxX = std::max<double>(MaxX, v.x);
289 MaxY = std::max<double>(MaxY, v.y);
290 }
291
292
293}
294
295#endif
#define DOUBLE_MAX
Definition BoundBox2D.h:44
double MaxY
最大Y坐标
Definition BoundBox2D.h:199
double Height() const
获取包围盒的高度
Definition BoundBox2D.h:256
Vector2d GetCenter() const
获取包围盒的中心点
Definition BoundBox2D.h:273
BoundBox2d()
默认构造函数
Definition BoundBox2D.h:201
double MaxX
最大X坐标
Definition BoundBox2D.h:198
bool Intersect(const BoundBox2d &rclBB) const
检测另一个包围盒是否与当前包围盒相交
bool Intersect(const LineSegment2D &rclLine) const
检测线段是否与包围盒相交
bool IsValid()
检测包围盒是否有效
Definition BoundBox2D.h:223
double MinX
最小X坐标
Definition BoundBox2D.h:196
double MinY
最小Y坐标
Definition BoundBox2D.h:197
bool IsEqual(const BoundBox2d &rclBB, double tolerance) const
检测两个包围盒是否在指定容差范围内相等
Definition BoundBox2D.h:228
bool Contains(const Base::Vector2d &v) const
检测点是否在包围盒内
Definition BoundBox2D.h:261
void Add(const Vector2d &v)
将点添加到包围盒
Definition BoundBox2D.h:284
bool operator==(const BoundBox2d &rclBB) const
重载 == 操作符
Definition BoundBox2D.h:243
double Width() const
获取包围盒的宽度
Definition BoundBox2D.h:251
void SetVoid()
设置包围盒为空
Definition BoundBox2D.h:278
BoundBox2d & operator=(const BoundBox2d &rclBB)
重载 = 操作符
Definition BoundBox2D.h:234
bool Intersect(const Polygon2d &rclPoly) const
检测多边形是否与包围盒相交
二维轴对齐包围盒(AABB)实现
Definition BoundBox2D.h:63
Definition LineSegment2D.h:28
bool IsEqual(const Vector2d &v, double tolerance=0.0) const
比较两个向量是否相等
Definition Vector2D.h:567
double y
x 和 y 坐标
Definition Vector2D.h:384
double x
Definition Vector2D.h:384
Vector2d 类,表示二维空间中的向量
Definition Vector2D.h:58
Definition BaseFigureFactory.h:24