./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
24 / 37
64.9%
Branches:
30 / 106
28.3%
Functions:
3 / 5, 2 excl
60.0%

include/terraingen/layers/dunes.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief `DuneLayer`, dune fields of barchans, domes or crest-line dunes, with ripples on them.
5 #pragma once
6
7 #include "terraingen/geometry/geometry.h"
8 #include "terraingen/geometry/surface.h"
9 #include "terraingen/layers/fade.h"
10 #include "terraingen/layers/layer.h"
11 #include "terraingen/layers/material.h"
12 #include "terraingen/random/stream.h"
13
14 #include <cstdint>
15 #include <optional>
16
17 namespace terraingen {
18
19 /// The kind of dune a `DuneLayer` builds.
20 enum class DuneShape {
21 Barchan, ///< Isolated crescents with a slip face, horns downwind; domes below `minWidth`.
22 Dome, ///< Isolated rounded heaps without a slip face.
23 Transverse, ///< Continuous crests across the wind, each with a slip face.
24 Barchanoid, ///< Transverse crests bent into linked crescents.
25 Linear, ///< Symmetric crests along the wind, as under a bimodal wind on scarce sand.
26 };
27
28 /// Ripples superposed on dunes, their crests bent by the dune's slope: each rises along a straight
29 /// stoss to a sharp crest and falls along a straight lee. By default the large ripples 2.1 m apart
30 /// and 0.2 m high on Mars's Bagnold dunes (`MarsDuneRipples`), symmetric and without defects.
31 7 struct DuneRipples {
32 double wavelength = 2.1; ///< Crest to crest on flat sand (m).
33 double height = 0.2; ///< Trough to crest on flat sand (m).
34 /// Share of a wavelength the lee face takes, in (0, 1): 0.5 is symmetric, less is steeper.
35 double leeFraction = 0.5;
36 double sinuosity = 0.3; ///< How far a crest wanders along its length, in wavelengths.
37 /// How strongly the dune's slope turns and tightens the ripples: their wave vector is the wind
38 /// minus this times the dune's gradient, so they shorten and lower on steep lee faces and turn
39 /// on flanks; they never grow above `height`.
40 double slopeCoupling = 1.0;
41 double interduneCover = 0.0; ///< Share of the ripples' height kept off the dunes, in [0, 1].
42 /// Defect density: pairs of crest ends (forked crests, Y-junctions) per meter of crest, as
43 /// Ewing et al. (2010) measure it; 0 for none.
44 double defectDensity = 0.0;
45 /// How far crests straighten off the stoss, in [0, 1]: 0 keeps `sinuosity` everywhere, 1
46 /// keeps it on flat sand and stoss slopes and takes it away on flanks and lee slopes as steep
47 /// as the stoss.
48 double flankStraightening = 0.0;
49 ResolutionFade fade{}; ///< How ripples too fine for the spacing fade out.
50
51 ✗ bool operator==(const DuneRipples& other) const {
52
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return wavelength == other.wavelength && height == other.height &&
53
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ leeFraction == other.leeFraction && sinuosity == other.sinuosity &&
54
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ slopeCoupling == other.slopeCoupling && interduneCover == other.interduneCover &&
55
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ defectDensity == other.defectDensity &&
56
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ flankStraightening == other.flankStraightening && fade == other.fade;
57 }
58 };
59
60 /// Grainflows on slip faces: avalanches that leave a theater-headed scarp below the brink and a
61 /// tongue-shaped lobe of sand below it. Each is drawn on a lattice aligned with the wind, its sizes
62 /// uniform between the bounds, and shown only where the ground faces downwind near the slip face
63 /// angle. Lengths are in meters, along the slope; widths across it.
64 2 struct DuneGrainflows {
65 double cover = 1.0; ///< Chance, in [0, 1], that each lattice cell holds one.
66 double minScarpWidth = 0.5; ///< Narrowest scarp.
67 double maxScarpWidth = 0.5; ///< Widest scarp; at least `minScarpWidth`.
68 double minScarpLength = 1.0; ///< Shortest scarp.
69 double maxScarpLength = 1.0; ///< Longest scarp; at least `minScarpLength`.
70 double minLobeWidth = 0.5; ///< Narrowest lobe.
71 double maxLobeWidth = 0.5; ///< Widest lobe; at least `minLobeWidth`.
72 double minLobeLength = 3.0; ///< Shortest lobe.
73 double maxLobeLength = 3.0; ///< Longest lobe; at least `minLobeLength`.
74 double thickness = 0.015; ///< Depth of a scarp and height of a lobe (m).
75 ResolutionFade fade{}; ///< How grainflows too narrow for the spacing fade out.
76
77 ✗ bool operator==(const DuneGrainflows& other) const {
78
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return cover == other.cover && minScarpWidth == other.minScarpWidth &&
79
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ maxScarpWidth == other.maxScarpWidth && minScarpLength == other.minScarpLength &&
80
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ maxScarpLength == other.maxScarpLength && minLobeWidth == other.minLobeWidth &&
81
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ maxLobeWidth == other.maxLobeWidth && minLobeLength == other.minLobeLength &&
82
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ maxLobeLength == other.maxLobeLength && thickness == other.thickness &&
83
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ fade == other.fade;
84 }
85 };
86
87 /// The dunes a `DuneLayer` builds, their sizes and slopes. Lengths are in meters, angles in
88 /// degrees.
89 struct DuneOptions {
90 DuneShape shape = DuneShape::Barchan; ///< The kind of dune.
91 /// Where the wind blows toward, degrees counter-clockwise from +x; slip faces face it.
92 double windAzimuthDegrees = 0.0;
93 double azimuthSpreadDegrees = 0.0; ///< Each barchan or dome's wind differs by up to this.
94 double slipFaceAngleDegrees = 32.0; ///< Lee slip face slope.
95 double stossSlopeDegrees = 10.0; ///< Mean windward slope.
96 double stossExponent = 3.0; ///< Exponent of the cos^alpha windward profile of a dune.
97 double domeExponent = 1.8; ///< Exponent of the cos^alpha profile of a dome.
98
99 /// Barchans and domes: one candidate per lattice cell of `latticeSpacing` (m). By default 50 m
100 /// wide barchans in a lattice of 129 m cells half of which hold a dune (Elbelrhiti et al. 2008,
101 /// https://arxiv.org/abs/cond-mat/0609120).
102 double latticeSpacing = 129.0;
103 double density = 0.5; ///< Chance a cell holds a dune, in [0, 1].
104 double width = 50.0; ///< Mean width, horn to horn (m).
105 double widthSpread = 0.3; ///< Each width varies from (1 - it) to (1 + it) times `width`.
106 double minWidth = 15.0; ///< Width below which a barchan is a dome without a slip face.
107 double heightRatio = 1.0 / 15.0; ///< Height over width.
108 double hornLength = 9.1; ///< How far the horns reach downwind of the crest, in heights.
109 double domeLength = 2.0; ///< Length of a dome over its width.
110
111 /// Transverse, barchanoid and linear dunes: crest-to-crest spacing (m).
112 double wavelength = 550.0;
113 double height = 20.0; ///< Crest height (m).
114 double sinuosity = 0.3; ///< How far a crest wanders along its length, in wavelengths.
115 /// Share of the height that varies along crests, ending some, in [0, 1].
116 double defects = 0.3;
117 /// Crest-line dunes: pairs of crest ends per meter of crest, as Ewing et al. (2010) measure it;
118 /// crests break into segments that stand above half their height for 1 / `defectDensity`
119 /// meters on average, falling to nothing over a wavelength either side of each break. 0 for
120 /// none.
121 double defectDensity = 0.0;
122 double flankSlopeDegrees = 20.0; ///< Mean flank slope of linear dunes.
123
124 std::uint32_t seed = 1; ///< Varies the dunes and ripples.
125 /// On a body, the surface point whose tangent frame the dunes are laid out in; true to scale
126 /// near it, and no dunes on the far hemisphere.
127 Vec2d anchor{};
128 ResolutionFade
129 fade{}; ///< How dunes too fine for the spacing fade out, by samples across them.
130 std::optional<DuneRipples> ripples{}; ///< Ripples on the dunes; unset for none.
131 std::optional<DuneGrainflows> grainflows{}; ///< Avalanches on the slip faces; unset for none.
132 /// Source cells across from which the source's measured data already holds a dune: with a
133 /// source given to the layer, the dunes are left out where their size (`width` of barchans
134 /// and domes, `wavelength` of crests) is at least that many of its cells, as are ripples by
135 /// their wavelength and grainflows by their narrowest lobe (`SourceResolvedLimit`). Unset, or
136 /// without a source, adds them everywhere.
137 std::optional<double> sourceResolvesPixels = 4.0;
138
139 1 bool operator==(const DuneOptions& other) const {
140
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return shape == other.shape && windAzimuthDegrees == other.windAzimuthDegrees &&
141
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 azimuthSpreadDegrees == other.azimuthSpreadDegrees &&
142
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 slipFaceAngleDegrees == other.slipFaceAngleDegrees &&
143
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 stossSlopeDegrees == other.stossSlopeDegrees &&
144
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 stossExponent == other.stossExponent && domeExponent == other.domeExponent &&
145
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 latticeSpacing == other.latticeSpacing && density == other.density &&
146
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 width == other.width && widthSpread == other.widthSpread &&
147
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 minWidth == other.minWidth && heightRatio == other.heightRatio &&
148
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 hornLength == other.hornLength && domeLength == other.domeLength &&
149
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 wavelength == other.wavelength && height == other.height &&
150
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 sinuosity == other.sinuosity && defects == other.defects &&
151
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 defectDensity == other.defectDensity &&
152
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 flankSlopeDegrees == other.flankSlopeDegrees && seed == other.seed &&
153
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
3 anchor == other.anchor && fade == other.fade && ripples == other.ripples &&
154
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 grainflows == other.grainflows && sourceResolvesPixels == other.sourceResolvesPixels;
155 }
156 };
157
158 /// Checks dune options.
159 /// @throws std::invalid_argument for non-positive sizes or slopes, a slip face or flank of 90 deg
160 /// or more, a stoss slope not below the slip face, a density, defects or interdune cover outside
161 /// [0, 1], a width spread outside [0, 1), negative spreads or sinuosity, or invalid ripples or
162 /// fade.
163 void ValidateDuneOptions(const DuneOptions& options);
164
165 /// Martian large wind ripples to lay on dunes: 2.1 m wavelength, 0.2 m height and a 30 deg lee;
166 /// 0.099 pairs of crest ends per meter, as Olympia Undae's 2.1 m wind ripples (Ewing et al. 2010);
167 /// sinuous on stoss slopes and straighter on flanks and lee slopes (Lapotre et al. 2016; Ewing et
168 /// al. 2017).
169 DuneRipples MarsDuneRipples();
170
171 /// The grainflows of Namib Dune's lee slope (Ewing et al. 2017, Table 1): scarps 0.54 to 0.67 m
172 /// wide and 1.0 to 1.6 m long, lobes 0.57 to 0.66 m wide and 2.9 to 6 m long. Their thickness is
173 /// not measured on Mars; 0.015 m is that of a Namibian barchan's grainflows (Nield et al. 2017, in
174 /// Ewing et al. 2017). Every cell holds one, as palimpsest grainflows are abundant on that slope.
175 DuneGrainflows MarsDuneGrainflows();
176
177 /// Martian barchans with large ripples: 29 deg slip faces, 8 deg stoss slopes, 370 m wide, domes
178 /// below 200 m wide and twice as long as wide; spacing and density are placeholders to set per
179 /// site.
180 DuneOptions MarsBarchans();
181
182 /// The transverse dunes of Mars's north polar erg with large ripples: 248 m apart and 28 m high,
183 /// with 29 deg slip faces and 8 deg stoss slopes, and the defect density of Olympia Undae's primary
184 /// crests, 0.0027 pairs of ends per meter (Ewing et al. 2010).
185 DuneOptions MarsTransverseDunes();
186
187 /// Wind-blown dunes added to the terrain, barchan crescents or crest-line dunes with slip faces
188 /// at `slipFaceAngle`; overlapping dunes join as their upper envelope.
189
3/12
<unknown function 189>:
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
<unknown function 31>:
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
39 class DuneLayer final : public FormulaLayer, public MaterialSource {
190 public:
191 /// Builds the layer; on a body dunes lie in the tangent frame of `options.anchor`.
192 /// @throws std::invalid_argument as `ValidateDuneOptions`.
193 explicit DuneLayer(DuneOptions options);
194
195 /// The dunes' height at world (x, y) on `surface`, without ripples (m).
196 double DuneHeightAt(double x, double y, const Surface& surface = {}) const;
197
198 /// The options the layer was made with.
199 1 const DuneOptions& Options() const {
200 1 return m_options;
201 }
202
203 /// The layer with its seed `ReseedWord(seed, 0)`.
204 LayerPtr Reseeded(std::uint64_t seed) const override;
205
206 444 MaterialClass Material() const override {
207 444 return MaterialClass::Sand;
208 }
209 /// How fully dune sand covers (x, y): the dunes, and with ripples their `interduneCover`.
210 double MaterialCoverAt(double x, double y, const LayerContext& context) const override;
211
212 protected:
213 float HeightAt(float height, double x, double y, const LayerContext& context) const override;
214
215 private:
216 double Height(const Vec2d& q) const;
217 double Barchans(const Vec2d& q) const;
218 /// The height at planar point `q` of the barchan or dome of lattice cell (`gx`, `gy`), drawn
219 /// from `random` after its chance.
220 double BarchanCell(RandomStream& random, std::int64_t gx, std::int64_t gy,
221 const Vec2d& q) const;
222 double Crests(const Vec2d& q) const;
223 /// How far planar point `p` lies from a barchanoid crest's links, in [0, 1].
224 double CrestSegment(const Vec2d& p) const;
225 /// The warped crest phase at planar point `p`, in wavelengths.
226 double CrestPhase(const Vec2d& p) const;
227 /// The share of the crest height standing at planar point `q` on crest phase `psi`.
228 double CrestAmplitude(const Vec2d& q, double psi) const;
229 /// The crest profile at phase `u` in [0, 1) for a crest `height` meters high whose phase is
230 /// stretched by `stretch`.
231 double CrestProfile(double u, double height, double stretch) const;
232 double Ripples(const Vec2d& q, double dune) const;
233 /// How fully a dune `dune` meters high covers the ground with sand, in [0, 1].
234 double SandDepth(double dune) const;
235 /// How much of crest `crest` stands at `along` meters along it, in [0, 1]: 0 at its breaks.
236 double CrestBreaks(std::int64_t crest, double along) const;
237 /// The phase that forked crests `wavelength` meters apart, `density` pairs of ends per meter of
238 /// crest, add near planar point `q`, in wavelengths; `core` becomes the weight in [0, 1] that
239 /// fades the crests out at the forks' ends.
240 double Forks(const Vec2d& q, double wavelength, double density, double& core) const;
241 /// Adds to `phase` and `core` the fork centered at `center`, its shape drawn from `random`.
242 static void AddFork(double& phase, double& core, RandomStream& random, const Vec2d& q,
243 const Vec2d& center, double wavelength);
244 /// The dunes' gradient at planar point `q`, by central differences `step` meters apart.
245 Vec2d Gradient(const Vec2d& q, double step) const;
246 /// How far the ground with gradient `g` is a slip face, in [0, 1]: facing downwind near the
247 /// slip face angle.
248 double SlipFaceWeight(const Vec2d& g) const;
249 /// The grainflows' relief at planar point `q` (m), on the slip face share `face`.
250 double Grainflows(const Vec2d& q, double face) const;
251 /// Adds to `relief` the scarp and lobe of one grainflow at `s` meters downslope of its head
252 /// and `n` across, its sizes drawn from `random`.
253 void AddGrainflow(double& relief, RandomStream& random, double s, double n) const;
254
255 DuneOptions m_options;
256 Vec2d m_wind{1.0, 0.0};
257 std::int64_t m_rings = 1;
258 std::uint32_t m_defectSeed = 0; ///< Seed of the noise of defects along crests.
259 std::uint32_t m_wanderSeed = 0; ///< Seed of the ripple crest wander.
260 std::uint32_t m_kinkSeed = 0; ///< Seed of the ripple crest kinks.
261 std::uint32_t m_fadeSeed = 0; ///< Seed of the ripple fading along crests.
262 };
263
264 } // namespace terraingen
265