File indexing completed on 2025-09-17 09:13:45
0001 #ifndef BVH_V2_BBOX_H
0002 #define BVH_V2_BBOX_H
0003
0004 #include "bvh/v2/vec.h"
0005 #include "bvh/v2/utils.h"
0006
0007 #include <limits>
0008
0009 namespace bvh::v2 {
0010
0011 template <typename T, size_t N>
0012 struct BBox {
0013 Vec<T, N> min, max;
0014
0015 BBox() = default;
0016 BVH_ALWAYS_INLINE BBox(const Vec<T, N>& min, const Vec<T, N>& max) : min(min), max(max) {}
0017 BVH_ALWAYS_INLINE explicit BBox(const Vec<T, N>& point) : BBox(point, point) {}
0018
0019 BVH_ALWAYS_INLINE BBox& extend(const Vec<T, N>& point) {
0020 return extend(BBox(point));
0021 }
0022
0023 BVH_ALWAYS_INLINE BBox& extend(const BBox& other) {
0024 min = robust_min(min, other.min);
0025 max = robust_max(max, other.max);
0026 return *this;
0027 }
0028
0029 BVH_ALWAYS_INLINE Vec<T, N> get_diagonal() const { return max - min; }
0030 BVH_ALWAYS_INLINE Vec<T, N> get_center() const { return (max + min) * static_cast<T>(0.5); }
0031
0032 BVH_ALWAYS_INLINE T get_half_area() const {
0033 auto d = get_diagonal();
0034 static_assert(N == 2 || N == 3);
0035 if constexpr (N == 3) return (d[0] + d[1]) * d[2] + d[0] * d[1];
0036 if constexpr (N == 2) return d[0] + d[1];
0037 return static_cast<T>(0.);
0038 }
0039
0040 BVH_ALWAYS_INLINE static constexpr BBox make_empty() {
0041 return BBox(
0042 Vec<T, N>(+std::numeric_limits<T>::max()),
0043 Vec<T, N>(-std::numeric_limits<T>::max()));
0044 }
0045 };
0046
0047 }
0048
0049 #endif