./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
89 / 89
100.0%
Branches:
74 / 80
92.5%
Functions:
10 / 10
100.0%

src/layers/craters/crater.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/craters/crater.h"
4
5 #include "terraingen/random/philox.h"
6 #include "terraingen/random/stream.h"
7 #include "terraingen/util/bits.h"
8 #include "util/checks.h"
9
10 #include <algorithm>
11 #include <cmath>
12 #include <limits>
13 #include <stdexcept>
14 #include <string>
15
16 namespace terraingen {
17
18 82300 std::uint64_t ManualCraterId(double x, double y, double diameter) {
19 82300 const IdWords xBits = SplitId(BitCast<std::uint64_t>(x));
20 82300 const IdWords yBits = SplitId(BitCast<std::uint64_t>(y));
21 82300 const IdWords dBits = SplitId(BitCast<std::uint64_t>(diameter));
22 82300 const U32x4 block =
23 82300 Philox4x32({xBits.low, xBits.high, yBits.low, yBits.high}, {dBits.low, dBits.high});
24 82300 return JoinWords(block.x, block.y);
25 }
26
27 291795 void AppendManualCraters(const CraterList& manual, CraterList& craters,
28 const std::function<bool(const Crater&)>& keep) {
29
2/2
✓ Branch 0 taken 75602 times.
✓ Branch 1 taken 293218 times.
368820 for (std::size_t n = 0; n < manual.Size(); ++n) {
30 75602 Crater crater = manual.At(n);
31
4/6
✓ Branch 0 taken 77036 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 77036 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 77036 times.
✗ Branch 5 not taken.
77038 if (!(std::isfinite(crater.x) && std::isfinite(crater.y) && IsPositive(crater.diameter) &&
32
1/2
✓ Branch 0 taken 77034 times.
✗ Branch 1 not taken.
77034 IsNonNegative(crater.ageGyr))) {
33 6 throw std::invalid_argument("Manual craters need finite positions, positive diameters "
34
1/1
✓ Branch 2 taken 6 times.
6 "and non-negative ages");
35 }
36
4/4
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 76940 times.
✓ Branch 2 taken 91 times.
✓ Branch 3 taken 1 time.
77032 if (crater.depth && !IsPositive(*crater.depth)) {
37
1/1
✓ Branch 2 taken 3 times.
3 throw std::invalid_argument("A manual crater's depth must be unset or positive");
38 }
39
4/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 77020 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 1 time.
77029 if (crater.rimHeight && !IsNonNegative(*crater.rimHeight)) {
40 2 throw std::invalid_argument(
41
1/1
✓ Branch 2 taken 2 times.
2 "A manual crater's rim height must be unset or not negative");
42 }
43
4/4
✓ Branch 0 taken 76940 times.
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 76938 times.
✓ Branch 3 taken 2 times.
77027 if (!crater.depth && crater.rimHeight) {
44
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("A manual crater's rim height needs its depth too");
45 }
46 77025 crater.manual = true;
47
2/2
✓ Branch 0 taken 72524 times.
✓ Branch 1 taken 4501 times.
77025 if (crater.id == 0) {
48 72524 crater.id = ManualCraterId(crater.x, crater.y, crater.diameter);
49 }
50
4/4
✓ Branch 0 taken 76879 times.
✓ Branch 1 taken 146 times.
✓ Branch 3 taken 27065 times.
✓ Branch 4 taken 49814 times.
77025 if (!keep || keep(crater)) {
51 27211 craters.Append(crater);
52 }
53 }
54 293218 }
55
56
1/1
✓ Branch 1 taken 33 times.
33 CraterList::CraterList(std::initializer_list<Crater> craters) {
57
1/1
✓ Branch 1 taken 33 times.
33 Reserve(craters.size());
58
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 33 times.
70 for (const Crater& crater : craters) {
59
1/1
✓ Branch 1 taken 37 times.
37 Append(crater);
60 }
61 33 }
62
63 393035 void CraterList::Reserve(std::size_t count) {
64 393035 m_x.reserve(count);
65 393873 m_y.reserve(count);
66 394358 m_diameter.reserve(count);
67 394363 m_ageGyr.reserve(count);
68 394376 m_id.reserve(count);
69 394333 m_manual.reserve(count);
70 394086 m_depth.reserve(count);
71 394344 m_rimHeight.reserve(count);
72 394297 m_replace.reserve(count);
73 394335 }
74
75 5661342 void CraterList::Append(const Crater& crater) {
76 5661342 m_x.push_back(crater.x);
77 5661685 m_y.push_back(crater.y);
78 5661767 m_diameter.push_back(crater.diameter);
79 5661944 m_ageGyr.push_back(crater.ageGyr);
80 5661989 m_id.push_back(crater.id);
81
2/2
✓ Branch 0 taken 5624240 times.
✓ Branch 1 taken 37792 times.
11286272 m_manual.push_back(crater.manual ? 1 : 0);
82
2/2
✓ Branch 0 taken 589 times.
✓ Branch 1 taken 5661528 times.
5662706 m_depth.push_back(crater.depth.value_or(std::numeric_limits<double>::quiet_NaN()));
83
2/2
✓ Branch 0 taken 293 times.
✓ Branch 1 taken 5661844 times.
5662430 m_rimHeight.push_back(crater.rimHeight.value_or(std::numeric_limits<double>::quiet_NaN()));
84
2/2
✓ Branch 0 taken 5661925 times.
✓ Branch 1 taken 202 times.
11324052 m_replace.push_back(crater.replace ? 1 : 0);
85 5661998 }
86
87 6424 void CraterList::Append(const CraterList& other) {
88 6424 m_x.insert(m_x.end(), other.m_x.begin(), other.m_x.end());
89 6424 m_y.insert(m_y.end(), other.m_y.begin(), other.m_y.end());
90 6424 m_diameter.insert(m_diameter.end(), other.m_diameter.begin(), other.m_diameter.end());
91 6424 m_ageGyr.insert(m_ageGyr.end(), other.m_ageGyr.begin(), other.m_ageGyr.end());
92 6424 m_id.insert(m_id.end(), other.m_id.begin(), other.m_id.end());
93 6424 m_manual.insert(m_manual.end(), other.m_manual.begin(), other.m_manual.end());
94 6424 m_depth.insert(m_depth.end(), other.m_depth.begin(), other.m_depth.end());
95 6424 m_rimHeight.insert(m_rimHeight.end(), other.m_rimHeight.begin(), other.m_rimHeight.end());
96 6424 m_replace.insert(m_replace.end(), other.m_replace.begin(), other.m_replace.end());
97 6424 }
98
99 9853137 Crater CraterList::At(std::size_t index) const {
100
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 9853136 times.
9853137 if (index >= Size()) {
101
1/1
✓ Branch 2 taken 1 time.
1 throw std::out_of_range("Crater index out of range");
102 }
103
2/2
✓ Branch 0 taken 9852253 times.
✓ Branch 1 taken 883 times.
9853136 return {m_x[index],
104 9853136 m_y[index],
105 9853136 m_diameter[index],
106 9853136 m_ageGyr[index],
107 9853136 m_id[index],
108 9853136 m_manual[index] != 0,
109 19706272 UnlessNan(m_depth[index]),
110
1/2
✓ Branch 0 taken 9853149 times.
✗ Branch 1 not taken.
9853136 UnlessNan(m_rimHeight[index]),
111
2/2
✓ Branch 0 taken 9852253 times.
✓ Branch 1 taken 883 times.
19706272 m_replace[index] != 0};
112 }
113
114 2 bool CraterList::AnyReplace() const {
115 2 return std::find(m_replace.begin(), m_replace.end(), std::uint8_t{1}) != m_replace.end();
116 }
117
118 1812441 bool CraterSelection::Includes(const Crater& crater) const {
119
8/8
✓ Branch 0 taken 952708 times.
✓ Branch 1 taken 859733 times.
✓ Branch 2 taken 952671 times.
✓ Branch 3 taken 37 times.
✓ Branch 4 taken 476727 times.
✓ Branch 5 taken 475944 times.
✓ Branch 6 taken 1336490 times.
✓ Branch 7 taken 7 times.
1812441 return !((crater.manual && !crater.replace && !manual) || crater.diameter < minDiameter ||
120
2/4
✓ Branch 0 taken 1336506 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1336510 times.
✗ Branch 3 not taken.
1336490 crater.diameter > maxDiameter || crater.ageGyr < minAgeGyr ||
121
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1336507 times.
1336510 crater.ageGyr > maxAgeGyr);
122 }
123
124 161504 void ValidateCraterSelection(const CraterSelection& selection, std::string_view what) {
125
4/4
✓ Branch 0 taken 161503 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 161500 times.
✓ Branch 3 taken 3 times.
161504 if (!(selection.minDiameter >= 0.0 && selection.maxDiameter >= selection.minDiameter &&
126
4/4
✓ Branch 0 taken 161498 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 161496 times.
161500 selection.minAgeGyr >= 0.0 && selection.maxAgeGyr >= selection.minAgeGyr)) {
127
2/2
✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
24 throw std::invalid_argument(std::string(what) +
128
2/2
✓ Branch 2 taken 8 times.
✓ Branch 5 taken 8 times.
24 " size and age ranges must satisfy 0 <= min <= max");
129 }
130 161496 }
131
132 } // namespace terraingen
133