载入中...
搜索中...
未找到
ArchSpaceQuery.cpp
浏览该文件的文档.
2
4
5#include <cmath>
6#include <queue>
7#include <unordered_map>
8#include <unordered_set>
9#include <utility>
10
11namespace eve::archspace {
12namespace {
13
14constexpr double kEps = 1e-8;
15
16bool pointInPolygon(Vec2 p, const std::vector<Vec2>& poly) {
17 if (poly.size() < 3) return false;
18 bool inside = false;
19 for (std::size_t i = 0, j = poly.size() - 1; i < poly.size(); j = i++) {
20 const Vec2& a = poly[i];
21 const Vec2& b = poly[j];
22 const bool intersect = ((a.z > p.z) != (b.z > p.z)) &&
23 (p.x < (b.x - a.x) * (p.z - a.z) / ((b.z - a.z) + (b.z == a.z ? kEps : 0.0)) + a.x);
24 if (intersect) inside = !inside;
25 }
26 return inside;
27}
28
29SpaceRegion toRegion(const Node& zone) {
30 SpaceRegion region;
31 region.id = zone.id;
32 region.levelId = zone.parentId;
33 region.name = zone.name;
34 region.polygon = zone.polygon;
35 return region;
36}
37
38Vec3 openingWorldPosition(const Document& document, const Node& opening) {
39 const Node* wall = document.find(opening.parentId);
40 if (!wall) return {};
41 return Vec3{wall->start.x + (wall->end.x - wall->start.x) * opening.t, opening.sill + opening.openingHeight * 0.5,
42 wall->start.z + (wall->end.z - wall->start.z) * opening.t};
43}
44
45std::string nearestRegionId(const Document& document, const std::string& levelId, Vec2 xz) {
46 std::string best;
47 double bestDist2 = 0.0;
48 bool any = false;
49 for (const auto& [id, node] : document.nodes()) {
50 (void)id;
51 if (node.kind != NodeKind::Zone || node.parentId != levelId || node.polygon.empty()) continue;
52 double cx = 0.0, cz = 0.0;
53 for (const auto& p : node.polygon) {
54 cx += p.x;
55 cz += p.z;
56 }
57 cx /= static_cast<double>(node.polygon.size());
58 cz /= static_cast<double>(node.polygon.size());
59 const double dx = cx - xz.x;
60 const double dz = cz - xz.z;
61 const double d2 = dx * dx + dz * dz;
62 if (!any || d2 < bestDist2) {
63 any = true;
64 bestDist2 = d2;
65 best = node.id;
66 }
67 }
68 return best;
69}
70
71SpacePortal toPortalFromOpening(const Document& document, const Node& opening) {
72 SpacePortal portal;
73 portal.id = opening.id;
74 portal.name = opening.name;
75 portal.wallHosted = true;
76 portal.passageMask = opening.openingKind == OpeningKind::Door ? kPassageWalk : PassageMask{0};
77 portal.position = openingWorldPosition(document, opening);
78 const Node* wall = document.find(opening.parentId);
79 portal.levelId = wall ? wall->parentId : std::string{};
80 if (wall) {
81 const Vec2 mid{(wall->start.x + wall->end.x) * 0.5, (wall->start.z + wall->end.z) * 0.5};
82 // Sample slightly off the wall midline toward both normals to find adjacent rooms.
83 Vec2 dir{wall->end.x - wall->start.x, wall->end.z - wall->start.z};
84 const double len = std::sqrt(dir.x * dir.x + dir.z * dir.z);
85 if (len > kEps) {
86 dir.x /= len;
87 dir.z /= len;
88 const Vec2 n{-dir.z, dir.x};
89 const double offset = std::max(wall->thickness, 0.2);
90 const auto a = nearestRegionId(document, portal.levelId, Vec2{mid.x + n.x * offset, mid.z + n.z * offset});
91 const auto b = nearestRegionId(document, portal.levelId, Vec2{mid.x - n.x * offset, mid.z - n.z * offset});
92 portal.regionAId = a;
93 portal.regionBId = (b != a) ? b : std::string{};
94 }
95 }
96 return portal;
97}
98
99SpacePortal toPortalFromNode(const Node& node) {
100 SpacePortal portal;
101 portal.id = node.id;
102 portal.levelId = node.parentId;
103 portal.name = node.name;
104 portal.regionAId = node.regionAId;
105 portal.regionBId = node.regionBId;
106 portal.passageMask = node.passageMask;
107 portal.position = node.portalPosition;
108 portal.wallHosted = false;
109 return portal;
110}
111
112} // namespace
113
114SpaceQuery::SpaceQuery(const Document& document) noexcept : document_(&document) {}
115
117 return document_ ? document_->topology() : TopologyKind::Architectural;
118}
119
120std::vector<std::string> SpaceQuery::strata() const {
121 std::vector<std::string> out;
122 if (!document_) return out;
123 for (const auto& [id, node] : document_->nodes()) {
124 if (node.kind == NodeKind::Level) out.push_back(id);
125 }
126 return out;
127}
128
129std::vector<SpaceRegion> SpaceQuery::regions() const {
130 std::vector<SpaceRegion> out;
131 if (!document_) return out;
132 for (const auto& [id, node] : document_->nodes()) {
133 (void)id;
134 if (node.kind == NodeKind::Zone) out.push_back(toRegion(node));
135 }
136 return out;
137}
138
139std::optional<SpaceRegion> SpaceQuery::regionAt(Vec2 xz, const std::string& levelId) const {
140 if (!document_ || !std::isfinite(xz.x) || !std::isfinite(xz.z)) return std::nullopt;
141 for (const auto& [id, node] : document_->nodes()) {
142 (void)id;
143 if (node.kind != NodeKind::Zone || node.parentId != levelId) continue;
144 if (pointInPolygon(xz, node.polygon)) return toRegion(node);
145 }
146 return std::nullopt;
147}
148
149std::vector<SpacePortal> SpaceQuery::portals() const {
150 std::vector<SpacePortal> out;
151 if (!document_) return out;
152 for (const auto& [id, node] : document_->nodes()) {
153 (void)id;
154 if (node.kind == NodeKind::Opening)
155 out.push_back(toPortalFromOpening(*document_, node));
156 else if (node.kind == NodeKind::Portal)
157 out.push_back(toPortalFromNode(node));
158 }
159 return out;
160}
161
162std::vector<SpacePortal> SpaceQuery::portalsFrom(const std::string& regionId) const {
163 std::vector<SpacePortal> out;
164 if (regionId.empty()) return out;
165 for (auto& portal : portals()) {
166 if (portal.regionAId == regionId || portal.regionBId == regionId) out.push_back(std::move(portal));
167 }
168 return out;
169}
170
171eve::Result<bool> SpaceQuery::regionsConnected(const std::string& regionAId, const std::string& regionBId) const {
172 if (!document_)
174 "ArchSpace SpaceQuery has no document", "document", {},
175 "archspace.query"));
176 if (regionAId.empty() || regionBId.empty())
178 "ArchSpace region ids must be non-empty", "regionId",
179 {}, "archspace.query"));
180 if (!document_->find(regionAId) || !document_->find(regionBId))
182 eve::DiagnosticCode::NotFound, "ArchSpace region id is unknown", "regionId", {}, "archspace.query"));
183 if (regionAId == regionBId) return eve::Result<bool>::success(true);
184 std::unordered_map<std::string, std::vector<std::string>> adj;
185 for (const auto& portal : portals()) {
186 if (portal.regionAId.empty()) continue;
187 if (!portal.regionBId.empty()) {
188 adj[portal.regionAId].push_back(portal.regionBId);
189 adj[portal.regionBId].push_back(portal.regionAId);
190 }
191 }
192 std::unordered_set<std::string> seen;
193 std::queue<std::string> q;
194 q.push(regionAId);
195 seen.insert(regionAId);
196 while (!q.empty()) {
197 const std::string cur = std::move(q.front());
198 q.pop();
199 if (cur == regionBId) return eve::Result<bool>::success(true);
200 const auto it = adj.find(cur);
201 if (it == adj.end()) continue;
202 for (const auto& next : it->second) {
203 if (seen.insert(next).second) q.push(next);
204 }
205 }
206 return eve::Result<bool>::success(false);
207}
208
209std::vector<SpaceAnchor> SpaceQuery::anchorsIn(const std::string& regionId) const {
210 std::vector<SpaceAnchor> out;
211 if (!document_) return out;
212 const Node* region = document_->find(regionId);
213 if (!region || region->kind != NodeKind::Zone) return out;
214 for (const auto& [id, node] : document_->nodes()) {
215 (void)id;
216 if (node.kind != NodeKind::Item || node.parentId != region->parentId) continue;
217 if (!pointInPolygon(Vec2{node.position.x, node.position.z}, region->polygon)) continue;
218 SpaceAnchor anchor;
219 anchor.id = node.id;
220 anchor.levelId = node.parentId;
221 anchor.name = node.name;
222 anchor.catalogId = node.catalogId;
223 anchor.position = node.position;
224 anchor.yawDegrees = node.yawDegrees;
225 out.push_back(std::move(anchor));
226 }
227 return out;
228}
229
230} // namespace eve::archspace
int mid
Definition AnimSmr.cpp:120
float cx
Definition CardTypes.cpp:36
glm::vec4 p[6]
ScriptFieldRegion region
glm::vec3 n
Definition Grass.cpp:63
std::array< double, 10 > q
size_t offset
std::map< std::string, Node > nodes
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
glm::dvec3 dx
glm::dvec3 dz
const RoadNode * node
ScriptStructure out
std::string string
std::map< Cell, int > best
V3 dir
Definition TreeMesh.cpp:151
std::vector< char > inside
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
Authoritative ArchSpace document (Pascal-like node graph).
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....
const std::unordered_map< std::string, Node > & nodes() const noexcept
TopologyKind topology() const noexcept
Topology dialect for authoring helpers and SpaceQuery.
TopologyKind topology() const noexcept
Document topology dialect.
std::vector< std::string > strata() const
Level (stratum) ids in document iteration order.
eve::Result< bool > regionsConnected(const std::string &regionAId, const std::string &regionBId) const
Whether two regions share a portal path (undirected, same connected component).
std::vector< SpaceRegion > regions() const
All Zone regions as SpaceRegion snapshots.
std::vector< SpacePortal > portals() const
Wall openings and level portals as SpacePortal snapshots.
std::vector< SpaceAnchor > anchorsIn(const std::string &regionId) const
Item anchors whose position lies inside the region polygon (same level).
SpaceQuery(const Document &document) noexcept
Bind to an existing document. @ownership Non-owning borrow of document; caller retains ownership....
std::optional< SpaceRegion > regionAt(Vec2 xz, const std::string &levelId) const
First region whose polygon contains the XZ point on the given level.
std::vector< SpacePortal > portalsFrom(const std::string &regionId) const
Portals that touch the given region id.
std::uint8_t PassageMask
Bitmask of PassageMode values.
constexpr PassageMask kPassageWalk
Default walkable mask.
@ 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 pointInPolygon(float px, float py, const std::vector< HousePolygonPoint > &poly)
Ray-casting point-in-polygon test in corner coordinates.
One authored architectural node. Only fields for the active kind are meaningful.
Read-only item/anchor view.
Metres on the level XZ plane.