src/layers/dunes.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/dunes.h" | ||
| 4 | |||
| 5 | #include "layers/lattice.h" | ||
| 6 | #include "terraingen/geometry/surface.h" | ||
| 7 | #include "terraingen/layers/anchored_plane.h" | ||
| 8 | #include "terraingen/layers/dunes_fields.h" | ||
| 9 | #include "terraingen/layers/source_resolved.h" | ||
| 10 | #include "terraingen/noise/perlin.h" | ||
| 11 | #include "terraingen/random/stream.h" | ||
| 12 | #include "terraingen/util/bits.h" | ||
| 13 | |||
| 14 | #include <algorithm> | ||
| 15 | #include <cmath> | ||
| 16 | #include <limits> | ||
| 17 | #include <memory> | ||
| 18 | #include <optional> | ||
| 19 | #include <stdexcept> | ||
| 20 | #include <string> | ||
| 21 | |||
| 22 | namespace terraingen { | ||
| 23 | |||
| 24 | namespace { | ||
| 25 | |||
| 26 | constexpr double kHalfPi = 0.5 * kPi; | ||
| 27 | /// Wavelengths per lattice unit of the crest wander and of the defects along crests. | ||
| 28 | constexpr double kWanderWavelengths = 4.0; | ||
| 29 | constexpr double kDefectWavelengths = 3.0; | ||
| 30 | /// Phase by which barchanoid crest centers trail their links, in wavelengths, and the length of | ||
| 31 | /// a crest segment from link to link, in wavelengths. | ||
| 32 | constexpr double kBarchanoidBend = 0.15; | ||
| 33 | constexpr double kBarchanoidSegment = 1.5; | ||
| 34 | /// Newton steps for the closest point on a barchan's brink. | ||
| 35 | constexpr int kBrinkSteps = 6; | ||
| 36 | /// Share of the dune height over which sand ripples come in at a dune's edge. | ||
| 37 | constexpr double kSandEdge = 0.05; | ||
| 38 | /// Share of the ripple height kept on a slip face, where avalanches leave them faint. | ||
| 39 | constexpr double kSlipFaceRipples = 0.2; | ||
| 40 | /// Ripple crest wander and kinks: wavelengths per lattice unit of each, and the kinks' share of | ||
| 41 | /// the wander. | ||
| 42 | constexpr double kRippleWander = 5.0; | ||
| 43 | constexpr double kRippleKinkScale = 1.5; | ||
| 44 | constexpr double kRippleKinks = 0.25; | ||
| 45 | /// Share of the ripple height that fades along crests, and its scale in wavelengths. | ||
| 46 | constexpr double kRippleFading = 0.6; | ||
| 47 | constexpr double kRippleFadeScale = 4.0; | ||
| 48 | /// Forked crests: the smallest side in wavelengths of the cells their pairs are drawn in (cells | ||
| 49 | /// otherwise hold one pair on average), the separation of a pair in wavelengths, and the radius in | ||
| 50 | /// wavelengths within which its phase is whole and beyond which it is gone. | ||
| 51 | constexpr double kForkMinCell = 0.5; | ||
| 52 | constexpr double kForkMin = 1.5; | ||
| 53 | constexpr double kForkMax = 3.0; | ||
| 54 | constexpr double kForkInner = 3.2; | ||
| 55 | constexpr double kForkOuter = 4.0; | ||
| 56 | /// Distance in wavelengths over which the ripples fade in from a fork's end. | ||
| 57 | constexpr double kForkCore = 0.5; | ||
| 58 | /// Distance in wavelengths over which a crest falls to nothing at a break. | ||
| 59 | constexpr double kBreakTaper = 1.0; | ||
| 60 | /// Grainflows: the share of a scarp's length its theater head takes to reach full depth, the share | ||
| 61 | /// of a lobe's length it takes to rise, and the share after which its toe rounds off. | ||
| 62 | constexpr double kGrainflowHead = 0.15; | ||
| 63 | constexpr double kGrainflowRise = 0.25; | ||
| 64 | constexpr double kGrainflowToe = 0.6; | ||
| 65 | |||
| 66 | /// A cos^alpha bump falling from 1 at s = 0 to 0 at s = 1. | ||
| 67 | 8550531 | double Bump(double s, double alpha) { | |
| 68 | 8550531 | return s >= 1.0 ? 0.0 : std::pow(std::cos(kHalfPi * s), alpha); | |
| 69 | } | ||
| 70 | |||
| 71 | /// Whether dunes of `shape` stand apart (barchans and domes) rather than along crests. | ||
| 72 | 1600929 | bool IsIsolated(DuneShape shape) { | |
| 73 | 1600929 | return shape == DuneShape::Barchan || shape == DuneShape::Dome; | |
| 74 | } | ||
| 75 | |||
| 76 | /// Whether `ripples` add any relief. | ||
| 77 | 1586091 | bool HasRipples(const std::optional<DuneRipples>& ripples) { | |
| 78 | 1586091 | return ripples && ripples->height > 0.0; | |
| 79 | } | ||
| 80 | |||
| 81 | /// Whether `flows` add any relief. | ||
| 82 | 1501843 | bool HasGrainflows(const std::optional<DuneGrainflows>& flows) { | |
| 83 | 1501843 | return flows && flows->cover > 0.0 && flows->thickness > 0.0; | |
| 84 | } | ||
| 85 | |||
| 86 | /// 2D Perlin noise at `p` / `scale`, as a double. | ||
| 87 | 44748721 | double Noise(const Vec2d& p, double scale, std::uint32_t seed) { | |
| 88 | 44748721 | return static_cast<double>(PerlinNoise2Precise(p.x / scale, p.y / scale, seed)); | |
| 89 | } | ||
| 90 | |||
| 91 | /// The fractional part of `x`, in [0, 1). | ||
| 92 | 11195149 | double Fract(double x) { | |
| 93 | 11195149 | return x - std::floor(x); | |
| 94 | } | ||
| 95 | |||
| 96 | /// One isolated dune: its height, width and how far it is a barchan rather than a dome. | ||
| 97 | struct Dune { | ||
| 98 | double height = 0.0; | ||
| 99 | double width = 0.0; | ||
| 100 | double barchan = 1.0; ///< 1 for a barchan, 0 for a dome, between for the transition. | ||
| 101 | }; | ||
| 102 | |||
| 103 | /// The height of a barchan at (x, y) in its wind frame: x downwind from the brink's apex, y across. | ||
| 104 | 1114335 | double BarchanAt(double x, double y, const Dune& dune, const DuneOptions& options) { | |
| 105 | 1114335 | const double half = 0.5 * dune.width; | |
| 106 | 1114335 | if (std::fabs(y) >= half) { | |
| 107 | return 0.0; | ||
| 108 | } | ||
| 109 | 121983 | const double eta = y / half; | |
| 110 | 121983 | const double crest = dune.height * (1.0 - eta * eta); | |
| 111 | 121983 | const double bend = options.hornLength * dune.height / (half * half); | |
| 112 | 121983 | const double brink = bend * y * y; | |
| 113 | 121983 | if (x <= brink) { | |
| 114 | 80674 | const double stoss = dune.height / std::tan(options.stossSlopeDegrees * kDegree); | |
| 115 | 80674 | return crest * Bump((brink - x) / stoss, options.stossExponent); | |
| 116 | } | ||
| 117 | double t = y; | ||
| 118 | 277277 | for (int step = 0; step < kBrinkSteps; ++step) { | |
| 119 | 235968 | const double f = 2.0 * bend * bend * t * t * t + (1.0 - 2.0 * bend * x) * t - y; | |
| 120 | 235968 | const double df = 6.0 * bend * bend * t * t + 1.0 - 2.0 * bend * x; | |
| 121 | 235968 | if (df == 0.0) { | |
| 122 | break; | ||
| 123 | } | ||
| 124 | 471936 | t = std::clamp(t - f / df, -half, half); | |
| 125 | } | ||
| 126 | 41309 | const double tau = t / half; | |
| 127 | 41309 | const double distance = std::hypot(x - bend * t * t, y - t); | |
| 128 | 82618 | return std::max(0.0, dune.height * (1.0 - tau * tau) - | |
| 129 | 42047 | std::tan(options.slipFaceAngleDegrees * kDegree) * distance); | |
| 130 | } | ||
| 131 | |||
| 132 | /// The height of a dome at (x, y) about its center in its wind frame. | ||
| 133 | 3733380 | double DomeAt(double x, double y, const Dune& dune, const DuneOptions& options) { | |
| 134 | 3733380 | const double along = x / (0.5 * options.domeLength * dune.width); | |
| 135 | 3733380 | const double across = y / (0.5 * dune.width); | |
| 136 | 3733380 | return dune.height * Bump(std::hypot(along, across), options.domeExponent); | |
| 137 | } | ||
| 138 | |||
| 139 | /// The farthest a dune of `width` reaches from its center (m). | ||
| 140 | 52 | double Reach(double width, const DuneOptions& options) { | |
| 141 | 52 | const double height = options.heightRatio * width; | |
| 142 | 52 | const double barchan = std::max( | |
| 143 | 52 | {height / std::tan(options.stossSlopeDegrees * kDegree), | |
| 144 | 52 | options.hornLength * height + height / std::tan(options.slipFaceAngleDegrees * kDegree), | |
| 145 | 52 | 0.5 * width}); | |
| 146 | 52 | const double dome = 0.5 * width * std::max(1.0, options.domeLength); | |
| 147 | 52 | return std::max(barchan, dome); | |
| 148 | } | ||
| 149 | |||
| 150 | /// The ripple profile at phase `t` in [0, 1): a straight rise to a sharp crest, then a straight | ||
| 151 | /// fall over `lee`. | ||
| 152 | 14967 | double RippleProfile(double t, double lee) { | |
| 153 | 14967 | const double upwind = 1.0 - lee; | |
| 154 | 14967 | return t < upwind ? t / upwind : 1.0 - (t - upwind) / lee; | |
| 155 | } | ||
| 156 | |||
| 157 | /// Checks that a size range of grainflows has its minimum at most its maximum. | ||
| 158 | 32 | void RequireSizeOrder(double low, double high, const char* what) { | |
| 159 | 32 | if (!(high >= low)) { | |
| 160 | 2 | throw std::invalid_argument(std::string(what) + | |
| 161 | 3 | ": the minimum must not exceed the maximum"); | |
| 162 | } | ||
| 163 | 31 | } | |
| 164 | |||
| 165 | /// Checks the size order and fade of the grainflows on slip faces. | ||
| 166 | /// @throws std::invalid_argument as `ValidateDuneOptions`. | ||
| 167 | 8 | void ValidateDuneGrainflows(const DuneGrainflows& flows) { | |
| 168 | 8 | RequireSizeOrder(flows.minScarpWidth, flows.maxScarpWidth, "Dune grainflow scarp widths"); | |
| 169 | 8 | RequireSizeOrder(flows.minScarpLength, flows.maxScarpLength, "Dune grainflow scarp lengths"); | |
| 170 | 8 | RequireSizeOrder(flows.minLobeWidth, flows.maxLobeWidth, "Dune grainflow lobe widths"); | |
| 171 | 8 | RequireSizeOrder(flows.minLobeLength, flows.maxLobeLength, "Dune grainflow lobe lengths"); | |
| 172 | 7 | ValidateResolutionFade(flows.fade); | |
| 173 | 6 | } | |
| 174 | |||
| 175 | /// The height of a dune of `dune` at (x, y) in its wind frame: barchan and dome blended. | ||
| 176 | 4847084 | double IsolatedDuneAt(double x, double y, const Dune& dune, const DuneOptions& options) { | |
| 177 | 4847084 | double h = 0.0; | |
| 178 | 4847084 | if (dune.barchan > 0.0) { | |
| 179 | 1113788 | h += dune.barchan * BarchanAt(x, y, dune, options); | |
| 180 | } | ||
| 181 | 4846792 | if (dune.barchan < 1.0) { | |
| 182 | 3733380 | h += (1.0 - dune.barchan) * DomeAt(x, y, dune, options); | |
| 183 | } | ||
| 184 | 4846792 | return h; | |
| 185 | } | ||
| 186 | |||
| 187 | /// The value between `low` and `high` at `t` in [0, 1]. | ||
| 188 | 3050316 | double Between(double low, double high, double t) { | |
| 189 | 3050316 | return low + (high - low) * t; | |
| 190 | } | ||
| 191 | |||
| 192 | } // namespace | ||
| 193 | |||
| 194 | 80 | void ValidateDuneOptions(const DuneOptions& options) { | |
| 195 | 80 | CheckRanges(options); | |
| 196 | 59 | if (!(options.stossSlopeDegrees < options.slipFaceAngleDegrees)) { | |
| 197 | 3 | throw std::invalid_argument("Dune stoss slope must be below the slip face angle"); | |
| 198 | } | ||
| 199 | 56 | if (options.ripples) { | |
| 200 | 16 | ValidateResolutionFade(options.ripples->fade); | |
| 201 | } | ||
| 202 | 56 | ValidateResolutionFade(options.fade); | |
| 203 | 55 | if (options.grainflows) { | |
| 204 | 8 | ValidateDuneGrainflows(*options.grainflows); | |
| 205 | } | ||
| 206 | 53 | } | |
| 207 | |||
| 208 | 27 | DuneRipples MarsDuneRipples() { | |
| 209 | 27 | DuneRipples ripples; | |
| 210 | 27 | ripples.wavelength = 2.1; | |
| 211 | 27 | ripples.height = 0.2; | |
| 212 | 27 | const double leeSlope = std::tan(30.0 * kDegree); | |
| 213 | 27 | ripples.leeFraction = ripples.height / (ripples.wavelength * leeSlope); | |
| 214 | 27 | ripples.sinuosity = 0.8; | |
| 215 | 27 | ripples.defectDensity = 0.099; | |
| 216 | 27 | ripples.flankStraightening = 1.0; | |
| 217 | 27 | return ripples; | |
| 218 | } | ||
| 219 | |||
| 220 | 12 | DuneGrainflows MarsDuneGrainflows() { | |
| 221 | 12 | DuneGrainflows flows; | |
| 222 | 12 | flows.cover = 1.0; | |
| 223 | 12 | flows.minScarpWidth = 0.54; | |
| 224 | 12 | flows.maxScarpWidth = 0.67; | |
| 225 | 12 | flows.minScarpLength = 1.0; | |
| 226 | 12 | flows.maxScarpLength = 1.6; | |
| 227 | 12 | flows.minLobeWidth = 0.57; | |
| 228 | 12 | flows.maxLobeWidth = 0.66; | |
| 229 | 12 | flows.minLobeLength = 2.9; | |
| 230 | 12 | flows.maxLobeLength = 6.0; | |
| 231 | 12 | flows.thickness = 0.015; | |
| 232 | 12 | return flows; | |
| 233 | } | ||
| 234 | |||
| 235 | 8 | DuneOptions MarsBarchans() { | |
| 236 | 8 | DuneOptions dunes; | |
| 237 | 8 | dunes.shape = DuneShape::Barchan; | |
| 238 | 8 | dunes.slipFaceAngleDegrees = 29.0; | |
| 239 | 8 | dunes.stossSlopeDegrees = 8.0; | |
| 240 | 8 | dunes.width = 370.0; | |
| 241 | 8 | dunes.minWidth = 200.0; | |
| 242 | 8 | dunes.domeLength = 2.0; | |
| 243 | 8 | dunes.latticeSpacing = 900.0; | |
| 244 | 8 | dunes.density = 0.5; | |
| 245 | 8 | dunes.ripples = MarsDuneRipples(); | |
| 246 | 8 | return dunes; | |
| 247 | } | ||
| 248 | |||
| 249 | 10 | DuneOptions MarsTransverseDunes() { | |
| 250 | 10 | DuneOptions dunes; | |
| 251 | 10 | dunes.shape = DuneShape::Transverse; | |
| 252 | 10 | dunes.slipFaceAngleDegrees = 29.0; | |
| 253 | 10 | dunes.stossSlopeDegrees = 8.0; | |
| 254 | 10 | dunes.wavelength = 248.0; | |
| 255 | 10 | dunes.height = 28.0; | |
| 256 | 10 | dunes.defectDensity = 0.0027; | |
| 257 | 10 | dunes.ripples = MarsDuneRipples(); | |
| 258 | 10 | return dunes; | |
| 259 | } | ||
| 260 | |||
| 261 | 77 | DuneLayer::DuneLayer(DuneOptions options) : m_options(options) { | |
| 262 | 77 | ValidateDuneOptions(m_options); | |
| 263 | 52 | m_wind = {std::cos(m_options.windAzimuthDegrees * kDegree), | |
| 264 | 52 | std::sin(m_options.windAzimuthDegrees * kDegree)}; | |
| 265 | 52 | const double widest = m_options.width * (1.0 + m_options.widthSpread); | |
| 266 | 52 | m_rings = | |
| 267 | 52 | static_cast<std::int64_t>(std::ceil(Reach(widest, m_options) / m_options.latticeSpacing)); | |
| 268 | 52 | m_defectSeed = SubstreamSeed(m_options.seed, kDuneDefectStream); | |
| 269 | 52 | m_wanderSeed = SubstreamSeed(m_options.seed, kRippleWanderStream); | |
| 270 | 52 | m_kinkSeed = SubstreamSeed(m_options.seed, kRippleKinkStream); | |
| 271 | 52 | m_fadeSeed = SubstreamSeed(m_options.seed, kRippleFadeStream); | |
| 272 | 52 | } | |
| 273 | |||
| 274 | 4 | LayerPtr DuneLayer::Reseeded(std::uint64_t seed) const { | |
| 275 | 4 | DuneOptions options = m_options; | |
| 276 | 4 | options.seed = ReseedWord(seed, 0); | |
| 277 | 4 | return std::make_shared<DuneLayer>(options); | |
| 278 | } | ||
| 279 | |||
| 280 | 538803 | double DuneLayer::Barchans(const Vec2d& q) const { | |
| 281 | 538803 | double highest = 0.0; | |
| 282 | 1077630 | ForEachLatticeCell(q, {m_options.latticeSpacing, m_options.latticeSpacing}, m_rings, | |
| 283 | 538803 | [&](std::int64_t gx, std::int64_t gy) { | |
| 284 | 4846315 | RandomStream random = LatticeStream(m_options.seed, kDuneStream, gx, gy); | |
| 285 | 4845503 | if (random.NextDouble() >= m_options.density) { | |
| 286 | ✗ | return; | |
| 287 | } | ||
| 288 | 5078886 | highest = std::max(highest, BarchanCell(random, gx, gy, q)); | |
| 289 | }); | ||
| 290 | 538827 | return highest; | |
| 291 | } | ||
| 292 | |||
| 293 | 4846188 | double DuneLayer::BarchanCell(RandomStream& random, std::int64_t gx, std::int64_t gy, | |
| 294 | const Vec2d& q) const { | ||
| 295 | 4846188 | const double cx = (static_cast<double>(gx) + random.NextDouble()) * m_options.latticeSpacing; | |
| 296 | 4846558 | const double cy = (static_cast<double>(gy) + random.NextDouble()) * m_options.latticeSpacing; | |
| 297 | 4846587 | Dune dune; | |
| 298 | 9693590 | dune.width = | |
| 299 | 4846587 | m_options.width * (1.0 + m_options.widthSpread * (2.0 * random.NextDouble() - 1.0)); | |
| 300 | 4847003 | dune.height = m_options.heightRatio * dune.width; | |
| 301 | 4847003 | const double azimuth = | |
| 302 | 4847003 | (m_options.windAzimuthDegrees * kDegree) + | |
| 303 | 4847003 | (m_options.azimuthSpreadDegrees * kDegree) * (2.0 * random.NextDouble() - 1.0); | |
| 304 | 9694149 | dune.barchan = m_options.shape == DuneShape::Dome | |
| 305 | 4847144 | ? 0.0 | |
| 306 | 4368934 | : Smoothstep((dune.width - m_options.minWidth) / | |
| 307 | 2184467 | (0.5 * std::max(m_options.minWidth, 1e-9))); | |
| 308 | 4847005 | const double dx = q.x - cx; | |
| 309 | 4847005 | const double dy = q.y - cy; | |
| 310 | 4847005 | const double c = std::cos(azimuth); | |
| 311 | 4847005 | const double s = std::sin(azimuth); | |
| 312 | 4847005 | return IsolatedDuneAt(dx * c + dy * s, dy * c - dx * s, dune, m_options); | |
| 313 | } | ||
| 314 | |||
| 315 | 8 | double DuneLayer::CrestSegment(const Vec2d& p) const { | |
| 316 | 8 | const Vec2d across{-m_wind.y, m_wind.x}; | |
| 317 | 8 | return std::fabs(std::cos(kPi * (p.x * across.x + p.y * across.y) / | |
| 318 | 8 | (kBarchanoidSegment * m_options.wavelength))); | |
| 319 | } | ||
| 320 | |||
| 321 | 33531363 | double DuneLayer::CrestPhase(const Vec2d& p) const { | |
| 322 | 33531363 | const double lambda = m_options.wavelength; | |
| 323 | 33531363 | const Vec2d normal = m_options.shape == DuneShape::Linear ? Vec2d{-m_wind.y, m_wind.x} : m_wind; | |
| 324 | 33531363 | double value = (p.x * normal.x + p.y * normal.y) / lambda + | |
| 325 | 33531363 | m_options.sinuosity * Noise(p, kWanderWavelengths * lambda, m_options.seed); | |
| 326 | 33531363 | if (m_options.shape == DuneShape::Barchanoid) { | |
| 327 | 6 | value += kBarchanoidBend * CrestSegment(p); | |
| 328 | } | ||
| 329 | 33531363 | return value; | |
| 330 | } | ||
| 331 | |||
| 332 | 11180097 | double DuneLayer::CrestAmplitude(const Vec2d& q, double psi) const { | |
| 333 | 11180097 | const double lambda = m_options.wavelength; | |
| 334 | 11180097 | double amplitude = | |
| 335 | 11180097 | 1.0 - m_options.defects * (0.5 + 0.5 * Noise(q, kDefectWavelengths * lambda, m_defectSeed)); | |
| 336 | 11180097 | if (m_options.shape == DuneShape::Barchanoid) { | |
| 337 | 2 | amplitude *= 0.6 + 0.4 * CrestSegment(q); | |
| 338 | } | ||
| 339 | 11180097 | if (m_options.defectDensity > 0.0) { | |
| 340 | 1512000 | const Vec2d along = | |
| 341 | 1512000 | m_options.shape == DuneShape::Linear ? m_wind : Vec2d{-m_wind.y, m_wind.x}; | |
| 342 | 1512000 | amplitude *= | |
| 343 | 1512000 | CrestBreaks(static_cast<std::int64_t>(std::floor(psi)), q.x * along.x + q.y * along.y); | |
| 344 | } | ||
| 345 | 11180097 | return amplitude; | |
| 346 | } | ||
| 347 | |||
| 348 | 11179842 | double DuneLayer::Crests(const Vec2d& q) const { | |
| 349 | 11179842 | const double lambda = m_options.wavelength; | |
| 350 | 11179842 | const double psi = CrestPhase(q); | |
| 351 | 11179842 | const double step = 0.01 * lambda; | |
| 352 | 11179842 | const double gx = (CrestPhase({q.x + step, q.y}) - psi) / step; | |
| 353 | 11179842 | const double gy = (CrestPhase({q.x, q.y + step}) - psi) / step; | |
| 354 | 11179842 | const double stretch = std::max(1e-6, std::hypot(gx, gy) * lambda); | |
| 355 | 11179842 | const double height = CrestAmplitude(q, psi) * m_options.height; | |
| 356 | 11180014 | if (height <= 0.0) { | |
| 357 | return 0.0; | ||
| 358 | } | ||
| 359 | 11180014 | return CrestProfile(Fract(psi), height, stretch); | |
| 360 | } | ||
| 361 | |||
| 362 | 11180957 | double DuneLayer::CrestProfile(double u, double height, double stretch) const { | |
| 363 | 11180957 | const double lambda = m_options.wavelength; | |
| 364 | 11180957 | if (m_options.shape == DuneShape::Linear) { | |
| 365 | 2891812 | const double flank = std::min( | |
| 366 | 1445906 | 0.5, height * stretch / (std::tan(m_options.flankSlopeDegrees * kDegree) * lambda)); | |
| 367 | 1445906 | const double offset = std::fabs(u - 0.5); | |
| 368 | 1445906 | return offset >= flank ? 0.0 : height * Bump(offset / flank, 2.0); | |
| 369 | } | ||
| 370 | 9735051 | const double leeSlope = std::tan(m_options.slipFaceAngleDegrees * kDegree); | |
| 371 | 9735051 | const double stossSlope = std::tan(m_options.stossSlopeDegrees * kDegree); | |
| 372 | 9735051 | const double tallest = m_options.height * stretch / lambda; | |
| 373 | 19469458 | const double brink = std::min(1.0 - std::min(1.0, tallest / leeSlope), tallest / stossSlope); | |
| 374 | 9735051 | const double stoss = std::min(brink, height * stretch / (stossSlope * lambda)); | |
| 375 | 9735051 | if (u <= brink) { | |
| 376 | 4350433 | return stoss <= 0.0 ? 0.0 : height * Bump((brink - u) / stoss, m_options.stossExponent); | |
| 377 | } | ||
| 378 | 5384618 | const double down = (u - brink) * lambda / stretch; | |
| 379 | 6476361 | return std::max(0.0, height - leeSlope * down); | |
| 380 | } | ||
| 381 | |||
| 382 | 11718702 | double DuneLayer::Height(const Vec2d& q) const { | |
| 383 | 11718702 | switch (m_options.shape) { | |
| 384 | 538815 | case DuneShape::Barchan: | |
| 385 | 538815 | case DuneShape::Dome: | |
| 386 | 538815 | return Barchans(q); | |
| 387 | 11179887 | case DuneShape::Transverse: | |
| 388 | 11179887 | case DuneShape::Barchanoid: | |
| 389 | 11179887 | case DuneShape::Linear: | |
| 390 | 11179887 | return Crests(q); | |
| 391 | } | ||
| 392 | return 0.0; | ||
| 393 | } | ||
| 394 | |||
| 395 | 1512000 | double DuneLayer::CrestBreaks(std::int64_t crest, double along) const { | |
| 396 | 1512000 | const double taper = kBreakTaper * m_options.wavelength; | |
| 397 | 1512000 | const double length = 1.0 / m_options.defectDensity + taper; | |
| 398 | 1512000 | const auto cell = static_cast<std::int64_t>(std::floor(along / length)); | |
| 399 | 1512000 | double weight = 1.0; | |
| 400 | 6048000 | for (std::int64_t c = cell - 1; c <= cell + 1; ++c) { | |
| 401 | 4536000 | RandomStream random = LatticeStream(m_options.seed, kDuneCrestBreakStream, crest, c); | |
| 402 | 4536000 | const double at = (static_cast<double>(c) + random.NextDouble()) * length; | |
| 403 | 5356982 | weight = std::min(weight, Smoothstep(std::fabs(along - at) / taper)); | |
| 404 | } | ||
| 405 | 1512000 | return weight; | |
| 406 | } | ||
| 407 | |||
| 408 | 715991 | Vec2d DuneLayer::Gradient(const Vec2d& q, double step) const { | |
| 409 | 715991 | return {(Height({q.x + step, q.y}) - Height({q.x - step, q.y})) / (2.0 * step), | |
| 410 | 715981 | (Height({q.x, q.y + step}) - Height({q.x, q.y - step})) / (2.0 * step)}; | |
| 411 | } | ||
| 412 | |||
| 413 | 701008 | double DuneLayer::SlipFaceWeight(const Vec2d& g) const { | |
| 414 | 701008 | const double grade = std::hypot(g.x, g.y); | |
| 415 | 701008 | if (grade <= 0.0) { | |
| 416 | return 0.0; | ||
| 417 | } | ||
| 418 | 406368 | const double facing = -(g.x * m_wind.x + g.y * m_wind.y) / grade; | |
| 419 | 406368 | return Smoothstep(2.0 * facing) * | |
| 420 | 406368 | Smoothstep(2.0 * grade / std::tan(m_options.slipFaceAngleDegrees * kDegree) - 1.0); | |
| 421 | } | ||
| 422 | |||
| 423 | 701008 | double DuneLayer::Grainflows(const Vec2d& q, double face) const { | |
| 424 | 701008 | const DuneGrainflows& flows = *m_options.grainflows; | |
| 425 | 701008 | if (face <= 0.0) { | |
| 426 | return 0.0; | ||
| 427 | } | ||
| 428 | 89088 | const Vec2d across{-m_wind.y, m_wind.x}; | |
| 429 | 89088 | const double u = q.x * m_wind.x + q.y * m_wind.y; | |
| 430 | 89088 | const double v = q.x * across.x + q.y * across.y; | |
| 431 | 89088 | const double cellAlong = flows.maxScarpLength + flows.maxLobeLength; | |
| 432 | 89088 | const double cellAcross = std::max(flows.maxScarpWidth, flows.maxLobeWidth); | |
| 433 | 89088 | double relief = 0.0; | |
| 434 | 89088 | ForEachLatticeCell({v, u}, {cellAcross, cellAlong}, 1, [&](std::int64_t cv, std::int64_t cu) { | |
| 435 | 801792 | RandomStream random = LatticeStream(m_options.seed, kDuneGrainflowStream, cu, cv); | |
| 436 | 801792 | if (random.NextDouble() >= flows.cover) { | |
| 437 | 39213 | return; | |
| 438 | } | ||
| 439 | 762579 | const double head = (static_cast<double>(cu) + random.NextDouble()) * cellAlong; | |
| 440 | 762579 | const double middle = (static_cast<double>(cv) + random.NextDouble()) * cellAcross; | |
| 441 | 762579 | AddGrainflow(relief, random, u - head, std::fabs(v - middle)); | |
| 442 | }); | ||
| 443 | 89088 | return face * flows.thickness * relief; | |
| 444 | } | ||
| 445 | |||
| 446 | 762579 | void DuneLayer::AddGrainflow(double& relief, RandomStream& random, double s, double n) const { | |
| 447 | 762579 | const DuneGrainflows& flows = *m_options.grainflows; | |
| 448 | 762579 | const double scarpWidth = | |
| 449 | 762579 | Between(flows.minScarpWidth, flows.maxScarpWidth, random.NextDouble()); | |
| 450 | 762579 | const double scarpLength = | |
| 451 | 762579 | Between(flows.minScarpLength, flows.maxScarpLength, random.NextDouble()); | |
| 452 | 762579 | const double lobeWidth = Between(flows.minLobeWidth, flows.maxLobeWidth, random.NextDouble()); | |
| 453 | 762579 | const double lobeLength = | |
| 454 | 762579 | Between(flows.minLobeLength, flows.maxLobeLength, random.NextDouble()); | |
| 455 | 762579 | if (s >= 0.0 && s < scarpLength) { | |
| 456 | 45189 | const double half = 0.5 * scarpWidth; | |
| 457 | 45189 | const double profile = | |
| 458 | 45189 | Smoothstep(s / (kGrainflowHead * scarpLength)) * (1.0 - s / scarpLength); | |
| 459 | 58871 | relief -= profile * std::max(0.0, 1.0 - (n / half) * (n / half)); | |
| 460 | } | ||
| 461 | 762579 | const double t = s - scarpLength; | |
| 462 | 762579 | if (t >= 0.0 && t < lobeLength) { | |
| 463 | 153902 | const double toe = | |
| 464 | 153902 | std::max(0.0, (t - kGrainflowToe * lobeLength) / ((1.0 - kGrainflowToe) * lobeLength)); | |
| 465 | 153902 | const double half = 0.5 * lobeWidth * std::sqrt(std::max(0.0, 1.0 - toe * toe)); | |
| 466 | 153902 | if (n < half) { | |
| 467 | 42867 | const double profile = Smoothstep(t / (kGrainflowRise * lobeLength)); | |
| 468 | 42867 | relief += profile * (1.0 - (n / half) * (n / half)); | |
| 469 | } | ||
| 470 | } | ||
| 471 | 762579 | } | |
| 472 | |||
| 473 | 14967 | double DuneLayer::Ripples(const Vec2d& q, double dune) const { | |
| 474 | 14967 | const DuneRipples& ripples = *m_options.ripples; | |
| 475 | 14967 | const double lambda = ripples.wavelength; | |
| 476 | 14967 | const Vec2d g = Gradient(q, 0.25 * lambda); | |
| 477 | 14967 | const double gx = g.x; | |
| 478 | 14967 | const double gy = g.y; | |
| 479 | 14967 | const double kx = m_wind.x - ripples.slopeCoupling * gx; | |
| 480 | 14967 | const double ky = m_wind.y - ripples.slopeCoupling * gy; | |
| 481 | 14967 | const double tighten = std::max(1e-6, std::hypot(kx, ky)); | |
| 482 | 14967 | double sinuosity = ripples.sinuosity; | |
| 483 | 14967 | if (ripples.flankStraightening > 0.0) { | |
| 484 | 13582 | const double grade = std::hypot(gx, gy); | |
| 485 | 13582 | const double stoss = | |
| 486 | 26444 | grade > 0.0 ? std::max(0.0, (gx * m_wind.x + gy * m_wind.y) / grade) : 1.0; | |
| 487 | 27164 | sinuosity *= 1.0 - ripples.flankStraightening * | |
| 488 | 13582 | Smoothstep(grade / std::tan(m_options.stossSlopeDegrees * kDegree)) * | |
| 489 | 13582 | (1.0 - stoss); | |
| 490 | } | ||
| 491 | 14967 | double core = 1.0; | |
| 492 | 14967 | const double psi = | |
| 493 | 14967 | (q.x * m_wind.x + q.y * m_wind.y - ripples.slopeCoupling * dune) / lambda + | |
| 494 | 14967 | sinuosity * (Noise(q, kRippleWander * lambda, m_wanderSeed) + | |
| 495 | 14967 | kRippleKinks * Noise(q, kRippleKinkScale * lambda, m_kinkSeed)) + | |
| 496 | 14967 | Forks(q, lambda, ripples.defectDensity, core); | |
| 497 | 14967 | const double fading = | |
| 498 | 14967 | 1.0 - kRippleFading * (0.5 + 0.5 * Noise(q, kRippleFadeScale * lambda, m_fadeSeed)); | |
| 499 | 14967 | const double sand = SandDepth(dune); | |
| 500 | 14967 | const double steep = std::hypot(gx, gy) / std::tan(m_options.slipFaceAngleDegrees * kDegree); | |
| 501 | 14967 | const double avalanche = 1.0 - (1.0 - kSlipFaceRipples) * Smoothstep(2.0 * steep - 1.0); | |
| 502 | 14967 | const double share = | |
| 503 | 14967 | (ripples.interduneCover + (1.0 - ripples.interduneCover) * sand) * avalanche; | |
| 504 | 14967 | return share * fading * core * ripples.height / std::max(1.0, tighten) * | |
| 505 | 14967 | RippleProfile(Fract(psi), ripples.leeFraction); | |
| 506 | } | ||
| 507 | |||
| 508 | 14966 | double DuneLayer::Forks(const Vec2d& q, double wavelength, double density, double& core) const { | |
| 509 | 14966 | if (density <= 0.0) { | |
| 510 | return 0.0; | ||
| 511 | } | ||
| 512 | 12396 | const double cell = std::max(std::sqrt(wavelength / density), kForkMinCell * wavelength); | |
| 513 | 12396 | const double none = std::exp(-density * cell * cell / wavelength); | |
| 514 | 12396 | const auto rings = static_cast<std::int64_t>(std::ceil(kForkOuter * wavelength / cell)); | |
| 515 | 12396 | double phase = 0.0; | |
| 516 | 12396 | ForEachLatticeCell(q, {cell, cell}, rings, [&](std::int64_t gx, std::int64_t gy) { | |
| 517 | 454916 | RandomStream random = LatticeStream(m_options.seed, kDuneForkStream, gx, gy); | |
| 518 | 454886 | double draw = random.NextDouble(); | |
| 519 | 901285 | while (draw > none) { | |
| 520 | 446383 | draw *= random.NextDouble(); | |
| 521 | 446400 | const Vec2d center{(static_cast<double>(gx) + random.NextDouble()) * cell, | |
| 522 | 446380 | (static_cast<double>(gy) + random.NextDouble()) * cell}; | |
| 523 | 446361 | AddFork(phase, core, random, q, center, wavelength); | |
| 524 | } | ||
| 525 | 454902 | }); | |
| 526 | 12397 | return phase; | |
| 527 | } | ||
| 528 | |||
| 529 | 446334 | void DuneLayer::AddFork(double& phase, double& core, RandomStream& random, const Vec2d& q, | |
| 530 | const Vec2d& center, double wavelength) { | ||
| 531 | 446334 | const double inner = kForkInner * wavelength; | |
| 532 | 446334 | const double outer = kForkOuter * wavelength; | |
| 533 | 446334 | const double turn = 2.0 * kPi * random.NextDouble(); | |
| 534 | 446444 | const double half = 0.5 * wavelength * (kForkMin + (kForkMax - kForkMin) * random.NextDouble()); | |
| 535 | 446207 | const double sign = random.NextDouble() < 0.5 ? -1.0 : 1.0; | |
| 536 | 446279 | const double r = std::hypot(q.x - center.x, q.y - center.y); | |
| 537 | 446279 | const double window = 1.0 - Smoothstep((r - inner) / (outer - inner)); | |
| 538 | 446422 | if (window <= 0.0) { | |
| 539 | return; | ||
| 540 | } | ||
| 541 | 182409 | const Vec2d a{center.x - half * std::cos(turn), center.y - half * std::sin(turn)}; | |
| 542 | 182409 | const Vec2d b{center.x + half * std::cos(turn), center.y + half * std::sin(turn)}; | |
| 543 | 182409 | double subtended = std::atan2(q.y - a.y, q.x - a.x) - std::atan2(q.y - b.y, q.x - b.x); | |
| 544 | 182409 | subtended -= 2.0 * kPi * std::floor((subtended + kPi) / (2.0 * kPi)); | |
| 545 | 182409 | phase += sign * window * subtended / (2.0 * kPi); | |
| 546 | 182409 | const double nearest = | |
| 547 | 182409 | std::min(std::hypot(q.x - a.x, q.y - a.y), std::hypot(q.x - b.x, q.y - b.y)); | |
| 548 | 186832 | core = std::min(core, Smoothstep(nearest / (kForkCore * wavelength))); | |
| 549 | } | ||
| 550 | |||
| 551 | 99167 | double DuneLayer::SandDepth(double dune) const { | |
| 552 | 99167 | const double reference = | |
| 553 | 99167 | IsIsolated(m_options.shape) ? m_options.heightRatio * m_options.width : m_options.height; | |
| 554 | 198347 | return Smoothstep(dune / std::max(1e-9, kSandEdge * reference)); | |
| 555 | } | ||
| 556 | |||
| 557 | 84209 | double DuneLayer::MaterialCoverAt(double x, double y, const LayerContext& context) const { | |
| 558 | 84209 | const std::optional<Vec2d> q = | |
| 559 | 84209 | AnchoredPlane(context.SurfaceOrPlane(), m_options.anchor).Project(x, y); | |
| 560 | 84207 | if (!q) { | |
| 561 | return 0.0; | ||
| 562 | } | ||
| 563 | 84207 | const double sand = SandDepth(Height(*q)); | |
| 564 | 84212 | if (!HasRipples(m_options.ripples)) { | |
| 565 | return sand; | ||
| 566 | } | ||
| 567 | 405 | const double kept = m_options.ripples->interduneCover; | |
| 568 | 405 | return kept + (1.0 - kept) * sand; | |
| 569 | } | ||
| 570 | |||
| 571 | 7269787 | double DuneLayer::DuneHeightAt(double x, double y, const Surface& surface) const { | |
| 572 | 7269787 | const std::optional<Vec2d> q = AnchoredPlane(surface, m_options.anchor).Project(x, y); | |
| 573 | 7269787 | return q ? Height(*q) : 0.0; | |
| 574 | } | ||
| 575 | |||
| 576 | 1501977 | float DuneLayer::HeightAt(float height, double x, double y, const LayerContext& context) const { | |
| 577 | 1501977 | const Surface& surface = context.SurfaceOrPlane(); | |
| 578 | 1501914 | const std::optional<Vec2d> q = AnchoredPlane(surface, m_options.anchor).Project(x, y); | |
| 579 | 1501817 | if (!q) { | |
| 580 | return height; | ||
| 581 | } | ||
| 582 | 1501817 | const double size = IsIsolated(m_options.shape) ? m_options.width : m_options.wavelength; | |
| 583 | 1501817 | const double resolved = | |
| 584 | 1501817 | SourceResolvedSize(context.source, m_options.sourceResolvesPixels, surface, x, y); | |
| 585 | 3719607 | const auto weight = [&](double feature, const ResolutionFade& fade) { | |
| 586 | 2217958 | return feature >= resolved ? 0.0 : ResolutionWeight(feature, context.spacing, fade); | |
| 587 | 1501649 | }; | |
| 588 | 1501649 | const double duneWeight = weight(size, m_options.fade); | |
| 589 | 1501776 | const std::optional<DuneRipples>& ripples = m_options.ripples; | |
| 590 | 1501776 | const double rippleWeight = | |
| 591 | 1501776 | HasRipples(ripples) ? weight(ripples->wavelength, ripples->fade) : 0.0; | |
| 592 | 1501776 | const std::optional<DuneGrainflows>& flows = m_options.grainflows; | |
| 593 | 1501776 | const double flowWeight = HasGrainflows(flows) ? weight(flows->minLobeWidth, flows->fade) : 0.0; | |
| 594 | 1501776 | if (duneWeight <= 0.0 && rippleWeight <= 0.0 && flowWeight <= 0.0) { | |
| 595 | return height; | ||
| 596 | } | ||
| 597 | 1501640 | const double dune = Height(*q); | |
| 598 | 1501819 | double relief = duneWeight * dune; | |
| 599 | 1501819 | if (rippleWeight > 0.0) { | |
| 600 | 14967 | relief += rippleWeight * Ripples(*q, dune); | |
| 601 | } | ||
| 602 | 1501819 | if (flowWeight > 0.0) { | |
| 603 | 701024 | const Vec2d g = Gradient(*q, 0.25 * flows->minScarpLength); | |
| 604 | 701008 | relief += flowWeight * Grainflows(*q, SlipFaceWeight(g)); | |
| 605 | } | ||
| 606 | 1501803 | return static_cast<float>(static_cast<double>(height) + relief); | |
| 607 | } | ||
| 608 | |||
| 609 | } // namespace terraingen | ||
| 610 |