ViewDesignEngine  3.1.0
高性能 CAD 计算几何引擎
aabb.h
浏览该文件的文档.
1 #pragma once
2 #include "vde/core/point.h"
3 #include <algorithm>
4 #include <limits>
5 
6 namespace vde::core {
7 
20 template <typename T>
21 class AABB {
22 public:
29  AABB() : min_(Point3D::Constant(std::numeric_limits<T>::max())),
30  max_(Point3D::Constant(std::numeric_limits<T>::lowest())) {}
31 
40  AABB(const Point3D& min, const Point3D& max) : min_(min), max_(max) {}
41 
55  void expand(const Point3D& p) {
56  min_ = min_.cwiseMin(p);
57  max_ = max_.cwiseMax(p);
58  }
59 
65  void expand(const AABB& other) {
66  min_ = min_.cwiseMin(other.min_);
67  max_ = max_.cwiseMax(other.max_);
68  }
69 
74  [[nodiscard]] Point3D center() const { return (min_ + max_) * 0.5; }
75 
80  [[nodiscard]] Vector3D extent() const { return max_ - min_; }
81 
86  [[nodiscard]] T surface_area() const {
87  Vector3D e = extent();
88  return 2.0 * (e.x() * e.y() + e.y() * e.z() + e.z() * e.x());
89  }
90 
95  [[nodiscard]] T volume() const {
96  Vector3D e = extent();
97  return e.x() * e.y() * e.z();
98  }
99 
106  [[nodiscard]] bool contains(const Point3D& p) const {
107  return (p.array() >= min_.array()).all() && (p.array() <= max_.array()).all();
108  }
109 
116  [[nodiscard]] bool intersects(const AABB& other) const {
117  return (min_.array() <= other.max_.array()).all()
118  && (max_.array() >= other.min_.array()).all();
119  }
120 
122  [[nodiscard]] const Point3D& min() const { return min_; }
124  [[nodiscard]] const Point3D& max() const { return max_; }
125 
126 private:
127  Point3D min_, max_;
128 };
129 
134 
135 } // namespace vde::core
轴对齐包围盒(AABB)
Definition: aabb.h:21
Point3D center() const
包围盒中心点
Definition: aabb.h:74
bool contains(const Point3D &p) const
测试点是否在包围盒内部(含边界)
Definition: aabb.h:106
T volume() const
包围盒体积
Definition: aabb.h:95
bool intersects(const AABB &other) const
测试两个包围盒是否相交(含边界接触)
Definition: aabb.h:116
T surface_area() const
包围盒表面积
Definition: aabb.h:86
AABB()
构造空包围盒
Definition: aabb.h:29
void expand(const AABB &other)
扩展包围盒以包含另一个 AABB
Definition: aabb.h:65
AABB(const Point3D &min, const Point3D &max)
从两个角点构造包围盒
Definition: aabb.h:40
Vector3D extent() const
包围盒对角线向量(从 min 到 max)
Definition: aabb.h:80
void expand(const Point3D &p)
扩展包围盒以包含给定点
Definition: aabb.h:55
const Point3D & min() const
获取最小角点
Definition: aabb.h:122
const Point3D & max() const
获取最大角点
Definition: aabb.h:124
Definition: aabb.h:6
foundation::Vector3D Vector3D
双精度三维向量(重新导出)
Definition: point.h:49
foundation::Point3D Point3D
双精度三维点(重新导出)
Definition: point.h:31