载入中...
搜索中...
未找到
CrowdStep.cpp
浏览该文件的文档.
1// eve-hot-path
2#include "common/Profile.h"
4
5#include <chrono>
6
7namespace eve::crowd {
8
9Result<StepReport> Crowd::advanceWorking(float dt, const AdvanceBudget& budget) {
10 EV_PROFILE_MODULE("crowd", "Crowd::advance");
11 if (!std::isfinite(dt) || dt < 0.f)
13 Diagnostic::error(DiagnosticCode::InvalidArgument, "Crowd dt must be finite and non-negative", "dt"));
14 StepReport report;
15 report.navigation = getFieldStatus();
16 report.navigationClassMismatches = impl_->navigationMismatchCount();
17 if (dt == 0.f || impl_->xs.empty()) return Result<StepReport>::success(report, Status::success(StatusCode::NoOp));
18
19 auto& d = *impl_;
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;
24 float maxSpeed = 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]);
28 }
29 if (travelScale > 0.f && maxSpeed > 0.f) maxStep = std::min(maxStep, 0.5 * double(travelScale) / double(maxSpeed));
30 // Float dt=1/60 rounds slightly upward. Do not create an extra substep for
31 // representation error at an otherwise exact boundary.
32 const double count = std::ceil(double(dt) / maxStep - 1e-6);
33 if (!std::isfinite(count) || count > budget.maxSubsteps)
35 DiagnosticCode::PreconditionViolation, "Crowd step exceeds its substep budget; reduce dt or speed", "dt"));
36 report.substeps = std::max(1, static_cast<int>(count));
37 d.workReport = {};
38 const auto measure = [](std::int64_t& duration, auto&& work) {
39 const auto start = std::chrono::steady_clock::now();
40 work();
41 duration +=
42 std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - start).count();
43 };
44 const float substep = dt / float(report.substeps);
45 for (int sub = 0; sub < report.substeps; ++sub) {
46 d.simTime += substep;
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;
57 }
58 }
59 }
60
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;
74 });
75 if (d.field.valid()) {
76 float x = d.xs[i], y = d.ys[i];
77 d.field.resolvePenetration(x, y, d.radii[i]);
78 bool outside = false;
79 if (d.clampToField) {
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;
86 }
87 if (outside || std::hypot(x - d.xs[i], y - d.ys[i]) > 0.001f) ++report.unresolvedWalls;
88 }
89 }
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;
108 if (report.avoidanceTruncations > 0) quality |= static_cast<std::uint32_t>(SolveQuality::PredictionTruncated);
109 if (report.unresolvedContacts > 0) quality |= static_cast<std::uint32_t>(SolveQuality::ContactResidual);
110 if (report.unresolvedWalls > 0) quality |= static_cast<std::uint32_t>(SolveQuality::WallResidual);
111 report.quality = static_cast<SolveQuality>(quality);
113}
114
115void Crowd::step(float dt) { advance(dt).expect("Crowd step rejected"); }
116
117} // namespace eve::crowd
double y
double x
Duration start
float duration
double dt
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
Definition Profile.h:158
float d
int count
std::uint32_t depth
std::array< float, 4 > radii
int neighbor
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.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
FieldObservation getFieldStatus() const noexcept
Observe current field readiness and owner-local revision without rebuilding.
Definition Crowd.cpp:62
SolveQuality
Independent quality flags; complete is zero, truncation/residuals may coexist.
Definition Crowd.h:64
Vec3 sub(Vec3 a, Vec3 b)