载入中...
搜索中...
未找到
SceneRelations.cpp
浏览该文件的文档.
2
3#include <cmath>
4#include <map>
5#include <set>
7
8namespace eve::scene_editing {
10 const ISceneHierarchyEditTarget& hierarchy) {
11 using namespace editing;
12 using Value = editing::Value;
13 using Status = editing::Status;
14 const auto reject = [](const char* message) {
15 return failed<std::vector<SceneRelation>>(Status::Rejected, RuleId("scene.relations.schema"), message);
16 };
17 const auto* values = value.getIf<Value::Array>();
18 if (!values || values->size() > 1024) return reject("Relations require an array of at most 1024 entries");
19 std::set<std::string> ids;
20 std::map<std::pair<std::string, std::string>, eve::SceneLocalRegion> regions;
21 std::vector<SceneRelation> result;
22 for (const auto& entry : *values) {
23 const auto* object = entry.getIf<Value::Object>();
24 if (!object) return reject("Each relation must be an object");
25 for (const auto& [key, item] : *object) {
26 (void)item;
27 if (key != "id" && key != "type" && key != "subject" && key != "reference" && key != "hard" &&
28 key != "priority" && key != "weight" && key != "tolerance" && key != "normalMinDot" &&
29 key != "minimum" && key != "maximum" && key != "region")
30 return reject("Unknown relation field");
31 }
32 const auto text = [&](const char* key) -> const std::string* {
33 const auto found = object->find(key);
34 return found == object->end() ? nullptr : found->second.getIf<std::string>();
35 };
36 const auto* id = text("id");
37 const auto* type = text("type");
38 const auto* subject = text("subject");
39 const auto* reference = text("reference");
40 if (!id || id->empty() || !ids.insert(*id).second || !type || !subject || subject->empty() ||
41 (*type != "visible_fraction" && (!reference || reference->empty() || *subject == *reference)) ||
42 (*type == "visible_fraction" && object->contains("reference")))
43 return reject("Relations require unique ids and two distinct nonempty object references");
45 relation.id = *id;
46 relation.subject = ObjectId(*subject);
47 if (reference) relation.reference = ObjectId(*reference);
48 if (*type == "supported_on")
50 else if (*type == "nonpenetrating")
52 else if (*type == "visible_fraction")
54 else if (*type == "occludes")
56 else if (*type == "projected_overlap")
58 else
59 return failed<std::vector<SceneRelation>>(
60 Status::Unsupported, RuleId("scene.relations.type"),
61 "Supported types are supported_on, nonpenetrating, visible_fraction, occludes and projected_overlap");
62 for (const auto& participant : {relation.subject, relation.reference}) {
63 if (participant.empty()) continue;
64 auto resolved = hierarchy.sceneObject(participant);
65 if (!resolved.ok()) return Result<std::vector<SceneRelation>>::failure(resolved.status());
66 }
67 if (const auto found = object->find("hard"); found != object->end()) {
68 const auto* hard = found->second.getIf<bool>();
69 if (!hard) return reject("hard must be boolean");
70 relation.hard = *hard;
71 }
72 if (const auto found = object->find("priority"); found != object->end()) {
73 const auto* priority = found->second.getIf<std::int64_t>();
74 if (!priority || *priority < 0 || *priority > 1000) return reject("priority must be integer in [0,1000]");
75 relation.priority = static_cast<int>(*priority);
76 }
77 const auto numeric = [&](const char* key, double& destination) {
78 const auto found = object->find(key);
79 if (found == object->end()) return true;
80 if (const auto* real = found->second.getIf<double>())
81 destination = *real;
82 else if (const auto* integer = found->second.getIf<std::int64_t>())
83 destination = static_cast<double>(*integer);
84 else
85 return false;
86 return std::isfinite(destination);
87 };
88 if (!numeric("weight", relation.weight) || relation.weight <= 0 || relation.weight > 1e6 ||
89 !numeric("tolerance", relation.tolerance) || relation.tolerance < 0 || relation.tolerance > 1 ||
90 !numeric("normalMinDot", relation.normalMinDot) || relation.normalMinDot < 0 || relation.normalMinDot > 1)
91 return reject("Invalid relation weight, tolerance or normalMinDot");
92 const bool visual = relation.kind == SceneRelationKind::VisibleFraction ||
95 if (visual) {
96 if (!object->contains("minimum") || !numeric("minimum", relation.minimum) ||
97 !numeric("maximum", relation.maximum) || relation.minimum < 0 || relation.maximum > 1 ||
98 relation.minimum > relation.maximum || object->contains("normalMinDot") ||
99 object->contains("tolerance"))
100 return reject("Visual relations require minimum/maximum in [0,1]; contact fields are forbidden");
101 } else if (object->contains("minimum") || object->contains("maximum"))
102 return reject("Fraction fields only apply to visual relations");
103 if (relation.kind != SceneRelationKind::SupportedOn && object->contains("normalMinDot"))
104 return reject("normalMinDot only applies to supported_on");
105 if (object->contains("region")) {
106 if (!visual) return reject("Regions only apply to visual relations");
108 if (!region.ok()) return Result<std::vector<SceneRelation>>::failure(region.status());
109 const auto& measured =
110 relation.kind == SceneRelationKind::VisibleFraction ? relation.subject : relation.reference;
111 auto [it, inserted] = regions.emplace(std::make_pair(measured.value(), region.value().id), region.value());
112 if (!inserted && it->second != region.value())
113 return reject("Conflicting definitions for one object region id");
114 relation.region = std::move(region).takeValue();
115 }
116 result.push_back(std::move(relation));
117 }
118 return applied<std::vector<SceneRelation>>(std::move(result));
119}
120} // namespace eve::scene_editing
int subject
Definition AnimSmr.cpp:163
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
int priority
SQObjectPtr key
Definition ECS.cpp:155
ScriptFieldRegion region
std::string message
std::string relation
std::string id
Poco::JSON::Object::Ptr result
std::set< std::string > values
Object::Ptr value
RoadNetwork resolved
bool found
std::string text
Json object
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Deterministic owning value tree shared by authoring hosts.
std::vector< Value > Array
std::map< std::string, Value > Object
const T * getIf() const
Return an immediately borrowed typed value.
Stable hierarchy capability implemented by document and runtime targets.
Definition SceneTarget.h:35
virtual Result< SceneObjectSnapshot > sceneObject(const ObjectId &id) const =0
Return an immutable object snapshot.
std::unordered_map< const void *, size_t > ids
Definition Snapshot.cpp:109
eve::Value toPresentationValue(const Value &value)
To presentation value.
StrongId< ObjectIdTag > ObjectId
Definition EditingIds.h:65
StrongId< RuleIdTag > RuleId
Definition EditingIds.h:60
eve::StatusCode Status
editing::Result< std::vector< SceneRelation > > parseSceneRelations(const editing::Value &value, const ISceneHierarchyEditTarget &hierarchy)
Strictly decode physical and visual relations and validate all referenced objects.
StatusCode
Stable outcome category for an operation.
Definition Status.h:28
Immutable object-local volume selecting actual rendered surface pixels. @ownership Authoring relation...
static Result< SceneLocalRegion > fromValue(const Value &value)
Decode a finite, nonempty local box and immutable geometry revision.
Owning relation between stable scene objects, measured against admitted contact proxies or native cam...