src/layers/craters/generation/spacing.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/craters/generation/spacing.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/craters/band_buckets.h" | ||
| 6 | #include "terraingen/layers/craters/crater_fields.h" | ||
| 7 | |||
| 8 | #include <cmath> | ||
| 9 | |||
| 10 | namespace terraingen { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | /// Finds whether crater `n` conflicts with a crater that takes precedence over it, through | ||
| 15 | /// buckets per diameter band at least as wide as any distance at which a crater can conflict. | ||
| 16 | 86 | class ConflictSearch { | |
| 17 | public: | ||
| 18 | 43 | ConflictSearch(const CraterList& craters, const CraterSpacing& spacing, const Surface& surface) | |
| 19 | 43 | : m_craters(craters), m_spacing(spacing), m_surface(surface), m_buckets(surface) { | |
| 20 | 29937 | for (std::size_t n = 0; n < craters.Size(); ++n) { | |
| 21 | 29894 | const Crater crater = craters.At(n); | |
| 22 | 29894 | const int band = std::ilogb(crater.diameter); | |
| 23 | 29894 | m_buckets.Add(band, | |
| 24 | 29894 | spacing.separation * spacing.diameterRatio * std::ldexp(1.0, band + 1), | |
| 25 | 29894 | {crater.x, crater.y}, n); | |
| 26 | } | ||
| 27 | 43 | } | |
| 28 | |||
| 29 | 29872 | bool Outranked(std::size_t n) const { | |
| 30 | 29872 | const Crater crater = m_craters.At(n); | |
| 31 | 29872 | const int lowest = std::ilogb(crater.diameter / m_spacing.diameterRatio); | |
| 32 | 29872 | const int highest = std::ilogb(crater.diameter * m_spacing.diameterRatio); | |
| 33 | 29872 | bool outranked = false; | |
| 34 | 101017 | for (int band = lowest; band <= highest && !outranked; ++band) { | |
| 35 | 142290 | m_buckets.ForEachAround(band, {crater.x, crater.y}, 1, | |
| 36 | 71145 | [&](const std::vector<std::size_t>& others) { | |
| 37 | 306377 | outranked = outranked || OutrankedBy(others, n, crater); | |
| 38 | 306377 | }); | |
| 39 | } | ||
| 40 | 29872 | return outranked; | |
| 41 | } | ||
| 42 | |||
| 43 | private: | ||
| 44 | /// Whether one of `others` conflicts with `crater` and takes precedence over it. | ||
| 45 | 273199 | bool OutrankedBy(const std::vector<std::size_t>& others, std::size_t n, | |
| 46 | const Crater& crater) const { | ||
| 47 | 840652 | for (const std::size_t other : others) { | |
| 48 | 578145 | if (other == n) { | |
| 49 | 24429 | continue; | |
| 50 | } | ||
| 51 | 553716 | const Crater neighbor = m_craters.At(other); | |
| 52 | 553716 | if (CratersConflict(crater, neighbor, m_spacing, m_surface) && | |
| 53 | 24585 | CraterTakesPrecedence(neighbor, crater, m_spacing.outrankFrom)) { | |
| 54 | 10692 | return true; | |
| 55 | } | ||
| 56 | } | ||
| 57 | return false; | ||
| 58 | } | ||
| 59 | |||
| 60 | const CraterList& m_craters; | ||
| 61 | const CraterSpacing& m_spacing; | ||
| 62 | const Surface& m_surface; | ||
| 63 | BandBuckets m_buckets; | ||
| 64 | }; | ||
| 65 | |||
| 66 | } // namespace | ||
| 67 | |||
| 68 | 266036 | void ValidateCraterSpacing(const CraterSpacing& spacing) { | |
| 69 | 266036 | CheckRanges(spacing); | |
| 70 | 265969 | } | |
| 71 | |||
| 72 | 1174178 | bool CratersConflict(const Crater& a, const Crater& b, const CraterSpacing& spacing, | |
| 73 | const Surface& surface) { | ||
| 74 | 1174178 | const double larger = std::fmax(a.diameter, b.diameter); | |
| 75 | 1174178 | const double smaller = std::fmin(a.diameter, b.diameter); | |
| 76 | 1174178 | if (larger > spacing.diameterRatio * smaller) { | |
| 77 | return false; | ||
| 78 | } | ||
| 79 | 692346 | const double limit = spacing.separation * larger; | |
| 80 | 692346 | if (surface.IsBody()) { | |
| 81 | 531 | return surface.Distance({a.x, a.y}, {b.x, b.y}) < limit; | |
| 82 | } | ||
| 83 | 691815 | const double dx = a.x - b.x; | |
| 84 | 691815 | const double dy = a.y - b.y; | |
| 85 | 691815 | return dx * dx + dy * dy < limit * limit; | |
| 86 | } | ||
| 87 | |||
| 88 | 25018 | bool CraterTakesPrecedence(const Crater& a, const Crater& b, double outrankFrom) { | |
| 89 | 25018 | if (a.manual != b.manual) { | |
| 90 | return a.manual; | ||
| 91 | } | ||
| 92 | 25011 | if ((a.diameter >= outrankFrom) != (b.diameter >= outrankFrom)) { | |
| 93 | return a.diameter >= outrankFrom; | ||
| 94 | } | ||
| 95 | 24561 | if (a.ageGyr != b.ageGyr) { | |
| 96 | 20011 | return a.ageGyr < b.ageGyr; | |
| 97 | } | ||
| 98 | 4550 | if (a.diameter != b.diameter) { | |
| 99 | 4545 | return a.diameter > b.diameter; | |
| 100 | } | ||
| 101 | 5 | return a.id < b.id; | |
| 102 | } | ||
| 103 | |||
| 104 | 4730 | double SpacingReach(double diameter, double maxDiameter, const CraterSpacing& spacing) { | |
| 105 | 4730 | return spacing.separation * std::fmin(spacing.diameterRatio * diameter, maxDiameter); | |
| 106 | } | ||
| 107 | |||
| 108 | 48 | std::vector<bool> SpacingConflicts(const CraterList& craters, const CraterSpacing& spacing, | |
| 109 | const Surface& surface) { | ||
| 110 | 48 | ValidateCraterSpacing(spacing); | |
| 111 | 45 | std::vector<bool> conflicts(craters.Size(), false); | |
| 112 | 45 | if (!spacing.Enabled() || craters.Size() < 2) { | |
| 113 | return conflicts; | ||
| 114 | } | ||
| 115 | 43 | const ConflictSearch search(craters, spacing, surface); | |
| 116 | 29937 | for (std::size_t n = 0; n < craters.Size(); ++n) { | |
| 117 | 29894 | conflicts[n] = !craters.At(n).manual && search.Outranked(n); | |
| 118 | } | ||
| 119 | 43 | return conflicts; | |
| 120 | 43 | } | |
| 121 | |||
| 122 | 4 | CraterList ThinBySpacing(const CraterList& craters, const CraterSpacing& spacing, | |
| 123 | const Surface& surface) { | ||
| 124 | 4 | const std::vector<bool> conflicts = SpacingConflicts(craters, spacing, surface); | |
| 125 | 4 | return FilterCraters(craters, [&](std::size_t n, const Crater&) { return !conflicts[n]; }); | |
| 126 | 4 | } | |
| 127 | |||
| 128 | } // namespace terraingen | ||
| 129 |