Add exact periodic neighborhood queries and measured scan baseline
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
#include "evo/simulation/spatial.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace evo {
|
||||
namespace {
|
||||
std::uint64_t axis_distance(Millimetres first, Millimetres second) noexcept {
|
||||
const auto direct =
|
||||
first.value > second.value ? first.value - second.value : second.value - first.value;
|
||||
return std::min(direct, domain_extent_mm - direct);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<std::vector<EntityId>, Error> query_neighbors(const SimulationWorld& world,
|
||||
Position2 center, Millimetres radius) {
|
||||
if (center.x.value >= domain_extent_mm || center.y.value >= domain_extent_mm) {
|
||||
return std::unexpected{Error::invalid_position};
|
||||
}
|
||||
const auto squared_radius = static_cast<std::uint64_t>(radius.value) * radius.value;
|
||||
std::vector<EntityId> matches;
|
||||
for (const auto& entity : world.entities()) {
|
||||
const auto dx = axis_distance(center.x, entity.position.x);
|
||||
const auto dy = axis_distance(center.y, entity.position.y);
|
||||
if (dx * dx + dy * dy <= squared_radius) {
|
||||
matches.push_back(entity.id);
|
||||
}
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
} // namespace evo
|
||||
Reference in New Issue
Block a user