src/layers/craters/shaping/apply_options.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/craters/shaping/apply_options.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/craters/crater_fields.h" | ||
| 6 | #include "terraingen/random/stream.h" | ||
| 7 | #include "terraingen/random/streams.h" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <cstdint> | ||
| 11 | #include <optional> | ||
| 12 | #include <stdexcept> | ||
| 13 | |||
| 14 | namespace terraingen { | ||
| 15 | |||
| 16 | 1 | void ValidateRimSmoothing(const RimSmoothing& smoothing) { | |
| 17 | 1 | CheckRanges(smoothing); | |
| 18 | } | ||
| 19 | |||
| 20 | 161350 | void ValidateCraterTerraces(const CraterTerraces& terraces) { | |
| 21 | 161350 | CheckRanges(terraces); | |
| 22 | 161667 | if (!(terraces.fullDiameter > terraces.onsetDiameter)) { | |
| 23 | 1 | throw std::invalid_argument( | |
| 24 | 1 | "Crater terrace full diameter must be above the onset diameter"); | |
| 25 | } | ||
| 26 | 161666 | if (terraces.minBenches > terraces.maxBenches) { | |
| 27 | 1 | throw std::invalid_argument("Crater terrace min_benches must be at most max_benches"); | |
| 28 | } | ||
| 29 | 161665 | if (!(terraces.scarpSlopeDegrees > terraces.benchSlopeDegrees)) { | |
| 30 | 1 | throw std::invalid_argument("Crater terrace scarp slope must be above the bench slope"); | |
| 31 | } | ||
| 32 | 161664 | } | |
| 33 | |||
| 34 | 161003 | void ValidateCraterPeaks(const CraterPeaks& peaks) { | |
| 35 | 161003 | CheckRanges(peaks); | |
| 36 | 161715 | if (peaks.minMassifs > peaks.maxMassifs) { | |
| 37 | 1 | throw std::invalid_argument("Crater peak min_massifs must be at most max_massifs"); | |
| 38 | } | ||
| 39 | 161714 | } | |
| 40 | |||
| 41 | 5 | void ValidateCraterNoise(const CraterNoise& noise) { | |
| 42 | 5 | CheckRanges(noise); | |
| 43 | } | ||
| 44 | |||
| 45 | 4 | void ValidateCraterTexture(const CraterTexture& texture) { | |
| 46 | 4 | CheckRanges(texture); | |
| 47 | } | ||
| 48 | |||
| 49 | 1298 | CraterApplyOptions DefaultCraterApply() { | |
| 50 | 1298 | CraterApplyOptions apply; | |
| 51 | 1298 | apply.irregularity.enabled = true; | |
| 52 | 1298 | apply.terraces.enabled = true; | |
| 53 | 1298 | apply.peaks.enabled = true; | |
| 54 | 1298 | return apply; | |
| 55 | } | ||
| 56 | |||
| 57 | 1021 | CraterApplyOptions SeededCraterApply(CraterApplyOptions apply, std::uint32_t seed) { | |
| 58 | 7147 | const auto follow = [seed](std::optional<std::uint32_t>& own, std::uint32_t stream) { | |
| 59 | 6126 | if (!own) { | |
| 60 | 6095 | own = SubstreamSeed(seed, stream); | |
| 61 | } | ||
| 62 | 7147 | }; | |
| 63 | 1021 | follow(apply.irregularity.seed, kCraterIrregularityStream); | |
| 64 | 1021 | follow(apply.noise.seed, kCraterNoiseStream); | |
| 65 | 1021 | follow(apply.texture.seed, kCraterTextureStream); | |
| 66 | 1021 | follow(apply.slope.seed, kCraterSlopeStream); | |
| 67 | 1021 | follow(apply.terraces.seed, kCraterTerraceStream); | |
| 68 | 1021 | follow(apply.peaks.seed, kCraterPeakStream); | |
| 69 | 1021 | return apply; | |
| 70 | } | ||
| 71 | |||
| 72 | 161464 | void ValidateCraterReplacement(const CraterReplacement& replacement) { | |
| 73 | 161464 | CheckRanges(replacement); | |
| 74 | 161671 | if (replacement.endRadii && !(*replacement.endRadii > replacement.startRadii)) { | |
| 75 | 3 | throw std::invalid_argument("Crater replacement end_radii must be above start_radii"); | |
| 76 | } | ||
| 77 | 161668 | } | |
| 78 | |||
| 79 | 161605 | void ValidateCraterApplyOptions(const CraterApplyOptions& options) { | |
| 80 | 161605 | CheckRanges(options); | |
| 81 | 161554 | ValidateResolutionFade(options.fade); | |
| 82 | 161734 | ValidateCraterIrregularity(options.irregularity); | |
| 83 | 161616 | ValidateCraterSlopeResponse(options.slope); | |
| 84 | 161421 | ValidateCraterTerraces(options.terraces); | |
| 85 | 161627 | ValidateCraterPeaks(options.peaks); | |
| 86 | 161708 | ValidateCraterReplacement(options.replacement); | |
| 87 | 161325 | if ((options.slope.asymmetric || options.slope.criticalGradient) && | |
| 88 | ✗ | options.datum != CraterDatum::Plane) { | |
| 89 | 3 | throw std::invalid_argument("A crater slope response needs the plane datum"); | |
| 90 | } | ||
| 91 | 161322 | if (!(options.blend.endRadii > options.blend.startRadii)) { | |
| 92 | 1 | throw std::invalid_argument("Crater blend end_radii must be above start_radii"); | |
| 93 | } | ||
| 94 | 161321 | } | |
| 95 | |||
| 96 | 77458 | std::vector<std::size_t> CraterApplyOrder(const CraterList& craters, CraterOrder order) { | |
| 97 | 77458 | struct Key { | |
| 98 | double age; | ||
| 99 | double diameter; | ||
| 100 | std::uint64_t id; | ||
| 101 | std::size_t index; | ||
| 102 | }; | ||
| 103 | 77458 | const auto& age = craters.AgesGyr(); | |
| 104 | 77458 | const auto& diameter = craters.Diameters(); | |
| 105 | 77458 | const auto& id = craters.Ids(); | |
| 106 | 77458 | std::vector<Key> keys(craters.Size()); | |
| 107 | 659800 | for (std::size_t n = 0; n < keys.size(); ++n) { | |
| 108 | 582350 | keys[n] = {age[n], diameter[n], id[n], n}; | |
| 109 | } | ||
| 110 | 77450 | const bool oldestFirst = order == CraterOrder::OldestFirst; | |
| 111 | 77450 | std::sort(keys.begin(), keys.end(), [oldestFirst](const Key& a, const Key& b) { | |
| 112 | 4483047 | if (oldestFirst && a.age != b.age) { | |
| 113 | 846502 | return a.age > b.age; | |
| 114 | } | ||
| 115 | 3636545 | if (a.diameter != b.diameter) { | |
| 116 | 3633042 | return a.diameter > b.diameter; | |
| 117 | } | ||
| 118 | 3503 | if (a.age != b.age) { | |
| 119 | 3 | return a.age > b.age; | |
| 120 | } | ||
| 121 | 3500 | return a.id != b.id ? a.id < b.id : a.index < b.index; | |
| 122 | }); | ||
| 123 | 77510 | std::vector<std::size_t> indices(keys.size()); | |
| 124 | 660246 | for (std::size_t n = 0; n < keys.size(); ++n) { | |
| 125 | 582732 | indices[n] = keys[n].index; | |
| 126 | } | ||
| 127 | 77506 | return indices; | |
| 128 | 77514 | } | |
| 129 | |||
| 130 | } // namespace terraingen | ||
| 131 |