载入中...
搜索中...
未找到
CrowdContactSolver.cpp
浏览该文件的文档.
1#include "common/Assert.h"
3namespace eve::crowd {
6 const int n = int(actions.size());
7 const float maxRadius = radii.empty() ? 0.f : *std::max_element(radii.begin(), radii.end());
8 const auto spend = [&](std::int64_t amount = 1) {
9 const auto limit = budgetActive ? activeBudget.maxPairVisits : std::numeric_limits<std::int64_t>::max();
10 if (amount > limit - workReport.pairVisits) throw WorkLimit{"Crowd pair construction budget exhausted"};
11 workReport.pairVisits += amount;
12 };
13 const auto maxPairs = budgetActive ? activeBudget.maxContactPairs : std::int64_t(1000000);
14 spend(std::int64_t(n) * 3);
15 correctionXs.assign(size_t(n), 0.f);
16 correctionYs.assign(size_t(n), 0.f);
17 pairCounts.assign(size_t(n), 0);
18 std::size_t total = 0;
19 for (int i = 0; i < n; ++i) {
20 spend();
21 forEachNeighbor(xs[size_t(i)], ys[size_t(i)], radii[size_t(i)] + maxRadius, [&](int j) {
22 if (j <= i || !canContact(size_t(i), size_t(j))) return;
23 spend();
24 if (total >= static_cast<std::uint64_t>(maxPairs)) throw WorkLimit{"Crowd contact pair capacity exhausted"};
25 ++pairCounts[size_t(i)];
26 ++total;
27 });
28 }
29 spend(std::int64_t(n) + 1);
30 pairStarts.resize(size_t(n) + 1);
31 std::size_t prefix = 0;
32 for (int i = 0; i < n; ++i) {
33 spend();
34 pairStarts[size_t(i)] = prefix;
35 prefix += pairCounts[size_t(i)];
36 }
37 pairStarts[size_t(n)] = prefix;
38 spend(static_cast<std::int64_t>(total));
39 contactPairs.resize(total);
40 workReport.contactPairsPeak = std::max(workReport.contactPairsPeak, static_cast<std::int64_t>(total));
41 for (int i = 0; i < n; ++i) {
42 std::size_t cursor = pairStarts[size_t(i)];
43 forEachNeighbor(xs[size_t(i)], ys[size_t(i)], radii[size_t(i)] + maxRadius, [&](int j) {
44 if (j <= i || !canContact(size_t(i), size_t(j))) return;
45 spend();
46 EV_ASSERT(cursor < pairStarts[size_t(i) + 1], "Contact snapshot changed during pair fill");
47 contactPairs[cursor++] = tieKeys[size_t(i)] < tieKeys[size_t(j)] ? ContactPair{i, j} : ContactPair{j, i};
48 });
49 EV_ASSERT(cursor == pairStarts[size_t(i) + 1], "Contact pair count/fill mismatch");
50 }
51 std::sort(contactPairs.begin(), contactPairs.end(), [&](const ContactPair& a, const ContactPair& b) {
52 spend();
53 if (tieKeys[size_t(a.left)] != tieKeys[size_t(b.left)])
54 return tieKeys[size_t(a.left)] < tieKeys[size_t(b.left)];
55 return tieKeys[size_t(a.right)] < tieKeys[size_t(b.right)];
56 });
57 // Reserve all bounded scratch work before allocation. Each pair has exactly two incidents.
58 spend(std::int64_t(n) * 3 + 1 + static_cast<std::int64_t>(total) * 6);
59 pairCounts.assign(size_t(n), 0);
60 for (const auto pair : contactPairs) {
61 ++pairCounts[size_t(pair.left)];
62 ++pairCounts[size_t(pair.right)];
63 }
64 incidentStarts.resize(size_t(n) + 1);
65 prefix = 0;
66 for (int i = 0; i < n; ++i) {
67 incidentStarts[size_t(i)] = prefix;
68 prefix += pairCounts[size_t(i)];
69 pairCounts[size_t(i)] = incidentStarts[size_t(i)];
70 }
71 incidentStarts[size_t(n)] = prefix;
72 incidentPairs.resize(total * 2);
73 for (std::size_t k = 0; k < total; ++k) {
74 const auto pair = contactPairs[k];
75 incidentPairs[pairCounts[size_t(pair.left)]++] = k;
76 incidentPairs[pairCounts[size_t(pair.right)]++] = k;
77 }
78 if (budgetActive &&
79 static_cast<std::int64_t>(total) > activeBudget.maxContactChecks - workReport.contactPairsChecked)
80 throw WorkLimit{"Crowd contact check budget exhausted"};
81 workReport.contactPairsChecked += static_cast<std::int64_t>(total);
82 contactDeltas.assign(total, ContactDelta{});
83 dispatchWork(total, [&](std::size_t first, std::size_t last) {
84 for (std::size_t k = first; k < last; ++k) {
85 const auto pair = contactPairs[k];
86 auto& delta = contactDeltas[k];
87 const int i = pair.left, j = pair.right;
88 float dx = xs[size_t(j)] - xs[size_t(i)];
89 float dy = ys[size_t(j)] - ys[size_t(i)];
90 float d2 = dx * dx + dy * dy;
91 const float minD = radii[size_t(i)] + radii[size_t(j)];
92 if (d2 >= minD * minD) continue;
93 const bool exactOverlap = d2 <= 1e-9f;
94 if (exactOverlap) {
95 const auto left = tieKeys[size_t(i)], right = tieKeys[size_t(j)];
96 const auto first = std::min(left, right), second = std::max(left, right);
97 const std::uint64_t hash =
98 first.value ^ (second.value + 0x9e3779b97f4a7c15ULL + (first.value << 6) + (first.value >> 2)) ^
99 (std::uint64_t(first.anonymous) << 62) ^ (std::uint64_t(second.anonymous) << 63);
100 const float angle = static_cast<float>(hash % 104729ULL) / 104729.0f * 6.28318530717958647692f;
101 dx = std::cos(angle);
102 dy = std::sin(angle);
103 if (left > right) {
104 dx = -dx;
105 dy = -dy;
106 }
107 d2 = 1.0f;
108 }
109 const float d = std::sqrt(d2);
110 const float push = minD - (exactOverlap ? 0.0f : d);
111 delta.penetration = push;
112 const float nx = dx / d;
113 const float ny = dy / d;
114 float leftShare = 0.5f;
115 float rightShare = 0.5f;
116 const auto& leftPolicy = interactions[size_t(i)];
117 const auto& rightPolicy = interactions[size_t(j)];
118 const float leftMobility = leftPolicy.holdPosition ? 0.f : leftPolicy.pushability;
119 const float rightMobility = rightPolicy.holdPosition ? 0.f : rightPolicy.pushability;
120 if (leftMobility + rightMobility == 0.f) continue;
121 leftShare = leftMobility / (leftMobility + rightMobility);
122 rightShare = 1.f - leftShare;
123 // Preserve the existing priority preference only when both can yield.
124 // Priority never overrides an explicit immovable/hold policy.
125 if (leftMobility > 0.f && rightMobility > 0.f &&
126 avoidancePriorities[size_t(i)] > avoidancePriorities[size_t(j)]) {
127 leftShare = 0.0f;
128 rightShare = 1.0f;
129 } else if (leftMobility > 0.f && rightMobility > 0.f &&
130 avoidancePriorities[size_t(j)] > avoidancePriorities[size_t(i)]) {
131 leftShare = 1.0f;
132 rightShare = 0.0f;
133 }
134 delta.leftX = nx * push * leftShare;
135 delta.leftY = ny * push * leftShare;
136 delta.rightX = nx * push * rightShare;
137 delta.rightY = ny * push * rightShare;
138 }
139 });
140 float maxPenetration = 0.f;
141 for (const auto& delta : contactDeltas) maxPenetration = std::max(maxPenetration, delta.penetration);
142 spend(static_cast<std::int64_t>(total) * 3 + n);
143 dispatchWork(size_t(n), [&](std::size_t first, std::size_t last) {
144 for (int i = int(first); i < int(last); ++i) {
145 // Incident indices retain canonical pair order, independent of worker completion order.
146 for (std::size_t cursor = incidentStarts[size_t(i)]; cursor < incidentStarts[size_t(i) + 1]; ++cursor) {
147 const auto k = incidentPairs[cursor];
148 const auto& delta = contactDeltas[k];
149 if (contactPairs[k].left == i) {
150 correctionXs[size_t(i)] -= delta.leftX;
151 correctionYs[size_t(i)] -= delta.leftY;
152 } else {
153 correctionXs[size_t(i)] += delta.rightX;
154 correctionYs[size_t(i)] += delta.rightY;
155 }
156 }
157 // Bound dense-cluster corrections so a single pass cannot eject an agent.
158 const float length = std::hypot(correctionXs[size_t(i)], correctionYs[size_t(i)]);
159 const float limit = std::max(radii[size_t(i)], 1e-4f);
160 const float scale = length > limit ? limit / length : 1.f;
161 xs[size_t(i)] += correctionXs[size_t(i)] * scale;
162 ys[size_t(i)] += correctionYs[size_t(i)] * scale;
163 }
164 });
165 return maxPenetration;
166}
167
168
169} // namespace eve::crowd
EVEngine assertion entry point, backed by zeroerr.
#define EV_ASSERT(cond,...)
Assert an internal engine invariant (state that must always hold).
Definition Assert.h:37
std::size_t cursor
glm::vec3 n
Definition Grass.cpp:63
HexVec3 left
HexVec3 right
std::int32_t second
std::int32_t first
float length
std::array< float, 3 > scale
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
glm::dvec3 dx
std::string hash
float d
float dy
Definition Shadow.cpp:151
int limit
Definition TreeMesh.cpp:166
std::int64_t maxContactChecks
Definition Crowd.h:77
std::int64_t maxPairVisits
Count/prefix/init/fill/sort work per advance.
Definition Crowd.h:80
std::int64_t maxContactPairs
Live candidate-pair entries per pass; never truncated.
Definition Crowd.h:79
std::vector< float > xs
SOA agent storage, indexed by compact slot id. @cost Linear in agent count; mutation keeps all owned ...
std::vector< std::size_t > incidentStarts
std::vector< float > correctionXs
std::vector< std::size_t > pairStarts
std::vector< float > ys
std::vector< std::size_t > pairCounts
std::vector< std::size_t > incidentPairs
bool canContact(size_t a, size_t b) const
std::vector< ContactPair > contactPairs
void dispatchWork(std::size_t items, const std::function< void(std::size_t, std::size_t)> &body)
std::vector< TieKey > tieKeys
void forEachNeighbor(float qx, float qy, float radius, Fn &&fn, StepReport *partitionReport=nullptr) const
std::vector< float > correctionYs
std::vector< InteractionPolicy > interactions
std::vector< ContactDelta > contactDeltas
std::vector< int32_t > avoidancePriorities
std::vector< int32_t > actions
std::vector< float > radii
std::int64_t contactPairsPeak
Largest candidate-pair buffer in this advance.
Definition Crowd.h:109
std::int64_t contactPasses
Contact projection iterations across all substeps.
Definition Crowd.h:114
std::int64_t contactPairsChecked
Unique interacting pairs tested during contact iterations.
Definition Crowd.h:112
std::int64_t pairVisits
Count/prefix/buffer-init/fill/sort work across contact passes.
Definition Crowd.h:110
Non-allocating internal unwind signal, translated at the checked simulation boundary.