载入中...
搜索中...
未找到
BuildingAssemblyGeometry.cpp
浏览该文件的文档.
1#include <algorithm>
2#include <atomic>
3#include <cmath>
4#include <limits>
8#include "grid/GridConfig.h"
9
11uint64_t nextEpoch() {
12 static std::atomic<uint64_t> sequence{1};
13 return sequence.fetch_add(1, std::memory_order_relaxed);
14}
16 const auto& g = world.getGrid();
17 if (g.layout != grid::GridLayout::Rectangle || g.cellW != g.cellH || g.cellGapX != 0 || g.cellGapY != 0 ||
18 !std::isfinite(g.cellW) || g.cellW <= 0 || !std::isfinite(g.originX) || !std::isfinite(g.originY) ||
19 world.hasGridFromLayer())
20 return fail<void>("House composition requires an unbound rectangular square grid without gaps");
21 return Result<void>::success();
22}
23namespace {
24std::pair<double, double> rotate(double x, double y, int q) {
25 switch ((q % 4 + 4) % 4) {
26 case 1: return {y, -x};
27 case 2: return {-x, -y};
28 case 3: return {-y, x};
29 default: return {x, y};
30 }
31}
32} // namespace
35 if (from.quarterTurns < 0 || from.quarterTurns > 3 || to.quarterTurns < 0 || to.quarterTurns > 3 ||
36 !std::isfinite(from.elevation) || !std::isfinite(to.elevation))
37 return fail<PlacedBuilding>("Invalid assembly pose");
38 const auto* def = BuildingRegistry::find(part.buildingId);
39 if (!def) return fail<PlacedBuilding>("Missing definition: " + part.buildingId);
40 const int q = (to.quarterTurns - from.quarterTurns + 4) % 4;
41 auto point = [&](double x, double y) {
42 auto [rx, ry] = rotate(x - from.x, y - from.y, q);
43 return std::pair{rx + to.x, ry + to.y};
44 };
45 // Bound arithmetic before any integer conversion, including footprint expansion.
46 constexpr double limit = 10000000;
47 auto valid = [&](double v) { return std::isfinite(v) && std::abs(v) < limit; };
48 PlacedBuilding out = part;
49 const double level = double(part.level) - from.level + to.level;
50 if (!valid(level)) return fail<PlacedBuilding>("Floor coordinate overflow");
51 out.level = static_cast<int>(level);
52 out.elevation = part.elevation - from.elevation + to.elevation;
53 out.rotationDeg = std::fmod(part.rotationDeg + 90.f * q + 360.f, 360.f);
54 if (part.placementKind == "cell") {
55 if (def->rotationMode == "hex" || (q && def->rotationMode == "none"))
56 return fail<PlacedBuilding>("Definition does not support rigid quarter-turn rotation");
57 double minX = limit, minY = limit;
59 *def, part.originCellX, part.originCellY, part.rotationDeg, [&](int cx, int cy) {
60 for (auto [x, y] : {point(cx, cy), point(double(cx) + 1, cy), point(cx, double(cy) + 1),
61 point(double(cx) + 1, double(cy) + 1)}) {
62 minX = std::min(minX, x);
63 minY = std::min(minY, y);
64 }
65 return true;
66 });
67 if (!valid(minX) || !valid(minY)) return fail<PlacedBuilding>("Cell coordinate overflow");
68 out.originCellX = int(minX);
69 out.originCellY = int(minY);
70 world.cellToWorldPlane(out.originCellX, out.originCellY, out.worldX, out.worldY);
71 } else if (part.placementKind == "edge") {
72 auto a = point(part.edge.x, part.edge.y);
73 auto b = point(double(part.edge.x) + (part.edge.axis == EdgeAxis::Horizontal),
74 double(part.edge.y) + (part.edge.axis == EdgeAxis::Vertical));
75 if (!valid(a.first) || !valid(a.second) || !valid(b.first) || !valid(b.second))
76 return fail<PlacedBuilding>("Edge coordinate overflow");
77 out.edge = {int(std::min(a.first, b.first)), int(std::min(a.second, b.second)),
78 a.second == b.second ? EdgeAxis::Horizontal : EdgeAxis::Vertical};
79 out.originCellX = out.edge.x;
80 out.originCellY = out.edge.y;
81 out.worldX = world.getOriginX() + float((a.first + b.first) * 0.5) * world.getCellSize();
82 out.worldY = world.getOriginY() + float((a.second + b.second) * 0.5) * world.getCellSize();
83 out.rotationDeg = out.edge.axis == EdgeAxis::Horizontal ? 0.f : 90.f;
84 } else if (part.placementKind == "corner") {
85 auto [x, y] = point(part.corner.x, part.corner.y);
86 if (!valid(x) || !valid(y)) return fail<PlacedBuilding>("Corner coordinate overflow");
87 out.corner = {int(x), int(y)};
88 out.originCellX = int(x);
89 out.originCellY = int(y);
90 out.worldX = world.getOriginX() + float(x) * world.getCellSize();
91 out.worldY = world.getOriginY() + float(y) * world.getCellSize();
92 } else if (part.placementKind == "free") {
93 out.rotationDeg = std::fmod(part.rotationDeg - 90.f * q + 720.f, 360.f);
94 auto [x, y] = point((part.worldX - world.getOriginX()) / world.getCellSize(),
95 (part.worldY - world.getOriginY()) / world.getCellSize());
96 if (!valid(x) || !valid(y)) return fail<PlacedBuilding>("Free coordinate overflow");
97 out.worldX = world.getOriginX() + float(x) * world.getCellSize();
98 out.worldY = world.getOriginY() + float(y) * world.getCellSize();
99 out.originCellX = int(std::floor(x));
100 out.originCellY = int(std::floor(y));
101 } else
102 return fail<PlacedBuilding>("Unknown placement domain");
103 if (!std::isfinite(out.elevation) || !std::isfinite(out.rotationDeg))
104 return fail<PlacedBuilding>("Non-finite transformed pose");
105 out.supportInstanceIds.clear();
106 // A rigid blueprint does not transfer provider-local surface attachments.
107 out.surfaceId.clear();
108 out.surfaceRevision = 0;
109 out.surfaceSampleCount = 0;
110 out.surfaceMaxSlopeDegrees = 0;
111 out.surfaceHeightDelta = 0;
112 out.surfaceNormalX = 0;
113 out.surfaceNormalY = world.getGridPlaneName() == "xz" ? 1.f : 0.f;
114 out.surfaceNormalZ = world.getGridPlaneName() == "xz" ? 0.f : 1.f;
115 out.surfaceTangentX = 1;
116 out.surfaceTangentY = 0;
117 out.surfaceTangentZ = 0;
118 return Result<PlacedBuilding>::success(std::move(out));
119}
121 return Value::Object{
122 {"x", p.x}, {"y", p.y}, {"level", p.level}, {"quarterTurns", p.quarterTurns}, {"elevation", p.elevation}};
123}
125 for (const auto& key : v.keys())
126 if (key != "x" && key != "y" && key != "level" && key != "quarterTurns" && key != "elevation")
127 return fail<AssemblyPose>("Unknown pose field");
129 for (auto [key, out] :
130 {std::pair{"x", &p.x}, {"y", &p.y}, {"level", &p.level}, {"quarterTurns", &p.quarterTurns}}) {
131 const auto* n = v.find(key);
132 if (!n || !n->isInt64() || std::abs(double(n->asInt())) >= 10000000)
133 return fail<AssemblyPose>("Invalid pose integer");
134 *out = int(n->asInt());
135 }
136 const auto* e = v.find("elevation");
137 if (!e || !e->isNumeric()) return fail<AssemblyPose>("Invalid pose elevation");
138 p.elevation = float(e->isInt64() ? double(e->asInt()) : e->asDouble());
139 if (!std::isfinite(p.elevation) || p.quarterTurns < 0 || p.quarterTurns > 3)
140 return fail<AssemblyPose>("Invalid pose");
142}
144 for (auto it = state.records.begin(); it != state.records.end();) {
145 std::erase_if(it->second.members, [&](const auto& entry) { return entry.second == member; });
146 if (it->second.members.empty()) {
147 state.handles.erase(it->first);
148 it = state.records.erase(it);
149 } else
150 ++it;
151 }
152}
153} // namespace eve::building::assembly_detail
double y
double x
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
glm::vec4 p[6]
SQObjectPtr key
Definition ECS.cpp:155
tensor::Graph g
Definition GpuGraph.cpp:7
glm::vec3 n
Definition Grass.cpp:63
Module-neutral grid topology and sizing (no building/map dependency).
std::array< double, 10 > q
float v
std::string to
std::string from
bool valid
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
int level
std::unique_ptr< gpgpu::Sequence > sequence
Definition OnnxGpgpu.cpp:43
ScriptStructure out
int limit
Definition TreeMesh.cpp:166
float(ui::Theme::* member)[4]
glm::vec3 point
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
std::map< std::string, Value > Object
Definition Value.h:34
static const BuildingDefinition * find(const std::string &id)
Finds .
Definition Building.cpp:557
static bool foreachFootprintCell(const BuildingDefinition &def, int originCellX, int originCellY, float rotationDeg, const std::function< bool(int cx, int cy)> &fn)
枚举占地格子(旋转后局部 → 世界格子)。返回 false 若定义未知。
格子型建筑放置世界(脚本可直接操作)。
EVENGINE_API_FOUNDATION public API.
Definition Json.h:39
void removeMembership(AssemblyState &state, int member)
Result< AssemblyPose > parsePose(const Value &v)
Result< void > checkGrid(const PlacementWorld &world)
Result< PlacedBuilding > transformPart(const PlacedBuilding &part, const PlacementWorld &world, AssemblyPose from, AssemblyPose to)
Integer grid-vertex origin, floor offset and quarter turns: (x,y) maps to (y,-x) for one turn.
已放置的建筑实例。
int level
Authoritative discrete floor coordinate used by occupancy and topology.
std::string placementKind
Placement domain snapshot: cell, edge, corner, or free.