用c++ 写一个aabb包围盒算法
时间: 2024-03-27 14:35:24 浏览: 244
下面是一个简单的 AABB 包围盒算法的 C++ 代码示例:
```c++
struct AABB {
glm::vec3 min;
glm::vec3 max;
};
AABB calculateAABB(const std::vector<glm::vec3>& points) {
AABB aabb;
aabb.min = aabb.max = points[0];
for (const auto& point : points) {
if (point.x < aabb.min.x) aabb.min.x = point.x;
if (point.y < aabb.min.y) aabb.min.y = point.y;
if (point.z < aabb.min.z) aabb.min.z = point.z;
if (point.x > aabb.max.x) aabb.max.x = point.x;
if (point.y > aabb.max.y) aabb.max.y = point.y;
if (point.z > aabb.max.z) aabb.max.z = point.z;
}
return aabb;
}
```
该函数接受一个包含三维点的向量的容器作为输入,计算并返回对应的 AABB 包围盒。算法的核心思想是遍历所有输入点,并对每个维度上的最小值和最大值进行更新。最终返回的结构体包含了计算出的最小值和最大值,即 AABB 包围盒。
这里使用了 GLM 库来处理向量运算。如果您没有使用 GLM,可以使用自己的向量类或其他数学库来代替。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)