载入中...
搜索中...
未找到
SpatialHash2D.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include "common/Exception.h"
6
7// SpatialHash2D.cpp
8namespace eve::spatial {
9
10SpatialHash2D::SpatialHash2D(float cellSize) { setCellSize(cellSize); }
11
12void SpatialHash2D::setCellSize(float cellSize) {
13 if (cellSize <= 0.f) {
14 throw Exception("SpatialHash2D: cellSize must be > 0");
15 }
16 if (!items_.empty() && cellSize != cellSize_) {
17 // Rebuild with new size.
18 auto snapshot = items_;
19 clear();
20 cellSize_ = cellSize;
21 for (const auto &kv : snapshot) {
22 insert(kv.first, kv.second.minX, kv.second.minY, kv.second.maxX, kv.second.maxY);
23 }
24 return;
25 }
26 cellSize_ = cellSize;
27}
28
30 items_.clear();
31 cells_.clear();
32 results_.clear();
33}
34
35bool SpatialHash2D::contains(int id) const { return items_.find(id) != items_.end(); }
36
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_));
42}
43
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) {
49 cells_[cellKey2(cx, cy)].push_back(id);
50 }
51 }
52}
53
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) {
59 auto it = cells_.find(cellKey2(cx, cy));
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);
64 }
65 }
66}
67
68bool SpatialHash2D::insert(int id, float minX, float minY, float maxX, float maxY) {
69 AABB2 b = makeAABB2(minX, minY, maxX, maxY);
70 if (!b.valid()) return false;
71 if (contains(id)) remove(id);
72 items_[id] = b;
73 insertCells(id, b);
74 return true;
75}
76
78 auto it = items_.find(id);
79 if (it == items_.end()) return false;
80 eraseCells(id, it->second);
81 items_.erase(it);
82 return true;
83}
84
85bool SpatialHash2D::update(int id, float minX, float minY, float maxX, float maxY) {
86 auto it = items_.find(id);
87 if (it == items_.end()) return false;
88 const AABB2 next = makeAABB2(minX, minY, maxX, maxY);
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) {
94 // Retain the legacy update-to-end order without rebuilding occupied buckets.
95 for (int y = oldMinY; y <= oldMaxY; ++y) {
96 for (int x = oldMinX; x <= oldMaxX; ++x) {
97 auto &bucket = cells_.at(cellKey2(x, y));
98 const auto position = std::find(bucket.begin(), bucket.end(), id);
99 std::rotate(position, position + 1, bucket.end());
100 }
101 }
102 it->second = next;
103 return true;
104 }
105 eraseCells(id, it->second);
106 it->second = next;
107 insertCells(id, next);
108 return true;
109}
110
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) {
113 results_.clear();
114 for (int y = minCY; y <= maxCY; ++y) {
115 for (int x = minCX; x <= maxCX; ++x) {
116 auto it = cells_.find(cellKey2(x, y));
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;
122 // Visit an item only in its first bucket intersecting this query.
123 // This preserves traversal order without allocating a set for every query.
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;
127 bool hit = false;
128 if (rect) {
129 hit = b.intersectsAABB(*rect);
130 } else if (useCircle) {
131 hit = b.intersectsCircle(cx, cy, radius);
132 } else if (usePoint) {
133 hit = b.containsPoint(cx, cy);
134 }
135 if (hit) results_.addUnchecked(id);
136 }
137 }
138 }
139}
140
141int SpatialHash2D::queryPoint(float x, float y) {
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);
145 return results_.getCount();
146}
147
148int SpatialHash2D::queryRect(float minX, float minY, float maxX, float maxY) {
149 AABB2 rect = makeAABB2(minX, minY, maxX, maxY);
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);
153 return results_.getCount();
154}
155
156int SpatialHash2D::queryCircle(float cx, float cy, float radius) {
157 if (radius < 0.f) radius = 0.f;
158 AABB2 rect = makeAABB2(cx - radius, cy - radius, cx + radius, cy + radius);
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);
162 return results_.getCount();
163}
164
165} // namespace eve::spatial
166
167// SpatialHash3D.cpp
168namespace eve::spatial {
169
170SpatialHash3D::SpatialHash3D(float cellSize) { setCellSize(cellSize); }
171
172void SpatialHash3D::setCellSize(float cellSize) {
173 if (cellSize <= 0.f) {
174 throw Exception("SpatialHash3D: cellSize must be > 0");
175 }
176 if (!items_.empty() && cellSize != cellSize_) {
177 auto snapshot = items_;
178 clear();
179 cellSize_ = cellSize;
180 for (const auto &kv : snapshot) {
181 insert(kv.first, kv.second.minX, kv.second.minY, kv.second.minZ, kv.second.maxX, kv.second.maxY,
182 kv.second.maxZ);
183 }
184 return;
185 }
186 cellSize_ = cellSize;
187}
188
190 items_.clear();
191 cells_.clear();
192 results_.clear();
193}
194
195bool SpatialHash3D::contains(int id) const { return items_.find(id) != items_.end(); }
196
197void SpatialHash3D::cellRange(const AABB3 &b, int &minCX, int &minCY, int &minCZ, int &maxCX, int &maxCY,
198 int &maxCZ) const {
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_));
205}
206
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) {
213 cells_[cellKey3(cx, cy, cz)].push_back(id);
214 }
215 }
216 }
217}
218
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) {
225 auto it = cells_.find(cellKey3(cx, cy, cz));
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);
230 }
231 }
232 }
233}
234
235bool SpatialHash3D::insert(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
236 AABB3 b = makeAABB3(minX, minY, minZ, maxX, maxY, maxZ);
237 if (!b.valid()) return false;
238 if (contains(id)) remove(id);
239 items_[id] = b;
240 insertCells(id, b);
241 return true;
242}
243
245 auto it = items_.find(id);
246 if (it == items_.end()) return false;
247 eraseCells(id, it->second);
248 items_.erase(it);
249 return true;
250}
251
252bool SpatialHash3D::update(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
253 if (!contains(id)) return false;
254 return insert(id, minX, minY, minZ, maxX, maxY, maxZ);
255}
256
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) {
259 results_.clear();
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) {
264 auto it = cells_.find(cellKey3(x, y, z));
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;
271 bool hit = false;
272 if (box) {
273 hit = b.intersectsAABB(*box);
274 } else if (useSphere) {
275 hit = b.intersectsSphere(cx, cy, cz, radius);
276 } else if (usePoint) {
277 hit = b.containsPoint(cx, cy, cz);
278 }
279 if (hit) results_.addUnchecked(id);
280 }
281 }
282 }
283 }
284}
285
286int SpatialHash3D::queryPoint(float x, float y, float z) {
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);
291 return results_.getCount();
292}
293
294int SpatialHash3D::queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
295 AABB3 box = makeAABB3(minX, minY, minZ, maxX, maxY, maxZ);
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);
299 return results_.getCount();
300}
301
302int SpatialHash3D::querySphere(float cx, float cy, float cz, float radius) {
303 if (radius < 0.f) radius = 0.f;
304 AABB3 box = makeAABB3(cx - radius, cy - radius, cz - radius, cx + radius, cy + radius, cz + radius);
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);
308 return results_.getCount();
309}
310
311} // namespace eve::spatial
double z
double y
double x
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
std::int32_t second
std::array< float, 3 > position
std::string id
Snapshot snapshot
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
bool hit
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
void clear()
Removes all stored entries.
Definition QueryIds.h:14
void addUnchecked(int id)
Appends id without uniqueness checks.
Definition QueryIds.h:25
int getCount() const
Number of stored ids.
Definition QueryIds.h:28
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).
Definition Bounds.h:108
AABB3 makeAABB3(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Builds a normalized 3D AABB (swaps inverted mins/maxes).
Definition Bounds.h:115
uint64_t cellKey3(int cx, int cy, int cz)
Packs 3D cell indices into a 63-bit hash key (21 bits/axis).
Definition Bounds.h:128
uint64_t cellKey2(int cx, int cy)
Integer cell key for spatial hashing (stable across platforms for reasonable ranges).
Definition Bounds.h:123
Axis-aligned 2D bounding box (min/max inclusive).
Definition Bounds.h:10
Axis-aligned 3D bounding box (min/max inclusive).
Definition Bounds.h:54
bool intersectsAABB(const AABB3 &o) const
True if the boxes overlap (inclusive edges).
Definition Bounds.h:90
uint32_t bucket