载入中...
搜索中...
未找到
TerrainLegacyHydraulic.cpp
浏览该文件的文档.
3
4#include <array>
5
6namespace eve::procgen {
9 using namespace raster_detail;
10 const auto sameShape = [&](const Heightmap& map) {
11 return validRaster(map) && map.getWidth() == heights.getWidth() && map.getHeight() == heights.getHeight();
12 };
13 const auto unit = [](float value) { return value >= 0 && value <= 1; };
14 if (&heights == &sediment || !validRaster(heights) || !sameShape(sediment) || !sameShape(hardness) ||
15 !sameShape(rain) || settings.iterations < 0 || settings.rainFrequency <= 0 ||
16 !std::isfinite(settings.sedimentDissolveRate) || !unit(settings.sedimentDissolveRate) ||
17 !std::all_of(heights.data().begin(), heights.data().end(), unit) ||
18 !std::all_of(hardness.data().begin(), hardness.data().end(), unit) ||
19 std::any_of(rain.data().begin(), rain.data().end(), [](float value) { return value < 0; }))
20 return invalid("terrain.legacyHydraulic: distinct matching rasters, normalized heights/hardness, "
21 "nonnegative rain/iterations, positive rain frequency and dissolve rate in [0,1] required");
22 if (settings.iterations == 0) return Result<int>::success(0);
23
24 constexpr double time = 0.2;
25 const int width = heights.getWidth(), depth = heights.getHeight();
26 const size_t count = heights.data().size();
27 auto terrain = heights.data(), sedimentResult = sediment.data();
28 // Inputs remain unchanged until both candidate outputs are published below, including aliases.
29 const auto& hardnessInput = hardness.data();
30 const auto& rainInput = rain.data();
31 std::vector<double> water(count, 0), sedimentDiff(count, 0);
32 std::vector<std::array<double, 4>> outflow(count);
33 const auto index = [width](int x, int z) { return size_t(z) * size_t(width) + size_t(x); };
34
35 for (int iteration = 0; iteration < settings.iterations; ++iteration) {
36 if (iteration % settings.rainFrequency == 0)
37 for (size_t i = 0; i < count; ++i) {
38 water[i] += rainInput[i];
39 if (!std::isfinite(water[i])) return invalid("terrain.legacyHydraulic: water exceeds finite range");
40 }
41
42 for (int z = 0; z < depth; ++z)
43 for (int x = 0; x < width; ++x) {
44 const size_t i = index(x, z);
45 const std::array<size_t, 4> neighbor = {
46 index(std::max(x - 1, 0), z), index(std::min(x + 1, width - 1), z),
47 index(x, std::max(z - 1, 0)), index(x, std::min(z + 1, depth - 1))};
48 double sum = 0;
49 for (int direction = 0; direction < 4; ++direction) {
50 const double difference = water[i] + terrain[i] - water[neighbor[direction]] - terrain[neighbor[direction]];
51 outflow[i][direction] = std::max(0.0, outflow[i][direction] + difference);
52 sum += outflow[i][direction];
53 }
54 if (sum > 0) {
55 const double scale = std::clamp(water[i] / (sum * time), 0.0, 1.0);
56 for (double& flow : outflow[i]) flow *= scale;
57 } else {
58 outflow[i].fill(0);
59 }
60 }
61
62 auto nextWater = water;
63 for (int z = 0; z < depth; ++z)
64 for (int x = 0; x < width; ++x) {
65 const size_t i = index(x, z);
66 const double flowOut = outflow[i][0] + outflow[i][1] + outflow[i][2] + outflow[i][3];
67 double flowIn = 0;
68 if (x > 0) flowIn += outflow[index(x - 1, z)][1];
69 if (x + 1 < width) flowIn += outflow[index(x + 1, z)][0];
70 if (z > 0) flowIn += outflow[index(x, z - 1)][3];
71 if (z + 1 < depth) flowIn += outflow[index(x, z + 1)][2];
72 nextWater[i] = std::max(0.0, water[i] + (flowIn - flowOut) * time);
73 if (!std::isfinite(nextWater[i])) return invalid("terrain.legacyHydraulic: water exceeds finite range");
74 }
75 water.swap(nextWater);
76
77 for (int z = 0; z < depth; ++z)
78 for (int x = 0; x < width; ++x) {
79 const size_t i = index(x, z);
80 double totalDrop = 0;
81 for (int nz = std::max(z - 1, 0); nz <= std::min(z + 1, depth - 1); ++nz)
82 for (int nx = std::max(x - 1, 0); nx <= std::min(x + 1, width - 1); ++nx)
83 totalDrop += std::max(0.0, double(terrain[i]) - terrain[index(nx, nz)]);
84 if (totalDrop == 0) continue;
85 const double moved = water[i] * totalDrop * settings.sedimentDissolveRate * (1.0 - hardnessInput[i]);
86 sedimentDiff[i] -= moved;
87 for (int nz = std::max(z - 1, 0); nz <= std::min(z + 1, depth - 1); ++nz)
88 for (int nx = std::max(x - 1, 0); nx <= std::min(x + 1, width - 1); ++nx) {
89 const size_t n = index(nx, nz);
90 const double drop = double(terrain[i]) - terrain[n];
91 if (drop > 0) sedimentDiff[n] += moved * drop / totalDrop;
92 }
93 }
94
95 for (size_t i = 0; i < count; ++i) {
96 const double nextSediment = double(sedimentResult[i]) + sedimentDiff[i];
97 if (!isRepresentable(nextSediment)) return invalid("terrain.legacyHydraulic: sediment exceeds finite float range");
98 sedimentResult[i] = float(nextSediment);
99 terrain[i] = float(std::clamp(double(terrain[i]) + sedimentDiff[i], 0.0, 1.0));
100 water[i] = std::clamp(water[i] - 1.0 / settings.rainFrequency, 0.0, 1.0);
101 sedimentDiff[i] = 0;
102 }
103 }
104 int changed = 0;
105 for (size_t i = 0; i < count; ++i)
106 changed += terrain[i] != heights.data()[i] || sedimentResult[i] != sediment.data()[i];
107 heights.data().swap(terrain);
108 sediment.data().swap(sedimentResult);
110}
111} // namespace eve::procgen
double z
double x
std::string terrain
bool changed
glm::vec3 n
Definition Grass.cpp:63
std::uint32_t width
std::array< float, 3 > scale
OceanWakeSettings settings
ssq::Object water
Object::Ptr value
RoadLaneDirection direction
int count
TacticalUnit * unit
Heightmap sediment
std::uint32_t depth
uint32_t index
int neighbor
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
bool validRaster(const Heightmap &map)
Valid raster.
Result< int > invalid(std::string message)
Invalid.
bool isRepresentable(double value)
True when representable.
EVENGINE_API_DOMAINS Result< int > applyTerrainLegacyHydraulic(Heightmap &heights, Heightmap &sediment, const Heightmap &hardness, const Heightmap &rain, const TerrainLegacyHydraulicSettings &settings)
Apply Pcg HeightMap.ErodeHydraulic's rain, four-way water and sediment loop atomically.
Controls Pcg HeightMap.ErodeHydraulic's legacy CPU erosion loop.