载入中...
搜索中...
未找到
OceanSpectrumFrame.cpp
浏览该文件的文档.
4
5#include <array>
6#include <cmath>
7#include <numbers>
8
9namespace eve::fluids {
10namespace {
11using Complex = std::complex<double>;
12constexpr double tau = 2.0 * std::numbers::pi;
13
14bool validPoint(glm::dvec2 q) {
15 return std::isfinite(q.x) && std::isfinite(q.y) && std::abs(q.x) <= 1e9 && std::abs(q.y) <= 1e9;
16}
17template <class T>
19 return Result<T>::failure(
20 Diagnostic::error(DiagnosticCode::InvalidArgument, "Invalid ocean frame query", "fluids.ocean.frame"));
21}
22struct Evaluation {
23 glm::dvec3 displacement{0.0};
24 glm::dvec3 velocity{0.0};
25 glm::dvec3 dx{0.0};
26 glm::dvec3 dz{0.0};
27};
28Evaluation evaluate(std::span<const OceanSpectrumGrid> grids, glm::dvec2 q) {
29 Evaluation out;
30 for (const auto& g : grids) {
31 const double scale = g.resolution / g.length;
32 glm::dvec2 uv = q / g.length;
33 uv = (uv - glm::floor(uv)) * static_cast<double>(g.resolution);
34 const auto baseX = static_cast<unsigned>(uv.x), baseZ = static_cast<unsigned>(uv.y);
35 const double fx = uv.x - baseX, fz = uv.y - baseZ;
38 for (unsigned j = 0; j < 4; ++j) {
39 for (unsigned i = 0; i < 4; ++i) {
40 const auto x = (baseX + g.resolution + i - 1) % g.resolution;
41 const auto z = (baseZ + g.resolution + j - 1) % g.resolution;
42 const auto index = z * g.resolution + x;
43 out.displacement += g.displacement[index] * (wx[i] * wz[j]);
44 out.velocity += g.velocity[index] * (wx[i] * wz[j]);
45 out.dx += g.displacement[index] * (dx[i] * wz[j] * scale);
46 out.dz += g.displacement[index] * (wx[i] * dz[j] * scale);
47 }
48 }
49 }
50 return out;
51}
52} // namespace
53
55 if (!std::isfinite(time_) || std::abs(time_) > 1e9) return invalid<OceanSpectrumFrame>();
57 out.time_ = time_;
58 out.seaLevel_ = data_->settings.seaLevel;
59 out.current_ = data_->settings.current;
60 for (const auto& band : data_->bands) {
61 const auto n = band.settings.resolution;
62 std::array<std::vector<Complex>, 6> fields;
63 for (auto& field : fields) field.resize(n * n);
64 for (unsigned z = 0; z < n; ++z) {
65 for (unsigned x = 0; x < n; ++x) {
66 const int ix = x < n / 2 ? static_cast<int>(x) : static_cast<int>(x) - static_cast<int>(n);
67 const int iz = z < n / 2 ? static_cast<int>(z) : static_cast<int>(z) - static_cast<int>(n);
68 if (ix == 0 && iz == 0) continue;
69 const glm::dvec2 kvec = glm::dvec2(ix, iz) * (tau / band.settings.length);
70 const double k = glm::length(kvec), omega = std::sqrt(data_->settings.gravity * k);
71 const auto index = z * n + x;
72 const auto opposite = ((n - z) % n) * n + (n - x) % n;
73 const auto forward = band.coefficients[index] * std::polar(1.0, std::remainder(-omega * time_, tau));
74 const auto backward =
75 std::conj(band.coefficients[opposite]) * std::polar(1.0, std::remainder(omega * time_, tau));
76 const auto advection = std::polar(1.0, std::remainder(-glm::dot(kvec, out.current_) * time_, tau));
77 const auto height = (forward + backward) * advection;
78 const auto velocity = Complex(0.0, omega) * (backward - forward) * advection;
79 const auto dx = Complex(0.0, kvec.x / k * data_->settings.choppiness);
80 const auto dz = Complex(0.0, kvec.y / k * data_->settings.choppiness);
81 fields[0][index] = dx * height;
82 fields[1][index] = height;
83 fields[2][index] = dz * height;
84 fields[3][index] = dx * velocity;
85 fields[4][index] = velocity;
86 fields[5][index] = dz * velocity;
87 }
88 }
91 grid.resolution = n;
92 grid.length = band.settings.length;
93 grid.displacement.resize(n * n);
94 grid.velocity.resize(n * n);
95 for (unsigned i = 0; i < n * n; ++i) {
96 grid.displacement[i] = {fields[0][i].real(), fields[1][i].real(), fields[2][i].real()};
97 grid.velocity[i] = {fields[3][i].real(), fields[4][i].real(), fields[5][i].real()};
98 }
99 out.grids_.push_back(std::move(grid));
100 }
101 return Result<OceanSpectrumFrame>::success(std::move(out));
102}
103
106 return Result<glm::dvec3>::success(glm::dvec3(parameterXZ.x, seaLevel_, parameterXZ.y) +
107 evaluate(grids_, parameterXZ).displacement);
108}
109
112 const auto e = evaluate(grids_, parameterXZ);
114 geometry.position = glm::dvec3(parameterXZ.x, seaLevel_, parameterXZ.y) + e.displacement;
115 geometry.tangentX += e.dx;
116 geometry.tangentZ += e.dz;
118}
119
122 auto q = worldXZ;
123 for (int iteration = 0; iteration < 32; ++iteration) {
124 const auto e = evaluate(grids_, q);
125 const auto residual = q + glm::dvec2(e.displacement.x, e.displacement.z) - worldXZ;
126 const double a = 1.0 + e.dx.x, b = e.dz.x, c = e.dx.z, d = 1.0 + e.dz.z;
127 const double det = a * d - b * c;
128 if (det <= 1e-6 || !std::isfinite(det)) break;
129 if (glm::length(residual) < 1e-6) {
131 sample.height = seaLevel_ + e.displacement.y;
132 sample.normal = glm::normalize(glm::cross(glm::dvec3(b, e.dz.y, d), glm::dvec3(a, e.dx.y, c)));
133 sample.velocity = e.velocity + glm::dvec3(current_.x, 0.0, current_.y);
134 sample.time = time_;
135 sample.parameterPosition = q;
136 sample.horizontalJacobian = det;
138 }
139 auto step = glm::dvec2(d * residual.x - b * residual.y, a * residual.y - c * residual.x) / det;
140 const double length = glm::length(step);
141 if (length > 10.0) step *= 10.0 / length;
142 q -= step;
143 if (!validPoint(q)) break;
144 }
146 Diagnostic::error(DiagnosticCode::Unsupported, "Ocean surface folds or world-space inversion did not converge",
147 "fluids.ocean.sample"));
148}
149} // namespace eve::fluids
double z
double x
float uv
std::vector< std::string > fields
Definition ECS.cpp:42
tensor::Graph g
Definition GpuGraph.cpp:7
glm::vec3 n
Definition Grass.cpp:63
std::array< double, 10 > q
std::int32_t c
std::uint32_t height
float length
std::array< float, 3 > scale
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
glm::dvec3 dx
glm::dvec3 displacement
glm::dvec3 velocity
glm::dvec3 dz
float d
std::string geometry
ScriptStructure out
float step
Definition TreeMesh.cpp:451
uint32_t index
V forward
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
EVENGINE_API_DOMAINS Result< OceanSpectrumFrame > build() const
Evaluate six fields through inverse FFT and publish an owning frame.
Immutable spatial projection at one simulation timestamp, independent of its spectrum owner....
EVENGINE_API_DOMAINS Result< OceanSurfaceSample > sample(glm::dvec2 worldXZ) const
Invert horizontal displacement at world XZ and return the matching water sample.
EVENGINE_API_DOMAINS Result< glm::dvec3 > position(glm::dvec2 parameterXZ) const
Sample displaced geometry using periodic C1-continuous Catmull-Rom interpolation.
EVENGINE_API_DOMAINS Result< OceanSurfaceGeometry > geometry(glm::dvec2 parameterXZ) const
Evaluate position and exact interpolation derivatives without world-space inversion.
const Value & field(const Value &value, const char *name)
Borrow a required child of value; throws on a missing field.
std::array< double, 4 > derivatives(double t)
std::array< double, 4 > weights(double t)
std::complex< double > Complex
void inverse2D(std::vector< Complex > &a, unsigned n)
Transform a validated N by N grid in place using row/column scratch. @cost O(N squared log N) arithme...
Status invalid(const char *message)
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12
std::vector< OnnxNamedTensor > evaluate(const ModelData &, std::span< const OnnxNamedTensor >, const std::vector< std::string > &, OnnxCompute *, OnnxRunOptions options)
Evaluate.
Owning periodic displacement and particle velocity arrays; index = z * resolution + x.
Owning parametric surface geometry; derivatives are with respect to undisplaced world X and Z.
Owning sample of the displaced surface at a requested world XZ and time.