src/layers/craters/generation/population.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/craters/generation/population.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/craters/generation/spacing.h" | ||
| 6 | #include "terraingen/random/philox.h" | ||
| 7 | #include "terraingen/random/samplers.h" | ||
| 8 | #include "terraingen/util/log.h" | ||
| 9 | #include "terraingen/util/parallel.h" | ||
| 10 | #include "util/checks.h" | ||
| 11 | #include "util/numerics.h" | ||
| 12 | |||
| 13 | #include <algorithm> | ||
| 14 | #include <cmath> | ||
| 15 | #include <memory> | ||
| 16 | #include <stdexcept> | ||
| 17 | #include <string> | ||
| 18 | #include <utility> | ||
| 19 | |||
| 20 | namespace terraingen { | ||
| 21 | |||
| 22 | namespace { | ||
| 23 | |||
| 24 | 2696222 | std::uint64_t CraterId(std::uint32_t seed, const CraterBand& band, const CellIndex& cell, | |
| 25 | std::uint32_t index) { | ||
| 26 | 2696222 | const U32x4 block = | |
| 27 | 2696222 | Philox4x32({static_cast<std::uint32_t>(cell.i), static_cast<std::uint32_t>(cell.j), | |
| 28 | 2696222 | static_cast<std::uint32_t>(band.exponent), index}, | |
| 29 | {seed, kCraterIdStream}); | ||
| 30 | 2696222 | return JoinWords(block.x, block.y); | |
| 31 | } | ||
| 32 | |||
| 33 | } // namespace | ||
| 34 | |||
| 35 | 264189 | void ValidateCraterGenerationOptions(const CraterGenerationOptions& options) { | |
| 36 | 264189 | ValidateCraterSpacing(options.spacing); | |
| 37 | 264655 | RequirePositive(options.minDiameter, "minDiameter"); | |
| 38 | 264517 | if (!(options.maxDiameter > options.minDiameter)) { | |
| 39 | 2 | throw std::invalid_argument("maxDiameter must exceed minDiameter"); | |
| 40 | } | ||
| 41 | 264515 | RequireNonNegative(options.marginRadii, "marginRadii"); | |
| 42 | 264640 | RequirePositive(options.cellSizeFactor, "cellSizeFactor"); | |
| 43 | 264630 | if (!(options.maxExpectedCraters >= 0.0)) { | |
| 44 | 2 | throw std::invalid_argument("maxExpectedCraters must not be negative"); | |
| 45 | } | ||
| 46 | 264628 | RequireNonNegative(options.minResolvedDiameter, "minResolvedDiameter"); | |
| 47 | 264644 | if (!(options.maxBandDiameter > 0.0)) { | |
| 48 | 1 | throw std::invalid_argument("maxBandDiameter must be positive"); | |
| 49 | } | ||
| 50 | 264643 | } | |
| 51 | |||
| 52 | namespace { | ||
| 53 | |||
| 54 | /// One band of a generation: the cells drawn and the craters they are expected to hold. | ||
| 55 | 710948 | struct BandPlan { | |
| 56 | CraterBand band; | ||
| 57 | std::vector<CellIndex> cells; | ||
| 58 | double expected = 0.0; | ||
| 59 | }; | ||
| 60 | |||
| 61 | /// What a generation keeps and drops, for its log line. | ||
| 62 | struct GenerationCounts { | ||
| 63 | std::size_t bands = 0; | ||
| 64 | double expected = 0.0; | ||
| 65 | std::size_t removed = 0; ///< By spacing. | ||
| 66 | std::size_t capped = 0; ///< Over `maxCraters`. | ||
| 67 | }; | ||
| 68 | |||
| 69 | /// One call of `GenerateCraters`, in steps. | ||
| 70 | class Generation { | ||
| 71 | public: | ||
| 72 | 264704 | Generation(const SurfaceCells& cells, const Bounds2d& region, | |
| 73 | const SizeFrequencyDistribution& density, const CraterAgeModel& ages, | ||
| 74 | std::uint32_t seed, const CraterGenerationOptions& options) | ||
| 75 | 264704 | : m_cells(cells), m_region(region), m_density(density), m_ages(ages), m_seed(seed), | |
| 76 | 264704 | m_options(options), m_surface(options.surface) { | |
| 77 | 264704 | if (density.Kind() != SfdKind::Density) { | |
| 78 | 1 | throw std::invalid_argument("Craters are generated from a Density distribution"); | |
| 79 | } | ||
| 80 | 264703 | m_maxDiameter = ResolveMaxDiameter(options.maxDiameter, density); | |
| 81 | 264703 | if (std::isinf(m_maxDiameter)) { | |
| 82 | 1 | throw std::invalid_argument("An unbounded density needs a finite maxDiameter"); | |
| 83 | } | ||
| 84 | 264702 | if (!(m_maxDiameter > options.minDiameter)) { | |
| 85 | 1 | throw std::invalid_argument("maxDiameter must exceed minDiameter"); | |
| 86 | } | ||
| 87 | 264701 | } | |
| 88 | |||
| 89 | 264264 | CraterList Run(GenerationCounts& counts) const { | |
| 90 | 264264 | const std::vector<BandPlan> plan = PlanBands(); | |
| 91 | 264749 | counts.bands = plan.size(); | |
| 92 | 975929 | for (const BandPlan& entry : plan) { | |
| 93 | 711180 | counts.expected += entry.expected; | |
| 94 | } | ||
| 95 | 264749 | if (counts.expected > m_options.maxExpectedCraters) { | |
| 96 | 2 | throw std::invalid_argument("Expected crater count " + std::to_string(counts.expected) + | |
| 97 | 3 | " exceeds maxExpectedCraters"); | |
| 98 | } | ||
| 99 | 264748 | CraterList craters = DrawPlannedCells(plan, counts.expected); | |
| 100 | 264696 | const std::size_t generated = craters.Size(); | |
| 101 | 264699 | AppendManualCraters(m_options.manual, craters); | |
| 102 | 264816 | if (m_options.spacing.Enabled()) { | |
| 103 | 36 | craters = RemoveSpacingConflicts(craters, counts.removed); | |
| 104 | 264780 | } else if (m_options.manual.Size() > 0) { | |
| 105 | 52 | craters = CropToRegion(craters, generated); | |
| 106 | } | ||
| 107 | 529582 | return KeepLargest(craters, counts.capped); | |
| 108 | 264538 | } | |
| 109 | |||
| 110 | 264707 | void Log(std::size_t kept, const GenerationCounts& counts, const Stopwatch& stopwatch) const { | |
| 111 | 264707 | if (!LogEnabled(LogLevel::Debug)) { | |
| 112 | return; | ||
| 113 | } | ||
| 114 | 37 | const bool spaced = m_options.spacing.Enabled(); | |
| 115 | 37 | const std::size_t manual = m_options.manual.Size(); | |
| 116 | 74 | LogDebug(m_options.logName, "Generated ", kept, " ", m_options.logName, " (", | |
| 117 | 37 | m_options.minDiameter, "-", m_maxDiameter, " m, ", counts.bands, " bands, ", | |
| 118 | 37 | static_cast<long long>(std::llround(counts.expected)), " expected", | |
| 119 | 76 | spaced ? ", " + std::to_string(counts.removed) + " removed by spacing" : "", | |
| 120 | 76 | manual > 0 ? ", " + std::to_string(manual) + " manual supplied" : "", | |
| 121 | 37 | counts.capped > 0 | |
| 122 | 76 | ? ", " + std::to_string(counts.capped) + " over maxCraters dropped" | |
| 123 | : "", | ||
| 124 | 74 | ") in ", FormatDuration(stopwatch.Milliseconds())); | |
| 125 | } | ||
| 126 | |||
| 127 | private: | ||
| 128 | /// How far beyond the region a crater `diameter` across is drawn: its margin, and with spacing | ||
| 129 | /// as far again as the craters it can conflict with reach. | ||
| 130 | 2458757 | double CandidateMargin(double diameter) const { | |
| 131 | 2458757 | const CraterSpacing& spacing = m_options.spacing; | |
| 132 | 2458757 | if (!spacing.Enabled()) { | |
| 133 | 2454030 | return 0.5 * m_options.marginRadii * diameter; | |
| 134 | } | ||
| 135 | 4727 | const double neighbor = std::fmin(spacing.diameterRatio * diameter, m_maxDiameter); | |
| 136 | 4727 | return SpacingReach(diameter, m_maxDiameter, spacing) + | |
| 137 | 4727 | 0.5 * m_options.marginRadii * neighbor; | |
| 138 | } | ||
| 139 | |||
| 140 | /// Whether `crater` is centered in the region or within its margin of it. | ||
| 141 | 357713 | bool InRegion(const Crater& crater, double margin) const { | |
| 142 | 357713 | return m_surface.Contains(m_surface.Expand(m_region, margin), {crater.x, crater.y}); | |
| 143 | } | ||
| 144 | |||
| 145 | 264450 | std::vector<BandPlan> PlanBands() const { | |
| 146 | 264450 | const CraterSpacing& spacing = m_options.spacing; | |
| 147 | 264450 | const double skipBelow = | |
| 148 | 264450 | m_options.minResolvedDiameter / (spacing.Enabled() ? spacing.diameterRatio : 1.0); | |
| 149 | 264450 | std::vector<BandPlan> plan; | |
| 150 | 1043632 | for (const CraterBand& band : | |
| 151 | 1043223 | CraterBands(m_options.minDiameter, m_maxDiameter, m_options.cellSizeFactor)) { | |
| 152 | 778866 | const bool tooLarge = | |
| 153 | 778866 | !spacing.Enabled() && !m_options.maxCraters && band.low > m_options.maxBandDiameter; | |
| 154 | 778866 | if (band.high <= skipBelow || tooLarge) { | |
| 155 | 67167 | continue; | |
| 156 | } | ||
| 157 | 711699 | const double margin = CandidateMargin(band.high); | |
| 158 | 710360 | BandPlan entry{ | |
| 159 | 710360 | band, m_cells.CellsCovering(m_surface.Expand(m_region, margin), band.cellSize)}; | |
| 160 | 712234 | const double countPerArea = m_options.bandCount | |
| 161 | 712234 | ? m_options.bandCount(band) | |
| 162 | 257096 | : m_density.ExpectedCount(1.0, band.low, band.high); | |
| 163 | 2629827 | for (const CellIndex& cell : entry.cells) { | |
| 164 | 1918139 | entry.expected += countPerArea * m_cells.CellArea(cell, band.cellSize); | |
| 165 | } | ||
| 166 | 711688 | LogDebug(m_options.logName, "Band [", band.low, ", ", band.high, | |
| 167 | 1 | ") m: ", entry.cells.size(), " cells of ", band.cellSize, " m, ", | |
| 168 | 711688 | entry.expected, " ", m_options.logName, " expected"); | |
| 169 | 711478 | plan.push_back(std::move(entry)); | |
| 170 | 976374 | } | |
| 171 | 264725 | return plan; | |
| 172 | 2 | } | |
| 173 | |||
| 174 | 264772 | CraterList DrawPlannedCells(const std::vector<BandPlan>& plan, double expected) const { | |
| 175 | 264772 | CraterList craters; | |
| 176 | 264706 | craters.Reserve(static_cast<std::size_t>(expected)); | |
| 177 | 264873 | std::vector<BandRegion> regions; | |
| 178 | 264873 | std::vector<std::pair<std::size_t, CellIndex>> cells; | |
| 179 | 976474 | for (std::size_t b = 0; b < plan.size(); ++b) { | |
| 180 | 711684 | regions.push_back(BandBounds(plan[b].band)); | |
| 181 | 2414639 | for (const CellIndex& cell : plan[b].cells) { | |
| 182 | 1703038 | cells.emplace_back(b, cell); | |
| 183 | } | ||
| 184 | } | ||
| 185 | 264790 | if (m_options.threads == 1 || cells.size() < 2) { | |
| 186 | 1965154 | for (const auto& [b, cell] : cells) { | |
| 187 | 1700612 | DrawKept(plan[b].band, regions[b], cell, craters); | |
| 188 | } | ||
| 189 | return craters; | ||
| 190 | } | ||
| 191 | 248 | std::vector<CraterList> kept(cells.size()); | |
| 192 | 248 | ParallelFor(cells.size(), m_options.threads, [&](std::size_t n) { | |
| 193 | 5219 | const auto& [b, cell] = cells[n]; | |
| 194 | 5219 | DrawKept(plan[b].band, regions[b], cell, kept[n]); | |
| 195 | 5223 | }); | |
| 196 | 5471 | for (const CraterList& some : kept) { | |
| 197 | 5223 | craters.Append(some); | |
| 198 | } | ||
| 199 | 248 | return craters; | |
| 200 | 264933 | } | |
| 201 | |||
| 202 | /// The region expanded by the candidate margins of a band's smallest and largest craters, | ||
| 203 | /// which bound every margin in between since `Expand` and `CandidateMargin` only grow. | ||
| 204 | struct BandRegion { | ||
| 205 | CraterBand band; | ||
| 206 | Bounds2d inner; | ||
| 207 | Bounds2d outer; | ||
| 208 | }; | ||
| 209 | |||
| 210 | /// Appends to `craters` those of `cell` of `band` kept in the band's `region`, in the order | ||
| 211 | /// drawn. | ||
| 212 | 1705445 | void DrawKept(const CraterBand& band, const BandRegion& region, const CellIndex& cell, | |
| 213 | CraterList& craters) const { | ||
| 214 | 1705445 | const std::shared_ptr<const CellCraters> drawn = DrawCell(band, cell); | |
| 215 | 1704161 | const CraterList& list = drawn->craters; | |
| 216 | 1704161 | const Bounds2d reach = drawn->maxDiameter > band.high | |
| 217 | 1704161 | ? m_surface.Expand(m_region, CandidateMargin(drawn->maxDiameter)) | |
| 218 | 1704141 | : region.outer; | |
| 219 | 8161165 | for (const std::uint32_t n : drawn->Rows(reach.minY, reach.maxY)) { | |
| 220 | 6456966 | if (OutsideBand(region, list.Diameters()[n], list.X()[n], list.Y()[n])) { | |
| 221 | 3871855 | continue; | |
| 222 | } | ||
| 223 | 2585111 | const Crater crater = list.At(n); | |
| 224 | 2585081 | if (InBandRegion(region, crater) && Kept(crater)) { | |
| 225 | 2325651 | craters.Append(crater); | |
| 226 | } | ||
| 227 | 1699691 | } | |
| 228 | 1706152 | } | |
| 229 | |||
| 230 | 711371 | BandRegion BandBounds(const CraterBand& band) const { | |
| 231 | 711539 | return {band, m_surface.Expand(m_region, CandidateMargin(band.low)), | |
| 232 | 1422917 | m_surface.Expand(m_region, CandidateMargin(band.high))}; | |
| 233 | } | ||
| 234 | |||
| 235 | /// Whether a crater of the band lies outside its outer region, which `InBandRegion` rejects | ||
| 236 | /// first; tested on the list's columns before the crater is copied out. | ||
| 237 | 6450258 | bool OutsideBand(const BandRegion& region, double diameter, double x, double y) const { | |
| 238 | 6450258 | return diameter >= region.band.low && diameter <= region.band.high && | |
| 239 | 6454031 | !m_surface.Contains(region.outer, {x, y}); | |
| 240 | } | ||
| 241 | |||
| 242 | /// `InRegion` with the crater's candidate margin, expanding the region only for craters of the | ||
| 243 | /// band between its inner and outer regions. | ||
| 244 | 2585120 | bool InBandRegion(const BandRegion& region, const Crater& crater) const { | |
| 245 | 2585120 | if (!(crater.diameter >= region.band.low && crater.diameter <= region.band.high)) { | |
| 246 | 41 | return InRegion(crater, CandidateMargin(crater.diameter)); | |
| 247 | } | ||
| 248 | 2585079 | const Vec2d center{crater.x, crater.y}; | |
| 249 | 2585079 | if (!m_surface.Contains(region.outer, center)) { | |
| 250 | return false; | ||
| 251 | } | ||
| 252 | 2585079 | return m_surface.Contains(region.inner, center) || | |
| 253 | 328264 | InRegion(crater, CandidateMargin(crater.diameter)); | |
| 254 | } | ||
| 255 | |||
| 256 | /// Whether a generated crater stays: below the catalog's completeness limit at its center, and | ||
| 257 | /// within the keep share there by a uniform keyed to its ID. | ||
| 258 | 2362095 | bool Kept(const Crater& crater) const { | |
| 259 | 431268 | if (m_options.completeness && | |
| 260 | 2362095 | crater.diameter >= m_options.completeness->ValueAt(crater.x, crater.y)) { | |
| 261 | return false; | ||
| 262 | } | ||
| 263 | 2360815 | if (!m_options.keepShare) { | |
| 264 | return true; | ||
| 265 | } | ||
| 266 | 543683 | const IdWords id = SplitId(crater.id); | |
| 267 | 543683 | RandomStream random(m_seed, kCraterKeepStream, id.low, id.high); | |
| 268 | 543683 | return random.NextDouble() < m_options.keepShare(crater); | |
| 269 | } | ||
| 270 | |||
| 271 | 1705243 | std::shared_ptr<const CellCraters> DrawCell(const CraterBand& band, | |
| 272 | const CellIndex& cell) const { | ||
| 273 | 1705243 | if (m_options.cellCraters) { | |
| 274 | 1686378 | return m_options.cellCraters(band, cell); | |
| 275 | } | ||
| 276 | 18865 | return std::make_shared<const CellCraters>( | |
| 277 | 37729 | GenerateCellCraters(m_cells, band, cell, m_density, m_ages, m_seed)); | |
| 278 | } | ||
| 279 | |||
| 280 | /// The craters within their margin of the region, the first `generated` of which already are. | ||
| 281 | 52 | CraterList CropToRegion(const CraterList& craters, std::size_t generated) const { | |
| 282 | 52 | return FilterCraters(craters, [&](std::size_t n, const Crater& crater) { | |
| 283 | 514195 | return n < generated || InRegion(crater, 0.5 * m_options.marginRadii * crater.diameter); | |
| 284 | 52 | }); | |
| 285 | } | ||
| 286 | |||
| 287 | /// The craters within their margin of the region that spacing keeps, counting those removed. | ||
| 288 | 36 | CraterList RemoveSpacingConflicts(const CraterList& craters, std::size_t& removed) const { | |
| 289 | 36 | const std::vector<bool> conflicts = SpacingConflicts(craters, m_options.spacing, m_surface); | |
| 290 | 36 | return FilterCraters(craters, [&](std::size_t n, const Crater& crater) { | |
| 291 | 29274 | if (!InRegion(crater, 0.5 * m_options.marginRadii * crater.diameter)) { | |
| 292 | return false; | ||
| 293 | } | ||
| 294 | 24798 | if (conflicts[n]) { | |
| 295 | 8922 | ++removed; | |
| 296 | 8922 | return false; | |
| 297 | } | ||
| 298 | return true; | ||
| 299 | 36 | }); | |
| 300 | 36 | } | |
| 301 | |||
| 302 | /// The manual craters and the `maxCraters` largest generated ones (ties to the smaller ID), | ||
| 303 | /// counting the generated craters dropped. | ||
| 304 | 264799 | CraterList KeepLargest(const CraterList& craters, std::size_t& capped) const { | |
| 305 | 264799 | if (!m_options.maxCraters) { | |
| 306 | 264796 | return craters; | |
| 307 | } | ||
| 308 | 3 | const std::size_t limit = *m_options.maxCraters; | |
| 309 | 3 | std::vector<std::size_t> generated; | |
| 310 | 4714 | for (std::size_t n = 0; n < craters.Size(); ++n) { | |
| 311 | 4711 | if (craters.Manual()[n] == 0) { | |
| 312 | 4710 | generated.push_back(n); | |
| 313 | } | ||
| 314 | } | ||
| 315 | 3 | if (generated.size() <= limit) { | |
| 316 | 1 | return craters; | |
| 317 | } | ||
| 318 | 2 | const auto& diameters = craters.Diameters(); | |
| 319 | 2 | const auto& ids = craters.Ids(); | |
| 320 | 2 | std::nth_element(generated.begin(), generated.begin() + static_cast<std::ptrdiff_t>(limit), | |
| 321 | 4026 | generated.end(), [&](std::size_t a, std::size_t b) { | |
| 322 | 4026 | if (diameters[a] != diameters[b]) { | |
| 323 | 4026 | return diameters[a] > diameters[b]; | |
| 324 | } | ||
| 325 | ✗ | return ids[a] < ids[b]; | |
| 326 | }); | ||
| 327 | 2 | std::vector<bool> keep(craters.Size(), false); | |
| 328 | 62 | for (std::size_t k = 0; k < limit; ++k) { | |
| 329 | 60 | keep[generated[k]] = true; | |
| 330 | } | ||
| 331 | 2 | capped = generated.size() - limit; | |
| 332 | 2 | return FilterCraters( | |
| 333 | 2424 | craters, [&](std::size_t n, const Crater& crater) { return keep[n] || crater.manual; }); | |
| 334 | 3 | } | |
| 335 | |||
| 336 | const SurfaceCells& m_cells; | ||
| 337 | const Bounds2d& m_region; | ||
| 338 | const SizeFrequencyDistribution& m_density; | ||
| 339 | const CraterAgeModel& m_ages; | ||
| 340 | std::uint32_t m_seed; | ||
| 341 | const CraterGenerationOptions& m_options; | ||
| 342 | const Surface& m_surface; | ||
| 343 | double m_maxDiameter = 0.0; | ||
| 344 | }; | ||
| 345 | |||
| 346 | } // namespace | ||
| 347 | |||
| 348 | 264773 | std::vector<CraterBand> CraterBands(double minDiameter, double maxDiameter, double cellSizeFactor) { | |
| 349 | 264773 | std::vector<CraterBand> bands; | |
| 350 | 1045507 | for (int k = std::ilogb(minDiameter); std::ldexp(1.0, k) < maxDiameter; ++k) { | |
| 351 | 780155 | const double low = std::fmax(minDiameter, std::ldexp(1.0, k)); | |
| 352 | 780155 | const double high = std::fmin(maxDiameter, std::ldexp(1.0, k + 1)); | |
| 353 | 780155 | bands.push_back({k, low, high, cellSizeFactor * std::ldexp(1.0, k + 1)}); | |
| 354 | } | ||
| 355 | 265352 | return bands; | |
| 356 | } | ||
| 357 | |||
| 358 | 128633 | CellCraters::CellCraters(CraterList list) : craters(std::move(list)), byY(craters.Size()) { | |
| 359 | 128637 | const std::vector<double>& y = craters.Y(); | |
| 360 | 2827932 | for (std::uint32_t n = 0; n < byY.size(); ++n) { | |
| 361 | 2699295 | byY[n] = n; | |
| 362 | } | ||
| 363 | 128637 | std::stable_sort(byY.begin(), byY.end(), | |
| 364 | 16117784 | [&y](std::uint32_t a, std::uint32_t b) { return y[a] < y[b]; }); | |
| 365 | 2827634 | for (const double diameter : craters.Diameters()) { | |
| 366 | 2698999 | maxDiameter = std::fmax(maxDiameter, diameter); | |
| 367 | } | ||
| 368 | 128635 | } | |
| 369 | |||
| 370 | 1703827 | std::vector<std::uint32_t> CellCraters::Rows(double minY, double maxY) const { | |
| 371 | 1703827 | const std::vector<double>& y = craters.Y(); | |
| 372 | 1703827 | const auto first = std::lower_bound( | |
| 373 | 7438808 | byY.begin(), byY.end(), minY, [&y](std::uint32_t n, double value) { return y[n] < value; }); | |
| 374 | 1701592 | const auto last = std::upper_bound( | |
| 375 | 5080615 | first, byY.end(), maxY, [&y](double value, std::uint32_t n) { return value < y[n]; }); | |
| 376 | 1700175 | std::vector<std::uint32_t> rows(first, last); | |
| 377 | 1700366 | std::sort(rows.begin(), rows.end()); | |
| 378 | 1699627 | return rows; | |
| 379 | } | ||
| 380 | |||
| 381 | 128205 | CraterList GenerateCellCraters(const SurfaceCells& cells, const CraterBand& band, | |
| 382 | const CellIndex& cell, const SizeFrequencyDistribution& density, | ||
| 383 | const CraterAgeModel& ages, std::uint32_t seed) { | ||
| 384 | 128205 | const auto k = static_cast<std::uint32_t>(band.exponent); | |
| 385 | 128205 | const auto i = static_cast<std::uint32_t>(cell.i); | |
| 386 | 128205 | const auto j = static_cast<std::uint32_t>(cell.j); | |
| 387 | 128205 | RandomStream geometry(seed, kCraterStream, i, j, k); | |
| 388 | 128209 | RandomStream ageStream(seed, kCraterAgeStream, i, j, k); | |
| 389 | 128206 | const double countPerArea = density.ExpectedCount(1.0, band.low, band.high); | |
| 390 | 128205 | const SurfaceCells::Sampling sampling = cells.CellSampling(cell, band.cellSize); | |
| 391 | 128205 | const std::uint64_t count = SamplePoisson(geometry, countPerArea * sampling.area); | |
| 392 | 128209 | CraterList craters; | |
| 393 | 128207 | craters.Reserve(static_cast<std::size_t>(count)); | |
| 394 | 2932023 | for (std::uint64_t index = 0; index < count; ++index) { | |
| 395 | 2803814 | const double diameter = density.SampleDiameter(geometry, band.low, band.high); | |
| 396 | 2803446 | const double u = geometry.NextDouble(); | |
| 397 | 2803674 | const double v = geometry.NextDouble(); | |
| 398 | 2803988 | const Vec2d center = cells.SamplePoint(u, v, cell, band.cellSize); | |
| 399 | 3014527 | if (sampling.bound > 0.0 && | |
| 400 | 210816 | geometry.NextDouble() * sampling.bound >= cells.Weight(center)) { | |
| 401 | 106569 | continue; | |
| 402 | } | ||
| 403 | 2697142 | const double ageGyr = ages.SampleAgeGyr(ageStream, diameter); | |
| 404 | 2697243 | craters.Append({center.x, center.y, diameter, ageGyr, | |
| 405 | 2696174 | CraterId(seed, band, cell, static_cast<std::uint32_t>(index))}); | |
| 406 | } | ||
| 407 | 128209 | return craters; | |
| 408 | } | ||
| 409 | |||
| 410 | 394642 | double ResolveMaxDiameter(double maxDiameter, const SizeFrequencyDistribution& density) { | |
| 411 | 394642 | return std::isinf(maxDiameter) ? density.ValidRange().max : maxDiameter; | |
| 412 | } | ||
| 413 | |||
| 414 | 264730 | CraterList GenerateCraters(const SurfaceCells& cells, const Bounds2d& region, | |
| 415 | const SizeFrequencyDistribution& density, const CraterAgeModel& ages, | ||
| 416 | std::uint32_t seed, const CraterGenerationOptions& options) { | ||
| 417 | 264730 | ValidateCraterGenerationOptions(options); | |
| 418 | 264647 | const Stopwatch stopwatch; | |
| 419 | 264836 | const Generation generation(cells, region, density, ages, seed, options); | |
| 420 | 264898 | GenerationCounts counts; | |
| 421 | 264898 | CraterList craters = generation.Run(counts); | |
| 422 | 264599 | generation.Log(craters.Size(), counts, stopwatch); | |
| 423 | 264821 | return craters; | |
| 424 | } | ||
| 425 | |||
| 426 | 6 | double CountLimitDiameter(const SizeFrequencyDistribution& density, double area, double count, | |
| 427 | double minDiameter, double maxDiameter) { | ||
| 428 | 6 | RequirePositive(count, "Count"); | |
| 429 | 5 | RequirePositive(area, "Area"); | |
| 430 | 8 | if (!(IsPositive(minDiameter) && maxDiameter > minDiameter && std::isfinite(maxDiameter))) { | |
| 431 | 1 | throw std::invalid_argument("Diameter interval must satisfy 0 < minDiameter < maxDiameter"); | |
| 432 | } | ||
| 433 | 3 | if (density.ExpectedCount(area, minDiameter, maxDiameter) <= count) { | |
| 434 | return minDiameter; | ||
| 435 | } | ||
| 436 | 2 | return BisectLog(minDiameter, maxDiameter, 200, 1e-12, [&](double diameter) { | |
| 437 | 86 | return !(density.ExpectedCount(area, diameter, maxDiameter) > count); | |
| 438 | }); | ||
| 439 | } | ||
| 440 | |||
| 441 | } // namespace terraingen | ||
| 442 |