载入中...
搜索中...
未找到
CrowdAdvance.cpp
浏览该文件的文档.
2
3#include <new>
4
5namespace eve::crowd {
6
9 float oldTime;
11 std::int64_t oldPredictions;
13 bool committed = false;
14
16 : data(value),
21 auto& saved = data.rollback;
22 saved.xs.assign(data.xs.begin(), data.xs.end());
23 saved.ys.assign(data.ys.begin(), data.ys.end());
24 saved.headings.assign(data.headings.begin(), data.headings.end());
25 saved.vxs.assign(data.vxs.begin(), data.vxs.end());
26 saved.vys.assign(data.vys.begin(), data.vys.end());
27 saved.speeds.assign(data.speeds.begin(), data.speeds.end());
28 saved.wanderPhases.assign(data.wanderPhases.begin(), data.wanderPhases.end());
29 data.activeBudget = budget;
30 data.budgetActive = true;
31 }
33 data.budgetActive = false;
34 if (committed) return;
35 auto& saved = data.rollback;
36 data.xs.swap(saved.xs);
37 data.ys.swap(saved.ys);
38 data.headings.swap(saved.headings);
39 data.vxs.swap(saved.vxs);
40 data.vys.swap(saved.vys);
41 data.speeds.swap(saved.speeds);
42 data.wanderPhases.swap(saved.wanderPhases);
48 data.contactPairs.clear();
49 data.contactDeltas.clear();
52 data.preferredStarts.clear();
53 data.preferredCounts.clear();
54 data.preferredFlows.clear();
55 data.incidentStarts.clear();
56 data.incidentPairs.clear();
57 }
58};
59
61 if (!std::isfinite(dt) || dt < 0.f)
63 Diagnostic::error(DiagnosticCode::InvalidArgument, "Crowd dt must be finite and nonnegative", "dt"));
65 if (!duration) return Result<StepReport>::failure(duration.status());
66 SimulationTick tick = impl_->tick;
67 if (!duration.value().isZero()) {
68 const auto next = tick.incremented();
69 if (!next)
71 Diagnostic::error(DiagnosticCode::PreconditionViolation, "Crowd tick exhausted", "tick"));
72 tick = *next;
73 }
74 return advanceInternal({tick, duration.value(), {}}, ClockAuthority::Compatibility);
75}
76
77Result<StepReport> Crowd::advance(const CrowdStep& step) { return advanceInternal(step, ClockAuthority::External); }
78
80 return checkClock(step, ClockAuthority::External);
81}
82
83Result<void> Crowd::checkClock(const CrowdStep& step, ClockAuthority authority) const {
84 const auto& d = *impl_;
85 const auto fail = [](const char* message) {
87 };
88 if (d.budgetActive) return fail("Crowd advancement cannot reenter");
89 if (step.delta.nanoseconds() < 0)
91 Diagnostic::error(DiagnosticCode::InvalidArgument, "Negative Crowd duration", "crowdStep.delta"));
92 if (step.delta.isZero()) {
93 if (step.tick != d.tick) return fail("Zero-duration step must reconcile the current tick");
94 return Result<void>::success();
95 }
96 const auto next = d.tick.incremented();
97 if (!next || step.tick != *next) return fail("Crowd step must consume the next contiguous tick");
98 if (d.clock && *d.clock != authority) return fail("Cannot mix compatibility and external Crowd clocks");
99 auto elapsed = d.elapsed.tryAdd(step.delta);
100 if (!elapsed) return Result<void>::failure(elapsed.status());
101 return Result<void>::success();
102}
103
104Result<StepReport> Crowd::advanceInternal(const CrowdStep& step, ClockAuthority authority) try {
105 const auto failure = [](DiagnosticCode code, const char* message) {
107 };
108 auto& d = *impl_;
109 auto admitted = checkClock(step, authority);
110 if (!admitted) return Result<StepReport>::failure(admitted.status());
111 const auto& budget = step.budget;
112 if (step.delta.nanoseconds() < 0 || budget.maxSubsteps < 1 || budget.maxSubsteps > 1024 ||
113 budget.maxContactPasses < 1 || budget.maxContactPasses > 12 || budget.maxGridCells < 1 ||
114 budget.maxNeighborVisits < 0 || budget.maxPredictionChecks < 0 || budget.maxContactChecks < 0 ||
115 budget.maxSpatialVisits < 0 || budget.maxContactPairs < 0 || budget.maxContactPairs > 10000000 ||
116 budget.maxPairVisits < 0 || budget.maxPreferredVisits < 0 || budget.maxPreferredNeighbors < 0 ||
117 budget.maxPreferredNeighbors > 10000000)
118 return failure(DiagnosticCode::InvalidArgument, "Invalid duration or work budget");
119 if (step.delta.isZero()) {
120 StepReport report;
121 report.navigation = getFieldStatus();
122 report.navigationClassMismatches = d.navigationMismatchCount();
123 report.tick = d.tick;
124 report.elapsed = d.elapsed;
126 }
127 auto elapsed = d.elapsed.tryAdd(step.delta);
128 if (!elapsed) return Result<StepReport>::failure(elapsed.status());
129 for (const float parameter : {d.arriveRadius, d.sepRadius, d.perceiveRadius, d.sepWeight, d.alignWeight,
130 d.cohesionWeight, d.wanderWeight, d.goalWeight})
131 if (!std::isfinite(parameter))
132 return failure(DiagnosticCode::PreconditionViolation, "Crowd steering parameters are nonfinite");
133 for (std::size_t index = 0; index < d.xs.size(); ++index) {
134 if (!std::isfinite(d.xs[index]) || !std::isfinite(d.ys[index]) || !std::isfinite(d.vxs[index]) ||
135 !std::isfinite(d.vys[index]) || !std::isfinite(d.headings[index]) ||
136 !std::isfinite(d.wanderPhases[index]) || !std::isfinite(d.radii[index]) || d.radii[index] < 0.f ||
137 !std::isfinite(d.maxSpeeds[index]) || d.maxSpeeds[index] < 0.f || !std::isfinite(d.maxAccels[index]) ||
138 d.maxAccels[index] < 0.f || !std::isfinite(d.turnRates[index]) || d.turnRates[index] < 0.f)
139 return failure(DiagnosticCode::PreconditionViolation, "Crowd geometry or motion limits are invalid");
140 }
141 Impl::AdvanceGuard guard(d, budget);
142 auto advanced = advanceWorking(static_cast<float>(step.delta.seconds()), budget);
143 if (!advanced) return Result<StepReport>::failure(advanced.status());
144 for (std::size_t index = 0; index < d.xs.size(); ++index)
145 if (!std::isfinite(d.xs[index]) || !std::isfinite(d.ys[index]) || !std::isfinite(d.vxs[index]) ||
146 !std::isfinite(d.vys[index]) || !std::isfinite(d.headings[index]) || !std::isfinite(d.speeds[index]) ||
147 !std::isfinite(d.wanderPhases[index]))
148 return failure(DiagnosticCode::Failed, "Crowd solver produced nonfinite motion");
149 auto report = std::move(advanced).takeValue();
150 report.tick = step.tick;
151 report.elapsed = elapsed.value();
152 d.tick = step.tick;
153 d.elapsed = elapsed.value();
154 d.clock = authority;
155 d.simTime = static_cast<float>(d.elapsed.seconds());
156 guard.committed = true;
158} catch (const Impl::SchedulerFailure& failure) {
159 return Result<StepReport>::failure(failure.status);
160} catch (const WorkLimit& limit) {
163} catch (const std::bad_alloc&) {
165 Diagnostic::error(DiagnosticCode::Failed, "Cannot allocate Crowd step workspace", "crowdStep"));
166}
167
168} // namespace eve::crowd
float duration
std::string message
DiagnosticCode code
double dt
Object::Ptr value
float d
std::string parameter
SimulationTick tick
int limit
Definition TreeMesh.cpp:166
float step
Definition TreeMesh.cpp:451
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
static Result< Duration > fromSeconds(double seconds)
Convert finite seconds to the nearest nanosecond.
Definition Time.cpp:17
Move-only operation result carrying either a value or Status.
Definition Result.h:176
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
void clear() noexcept
Invalidate derived directories without releasing reusable capacity; no callbacks.
FieldObservation getFieldStatus() const noexcept
Observe current field readiness and owner-local revision without rebuilding.
Definition Crowd.cpp:62
Result< StepReport > advance(float dt)
Advance using bounded simultaneous integration and contact projection.
Result< void > checkAdvanceClock(const CrowdStep &step) const
Validate external clock admission before a consumer publishes its input batch.
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
StatusCode
Stable outcome category for an operation.
Definition Status.h:28
Deterministic operation budgets; exhaustion rejects the entire step before publication.
Definition Crowd.h:71
Explicit owner step; positive duration consumes the next contiguous tick. Zero reconciles current tic...
Definition Crowd.h:88
AdvanceGuard(Impl &value, const AdvanceBudget &budget)
std::vector< std::size_t > preferredStarts
std::vector< std::size_t > preferredCounts
std::vector< float > xs
SOA agent storage, indexed by compact slot id. @cost Linear in agent count; mutation keeps all owned ...
std::vector< float > headings
std::vector< std::size_t > incidentStarts
struct eve::crowd::Crowd::Impl::MotionBackup rollback
std::vector< float > vys
std::vector< float > ys
std::vector< std::size_t > incidentPairs
std::vector< int > preferredNeighbors
std::vector< ContactPair > contactPairs
std::vector< float > wanderPhases
std::vector< PreferredFlow > preferredFlows
std::vector< ContactDelta > contactDeltas
std::vector< float > vxs
CrowdSpatialIndex spatialIndex
std::vector< std::vector< AvoidanceNeighbor > > predictionNeighbors
Per-step avoidance neighbor scratch space. @cost Linear in checked neighbors and bounded by avoidance...
std::vector< float > speeds
Owning observations of one completed advance; simulation-thread only.
Definition Crowd.h:94