9Result<StepReport> Crowd::advanceWorking(
float dt,
const AdvanceBudget& budget) {
11 if (!std::isfinite(
dt) ||
dt < 0.f)
16 report.navigationClassMismatches = impl_->navigationMismatchCount();
20 d.avoidanceChecks = 0;
21 d.avoidanceTruncations = 0;
22 double maxStep = 1.0 / 60.0;
23 float travelScale =
d.field.valid() ?
d.field.getCellSize() : 0.f;
25 for (
size_t i = 0; i <
d.xs.size(); ++i) {
26 if (
d.radii[i] > 0.f) travelScale = travelScale > 0.f ? std::min(travelScale,
d.radii[i]) :
d.
radii[i];
27 maxSpeed = std::max(maxSpeed,
d.maxSpeeds[i]);
29 if (travelScale > 0.f && maxSpeed > 0.f) maxStep = std::min(maxStep, 0.5 *
double(travelScale) /
double(maxSpeed));
32 const double count = std::ceil(
double(
dt) / maxStep - 1e-6);
33 if (!std::isfinite(
count) ||
count > budget.maxSubsteps)
36 report.substeps = std::max(1,
static_cast<int>(
count));
38 const auto measure = [](std::int64_t&
duration,
auto&& work) {
39 const auto start = std::chrono::steady_clock::now();
42 std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() -
start).count();
44 const float substep =
dt / float(report.substeps);
45 for (
int sub = 0;
sub < report.substeps; ++
sub) {
47 measure(report.gridNanoseconds, [&] { d.rebuildGrid(); });
48 measure(report.motionNanoseconds, [&] { d.stepAgents(substep); });
49 measure(report.wallNanoseconds, [&] { d.resolveWalls(); });
50 if (
d.resolveOverlaps) {
51 for (
int pass = 0; pass < budget.maxContactPasses; ++pass) {
52 measure(report.gridNanoseconds, [&] { d.rebuildGrid(); });
53 float penetration = 0.f;
54 measure(report.contactNanoseconds, [&] { penetration = d.resolveOverlapsPass(); });
55 measure(report.wallNanoseconds, [&] { d.resolveWalls(); });
56 if (penetration < 1e-4f)
break;
61 measure(report.gridNanoseconds, [&] { d.rebuildGrid(); });
62 const auto residualStart = std::chrono::steady_clock::now();
63 const float maxRadius = *std::max_element(
d.radii.begin(),
d.radii.end());
64 for (
size_t i = 0; i <
d.xs.size(); ++i) {
65 d.forEachNeighbor(
d.xs[i],
d.ys[i],
d.radii[i] + maxRadius, [&](
int neighbor) {
66 const size_t j = size_t(neighbor);
67 if (j <= i || !d.canContact(i, j)) return;
68 if (report.residualPairsChecked >= budget.maxContactChecks - d.workReport.contactPairsChecked)
69 throw WorkLimit{
"Crowd final contact inspection exceeds its contact budget"};
70 ++report.residualPairsChecked;
71 const float depth =
d.radii[i] +
d.radii[j] - std::hypot(
d.xs[j] -
d.xs[i],
d.ys[j] -
d.ys[i]);
72 report.maxPenetration = std::max(report.maxPenetration,
depth);
73 if (
depth > 0.001f) ++report.unresolvedContacts;
75 if (
d.field.valid()) {
76 float x =
d.xs[i],
y =
d.ys[i];
77 d.field.resolvePenetration(
x,
y,
d.radii[i]);
80 outside =
d.xs[i] -
d.radii[i] <
d.field.getOriginX() - 0.001f ||
81 d.ys[i] -
d.radii[i] <
d.field.getOriginY() - 0.001f ||
82 d.xs[i] +
d.radii[i] >
83 d.field.getOriginX() + float(
d.field.getWidth()) *
d.field.getCellSize() + 0.001f ||
84 d.ys[i] +
d.radii[i] >
85 d.field.getOriginY() + float(
d.field.getHeight()) *
d.field.getCellSize() + 0.001f;
87 if (outside || std::hypot(
x -
d.xs[i],
y -
d.ys[i]) > 0.001f) ++report.unresolvedWalls;
90 report.residualNanoseconds =
91 std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - residualStart).count();
92 report.gridBuilds =
d.workReport.gridBuilds;
93 report.gridCellsPeak =
d.workReport.gridCellsPeak;
94 report.cellsVisited =
d.workReport.cellsVisited;
95 report.gridEntriesPeak =
d.workReport.gridEntriesPeak;
96 report.gridSortComparisons =
d.workReport.gridSortComparisons;
97 report.spatialVisits =
d.workReport.spatialVisits;
98 report.contactPairsPeak =
d.workReport.contactPairsPeak;
99 report.pairVisits =
d.workReport.pairVisits;
100 report.preferredVisits =
d.workReport.preferredVisits;
101 report.preferredNeighborsPeak =
d.workReport.preferredNeighborsPeak;
102 report.candidatesVisited =
d.workReport.candidatesVisited;
103 report.contactPairsChecked =
d.workReport.contactPairsChecked;
104 report.contactPasses =
d.workReport.contactPasses;
105 report.avoidanceChecks =
d.avoidanceChecks;
106 report.avoidanceTruncations =
d.avoidanceTruncations;
107 std::uint32_t quality = 0;
115void Crowd::step(
float dt) { advance(
dt).expect(
"Crowd step rejected"); }
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
std::array< float, 4 > radii
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
FieldObservation getFieldStatus() const noexcept
Observe current field readiness and owner-local revision without rebuilding.
SolveQuality
Independent quality flags; complete is zero, truncation/residuals may coexist.