./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
23 / 23
100.0%
Branches:
17 / 19
89.5%
Functions:
3 / 3
100.0%

src/layers/craters/shaping/slope.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/craters/shaping/slope.h"
4
5 #include "terraingen/layers/craters/crater_fields.h"
6 #include "terraingen/layers/fade.h"
7 #include "terraingen/random/stream.h"
8
9 #include <cmath>
10 #include <stdexcept>
11
12 namespace terraingen {
13
14 namespace {
15
16 /// The largest floor offset in diameters that keeps the shifted interior from folding.
17 constexpr double kMaxFloorOffset = 0.23;
18
19 } // namespace
20
21 161424 void ValidateCraterSlopeResponse(const CraterSlopeResponse& response) {
22 161424 CheckRanges(response);
23 161701 ValidateValueDistribution(response.floorOffset);
24
4/4
✓ Branch 1 taken 161576 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 161742 times.
323493 if (!(response.floorOffset.Lowest() >= 0.0 &&
25 161576 response.floorOffset.Highest() < kMaxFloorOffset)) {
26
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Crater floor offset must be in [0, 0.23) diameters");
27 }
28 161742 }
29
30 1847 double SlopeWearRate(const CraterSlopeResponse& response, double east, double north) {
31
2/2
✓ Branch 0 taken 604 times.
✓ Branch 1 taken 1243 times.
1847 if (!response.criticalGradient) {
32 return 1.0;
33 }
34
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 2 times.
604 const double share = std::hypot(east, north) / *response.criticalGradient;
35 604 const double room = 1.0 - share * share;
36
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 2 times.
604 return room > 1.0 / response.maxWearRate ? 1.0 / room : response.maxWearRate;
37 }
38
39 2371 CraterSlopeShape SlopeShape(const Crater& crater, double east, double north,
40 const CraterSlopeResponse& response) {
41 2371 const double gradient = std::hypot(east, north);
42
4/4
✓ Branch 0 taken 2259 times.
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 2258 times.
2371 if (!response.asymmetric || !(gradient > 0.0)) {
43 113 return {};
44 }
45 2258 const double strength = Smoothstep(std::atan(gradient) / (response.fullSlopeDegrees * kDegree));
46
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2258 times.
2258 const IdWords id = SplitId(crater.id);
47
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2258 times.
2258 RandomStream random(response.seed.value_or(0u), kCraterSlopeStream, id.low, id.high);
48 2258 const double offset = strength * response.floorOffset.Draw(random) * crater.diameter;
49 2258 const Vec2d up{east / gradient, north / gradient};
50 2258 const double removed = strength * (1.0 - response.upslopeRim);
51 2258 return {{-offset * up.x, -offset * up.y}, {removed * up.x, removed * up.y}};
52 }
53
54 } // namespace terraingen
55