载入中...
搜索中...
未找到
EditorSimulationPreview.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5#include <set>
6
7namespace eve::editor {
8
10 if (!std::isfinite(seconds) || seconds <= 0.0 || seconds > 1.0)
11 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.simulation.invalid-fixed-delta"),
12 "Simulation preview delta must be finite and within (0, 1] seconds");
13 fixedDelta_ = seconds;
14 return eve::editing::applied<void>();
15}
16
17Result<SimulationPreviewFrame> SimulationPreviewController::advance(IEditorSimulationBackend& backend,
18 std::uint64_t tick) const {
19 auto stepped = backend.step(tick, fixedDelta_);
20 if (!stepped.ok()) return Result<SimulationPreviewFrame>::failure(stepped.status());
21 auto captured = backend.capture();
22 if (!captured.ok()) return Result<SimulationPreviewFrame>::failure(captured.status());
23 std::set<std::string> ids;
24 for (const auto& object : captured.value()) {
25 if (object.object.empty() || !ids.insert(object.object).second)
26 return eve::editing::failed<SimulationPreviewFrame>(
27 EditorStatus::Conflict, RuleId("editor.simulation.unstable-object-id"),
28 "Simulation preview samples require unique stable object ids");
29 const double values[]{object.positionX, object.positionY, object.positionZ, object.rotationX,
30 object.rotationY, object.rotationZ, object.rotationW};
31 if (std::any_of(std::begin(values), std::end(values), [](double value) { return !std::isfinite(value); }))
32 return eve::editing::failed<SimulationPreviewFrame>(EditorStatus::Failed,
33 RuleId("editor.simulation.nonfinite-sample"),
34 "Simulation preview produced a non-finite transform");
35 }
36 std::sort(captured.value().begin(), captured.value().end(),
37 [](const auto& a, const auto& b) { return a.object < b.object; });
38 return eve::editing::applied<SimulationPreviewFrame>(
39 {tick, static_cast<double>(tick) * fixedDelta_, std::move(captured.value())});
40}
41
43 if (!previewBackend_) previewBackend_ = source.cloneForPreview();
44 if (!previewBackend_)
45 return eve::editing::failed<SimulationPreviewFrame>(EditorStatus::Unsupported,
46 RuleId("editor.simulation.clone-unsupported"),
47 "Simulation backend cannot create an isolated preview");
48 auto result = advance(*previewBackend_, tick_ + 1);
49 if (result.ok()) ++tick_;
50 return result;
51}
52
54 tick_ = 0;
55 previewBackend_.reset();
56}
57
59 if (paused_) {
61 SimulationPreviewFrame{tick_, static_cast<double>(tick_) * fixedDelta_, {}},
62 eve::Status::success(EditorStatus::NoOp));
63 }
64 return singleStep(source);
65}
66
68 std::uint64_t sourceRevision, int steps, int maxSteps) const {
70 result.sourceRevision = sourceRevision;
71 result.fixedDelta = fixedDelta_;
72 if (steps < 0 || maxSteps <= 0 || steps > maxSteps) {
73 result.status = EditorStatus::Rejected;
75 eve::DiagnosticCode::InvalidArgument, RuleId("editor.simulation.bake-budget"),
76 DiagnosticSeverity::Error, "Simulation bake step count exceeds the configured budget"));
77 return result;
78 }
79 auto backend = source.cloneForPreview();
80 if (!backend) {
81 result.status = EditorStatus::Unsupported;
83 eve::DiagnosticCode::Unsupported, RuleId("editor.simulation.clone-unsupported"),
84 DiagnosticSeverity::Error, "Simulation backend cannot create an isolated preview"));
85 return result;
86 }
87 auto initial = backend->capture();
88 if (!initial.ok()) {
89 result.status = initial.code();
90 result.diagnostics = initial.diagnostics();
91 return result;
92 }
93 std::sort(initial.value().begin(), initial.value().end(),
94 [](const auto& a, const auto& b) { return a.object < b.object; });
95 result.frames.push_back({0, 0.0, std::move(initial.value())});
96 for (int index = 1; index <= steps; ++index) {
97 auto frame = advance(*backend, static_cast<std::uint64_t>(index));
98 if (!frame.ok()) {
99 result.status = frame.code();
100 result.diagnostics = frame.diagnostics();
101 return result;
102 }
103 result.frames.push_back(std::move(frame.value()));
104 }
105 result.status = EditorStatus::Applied;
106 return result;
107}
108
109} // namespace eve::editor
double value
std::map< std::string, Var > values
glm::uvec4 ids
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
int steps
SimulationTick tick
uint32_t index
const UnitySourceAsset & source
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.h:81
Cloneable backend boundary for physics/crowd/fluid editor simulation previews.
virtual Result< void > step(std::uint64_t tick, double fixedDelta)=0
Advance exactly one fixed deterministic step.
virtual std::unique_ptr< IEditorSimulationBackend > cloneForPreview() const =0
Clone complete simulation state so preview never mutates the live world.
virtual Result< std::vector< SimulationObjectSample > > capture() const =0
Capture stable object transforms after the current step.
Result< SimulationPreviewFrame > singleStep(const IEditorSimulationBackend &source)
Advance one step even while paused, without mutating the live backend.
Result< void > setFixedDelta(double seconds)
Set the fixed preview delta in seconds.
void rewind()
Reset preview clock and discard the isolated preview clone.
SimulationBakeResult bake(const IEditorSimulationBackend &source, std::uint64_t sourceRevision, int steps, int maxSteps=10000) const
Bake a bounded non-destructive trajectory from a fresh source clone.
Result< SimulationPreviewFrame > update(const IEditorSimulationBackend &source)
Advance only when unpaused. Paused calls return NoOp.
Diagnostic ruleDiagnostic(eve::DiagnosticCode code, RuleId rule, DiagnosticSeverity severity, std::string message)
Build a common diagnostic carrying an open editing rule identity.
eve::editing::RuleId RuleId
Definition EditorIds.h:32
Non-destructive baked preview trajectory and diagnostics.
std::vector< SimulationPreviewFrame > frames
std::vector< EditorDiagnostic > diagnostics
One deterministic editor-preview simulation frame.