./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
82 / 82
100.0%
Branches:
64 / 73
87.7%
Functions:
8 / 8
100.0%

src/layers/craters/shaping/resolved_state.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "layers/craters/shaping/resolved_state.h"
4
5 #include "terraingen/layers/craters/degradation/diffusion.h"
6 #include "terraingen/layers/craters/profiles/complex.h"
7 #include "terraingen/layers/craters/profiles/fassett_thomson.h"
8 #include "terraingen/layers/craters/shaping/apply.h"
9 #include "util/checks.h"
10
11 #include <algorithm>
12 #include <array>
13 #include <cmath>
14 #include <cstdio>
15 #include <limits>
16 #include <stdexcept>
17 #include <string>
18
19 namespace terraingen {
20
21 namespace {
22
23 /// "The manual crater at (x, y), D m across", which starts error messages.
24 11 std::string CraterName(const Crater& crater) {
25 11 std::array<char, 128> buffer{};
26 11 std::snprintf(buffer.data(), buffer.size(), "The manual crater at (%g, %g), %g m across",
27 11 crater.x, crater.y, crater.diameter);
28 11 return buffer.data();
29 }
30
31 /// The profile that shapes a crater of `diameter` meters from its fresh state: `profile` without
32 /// diffusion, and its simple profile below the complex transition.
33 360 const CraterProfile& FreshShape(const CraterProfile& profile, double diameter) {
34 360 const CraterProfile* shape = &profile;
35
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 210 times.
360 if (const auto* diffused = dynamic_cast<const DiffusedProfile*>(shape)) {
36 150 shape = &diffused->Fresh();
37 }
38
3/4
✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
✓ Branch 3 taken 275 times.
360 if (const auto* complex = dynamic_cast<const ComplexCraterProfile*>(shape)) {
39
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 5 times.
85 if (!complex->IsComplex(diameter)) {
40 80 shape = &complex->Simple();
41 }
42 }
43 360 return *shape;
44 }
45
46 /// The lowest point of the Fassett-Thomson fit within the rim, over the diameter.
47 10 double FitFloorRatio(const FassettThomsonProfile& profile, double diameter,
48 const CraterMorphometry& fresh) {
49 10 constexpr double kInf = std::numeric_limits<double>::infinity();
50 10 const CraterState unbounded{fresh, {kInf, fresh.rimHeightRatio}, 0.0};
51 10 double floor = kInf;
52
2/2
✓ Branch 0 taken 2010 times.
✓ Branch 1 taken 10 times.
2020 for (int step = 0; step <= 200; ++step) {
53 2010 const double distance = 0.0025 * static_cast<double>(step) * diameter;
54 2010 floor = std::fmin(floor, profile.Height(distance, diameter, unbounded) / diameter);
55 }
56 10 return floor;
57 }
58
59 /// Checks that a Fassett-Thomson simple crater can take `crater`'s explicit depth and rim height.
60 /// @throws std::invalid_argument otherwise.
61 14 void CheckFassettThomson(const FassettThomsonProfile& profile, const Crater& crater,
62 const CraterMorphometry& fresh) {
63 14 const std::string honored = "; the Martin-Parkes-Dunstan profile and complex craters honor "
64 14 "both depth and rim height";
65
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10 times.
14 if (crater.rimHeight) {
66
1/1
✓ Branch 1 taken 4 times.
8 throw std::invalid_argument(CraterName(crater) +
67
1/1
✓ Branch 1 taken 4 times.
8 " has a rim height, which the Fassett-Thomson profile fixes" +
68
1/1
✓ Branch 2 taken 4 times.
12 honored);
69 }
70
1/1
✓ Branch 1 taken 10 times.
10 const double deepest = fresh.rimHeightRatio - FitFloorRatio(profile, crater.diameter, fresh);
71
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 9 times.
10 if (*crater.depth / crater.diameter > deepest * (1.0 + 1e-12)) {
72 1 throw std::invalid_argument(CraterName(crater) +
73
1/1
✓ Branch 1 taken 1 time.
2 " is deeper than the Fassett-Thomson fit (" +
74
4/4
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
4 std::to_string(deepest * crater.diameter) + " m)" + honored);
75 }
76 9 }
77
78 /// The state of a crater of explicit depth ratio `depth` under a diffused profile, from `fresh`.
79 /// @throws std::invalid_argument for a rim height, or a depth shallower than the oldest table.
80 150 CraterState DiffusedState(const DiffusedProfile& profile, const Crater& crater,
81 CraterMorphometry fresh, double depth) {
82
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 149 times.
150 if (crater.rimHeight) {
83 1 throw std::invalid_argument(
84
1/1
✓ Branch 1 taken 1 time.
2 CraterName(crater) + " has a rim height, which a diffused profile ties to its depth; "
85
1/1
✓ Branch 2 taken 1 time.
3 "give the depth alone");
86 }
87 149 const double diameter = crater.diameter;
88 149 double tau = 0.0;
89
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 35 times.
149 if (depth < fresh.depthRatio) {
90 114 const double oldest =
91 114 profile.Morphometry(fresh, diameter, profile.Ages().back()).depthRatio;
92
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 112 times.
114 if (oldest > depth) {
93 2 throw std::invalid_argument(CraterName(crater) +
94
1/1
✓ Branch 1 taken 2 times.
4 " is shallower than diffusion wears it (" +
95
3/3
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
8 std::to_string(oldest * diameter) + " m)");
96 }
97 112 tau = profile.AgeAtDepthRatio(fresh, diameter, depth);
98 }
99
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 112 times.
147 if (!(tau > 0.0)) {
100 35 fresh.depthRatio = depth;
101 35 return {fresh, fresh, 0.0};
102 }
103 112 return {fresh, profile.Morphometry(fresh, diameter, tau), tau};
104 }
105
106 } // namespace
107
108 362563 CraterState ResolvedCraterState(const CraterProfile& profile, const DegradationModel& degradation,
109 const Crater& crater, double rate) {
110
3/4
✓ Branch 0 taken 362196 times.
✓ Branch 1 taken 367 times.
✓ Branch 2 taken 362219 times.
✗ Branch 3 not taken.
362563 if (!crater.depth && !crater.rimHeight) {
111
2/2
✓ Branch 0 taken 601 times.
✓ Branch 1 taken 361618 times.
362219 return rate > 1.0 ? degradation.WornState(crater, rate) : degradation.State(crater);
112 }
113
3/4
✓ Branch 0 taken 343 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 343 times.
✗ Branch 3 not taken.
344 if (!(crater.depth && IsPositive(*crater.depth) &&
114
3/4
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 241 times.
✓ Branch 2 taken 101 times.
✗ Branch 3 not taken.
342 (!crater.rimHeight || IsNonNegative(*crater.rimHeight)))) {
115
1/1
✓ Branch 1 taken 3 times.
6 throw std::invalid_argument(CraterName(crater) +
116 " needs a positive depth, and a rim height that is unset or "
117
1/1
✓ Branch 2 taken 3 times.
9 "not negative");
118 }
119 341 CraterMorphometry fresh = degradation.State(crater).fresh;
120 341 const double diameter = crater.diameter;
121
1/2
✓ Branch 0 taken 341 times.
✗ Branch 1 not taken.
341 const double depth = *crater.depth / diameter;
122 682 if (const auto* ft =
123
3/4
✓ Branch 0 taken 341 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 327 times.
341 dynamic_cast<const FassettThomsonProfile*>(&FreshShape(profile, diameter))) {
124 14 CheckFassettThomson(*ft, crater, fresh);
125 }
126
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 186 times.
336 if (const auto* diffused = dynamic_cast<const DiffusedProfile*>(&profile)) {
127 150 return DiffusedState(*diffused, crater, fresh, depth);
128 }
129 186 CraterMorphometry current{depth, 0.0};
130
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 91 times.
186 if (crater.rimHeight) {
131 95 current.rimHeightRatio = *crater.rimHeight / diameter;
132 } else {
133 91 const double kept =
134
2/4
✓ Branch 0 taken 91 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 91 times.
182 fresh.depthRatio > 0.0 ? std::clamp(depth / fresh.depthRatio, 0.0, 1.0) : 1.0;
135 91 current.rimHeightRatio = fresh.rimHeightRatio * kept;
136 }
137 186 fresh.depthRatio = std::fmax(fresh.depthRatio, current.depthRatio);
138 186 fresh.rimHeightRatio = std::fmax(fresh.rimHeightRatio, current.rimHeightRatio);
139 186 return {fresh, current, 0.0};
140 }
141
142 52 bool TakesRimHeight(const CraterProfile& profile, double diameter) {
143
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 33 times.
52 return dynamic_cast<const DiffusedProfile*>(&profile) == nullptr &&
144
3/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 4 times.
19 dynamic_cast<const FassettThomsonProfile*>(&FreshShape(profile, diameter)) == nullptr;
145 }
146
147 344285 CraterState ResolvedCraterState(const CraterShapeModels& models, const Crater& crater,
148 double rate) {
149 344285 return ResolvedCraterState(*models.profile, *models.degradation, crater, rate);
150 }
151
152 } // namespace terraingen
153