|
| 1 | +/** |
| 2 | + * @file bounding_box.h |
| 3 | + * @brief Axis-Aligned Bounding Box (AABB) implementation |
| 4 | + * |
| 5 | + * Provides the BB<D> class for D-dimensional bounding boxes with |
| 6 | + * geometric operations like intersection, union, and containment tests. |
| 7 | + */ |
| 8 | +#pragma once |
| 9 | + |
| 10 | +#include <algorithm> |
| 11 | +#include <array> |
| 12 | +#include <cmath> |
| 13 | +#include <limits> |
| 14 | +#include <numeric> |
| 15 | + |
| 16 | +#include <cereal/cereal.hpp> |
| 17 | + |
| 18 | +#include "prtree/core/detail/types.h" |
| 19 | + |
| 20 | +using Real = float; |
| 21 | + |
| 22 | +/** |
| 23 | + * @brief D-dimensional Axis-Aligned Bounding Box |
| 24 | + * |
| 25 | + * Stores min/max coordinates for each dimension and provides |
| 26 | + * geometric operations. |
| 27 | + * |
| 28 | + * @tparam D Number of dimensions (2, 3, or 4) |
| 29 | + */ |
| 30 | +template <int D = 2> |
| 31 | +class BB { |
| 32 | +public: |
| 33 | + std::array<Real, D> lo; ///< Minimum coordinates |
| 34 | + std::array<Real, D> hi; ///< Maximum coordinates |
| 35 | + |
| 36 | + /// Default constructor - creates an invalid/empty box |
| 37 | + BB() { |
| 38 | + for (int i = 0; i < D; i++) { |
| 39 | + lo[i] = std::numeric_limits<Real>::max(); |
| 40 | + hi[i] = -std::numeric_limits<Real>::max(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// Constructor from coordinate arrays |
| 45 | + BB(const std::array<Real, D> &lo_, const std::array<Real, D> &hi_) |
| 46 | + : lo(lo_), hi(hi_) {} |
| 47 | + |
| 48 | + /// Constructor from iterators (for compatibility with span/vector) |
| 49 | + template <typename Iterator> |
| 50 | + BB(Iterator lo_begin, Iterator lo_end, Iterator hi_begin, Iterator hi_end) { |
| 51 | + std::copy(lo_begin, lo_end, lo.begin()); |
| 52 | + std::copy(hi_begin, hi_end, hi.begin()); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @brief Check if this box intersects with another |
| 57 | + * |
| 58 | + * Two boxes intersect if they overlap in all dimensions. |
| 59 | + */ |
| 60 | + bool intersects(const BB &other) const { |
| 61 | + for (int i = 0; i < D; i++) { |
| 62 | + if (hi[i] < other.lo[i] || lo[i] > other.hi[i]) |
| 63 | + return false; |
| 64 | + } |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @brief Check if this box contains a point |
| 70 | + */ |
| 71 | + bool contains_point(const std::array<Real, D> &point) const { |
| 72 | + for (int i = 0; i < D; i++) { |
| 73 | + if (point[i] < lo[i] || point[i] > hi[i]) |
| 74 | + return false; |
| 75 | + } |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @brief Check if this box completely contains another |
| 81 | + */ |
| 82 | + bool contains(const BB &other) const { |
| 83 | + for (int i = 0; i < D; i++) { |
| 84 | + if (other.lo[i] < lo[i] || other.hi[i] > hi[i]) |
| 85 | + return false; |
| 86 | + } |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @brief Compute the union of this box with another |
| 92 | + * |
| 93 | + * Returns the smallest box that contains both boxes. |
| 94 | + */ |
| 95 | + BB union_with(const BB &other) const { |
| 96 | + BB result; |
| 97 | + for (int i = 0; i < D; i++) { |
| 98 | + result.lo[i] = std::min(lo[i], other.lo[i]); |
| 99 | + result.hi[i] = std::max(hi[i], other.hi[i]); |
| 100 | + } |
| 101 | + return result; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @brief Compute the intersection of this box with another |
| 106 | + * |
| 107 | + * Returns an empty box if they don't intersect. |
| 108 | + */ |
| 109 | + BB intersection_with(const BB &other) const { |
| 110 | + BB result; |
| 111 | + for (int i = 0; i < D; i++) { |
| 112 | + result.lo[i] = std::max(lo[i], other.lo[i]); |
| 113 | + result.hi[i] = std::min(hi[i], other.hi[i]); |
| 114 | + if (result.lo[i] > result.hi[i]) |
| 115 | + return BB(); // Empty box |
| 116 | + } |
| 117 | + return result; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @brief Compute the volume (area in 2D) of the box |
| 122 | + */ |
| 123 | + Real volume() const { |
| 124 | + Real vol = 1.0; |
| 125 | + for (int i = 0; i < D; i++) { |
| 126 | + Real extent = hi[i] - lo[i]; |
| 127 | + if (extent < 0) |
| 128 | + return 0; // Invalid box |
| 129 | + vol *= extent; |
| 130 | + } |
| 131 | + return vol; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @brief Compute the perimeter (in 2D) or surface area (in 3D) |
| 136 | + */ |
| 137 | + Real perimeter() const { |
| 138 | + if constexpr (D == 2) { |
| 139 | + return 2 * ((hi[0] - lo[0]) + (hi[1] - lo[1])); |
| 140 | + } else if constexpr (D == 3) { |
| 141 | + Real dx = hi[0] - lo[0]; |
| 142 | + Real dy = hi[1] - lo[1]; |
| 143 | + Real dz = hi[2] - lo[2]; |
| 144 | + return 2 * (dx * dy + dy * dz + dz * dx); |
| 145 | + } else { |
| 146 | + // For other dimensions, return sum of extents |
| 147 | + Real sum = 0; |
| 148 | + for (int i = 0; i < D; i++) |
| 149 | + sum += hi[i] - lo[i]; |
| 150 | + return sum; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @brief Compute the center point of the box |
| 156 | + */ |
| 157 | + std::array<Real, D> center() const { |
| 158 | + std::array<Real, D> c; |
| 159 | + for (int i = 0; i < D; i++) |
| 160 | + c[i] = (lo[i] + hi[i]) / 2; |
| 161 | + return c; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @brief Check if the box is valid (min <= max for all dimensions) |
| 166 | + */ |
| 167 | + bool is_valid() const { |
| 168 | + for (int i = 0; i < D; i++) { |
| 169 | + if (lo[i] > hi[i]) |
| 170 | + return false; |
| 171 | + } |
| 172 | + return true; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * @brief Check if the box is empty (zero volume) |
| 177 | + */ |
| 178 | + bool is_empty() const { return volume() == 0; } |
| 179 | + |
| 180 | + /** |
| 181 | + * @brief Expand the box to include a point |
| 182 | + */ |
| 183 | + void expand_to_include(const std::array<Real, D> &point) { |
| 184 | + for (int i = 0; i < D; i++) { |
| 185 | + lo[i] = std::min(lo[i], point[i]); |
| 186 | + hi[i] = std::max(hi[i], point[i]); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * @brief Expand the box to include another box |
| 192 | + */ |
| 193 | + void expand_to_include(const BB &other) { |
| 194 | + for (int i = 0; i < D; i++) { |
| 195 | + lo[i] = std::min(lo[i], other.lo[i]); |
| 196 | + hi[i] = std::max(hi[i], other.hi[i]); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + /// Serialization support |
| 201 | + template <class Archive> |
| 202 | + void serialize(Archive &ar) { |
| 203 | + ar(CEREAL_NVP(lo), CEREAL_NVP(hi)); |
| 204 | + } |
| 205 | +}; |
0 commit comments