src/layers/boulders/boulder_layer_abundance.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/boulders/boulder_layer.h" | ||
| 4 | |||
| 5 | #include "layers/boulders/boulder_clusters.h" | ||
| 6 | #include "layers/craters/shaping/resolved_state.h" | ||
| 7 | #include "terraingen/distribution/golombek_rapp.h" | ||
| 8 | #include "terraingen/layers/exposure.h" | ||
| 9 | #include "terraingen/layers/fade.h" | ||
| 10 | #include "terraingen/noise/hash.h" | ||
| 11 | #include "terraingen/noise/worley.h" | ||
| 12 | #include "terraingen/random/philox.h" | ||
| 13 | #include "terraingen/random/samplers.h" | ||
| 14 | #include "terraingen/random/stream.h" | ||
| 15 | #include "terraingen/util/bits.h" | ||
| 16 | #include "terraingen/util/parallel.h" | ||
| 17 | #include "util/checks.h" | ||
| 18 | #include "util/numerics.h" | ||
| 19 | |||
| 20 | #include <algorithm> | ||
| 21 | #include <cmath> | ||
| 22 | #include <cstdint> | ||
| 23 | |||
| 24 | namespace terraingen { | ||
| 25 | |||
| 26 | namespace { | ||
| 27 | |||
| 28 | /// How far, in radii from its center, a crater's blocks reach. | ||
| 29 | 56067 | double EjectaReach(const BoulderEjecta& ejecta, double diameter) { | |
| 30 | 56067 | if (!(ejecta.gravity && ejecta.regolithThickness)) { | |
| 31 | 56063 | return ejecta.reach; | |
| 32 | } | ||
| 33 | 4 | const double radius = 0.5 * diameter; | |
| 34 | 4 | const double thrown = | |
| 35 | 4 | BoulderThrowDistance(diameter, *ejecta.regolithThickness, *ejecta.gravity) / radius; | |
| 36 | 4 | return std::fmin(ejecta.reach, std::fmax(1.0, thrown)); | |
| 37 | } | ||
| 38 | |||
| 39 | /// A crater's share of blocks at `rho` radii from its center, within its `reach`. | ||
| 40 | 19129 | double RadialShare(const BoulderEjecta& ejecta, double rho, double reach) { | |
| 41 | 19129 | if (ejecta.profile == EjectaProfile::Smoothstep) { | |
| 42 | 19125 | return rho <= 1.0 ? 1.0 : Smoothstep((reach - rho) / (reach - 1.0)); | |
| 43 | } | ||
| 44 | 4 | return rho <= 1.0 ? ejecta.floorShare : std::exp(-(rho - 1.0) / ejecta.decayRadii); | |
| 45 | } | ||
| 46 | |||
| 47 | /// The share of its blocks `crater` keeps at its age. | ||
| 48 | 18230 | double Retention(const BoulderEjecta& ejecta, const Crater& crater, const CraterModels& models) { | |
| 49 | 18230 | if (ejecta.aging == EjectaAging::Freshness) { | |
| 50 | 18226 | const CraterState state = ResolvedCraterState(*models.profile, *models.degradation, crater); | |
| 51 | 18226 | const double freshness = state.fresh.depthRatio > 0.0 ? state.Freshness() : 0.0; | |
| 52 | 18226 | return Smoothstep((freshness - ejecta.minFreshness) / (1.0 - ejecta.minFreshness)); | |
| 53 | } | ||
| 54 | 4 | return std::pow(std::fmax(0.0, 1.0 - crater.ageGyr / ejecta.lifetimeGyr), ejecta.agingExponent); | |
| 55 | } | ||
| 56 | |||
| 57 | } // namespace | ||
| 58 | |||
| 59 | 2869320 | EjectaIndex BoulderLayer::EjectaCraters(const Bounds2d& region) const { | |
| 60 | 2869320 | if (m_minCraterDiameter <= 0.0) { | |
| 61 | 2868257 | return {}; | |
| 62 | } | ||
| 63 | 1063 | CraterList ejecta; | |
| 64 | 1063 | std::vector<std::uint32_t> sources; | |
| 65 | 2130 | for (std::size_t k = 0; k < m_craters.size(); ++k) { | |
| 66 | 1067 | const CraterLayer& layer = *m_craters[k]; | |
| 67 | 1067 | const double margin = m_options.ejecta.reach * 0.5 * layer.MaxFeatureDiameter(); | |
| 68 | 1067 | const double spacing = m_minCraterDiameter / layer.Options().apply.fade.startSamples; | |
| 69 | 1067 | const CraterList all = layer.Craters(m_surface.Expand(region, margin), spacing); | |
| 70 | 11163 | const CraterList thrown = FilterCraters(all, [&](std::size_t, const Crater& crater) { | |
| 71 | 9029 | return crater.diameter >= m_minCraterDiameter; | |
| 72 | 1067 | }); | |
| 73 | 1067 | ejecta.Append(thrown); | |
| 74 | 1067 | sources.resize(ejecta.Size(), static_cast<std::uint32_t>(k)); | |
| 75 | 1067 | } | |
| 76 | 7958 | const auto reach = [&](const Crater& crater) { | |
| 77 | 6895 | return EjectaReach(m_options.ejecta, crater.diameter) * 0.5 * crater.diameter; | |
| 78 | 1063 | }; | |
| 79 | 1063 | return {std::move(ejecta), m_surface, reach, std::move(sources)}; | |
| 80 | 1063 | } | |
| 81 | |||
| 82 | 215952 | double BoulderLayer::NearestClusterDistance(double x, double y) const { | |
| 83 | 215952 | const double spacing = m_options.clusters.centerSpacing; | |
| 84 | 215952 | if (m_surface.IsBody()) { | |
| 85 | 2081 | const Vec3d p = m_surface.Position({x, y}); | |
| 86 | 2081 | const double s = 1.0 / spacing; | |
| 87 | 2081 | return spacing * | |
| 88 | 2081 | static_cast<double>( | |
| 89 | 2081 | Worley3(SplitLatticePrecise3(s * p.x, s * p.y, s * p.z), m_clusterSeed).f1); | |
| 90 | } | ||
| 91 | 427742 | return spacing * static_cast<double>(Worley2(static_cast<float>(x / spacing), | |
| 92 | 213871 | static_cast<float>(y / spacing), m_clusterSeed) | |
| 93 | 213871 | .f1); | |
| 94 | } | ||
| 95 | |||
| 96 | namespace { | ||
| 97 | |||
| 98 | /// The cells and Gaussians of a `Thomas` cluster field: centers scattered `perCell` to a cell | ||
| 99 | /// `side` across, each adding a Gaussian of `sigma` cut at `side`. | ||
| 100 | struct ThomasLattice { | ||
| 101 | double sigma = 1.0; | ||
| 102 | double side = 1.0; | ||
| 103 | double perCell = 0.0; | ||
| 104 | std::uint32_t seed = 0; | ||
| 105 | |||
| 106 | /// The word of the lattice cell index `cell`. | ||
| 107 | 5599242 | static std::uint32_t Word(double cell) { | |
| 108 | 5599242 | return static_cast<std::uint32_t>(static_cast<std::int64_t>(cell)); | |
| 109 | } | ||
| 110 | |||
| 111 | /// The Gaussian of a center `squared` square meters away. | ||
| 112 | 12628516 | double Bump(double squared) const { | |
| 113 | 12628516 | return squared < side * side ? std::exp(-0.5 * squared / (sigma * sigma)) : 0.0; | |
| 114 | } | ||
| 115 | |||
| 116 | /// The field at `p` from centers scattered through space, or over the plane when `planar` | ||
| 117 | /// (`p.z` unused). | ||
| 118 | 442138 | double Sum(const Vec3d& p, bool planar) const { | |
| 119 | 442138 | double sum = 0.0; | |
| 120 | 442138 | const double cx = std::floor(p.x / side); | |
| 121 | 442138 | const double cy = std::floor(p.y / side); | |
| 122 | 442138 | const double cz = planar ? 0.0 : std::floor(p.z / side); | |
| 123 | 90000 | const double layers = planar ? 0.0 : 1.0; | |
| 124 | 1064276 | for (double k = cz - layers; k <= cz + layers; k += 1.0) { | |
| 125 | 2488552 | for (double j = cy - 1.0; j <= cy + 1.0; j += 1.0) { | |
| 126 | 7465656 | for (double i = cx - 1.0; i <= cx + 1.0; i += 1.0) { | |
| 127 | 5599242 | RandomStream random = | |
| 128 | 5599242 | planar ? RandomStream(seed, kThomasPlaneStream, Word(i), Word(j)) | |
| 129 | 5599242 | : RandomStream(seed, kThomasSpaceStream, Word(i), Word(j), Word(k)); | |
| 130 | 5599242 | const std::uint64_t count = SamplePoisson(random, perCell); | |
| 131 | 18227758 | for (std::uint64_t n = 0; n < count; ++n) { | |
| 132 | 12628516 | const double dx = (i + random.NextDouble()) * side - p.x; | |
| 133 | 12628516 | const double dy = (j + random.NextDouble()) * side - p.y; | |
| 134 | 12628516 | const double dz = planar ? 0.0 : (k + random.NextDouble()) * side - p.z; | |
| 135 | 12628516 | sum += Bump(dx * dx + dy * dy + dz * dz); | |
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | 442138 | return sum; | |
| 141 | } | ||
| 142 | }; | ||
| 143 | |||
| 144 | } // namespace | ||
| 145 | |||
| 146 | 442138 | double BoulderLayer::ThomasClusterField(double x, double y) const { | |
| 147 | 442138 | const BoulderClusters& clusters = m_options.clusters; | |
| 148 | 442138 | ThomasLattice lattice; | |
| 149 | 442138 | lattice.sigma = clusters.radius; | |
| 150 | 442138 | lattice.side = kThomasReachRadii * lattice.sigma; | |
| 151 | 442138 | lattice.seed = m_options.seed; | |
| 152 | 442138 | const double perArea = 1.0 / (clusters.centerSpacing * clusters.centerSpacing); | |
| 153 | 442138 | const double side = lattice.side; | |
| 154 | 442138 | if (m_surface.IsBody()) { | |
| 155 | 90000 | lattice.perCell = perArea / (std::sqrt(2.0 * kPi) * lattice.sigma) * side * side * side; | |
| 156 | 90000 | return lattice.Sum(m_surface.Position({x, y}), false); | |
| 157 | } | ||
| 158 | 352138 | lattice.perCell = perArea * side * side; | |
| 159 | 352138 | return lattice.Sum({x, y, 0.0}, true); | |
| 160 | } | ||
| 161 | |||
| 162 | 3767488 | double BoulderLayer::BackgroundAbundance(double x, double y, const Ground* ground, | |
| 163 | double ejecta) const { | ||
| 164 | 3767488 | double background = m_background->map | |
| 165 | 3767488 | ? m_background->map->At(MapValueAt(*m_options.densityMap, x, y), ejecta) | |
| 166 | 3767488 | : 1.0; | |
| 167 | 3767488 | const BoulderPatches& patches = m_options.patches; | |
| 168 | 3767488 | if (patches.spread > 1.0) { | |
| 169 | 2153325 | const double noise = PatchNoise(m_surface, x, y, patches.size, m_patchSeed); | |
| 170 | 2153325 | background *= std::pow(patches.spread, noise) / m_background->patchMean; | |
| 171 | } | ||
| 172 | 3767488 | const BoulderClusters& clusters = m_options.clusters; | |
| 173 | 3767488 | if (clusters.share > 0.0) { | |
| 174 | 658090 | const double field = clusters.model == BoulderClusterModel::Thomas | |
| 175 | 658090 | ? ThomasClusterField(x, y) | |
| 176 | 215952 | : ClusterBump(NearestClusterDistance(x, y), clusters); | |
| 177 | 658090 | background *= 1.0 - clusters.share + clusters.share * field / m_clusterMean; | |
| 178 | } | ||
| 179 | 3767488 | if (m_exposure) { | |
| 180 | 124080 | const double bedrock = m_exposure.At(x, y, ground, m_surface); | |
| 181 | 124079 | background *= 1.0 + bedrock * (m_options.bedrock.abundance - 1.0); | |
| 182 | 124079 | if (m_bedrockMeans) { | |
| 183 | 80002 | background /= BedrockMean(x, y, *ground); | |
| 184 | } | ||
| 185 | } | ||
| 186 | 3767487 | return std::min(background, m_background->max); | |
| 187 | } | ||
| 188 | |||
| 189 | 640102 | std::size_t BoulderLayer::BedrockCellHash::operator()(const BedrockCell& cell) const { | |
| 190 | 640102 | return HashWords({static_cast<std::uint64_t>(cell.i), static_cast<std::uint64_t>(cell.j), | |
| 191 | 640102 | static_cast<std::uint64_t>(reinterpret_cast<std::uintptr_t>(cell.ground))}); | |
| 192 | } | ||
| 193 | |||
| 194 | 80002 | double BoulderLayer::BedrockMean(double x, double y, const Ground& ground) const { | |
| 195 | 80002 | constexpr int kSide = 8; | |
| 196 | 80002 | const double gain = m_options.bedrock.abundance - 1.0; | |
| 197 | 400010 | const auto cellMean = [&](std::int64_t i, std::int64_t j) { | |
| 198 | 640016 | return *m_bedrockMeans->Get({&ground, i, j}, [&] { | |
| 199 | 72 | double sum = 0.0; | |
| 200 | 648 | for (int b = 0; b < kSide; ++b) { | |
| 201 | 5184 | for (int a = 0; a < kSide; ++a) { | |
| 202 | 4608 | const double px = | |
| 203 | 4608 | m_bedrockOrigin.x + | |
| 204 | 4608 | (static_cast<double>(i) + (a + 0.5) / kSide) * m_bedrockCellSize.x; | |
| 205 | 4608 | const double py = | |
| 206 | 4608 | m_bedrockOrigin.y + | |
| 207 | 4608 | (static_cast<double>(j) + (b + 0.5) / kSide) * m_bedrockCellSize.y; | |
| 208 | 4608 | sum += 1.0 + gain * m_exposure.At(px, py, &ground, m_surface); | |
| 209 | } | ||
| 210 | } | ||
| 211 | 72 | return sum / (kSide * kSide); | |
| 212 | 640016 | }); | |
| 213 | 80002 | }; | |
| 214 | 80002 | const double u = (x - m_bedrockOrigin.x) / m_bedrockCellSize.x - 0.5; | |
| 215 | 80002 | const double v = (y - m_bedrockOrigin.y) / m_bedrockCellSize.y - 0.5; | |
| 216 | 80002 | const double fu = std::floor(u); | |
| 217 | 80002 | const double fv = std::floor(v); | |
| 218 | 80002 | const auto i = static_cast<std::int64_t>(fu); | |
| 219 | 80002 | const auto j = static_cast<std::int64_t>(fv); | |
| 220 | 80002 | const double tu = u - fu; | |
| 221 | 80002 | const double tv = v - fv; | |
| 222 | 80002 | return (1.0 - tu) * (1.0 - tv) * cellMean(i, j) + tu * (1.0 - tv) * cellMean(i + 1, j) + | |
| 223 | 80002 | (1.0 - tu) * tv * cellMean(i, j + 1) + tu * tv * cellMean(i + 1, j + 1); | |
| 224 | } | ||
| 225 | |||
| 226 | 22278 | void BoulderLayer::EjectaAbundance(double x, double y, const EjectaIndex& ejecta, | |
| 227 | BoulderAbundance& abundance) const { | ||
| 228 | 22278 | const BoulderEjecta& eject = m_options.ejecta; | |
| 229 | 22278 | double share = 0.0; | |
| 230 | 22278 | double wear = 0.0; | |
| 231 | 71450 | for (const std::uint32_t n : ejecta.Near(x, y)) { | |
| 232 | 98344 | const CraterModels& models = m_craters[ejecta.Source(n)]->Models(); | |
| 233 | 49172 | const Crater crater = ejecta.Craters().At(n); | |
| 234 | 49172 | const double reach = EjectaReach(eject, crater.diameter); | |
| 235 | 49172 | const double rho = | |
| 236 | 49172 | m_surface.Distance({crater.x, crater.y}, {x, y}) / (0.5 * crater.diameter); | |
| 237 | 49172 | if (rho >= reach) { | |
| 238 | 30942 | continue; | |
| 239 | } | ||
| 240 | 19129 | const double radial = RadialShare(eject, rho, reach); | |
| 241 | 19129 | if (radial <= share) { | |
| 242 | 899 | continue; | |
| 243 | } | ||
| 244 | 18230 | const double retention = Retention(eject, crater, models); | |
| 245 | 18230 | if (retention * radial > share) { | |
| 246 | 18198 | share = retention * radial; | |
| 247 | 18198 | wear = 1.0 - retention; | |
| 248 | } | ||
| 249 | 22278 | } | |
| 250 | 22278 | abundance.ejecta = share; | |
| 251 | 22278 | abundance.ejectaWear = wear; | |
| 252 | 22278 | } | |
| 253 | |||
| 254 | 3767487 | BoulderAbundance BoulderLayer::Abundance(double x, double y, const Ground* ground, | |
| 255 | const EjectaIndex& ejecta) const { | ||
| 256 | 3767487 | BoulderAbundance abundance; | |
| 257 | 3767487 | if (!ejecta.Empty()) { | |
| 258 | 22278 | EjectaAbundance(x, y, ejecta, abundance); | |
| 259 | } | ||
| 260 | 3767487 | abundance.background = BackgroundAbundance(x, y, ground, abundance.ejecta); | |
| 261 | 3767492 | if (m_options.slopeMap) { | |
| 262 | 72189 | abundance.exponent = SizeSlopeAt(*m_options.slopeMap, x, y); | |
| 263 | } | ||
| 264 | 3767492 | return abundance; | |
| 265 | } | ||
| 266 | |||
| 267 | 2730242 | BoulderAbundance BoulderLayer::AbundanceAt(double x, double y, const Ground* ground) const { | |
| 268 | 2730242 | if (!m_options.generateRandom) { | |
| 269 | 2 | BoulderAbundance none; | |
| 270 | 2 | none.background = 0.0; | |
| 271 | 2 | return none; | |
| 272 | } | ||
| 273 | 2730240 | return Abundance(x, y, ground, EjectaCraters(PointBounds(x, y))); | |
| 274 | } | ||
| 275 | |||
| 276 | 11456791 | double BoulderLayer::Density(double diameter, const BoulderAbundance& abundance) const { | |
| 277 | 11456791 | if (!m_options.generateRandom) { | |
| 278 | return 0.0; | ||
| 279 | } | ||
| 280 | 11456791 | const BoulderSizes& sizes = m_options.sizes; | |
| 281 | 11456791 | double density = 0.0; | |
| 282 | 11456791 | if (abundance.exponent) { | |
| 283 | 72188 | const double alpha = *abundance.exponent; | |
| 284 | 72188 | const double d0 = m_options.slopeMap->referenceDiameter; | |
| 285 | 72188 | density = abundance.background * m_referenceCount * alpha * std::pow(d0, alpha) * | |
| 286 | 72188 | std::pow(diameter, -alpha - 1.0); | |
| 287 | 11384603 | } else if (sizes.distribution) { | |
| 288 | 10837734 | density = abundance.background * sizes.distribution->Incremental(diameter); | |
| 289 | } else { | ||
| 290 | 546869 | const double k = sizes.Coverage() * abundance.background; | |
| 291 | 546869 | density = GolombekRappIncremental(diameter, k, | |
| 292 | 546869 | sizes.QConstant() + sizes.QCoverageCoefficient() / k); | |
| 293 | } | ||
| 294 | 11458064 | if (abundance.ejecta > 0.0) { | |
| 295 | 17652 | density += abundance.ejecta * m_ejectaSizes->Incremental(diameter); | |
| 296 | } | ||
| 297 | return density; | ||
| 298 | } | ||
| 299 | |||
| 300 | 178 | std::vector<BoulderLayer::UnresolvedNode> BoulderLayer::UnresolvedNodes(double spacing) const { | |
| 301 | 178 | const BoulderSizes& sizes = m_options.sizes; | |
| 302 | 178 | const double start = m_options.fade.startSamples * spacing; | |
| 303 | 178 | const double end = std::fmin(m_options.fade.endSamples * spacing, sizes.maxDiameter); | |
| 304 | 178 | std::vector<UnresolvedNode> nodes; | |
| 305 | 534 | const auto add = [&](double low, double high) { | |
| 306 | 356 | if (!(high > low)) { | |
| 307 | return; | ||
| 308 | } | ||
| 309 | 350 | ForEachLogSimpsonNode(low, high, 16, [&](double d, double weight) { | |
| 310 | 5950 | const double unresolved = 1.0 - ResolutionWeight(d, spacing, m_options.fade); | |
| 311 | 5950 | if (unresolved > 0.0) { | |
| 312 | 5781 | nodes.push_back({d, 0.25 * kPi * d * d, unresolved * weight}); | |
| 313 | } | ||
| 314 | 5950 | }); | |
| 315 | 178 | }; | |
| 316 | 178 | add(sizes.minDiameter, std::fmin(start, end)); | |
| 317 | 178 | add(std::fmax(sizes.minDiameter, start), end); | |
| 318 | 178 | return nodes; | |
| 319 | } | ||
| 320 | |||
| 321 | 326012 | UnresolvedCover BoulderLayer::UnresolvedCoverWith(const BoulderAbundance& abundance, | |
| 322 | const std::vector<UnresolvedNode>& nodes, | ||
| 323 | double spacing) const { | ||
| 324 | 326012 | UnresolvedCover cover; | |
| 325 | 11074357 | for (const UnresolvedNode& node : nodes) { | |
| 326 | 10748331 | const double n = Density(node.diameter, abundance) * node.weight; | |
| 327 | 10748345 | cover.mean += node.footprint * n; | |
| 328 | 10748345 | cover.variance += node.footprint * node.footprint * n; | |
| 329 | } | ||
| 330 | 326026 | cover.variance /= spacing * spacing; | |
| 331 | 326026 | return cover; | |
| 332 | } | ||
| 333 | |||
| 334 | 184 | bool BoulderLayer::LeavesUnresolved(double spacing) const { | |
| 335 | 184 | RequirePositive(spacing, "Unresolved cover spacing"); | |
| 336 | 183 | return m_options.generateRandom && | |
| 337 | 180 | m_options.sizes.minDiameter < m_options.fade.endSamples * spacing; | |
| 338 | } | ||
| 339 | |||
| 340 | 176 | UnresolvedCover BoulderLayer::UnresolvedCoverAt(double x, double y, double spacing, | |
| 341 | const Ground* ground) const { | ||
| 342 | 176 | if (!LeavesUnresolved(spacing)) { | |
| 343 | 3 | return {}; | |
| 344 | } | ||
| 345 | 172 | return UnresolvedCoverWith(AbundanceAt(x, y, ground), UnresolvedNodes(spacing), spacing); | |
| 346 | } | ||
| 347 | |||
| 348 | 8 | std::vector<UnresolvedCover> BoulderLayer::UnresolvedCoverOver(const RasterGrid& grid, | |
| 349 | double spacing, const Ground* ground, | ||
| 350 | unsigned threads) const { | ||
| 351 | 8 | std::vector<UnresolvedCover> cover(grid.width * grid.height); | |
| 352 | 8 | if (!LeavesUnresolved(spacing)) { | |
| 353 | return cover; | ||
| 354 | } | ||
| 355 | 6 | const EjectaIndex ejecta = EjectaCraters(grid.Bounds()); | |
| 356 | 6 | const std::vector<UnresolvedNode> nodes = UnresolvedNodes(spacing); | |
| 357 | 6 | ParallelFor(grid.height, threads, [&](std::size_t row) { | |
| 358 | 326714 | for (std::size_t col = 0; col < grid.width; ++col) { | |
| 359 | 325827 | const Vec2d center = grid.transform.PixelToWorld(static_cast<double>(col) + 0.5, | |
| 360 | 325827 | static_cast<double>(row) + 0.5); | |
| 361 | 325827 | cover[row * grid.width + col] = | |
| 362 | 325827 | UnresolvedCoverWith(Abundance(center.x, center.y, ground, ejecta), nodes, spacing); | |
| 363 | } | ||
| 364 | 887 | }); | |
| 365 | 6 | return cover; | |
| 366 | 6 | } | |
| 367 | |||
| 368 | } // namespace terraingen | ||
| 369 |