载入中...
搜索中...
未找到
ArchSpaceProjector.cpp
浏览该文件的文档.
2
5
6#include <algorithm>
7#include <cmath>
8#include <limits>
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
29void stampSegment(GridProjection& grid, Vec2 a, Vec2 b, GridCellKind kind) {
30 if (grid.width <= 0 || grid.height <= 0 || grid.cellSize <= kEps) return;
31 const double len = std::sqrt((b.x - a.x) * (b.x - a.x) + (b.z - a.z) * (b.z - a.z));
32 const int steps = std::max(1, static_cast<int>(std::ceil(len / (grid.cellSize * 0.5))));
33 for (int s = 0; s <= steps; ++s) {
34 const double t = static_cast<double>(s) / static_cast<double>(steps);
35 const double x = a.x + (b.x - a.x) * t;
36 const double z = a.z + (b.z - a.z) * t;
37 const int cx = static_cast<int>(std::floor((x - grid.originX) / grid.cellSize));
38 const int cz = static_cast<int>(std::floor((z - grid.originZ) / grid.cellSize));
39 if (cx < 0 || cz < 0 || cx >= grid.width || cz >= grid.height) continue;
40 const std::size_t idx = static_cast<std::size_t>(cz * grid.width + cx);
42 grid.cells[idx] == GridCellKind::Floor) {
43 grid.cells[idx] = kind;
44 }
45 }
46}
47
48} // namespace
49
51 mesh_ = document.bakeMesh();
52 revision_ = revision;
54}
55
57 mesh_ = {};
58 revision_ = 0;
59}
60
61eve::Result<GridProjection> bakeGridProjection(const Document& document, const std::string& levelId, double cellSize,
62 const std::string& orientation) {
63 const Node* level = document.find(levelId);
64 if (!level || level->kind != NodeKind::Level)
66 eve::Diagnostic::error(eve::DiagnosticCode::NotFound, "ArchSpace grid bake requires an existing level",
67 "levelId", {}, "archspace.grid"));
68 if (!std::isfinite(cellSize) || cellSize <= kEps)
71 "ArchSpace grid cellSize must be finite and > 0", "cellSize", {}, "archspace.grid"));
72 if (orientation != "orthogonal" && orientation != "isometric")
74 eve::DiagnosticCode::InvalidArgument, "ArchSpace grid orientation must be orthogonal or isometric",
75 "orientation", {}, "archspace.grid"));
76
77 double minX = std::numeric_limits<double>::infinity();
78 double minZ = std::numeric_limits<double>::infinity();
79 double maxX = -std::numeric_limits<double>::infinity();
80 double maxZ = -std::numeric_limits<double>::infinity();
81 bool any = false;
82 for (const auto& [id, node] : document.nodes()) {
83 (void)id;
84 if (node.kind == NodeKind::Zone && node.parentId == levelId) {
85 for (const auto& p : node.polygon) {
86 minX = std::min(minX, p.x);
87 minZ = std::min(minZ, p.z);
88 maxX = std::max(maxX, p.x);
89 maxZ = std::max(maxZ, p.z);
90 any = true;
91 }
92 } else if (node.kind == NodeKind::Wall && node.parentId == levelId) {
93 minX = std::min({minX, node.start.x, node.end.x});
94 minZ = std::min({minZ, node.start.z, node.end.z});
95 maxX = std::max({maxX, node.start.x, node.end.x});
96 maxZ = std::max({maxZ, node.start.z, node.end.z});
97 any = true;
98 }
99 }
100 GridProjection grid;
101 grid.cellSize = cellSize;
102 grid.orientation = orientation;
103 if (!any) {
104 grid.width = 0;
105 grid.height = 0;
106 return eve::Result<GridProjection>::success(std::move(grid));
107 }
108 // Pad one cell so wall midlines near the AABB edge stay in-bounds.
109 minX -= cellSize;
110 minZ -= cellSize;
111 maxX += cellSize;
112 maxZ += cellSize;
113 grid.originX = minX;
114 grid.originZ = minZ;
115 grid.width = std::max(1, static_cast<int>(std::ceil((maxX - minX) / cellSize)));
116 grid.height = std::max(1, static_cast<int>(std::ceil((maxZ - minZ) / cellSize)));
117 const std::size_t count = static_cast<std::size_t>(grid.width) * static_cast<std::size_t>(grid.height);
118 grid.cells.assign(count, GridCellKind::Empty);
119 grid.regionIds.assign(count, {});
120
121 SpaceQuery query(document);
122 for (int z = 0; z < grid.height; ++z) {
123 for (int x = 0; x < grid.width; ++x) {
124 const Vec2 sample{grid.originX + (static_cast<double>(x) + 0.5) * cellSize,
125 grid.originZ + (static_cast<double>(z) + 0.5) * cellSize};
126 const auto region = query.regionAt(sample, levelId);
127 if (!region) continue;
128 const std::size_t idx = static_cast<std::size_t>(z * grid.width + x);
129 grid.cells[idx] = GridCellKind::Floor;
130 grid.regionIds[idx] = region->id;
131 }
132 }
133
134 for (const auto& [id, node] : document.nodes()) {
135 (void)id;
136 if (node.kind == NodeKind::Wall && node.parentId == levelId) {
137 stampSegment(grid, node.start, node.end, GridCellKind::Wall);
138 } else if (node.kind == NodeKind::Portal && node.parentId == levelId) {
139 stampSegment(grid, Vec2{node.portalPosition.x, node.portalPosition.z},
140 Vec2{node.portalPosition.x, node.portalPosition.z}, GridCellKind::Portal);
141 } else if (node.kind == NodeKind::Opening) {
142 const Node* wall = document.find(node.parentId);
143 if (!wall || wall->parentId != levelId) continue;
144 const Vec2 mid{wall->start.x + (wall->end.x - wall->start.x) * node.t,
145 wall->start.z + (wall->end.z - wall->start.z) * node.t};
146 stampSegment(grid, mid, mid, GridCellKind::Portal);
147 }
148 }
149
150 return eve::Result<GridProjection>::success(std::move(grid));
151}
152
153} // namespace eve::archspace
double z
double x
int mid
Definition AnimSmr.cpp:120
std::uint64_t revision
const std::string & s
float cx
Definition CardTypes.cpp:36
glm::vec4 p[6]
ScriptFieldRegion region
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
int level
int idx
int steps
float t
const RoadNode * node
std::string kind
int count
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
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).
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
MeshBake bakeMesh() const
Build wall boxes (with opening cutouts), slabs and item markers for viewport upload.
eve::Result< void > sync(const Document &document, std::uint64_t revision) override
Rebuild derived presentation from document at revision.
void clear() override
Drop derived presentation state.
std::uint64_t revision() const noexcept
Revision last successfully synced; 0 before the first sync.
Presentation-agnostic query facade over an ArchSpace document.
@ Portal
Level-hosted portal connecting two regions (dungeon/cave or free openings).
GridCellKind
Semantic cell kind for grid projections (tilemap / HD2D hosts).
eve::Result< GridProjection > bakeGridProjection(const Document &document, const std::string &levelId, double cellSize, const std::string &orientation)
Rasterize one level into a grid for ortho/iso/HD2D hosts.
bool pointInPolygon(float px, float py, const std::vector< HousePolygonPoint > &poly)
Ray-casting point-in-polygon test in corner coordinates.
WidgetDesc grid(int columns, std::vector< WidgetDesc > children, std::string id)
Fixed-column grid; children are placed in source order.
Definition Widget.cpp:878
Renderer-neutral occupancy grid rebuilt from an ArchSpace level.
One authored architectural node. Only fields for the active kind are meaningful.
Metres on the level XZ plane.
std::string id
Definition Widget.h:17