载入中...
搜索中...
未找到
PortableWorldSnapshot.cpp
浏览该文件的文档.
76 static std::unique_ptr<World3D> makeDetachedWorld3D(float gx, float gy, float gz, eve::PersistentId instanceId,
80 static eve::Result<void> finishPrepared(World& world, int nextId, const SimulationObservation& observation) {
87 static eve::Result<void> finishPrepared(World3D& world, int nextBodyId, int nextShapeId, int nextJointId,
113 eve::DiagnosticCode::InvariantViolation, "physics snapshot schema constant is invalid", "schema"));
117eve::Result<void> checkEnvelope(const eve::SnapshotEnvelope& snapshot, std::string_view type, std::string_view schema) {
145 if (duration.value() < 0 || revision.value() != stepCount.value() || tick.value() != snapshot.tick.value() ||
158 auto valid = detail::validateSimulationObservation(result.value, "physics.snapshot.observation");
182bool validBodyType(const std::string& type) { return type == "static" || type == "kinematic" || type == "dynamic"; }
186 if (!object || !hasExactFields(*object, {"active", "angle", "awake", "bullet", "fixedRotation", "fixtures", "id",
195 eve::DiagnosticCode::ParseError, "2D physics snapshot body id is invalid", "payload.bodies.id"));
200 eve::DiagnosticCode::InvalidArgument, "2D physics snapshot body type is invalid", "payload.bodies.type"));
234 return eve::Result<Body2DState>::failure(eve::Diagnostic::error(eve::DiagnosticCode::ParseError,
243 if (!object || !hasExactFields(*object, {"active", "awake", "bullet", "fixedRotation", "id", "type", "x", "y", "z",
252 eve::DiagnosticCode::ParseError, "3D physics snapshot body id is invalid", "payload.bodies.id"));
257 eve::DiagnosticCode::InvalidArgument, "3D physics snapshot body type is invalid", "payload.bodies.type"));
283 return eve::Result<Body3DState>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument,
398 if (shape && shape->isValid()) values.push_back(PortableWorldSnapshotAccess::shapeSource(*shape));
425eve::Value::Object observationFields(const SimulationObservation& observation, eve::SimulationTick tick) {
430 object.emplace("simulatedDurationNs", eve::Value(std::to_string(observation.simulatedDuration.nanoseconds())));
483 eve::DiagnosticCode::Conflict, "physics snapshot contains a duplicate body id", "payload.bodies.id"));
493 eve::DiagnosticCode::ParseError, "physics snapshot bodies must be an array", "payload.bodies"));
509 eve::DiagnosticCode::ParseError, "physics snapshot bodies must be an array", "payload.bodies"));
520eve::Result<std::unique_ptr<World>> prepareWorld2D(const std::vector<Body2DState>& states, float gravityX,
592eve::Result<std::vector<float>> readFloatArray(const eve::Value::Object& object, std::string_view name) {
609eve::Result<std::vector<std::int32_t>> readIntArray(const eve::Value::Object& object, std::string_view name) {
619 eve::DiagnosticCode::ParseError, "snapshot integer array item must be a string", std::string(name)));
623 if (ec != std::errc{} || end != text.data() + text.size() || parsed < INT32_MIN || parsed > INT32_MAX)
638 PortableWorldSnapshotAccess::makeDetachedWorld3D(gravityX, gravityY, gravityZ, instanceId, backend);
671 eve::DiagnosticCode::Conflict, "3D shape references invalid identity or source", "payload.shapes"));
697 shape = body->newTriangleMeshShape(vertices.value(), indices.value(), weld.value(), tolerance.value(),
712 body->newHeightFieldShape(static_cast<int>(cx.value()), static_cast<int>(cz.value()), sx.value(),
717 eve::DiagnosticCode::Unsupported, "3D shape kind cannot be reconstructed", "payload.shapes.kind"));
733 if (!bodyAId || !bodyBId || !id || id.value() <= 0 || id.value() > std::numeric_limits<int>::max() ||
738 eve::DiagnosticCode::Conflict, "3D joint references invalid bodies or frames", "payload.joints"));
743 if (!anchorAResult) return eve::Result<std::unique_ptr<World3D>>::failure(anchorAResult.status());
745 if (!anchorBResult) return eve::Result<std::unique_ptr<World3D>>::failure(anchorBResult.status());
758 joint = prepared->newDistanceJoint(aBody, bBody, anchorA.x, anchorA.y, anchorA.z, anchorB.x, anchorB.y,
765 prepared->newRevoluteJoint(aBody, bBody, anchorA.x, anchorA.y, anchorA.z, axis.x, axis.y, axis.z);
771 prepared->newPrismaticJoint(aBody, bBody, anchorA.x, anchorA.y, anchorA.z, axis.x, axis.y, axis.z);
777 prepared->newSphericalJoint(aBody, bBody, anchorA.x, anchorA.y, anchorA.z, axis.x, axis.y, axis.z);
780 if (!suspensionResult) return eve::Result<std::unique_ptr<World3D>>::failure(suspensionResult.status());
801 eve::DiagnosticCode::Unsupported, "3D joint kind cannot be reconstructed", "payload.joints.kind"));
805 auto restored = PortableWorldSnapshotAccess::finishPrepared(*prepared, maxBodyId + 1, maxShapeId + 1,
818eve::Result<eve::SnapshotEnvelope> World3D::snapshotPortable(const eve::SnapshotHashProvider& hashProvider) const {
821 eve::DiagnosticCode::PreconditionViolation, "Cannot snapshot a destroyed or uninitialized 3D physics world",
827 auto payload = make3DPayload(*this, bodies, std::vector<Shape3D*>(shapes_.begin(), shapes_.end()),
831 std::string(kWorld3DType), std::move(schema).takeValue(), eve::SchemaVersion(kSnapshotVersion), instanceId_,
832 eve::Revision(observation.stepCount), simulationTick(), std::move(payload).takeValue(), hashProvider);
839 eve::DiagnosticCode::PreconditionViolation, "Cannot restore a destroyed or uninitialized 3D physics world",
856 migrated->emplace("shapes", shapesTopologyValue(std::vector<Shape3D*>(shapes_.begin(), shapes_.end())));
867 if (!object || !hasExactFields(*object, {"bodies", "gravityX", "gravityY", "gravityZ", "joints", "shapes", "tick",
870 eve::DiagnosticCode::ParseError, "3D physics snapshot payload has unknown or missing fields", "payload"));
883 if (!snapshotShapes || !snapshotShapes->isArray() || !snapshotJoints || !snapshotJoints->isArray())
885 eve::DiagnosticCode::ParseError, "3D physics snapshot topology fields must be arrays", "payload.shapes"));
890 prepareRuntimeHandleRefresh(bodies.value().size(), snapshotShapes->getIf<Value::Array>()->size(),
std::vector< std::unique_ptr< ScriptClimbingEventSink > > live
Definition ClimbingServices.cpp:187
#define EV_READ_BODY3D_BOOL(name)
#define EV_READ_BODY3D_FLOAT(name)
Backend-neutral, observable fixed-step contract for physics domains.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.cpp:21
static constexpr Duration fromNanoseconds(std::int64_t nanoseconds) noexcept
Construct an exact duration from nanoseconds.
Definition Time.h:55
static std::optional< LogicalId > parse(std::string_view text)
Parses a scoped logical name.
Definition Identity.cpp:37
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
const T * getIf() const noexcept
Return a typed pointer, or nullptr when the kind differs.
Definition Value.h:209
constexpr bool isNil() const noexcept
Returns whether this value is the all-zero nil ID.
Definition Identity.h:175
constexpr std::uint64_t value() const noexcept
Returns the underlying value at an explicit protocol boundary.
Definition StrongUint64.h:26
Backend-neutral 3D rigid body in meter-space coordinates (+Y up by convention). Owned by a World3D; c...
Definition Body3D.h:31
2D rigid body (Box2D) in pixel-space coordinates. Owned by a World; create shapes with newRectangleFi...
Definition Body.h:21
Script-facing joint owned by a World3D. Legacy scalar/void methods adapt the checked backend contract...
Definition Joint3D.h:27
3D shape (box/sphere/capsule) attached to a Body3D with material settings. Created via Body3D::new*Sh...
Definition Shape3D.h:34
Box3D rigid-body world. Script coordinates are meters (Box3D native), unlike 2D World which uses pixe...
Definition World3D.h:47
SimulationObservation simulationObservation() const noexcept
Snapshot of completed backend steps and logical simulation time.
Definition World3DSimulation.cpp:65
eve::SimulationTick simulationTick() const noexcept
Current deterministic tick; save data should persist this value.
Definition World3D.h:134
void clearContactEvents()
Clears all contact and trigger buffers before the next step.
Definition World3DContacts.cpp:14
Box2D world wrapper (2D physics) with pixel-space coordinates. Handles stepping, gravity,...
Definition World.h:44
constexpr std::string_view kWorld2DSchema
Definition WorldSnapshotCodec.h:24
Result< float > readFloat(const Value::Object &object, std::string_view name)
Reads a required finite number representable by float.
Definition WorldSnapshotParsing.cpp:66
eve::Result< std::unique_ptr< World3D > > prepareWorld3D(const std::vector< Body3DState > &states, const eve::Value::Array &shapes, const eve::Value::Array &joints, float gravityX, float gravityY, float gravityZ, const SimulationObservation &observation, eve::PersistentId instanceId, bool legacyParameters, bool allowSleep)
Definition WorldSnapshotCodec.cpp:601
constexpr std::uint64_t kSnapshotVersion
Definition WorldSnapshotCodec.h:27
const Value * field(const Value::Object &object, std::string_view name)
Borrow an optional field only during the current synchronous codec call. @ownership Object owns the r...
Definition WorldSnapshotParsing.cpp:17
eve::Value shapesTopologyValue(const std::vector< Shape3D * > &shapes)
Definition WorldSnapshotCodec.cpp:350
eve::Value make2DPayload(const World &world, const std::vector< Body * > &bodies)
Definition WorldSnapshotCodec.cpp:397
constexpr std::string_view kWorld3DSchema
Definition WorldSnapshotCodec.h:26
eve::Value jointsTopologyValue(const std::vector< Joint3D * > &joints)
Definition WorldSnapshotCodec.cpp:360
eve::Value::Object observationFields(const SimulationObservation &observation, eve::SimulationTick tick)
Definition WorldSnapshotCodec.cpp:387
bool hasExactFields(const Value::Object &object, std::initializer_list< std::string_view > expected)
Internal exact-field schema predicate.
Definition WorldSnapshotParsing.cpp:9
eve::Value fixtureTopologyValue(const Body &body)
Definition WorldSnapshotCodec.cpp:298
constexpr std::string_view kWorld3DType
Definition WorldSnapshotCodec.h:25
Result< std::uint64_t > readUint64(const Value::Object &object, std::string_view name)
Reads a required unsigned decimal integer string.
Definition WorldSnapshotParsing.cpp:30
eve::Value make3DPayload(const World3D &world, const std::vector< Body3D * > &bodies, const std::vector< Shape3D * > &shapes, const std::vector< Joint3D * > &joints)
Definition WorldSnapshotCodec.cpp:419
constexpr std::string_view kWorld2DType
Definition WorldSnapshotCodec.h:23
Result< std::int64_t > readInt64(const Value::Object &object, std::string_view name)
Reads a required signed decimal integer string.
Definition WorldSnapshotParsing.cpp:42
Result< bool > readBool(const Value::Object &object, std::string_view name)
Reads a required boolean.
Definition WorldSnapshotParsing.cpp:75
eve::Result< std::vector< float > > readFloatArray(const eve::Value::Object &object, std::string_view name)
Read a finite float array, rejecting missing or malformed elements.
Definition WorldSnapshotCodec.cpp:558
eve::Result< std::vector< std::int32_t > > readIntArray(const eve::Value::Object &object, std::string_view name)
Definition WorldSnapshotCodec.cpp:579
eve::Result< Body2DState > parseBody2D(const eve::Value &value)
Definition WorldSnapshotCodec.cpp:89
eve::Result< void > matchBodyIds(const std::vector< BodyState > &states, std::set< int > &ids)
Definition WorldSnapshotCodec.h:55
eve::Result< eve::LogicalId > snapshotSchema(std::string_view text)
Definition WorldSnapshotCodec.cpp:33
eve::Result< std::unique_ptr< World > > prepareWorld2D(const std::vector< Body2DState > &states, float gravityX, float gravityY, float meter, const SimulationObservation &observation, eve::PersistentId instanceId)
Definition WorldSnapshotCodec.cpp:472
eve::Result< std::vector< Body2DState > > parseBodies2D(const eve::Value::Object &object)
Definition WorldSnapshotCodec.cpp:440
eve::Result< void > checkEnvelope(const eve::SnapshotEnvelope &snapshot, std::string_view type, std::string_view schema)
Definition WorldSnapshotCodec.cpp:41
eve::Result< std::vector< Body3DState > > parseBodies3D(const eve::Value::Object &object)
Definition WorldSnapshotCodec.cpp:456
eve::Result< Body3DState > parseBody3D(const eve::Value &value)
Definition WorldSnapshotCodec.cpp:166
eve::Value bodyValue(const Body &body)
Definition WorldSnapshotCodec.cpp:246
bool validBodyType(const std::string &type)
Definition WorldSnapshotCodec.cpp:87
Result< std::string > readString(const Value::Object &object, std::string_view name)
Reads a required owning string.
Definition WorldSnapshotParsing.cpp:22
eve::Result< ObservationState > parseObservation(const eve::Value::Object &object, const eve::SnapshotEnvelope &snapshot)
Definition WorldSnapshotCodec.cpp:56
RigidBackend3D
Solver identity fixed for the lifetime of one world; independent of CPU/GPU execution.
Definition WorldBackend3D.h:11
@ InvalidArgument
@ UnknownVersion
@ PreconditionViolation
@ Unsupported
@ InvariantViolation
@ Failed
@ ParseError
@ Conflict
@ Applied
detail::StrongUint64< detail::SimulationTickTag > SimulationTick
Deterministic simulation time step; it is not wall-clock time.
Definition Time.h:31
Result< SnapshotEnvelope > makeSnapshotEnvelope(std::string type, LogicalId schema, SchemaVersion schemaVersion, PersistentId instanceId, Revision revision, SimulationTick tick, Value payload, const SnapshotHashProvider &hashProvider)
Construct and seal a snapshot envelope.
Definition Snapshot.cpp:103
Result< void > verifySnapshotEnvelope(const SnapshotEnvelope &snapshot, const SnapshotHashProvider &hashProvider)
Verify an envelope's content hash without modifying it.
Definition Snapshot.cpp:63
std::function< Result< ContentId >(std::string_view canonicalInput)> SnapshotHashProvider
Injected content-digest implementation used by snapshots.
Definition Snapshot.h:36
Stable outer format shared by persistence and cross-process snapshots.
Definition Snapshot.h:46
static void adopt(World3D &live, World3D &prepared)
Definition PortableWorldSnapshot.cpp:99
static void adopt(World &live, World &prepared)
Definition PortableWorldSnapshot.cpp:98
static std::unique_ptr< World3D > makeDetachedWorld3D(float gx, float gy, float gz, eve::PersistentId instanceId, RigidBackend3D backend)
Definition PortableWorldSnapshot.cpp:76
static eve::Result< void > finishPrepared(World &world, int nextId, const SimulationObservation &observation)
Definition PortableWorldSnapshot.cpp:80
static void setBodyId(Body3D &body, int id)
Definition PortableWorldSnapshot.cpp:73
static eve::Result< void > finishPrepared(World3D &world, int nextBodyId, int nextShapeId, int nextJointId, const SimulationObservation &observation)
Definition PortableWorldSnapshot.cpp:87
static void setJointId(Joint3D &joint, int id)
Definition PortableWorldSnapshot.cpp:75
static void setBodyId(Body &body, int id)
Definition PortableWorldSnapshot.cpp:72
static void setShapeId(Shape3D &shape, int id)
Definition PortableWorldSnapshot.cpp:74
static eve::Value shapeSource(const Shape3D &shape)
Definition PortableWorldSnapshot.cpp:32
Observable backend progress shared by CPU and accelerator providers.
Definition SimulationBackend.h:136
eve::SimulationTick lastTick
Tick of the most recently completed step.
Definition SimulationBackend.h:140