9 : rootBounds_(
makeAABB2(minX, minY, maxX, maxY)),
10 maxDepth_(
std::
max(1, maxDepth)),
11 maxPerNode_(
std::
max(1, maxPerNode)) {
12 if (!rootBounds_.
valid() || rootBounds_.
width() <= 0.f || rootBounds_.
height() <= 0.f) {
13 throw Exception(
"BSPTree2D: bounds must have positive area");
18BSPTree2D::Node *BSPTree2D::ensureRoot() {
20 root_ = std::make_unique<Node>();
21 root_->bounds = rootBounds_;
39 if (!
b.valid())
return false;
42 if (!insertInto(*ensureRoot(),
id,
b)) {
43 ensureRoot()->itemIds.push_back(
id);
57 return insert(
id, minX, minY, maxX, maxY);
60void BSPTree2D::rebuild() {
64 for (
const auto &kv : items_) {
65 if (!insertInto(*root_, kv.first, kv.second)) {
66 root_->itemIds.push_back(kv.first);
71bool BSPTree2D::insertInto(Node &
node,
int id,
const AABB2 &
bounds) {
73 if (!
node.bounds.intersectsAABB(
bounds))
return false;
79 node.itemIds.push_back(
id);
80 if (
static_cast<int>(
node.itemIds.size()) > maxPerNode_ &&
node.depth < maxDepth_) {
102 node.itemIds.push_back(
id);
106void BSPTree2D::split(Node &
node) {
108 node.split = (
node.axis == 0) ?
node.bounds.centerX() :
node.bounds.centerY();
110 node.left = std::make_unique<Node>();
111 node.right = std::make_unique<Node>();
114 node.left->axis =
node.left->depth % 2;
115 node.right->axis =
node.right->depth % 2;
117 if (
node.axis == 0) {
125 std::vector<int> remain;
126 remain.reserve(
node.itemIds.size());
127 for (
int id :
node.itemIds) {
128 auto it = items_.find(
id);
129 if (it == items_.end())
continue;
130 const AABB2 &
b = it->second;
131 if (
node.axis == 0) {
132 if (
b.maxX <=
node.split) {
133 node.left->itemIds.push_back(
id);
134 }
else if (
b.minX >=
node.split) {
135 node.right->itemIds.push_back(
id);
137 remain.push_back(
id);
140 if (
b.maxY <=
node.split) {
141 node.left->itemIds.push_back(
id);
142 }
else if (
b.minY >=
node.split) {
143 node.right->itemIds.push_back(
id);
145 remain.push_back(
id);
149 node.itemIds.swap(remain);
152void BSPTree2D::collect(Node &
node,
const AABB2 *rect,
float cx,
float cy,
float radius,
bool useCircle) {
153 if (rect && !
node.bounds.intersectsAABB(*rect))
return;
154 if (useCircle && !
node.bounds.intersectsCircle(
cx,
cy,
radius))
return;
156 for (
int id :
node.itemIds) {
157 auto it = items_.find(
id);
158 if (it == items_.end())
continue;
159 const AABB2 &
b = it->second;
162 hit =
b.intersectsAABB(*rect);
163 }
else if (useCircle) {
171 if (!
node.isLeaf()) {
179 collect(*ensureRoot(),
nullptr,
x,
y, 0.f,
false);
186 collect(*ensureRoot(), &rect, 0.f, 0.f, 0.f,
false);
193 collect(*ensureRoot(),
nullptr,
cx,
cy,
radius,
true);
204 : rootBounds_(
makeAABB3(minX, minY, minZ, maxX, maxY, maxZ)),
205 maxDepth_(
std::
max(1, maxDepth)),
206 maxPerNode_(
std::
max(1, maxPerNode)) {
207 if (!rootBounds_.
valid() || rootBounds_.
width() <= 0.f || rootBounds_.
height() <= 0.f ||
208 rootBounds_.
depth() <= 0.f) {
209 throw Exception(
"BSPTree3D: bounds must have positive volume");
214BSPTree3D::Node *BSPTree3D::ensureRoot() {
216 root_ = std::make_unique<Node>();
217 root_->bounds = rootBounds_;
233bool BSPTree3D::insert(
int id,
float minX,
float minY,
float minZ,
float maxX,
float maxY,
float maxZ) {
235 if (!
b.valid())
return false;
238 if (!insertInto(*ensureRoot(),
id,
b)) {
239 ensureRoot()->itemIds.push_back(
id);
251bool BSPTree3D::update(
int id,
float minX,
float minY,
float minZ,
float maxX,
float maxY,
float maxZ) {
253 return insert(
id, minX, minY, minZ, maxX, maxY, maxZ);
256void BSPTree3D::rebuild() {
260 for (
const auto &kv : items_) {
261 if (!insertInto(*root_, kv.first, kv.second)) {
262 root_->itemIds.push_back(kv.first);
267bool BSPTree3D::insertInto(Node &
node,
int id,
const AABB3 &
bounds) {
269 if (!
node.bounds.intersectsAABB(
bounds))
return false;
275 node.itemIds.push_back(
id);
276 if (
static_cast<int>(
node.itemIds.size()) > maxPerNode_ &&
node.depth < maxDepth_) {
282 float bMin = 0.f, bMax = 0.f;
283 if (
node.axis == 0) {
286 }
else if (
node.axis == 1) {
294 if (bMax <=
node.split &&
node.left) {
297 if (bMin >=
node.split &&
node.right) {
300 node.itemIds.push_back(
id);
304void BSPTree3D::split(Node &
node) {
306 if (
node.axis == 0) {
308 }
else if (
node.axis == 1) {
314 node.left = std::make_unique<Node>();
315 node.right = std::make_unique<Node>();
318 node.left->axis =
node.left->depth % 3;
319 node.right->axis =
node.right->depth % 3;
321 const AABB3 &
b =
node.bounds;
322 if (
node.axis == 0) {
325 }
else if (
node.axis == 1) {
333 std::vector<int> remain;
334 remain.reserve(
node.itemIds.size());
335 for (
int id :
node.itemIds) {
336 auto it = items_.find(
id);
337 if (it == items_.end())
continue;
338 const AABB3 &ib = it->second;
339 float iMin = 0.f, iMax = 0.f;
340 if (
node.axis == 0) {
343 }
else if (
node.axis == 1) {
350 if (iMax <=
node.split) {
351 node.left->itemIds.push_back(
id);
352 }
else if (iMin >=
node.split) {
353 node.right->itemIds.push_back(
id);
355 remain.push_back(
id);
358 node.itemIds.swap(remain);
361void BSPTree3D::collect(Node &
node,
const AABB3 *box,
float cx,
float cy,
float cz,
float radius,
bool useSphere) {
362 if (box && !
node.bounds.intersectsAABB(*box))
return;
363 if (useSphere && !
node.bounds.intersectsSphere(
cx,
cy, cz,
radius))
return;
365 for (
int id :
node.itemIds) {
366 auto it = items_.find(
id);
367 if (it == items_.end())
continue;
368 const AABB3 &
b = it->second;
371 hit =
b.intersectsAABB(*box);
372 }
else if (useSphere) {
380 if (!
node.isLeaf()) {
388 collect(*ensureRoot(),
nullptr,
x,
y,
z, 0.f,
false);
395 collect(*ensureRoot(), &box, 0.f, 0.f, 0.f, 0.f,
false);
402 collect(*ensureRoot(),
nullptr,
cx,
cy, cz,
radius,
true);
EVENGINE_API_FOUNDATION public API.
void clear()
Removes all stored entries.
bool update(int id, float minX, float minY, float maxX, float maxY)
Moves an existing item to a new AABB; false if unknown or out of bounds.
BSPTree2D(float minX, float minY, float maxX, float maxY, int maxDepth=12, int maxPerNode=8)
Creates a 2D BSP/kd tree covering the given bounds.
int queryPoint(float x, float y)
Finds items overlapping a point; fills the result buffer.
bool contains(int id) const
True if the id is currently stored.
int queryRect(float minX, float minY, float maxX, float maxY)
Finds items overlapping an AABB; fills the result buffer.
int queryCircle(float cx, float cy, float radius)
Finds items overlapping a circle; fills the result buffer.
bool remove(int id)
Removes an item by id; false if unknown.
bool insert(int id, float minX, float minY, float maxX, float maxY)
Inserts an item AABB; false if out of bounds or id already present.
bool contains(int id) const
True if the id is currently stored.
bool remove(int id)
Removes an item by id; false if unknown.
void clear()
Removes all stored entries.
int queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Finds items overlapping an AABB; fills the result buffer.
int querySphere(float cx, float cy, float cz, float radius)
Finds items overlapping a sphere; fills the result buffer.
BSPTree3D(float minX, float minY, float minZ, float maxX, float maxY, float maxZ, int maxDepth=12, int maxPerNode=8)
Creates a 3D BSP/kd tree covering the given bounds.
int queryPoint(float x, float y, float z)
Finds items overlapping a point; fills the result buffer.
bool update(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Moves an existing item to a new AABB; false if unknown or out of bounds.
bool insert(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Inserts an item AABB; false if out of bounds or id already present.
void clear()
Removes all stored entries.
int getCount() const
Number of stored ids.
void addUnique(int id)
Appends id if not already present.
AABB2 makeAABB2(float minX, float minY, float maxX, float maxY)
Builds a normalized 2D AABB (swaps inverted mins/maxes).
AABB3 makeAABB3(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Builds a normalized 3D AABB (swaps inverted mins/maxes).
Axis-aligned 2D bounding box (min/max inclusive).
bool valid() const
True when min <= max on every axis.
float width() const
Extent along X (max - min).
float height() const
Extent along Y (max - min).
Axis-aligned 3D bounding box (min/max inclusive).
float height() const
Extent along Y (max - min).
bool valid() const
True when min <= max on every axis.
float width() const
Extent along X (max - min).
float depth() const
Extent along Z (max - min).