src/layers/boulders/ejecta_index.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/boulders/ejecta_index.h" | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | ||
| 7 | #include <utility> | ||
| 8 | |||
| 9 | namespace terraingen { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | /// The shortest reach (m) bucketed apart, below which craters share the finest band. | ||
| 14 | constexpr double kFinestReach = 1e-3; | ||
| 15 | |||
| 16 | } // namespace | ||
| 17 | |||
| 18 | 1065 | EjectaIndex::EjectaIndex(CraterList craters, const Surface& surface, | |
| 19 | const std::function<double(const Crater&)>& reach, | ||
| 20 | 1065 | std::vector<std::uint32_t> sources) | |
| 21 | 1065 | : m_craters(std::move(craters)), m_sources(std::move(sources)), | |
| 22 | 1065 | m_surface(std::make_shared<const Surface>(surface)) { | |
| 23 | 1065 | m_buckets.emplace(*m_surface); | |
| 24 | 8760 | for (std::size_t n = 0; n < m_craters.Size(); ++n) { | |
| 25 | 7695 | const Crater crater = m_craters.At(n); | |
| 26 | 7695 | const int band = | |
| 27 | 7695 | static_cast<int>(std::ceil(std::log2(std::fmax(reach(crater), kFinestReach)))); | |
| 28 | 7695 | m_buckets->Add(band, std::ldexp(1.0, band), {crater.x, crater.y}, n); | |
| 29 | } | ||
| 30 | 1065 | } | |
| 31 | |||
| 32 | 28119 | std::vector<std::uint32_t> EjectaIndex::Near(double x, double y) const { | |
| 33 | 28119 | std::vector<std::uint32_t> near; | |
| 34 | 28119 | if (!m_buckets) { | |
| 35 | return near; | ||
| 36 | } | ||
| 37 | 94182 | for (const int band : m_buckets->Bands()) { | |
| 38 | 66064 | m_buckets->ForEachAround(band, {x, y}, 1, [&](const std::vector<std::size_t>& indices) { | |
| 39 | 297953 | for (const std::size_t n : indices) { | |
| 40 | 210353 | near.push_back(static_cast<std::uint32_t>(n)); | |
| 41 | } | ||
| 42 | 87600 | }); | |
| 43 | 28118 | } | |
| 44 | 28118 | std::sort(near.begin(), near.end()); | |
| 45 | return near; | ||
| 46 | } | ||
| 47 | |||
| 48 | } // namespace terraingen | ||
| 49 |