./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
110 / 110
100.0%
Branches:
45 / 48
93.8%
Functions:
11 / 11
100.0%

src/layers/bedforms.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/bedforms.h"
4
5 #include "layers/lattice.h"
6 #include "terraingen/geometry/geometry.h"
7 #include "terraingen/layers/bedforms_fields.h"
8 #include "terraingen/layers/source_resolved.h"
9 #include "terraingen/noise/perlin.h"
10 #include "terraingen/random/stream.h"
11 #include "terraingen/raster/cell_loops.h"
12 #include "terraingen/raster/grid.h"
13
14 #include <algorithm>
15 #include <cmath>
16 #include <memory>
17 #include <stdexcept>
18
19 namespace terraingen {
20
21 namespace {
22
23 constexpr double kRadiusMin = 0.40;
24 constexpr double kRadiusMax = 0.70;
25 constexpr double kRagged = 0.30;
26 constexpr double kEdgeShare = 0.35;
27 constexpr double kWavelengthMin = 0.8;
28 constexpr double kWavelengthSpan = 0.4;
29 constexpr double kWanderWavelengths = 5.0;
30 constexpr double kRagRadii = 0.5;
31
32 constexpr const char* kExposureReader = "Bedforms that read the exposure field";
33
34 } // namespace
35
36 37 void ValidateBedformOptions(const BedformOptions& options) {
37 37 CheckRanges(options);
38
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 26 times.
30 if (options.exposure) {
39 4 ValidateExposureParams(*options.exposure);
40 }
41 30 ValidateResolutionFade(options.fade);
42 29 }
43
44 7 BedformOptions MarsLargeRipples() {
45 7 BedformOptions ripples;
46 7 ripples.wavelength = 2.0;
47 7 ripples.height = 0.2;
48 7 const double leeSlope = std::tan(30.0 * kDegree);
49 7 ripples.leeFraction = kPi * ripples.height / (2.0 * ripples.wavelength * leeSlope);
50 7 ripples.sinuosity = 0.6;
51 7 ripples.cover = 0.15;
52 7 ripples.fieldSize = 80.0;
53 7 return ripples;
54 }
55
56 4 BedformOptions MarsTransverseAeolianRidges() {
57 4 BedformOptions ridges;
58 4 ridges.wavelength = 25.8;
59 4 ridges.height = 1.3;
60 4 ridges.leeFraction = 0.5;
61 4 ridges.sinuosity = 0.2;
62 4 ridges.cover = 0.07;
63 4 ridges.fieldSize = 250.0;
64 4 return ridges;
65 }
66
67
1/1
✓ Branch 1 taken 28 times.
36 BedformLayer::BedformLayer(BedformOptions options) : m_options(options) {
68
1/1
✓ Branch 1 taken 28 times.
36 ValidateBedformOptions(m_options);
69 28 m_cell = m_options.fieldSize / (kRadiusMin + kRadiusMax);
70 28 const double meanArea = kPi * (std::pow(kRadiusMax, 3.0) - std::pow(kRadiusMin, 3.0)) /
71 (3.0 * (kRadiusMax - kRadiusMin));
72
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10 times.
28 m_density = std::min(1.0, -std::log1p(-m_options.cover) / meanArea);
73 28 m_wanderSeed = SubstreamSeed(m_options.seed, kBedformWanderStream);
74
1/1
✓ Branch 1 taken 28 times.
28 m_exposure = LayerExposure(m_options.exposure, m_options.exposureSpacing, kExposureReader);
75 36 }
76
77 3 LayerPtr BedformLayer::Reseeded(std::uint64_t seed) const {
78 3 BedformOptions options = m_options;
79 3 options.seed = ReseedWord(seed, 0);
80 3 return std::make_shared<BedformLayer>(options);
81 }
82
83 743790 void BedformLayer::AddField(FieldSum& sum, RandomStream& random, const Vec2d& center,
84 const Vec2d& offset, const LayerContext& context) const {
85 743790 const double barren = random.NextDouble();
86 743734 const auto ox = static_cast<float>(97.0 * random.NextDouble());
87 743772 const auto oy = static_cast<float>(97.0 * random.NextDouble());
88 743754 const double radius = (kRadiusMin + (kRadiusMax - kRadiusMin) * random.NextDouble()) * m_cell;
89 743775 const double scale = kWavelengthMin + kWavelengthSpan * random.NextDouble();
90 743763 const double azimuth =
91 743763 (m_options.windAzimuthDegrees * kDegree) +
92 743763 (m_options.azimuthSpreadDegrees * kDegree) * (2.0 * random.NextDouble() - 1.0);
93 743779 const double phase = random.NextDouble();
94 743750 const double dx = offset.x;
95 743750 const double dy = offset.y;
96 743750 const double rag = static_cast<double>(
97 743750 PerlinNoise2(static_cast<float>(dx / (kRagRadii * radius)) + ox,
98 743750 static_cast<float>(dy / (kRagRadii * radius)) + oy, m_options.seed));
99 743750 const double inside = 1.0 - std::hypot(dx, dy) / (radius * (1.0 + kRagged * rag));
100
2/2
✓ Branch 0 taken 80765 times.
✓ Branch 1 taken 662985 times.
743750 if (inside <= 0.0) {
101 return;
102 }
103
2/2
✓ Branch 0 taken 1011 times.
✓ Branch 1 taken 999 times.
2010 if (m_exposure &&
104
2/2
✓ Branch 0 taken 2010 times.
✓ Branch 1 taken 78755 times.
80765 barren < m_exposure.At(center.x, center.y, context.ground, context.SurfaceOrPlane())) {
105 return;
106 }
107 79766 const double cover = Smoothstep(inside / kEdgeShare);
108 79769 const double wavelength = m_options.wavelength * scale;
109 79769 const double wander = static_cast<double>(PerlinNoise2(
110 79769 static_cast<float>(dx / (kWanderWavelengths * wavelength)) + oy,
111 79769 static_cast<float>(dy / (kWanderWavelengths * wavelength)) + ox, m_wanderSeed));
112 79769 const double along = (dx * std::cos(azimuth) + dy * std::sin(azimuth)) / wavelength +
113 79769 m_options.sinuosity * wander + phase;
114 79769 const double t = along - std::floor(along);
115 79769 const double upwind = 1.0 - m_options.leeFraction;
116 159538 const double profile = t < upwind
117
2/2
✓ Branch 0 taken 53428 times.
✓ Branch 1 taken 26341 times.
79769 ? 0.5 * (1.0 - std::cos(kPi * t / upwind))
118 26341 : 0.5 * (1.0 + std::cos(kPi * (t - upwind) / m_options.leeFraction));
119 79769 sum.cover += cover;
120 79769 sum.relief += cover * m_options.height * scale * profile;
121 }
122
123 2 double BedformLayer::GroundReach(double /*spacing*/) const {
124
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 return m_exposure ? kRadiusMax * (1.0 + kRagged) * m_cell + m_exposure.Reach() : 0.0;
125 }
126
127 190288 BedformLayer::FieldSum BedformLayer::SumFields(double x, double y,
128 const LayerContext& context) const {
129 190288 FieldSum sum;
130
2/2
✓ Branch 0 taken 40001 times.
✓ Branch 1 taken 150287 times.
190288 if (m_density <= 0.0) {
131 40001 return sum;
132 }
133 150287 m_exposure.Require(context.ground);
134 150286 const Surface& surface = context.SurfaceOrPlane();
135 150286 const std::shared_ptr<const SurfaceCells> cells = ThreadCells(surface);
136 150284 const double reach = kRadiusMax * (1.0 + kRagged) * m_cell;
137
1/1
✓ Branch 1 taken 150286 times.
1532018 for (const CellIndex& cell :
138
4/6
✓ Branch 1 taken 150286 times.
✓ Branch 4 taken 150285 times.
✓ Branch 6 taken 1231453 times.
✓ Branch 7 taken 150281 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
1381733 cells->CellsCovering(surface.Expand({x, y, x, y}, reach), m_cell)) {
139
1/1
✓ Branch 1 taken 1231154 times.
1231453 RandomStream random = LatticeStream(m_options.seed, kBedformStream, cell.i, cell.j);
140
1/1
✓ Branch 1 taken 1231333 times.
1231154 const double chance = m_density * cells->CellArea(cell, m_cell) / (m_cell * m_cell);
141
3/3
✓ Branch 1 taken 1231503 times.
✓ Branch 3 taken 487717 times.
✓ Branch 4 taken 743786 times.
1231333 if (random.NextDouble() >= chance) {
142 487717 continue;
143 }
144
1/1
✓ Branch 1 taken 743774 times.
743786 const double u = random.NextDouble();
145
1/1
✓ Branch 1 taken 743773 times.
743774 const double v = random.NextDouble();
146
1/1
✓ Branch 1 taken 743784 times.
743773 const Vec2d center = cells->SamplePoint(u, v, cell, m_cell);
147
2/2
✓ Branch 1 taken 743788 times.
✓ Branch 4 taken 743732 times.
743784 AddField(sum, random, center, surface.Offset(center, {x, y}), context);
148 150281 }
149
1/2
✓ Branch 0 taken 150284 times.
✗ Branch 1 not taken.
150284 return sum;
150 150286 }
151
152 132923 double BedformLayer::MaterialCoverAt(double x, double y, const LayerContext& context) const {
153
2/2
✓ Branch 1 taken 110964 times.
✓ Branch 2 taken 21957 times.
132923 return std::min(1.0, SumFields(x, y, context).cover);
154 }
155
156 57571 float BedformLayer::HeightAt(float height, double x, double y, const LayerContext& context) const {
157 57571 const double resolved = SourceResolvedSize(context.source, m_options.sourceResolvesPixels,
158 context.SurfaceOrPlane(), x, y);
159 57572 const double weight =
160 57572 m_options.wavelength >= resolved
161
2/2
✓ Branch 0 taken 57415 times.
✓ Branch 1 taken 157 times.
57572 ? 0.0
162 57415 : ResolutionWeight(m_options.wavelength, context.spacing, m_options.fade);
163
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 57365 times.
57415 if (weight <= 0.0) {
164 207 return height;
165 }
166 57365 const FieldSum sum = SumFields(x, y, context);
167
2/2
✓ Branch 0 taken 25812 times.
✓ Branch 1 taken 31554 times.
57366 if (sum.cover <= 0.0) {
168 return height;
169 }
170 51624 return static_cast<float>(static_cast<double>(height) +
171
2/2
✓ Branch 0 taken 1616 times.
✓ Branch 1 taken 24196 times.
27428 weight * sum.relief / std::max(1.0, sum.cover));
172 }
173
174 20975 void BedformLayer::CheckContext(const LayerContext& context) const {
175 20975 m_exposure.Require(context.ground);
176 20974 }
177
178 } // namespace terraingen
179