src/layers/boulders/boulder_options.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/boulders/boulder_options.h" | ||
| 4 | |||
| 5 | #include "terraingen/distribution/sp8023.h" | ||
| 6 | #include "terraingen/layers/boulders/boulder_abundance.h" | ||
| 7 | #include "terraingen/layers/boulders/boulder_fields.h" | ||
| 8 | #include "terraingen/util/bits.h" | ||
| 9 | #include "util/checks.h" | ||
| 10 | |||
| 11 | #include <algorithm> | ||
| 12 | #include <cmath> | ||
| 13 | #include <stdexcept> | ||
| 14 | #include <string> | ||
| 15 | #include <unordered_set> | ||
| 16 | |||
| 17 | namespace terraingen { | ||
| 18 | |||
| 19 | namespace { | ||
| 20 | |||
| 21 | /// Whether `distribution` may be evaluated from `low` to `high` meters. | ||
| 22 | 173 | bool Covers(const SizeFrequencyDistribution& distribution, double low, double high) { | |
| 23 | 173 | return distribution.AllowsExtrapolation() || | |
| 24 | 148 | (distribution.ValidRange().min <= low && distribution.ValidRange().max >= high); | |
| 25 | } | ||
| 26 | |||
| 27 | /// Throws unless `distribution` is a density valid over the boulder sizes. | ||
| 28 | 175 | void CheckDistribution(const SizeFrequencyDistribution& distribution, const BoulderSizes& sizes, | |
| 29 | const char* what) { | ||
| 30 | 175 | if (distribution.Kind() != SfdKind::Density) { | |
| 31 | 6 | throw std::invalid_argument(std::string(what) + " must be a density, not a rate"); | |
| 32 | } | ||
| 33 | 173 | if (!Covers(distribution, sizes.minDiameter, sizes.maxDiameter)) { | |
| 34 | 4 | throw std::invalid_argument(std::string(what) + | |
| 35 | " must be valid from minDiameter to maxDiameter, or allow " | ||
| 36 | 6 | "extrapolation"); | |
| 37 | } | ||
| 38 | 171 | } | |
| 39 | |||
| 40 | /// Checks what boulder sizes tie together, their ranges checked: min < max diameter, and a density | ||
| 41 | /// distribution covering them or a coverage in (0, 1). | ||
| 42 | 463 | void CheckBoulderSizes(const BoulderSizes& sizes) { | |
| 43 | 463 | if (!(sizes.maxDiameter > sizes.minDiameter)) { | |
| 44 | 2 | throw std::invalid_argument("Boulder diameters must satisfy minDiameter < maxDiameter"); | |
| 45 | } | ||
| 46 | 461 | if (sizes.distribution) { | |
| 47 | 147 | CheckDistribution(*sizes.distribution, sizes, "The boulder distribution"); | |
| 48 | 590 | } else if (!(sizes.Coverage() > 0.0 && sizes.Coverage() < 1.0)) { | |
| 49 | 1 | throw std::invalid_argument("Boulder coverage must be between 0 and 1"); | |
| 50 | } | ||
| 51 | 458 | } | |
| 52 | |||
| 53 | /// Rejects Golombek-Rapp terms that a size distribution would leave unused. | ||
| 54 | 458 | void CheckGolombekRappTerms(const BoulderLayerOptions& options) { | |
| 55 | 458 | const BoulderSizes& sizes = options.sizes; | |
| 56 | 458 | const BoulderEjecta& ejecta = options.ejecta; | |
| 57 | 458 | if (sizes.distribution && sizes.coverage) { | |
| 58 | 2 | throw std::invalid_argument( | |
| 59 | "Boulder coverage is only used by Golombek-Rapp sizes; set sizes.distribution to null " | ||
| 60 | 2 | "for them or leave the coverage unset"); | |
| 61 | } | ||
| 62 | 456 | if (ejecta.distribution && ejecta.coverage) { | |
| 63 | 1 | throw std::invalid_argument( | |
| 64 | "Boulder ejecta coverage is only used by Golombek-Rapp blocks; set ejecta.distribution " | ||
| 65 | 1 | "to null for them or leave the coverage unset"); | |
| 66 | } | ||
| 67 | 455 | if (sizes.distribution && ejecta.distribution && | |
| 68 | 131 | (sizes.qConstant || sizes.qCoverageCoefficient)) { | |
| 69 | 1 | throw std::invalid_argument( | |
| 70 | "The Golombek-Rapp qConstant and qCoverageCoefficient are only used by Golombek-Rapp " | ||
| 71 | 1 | "sizes or blocks; leave them unset with size distributions"); | |
| 72 | } | ||
| 73 | 454 | } | |
| 74 | |||
| 75 | /// Checks what crater blocks that craters throw tie together, their ranges checked: a | ||
| 76 | /// distribution that covers `sizes`, or else a coverage. | ||
| 77 | 446 | void CheckBoulderEjecta(const BoulderEjecta& ejecta, const BoulderSizes& sizes) { | |
| 78 | 446 | if (!ejecta.ThrowingDiameter()) { | |
| 79 | return; | ||
| 80 | } | ||
| 81 | 63 | if (ejecta.distribution) { | |
| 82 | 28 | CheckDistribution(*ejecta.distribution, sizes, "The boulder ejecta distribution"); | |
| 83 | 49 | } else if (!(ejecta.Coverage() > 0.0 && ejecta.Coverage() < 1.0)) { | |
| 84 | ✗ | throw std::invalid_argument("Boulder ejecta coverage must be between 0 and 1"); | |
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | /// Checks what a boulder pose ties together, its ranges checked. | ||
| 89 | 471 | void CheckBoulderPose(const BoulderPose& pose) { | |
| 90 | 471 | if (!(pose.maxHeightRatio >= pose.minHeightRatio)) { | |
| 91 | 1 | throw std::invalid_argument("Boulder height ratios must satisfy min <= max"); | |
| 92 | } | ||
| 93 | 470 | if (!(pose.maxBurial >= pose.minBurial)) { | |
| 94 | 1 | throw std::invalid_argument("Boulder burial must satisfy min <= max"); | |
| 95 | } | ||
| 96 | 469 | if (!(pose.sideShare + pose.endShare <= 1.0)) { | |
| 97 | 1 | throw std::invalid_argument("Boulder sideShare and endShare must sum to at most 1"); | |
| 98 | } | ||
| 99 | 468 | if (pose.rollDistance && !pose.maxSlopeDegrees) { | |
| 100 | 1 | throw std::invalid_argument("Boulder rollDistance needs a maxSlopeDegrees"); | |
| 101 | } | ||
| 102 | 467 | } | |
| 103 | |||
| 104 | /// Checks what the drawing of boulders ties together, its ranges checked. | ||
| 105 | 465 | void CheckBoulderRendering(const BoulderRendering& rendering) { | |
| 106 | 465 | const BoulderReliefRange& range = rendering.reliefRange; | |
| 107 | 465 | if (range.maxDiameter && !(*range.maxDiameter > range.minDiameter)) { | |
| 108 | 1 | throw std::invalid_argument("Boulder relief maxDiameter must exceed its minDiameter"); | |
| 109 | } | ||
| 110 | 464 | ValidateBoulderShapeOptions(rendering.shapes); | |
| 111 | 464 | } | |
| 112 | |||
| 113 | } // namespace | ||
| 114 | |||
| 115 | 500520 | bool BoulderModelThreshold::IsModel(double diameter, double spacing) const { | |
| 116 | 500520 | const bool large = (minDiameter && diameter >= *minDiameter) || | |
| 117 | 494186 | (minPixels && spacing > 0.0 && diameter >= *minPixels * spacing); | |
| 118 | 6337 | return large && !(maxDiameter && diameter >= *maxDiameter) && | |
| 119 | 6336 | !(maxPixels && spacing > 0.0 && diameter >= *maxPixels * spacing); | |
| 120 | } | ||
| 121 | |||
| 122 | 1691 | std::shared_ptr<const SizeFrequencyDistribution> DefaultBoulderSizes() { | |
| 123 | 1691 | static const std::shared_ptr<const SizeFrequencyDistribution> sizes = | |
| 124 | 1867 | std::make_shared<TabulatedSfd>(Sp8023IntercraterBlocks(SpecificationCurve::Nominal)); | |
| 125 | 3382 | return sizes; | |
| 126 | } | ||
| 127 | |||
| 128 | 1357 | std::optional<double> BoulderEjecta::ThrowingDiameter() const { | |
| 129 | 1357 | const double excavated = regolithThickness ? *regolithThickness / excavationDepthRatio : 0.0; | |
| 130 | 1357 | const double smallest = std::fmax(minCraterDiameter.value_or(0.0), excavated); | |
| 131 | 1357 | return smallest > 0.0 ? std::optional<double>(smallest) : std::nullopt; | |
| 132 | } | ||
| 133 | |||
| 134 | 9 | double BoulderThrowDistance(double craterDiameter, double regolithThickness, double gravity) { | |
| 135 | 32 | for (const double value : {craterDiameter, regolithThickness, gravity}) { | |
| 136 | 25 | RequirePositive(value, "Crater diameter, regolith thickness and gravity"); | |
| 137 | } | ||
| 138 | 7 | return 138.5 / gravity * std::pow(craterDiameter / regolithThickness, 0.7086); | |
| 139 | } | ||
| 140 | |||
| 141 | 3 | void ValidateBoulderPatches(const BoulderPatches& patches) { | |
| 142 | 3 | CheckRanges(patches); | |
| 143 | 1 | } | |
| 144 | |||
| 145 | 3 | void ValidateBoulderFillet(const BoulderFillet& fillet) { | |
| 146 | 3 | CheckRanges(fillet); | |
| 147 | } | ||
| 148 | |||
| 149 | 482 | void ValidateBoulderClusters(const BoulderClusters& clusters) { | |
| 150 | 482 | CheckRanges(clusters); | |
| 151 | 482 | if (clusters.model == BoulderClusterModel::Thomas) { | |
| 152 | 8 | if (!(clusters.radius <= 2.0 * clusters.centerSpacing)) { | |
| 153 | 1 | throw std::invalid_argument("Thomas boulder clusters must satisfy radius <= 2 spacing"); | |
| 154 | } | ||
| 155 | 474 | } else if (!(clusters.radius < clusters.centerSpacing)) { | |
| 156 | 2 | throw std::invalid_argument("Boulder clusters must satisfy radius < spacing"); | |
| 157 | } | ||
| 158 | 479 | } | |
| 159 | |||
| 160 | 15 | void ValidateBoulderFragmentation(const BoulderFragmentation& fragmentation) { | |
| 161 | 15 | CheckRanges(fragmentation); | |
| 162 | 12 | if (!(fragmentation.rubbleWear >= fragmentation.splitWear)) { | |
| 163 | 1 | throw std::invalid_argument("Boulder fragmentation must satisfy splitWear <= rubbleWear"); | |
| 164 | } | ||
| 165 | 11 | if (!(fragmentation.maxPieces >= fragmentation.minPieces)) { | |
| 166 | ✗ | throw std::invalid_argument("Boulder fragments must satisfy minPieces <= maxPieces"); | |
| 167 | } | ||
| 168 | 11 | } | |
| 169 | |||
| 170 | 476 | void ValidateBoulderVentifacts(const BoulderVentifacts& ventifacts) { | |
| 171 | 476 | CheckRanges(ventifacts); | |
| 172 | 469 | if (!(ventifacts.minAngleDegrees <= ventifacts.maxAngleDegrees)) { | |
| 173 | 2 | throw std::invalid_argument( | |
| 174 | 2 | "Boulder ventifact facet angles must satisfy minAngleDegrees <= maxAngleDegrees"); | |
| 175 | } | ||
| 176 | 467 | } | |
| 177 | |||
| 178 | 2 | BoulderVentifacts GaleVentifacts() { | |
| 179 | 2 | BoulderVentifacts ventifacts; | |
| 180 | 2 | ventifacts.share = 0.5 * 0.78; | |
| 181 | 2 | ventifacts.keelShare = 12.0 / 39.0; | |
| 182 | 2 | return ventifacts; | |
| 183 | } | ||
| 184 | |||
| 185 | 50389 | double FilletReach(const Boulder& boulder, const BoulderFillet& fillet) { | |
| 186 | 50389 | return (0.5 + fillet.widthRatio) * boulder.diameter; | |
| 187 | } | ||
| 188 | |||
| 189 | 11 | FilletContact DomeFilletContact(const Boulder& boulder, const BoulderFillet& fillet) { | |
| 190 | 11 | const double share = std::clamp(fillet.heightShare, 0.0, 1.0); | |
| 191 | 11 | return {0.5 * boulder.diameter * std::sqrt(1.0 - share * share), | |
| 192 | 11 | share * boulder.ExposedHeight()}; | |
| 193 | } | ||
| 194 | |||
| 195 | 207717 | double FilletHeight(const Boulder& boulder, double distance, const BoulderFillet& fillet, | |
| 196 | const FilletContact& contact) { | ||
| 197 | 207717 | if (distance < contact.distance || !(contact.height > 0.0)) { | |
| 198 | return 0.0; | ||
| 199 | } | ||
| 200 | 170027 | const double reach = | |
| 201 | 170027 | std::fmax(0.5 * boulder.diameter, contact.distance) + fillet.widthRatio * boulder.diameter; | |
| 202 | 170027 | const double t = (distance - contact.distance) / (reach - contact.distance); | |
| 203 | 170027 | return t >= 1.0 ? 0.0 : contact.height * (1.0 - t) * (1.0 - t); | |
| 204 | } | ||
| 205 | |||
| 206 | 9 | double FilletHeight(const Boulder& boulder, double distance, const BoulderFillet& fillet) { | |
| 207 | 9 | return FilletHeight(boulder, distance, fillet, DomeFilletContact(boulder, fillet)); | |
| 208 | } | ||
| 209 | |||
| 210 | 139 | void ValidateManualBoulder(const ManualBoulder& boulder) { | |
| 211 | 139 | CheckRanges(boulder); | |
| 212 | 114 | } | |
| 213 | |||
| 214 | 9785 | std::uint64_t ManualBoulderId(const ManualBoulder& boulder) { | |
| 215 | 9785 | return boulder.id.value_or(0u) != 0 ? *boulder.id | |
| 216 | 9746 | : ManualCraterId(boulder.x, boulder.y, boulder.diameter); | |
| 217 | } | ||
| 218 | |||
| 219 | 536 | void ValidateBoulderLayerOptions(const BoulderLayerOptions& options) { | |
| 220 | 536 | CheckRanges(options); | |
| 221 | 506 | std::unordered_set<std::uint64_t> ids; | |
| 222 | 603 | for (const ManualBoulder& boulder : options.manual) { | |
| 223 | 113 | ValidateManualBoulder(boulder); | |
| 224 | 143 | if (!ids.insert(ManualBoulderId(boulder)).second) { | |
| 225 | 4 | throw std::invalid_argument("Manual boulders must not share an ID"); | |
| 226 | } | ||
| 227 | } | ||
| 228 | 490 | if (options.generateRandom) { | |
| 229 | 463 | CheckBoulderSizes(options.sizes); | |
| 230 | 458 | CheckGolombekRappTerms(options); | |
| 231 | } | ||
| 232 | 481 | if (options.densityMap) { | |
| 233 | 8 | ValidateDensityMap(*options.densityMap); | |
| 234 | } | ||
| 235 | 481 | if (options.slopeMap) { | |
| 236 | 9 | ValidateSizeSlopeMap(*options.slopeMap); | |
| 237 | 9 | if (options.generateRandom && !options.sizes.distribution) { | |
| 238 | 1 | throw std::invalid_argument( | |
| 239 | 1 | "A boulder size slope map needs a size distribution, not Golombek-Rapp sizes"); | |
| 240 | } | ||
| 241 | } | ||
| 242 | 480 | ValidateBoulderClusters(options.clusters); | |
| 243 | 478 | if (options.fragmentation) { | |
| 244 | 12 | ValidateBoulderFragmentation(*options.fragmentation); | |
| 245 | } | ||
| 246 | 477 | if (options.bedrock.exposure) { | |
| 247 | 18 | ValidateExposureParams(*options.bedrock.exposure); | |
| 248 | } | ||
| 249 | 477 | if (options.generateRandom) { | |
| 250 | 450 | if (!options.sizes.distribution && | |
| 251 | 584 | options.sizes.Coverage() * MaxBackgroundAbundance(options) >= 1.0) { | |
| 252 | 4 | throw std::invalid_argument("Boulder coverage times the patch spread, cluster and " | |
| 253 | 4 | "bedrock abundance must stay below 1"); | |
| 254 | } | ||
| 255 | 446 | CheckBoulderEjecta(options.ejecta, options.sizes); | |
| 256 | } | ||
| 257 | 471 | CheckBoulderPose(options.pose); | |
| 258 | 467 | ValidateBoulderVentifacts(options.ventifacts); | |
| 259 | 466 | ValidateResolutionFade(options.fade); | |
| 260 | 465 | CheckBoulderRendering(options.rendering); | |
| 261 | 464 | } | |
| 262 | } // namespace terraingen | ||
| 263 |