载入中...
搜索中...
未找到
ArchSpaceDocument.cpp
浏览该文件的文档.
2
4
5#include <algorithm>
6#include <cmath>
7#include <unordered_map>
8#include <utility>
9
10namespace eve::archspace {
11namespace {
12
13constexpr double kEps = 1e-8;
14
16
17bool isFinite(double v) { return std::isfinite(v); }
18
19bool isFinite(const Vec2& v) { return isFinite(v.x) && isFinite(v.z); }
20
21bool isFinite(const Vec3& v) { return isFinite(v.x) && isFinite(v.y) && isFinite(v.z); }
22
23double distance(const Vec2& a, const Vec2& b) {
24 const double dx = b.x - a.x;
25 const double dz = b.z - a.z;
26 return std::sqrt(dx * dx + dz * dz);
27}
28
29double polygonArea(const std::vector<Vec2>& poly) {
30 if (poly.size() < 3) return 0.0;
31 double acc = 0.0;
32 for (std::size_t i = 0; i < poly.size(); ++i) {
33 const Vec2& a = poly[i];
34 const Vec2& b = poly[(i + 1) % poly.size()];
35 acc += a.x * b.z - b.x * a.z;
36 }
37 return 0.5 * acc;
38}
39
40bool polygonValid(const std::vector<Vec2>& poly) {
41 if (poly.size() < 3 || poly.size() > 256) return false;
42 for (const auto& p : poly)
43 if (!isFinite(p)) return false;
44 return std::abs(polygonArea(poly)) > kEps;
45}
46
47void appendBox(MeshBake& bake, const Vec3& a, const Vec3& b, double thickness, double height, const std::string& id) {
48 Vec2 dir{b.x - a.x, b.z - a.z};
49 const double len = std::sqrt(dir.x * dir.x + dir.z * dir.z);
50 if (len <= kEps || thickness <= kEps || height <= kEps) return;
51 dir.x /= len;
52 dir.z /= len;
53 const Vec2 n{-dir.z * thickness * 0.5, dir.x * thickness * 0.5};
54 const std::uint32_t base = static_cast<std::uint32_t>(bake.positions.size());
55 const Vec3 corners[8] = {
56 {a.x + n.x, a.y, a.z + n.z}, {a.x - n.x, a.y, a.z - n.z},
57 {b.x - n.x, a.y, b.z - n.z}, {b.x + n.x, a.y, b.z + n.z},
58 {a.x + n.x, a.y + height, a.z + n.z}, {a.x - n.x, a.y + height, a.z - n.z},
59 {b.x - n.x, a.y + height, b.z - n.z}, {b.x + n.x, a.y + height, b.z + n.z},
60 };
61 for (const auto& c : corners) bake.positions.push_back(c);
62 static constexpr std::uint32_t faces[12][3] = {{0, 1, 2}, {0, 2, 3}, {4, 6, 5}, {4, 7, 6}, {0, 3, 7}, {0, 7, 4},
63 {1, 5, 6}, {1, 6, 2}, {0, 4, 5}, {0, 5, 1}, {3, 2, 6}, {3, 6, 7}};
64 for (const auto& f : faces) {
65 bake.indices.push_back(base + f[0]);
66 bake.indices.push_back(base + f[1]);
67 bake.indices.push_back(base + f[2]);
68 }
69 bake.primitiveIds.push_back(id);
70}
71
72void appendSlab(MeshBake& bake, const std::vector<Vec2>& poly, double y, double thickness, const std::string& id) {
73 if (poly.size() < 3 || thickness <= kEps) return;
74 const std::uint32_t base = static_cast<std::uint32_t>(bake.positions.size());
75 for (const auto& p : poly) bake.positions.push_back(Vec3{p.x, y, p.z});
76 for (const auto& p : poly) bake.positions.push_back(Vec3{p.x, y + thickness, p.z});
77 const std::uint32_t n = static_cast<std::uint32_t>(poly.size());
78 for (std::uint32_t i = 1; i + 1 < n; ++i) {
79 bake.indices.push_back(base);
80 bake.indices.push_back(base + i);
81 bake.indices.push_back(base + i + 1);
82 bake.indices.push_back(base + n);
83 bake.indices.push_back(base + n + i + 1);
84 bake.indices.push_back(base + n + i);
85 }
86 bake.primitiveIds.push_back(id);
87}
88
89} // namespace
90
91const Node* Document::find(const std::string& id) const {
92 const auto it = nodes_.find(id);
93 return it == nodes_.end() ? nullptr : &it->second;
94}
95
96Node* Document::findMutable(const std::string& id) {
97 const auto it = nodes_.find(id);
98 return it == nodes_.end() ? nullptr : &it->second;
99}
100
101bool Document::canParent(NodeKind child, const std::string& parentId) const {
102 if (child == NodeKind::Site) return parentId.empty();
103 const Node* parent = find(parentId);
104 if (!parent) return false;
105 switch (child) {
106 case NodeKind::Building: return parent->kind == NodeKind::Site;
107 case NodeKind::Level: return parent->kind == NodeKind::Building;
108 case NodeKind::Wall:
109 case NodeKind::Slab:
110 case NodeKind::Zone:
111 case NodeKind::Item:
112 case NodeKind::Portal: return parent->kind == NodeKind::Level;
113 case NodeKind::Opening: return parent->kind == NodeKind::Wall;
114 case NodeKind::Site: return false;
115 }
116 return false;
117}
118
119bool Document::validateNode(const Node& node) const {
120 if (node.id.empty() || node.id.size() > 128 || node.name.size() > 256) return false;
121 if (node.kind == NodeKind::Site) {
122 if (!node.parentId.empty()) return false;
123 } else if (!canParent(node.kind, node.parentId)) {
124 return false;
125 }
126 switch (node.kind) {
127 case NodeKind::Site:
128 case NodeKind::Building: return true;
129 case NodeKind::Level: return isFinite(node.elevation) && isFinite(node.height) && node.height > kEps;
130 case NodeKind::Wall:
131 return isFinite(node.start) && isFinite(node.end) && isFinite(node.thickness) && isFinite(node.height) &&
132 node.thickness > kEps && node.height > kEps && distance(node.start, node.end) > kEps;
133 case NodeKind::Slab:
134 return polygonValid(node.polygon) && isFinite(node.slabThickness) && node.slabThickness > kEps;
135 case NodeKind::Zone: return polygonValid(node.polygon);
136 case NodeKind::Item:
137 return isFinite(node.position) && isFinite(node.yawDegrees) && node.catalogId.size() <= 256;
139 return isFinite(node.t) && node.t >= 0.0 && node.t <= 1.0 && isFinite(node.width) && node.width > kEps &&
140 isFinite(node.openingHeight) && node.openingHeight > kEps && isFinite(node.sill) && node.sill >= 0.0;
141 case NodeKind::Portal:
142 return !node.regionAId.empty() && node.regionAId.size() <= 128 && node.regionBId.size() <= 128 &&
143 isFinite(node.portalPosition) && node.passageMask != 0;
144 }
145 return false;
146}
147
148void Document::unlinkFromParent(const std::string& id) {
149 Node* node = findMutable(id);
150 if (!node || node->parentId.empty()) return;
151 if (Node* parent = findMutable(node->parentId)) {
152 parent->children.erase(std::remove(parent->children.begin(), parent->children.end(), id),
153 parent->children.end());
154 }
155}
156
157void Document::collectDescendants(const std::string& id, std::vector<std::string>& out) const {
158 const Node* node = find(id);
159 if (!node) return;
160 for (const auto& child : node->children) {
161 out.push_back(child);
162 collectDescendants(child, out);
163 }
164}
165
167 if (nodes_.find(node.id) != nodes_.end())
169 eve::DiagnosticCode::Conflict, "ArchSpace node id already exists", "id", {}, "archspace.document"));
170 if (!validateNode(node))
172 "ArchSpace node failed validation", "node", {},
173 "archspace.document"));
174 if (node.kind == NodeKind::Site) {
175 if (!rootId_.empty())
177 "ArchSpace document already has a site root",
178 "root", {}, "archspace.document"));
179 rootId_ = node.id;
180 } else {
181 Node* parent = findMutable(node.parentId);
182 if (!parent)
184 "ArchSpace parent node is missing", "parentId", {},
185 "archspace.document"));
186 parent->children.push_back(node.id);
187 }
188 nodes_.emplace(node.id, std::move(node));
189 return ok();
190}
191
193 Node* existing = findMutable(node.id);
194 if (!existing)
196 eve::DiagnosticCode::NotFound, "ArchSpace node is missing", "id", {}, "archspace.document"));
197 node.parentId = existing->parentId;
198 node.kind = existing->kind;
199 node.children = existing->children;
200 if (!validateNode(node))
202 "ArchSpace node failed validation", "node", {},
203 "archspace.document"));
204 *existing = std::move(node);
205 return ok();
206}
207
209 if (!find(id))
211 eve::DiagnosticCode::NotFound, "ArchSpace node is missing", "id", {}, "archspace.document"));
212 std::vector<std::string> order;
213 collectDescendants(id, order);
214 std::reverse(order.begin(), order.end());
215 order.push_back(id);
216 for (const auto& victim : order) {
217 unlinkFromParent(victim);
218 nodes_.erase(victim);
219 }
220 if (rootId_ == id) rootId_.clear();
221 return ok();
222}
223
225 nodes_.clear();
226 rootId_.clear();
227 topology_ = TopologyKind::Architectural;
228}
229
234
235eve::Result<void> Document::bootstrap(const std::string& siteId, const std::string& buildingId,
236 const std::string& levelId, double levelHeight) {
237 if (!rootId_.empty() || siteId.empty() || buildingId.empty() || levelId.empty())
240 "ArchSpace bootstrap requires an empty document and non-empty ids", {}, {}, "archspace.document"));
241 if (siteId == buildingId || siteId == levelId || buildingId == levelId)
243 "ArchSpace bootstrap ids must be unique", {}, {},
244 "archspace.document"));
245 Node site;
246 site.id = siteId;
247 site.kind = NodeKind::Site;
248 site.name = "Site";
249 Node building;
250 building.id = buildingId;
251 building.kind = NodeKind::Building;
252 building.parentId = siteId;
253 building.name = "Building";
254 Node level;
255 level.id = levelId;
256 level.kind = NodeKind::Level;
257 level.parentId = buildingId;
258 level.name = "Level 0";
259 level.height = levelHeight;
260 Document candidate;
261 auto inserted = candidate.insert(std::move(site));
262 if (!inserted.ok()) return inserted;
263 inserted = candidate.insert(std::move(building));
264 if (!inserted.ok()) return inserted;
265 inserted = candidate.insert(std::move(level));
266 if (!inserted.ok()) return inserted;
267 *this = std::move(candidate);
268 return ok();
269}
270
271eve::Result<void> Document::createRoom(const std::string& levelId, const std::string& roomId, std::string roomName,
272 std::vector<Vec2> polygon, double wallHeight, double wallThickness,
273 double slabThickness) {
274 const Node* level = find(levelId);
275 if (!level || level->kind != NodeKind::Level)
277 "ArchSpace room requires an existing level", "levelId",
278 {}, "archspace.document"));
279 if (roomId.empty() || !polygonValid(polygon) || wallHeight <= kEps || wallThickness <= kEps ||
280 slabThickness <= kEps)
282 "ArchSpace room geometry is invalid", "polygon", {},
283 "archspace.document"));
284 if (roomName.empty()) roomName = roomId;
285
286 Document candidate = *this;
287 Node zone;
288 zone.id = roomId + ".zone";
289 zone.kind = NodeKind::Zone;
290 zone.parentId = levelId;
291 zone.name = roomName;
292 zone.polygon = polygon;
293 Node slab;
294 slab.id = roomId + ".slab";
295 slab.kind = NodeKind::Slab;
296 slab.parentId = levelId;
297 slab.name = roomName + " floor";
298 slab.polygon = polygon;
299 slab.slabThickness = slabThickness;
300 auto inserted = candidate.insert(std::move(zone));
301 if (!inserted.ok()) return inserted;
302 inserted = candidate.insert(std::move(slab));
303 if (!inserted.ok()) return inserted;
304 for (std::size_t i = 0; i < polygon.size(); ++i) {
305 Node wall;
306 wall.id = roomId + ".wall." + std::to_string(i);
307 wall.kind = NodeKind::Wall;
308 wall.parentId = levelId;
309 wall.name = roomName + " wall " + std::to_string(i);
310 wall.start = polygon[i];
311 wall.end = polygon[(i + 1) % polygon.size()];
312 wall.height = wallHeight;
313 wall.thickness = wallThickness;
314 inserted = candidate.insert(std::move(wall));
315 if (!inserted.ok()) return inserted;
316 }
317 *this = std::move(candidate);
318 return ok();
319}
320
321eve::Result<void> Document::createWall(const std::string& levelId, const std::string& wallId, std::string wallName,
322 Vec2 start, Vec2 end, double height, double thickness) {
323 const Node* level = find(levelId);
324 if (!level || level->kind != NodeKind::Level)
326 "ArchSpace wall requires an existing level", "levelId",
327 {}, "archspace.document"));
328 Node wall;
329 wall.id = wallId;
330 wall.kind = NodeKind::Wall;
331 wall.parentId = levelId;
332 wall.name = wallName.empty() ? wallId : std::move(wallName);
333 wall.start = start;
334 wall.end = end;
335 wall.height = height;
336 wall.thickness = thickness;
337 return insert(std::move(wall));
338}
339
340eve::Result<void> Document::createOpening(const std::string& wallId, const std::string& openingId,
341 std::string openingName, OpeningKind kind, double t, double width,
342 double height, double sill) {
343 const Node* wall = find(wallId);
344 if (!wall || wall->kind != NodeKind::Wall)
346 "ArchSpace opening requires an existing wall",
347 "wallId", {}, "archspace.document"));
348 Node opening;
349 opening.id = openingId;
350 opening.kind = NodeKind::Opening;
351 opening.parentId = wallId;
352 opening.name = openingName.empty() ? openingId : std::move(openingName);
353 opening.openingKind = kind;
354 opening.t = t;
355 opening.width = width;
356 opening.openingHeight = height;
357 opening.sill = sill;
358 return insert(std::move(opening));
359}
360
361eve::Result<void> Document::placeItem(const std::string& levelId, const std::string& itemId, std::string itemName,
362 std::string catalogId, Vec3 position, double yawDegrees) {
363 const Node* level = find(levelId);
364 if (!level || level->kind != NodeKind::Level)
366 "ArchSpace item requires an existing level", "levelId",
367 {}, "archspace.document"));
368 Node item;
369 item.id = itemId;
370 item.kind = NodeKind::Item;
371 item.parentId = levelId;
372 item.name = itemName.empty() ? itemId : std::move(itemName);
373 item.catalogId = std::move(catalogId);
374 item.position = position;
375 item.yawDegrees = yawDegrees;
376 return insert(std::move(item));
377}
378
379eve::Result<void> Document::createRegion(const std::string& levelId, const std::string& regionId,
380 std::string regionName, std::vector<Vec2> polygon) {
381 const Node* level = find(levelId);
382 if (!level || level->kind != NodeKind::Level)
384 "ArchSpace region requires an existing level",
385 "levelId", {}, "archspace.document"));
386 if (regionId.empty() || !polygonValid(polygon))
388 "ArchSpace region geometry is invalid", "polygon", {},
389 "archspace.document"));
390 Node zone;
391 zone.id = regionId;
392 zone.kind = NodeKind::Zone;
393 zone.parentId = levelId;
394 zone.name = regionName.empty() ? regionId : std::move(regionName);
395 zone.polygon = std::move(polygon);
396 return insert(std::move(zone));
397}
398
399eve::Result<void> Document::createPortal(const std::string& levelId, const std::string& portalId,
400 std::string portalName, std::string regionAId, std::string regionBId,
401 PassageMask passageMask, Vec3 position) {
402 const Node* level = find(levelId);
403 if (!level || level->kind != NodeKind::Level)
405 "ArchSpace portal requires an existing level",
406 "levelId", {}, "archspace.document"));
407 const Node* regionA = find(regionAId);
408 if (!regionA || regionA->kind != NodeKind::Zone || regionA->parentId != levelId)
410 "ArchSpace portal regionA must be a zone on the level",
411 "regionAId", {}, "archspace.document"));
412 if (!regionBId.empty()) {
413 const Node* regionB = find(regionBId);
414 if (!regionB || regionB->kind != NodeKind::Zone || regionB->parentId != levelId)
416 eve::DiagnosticCode::NotFound, "ArchSpace portal regionB must be a zone on the level", "regionBId", {},
417 "archspace.document"));
418 }
419 Node portal;
420 portal.id = portalId;
421 portal.kind = NodeKind::Portal;
422 portal.parentId = levelId;
423 portal.name = portalName.empty() ? portalId : std::move(portalName);
424 portal.regionAId = std::move(regionAId);
425 portal.regionBId = std::move(regionBId);
426 portal.passageMask = passageMask;
427 portal.portalPosition = position;
428 return insert(std::move(portal));
429}
430
431std::vector<std::string> Document::diagnostics() const {
432 std::vector<std::string> out;
433 if (rootId_.empty() && !nodes_.empty()) out.push_back("document has nodes but no root site");
434 if (!rootId_.empty() && !find(rootId_)) out.push_back("root id is missing from node map");
435 std::unordered_map<std::string, std::size_t> referenced;
436 for (const auto& [id, node] : nodes_) {
437 if (!validateNode(node)) out.push_back("node failed validation: " + id);
438 for (const auto& child : node.children) {
439 ++referenced[child];
440 const Node* childNode = find(child);
441 if (!childNode)
442 out.push_back("missing child " + child + " under " + id);
443 else if (childNode->parentId != id)
444 out.push_back("child parent mismatch: " + child);
445 }
446 if (node.kind != NodeKind::Site) {
447 const Node* parent = find(node.parentId);
448 if (!parent)
449 out.push_back("missing parent for " + id);
450 else if (!canParent(node.kind, node.parentId))
451 out.push_back("illegal parent kind for " + id);
452 }
453 }
454 for (const auto& [id, node] : nodes_) {
455 if (node.kind == NodeKind::Site) continue;
456 if (referenced.find(id) == referenced.end()) out.push_back("orphaned node: " + id);
457 }
458 return out;
459}
460
462 MeshBake bake;
463
464 for (const auto& [id, node] : nodes_) {
465 if (node.kind == NodeKind::Wall) {
466 const Node* level = find(node.parentId);
467 const double y = level ? level->elevation : 0.0;
468 std::vector<const Node*> openings;
469 openings.reserve(node.children.size());
470 for (const auto& childId : node.children) {
471 const Node* child = find(childId);
472 if (child && child->kind == NodeKind::Opening) openings.push_back(child);
473 }
474 appendWallWithOpenings(bake, node, y, openings);
475 } else if (node.kind == NodeKind::Slab) {
476 const Node* level = find(node.parentId);
477 const double y = level ? level->elevation : 0.0;
478 appendSlab(bake, node.polygon, y, node.slabThickness, id);
479 } else if (node.kind == NodeKind::Item) {
480 appendCatalogItem(bake, id, node.catalogId, node.position, node.yawDegrees);
481 }
482 }
483 return bake;
484}
485
486} // namespace eve::archspace
double z
double y
double x
Duration start
LogicalId itemId
glm::vec4 p[6]
std::uint64_t parentId
glm::vec3 n
Definition Grass.cpp:63
std::vector< float > positions
float v
std::int32_t c
std::uint32_t height
std::uint32_t width
const ColourScope * parent
std::array< float, 3 > position
float thickness
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
std::vector< std::int32_t > order
int level
glm::dvec3 dx
glm::dvec3 dz
Topology topology
float f
float t
const RoadNode * node
Value::Array corners
ScriptStructure out
size_t end
std::string kind
int children
Definition TreeMesh.cpp:432
V3 dir
Definition TreeMesh.cpp:151
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
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
Authoritative ArchSpace document (Pascal-like node graph).
eve::Result< void > createWall(const std::string &levelId, const std::string &wallId, std::string wallName, Vec2 start, Vec2 end, double height, double thickness)
Insert a single wall under an existing level.
eve::Result< void > createRegion(const std::string &levelId, const std::string &regionId, std::string regionName, std::vector< Vec2 > polygon)
Create a Region (Zone) without perimeter walls — dungeon rooms / cave chambers.
eve::Result< void > eraseCascade(const std::string &id)
Delete a node and every descendant. Root deletion clears the document.
Node * findMutable(const std::string &id)
Borrow a mutable node by id. @ownership Non-owning pointer into this document's node map; do not free...
eve::Result< void > placeItem(const std::string &levelId, const std::string &itemId, std::string itemName, std::string catalogId, Vec3 position, double yawDegrees=0.0)
Place a catalog item on an existing level.
eve::Result< void > bootstrap(const std::string &siteId, const std::string &buildingId, const std::string &levelId, double levelHeight=3.0)
Create site → building → level skeleton when the document is empty.
const Node * find(const std::string &id) const
Borrow a node by id. @ownership Non-owning pointer into this document's node map; do not free....
void clear()
Remove every node.
std::vector< std::string > diagnostics() const
Collect structural and geometric diagnostics as human-readable lines.
eve::Result< void > createPortal(const std::string &levelId, const std::string &portalId, std::string portalName, std::string regionAId, std::string regionBId, PassageMask passageMask, Vec3 position)
Create a level-hosted portal connecting two regions.
eve::Result< void > replace(Node node)
Replace authored fields while preserving id, kind, parent and children.
eve::Result< void > setTopology(TopologyKind topology)
Set topology dialect.
eve::Result< void > insert(Node node)
Insert a validated node and link it into the parent child list.
MeshBake bakeMesh() const
Build wall boxes (with opening cutouts), slabs and item markers for viewport upload.
TopologyKind topology() const noexcept
Topology dialect for authoring helpers and SpaceQuery.
eve::Result< void > createOpening(const std::string &wallId, const std::string &openingId, std::string openingName, OpeningKind kind, double t, double width, double height, double sill=0.0)
Insert a door/window opening on an existing wall.
eve::Result< void > createRoom(const std::string &levelId, const std::string &roomId, std::string roomName, std::vector< Vec2 > polygon, double wallHeight, double wallThickness, double slabThickness)
Create a closed room: perimeter walls + slab + zone under an existing level.
std::uint8_t PassageMask
Bitmask of PassageMode values.
OpeningKind
Opening semantic used by doors and windows hosted on walls.
void appendWallWithOpenings(MeshBake &bake, const Node &wall, double elevation, const std::vector< const Node * > &openings)
Bake one wall with merged opening cavities and explicit reveal liners.
void appendCatalogItem(MeshBake &bake, const std::string &itemId, const std::string &catalogId, const Vec3 &position, double yawDegrees)
Append yaw-oriented catalog geometry into a mesh bake.
NodeKind
Stable node kind discriminator for the architectural document.
@ Portal
Level-hosted portal connecting two regions (dungeon/cave or free openings).
TopologyKind
Document topology dialect. Storage stays one node graph; semantics and authoring helpers differ by di...
bool isFinite(float value)
True when finite.
void appendBox(MeshBuild &mesh, V3 center, V3 side, V3 up, V3 forward, float hx, float hy, float hz, RoadMaterial material)
Module-private road bake implementation helper.
Definition RoadBake.cpp:402
Renderer-neutral triangle mesh rebuilt from the document.
One authored architectural node. Only fields for the active kind are meaningful.
std::vector< std::string > children
std::vector< Vec2 > polygon
Metres on the level XZ plane.
Metres in world/level space.