14 spendPreferredWork(
static_cast<std::int64_t
>(
n) * 2 + 1);
15 preferredCounts.assign(
n, 0);
16 preferredStarts.resize(
n + 1);
17 const bool forces = sepWeight > 0.f || alignWeight > 0.f || cohesionWeight > 0.f;
18 const float queryR = std::max(sepRadius, perceiveRadius);
19 const auto maxEntries = budgetActive ? activeBudget.maxPreferredNeighbors : std::int64_t(1000000);
20 const auto visit = [&](
size_t i,
const auto& accept) {
21 if (!forces || interactions[i].holdPosition)
return;
22 forEachNeighbor(xs[i], ys[i], queryR, [&](
int j) {
23 if (!canAvoid(i,
size_t(j)))
return;
24 const float dx = xs[size_t(j)] - xs[i],
dy = ys[size_t(j)] - ys[i];
26 if (d2 <= 0.f)
return;
27 if (!(sepWeight > 0.f && d2 <= sepRadius * sepRadius) &&
28 !((alignWeight > 0.f || cohesionWeight > 0.f) && d2 <= perceiveRadius * perceiveRadius))
34 for (
size_t i = 0; i <
n; ++i) {
35 spendPreferredWork(1);
37 spendPreferredWork(1);
38 if (total >=
static_cast<std::uint64_t
>(maxEntries))
39 throw WorkLimit{
"Crowd preferred neighbor capacity exhausted"};
45 for (
size_t i = 0; i <
n; ++i) {
46 spendPreferredWork(1);
47 preferredStarts[i] = prefix;
48 prefix += preferredCounts[i];
50 preferredStarts[
n] = prefix;
51 spendPreferredWork(
static_cast<std::int64_t
>(total));
52 preferredNeighbors.resize(total);
53 workReport.preferredNeighborsPeak = std::max(workReport.preferredNeighborsPeak,
static_cast<std::int64_t
>(total));
54 for (
size_t i = 0; i <
n; ++i) {
55 spendPreferredWork(1);
56 size_t cursor = preferredStarts[i];
58 spendPreferredWork(1);
59 EV_ASSERT(
cursor < preferredStarts[i + 1],
"Preferred snapshot changed during fill");
60 preferredNeighbors[
cursor++] = j;
62 EV_ASSERT(
cursor == preferredStarts[i + 1],
"Preferred count/fill mismatch");
67 preparePreferredNeighbors();
68 spendPreferredWork(
n +
static_cast<std::int64_t
>(preferredNeighbors.size()));
69 spendPreferredWork(std::int64_t(
n) * 2);
71 for (
size_t i = 0; i <
actions.size(); ++i) {
72 if (
actions[i] != kFlow || interactions[i].holdPosition)
continue;
73 auto& flow = preferredFlows[i];
74 float fx = 0.f, fy = 0.f;
75 const auto* navigation = navigationField(i);
78 if (reachable) navigation->flowAtWorld(xs[i], ys[i], fx, fy);
79 if (reachable && navigation->isBuilt()) {
80 const float cell = navigation->getCellSize();
81 const int cx = int(std::floor((
double(xs[i]) - navigation->getOriginX()) /
cell));
82 const int cy = int(std::floor((
double(ys[i]) - navigation->getOriginY()) /
cell));
83 static constexpr int dx[4] = {1, -1, 0, 0},
dy[4] = {0, 0, 1, -1};
84 spendPreferredWork(8);
86 float best = navigation->costAtCell(
cx,
cy);
88 for (
int side = 0; side < 4; ++side) {
90 if (
nx >= 0 &&
ny >= 0 && nx < navigation->getWidth() && ny < navigation->getHeight() &&
91 !navigation->isReachable(
nx,
ny))
93 const float value = navigation->costAtCell(
nx,
ny);
100 const float centerX = navigation->getOriginX() + (float(
cx) + 0.5f) *
cell;
101 const float centerY = navigation->getOriginY() + (float(
cy) + 0.5f) *
cell;
102 const float inset = std::min(
radii[i] + (avoidance.enabled ? avoidance.margin : 0.f),
cell * 0.49f);
103 const bool aligned =
dx[next] != 0 ? std::fabs(ys[i] - centerY) <=
cell * 0.5f - inset
104 : std::fabs(xs[i] - centerX) <=
cell * 0.5f - inset;
106 fx = centerX + (aligned ? float(
dx[next]) *
cell : 0.f) - xs[i];
107 fy = centerY + (aligned ? float(
dy[next]) *
cell : 0.f) - ys[i];
108 const float length = std::hypot(fx, fy);
118 const float c =
cost;
119 const float cell = std::max(navigation->getCellSize(), 1e-6f);
120 const float arriveCells = arriveRadius /
cell;
121 if (c < CrowdField::kUnreachable && arriveCells > 0.f &&
c < arriveCells) {
129 dispatchWork(
size_t(
n), [&](std::size_t
first, std::size_t last) {
130 for (
int i =
int(
first); i < int(last); ++i) {
131 if (interactions[
size_t(i)].holdPosition) {
132 preferredVxs[size_t(i)] = preferredVys[size_t(i)] = 0.f;
133 arriveFactors[size_t(i)] = 1.f;
136 const float x = xs[size_t(i)];
137 const float y = ys[size_t(i)];
138 const float maxSpeed = maxSpeeds[size_t(i)];
143 float desX = 0.f, desY = 0.f;
145 const auto flow = preferredFlows[size_t(i)];
148 arriveK = flow.arrive;
149 }
else if (
action == kSeek && hasTargets[
size_t(i)]) {
150 const float tx = targetXs[size_t(i)] -
x;
151 const float ty = targetYs[size_t(i)] -
y;
152 const float d = std::sqrt(tx * tx + ty * ty);
156 if (
d < arriveRadius) {
157 const float k =
d / arriveRadius;
162 }
else if (
action == kBoids && hasTargets[
size_t(i)] && goalWeight > 0.f) {
163 const float tx = targetXs[size_t(i)] -
x;
164 const float ty = targetYs[size_t(i)] -
y;
165 const float d = std::sqrt(tx * tx + ty * ty);
167 desX = tx /
d * goalWeight;
168 desY = ty /
d * goalWeight;
175 if (sepWeight > 0.f || alignWeight > 0.f || cohesionWeight > 0.f) {
176 float sx = 0.f,
sy = 0.f;
177 float axv = 0.f, ayv = 0.f;
178 float cxv = 0.f, cyv = 0.f;
179 int alignN = 0, cohN = 0;
180 for (
size_t cursor = preferredStarts[
size_t(i)];
cursor < preferredStarts[size_t(i) + 1]; ++
cursor) {
181 const int j = preferredNeighbors[
cursor];
182 const float dx = xs[size_t(j)] -
x;
183 const float dy = ys[size_t(j)] -
y;
185 if (d2 <= 0.f)
continue;
186 if (sepWeight > 0.f && d2 <= sepRadius * sepRadius) {
187 const float d = std::sqrt(d2);
188 const float fall = 1.f -
d / sepRadius;
189 sx += (
x - xs[size_t(j)]) /
d * fall;
190 sy += (
y - ys[size_t(j)]) /
d * fall;
192 if (alignWeight > 0.f && d2 <= perceiveRadius * perceiveRadius) {
193 axv += vxs[size_t(j)];
194 ayv += vys[size_t(j)];
197 if (cohesionWeight > 0.f && d2 <= perceiveRadius * perceiveRadius) {
198 cxv += xs[size_t(j)];
199 cyv += ys[size_t(j)];
203 desX +=
sx * sepWeight * maxSpeed;
204 desY +=
sy * sepWeight * maxSpeed;
206 const float al = std::sqrt(axv * axv + ayv * ayv);
208 desX += axv / al * alignWeight * maxSpeed;
209 desY += ayv / al * alignWeight * maxSpeed;
213 const float cmx = cxv / float(cohN) -
x;
214 const float cmy = cyv / float(cohN) -
y;
215 const float cl = std::sqrt(cmx * cmx + cmy * cmy);
217 desX += cmx / cl * cohesionWeight * maxSpeed;
218 desY += cmy / cl * cohesionWeight * maxSpeed;
224 if (
action == kBoids && wanderWeight > 0.f) {
225 wanderPhases[size_t(i)] +=
dt * 2.5f;
226 desX += std::cos(wanderPhases[
size_t(i)]) * wanderWeight * maxSpeed;
227 desY += std::sin(wanderPhases[
size_t(i)]) * wanderWeight * maxSpeed;
231 float dl = std::sqrt(desX * desX + desY * desY);
232 if (dl > maxSpeed && dl > 1e-6f) {
233 desX *= maxSpeed / dl;
234 desY *= maxSpeed / dl;
237 preferredVxs[size_t(i)] = desX;
238 preferredVys[size_t(i)] = desY;
239 arriveFactors[size_t(i)] = arriveK;