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 | 360 | if (const auto* diffused = dynamic_cast<const DiffusedProfile*>(shape)) { | |
| 36 | 150 | shape = &diffused->Fresh(); | |
| 37 | } | ||
| 38 | 360 | if (const auto* complex = dynamic_cast<const ComplexCraterProfile*>(shape)) { | |
| 39 | 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 | 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 | 14 | if (crater.rimHeight) { | |
| 66 | 8 | throw std::invalid_argument(CraterName(crater) + | |
| 67 | 8 | " has a rim height, which the Fassett-Thomson profile fixes" + | |
| 68 | 12 | honored); | |
| 69 | } | ||
| 70 | 10 | const double deepest = fresh.rimHeightRatio - FitFloorRatio(profile, crater.diameter, fresh); | |
| 71 | 10 | if (*crater.depth / crater.diameter > deepest * (1.0 + 1e-12)) { | |
| 72 | 1 | throw std::invalid_argument(CraterName(crater) + | |
| 73 | 2 | " is deeper than the Fassett-Thomson fit (" + | |
| 74 | 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 | 150 | if (crater.rimHeight) { | |
| 83 | 1 | throw std::invalid_argument( | |
| 84 | 2 | CraterName(crater) + " has a rim height, which a diffused profile ties to its depth; " | |
| 85 | 3 | "give the depth alone"); | |
| 86 | } | ||
| 87 | 149 | const double diameter = crater.diameter; | |
| 88 | 149 | double tau = 0.0; | |
| 89 | 149 | if (depth < fresh.depthRatio) { | |
| 90 | 114 | const double oldest = | |
| 91 | 114 | profile.Morphometry(fresh, diameter, profile.Ages().back()).depthRatio; | |
| 92 | 114 | if (oldest > depth) { | |
| 93 | 2 | throw std::invalid_argument(CraterName(crater) + | |
| 94 | 4 | " is shallower than diffusion wears it (" + | |
| 95 | 8 | std::to_string(oldest * diameter) + " m)"); | |
| 96 | } | ||
| 97 | 112 | tau = profile.AgeAtDepthRatio(fresh, diameter, depth); | |
| 98 | } | ||
| 99 | 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 | 362563 | if (!crater.depth && !crater.rimHeight) { | |
| 111 | 362219 | return rate > 1.0 ? degradation.WornState(crater, rate) : degradation.State(crater); | |
| 112 | } | ||
| 113 | 344 | if (!(crater.depth && IsPositive(*crater.depth) && | |
| 114 | 342 | (!crater.rimHeight || IsNonNegative(*crater.rimHeight)))) { | |
| 115 | 6 | throw std::invalid_argument(CraterName(crater) + | |
| 116 | " needs a positive depth, and a rim height that is unset or " | ||
| 117 | 9 | "not negative"); | |
| 118 | } | ||
| 119 | 341 | CraterMorphometry fresh = degradation.State(crater).fresh; | |
| 120 | 341 | const double diameter = crater.diameter; | |
| 121 | 341 | const double depth = *crater.depth / diameter; | |
| 122 | 682 | if (const auto* ft = | |
| 123 | 341 | dynamic_cast<const FassettThomsonProfile*>(&FreshShape(profile, diameter))) { | |
| 124 | 14 | CheckFassettThomson(*ft, crater, fresh); | |
| 125 | } | ||
| 126 | 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 | 186 | if (crater.rimHeight) { | |
| 131 | 95 | current.rimHeightRatio = *crater.rimHeight / diameter; | |
| 132 | } else { | ||
| 133 | 91 | const double kept = | |
| 134 | 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 | 52 | return dynamic_cast<const DiffusedProfile*>(&profile) == nullptr && | |
| 144 | 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 |