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 | 368820 | for (std::size_t n = 0; n < manual.Size(); ++n) { | |
| 30 | 75602 | Crater crater = manual.At(n); | |
| 31 | 77038 | if (!(std::isfinite(crater.x) && std::isfinite(crater.y) && IsPositive(crater.diameter) && | |
| 32 | 77034 | IsNonNegative(crater.ageGyr))) { | |
| 33 | 6 | throw std::invalid_argument("Manual craters need finite positions, positive diameters " | |
| 34 | 6 | "and non-negative ages"); | |
| 35 | } | ||
| 36 | 77032 | if (crater.depth && !IsPositive(*crater.depth)) { | |
| 37 | 3 | throw std::invalid_argument("A manual crater's depth must be unset or positive"); | |
| 38 | } | ||
| 39 | 77029 | if (crater.rimHeight && !IsNonNegative(*crater.rimHeight)) { | |
| 40 | 2 | throw std::invalid_argument( | |
| 41 | 2 | "A manual crater's rim height must be unset or not negative"); | |
| 42 | } | ||
| 43 | 77027 | if (!crater.depth && crater.rimHeight) { | |
| 44 | 2 | throw std::invalid_argument("A manual crater's rim height needs its depth too"); | |
| 45 | } | ||
| 46 | 77025 | crater.manual = true; | |
| 47 | 77025 | if (crater.id == 0) { | |
| 48 | 72524 | crater.id = ManualCraterId(crater.x, crater.y, crater.diameter); | |
| 49 | } | ||
| 50 | 77025 | if (!keep || keep(crater)) { | |
| 51 | 27211 | craters.Append(crater); | |
| 52 | } | ||
| 53 | } | ||
| 54 | 293218 | } | |
| 55 | |||
| 56 | 33 | CraterList::CraterList(std::initializer_list<Crater> craters) { | |
| 57 | 33 | Reserve(craters.size()); | |
| 58 | 70 | for (const Crater& crater : craters) { | |
| 59 | 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 | 11286272 | m_manual.push_back(crater.manual ? 1 : 0); | |
| 82 | 5662706 | m_depth.push_back(crater.depth.value_or(std::numeric_limits<double>::quiet_NaN())); | |
| 83 | 5662430 | m_rimHeight.push_back(crater.rimHeight.value_or(std::numeric_limits<double>::quiet_NaN())); | |
| 84 | 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 | 9853137 | if (index >= Size()) { | |
| 101 | 1 | throw std::out_of_range("Crater index out of range"); | |
| 102 | } | ||
| 103 | 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 | 9853136 | UnlessNan(m_rimHeight[index]), | |
| 111 | 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 | 1812441 | return !((crater.manual && !crater.replace && !manual) || crater.diameter < minDiameter || | |
| 120 | 1336490 | crater.diameter > maxDiameter || crater.ageGyr < minAgeGyr || | |
| 121 | 1336510 | crater.ageGyr > maxAgeGyr); | |
| 122 | } | ||
| 123 | |||
| 124 | 161504 | void ValidateCraterSelection(const CraterSelection& selection, std::string_view what) { | |
| 125 | 161504 | if (!(selection.minDiameter >= 0.0 && selection.maxDiameter >= selection.minDiameter && | |
| 126 | 161500 | selection.minAgeGyr >= 0.0 && selection.maxAgeGyr >= selection.minAgeGyr)) { | |
| 127 | 24 | throw std::invalid_argument(std::string(what) + | |
| 128 | 24 | " size and age ranges must satisfy 0 <= min <= max"); | |
| 129 | } | ||
| 130 | 161496 | } | |
| 131 | |||
| 132 | } // namespace terraingen | ||
| 133 |