Find a bug in spatial key (oversight, really)

This commit is contained in:
ExeVirus 2024-05-11 07:21:53 -04:00
parent 39c5c73e42
commit 5f262780f2
3 changed files with 25 additions and 17 deletions

View File

@ -81,16 +81,16 @@ void benchGetObjectsInArea(Catch::Benchmark::Chronometer &meter)
return false;
};
fill(mgr, N);
// mgr.m_spatial_map.cacheUpdate([&mgr](u16 id)
// {
// auto obj = mgr.getActiveObject(id);
// if(obj != nullptr) {
// return obj->getBasePosition();
// } else {
// mgr.m_spatial_map.remove(id);
// return v3f();
// }
// });
mgr.m_spatial_map.cacheUpdate([&mgr](u16 id)
{
auto obj = mgr.getActiveObject(id);
if(obj != nullptr) {
return obj->getBasePosition();
} else {
mgr.m_spatial_map.remove(id);
return v3f();
}
});
v3f pos, off;
meter.measure([&] {
x = 0;
@ -101,7 +101,9 @@ void benchGetObjectsInArea(Catch::Benchmark::Chronometer &meter)
return x;
});
REQUIRE(result.empty());
mgr.getObjectsInAreaDumb({v3f(-2000,-20,-2000), v3f(2000,60,2000)}, result2, cb);
mgr.getObjectsInAreaDumb({v3f(0,-20,-2000), v3f(2000,60,2000)}, result2, [](ServerActiveObject *obj) { return true; });
//mgr.getObjectsInArea({v3f(0,-20,-2000), v3f(2000,60,2000)}, result, [](ServerActiveObject *obj) { return true; });
std::cout << "numberSmart:" << result.size() << std::endl;
std::cout << "numberDumb:" << result2.size() << std::endl;

View File

@ -126,7 +126,7 @@ void SpatialMap::getRelevantObjectIds(const aabb3f &box, const std::function<voi
for (s16 x = min.X; x < max.X;x++) {
for (s16 y = min.Y; y < max.Y;y++) {
for (s16 z = min.Z; z < max.Z;z++) {
SpatialKey key(x,y,z);
SpatialKey key(x,y,z, false);
if (m_cached.find(key) != m_cached.end()) {
auto range = m_cached.equal_range(key);
for (auto &it = range.first; it != range.second; ++it) {

View File

@ -43,17 +43,23 @@ public:
// Use the same basic algorithm for both area and radius lookups
void getRelevantObjectIds(const aabb3f &box, const std::function<void(u16 id)> &callback);
protected:
//protected:
typedef struct SpatialKey{
u16 padding{0};
s16 x;
s16 y;
s16 z;
SpatialKey(s16 _x, s16 _y, s16 _z) {
x = _x >> 4;
y = _y >> 4;
z = _z >> 4;
SpatialKey(s16 _x, s16 _y, s16 _z, bool _shrink = true) {
if(_shrink) {
x = _x >> 4;
y = _y >> 4;
z = _z >> 4;
} else {
x = _x;
y = _y;
z = _z;
}
}
SpatialKey(v3f _pos) : SpatialKey(_pos.X, _pos.Y, _pos.Z){}