13 if (cellSize <= 0.f) {
14 throw Exception(
"SpatialHash2D: cellSize must be > 0");
16 if (!items_.empty() && cellSize != cellSize_) {
22 insert(kv.first, kv.second.minX, kv.second.minY, kv.second.maxX, kv.second.maxY);
37void SpatialHash2D::cellRange(
const AABB2 &
b,
int &minCX,
int &minCY,
int &maxCX,
int &maxCY)
const {
38 minCX =
static_cast<int>(std::floor(
b.minX / cellSize_));
39 minCY =
static_cast<int>(std::floor(
b.minY / cellSize_));
40 maxCX =
static_cast<int>(std::floor(
b.maxX / cellSize_));
41 maxCY =
static_cast<int>(std::floor(
b.maxY / cellSize_));
44void SpatialHash2D::insertCells(
int id,
const AABB2 &
b) {
45 int minCX, minCY, maxCX, maxCY;
46 cellRange(
b, minCX, minCY, maxCX, maxCY);
47 for (
int cy = minCY;
cy <= maxCY; ++
cy) {
48 for (
int cx = minCX;
cx <= maxCX; ++
cx) {
54void SpatialHash2D::eraseCells(
int id,
const AABB2 &
b) {
55 int minCX, minCY, maxCX, maxCY;
56 cellRange(
b, minCX, minCY, maxCX, maxCY);
57 for (
int cy = minCY;
cy <= maxCY; ++
cy) {
58 for (
int cx = minCX;
cx <= maxCX; ++
cx) {
60 if (it == cells_.end())
continue;
61 auto &vec = it->second;
62 vec.erase(std::remove(vec.begin(), vec.end(),
id), vec.end());
63 if (vec.empty()) cells_.erase(it);
70 if (!
b.valid())
return false;
78 auto it = items_.find(
id);
79 if (it == items_.end())
return false;
80 eraseCells(
id, it->second);
86 auto it = items_.find(
id);
87 if (it == items_.end())
return false;
89 if (!next.valid())
return false;
90 int oldMinX, oldMinY, oldMaxX, oldMaxY, nextMinX, nextMinY, nextMaxX, nextMaxY;
91 cellRange(it->second, oldMinX, oldMinY, oldMaxX, oldMaxY);
92 cellRange(next, nextMinX, nextMinY, nextMaxX, nextMaxY);
93 if (oldMinX == nextMinX && oldMinY == nextMinY && oldMaxX == nextMaxX && oldMaxY == nextMaxY) {
95 for (
int y = oldMinY;
y <= oldMaxY; ++
y) {
96 for (
int x = oldMinX;
x <= oldMaxX; ++
x) {
105 eraseCells(
id, it->second);
107 insertCells(
id, next);
111void SpatialHash2D::queryCells(
int minCX,
int minCY,
int maxCX,
int maxCY,
const AABB2 *rect,
float cx,
float cy,
112 float radius,
bool useCircle,
bool usePoint) {
114 for (
int y = minCY;
y <= maxCY; ++
y) {
115 for (
int x = minCX;
x <= maxCX; ++
x) {
117 if (it == cells_.end())
continue;
118 for (
int id : it->
second) {
119 auto item = items_.find(
id);
120 if (item == items_.end())
continue;
121 const AABB2 &
b = item->second;
124 const int firstX = std::max(minCX,
static_cast<int>(std::floor(
b.minX / cellSize_)));
125 const int firstY = std::max(minCY,
static_cast<int>(std::floor(
b.minY / cellSize_)));
126 if (
x != firstX ||
y != firstY)
continue;
129 hit =
b.intersectsAABB(*rect);
130 }
else if (useCircle) {
132 }
else if (usePoint) {
142 const int cx =
static_cast<int>(std::floor(
x / cellSize_));
143 const int cy =
static_cast<int>(std::floor(
y / cellSize_));
144 queryCells(
cx,
cy,
cx,
cy,
nullptr,
x,
y, 0.f,
false,
true);
150 int minCX, minCY, maxCX, maxCY;
151 cellRange(rect, minCX, minCY, maxCX, maxCY);
152 queryCells(minCX, minCY, maxCX, maxCY, &rect, 0.f, 0.f, 0.f,
false,
false);
159 int minCX, minCY, maxCX, maxCY;
160 cellRange(rect, minCX, minCY, maxCX, maxCY);
161 queryCells(minCX, minCY, maxCX, maxCY,
nullptr,
cx,
cy,
radius,
true,
false);
173 if (cellSize <= 0.f) {
174 throw Exception(
"SpatialHash3D: cellSize must be > 0");
176 if (!items_.empty() && cellSize != cellSize_) {
179 cellSize_ = cellSize;
181 insert(kv.first, kv.second.minX, kv.second.minY, kv.second.minZ, kv.second.maxX, kv.second.maxY,
186 cellSize_ = cellSize;
197void SpatialHash3D::cellRange(
const AABB3 &
b,
int &minCX,
int &minCY,
int &minCZ,
int &maxCX,
int &maxCY,
199 minCX =
static_cast<int>(std::floor(
b.minX / cellSize_));
200 minCY =
static_cast<int>(std::floor(
b.minY / cellSize_));
201 minCZ =
static_cast<int>(std::floor(
b.minZ / cellSize_));
202 maxCX =
static_cast<int>(std::floor(
b.maxX / cellSize_));
203 maxCY =
static_cast<int>(std::floor(
b.maxY / cellSize_));
204 maxCZ =
static_cast<int>(std::floor(
b.maxZ / cellSize_));
207void SpatialHash3D::insertCells(
int id,
const AABB3 &
b) {
208 int minCX, minCY, minCZ, maxCX, maxCY, maxCZ;
209 cellRange(
b, minCX, minCY, minCZ, maxCX, maxCY, maxCZ);
210 for (
int cz = minCZ; cz <= maxCZ; ++cz) {
211 for (
int cy = minCY;
cy <= maxCY; ++
cy) {
212 for (
int cx = minCX;
cx <= maxCX; ++
cx) {
219void SpatialHash3D::eraseCells(
int id,
const AABB3 &
b) {
220 int minCX, minCY, minCZ, maxCX, maxCY, maxCZ;
221 cellRange(
b, minCX, minCY, minCZ, maxCX, maxCY, maxCZ);
222 for (
int cz = minCZ; cz <= maxCZ; ++cz) {
223 for (
int cy = minCY;
cy <= maxCY; ++
cy) {
224 for (
int cx = minCX;
cx <= maxCX; ++
cx) {
226 if (it == cells_.end())
continue;
227 auto &vec = it->second;
228 vec.erase(std::remove(vec.begin(), vec.end(),
id), vec.end());
229 if (vec.empty()) cells_.erase(it);
237 if (!
b.valid())
return false;
245 auto it = items_.find(
id);
246 if (it == items_.end())
return false;
247 eraseCells(
id, it->second);
254 return insert(
id, minX, minY, minZ, maxX, maxY, maxZ);
257void SpatialHash3D::queryCells(
int minCX,
int minCY,
int minCZ,
int maxCX,
int maxCY,
int maxCZ,
const AABB3 *box,
258 float cx,
float cy,
float cz,
float radius,
bool useSphere,
bool usePoint) {
260 std::unordered_set<int> seen;
261 for (
int z = minCZ;
z <= maxCZ; ++
z) {
262 for (
int y = minCY;
y <= maxCY; ++
y) {
263 for (
int x = minCX;
x <= maxCX; ++
x) {
265 if (it == cells_.end())
continue;
266 for (
int id : it->
second) {
267 if (!seen.insert(
id).second)
continue;
268 auto item = items_.find(
id);
269 if (item == items_.end())
continue;
270 const AABB3 &
b = item->second;
274 }
else if (useSphere) {
276 }
else if (usePoint) {
287 const int cx =
static_cast<int>(std::floor(
x / cellSize_));
288 const int cy =
static_cast<int>(std::floor(
y / cellSize_));
289 const int cz =
static_cast<int>(std::floor(
z / cellSize_));
290 queryCells(
cx,
cy, cz,
cx,
cy, cz,
nullptr,
x,
y,
z, 0.f,
false,
true);
296 int minCX, minCY, minCZ, maxCX, maxCY, maxCZ;
297 cellRange(box, minCX, minCY, minCZ, maxCX, maxCY, maxCZ);
298 queryCells(minCX, minCY, minCZ, maxCX, maxCY, maxCZ, &box, 0.f, 0.f, 0.f, 0.f,
false,
false);
305 int minCX, minCY, minCZ, maxCX, maxCY, maxCZ;
306 cellRange(box, minCX, minCY, minCZ, maxCX, maxCY, maxCZ);
307 queryCells(minCX, minCY, minCZ, maxCX, maxCY, maxCZ,
nullptr,
cx,
cy, cz,
radius,
true,
false);
std::array< float, 3 > position
EVENGINE_API_FOUNDATION public API.
void clear()
Removes all stored entries.
void addUnchecked(int id)
Appends id without uniqueness checks.
int getCount() const
Number of stored ids.
void clear()
Removes all stored entries.
bool remove(int id)
Removes an item by id; false if unknown.
bool contains(int id) const
True if the id is currently stored.
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.
void setCellSize(float cellSize)
Sets cell size and clears existing entries.
SpatialHash2D(float cellSize=64.f)
Creates a 2D spatial hash with the given cell size.
int queryPoint(float x, float y)
Finds items overlapping a point; fills the result buffer.
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 insert(int id, float minX, float minY, float maxX, float maxY)
Inserts an item AABB; false if out of bounds or id already present.
void clear()
Removes all stored entries.
SpatialHash3D(float cellSize=64.f)
Creates a 3D spatial hash with the given cell size.
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.
int querySphere(float cx, float cy, float cz, float radius)
Finds items overlapping a sphere; fills the result buffer.
void setCellSize(float cellSize)
Sets cell size and clears existing entries.
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 contains(int id) const
True if the id is currently stored.
bool remove(int id)
Removes an item by id; false if unknown.
int queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Finds items overlapping an AABB; fills the result buffer.
int queryPoint(float x, float y, float z)
Finds items overlapping a point; fills the result buffer.
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).
uint64_t cellKey3(int cx, int cy, int cz)
Packs 3D cell indices into a 63-bit hash key (21 bits/axis).
uint64_t cellKey2(int cx, int cy)
Integer cell key for spatial hashing (stable across platforms for reasonable ranges).
Axis-aligned 2D bounding box (min/max inclusive).
Axis-aligned 3D bounding box (min/max inclusive).
bool intersectsAABB(const AABB3 &o) const
True if the boxes overlap (inclusive edges).