载入中...
搜索中...
未找到
TerrainMeshStamp.cpp
浏览该文件的文档.
1// eve-hot-path
3
4#include "procgen/MeshBuild.h"
7
8#include <algorithm>
9#include <cmath>
10#include <limits>
11
12namespace eve::procgen {
13using namespace raster_detail;
14
16 const auto& positions = mesh.positions();
17 const auto& indices = mesh.indices();
18 constexpr std::size_t maximumElements = 3U * 1024U * 1024U;
19 if (positions.empty() || positions.size() % 3 != 0 || positions.size() > maximumElements || indices.empty() ||
20 indices.size() % 3 != 0 || indices.size() > maximumElements ||
21 !std::all_of(positions.begin(), positions.end(), [](float v) { return std::isfinite(v); }) ||
22 !std::all_of(indices.begin(), indices.end(), [&](std::uint32_t i) { return i < positions.size() / 3; }))
23 return invalid("terrain.meshStamp: finite triangle positions and in-range indices required (1M limit)");
24 auto candidatePositions = positions;
25 auto candidateIndices = indices;
26 positions_.swap(candidatePositions);
27 indices_.swap(candidateIndices);
28 return Result<int>::success(static_cast<int>(indices_.size() / 3));
29}
30
32 double width, double depth, double feather) const {
33 if (&heights == &coverage || !validRaster(heights) || !validRaster(coverage) || heights.getWidth() < 2 ||
34 heights.getHeight() < 2 || heights.getWidth() != coverage.getWidth() ||
35 heights.getHeight() != coverage.getHeight() || heights.data().size() > 4U * 1024U * 1024U || indices_.empty() ||
36 !std::isfinite(minX) || !std::isfinite(minZ) || !std::isfinite(width) || !std::isfinite(depth) ||
37 !std::isfinite(feather) || width <= 0 || depth <= 0 || feather < 0 || !std::isfinite(minX + width) ||
38 !std::isfinite(minZ + depth))
39 return invalid("terrain.meshStamp: source and distinct matching finite rasters/bake bounds required");
40 const int columns = heights.getWidth(), rows = heights.getHeight();
41 const double dx = width / (columns - 1), dz = depth / (rows - 1);
42 if (dx <= 0 || dz <= 0 || minX + dx == minX || minZ + dz == minZ)
43 return invalid("terrain.meshStamp: grid spacing is not representable");
44 std::vector<float> candidate(heights.data().size(), 0.f), mask(candidate.size(), 0.f);
45 std::uint64_t work = 0;
46 int covered = 0;
47 struct Projection {
48 std::size_t a, b, c;
49 double determinant;
50 int x0, x1, z0, z1;
51 };
52 std::vector<Projection> projections;
53 projections.reserve(indices_.size() / 3);
54 for (std::size_t triangle = 0; triangle < indices_.size(); triangle += 3) {
55 const auto a = static_cast<std::size_t>(indices_[triangle]) * 3;
56 const auto b = static_cast<std::size_t>(indices_[triangle + 1]) * 3;
57 const auto c = static_cast<std::size_t>(indices_[triangle + 2]) * 3;
58 const double ax = positions_[a], az = positions_[a + 2];
59 const double bx = positions_[b], bz = positions_[b + 2];
60 const double cx = positions_[c], cz = positions_[c + 2];
61 const double determinant = (bz - cz) * (ax - cx) + (cx - bx) * (az - cz);
62 if (determinant == 0) continue;
63 const double left = std::max(minX, std::min({ax, bx, cx}));
64 const double right = std::min(minX + width, std::max({ax, bx, cx}));
65 const double bottom = std::max(minZ, std::min({az, bz, cz}));
66 const double top = std::min(minZ + depth, std::max({az, bz, cz}));
67 if (left > right || bottom > top) continue;
68 // Expand by one sample to avoid losing a shared edge through rounding.
69 const int x0 = static_cast<int>(std::clamp(std::floor((left - minX) / dx) - 1, 0.0, double(columns - 1)));
70 const int x1 = static_cast<int>(std::clamp(std::ceil((right - minX) / dx) + 1, 0.0, double(columns - 1)));
71 const int z0 = static_cast<int>(std::clamp(std::floor((bottom - minZ) / dz) - 1, 0.0, double(rows - 1)));
72 const int z1 = static_cast<int>(std::clamp(std::ceil((top - minZ) / dz) + 1, 0.0, double(rows - 1)));
73 work += std::uint64_t(x1 - x0 + 1) * std::uint64_t(z1 - z0 + 1);
74 if (work > 128U * 1024U * 1024U)
75 return invalid("terrain.meshStamp: projected triangle work exceeds 128M sample budget");
76 projections.push_back({a, b, c, determinant, x0, x1, z0, z1});
77 }
78 for (const auto& projection : projections) {
79 const auto [a, b, c, determinant, x0, x1, z0, z1] = projection;
80 const double ax = positions_[a], az = positions_[a + 2], bx = positions_[b], bz = positions_[b + 2],
81 cx = positions_[c], cz = positions_[c + 2];
82 for (int z = z0; z <= z1; ++z) {
83 for (int x = x0; x <= x1; ++x) {
84 const double px = minX + x * dx, pz = minZ + z * dz;
85 double wa = ((bz - cz) * (px - cx) + (cx - bx) * (pz - cz)) / determinant;
86 double wb = ((cz - az) * (px - cx) + (ax - cx) * (pz - cz)) / determinant;
87 double wc = 1.0 - wa - wb;
88 constexpr double tolerance = 1e-10;
89 if (wa < -tolerance || wb < -tolerance || wc < -tolerance) continue;
90 wa = std::max(0.0, wa);
91 wb = std::max(0.0, wb);
92 wc = std::max(0.0, wc);
93 const double y =
94 (wa * positions_[a + 1] + wb * positions_[b + 1] + wc * positions_[c + 1]) / (wa + wb + wc);
95 if (!isRepresentable(y)) return invalid("terrain.meshStamp: nonfinite projected height");
96 const auto index = static_cast<std::size_t>(z) * columns + x;
97 if (mask[index] == 0 || y > candidate[index]) candidate[index] = static_cast<float>(y);
98 if (mask[index] == 0) ++covered;
99 mask[index] = 1;
100 }
101 }
102 }
103 if (feather > 0) {
104 // Distance to uncovered samples or the rectangle edge in the bake coordinate system.
105 std::vector<double> distances(mask.size(), 0.0);
106 for (int z = 0; z < rows; ++z)
107 for (int x = 0; x < columns; ++x) {
108 const auto i = static_cast<std::size_t>(z) * columns + x;
109 if (mask[i] == 0) continue;
110 distances[i] = std::min({x * dx, (columns - 1 - x) * dx, z * dz, (rows - 1 - z) * dz, feather});
111 if (x > 0) distances[i] = std::min(distances[i], distances[i - 1] + dx);
112 if (z > 0) distances[i] = std::min(distances[i], distances[i - columns] + dz);
113 }
114 for (int z = rows - 1; z >= 0; --z)
115 for (int x = columns - 1; x >= 0; --x) {
116 const auto i = static_cast<std::size_t>(z) * columns + x;
117 if (x + 1 < columns) distances[i] = std::min(distances[i], distances[i + 1] + dx);
118 if (z + 1 < rows) distances[i] = std::min(distances[i], distances[i + columns] + dz);
119 const double t = std::clamp(distances[i] / feather, 0.0, 1.0);
120 mask[i] = static_cast<float>(t * t * (3.0 - 2.0 * t));
121 }
122 }
123 heights.data().swap(candidate);
124 coverage.data().swap(mask);
126}
127} // namespace eve::procgen
double z
double y
double x
int mask
float cx
Definition CardTypes.cpp:36
int rows
int triangle
Definition FluidSdf.cpp:554
std::vector< std::uint32_t > indices
std::vector< float > positions
float v
HexVec3 left
HexVec3 right
std::int32_t c
std::vector< Colorf > px
std::uint32_t width
std::string coverage
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
OceanMeshSettings mesh
glm::dvec3 dx
glm::dvec3 dz
float t
int columns
std::uint32_t depth
uint32_t index
int covered
float bottom
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
In-memory terrain heightmap: a dense float grid (row-major, index = y * width + x) materialized from ...
Definition Heightmap.h:21
int getHeight() const
Returns the height.
Definition Heightmap.cpp:17
const std::vector< float > & data() const
Data.
Definition Heightmap.h:48
int getWidth() const
Returns the width.
Definition Heightmap.cpp:16
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Definition MeshBuild.h:20
Result< int > setSource(const MeshBuild &mesh)
Validate and copy a CPU triangle mesh; failure preserves the prior source.
Result< int > bake(Heightmap &heights, Heightmap &coverage, double minX, double minZ, double width, double depth, double feather) const
Bake the highest surface into two preallocated matching rasters, atomically.
SQInteger top
Definition Snapshot.cpp:115
bool validRaster(const Heightmap &map)
Valid raster.
Result< int > invalid(std::string message)
Invalid.
bool isRepresentable(double value)
True when representable.