载入中...
搜索中...
未找到
ScalarField3D.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5
6namespace eve::procgen {
7namespace {
8Result<void> invalidField() {
10 "scalar field requires finite samples and axis sizes in [2,256]", {},
11 {}, "procgen.field"));
12}
13bool validSize(int x, int y, int z) { return x >= 2 && y >= 2 && z >= 2 && x <= 256 && y <= 256 && z <= 256; }
14} // namespace
15
17 if (!validSize(x, y, z) || !std::isfinite(value)) return invalidField();
18 std::vector<float> candidate(size_t(x) * size_t(y) * size_t(z), value);
19 samples_ = std::move(candidate);
20 nx_ = x;
21 ny_ = y;
22 nz_ = z;
23 return Result<void>::success();
24}
25
26Result<void> ScalarField3D::assign(int x, int y, int z, std::span<const float> values) {
27 if (!validSize(x, y, z) || values.size() != size_t(x) * size_t(y) * size_t(z) ||
28 !std::all_of(values.begin(), values.end(), [](float v) { return std::isfinite(v); }))
29 return invalidField();
30 std::vector<float> candidate(values.begin(), values.end());
31 samples_ = std::move(candidate);
32 nx_ = x;
33 ny_ = y;
34 nz_ = z;
35 return Result<void>::success();
36}
37
38bool ScalarField3D::contains(int x, int y, int z) const noexcept {
39 return x >= 0 && y >= 0 && z >= 0 && x < nx_ && y < ny_ && z < nz_;
40}
41
42Result<float> ScalarField3D::sample(int x, int y, int z) const {
43 if (!contains(x, y, z))
45 DiagnosticCode::InvalidArgument, "scalar sample is outside the volume", {}, {}, "procgen.field"));
46 return Result<float>::success(samples_[size_t(x) + size_t(nx_) * (size_t(y) + size_t(ny_) * size_t(z))]);
47}
48
50 if (!contains(x, y, z) || !std::isfinite(value)) return invalidField();
51 samples_[size_t(x) + size_t(nx_) * (size_t(y) + size_t(ny_) * size_t(z))] = value;
52 return Result<void>::success();
53}
54
55} // namespace eve::procgen
double z
double y
double x
float v
std::set< std::string > values
Object::Ptr value
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
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
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Result< void > resize(int nx, int ny, int nz, float value)
Atomically allocate a finite volume with axis sizes in [2,256]. @cost Linear in sample count; reuse a...
Result< void > setSample(int x, int y, int z, float value)
Replace one finite sample; invalid input leaves the volume unchanged.
Result< float > sample(int x, int y, int z) const
Return one sample or an out-of-bounds diagnostic.
Result< void > assign(int nx, int ny, int nz, std::span< const float > samples)
Atomically copy finite samples of a correctly sized volume. @cost Linear in sample count,...