载入中...
搜索中...
未找到
CrowdSpatialIndex.h
浏览该文件的文档.
1#pragma once
2#include <algorithm>
3#include <cmath>
4#include <compare>
5#include <cstdint>
6#include <limits>
7#include <span>
8#include <vector>
9#include "common/Export.h"
10
11namespace eve::crowd {
13struct WorkLimit {
14 const char* message;
15};
18 bool anonymous = true;
19 std::uint64_t value = 0;
20 friend auto operator<=>(const CrowdTieKey&, const CrowdTieKey&) = default;
21};
22
29 struct Member {
30 std::int64_t x, y;
31 CrowdTieKey tie;
32 int slot;
33 };
34 struct Cell {
35 std::int64_t x, y;
36 std::size_t begin, end;
37 };
38 struct Row {
39 std::int64_t y;
40 std::size_t begin, end;
41 };
42 std::vector<Member> members_;
43 std::vector<Cell> cells_;
44 std::vector<Row> rows_;
45 double cellSize_ = 1.0;
46 static std::int64_t coordinate(double value, double cell);
47 static std::int64_t queryBound(double value, double cell) noexcept;
48
49public:
55 void rebuild(std::span<const float> xs, std::span<const float> ys, std::span<const CrowdTieKey> ties,
56 double cellSize, std::int64_t maxCells, std::int64_t& comparisons);
58 void clear() noexcept {
59 members_.clear();
60 cells_.clear();
61 rows_.clear();
62 }
64 [[nodiscard]] std::size_t cellCount() const noexcept { return cells_.size(); }
70 template <class Fn>
71 void visit(double x, double y, double radius, std::int64_t maxVisits, std::int64_t& visits,
72 std::int64_t& visitedCells, Fn&& fn) const {
73 if (!std::isfinite(radius)) throw WorkLimit{"Crowd neighborhood radius is nonfinite"};
74 coordinate(x, cellSize_);
75 coordinate(y, cellSize_);
76 if (radius <= 0.0 || rows_.empty()) return;
77 const auto x0 = queryBound(x - radius, cellSize_), x1 = queryBound(x + radius, cellSize_);
78 const auto y0 = queryBound(y - radius, cellSize_), y1 = queryBound(y + radius, cellSize_);
79 const auto spend = [&] {
80 if (visits >= maxVisits) throw WorkLimit{"Crowd spatial lookup budget exhausted"};
81 ++visits;
82 };
83 auto row = std::lower_bound(rows_.begin(), rows_.end(), y0, [&](const Row& candidate, auto key) {
84 spend();
85 return candidate.y < key;
86 });
87 for (; row != rows_.end() && row->y <= y1; ++row) {
88 spend();
89 const auto end = cells_.begin() + row->end;
90 auto cell = std::lower_bound(cells_.begin() + row->begin, end, x0, [&](const Cell& candidate, auto key) {
91 spend();
92 return candidate.x < key;
93 });
94 for (; cell != end && cell->x <= x1; ++cell) {
95 spend();
96 ++visitedCells;
97 for (std::size_t i = cell->begin; i < cell->end; ++i) fn(members_[i].slot);
98 }
99 }
100 }
101
102};
103} // namespace eve::crowd
double y
double x
SQObjectPtr key
Definition ECS.cpp:155
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
double radius
Definition MeleeHit.cpp:42
Object::Ptr value
std::string slot
size_t begin
size_t end
Cell cell
Owning sparse broadphase workspace; integer cell/row order and stable tie-key bucket order....
void clear() noexcept
Invalidate derived directories without releasing reusable capacity; no callbacks.
std::size_t cellCount() const noexcept
Observe occupied cell count; constant time, owner thread.
void visit(double x, double y, double radius, std::int64_t maxVisits, std::int64_t &visits, std::int64_t &visitedCells, Fn &&fn) const
Enumerate conservative candidates in a query box with explicit lookup-work charging....
SettlementPipeline::Stage fn
Canonical in-process tie ordering; named identities sort before anonymous admission ordinals.
friend auto operator<=>(const CrowdTieKey &, const CrowdTieKey &)=default
Non-allocating internal unwind signal, translated at the checked simulation boundary.