载入中...
搜索中...
未找到
ScalarFieldGeometry.cpp
浏览该文件的文档.
2
4
5#include <algorithm>
6#include <cmath>
7
8namespace eve::procgen {
9Result<void> ScalarField3D::carveEllipsoid(float cx, float cy, float cz, float rx, float ry, float rz) {
10 if (samples_.empty() || !std::isfinite(cx) || !std::isfinite(cy) || !std::isfinite(cz) || !std::isfinite(rx) ||
11 !std::isfinite(ry) || !std::isfinite(rz) || std::abs(cx) > 1.f || std::abs(cy) > 1.f || std::abs(cz) > 1.f ||
12 rx < 0.0001f || ry < 0.0001f || rz < 0.0001f || rx > 2.f || ry > 2.f || rz > 2.f)
15 "ellipsoid requires an allocated field, normalized center and radii in [0.0001,2]", {},
16 {}, "procgen.field"));
17 const float scale = std::min({rx, ry, rz});
18 for (int z = 0; z < nz_; ++z)
19 for (int y = 0; y < ny_; ++y)
20 for (int x = 0; x < nx_; ++x) {
21 const float px = (float(x) / float(nx_ - 1) * 2.f - 1.f - cx) / rx;
22 const float py = (float(y) / float(ny_ - 1) * 2.f - 1.f - cy) / ry;
23 const float pz = (float(z) / float(nz_ - 1) * 2.f - 1.f - cz) / rz;
24 auto& sample = samples_[size_t(x) + size_t(nx_) * (size_t(y) + size_t(ny_) * size_t(z))];
25 sample = std::min(sample, (std::sqrt(px * px + py * py + pz * pz) - 1.f) * scale);
26 }
27 return Result<void>::success();
28}
29
31 if (samples_.empty() || !std::isfinite(isolevel))
33 "isosurface requires an allocated field and a finite level",
34 {}, {}, "procgen.field"));
36 std::string error;
37 if (!marchingCubes(samples_.data(), nx_, ny_, nz_, isolevel, mesh, &error) || mesh.empty()) {
38 if (error.empty()) error = "scalar field has no isosurface at the requested level";
41 }
42 return Result<MeshBuild>::success(std::move(mesh));
43}
44} // namespace eve::procgen
double z
double y
double x
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
std::vector< Colorf > px
std::array< float, 3 > scale
OceanMeshSettings mesh
std::string error
Definition Package.cpp:174
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
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Definition MeshBuild.h:20
Result< MeshBuild > extractMesh(float isolevel) const
Extract an independent isosurface in [-0.5,0.5]; empty output returns a diagnostic....
Result< float > sample(int x, int y, int z) const
Return one sample or an out-of-bounds diagnostic.
Result< void > carveEllipsoid(float x, float y, float z, float rx, float ry, float rz)
Carve an ellipsoid from rock in normalized [-1,1] coordinates; bounded positive radii required....
bool marchingCubes(const float *density, int nx, int ny, int nz, float isolevel, MeshBuild &out, std::string *error)
Classic Marching Cubes (Lorensen & Cline) over a regular scalar volume. Density >= isolevel is treate...