载入中...
搜索中...
未找到
TerrainRasterInternal.h
浏览该文件的文档.
1#pragma once
2
3#include "common/Result.h"
5
6#include <algorithm>
7#include <cmath>
8#include <limits>
9#include <string>
10#include <vector>
11
12// Shared implementation details for the stamp and mask kernels.
15inline bool validRaster(const Heightmap& map) {
16 return map.getWidth() > 0 && map.getHeight() > 0 &&
17 map.getWidth() <= std::numeric_limits<int>::max() / map.getHeight() &&
18 map.data().size() == size_t(map.getWidth()) * size_t(map.getHeight()) &&
19 map.data().size() <= size_t(std::numeric_limits<int>::max()) &&
21 std::all_of(map.data().begin(), map.data().end(), [](float v) { return std::isfinite(v); });
22}
23
29
31inline bool isRepresentable(double value) {
33 return std::isfinite(value) && std::abs(value) <= std::numeric_limits<float>::max();
34}
35
36// Clamp/Bilinear texture-center sampling. Callers validate finite coordinates and rasters.
38inline double textureSample(const Heightmap& map, double u, double v) {
39 const double x = std::clamp(u * map.getWidth() - 0.5, 0.0, double(map.getWidth() - 1));
40 const double z = std::clamp(v * map.getHeight() - 0.5, 0.0, double(map.getHeight() - 1));
41 const int x0 = int(x), z0 = int(z), x1 = std::min(x0 + 1, map.getWidth() - 1),
42 z1 = std::min(z0 + 1, map.getHeight() - 1);
44 return std::lerp(std::lerp(double(map.height(x0, z0)), double(map.height(x1, z0)), x - x0),
46 std::lerp(double(map.height(x0, z1)), double(map.height(x1, z1)), x - x0), z - z0);
47}
48
49// Double intermediates avoid overflow when interpolating large finite floats.
51inline double sample(const Heightmap& map, double u, double v) {
52 const double x = u * (map.getWidth() - 1), y = v * (map.getHeight() - 1);
53 const int x0 = int(x), y0 = int(y);
54 const int x1 = std::min(x0 + 1, map.getWidth() - 1), y1 = std::min(y0 + 1, map.getHeight() - 1);
55 const double top = std::lerp(double(map.height(x0, y0)), double(map.height(x1, y0)), x - x0);
56 const double bottom = std::lerp(double(map.height(x0, y1)), double(map.height(x1, y1)), x - x0);
58 return std::lerp(top, bottom, y - y0);
59}
60
62inline Result<int> publish(Heightmap& target, std::vector<float> candidate) {
63 int changed = 0;
64 for (size_t i = 0; i < candidate.size(); ++i) changed += candidate[i] != target.data()[i];
65 target.data().swap(candidate);
67 return Result<int>::success(changed);
68}
69
70// Clamp/Bilinear one-row LUT sampling; callers validate shape and finite inputs.
72inline double curveSample(const Heightmap& curve, double value) {
73 const double pixel = std::clamp(value * curve.getWidth() - 0.5, 0.0, double(curve.getWidth() - 1));
74 const int left = int(pixel), right = std::min(left + 1, curve.getWidth() - 1);
76 return std::lerp(double(curve.height(left, 0)), double(curve.height(right, 0)), pixel - left);
77}
78} // namespace eve::procgen::raster_detail
LogicalId target
double value
SQInteger top
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
std::string message
float u
Definition Grass.cpp:233
float v
HexVec3 left
HexVec3 right
Move-only, checked operation results for the common layer.
Heightmap curve
float bottom
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
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
float height(int x, int y) const
Height.
Definition Heightmap.cpp:28
int getWidth() const
Returns the width.
Definition Heightmap.cpp:16
double textureSample(const Heightmap &map, double u, double v)
Texture sample.
double curveSample(const Heightmap &curve, double value)
Curve sample.
double sample(const Heightmap &map, double u, double v)
Sample.
Result< int > publish(Heightmap &target, std::vector< float > candidate)
Publish.
bool validRaster(const Heightmap &map)
Valid raster.
Result< int > invalid(std::string message)
Invalid.
bool isRepresentable(double value)
True when representable.