./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
61 / 61
100.0%
Branches:
25 / 28
89.3%
Functions:
10 / 10
100.0%

src/layers/strata.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/strata.h"
4
5 #include "terraingen/geometry/surface.h"
6 #include "terraingen/layers/anchored_plane.h"
7 #include "terraingen/layers/fade.h"
8 #include "terraingen/layers/pavement_fields.h"
9 #include "terraingen/random/stream.h"
10 #include "terraingen/util/bits.h"
11
12 #include <algorithm>
13 #include <cmath>
14 #include <memory>
15 #include <optional>
16 #include <stdexcept>
17
18 namespace terraingen {
19
20 namespace {
21
22 constexpr const char* kGroundReader = "A strata material cover";
23 /// Width of the fade from bench to cliff in the material map, in bed heights.
24 constexpr double kCliffFade = 0.05;
25
26 } // namespace
27
28 145 void ValidateStrataOptions(const StrataOptions& options) {
29 145 CheckRanges(options);
30 132 }
31
32 145 StrataLayer::StrataLayer(StrataOptions options) : m_options(options) {
33 145 ValidateStrataOptions(m_options);
34 132 m_dipDirection = {std::cos(m_options.dipAzimuthDegrees * kDegree),
35 132 std::sin(m_options.dipAzimuthDegrees * kDegree)};
36 132 m_dipSlope = std::tan(m_options.dipDegrees * kDegree);
37 132 }
38
39 111 LayerPtr StrataLayer::Reseeded(std::uint64_t seed) const {
40 111 StrataOptions options = m_options;
41 111 options.seed = ReseedWord(seed, 0);
42 111 return std::make_shared<StrataLayer>(options);
43 }
44
45 689983 RandomStream StrataLayer::BedStream(std::uint32_t stream, std::int64_t k) const {
46 689983 const IdWords bed = SplitId(static_cast<std::uint64_t>(k));
47 689983 return RandomStream(m_options.seed, stream, bed.low, bed.high);
48 }
49
50 689182 double StrataLayer::BedBoundary(std::int64_t k) const {
51 689182 RandomStream random = BedStream(kStrataStream, k);
52 689182 const double offset = m_options.thicknessVariation * (random.NextDouble() - 0.5);
53 689182 return m_options.bedThickness * (static_cast<double>(k) + offset);
54 }
55
56 323652 StrataLayer::Bed StrataLayer::BedAt(double level) const {
57 323652 auto k = static_cast<std::int64_t>(std::floor(level / m_options.bedThickness));
58 323652 double bottom = BedBoundary(k);
59
2/2
✓ Branch 1 taken 21620 times.
✓ Branch 2 taken 323652 times.
668924 while (level < bottom) {
60 21620 --k;
61 21620 bottom = BedBoundary(k);
62 }
63 323652 double top = BedBoundary(k + 1);
64
2/2
✓ Branch 1 taken 20258 times.
✓ Branch 2 taken 323652 times.
667562 while (level >= top) {
65 20258 ++k;
66 20258 bottom = top;
67 20258 top = BedBoundary(k + 1);
68 }
69
2/2
✓ Branch 0 taken 801 times.
✓ Branch 1 taken 322851 times.
323652 const bool hard = m_options.hardShare >= 1.0 ||
70
2/2
✓ Branch 2 taken 374 times.
✓ Branch 3 taken 427 times.
801 BedStream(kStrataHardnessStream, k).NextDouble() < m_options.hardShare;
71 323652 return {bottom, top, hard};
72 }
73
74 323681 bool StrataLayer::StratigraphicLevel(double height, double x, double y, const Surface& surface,
75 double& level) const {
76 323681 const std::optional<Vec2d> q = AnchoredPlane(surface, m_options.anchor).Project(x, y);
77
2/2
✓ Branch 0 taken 323680 times.
✓ Branch 1 taken 1 time.
323681 if (!q) {
78 return false;
79 }
80 323680 level = height + m_dipSlope * (q->x * m_dipDirection.x + q->y * m_dipDirection.y);
81 323680 return std::isfinite(level);
82 }
83
84 323278 double StrataLayer::TerracedAt(double height, double x, double y, const Surface& surface) const {
85 323278 double level = 0.0;
86
4/4
✓ Branch 1 taken 323277 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 323249 times.
✓ Branch 4 taken 28 times.
323278 if (!StratigraphicLevel(height, x, y, surface, level) || m_options.strength <= 0.0) {
87 return height;
88 }
89 323249 const Bed bed = BedAt(level);
90 323249 const double rise = bed.top - bed.bottom;
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 323249 times.
323249 const double u = std::clamp((level - bed.bottom) / rise, 0.0, 1.0);
92
2/2
✓ Branch 0 taken 323035 times.
✓ Branch 1 taken 214 times.
323249 const double sharpness = bed.hard ? m_options.sharpness : m_options.softSharpness;
93 323249 const double terraced = height + rise * (std::pow(u, sharpness) - u);
94 323249 return height + m_options.strength * (terraced - height);
95 }
96
97 406 double StrataLayer::MaterialCoverAt(double x, double y, const LayerContext& context) const {
98 406 RequireGround(context.ground, kGroundReader);
99 405 const double height = static_cast<double>(context.ground->HeightAt(x, y, context.spacing));
100
4/4
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 403 times.
✓ Branch 3 taken 1 time.
405 if (!std::isfinite(height) || m_options.strength <= 0.0) {
101 return 0.0;
102 }
103 403 double level = 0.0;
104
1/2
✓ Branch 2 taken 403 times.
✗ Branch 3 not taken.
403 if (!StratigraphicLevel(height, x, y, context.SurfaceOrPlane(), level)) {
105 return 0.0;
106 }
107 403 const Bed bed = BedAt(level);
108
2/2
✓ Branch 0 taken 190 times.
✓ Branch 1 taken 213 times.
403 if (!bed.hard) {
109 return 0.0;
110 }
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
190 const double u = std::clamp((level - bed.bottom) / (bed.top - bed.bottom), 0.0, 1.0);
112 190 return Smoothstep((u - (1.0 - m_options.cliffShare)) / kCliffFade + 0.5);
113 }
114
115 323278 float StrataLayer::HeightAt(float height, double x, double y, const LayerContext& context) const {
116 323278 return static_cast<float>(
117 323278 TerracedAt(static_cast<double>(height), x, y, context.SurfaceOrPlane()));
118 }
119
120 } // namespace terraingen
121