include/terraingen/layers/bedforms.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `BedformLayer`, fields of wind-blown ripples or dunes. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/layers/exposure.h" | ||
| 8 | #include "terraingen/layers/fade.h" | ||
| 9 | #include "terraingen/layers/layer.h" | ||
| 10 | #include "terraingen/layers/material.h" | ||
| 11 | #include "terraingen/random/stream.h" | ||
| 12 | |||
| 13 | #include <cstdint> | ||
| 14 | #include <optional> | ||
| 15 | |||
| 16 | namespace terraingen { | ||
| 17 | |||
| 18 | /// The bedform fields a `BedformLayer` scatters: their ripples, cover and wind. By default large | ||
| 19 | /// wind ripples 4 m apart and 0.4 m high (Lapotre et al. 2016, | ||
| 20 | /// https://doi.org/10.1126/science.aaf3206). | ||
| 21 | 3 | struct BedformOptions { | |
| 22 | double wavelength = 4.0; ///< Crest to crest (m); each field's varies from 0.8 to 1.2 times it. | ||
| 23 | double height = 0.4; ///< Trough to crest (m), scaled with each field's wavelength. | ||
| 24 | /// Where the wind blows toward, degrees counter-clockwise from +x; crests lie across it. | ||
| 25 | double windAzimuthDegrees = 0.0; | ||
| 26 | double azimuthSpreadDegrees = 0.0; ///< Each field's wind differs by up to this. | ||
| 27 | /// Share of a wavelength the lee face takes, in (0, 1): 0.5 is symmetric, less is steeper. | ||
| 28 | double leeFraction = 0.5; | ||
| 29 | double sinuosity = 0.5; ///< How far a crest wanders along its length, in wavelengths. | ||
| 30 | double cover = 0.9; ///< Share of the ground under fields, in [0, 0.9]. | ||
| 31 | double fieldSize = 100.0; ///< Typical width of a field (m). | ||
| 32 | std::uint32_t seed = 1; ///< Varies the fields. | ||
| 33 | /// When set, a field is left out with probability the exposure at its center, read from the | ||
| 34 | /// ground beneath the layer. | ||
| 35 | std::optional<ExposureParams> exposure; | ||
| 36 | double exposureSpacing = 1.0; ///< Spacing the ground is read at for the exposure field (m). | ||
| 37 | /// How bedforms too fine for the spacing fade out, by samples per wavelength. | ||
| 38 | ResolutionFade fade{}; | ||
| 39 | /// Source cells across from which the source's measured data already holds the bedforms: with | ||
| 40 | /// a source given to the layer, they are left out where `wavelength` is at least that many of | ||
| 41 | /// its cells (`SourceResolvedLimit`). Unset, or without a source, adds them everywhere. | ||
| 42 | std::optional<double> sourceResolvesPixels = 4.0; | ||
| 43 | |||
| 44 | 1 | bool operator==(const BedformOptions& other) const { | |
| 45 | 1 | return wavelength == other.wavelength && height == other.height && | |
| 46 | 1 | windAzimuthDegrees == other.windAzimuthDegrees && | |
| 47 | 1 | azimuthSpreadDegrees == other.azimuthSpreadDegrees && | |
| 48 | 1 | leeFraction == other.leeFraction && sinuosity == other.sinuosity && | |
| 49 | 1 | cover == other.cover && fieldSize == other.fieldSize && seed == other.seed && | |
| 50 | 1 | exposure == other.exposure && exposureSpacing == other.exposureSpacing && | |
| 51 | 3 | fade == other.fade && sourceResolvesPixels == other.sourceResolvesPixels; | |
| 52 | } | ||
| 53 | }; | ||
| 54 | |||
| 55 | /// Checks bedform options. | ||
| 56 | /// @throws std::invalid_argument for a non-positive wavelength or field size, a negative height, | ||
| 57 | /// a lee fraction outside (0, 1), a cover outside [0, 0.9], a negative sinuosity or spread, a | ||
| 58 | /// non-positive exposure spacing, or an invalid fade or exposure. | ||
| 59 | void ValidateBedformOptions(const BedformOptions& options); | ||
| 60 | |||
| 61 | /// Martian large wind ripples: 2 m wavelength, 0.2 m height, a 30 degree lee slope and sinuous | ||
| 62 | /// crests; cover and field size are placeholders to set per site. | ||
| 63 | BedformOptions MarsLargeRipples(); | ||
| 64 | |||
| 65 | /// Martian transverse aeolian ridges: symmetric, 25.8 m wavelength and 1.3 m height; cover and | ||
| 66 | /// field size are placeholders to set per site. | ||
| 67 | BedformOptions MarsTransverseAeolianRidges(); | ||
| 68 | |||
| 69 | /// Fields of wind-blown bedforms, ripples or dunes, added to the terrain. | ||
| 70 | /// Each lattice cell may hold a ragged-edged field; overlapping fields average by cover. | ||
| 71 | 28 | class BedformLayer final : public FormulaLayer, public MaterialSource { | |
| 72 | public: | ||
| 73 | /// Builds the layer; the azimuth is counter-clockwise from local east, and an enabled exposure | ||
| 74 | /// field needs `context.ground`. | ||
| 75 | /// @throws std::invalid_argument as `ValidateBedformOptions`. | ||
| 76 | explicit BedformLayer(BedformOptions options); | ||
| 77 | |||
| 78 | /// The options the layer was made with. | ||
| 79 | 1 | const BedformOptions& Options() const { | |
| 80 | 1 | return m_options; | |
| 81 | } | ||
| 82 | |||
| 83 | /// The layer with its seed `ReseedWord(seed, 0)`. | ||
| 84 | LayerPtr Reseeded(std::uint64_t seed) const override; | ||
| 85 | |||
| 86 | /// The largest field's reach plus the exposure field's reach at its center; 0 without | ||
| 87 | /// exposure. | ||
| 88 | double GroundReach(double spacing) const override; | ||
| 89 | |||
| 90 | ✗ | MaterialClass Material() const override { | |
| 91 | ✗ | return MaterialClass::Sand; | |
| 92 | } | ||
| 93 | /// How fully world (x, y) lies in a field, in [0, 1]: the bedform fields are sand. Independent | ||
| 94 | /// of the spacing. | ||
| 95 | /// @throws std::invalid_argument if the exposure field is enabled and `context.ground` is null. | ||
| 96 | double MaterialCoverAt(double x, double y, const LayerContext& context) const override; | ||
| 97 | |||
| 98 | protected: | ||
| 99 | float HeightAt(float height, double x, double y, const LayerContext& context) const override; | ||
| 100 | void CheckContext(const LayerContext& context) const override; | ||
| 101 | |||
| 102 | private: | ||
| 103 | struct FieldSum { | ||
| 104 | double cover = 0.0; | ||
| 105 | double relief = 0.0; | ||
| 106 | }; | ||
| 107 | FieldSum SumFields(double x, double y, const LayerContext& context) const; | ||
| 108 | void AddField(FieldSum& sum, RandomStream& random, const Vec2d& center, const Vec2d& offset, | ||
| 109 | const LayerContext& context) const; | ||
| 110 | |||
| 111 | BedformOptions m_options; | ||
| 112 | double m_cell = 1.0; | ||
| 113 | double m_density = 0.0; | ||
| 114 | std::uint32_t m_wanderSeed = 0; ///< Seed of the crest wander noise. | ||
| 115 | LayerExposure m_exposure; | ||
| 116 | }; | ||
| 117 | |||
| 118 | } // namespace terraingen | ||
| 119 |