载入中...
搜索中...
未找到
OceanSpectrum.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5#include <numbers>
6
7namespace eve::fluids {
8namespace {
9constexpr double tau = 2.0 * std::numbers::pi;
10double uniform(std::uint64_t& state) {
11 state += 0x9e3779b97f4a7c15ULL;
12 auto z = state;
13 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
14 z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
15 z ^= z >> 31;
16 return (static_cast<double>(z >> 11) + 0.5) / 9007199254740992.0;
17}
18bool finite(glm::dvec2 v) { return std::isfinite(v.x) && std::isfinite(v.y); }
21 Diagnostic::error(DiagnosticCode::InvalidArgument, "Invalid ocean spectrum settings", "fluids.ocean.spectrum"));
22}
23} // namespace
24
26 const auto& s = settings;
27 if (!std::isfinite(s.significantWaveHeight) || s.significantWaveHeight < 0.0 || s.significantWaveHeight > 30.0 ||
28 !std::isfinite(s.peakPeriod) || s.peakPeriod < 1.0 || s.peakPeriod > 30.0 ||
29 !std::isfinite(s.peakEnhancement) || s.peakEnhancement < 1.0 || s.peakEnhancement > 10.0 ||
30 !std::isfinite(s.gravity) || s.gravity <= 0.0 || s.gravity > 100.0 || !std::isfinite(s.seaLevel) ||
31 std::abs(s.seaLevel) > 1e6 || !std::isfinite(s.choppiness) || s.choppiness < 0.0 || s.choppiness > 1.0 ||
32 !finite(s.windDirection) || glm::length(s.windDirection) < 1e-8 || glm::length(s.windDirection) > 1e6 ||
33 !finite(s.current) || glm::length(s.current) > 100.0 || s.bands.empty() || s.bands.size() > 4)
34 return invalid();
35 double previous = 0.0;
36 for (const auto& b : s.bands) {
37 if (b.resolution < 8 || b.resolution > 512 || (b.resolution & (b.resolution - 1)) != 0 ||
38 !std::isfinite(b.length) || b.length < 1.0 || b.length > 1e6 || !std::isfinite(b.minimumWavelength) ||
39 !std::isfinite(b.maximumWavelength) || b.minimumWavelength < 2.0 * b.length / b.resolution ||
40 b.maximumWavelength > b.length || b.minimumWavelength >= b.maximumWavelength ||
41 b.minimumWavelength < previous)
42 return invalid();
43 previous = b.maximumWavelength;
44 }
45 auto data = std::make_shared<OceanSpectrumData>();
46 data->settings = s;
47 const auto wind = glm::normalize(s.windDirection);
48 std::uint64_t random = s.seed;
49 double energy = 0.0;
50 const double peak = tau / s.peakPeriod;
51 for (const auto& b : s.bands) {
52 OceanSpectrumData::Band band{b, {}};
53 band.coefficients.resize(b.resolution * b.resolution);
54 const double dk = tau / b.length;
55 for (unsigned z = 0; z < b.resolution; ++z) {
56 for (unsigned x = 0; x < b.resolution; ++x) {
57 const int ix =
58 x < b.resolution / 2 ? static_cast<int>(x) : static_cast<int>(x) - static_cast<int>(b.resolution);
59 const int iz =
60 z < b.resolution / 2 ? static_cast<int>(z) : static_cast<int>(z) - static_cast<int>(b.resolution);
61 // Exclude Nyquist axes so odd displacement multipliers remain Hermitian.
62 if ((ix == 0 && iz == 0) || x == b.resolution / 2 || z == b.resolution / 2) continue;
63 const glm::dvec2 kvec{ix * dk, iz * dk};
64 const double k = glm::length(kvec), wavelength = tau / k;
65 if (wavelength < b.minimumWavelength || wavelength >= b.maximumWavelength) continue;
66 const double omega = std::sqrt(s.gravity * k);
67 const double sigma = omega <= peak ? 0.07 : 0.09;
68 const double r = std::exp(-0.5 * std::pow((omega - peak) / (sigma * peak), 2.0));
69 const double direction = std::pow(std::max(glm::dot(kvec / k, wind), 0.0), 4.0);
70 const double density = std::pow(omega, -5.0) * std::exp(-1.25 * std::pow(peak / omega, 4.0)) *
71 std::pow(s.peakEnhancement, r) * direction * (s.gravity / (2.0 * omega)) / k;
72 const double magnitude = std::sqrt(density * dk * dk * -std::log(uniform(random)));
73 const auto coefficient = std::polar(magnitude, tau * uniform(random));
74 band.coefficients[z * b.resolution + x] = coefficient;
75 energy += 2.0 * std::norm(coefficient);
76 }
77 }
78 data->bands.push_back(std::move(band));
79 }
80 if (s.significantWaveHeight > 0.0 && energy < 1e-30) return invalid();
81 const double variance = s.significantWaveHeight * s.significantWaveHeight / 16.0;
82 const double scale = energy > 0.0 ? std::sqrt(variance / energy) : 0.0;
83 for (auto& band : data->bands)
84 for (auto& coefficient : band.coefficients) coefficient *= scale;
85 data->variance = variance;
86 return Result<OceanSpectrum>::success(OceanSpectrum(std::move(data)));
87}
88
90double OceanSpectrum::variance() const { return data_->variance; }
91} // namespace eve::fluids
double z
double x
const std::string & s
float density
double r
float v
std::array< float, 3 > scale
MeleePoint3 b
Definition MeleeHit.cpp:41
std::shared_ptr< const OceanSprayFrame > previous
bool finite
RoadLaneDirection direction
Battle::Random random
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< OceanSpectrum > build() const
Validate band coverage and build seeded JONSWAP coefficients atomically.
Owns a shared immutable spectrum lease and timestamp; safe after the original spectrum is destroyed.
Immutable initial spectrum; data is shared only with owning frame builders.
EVENGINE_API_DOMAINS OceanSpectrumFrameBuilder frameBuilder(double time) const
Prepare a cheap owning builder; time validation and FFT work occur at build().
EVENGINE_API_DOMAINS double variance() const
Expected ensemble surface variance, normalized to significantWaveHeight squared / 16.
Status invalid(const char *message)
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12
std::vector< std::complex< double > > coefficients