载入中...
搜索中...
未找到
PlacementGeometry.cpp
浏览该文件的文档.
2
4#include "building/Ghost.h"
8#include "common/Diagnostic.h"
10
11#include <algorithm>
12#include <cmath>
13#include <limits>
14#include <unordered_set>
15
16namespace eve::building {
17
18namespace placement_detail {
19
20bool hasFreeEdgeOverlap(const PlacementWorld &world, const BuildingDefinition &freeDefinition,
21 const FreeFootprint &footprint, const BuildingDefinition &edgeDefinition, EdgeAddress edge) {
22 if (edgeDefinition.edgeThicknessCells <= 0 ||
23 std::find(freeDefinition.blockedByEdgeChannels.begin(), freeDefinition.blockedByEdgeChannels.end(),
24 edgeDefinition.channel) == freeDefinition.blockedByEdgeChannels.end())
25 return false;
26 float ax = 0, ay = 0, bx = 0, by = 0;
27 world.cellToWorldPlane(edge.x, edge.y, ax, ay);
28 world.cellToWorldPlane(edge.x + (edge.axis == EdgeAxis::Horizontal), edge.y + (edge.axis == EdgeAxis::Vertical), bx,
29 by);
30 // cellToWorldPlane is a vertex projection for rectangle grids.
31 FreeFootprint wall;
32 wall.x = (ax + bx) * .5f;
33 wall.y = (ay + by) * .5f;
34 wall.halfWidth = std::hypot(bx - ax, by - ay) * .5f;
35 wall.halfHeight = edgeDefinition.edgeThicknessCells * world.getCellSize() * .5f;
36 wall.rotationRadians = std::atan2(by - ay, bx - ax);
37 wall.vertices = {{-wall.halfWidth, -wall.halfHeight},
38 {wall.halfWidth, -wall.halfHeight},
39 {wall.halfWidth, wall.halfHeight},
40 {-wall.halfWidth, wall.halfHeight}};
41 return hasFreeFootprintOverlap(footprint, wall);
42}
43
44float wrapDeg(float deg) {
45 deg = std::fmod(deg, 360.f);
46 if (deg < 0.f) deg += 360.f;
47 return deg;
48}
49
50int cardinalQuarter(float rotationDeg) {
51 const float d = wrapDeg(rotationDeg);
52 int q = int(std::lround(d / 90.f)) % 4;
53 if (q < 0) q += 4;
54 return q;
55}
56
57float length3(float x, float y, float z) { return std::sqrt(x * x + y * y + z * z); }
58
59
61 float y, float rotationDeg) {
63 result.x = x;
64 result.y = y;
65 result.radius = definition.freeRadiusCells * std::min(world.getGrid().cellW, world.getGrid().cellH);
66 if (!definition.freeFootprintVertices.empty()) {
67 for (size_t i = 0; i < definition.freeFootprintVertices.size(); i += 2) {
68 result.vertices.push_back({definition.freeFootprintVertices[i] * world.getGrid().cellW,
69 definition.freeFootprintVertices[i + 1] * world.getGrid().cellH});
70 result.halfWidth = std::max(result.halfWidth, std::fabs(result.vertices.back().x));
71 result.halfHeight = std::max(result.halfHeight, std::fabs(result.vertices.back().y));
72 }
73 } else if (definition.freeFootprintWidthCells > 0.f && definition.freeFootprintHeightCells > 0.f) {
74 result.halfWidth = definition.freeFootprintWidthCells * world.getGrid().cellW * 0.5f;
75 result.halfHeight = definition.freeFootprintHeightCells * world.getGrid().cellH * 0.5f;
76 result.vertices = {{-result.halfWidth, -result.halfHeight},
77 {result.halfWidth, -result.halfHeight},
78 {result.halfWidth, result.halfHeight},
79 {-result.halfWidth, result.halfHeight}};
80 }
81 result.rotationRadians = wrapDeg(rotationDeg) * 3.14159265358979323846f / 180.f;
82 return result;
83}
84
86 const PlacementWorld &world) {
87 const FreeFootprint footprint = definitionFreeFootprint(definition, world, placed.worldX, placed.worldY, 0.f);
88 placed.freeRadius = footprint.radius;
89 placed.freeHalfWidth = footprint.halfWidth;
90 placed.freeHalfHeight = footprint.halfHeight;
91 placed.freeFootprintVertices.clear();
92 if (!definition.freeFootprintVertices.empty()) {
93 placed.freeFootprintVertices.reserve(footprint.vertices.size() * 2);
94 for (const auto &point : footprint.vertices) {
95 placed.freeFootprintVertices.push_back(point.x);
96 placed.freeFootprintVertices.push_back(point.y);
97 }
98 }
99}
100
103 result.x = placed.worldX;
104 result.y = placed.worldY;
105 result.radius = placed.freeRadius;
106 result.halfWidth = placed.freeHalfWidth;
107 result.halfHeight = placed.freeHalfHeight;
108 result.rotationRadians = wrapDeg(placed.rotationDeg) * 3.14159265358979323846f / 180.f;
109 for (size_t i = 0; i + 1 < placed.freeFootprintVertices.size(); i += 2)
110 result.vertices.push_back({placed.freeFootprintVertices[i], placed.freeFootprintVertices[i + 1]});
111 if (result.vertices.empty() && result.halfWidth > 0.f && result.halfHeight > 0.f)
112 result.vertices = {{-result.halfWidth, -result.halfHeight},
113 {result.halfWidth, -result.halfHeight},
114 {result.halfWidth, result.halfHeight},
115 {-result.halfWidth, result.halfHeight}};
116 return result;
117}
118
119std::vector<FreeFootprint::Point> worldVertices(const FreeFootprint &footprint) {
120 std::vector<FreeFootprint::Point> result;
121 result.reserve(footprint.vertices.size());
122 const float c = std::cos(footprint.rotationRadians);
123 const float s = std::sin(footprint.rotationRadians);
124 for (const auto &point : footprint.vertices)
125 result.push_back({footprint.x + point.x * c - point.y * s, footprint.y + point.x * s + point.y * c});
126 return result;
127}
128
129bool isPointInPolygon(const FreeFootprint &polygon, float x, float y) {
130 const auto vertices = worldVertices(polygon);
131 float winding = 0.f;
132 for (size_t i = 0; i < vertices.size(); ++i) {
133 const auto &a = vertices[i];
134 const auto &b = vertices[(i + 1) % vertices.size()];
135 const float cross = (b.x - a.x) * (y - a.y) - (b.y - a.y) * (x - a.x);
136 if (std::fabs(cross) <= 1e-5f) continue;
137 if (winding == 0.f)
138 winding = cross;
139 else if (cross * winding < 0.f)
140 return false;
141 }
142 return true;
143}
144
146 const float dx = b.x - a.x;
147 const float dy = b.y - a.y;
148 if (!a.isPolygon() && !b.isPolygon()) return std::hypot(dx, dy) < a.radius + b.radius;
149
150 auto circlePolygonOverlap = [](const FreeFootprint &circle, const FreeFootprint &polygon) {
151 if (isPointInPolygon(polygon, circle.x, circle.y)) return true;
152 const auto vertices = worldVertices(polygon);
153 const float radiusSquared = circle.radius * circle.radius;
154 for (size_t i = 0; i < vertices.size(); ++i) {
155 const auto &a = vertices[i];
156 const auto &b = vertices[(i + 1) % vertices.size()];
157 const float edgeX = b.x - a.x;
158 const float edgeY = b.y - a.y;
159 const float lengthSquared = edgeX * edgeX + edgeY * edgeY;
160 const float t =
161 lengthSquared > 0.f
162 ? std::clamp(((circle.x - a.x) * edgeX + (circle.y - a.y) * edgeY) / lengthSquared, 0.f, 1.f)
163 : 0.f;
164 const float closestX = a.x + edgeX * t;
165 const float closestY = a.y + edgeY * t;
166 const float distanceX = circle.x - closestX;
167 const float distanceY = circle.y - closestY;
168 if (distanceX * distanceX + distanceY * distanceY < radiusSquared) return true;
169 }
170 return false;
171 };
172 if (!a.isPolygon()) return circlePolygonOverlap(a, b);
173 if (!b.isPolygon()) return circlePolygonOverlap(b, a);
174
175 const auto aVertices = worldVertices(a);
176 const auto bVertices = worldVertices(b);
177 for (const auto *vertices : {&aVertices, &bVertices}) {
178 for (size_t i = 0; i < vertices->size(); ++i) {
179 const auto &p = vertices->at(i);
180 const auto &q = vertices->at((i + 1) % vertices->size());
181 const float axisX = -(q.y - p.y);
182 const float axisY = q.x - p.x;
183 float aMin = std::numeric_limits<float>::max();
184 float aMax = std::numeric_limits<float>::lowest();
185 float bMin = std::numeric_limits<float>::max();
186 float bMax = std::numeric_limits<float>::lowest();
187 for (const auto &vertex : aVertices) {
188 const float projection = vertex.x * axisX + vertex.y * axisY;
189 aMin = std::min(aMin, projection);
190 aMax = std::max(aMax, projection);
191 }
192 for (const auto &vertex : bVertices) {
193 const float projection = vertex.x * axisX + vertex.y * axisY;
194 bMin = std::min(bMin, projection);
195 bMax = std::max(bMax, projection);
196 }
197 if (aMax <= bMin || bMax <= aMin) return false;
198 }
199 }
200 return true;
201}
202
203bool containsFreeFootprintPoint(const FreeFootprint &footprint, float x, float y) {
204 const float dx = x - footprint.x;
205 const float dy = y - footprint.y;
206 if (!footprint.isPolygon()) return std::hypot(dx, dy) <= footprint.radius;
207 return isPointInPolygon(footprint, x, y);
208}
209
210void cross3(float ax, float ay, float az, float bx, float by, float bz, float &x, float &y, float &z) {
211 x = ay * bz - az * by;
212 y = az * bx - ax * bz;
213 z = ax * by - ay * bx;
214}
215
217 if (!std::isfinite(hit.worldX) || !std::isfinite(hit.worldY) || !std::isfinite(hit.worldZ) ||
218 !std::isfinite(hit.normalX) || !std::isfinite(hit.normalY) || !std::isfinite(hit.normalZ)) {
219 return SurfaceFrameStatus::Invalid;
220 }
221 const float normalLength = length3(hit.normalX, hit.normalY, hit.normalZ);
222 if (normalLength <= 1e-5f) return SurfaceFrameStatus::Invalid;
223 hit.normalX /= normalLength;
224 hit.normalY /= normalLength;
225 hit.normalZ /= normalLength;
226
227 const float tangentDot = hit.tangentX * hit.normalX + hit.tangentY * hit.normalY + hit.tangentZ * hit.normalZ;
228 hit.tangentX -= tangentDot * hit.normalX;
229 hit.tangentY -= tangentDot * hit.normalY;
230 hit.tangentZ -= tangentDot * hit.normalZ;
231 float tangentLength = length3(hit.tangentX, hit.tangentY, hit.tangentZ);
232 if (tangentLength <= 1e-5f) {
233 if (std::fabs(hit.normalY) < 0.9f) {
234 hit.tangentX = 0.f;
235 hit.tangentY = 1.f;
236 hit.tangentZ = 0.f;
237 } else {
238 hit.tangentX = 1.f;
239 hit.tangentY = 0.f;
240 hit.tangentZ = 0.f;
241 }
242 const float fallbackDot = hit.tangentX * hit.normalX + hit.tangentY * hit.normalY + hit.tangentZ * hit.normalZ;
243 hit.tangentX -= fallbackDot * hit.normalX;
244 hit.tangentY -= fallbackDot * hit.normalY;
245 hit.tangentZ -= fallbackDot * hit.normalZ;
246 tangentLength = length3(hit.tangentX, hit.tangentY, hit.tangentZ);
247 }
248 hit.tangentX /= tangentLength;
249 hit.tangentY /= tangentLength;
250 hit.tangentZ /= tangentLength;
251 cross3(hit.tangentX, hit.tangentY, hit.tangentZ, hit.normalX, hit.normalY, hit.normalZ, hit.bitangentX,
252 hit.bitangentY, hit.bitangentZ);
253 return SurfaceFrameStatus::Normalized;
254}
255
256} // namespace placement_detail
257} // namespace eve::building
double z
double y
double x
const std::string & s
glm::vec4 p[6]
Stable, structured diagnostics shared by engine modules.
Pure cell/world projection helpers for GridConfig.
std::array< double, 10 > q
std::int32_t c
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
glm::dvec3 dx
bool hit
float d
float t
float axisY
Definition RockMesh.cpp:22
float axisX
Definition RockMesh.cpp:22
float dy
Definition Shadow.cpp:151
bool placed
std::array< double, 2 > vertices[3]
glm::vec3 point
格子型建筑放置世界(脚本可直接操作)。
bool containsFreeFootprintPoint(const FreeFootprint &footprint, float x, float y)
bool isPointInPolygon(const FreeFootprint &polygon, float x, float y)
SurfaceFrameStatus normalizeSurfaceFrame(PlacementSystem::PlacementHit &hit)
FreeFootprint definitionFreeFootprint(const BuildingDefinition &definition, const PlacementWorld &world, float x, float y, float rotationDeg)
std::vector< FreeFootprint::Point > worldVertices(const FreeFootprint &footprint)
Transform footprint vertices. @cost Copies vertex count per overlap query.
bool hasFreeEdgeOverlap(const PlacementWorld &world, const BuildingDefinition &freeDefinition, const FreeFootprint &footprint, const BuildingDefinition &edgeDefinition, EdgeAddress edge)
float length3(float x, float y, float z)
void applyDefinitionFreeFootprint(PlacedBuilding &placed, const BuildingDefinition &definition, const PlacementWorld &world)
FreeFootprint placedFreeFootprint(const PlacedBuilding &placed)
int cardinalQuarter(float rotationDeg)
void cross3(float ax, float ay, float az, float bx, float by, float bz, float &x, float &y, float &z)
bool hasFreeFootprintOverlap(const FreeFootprint &a, const FreeFootprint &b)
建筑放置模块入口:定义 / 放置世界 / 鬼影 / 变更事件的脚本绑定点。 设计文档:docs/dev/建筑放置系统设计.md
Definition Building.cpp:10
建筑模板(进程级注册表中的定义)。
std::string channel
占地格子尺寸。
std::vector< std::string > blockedByEdgeChannels
Edge channels that block this free-domain footprint at the same floor.
float edgeThicknessCells
Solid edge thickness in cells; zero disables free/edge collision.
Canonical address of one grid edge.
已放置的建筑实例。
One sampled point and orthonormal frame on a placement surface.