src/layers/craters/secondaries.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/craters/secondaries.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/fade.h" | ||
| 6 | #include "terraingen/layers/source_resolved.h" | ||
| 7 | #include "terraingen/noise/value.h" | ||
| 8 | #include "terraingen/random/philox.h" | ||
| 9 | #include "terraingen/random/samplers.h" | ||
| 10 | #include "terraingen/random/stream.h" | ||
| 11 | #include "terraingen/raster/grid.h" | ||
| 12 | #include "terraingen/util/bits.h" | ||
| 13 | #include "util/checks.h" | ||
| 14 | |||
| 15 | #include <algorithm> | ||
| 16 | #include <cmath> | ||
| 17 | #include <limits> | ||
| 18 | #include <memory> | ||
| 19 | #include <stdexcept> | ||
| 20 | #include <string> | ||
| 21 | #include <unordered_map> | ||
| 22 | #include <utility> | ||
| 23 | #include <vector> | ||
| 24 | |||
| 25 | namespace terraingen { | ||
| 26 | |||
| 27 | namespace { | ||
| 28 | |||
| 29 | /// The primary's ID mixed with a cell's indices into a secondary's ID. | ||
| 30 | 32740 | std::uint64_t SecondaryId(std::uint64_t primary, int band, std::int64_t ring, std::int64_t sector, | |
| 31 | std::uint32_t index) { | ||
| 32 | 32740 | const IdWords id = SplitId(primary); | |
| 33 | 32740 | const U32x4 block = Philox4x32( | |
| 34 | {id.low, id.high, | ||
| 35 | 32740 | static_cast<std::uint32_t>(band) ^ (static_cast<std::uint32_t>(ring) << 8), index}, | |
| 36 | {static_cast<std::uint32_t>(sector), kSecondaryIdStream}); | ||
| 37 | 32740 | return JoinWords(block.x, block.y); | |
| 38 | } | ||
| 39 | |||
| 40 | /// A region seen from a primary's center: its nearest and farthest distances (m), and its | ||
| 41 | /// directions as an interval around `mid` of half-width `half` (radians counter-clockwise from | ||
| 42 | /// east). | ||
| 43 | struct Footprint { | ||
| 44 | double near = 0.0; | ||
| 45 | double far = 0.0; | ||
| 46 | double mid = 0.0; | ||
| 47 | double half = kPi; | ||
| 48 | }; | ||
| 49 | |||
| 50 | /// `region` seen from `center` on `surface`; the whole circle when `region` holds the center. | ||
| 51 | 1080 | Footprint SecondaryFootprint(const Vec2d& center, const Bounds2d& region, const Surface& surface) { | |
| 52 | 1080 | Footprint footprint; | |
| 53 | 1080 | std::vector<Vec2d> corners; | |
| 54 | 3240 | for (const double x : {region.minX, region.maxX}) { | |
| 55 | 6480 | for (const double y : {region.minY, region.maxY}) { | |
| 56 | 8640 | corners.push_back(surface.Offset(center, {x, y})); | |
| 57 | } | ||
| 58 | } | ||
| 59 | 1080 | constexpr double kInf = std::numeric_limits<double>::infinity(); | |
| 60 | 1080 | double minX = kInf; | |
| 61 | 1080 | double maxX = -kInf; | |
| 62 | 1080 | double minY = kInf; | |
| 63 | 1080 | double maxY = -kInf; | |
| 64 | 5400 | for (const Vec2d& c : corners) { | |
| 65 | 4320 | minX = std::fmin(minX, c.x); | |
| 66 | 4320 | maxX = std::fmax(maxX, c.x); | |
| 67 | 4320 | minY = std::fmin(minY, c.y); | |
| 68 | 4320 | maxY = std::fmax(maxY, c.y); | |
| 69 | 4320 | footprint.far = std::fmax(footprint.far, std::hypot(c.x, c.y)); | |
| 70 | } | ||
| 71 | 1080 | const double dx = std::fmax(0.0, std::fmax(minX, -maxX)); | |
| 72 | 1080 | const double dy = std::fmax(0.0, std::fmax(minY, -maxY)); | |
| 73 | 1080 | footprint.near = std::hypot(dx, dy); | |
| 74 | 1080 | if (footprint.near == 0.0) { | |
| 75 | return footprint; | ||
| 76 | } | ||
| 77 | 1055 | footprint.mid = std::atan2(0.5 * (minY + maxY), 0.5 * (minX + maxX)); | |
| 78 | 1055 | double half = 0.0; | |
| 79 | 5275 | for (const Vec2d& c : corners) { | |
| 80 | 4220 | half = std::fmax( | |
| 81 | 4220 | half, std::fabs(std::remainder(std::atan2(c.y, c.x) - footprint.mid, 2.0 * kPi))); | |
| 82 | } | ||
| 83 | 1055 | footprint.half = std::fmin(kPi, half); | |
| 84 | 1055 | return footprint; | |
| 85 | 1080 | } | |
| 86 | |||
| 87 | /// The sectors [first, last) of a ring of `sectors` sectors of angle `width` at inner radius `r0` | ||
| 88 | /// that can reach a footprint, in cells of side `cell`; indices may run past a turn. | ||
| 89 | 6803 | std::pair<std::int64_t, std::int64_t> SectorRange(const Footprint& footprint, double cell, | |
| 90 | double r0, double width, std::int64_t sectors) { | ||
| 91 | 6803 | if (footprint.half < kPi) { | |
| 92 | 6400 | const double margin = cell / r0 + width; | |
| 93 | 6400 | const double from = footprint.mid - footprint.half - margin; | |
| 94 | 6400 | const double to = footprint.mid + footprint.half + margin; | |
| 95 | 6400 | if (to - from < 2.0 * kPi) { | |
| 96 | 6400 | return {static_cast<std::int64_t>(std::floor(from / width)), | |
| 97 | 6400 | static_cast<std::int64_t>(std::ceil(to / width))}; | |
| 98 | } | ||
| 99 | } | ||
| 100 | 403 | return {0, sectors}; | |
| 101 | } | ||
| 102 | |||
| 103 | /// One primary, with the density its secondaries follow and the region they are wanted in. | ||
| 104 | 1079 | struct Primary { | |
| 105 | Crater crater; | ||
| 106 | SecondaryDensity density; | ||
| 107 | Footprint footprint; ///< The region secondaries are wanted in, seen from the primary. | ||
| 108 | }; | ||
| 109 | |||
| 110 | /// One ring of a size band of a primary's polar cells. | ||
| 111 | struct Ring { | ||
| 112 | int band = 0; | ||
| 113 | double low = 0.0; ///< Smallest diameter of the band. | ||
| 114 | double bandShare = 0.0; ///< The band's share of the cumulative count at the reference. | ||
| 115 | std::int64_t index = 0; | ||
| 116 | double r0 = 0.0; | ||
| 117 | double r1 = 0.0; | ||
| 118 | double r = 0.0; ///< Chapman's scaled range at `r0`. | ||
| 119 | double width = 0.0; ///< Angle of a sector. | ||
| 120 | double mean = 0.0; ///< Secondaries expected in a sector. | ||
| 121 | double peak = 1.0; ///< `SecondaryRayField::Peak` over the ring; 1 without rays. | ||
| 122 | }; | ||
| 123 | |||
| 124 | /// One secondary drawn in a sector, before it is kept or thinned. | ||
| 125 | struct Candidate { | ||
| 126 | double range = 0.0; | ||
| 127 | double azimuth = 0.0; | ||
| 128 | double size = 0.0; | ||
| 129 | double keep = 0.0; | ||
| 130 | double rayKeep = 0.0; | ||
| 131 | double e0 = 1.0; ///< Whether it is elongated, against `elongatedShare`. | ||
| 132 | double e1 = 0.0; ///< The draws of its ellipticity. | ||
| 133 | double e2 = 0.0; | ||
| 134 | }; | ||
| 135 | |||
| 136 | /// Draws the secondaries of primaries into one list, keeping count of those expected. | ||
| 137 | 1084 | class Scatter { | |
| 138 | public: | ||
| 139 | 1084 | Scatter(const SecondaryCraterOptions& options, const SurfaceField* limit, | |
| 140 | const Surface& surface, const Bounds2d& region, const Bounds2d& wanted, | ||
| 141 | double reachRadii, double skipBelow) | ||
| 142 | 1084 | : m_options(options), m_limit(limit), m_surface(surface), m_region(region), | |
| 143 | 1084 | m_wanted(wanted), m_reachRadii(reachRadii), m_skipBelow(skipBelow), | |
| 144 | 1084 | m_maxStretch(SecondaryStretch(options.shapes)) {} | |
| 145 | |||
| 146 | /// Draws the secondaries `crater` throws, if it is a selected primary. | ||
| 147 | 480214 | void DrawSecondaries(const Crater& crater) { | |
| 148 | 480214 | Primary primary{crater, SecondaryDensity(crater, m_options), {}}; | |
| 149 | 480214 | if (!(primary.density.Reach() > 0.0)) { | |
| 150 | 479134 | return; | |
| 151 | } | ||
| 152 | 1081 | primary.footprint = SecondaryFootprint({crater.x, crater.y}, m_wanted, m_surface); | |
| 153 | 1080 | const int firstBand = std::ilogb(std::fmax(m_options.minDiameter, 1e-9)); | |
| 154 | 1080 | const int lastBand = std::ilogb(LargestSecondary(crater.diameter, m_options)); | |
| 155 | 5364 | for (int band = firstBand; band <= lastBand; ++band) { | |
| 156 | 4285 | DrawBand(primary, band); | |
| 157 | } | ||
| 158 | 480213 | } | |
| 159 | |||
| 160 | 1083 | CraterList Take() { | |
| 161 | 1083 | return std::move(m_secondaries); | |
| 162 | } | ||
| 163 | |||
| 164 | /// The outlines of the elongated secondaries drawn, by ID. | ||
| 165 | 1083 | std::unordered_map<std::uint64_t, SecondaryOutline> TakeOutlines() { | |
| 166 | 2166 | return std::move(m_outlines); | |
| 167 | } | ||
| 168 | |||
| 169 | private: | ||
| 170 | 4285 | void DrawBand(const Primary& primary, int band) { | |
| 171 | 4285 | const double low = std::fmax(m_options.minDiameter, std::ldexp(1.0, band)); | |
| 172 | 4285 | const double high = std::fmin(m_options.maxDiameter, std::ldexp(1.0, band + 1)); | |
| 173 | 4285 | if (!(high > low) || high <= m_skipBelow) { | |
| 174 | 1056 | return; | |
| 175 | } | ||
| 176 | 3235 | const SecondaryDensity& density = primary.density; | |
| 177 | 3235 | const double far = std::fmin(density.Reach(), SingerMaxRange(primary.crater.diameter, low)); | |
| 178 | 3235 | const double cell = m_options.cellSizeFactor * high; | |
| 179 | 3235 | const double inner = std::fmax(density.Onset(), primary.footprint.near - cell); | |
| 180 | 3235 | const double outerEdge = std::fmin(far, primary.footprint.far + cell); | |
| 181 | 3235 | if (!(outerEdge > inner)) { | |
| 182 | return; | ||
| 183 | } | ||
| 184 | 3229 | const auto firstRing = | |
| 185 | 3229 | static_cast<std::int64_t>(std::floor((inner - density.Onset()) / cell)); | |
| 186 | 3229 | const auto lastRing = | |
| 187 | 3229 | static_cast<std::int64_t>(std::ceil((outerEdge - density.Onset()) / cell)); | |
| 188 | 3229 | Ring ring; | |
| 189 | 3229 | ring.band = band; | |
| 190 | 3229 | ring.low = low; | |
| 191 | 3229 | ring.bandShare = density.SizeShare(low, high); | |
| 192 | 10031 | for (ring.index = firstRing; ring.index < lastRing; ++ring.index) { | |
| 193 | 6803 | ring.r0 = density.Onset() + static_cast<double>(ring.index) * cell; | |
| 194 | 6803 | ring.r1 = std::fmin(far, ring.r0 + cell); | |
| 195 | 6803 | if (!(ring.r1 > ring.r0)) { | |
| 196 | ✗ | continue; | |
| 197 | } | ||
| 198 | 6803 | DrawRing(primary, ring, cell); | |
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | 6803 | void DrawRing(const Primary& primary, Ring& ring, double cell) { | |
| 203 | 6803 | const SecondaryDensity& density = primary.density; | |
| 204 | 13606 | const auto sectors = std::max<std::int64_t>( | |
| 205 | 6803 | 1, static_cast<std::int64_t>(std::ceil(2.0 * kPi * 0.5 * (ring.r0 + ring.r1) / cell))); | |
| 206 | 6803 | ring.width = 2.0 * kPi / static_cast<double>(sectors); | |
| 207 | 6803 | const auto [first, last] = | |
| 208 | 6803 | SectorRange(primary.footprint, cell, ring.r0, ring.width, sectors); | |
| 209 | 6803 | ring.r = density.ScaledRange(ring.r0); | |
| 210 | 6803 | const double area = 0.5 * ring.width * (ring.r1 * ring.r1 - ring.r0 * ring.r0); | |
| 211 | 6803 | ring.mean = density.OnsetDensity() / (ring.r * ring.r) * ring.bandShare * area; | |
| 212 | 13606 | if (density.Rays()) { | |
| 213 | 6250 | ring.peak = density.Rays()->Peak(ring.r0, ring.r1); | |
| 214 | 6250 | ring.mean *= ring.peak; | |
| 215 | } | ||
| 216 | 70135 | for (std::int64_t raw = first; raw < last; ++raw) { | |
| 217 | 63333 | m_expected += ring.mean; | |
| 218 | 63333 | if (m_expected > m_options.maxExpected) { | |
| 219 | 1 | throw std::invalid_argument("Expected secondary count exceeds maxExpected"); | |
| 220 | } | ||
| 221 | 63332 | DrawSector(primary, ring, ((raw % sectors) + sectors) % sectors); | |
| 222 | } | ||
| 223 | 6802 | } | |
| 224 | |||
| 225 | 63332 | void DrawSector(const Primary& primary, const Ring& ring, std::int64_t sector) { | |
| 226 | 63332 | const IdWords id = SplitId(primary.crater.id); | |
| 227 | 63332 | RandomStream random(m_options.seed, kSecondaryStream, id.low ^ id.high, | |
| 228 | 63332 | static_cast<std::uint32_t>(ring.band) ^ | |
| 229 | 63332 | (static_cast<std::uint32_t>(ring.index) << 8), | |
| 230 | 63332 | static_cast<std::uint32_t>(sector)); | |
| 231 | 63332 | const std::uint64_t count = SamplePoisson(random, ring.mean); | |
| 232 | 3201772 | for (std::uint64_t index = 0; index < count; ++index) { | |
| 233 | 3138440 | const Candidate candidate = DrawCandidate(random, primary, ring, sector); | |
| 234 | 3138440 | if (Keeps(primary, ring, candidate)) { | |
| 235 | 286313 | Place(primary, ring, sector, static_cast<std::uint32_t>(index), candidate); | |
| 236 | } | ||
| 237 | } | ||
| 238 | 63332 | } | |
| 239 | |||
| 240 | /// Draws one candidate of `sector` from `random`. | ||
| 241 | 3138440 | Candidate DrawCandidate(RandomStream& random, const Primary& primary, const Ring& ring, | |
| 242 | std::int64_t sector) const { | ||
| 243 | 3138440 | const double s = m_options.sizeSlope; | |
| 244 | 3138440 | const double reference = primary.density.Reference(); | |
| 245 | 3138440 | Candidate candidate; | |
| 246 | 6276880 | candidate.range = std::sqrt(ring.r0 * ring.r0 + | |
| 247 | 3138440 | random.NextDouble() * (ring.r1 * ring.r1 - ring.r0 * ring.r0)); | |
| 248 | 3138440 | candidate.azimuth = (static_cast<double>(sector) + random.NextDouble()) * ring.width; | |
| 249 | 3138440 | const double u = random.NextDouble(); | |
| 250 | 3138440 | candidate.size = | |
| 251 | 3138440 | reference * std::pow(std::pow(ring.low / reference, -s) - u * ring.bandShare, -1.0 / s); | |
| 252 | 3138440 | candidate.keep = random.NextDouble(); | |
| 253 | 3138440 | candidate.rayKeep = primary.density.Rays() ? random.NextDouble() : 0.0; | |
| 254 | 3138440 | if (m_options.shapes.elongatedShare > 0.0) { | |
| 255 | 2789195 | candidate.e0 = random.NextDouble(); | |
| 256 | 2789195 | candidate.e1 = random.NextDouble(); | |
| 257 | 2789195 | candidate.e2 = random.NextDouble(); | |
| 258 | } | ||
| 259 | 3138440 | return candidate; | |
| 260 | } | ||
| 261 | |||
| 262 | /// Whether `candidate` survives the thinning by range, the largest size there, and the rays. | ||
| 263 | 3138440 | static bool Keeps(const Primary& primary, const Ring& ring, const Candidate& candidate) { | |
| 264 | 3138440 | const SecondaryDensity& density = primary.density; | |
| 265 | 3138440 | const double here = density.ScaledRange(candidate.range); | |
| 266 | 5772796 | if (candidate.keep >= (ring.r * ring.r) / (here * here) || | |
| 267 | 2634356 | candidate.size >= | |
| 268 | 2634356 | SingerMaxSecondaryDiameter(primary.crater.diameter, candidate.range)) { | |
| 269 | 519642 | return false; | |
| 270 | } | ||
| 271 | 2618798 | return !density.Rays() || !(candidate.rayKeep * ring.peak >= | |
| 272 | 2601140 | density.Rays()->Density(candidate.range, candidate.azimuth)); | |
| 273 | } | ||
| 274 | |||
| 275 | /// Appends `candidate` when it lands near the region, with its outline when elongated. | ||
| 276 | 286313 | void Place(const Primary& primary, const Ring& ring, std::int64_t sector, std::uint32_t index, | |
| 277 | const Candidate& candidate) { | ||
| 278 | 286313 | const Crater& crater = primary.crater; | |
| 279 | 286313 | const double range = candidate.range; | |
| 280 | 286313 | const Vec2d at = | |
| 281 | 572626 | m_surface.Locate({crater.x, crater.y}, {range * std::cos(candidate.azimuth), | |
| 282 | 286313 | range * std::sin(candidate.azimuth)}); | |
| 283 | 286313 | const double margin = 0.5 * m_reachRadii * m_maxStretch * candidate.size; | |
| 284 | 319093 | if (!m_surface.Contains(m_surface.Expand(m_region, margin), at) || | |
| 285 | 32780 | InSource(m_limit, candidate.size, at.x, at.y)) { | |
| 286 | 253573 | return; | |
| 287 | } | ||
| 288 | 32740 | const std::uint64_t secondary = | |
| 289 | 32740 | SecondaryId(crater.id, ring.band, ring.index, sector, index); | |
| 290 | 32740 | m_secondaries.Append({at.x, at.y, candidate.size, crater.ageGyr, secondary}); | |
| 291 | 32740 | const SecondaryShapes& shapes = m_options.shapes; | |
| 292 | 32740 | if (candidate.e0 < shapes.elongatedShare) { | |
| 293 | 1130 | const double onset = primary.density.Onset(); | |
| 294 | 1130 | const double outer = primary.density.Reach(); | |
| 295 | 1130 | const double toPrimary = std::clamp((outer - range) / (outer - onset), 0.0, 1.0); | |
| 296 | 1130 | const Vec2d back = m_surface.Offset(at, {crater.x, crater.y}); | |
| 297 | 1130 | m_outlines[secondary] = { | |
| 298 | 1130 | 1.0 + (shapes.ellipticity.Sample(candidate.e1, candidate.e2) - 1.0) * toPrimary, | |
| 299 | 1130 | std::atan2(back.y, back.x)}; | |
| 300 | } | ||
| 301 | } | ||
| 302 | |||
| 303 | const SecondaryCraterOptions& m_options; | ||
| 304 | const SurfaceField* m_limit; | ||
| 305 | const Surface& m_surface; | ||
| 306 | const Bounds2d& m_region; | ||
| 307 | const Bounds2d& m_wanted; | ||
| 308 | double m_reachRadii; | ||
| 309 | double m_skipBelow; | ||
| 310 | CraterList m_secondaries; | ||
| 311 | std::unordered_map<std::uint64_t, SecondaryOutline> m_outlines; | ||
| 312 | double m_expected = 0.0; | ||
| 313 | double m_maxStretch = 1.0; | ||
| 314 | }; | ||
| 315 | |||
| 316 | } // namespace | ||
| 317 | |||
| 318 | 2170 | double SecondaryStretch(const SecondaryShapes& shapes) { | |
| 319 | 2170 | return shapes.elongatedShare > 0.0 ? std::sqrt(shapes.ellipticity.Highest()) : 1.0; | |
| 320 | } | ||
| 321 | |||
| 322 | 55 | SecondaryCraterLayer::SecondaryCraterLayer(std::shared_ptr<const CraterLayer> primaries, | |
| 323 | CraterShapeModels models, SecondaryCraterOptions options, | ||
| 324 | 55 | CraterApplyOptions apply, TerrainBase base) | |
| 325 | 55 | : m_primaries(std::move(primaries)), m_models(std::move(models)), m_options(options), | |
| 326 | 55 | m_apply(apply) { | |
| 327 | 55 | if (!m_primaries || !m_models.degradation) { | |
| 328 | 2 | throw std::invalid_argument("Secondary craters need primaries and degradation"); | |
| 329 | } | ||
| 330 | 53 | m_models.profile = MatchedProfile(std::move(m_models.profile), *m_models.degradation); | |
| 331 | 53 | ValidateSecondaryCraterOptions(m_options); | |
| 332 | 42 | const std::vector<SecondaryCraterOptions>& thrown = m_primaries->Options().secondaries; | |
| 333 | 2 | if (m_primaries->Options().densityMap && | |
| 334 | 42 | std::find(thrown.begin(), thrown.end(), m_options) == thrown.end()) { | |
| 335 | 1 | throw std::invalid_argument( | |
| 336 | "Secondaries of a crater layer following a density map must be among its " | ||
| 337 | 1 | "CraterLayerOptions::secondaries, which the map counts"); | |
| 338 | } | ||
| 339 | 41 | ValidateCraterApplyOptions(m_apply); | |
| 340 | 41 | m_apply = SeededCraterApply(std::move(m_apply), m_options.seed); | |
| 341 | 41 | if (m_apply.datum != CraterDatum::None) { | |
| 342 | 1 | throw std::invalid_argument("Secondary craters are additive; datums are not supported"); | |
| 343 | } | ||
| 344 | 40 | if (!(m_primaries->LayerBase()->surface == base.surface)) { | |
| 345 | 1 | throw std::invalid_argument( | |
| 346 | 1 | "Secondary craters must be built on the surface of their primaries"); | |
| 347 | } | ||
| 348 | 39 | m_apply.surface = std::move(base.surface); | |
| 349 | 39 | m_source = std::move(base.source); | |
| 350 | 39 | m_limit = LayerResolvedLimit(m_source, m_options.sourceResolvesPixels, m_apply.surface); | |
| 351 | 103 | } | |
| 352 | |||
| 353 | 3 | SecondaryCraterLayer::SecondaryCraterLayer(std::shared_ptr<const CraterLayer> primaries, | |
| 354 | SecondaryCraterOptions options, CraterApplyOptions apply, | ||
| 355 | 3 | TerrainBase base) | |
| 356 | 3 | : SecondaryCraterLayer(primaries, primaries ? primaries->Models().Shape() : CraterShapeModels{}, | |
| 357 | 9 | std::move(options), std::move(apply), std::move(base)) {} | |
| 358 | |||
| 359 | 4 | LayerPtr SecondaryCraterLayer::Reseeded(std::uint64_t seed) const { | |
| 360 | 4 | auto layer = std::make_shared<SecondaryCraterLayer>(*this); | |
| 361 | 4 | layer->m_options.seed = ReseedWord(seed, 0); | |
| 362 | 4 | layer->m_apply.irregularity.seed = ReseedWord(seed, 1); | |
| 363 | 4 | layer->m_apply.noise.seed = ReseedWord(seed, 2); | |
| 364 | 4 | layer->m_apply.texture.seed = ReseedWord(seed, 3); | |
| 365 | 4 | layer->m_apply.slope.seed = ReseedWord(seed, 4); | |
| 366 | 4 | layer->m_apply.terraces.seed = ReseedWord(seed, 5); | |
| 367 | 4 | layer->m_apply.peaks.seed = ReseedWord(seed, 6); | |
| 368 | 4 | return layer; | |
| 369 | 4 | } | |
| 370 | |||
| 371 | 3 | LayerPtr SecondaryCraterLayer::Rebound(const LayerReplacements& replaced) const { | |
| 372 | 3 | std::shared_ptr<const CraterLayer> primaries = Replacement(m_primaries, replaced); | |
| 373 | 3 | if (primaries == m_primaries) { | |
| 374 | 2 | return nullptr; | |
| 375 | } | ||
| 376 | 1 | auto layer = std::make_shared<SecondaryCraterLayer>(*this); | |
| 377 | 1 | layer->m_primaries = std::move(primaries); | |
| 378 | 1 | return layer; | |
| 379 | 4 | } | |
| 380 | |||
| 381 | ✗ | LayerPtr SecondaryCraterLayer::Rebased(const TerrainBase& base) const { | |
| 382 | ✗ | return std::make_shared<SecondaryCraterLayer>(m_primaries, m_models, m_options, m_apply, base); | |
| 383 | } | ||
| 384 | |||
| 385 | 1093 | double SecondaryCraterLayer::OuterRange(double diameter) const { | |
| 386 | 1093 | return SecondaryOuterRange(diameter, m_options); | |
| 387 | } | ||
| 388 | |||
| 389 | 9 | CraterFeatures SecondaryCraterLayer::CratersOver(const RasterGrid& grid, double spacing) const { | |
| 390 | 9 | return DescribeCraters( | |
| 391 | 9 | Secondaries(grid.Bounds(), ListedSpacing(grid, spacing, m_apply.surface)), m_models); | |
| 392 | } | ||
| 393 | |||
| 394 | 1085 | double SecondaryCraterLayer::MaxFeatureDiameter() const { | |
| 395 | 1085 | const double largest = m_primaries->MaxFeatureDiameter(); | |
| 396 | 1085 | if (!(largest > 0.0)) { | |
| 397 | return 0.0; | ||
| 398 | } | ||
| 399 | 1085 | return std::fmin(m_options.maxDiameter, | |
| 400 | 1085 | SingerMaxSecondaryDiameter(largest, SecondaryOnsetRange(largest))); | |
| 401 | } | ||
| 402 | |||
| 403 | 38 | CraterList SecondaryCraterLayer::Secondaries(const Bounds2d& region, double spacing) const { | |
| 404 | 38 | std::unordered_map<std::uint64_t, SecondaryOutline> outlines; | |
| 405 | 38 | return Secondaries(region, spacing, outlines); | |
| 406 | 37 | } | |
| 407 | |||
| 408 | 1084 | CraterList SecondaryCraterLayer::Secondaries( | |
| 409 | const Bounds2d& region, double spacing, | ||
| 410 | std::unordered_map<std::uint64_t, SecondaryOutline>& outlines) const { | ||
| 411 | 1084 | const Surface& surface = m_apply.surface; | |
| 412 | 1084 | const double reachRadii = CraterReachRadii(*m_models.profile, m_apply); | |
| 413 | 1084 | const double largest = m_primaries->MaxFeatureDiameter(); | |
| 414 | 1084 | const Bounds2d wanted = surface.Expand( | |
| 415 | 1084 | region, 0.5 * reachRadii * SecondaryStretch(m_options.shapes) * MaxFeatureDiameter()); | |
| 416 | 1084 | const CraterList primaries = | |
| 417 | 1084 | m_primaries->Craters(surface.Expand(wanted, OuterRange(largest)), 0.0); | |
| 418 | 1084 | const double skipBelow = spacing > 0.0 ? m_apply.fade.startSamples * spacing : 0.0; | |
| 419 | 1084 | Scatter scatter(m_options, m_limit.get(), surface, region, wanted, reachRadii, skipBelow); | |
| 420 | 481297 | for (std::size_t n = 0; n < primaries.Size(); ++n) { | |
| 421 | 480214 | scatter.DrawSecondaries(primaries.At(n)); | |
| 422 | } | ||
| 423 | 1083 | outlines = scatter.TakeOutlines(); | |
| 424 | 2166 | return scatter.Take(); | |
| 425 | 1084 | } | |
| 426 | |||
| 427 | 1045 | void SecondaryCraterLayer::ApplySamples(SampleBlock& samples, const LayerContext& context) const { | |
| 428 | 1045 | const double spacing = samples.ResolvedSpacing(context.spacing, m_apply.surface); | |
| 429 | 1045 | std::unordered_map<std::uint64_t, SecondaryOutline> outlines; | |
| 430 | 1045 | const CraterList secondaries = Secondaries(samples.Bounds(), spacing, outlines); | |
| 431 | 1045 | CraterSampling sampling = SamplingFrom(context, spacing, this); | |
| 432 | 1045 | if (!outlines.empty()) { | |
| 433 | 1 | sampling.reshape = [&outlines](const Crater& crater, CraterOutline& outline) { | |
| 434 | 8 | const auto found = outlines.find(crater.id); | |
| 435 | 8 | if (found != outlines.end()) { | |
| 436 | 6 | outline.ellipticity = found->second.ellipticity; | |
| 437 | 6 | outline.heading = found->second.heading; | |
| 438 | } | ||
| 439 | 1 | }; | |
| 440 | } | ||
| 441 | 1045 | ApplyCraters(samples, secondaries, m_models, m_apply, sampling); | |
| 442 | 1045 | } | |
| 443 | |||
| 444 | } // namespace terraingen | ||
| 445 |