src/layers/boulders/boulder_layer.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/generation/cell_cache.h" | ||
| 7 | |||
| 8 | #include "terraingen/geometry/cells.h" | ||
| 9 | #include "terraingen/layers/craters/ages/age_model.h" | ||
| 10 | #include "terraingen/layers/source_resolved.h" | ||
| 11 | #include "terraingen/noise/hash.h" | ||
| 12 | #include "terraingen/random/stream.h" | ||
| 13 | #include "terraingen/raster/grid.h" | ||
| 14 | #include "terraingen/util/bits.h" | ||
| 15 | #include "terraingen/util/parallel.h" | ||
| 16 | #include "terraingen/util/span.h" | ||
| 17 | |||
| 18 | #include <algorithm> | ||
| 19 | #include <cmath> | ||
| 20 | #include <limits> | ||
| 21 | #include <memory> | ||
| 22 | #include <optional> | ||
| 23 | #include <stdexcept> | ||
| 24 | #include <unordered_map> | ||
| 25 | #include <utility> | ||
| 26 | #include <vector> | ||
| 27 | |||
| 28 | namespace terraingen { | ||
| 29 | |||
| 30 | namespace { | ||
| 31 | |||
| 32 | /// The ground under one candidate: its heights at a few points, at one spacing, read up front | ||
| 33 | /// with every other candidate's; any other point or spacing is read from the ground beneath. | ||
| 34 | /// Gives the same heights either way. | ||
| 35 | 325360 | class CandidateGround final : public Ground { | |
| 36 | public: | ||
| 37 | 325360 | CandidateGround(const Ground& ground, Span<const Vec2d> points, Span<const float> heights, | |
| 38 | double spacing) | ||
| 39 | 325360 | : m_ground(ground), m_points(points), m_heights(heights), m_spacing(spacing) {} | |
| 40 | |||
| 41 | protected: | ||
| 42 | 686163 | void FillSamples(SampleBlock& samples, double spacing, unsigned /*threads*/) const override { | |
| 43 | 2459658 | for (std::size_t i = 0; i < samples.Size(); ++i) { | |
| 44 | 1773495 | const Vec2d p = samples.Position(i); | |
| 45 | 1773495 | const std::optional<float> held = Held(p, spacing); | |
| 46 | 1773495 | samples.Height(i) = held ? *held : m_ground.HeightAt(p.x, p.y, spacing); | |
| 47 | } | ||
| 48 | 686163 | } | |
| 49 | |||
| 50 | private: | ||
| 51 | /// The height read up front at `p` and `spacing`, if any. | ||
| 52 | 1773495 | std::optional<float> Held(const Vec2d& p, double spacing) const { | |
| 53 | 1773495 | if (spacing == m_spacing) { | |
| 54 | 8439343 | for (std::size_t n = 0; n < m_points.size(); ++n) { | |
| 55 | 7231643 | if (BitCast<std::uint64_t>(m_points[n].x) == BitCast<std::uint64_t>(p.x) && | |
| 56 | 753451 | BitCast<std::uint64_t>(m_points[n].y) == BitCast<std::uint64_t>(p.y)) { | |
| 57 | 565795 | return m_heights[n]; | |
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | 1207700 | return std::nullopt; | |
| 62 | } | ||
| 63 | |||
| 64 | const Ground& m_ground; | ||
| 65 | Span<const Vec2d> m_points; | ||
| 66 | Span<const float> m_heights; | ||
| 67 | double m_spacing; | ||
| 68 | }; | ||
| 69 | |||
| 70 | /// How far in radii a boulder shape can reach from its center: half the diagonal of its unit box | ||
| 71 | /// over half its longest axis, for a shape tilted or resting on a side. | ||
| 72 | constexpr double kShapeReachRadii = 1.7320508075688772; | ||
| 73 | |||
| 74 | } // namespace | ||
| 75 | |||
| 76 | 1715337 | RandomStream BoulderLayer::AttributeStream(std::uint64_t id, BoulderDraw draw) const { | |
| 77 | 1715337 | const IdWords words = SplitId(id); | |
| 78 | 1715337 | return RandomStream(m_options.seed, kBoulderAttributeStream, words.low, words.high, | |
| 79 | 1715337 | static_cast<std::uint32_t>(draw)); | |
| 80 | } | ||
| 81 | |||
| 82 | 250 | BoulderLayer::BoulderLayer(BoulderLayerOptions options, std::shared_ptr<const CraterLayer> craters, | |
| 83 | 250 | TerrainBase base) | |
| 84 | : BoulderLayer(std::move(options), | ||
| 85 | 604 | craters ? std::vector<std::shared_ptr<const CraterLayer>>{std::move(craters)} | |
| 86 | : std::vector<std::shared_ptr<const CraterLayer>>{}, | ||
| 87 | 784 | std::move(base)) {} | |
| 88 | |||
| 89 | 360 | BoulderLayer::BoulderLayer(BoulderLayerOptions options, | |
| 90 | std::vector<std::shared_ptr<const CraterLayer>> craters, | ||
| 91 | 360 | TerrainBase base) | |
| 92 | 360 | : m_options(std::move(options)), m_craters(std::move(craters)), | |
| 93 | 360 | m_surface(std::move(base.surface)), m_source(std::move(base.source)), | |
| 94 | 364 | m_completeness(m_options.completeness) { | |
| 95 | 451 | for (const auto& layer : m_craters) { | |
| 96 | 93 | if (!layer) { | |
| 97 | 1 | throw std::invalid_argument("A boulder layer's crater layers must not be null"); | |
| 98 | } | ||
| 99 | 92 | if (!(layer->LayerBase()->surface == m_surface)) { | |
| 100 | 1 | throw std::invalid_argument( | |
| 101 | 1 | "A boulder layer and its crater layer must share a surface"); | |
| 102 | } | ||
| 103 | } | ||
| 104 | 358 | ValidateBoulderLayerOptions(m_options); | |
| 105 | 324 | if (auto limit = LayerResolvedLimit(m_source, m_options.sourceResolvesPixels, m_surface)) { | |
| 106 | 1 | m_completeness = m_completeness | |
| 107 | 2 | ? std::make_shared<const MinimumField>(m_completeness, limit) | |
| 108 | 1 | : std::move(limit); | |
| 109 | } | ||
| 110 | 405 | for (const ManualBoulder& boulder : m_options.manual) { | |
| 111 | 81 | if (boulder.FixesAny()) { | |
| 112 | 40 | m_fixed.emplace(ManualBoulderId(boulder), boulder); | |
| 113 | } | ||
| 114 | 81 | m_largestManual = std::fmax(m_largestManual, boulder.diameter); | |
| 115 | } | ||
| 116 | 324 | m_cells = MakeCells(m_surface); | |
| 117 | 324 | SetUpShapeCaches(); | |
| 118 | 324 | if (!m_options.generateRandom) { | |
| 119 | return; | ||
| 120 | } | ||
| 121 | 302 | m_minCraterDiameter = m_options.ejecta.ThrowingDiameter().value_or(0.0); | |
| 122 | 302 | if (m_minCraterDiameter > 0.0 && m_craters.empty()) { | |
| 123 | 1 | throw std::invalid_argument( | |
| 124 | 1 | "Boulder ejecta need the crater layer whose craters throw them"); | |
| 125 | } | ||
| 126 | 301 | SetUpAbundance(); | |
| 127 | 301 | if (m_options.cacheBoulders > 0) { | |
| 128 | 301 | m_cache = std::make_shared<CraterCellCache>(m_options.cacheBoulders); | |
| 129 | } | ||
| 130 | 301 | m_exposure = LayerExposure(m_options.bedrock.exposure, m_options.groundSpacing, | |
| 131 | 301 | "Boulders that read the exposure field"); | |
| 132 | 301 | if (m_exposure && m_options.densityMap) { | |
| 133 | 2 | SetUpBedrockMeans(); | |
| 134 | } | ||
| 135 | 77 | } | |
| 136 | |||
| 137 | 18 | std::shared_ptr<BoulderLayer> BoulderLayer::Manual(std::vector<ManualBoulder> boulders, | |
| 138 | BoulderLayerOptions options, TerrainBase base) { | ||
| 139 | 18 | if (!options.manual.empty()) { | |
| 140 | 1 | throw std::invalid_argument( | |
| 141 | 1 | "BoulderLayer::Manual takes its boulders as an argument; leave options.manual empty"); | |
| 142 | } | ||
| 143 | 17 | options.generateRandom = false; | |
| 144 | 17 | options.manual = std::move(boulders); | |
| 145 | 17 | return std::make_shared<BoulderLayer>(std::move(options), std::shared_ptr<const CraterLayer>{}, | |
| 146 | 16 | std::move(base)); | |
| 147 | } | ||
| 148 | |||
| 149 | 13 | LayerPtr BoulderLayer::Reseeded(std::uint64_t seed) const { | |
| 150 | 13 | BoulderLayerOptions options = m_options; | |
| 151 | 13 | options.seed = ReseedWord(seed, 0); | |
| 152 | 13 | options.rendering.shapes.seed = ReseedWord(seed, 1); | |
| 153 | 26 | return std::make_shared<BoulderLayer>(std::move(options), m_craters, *LayerBase()); | |
| 154 | 13 | } | |
| 155 | |||
| 156 | 22 | LayerPtr BoulderLayer::Rebound(const LayerReplacements& replaced) const { | |
| 157 | 22 | std::vector<std::shared_ptr<const CraterLayer>> craters; | |
| 158 | 40 | for (const std::shared_ptr<const CraterLayer>& layer : m_craters) { | |
| 159 | 18 | craters.push_back(Replacement(layer, replaced)); | |
| 160 | } | ||
| 161 | 22 | if (craters == m_craters) { | |
| 162 | 10 | return nullptr; | |
| 163 | } | ||
| 164 | 12 | return std::make_shared<BoulderLayer>(m_options, std::move(craters), *LayerBase()); | |
| 165 | 22 | } | |
| 166 | |||
| 167 | 3 | LayerPtr BoulderLayer::Rebased(const TerrainBase& base) const { | |
| 168 | 3 | return std::make_shared<BoulderLayer>(m_options, m_craters, base); | |
| 169 | } | ||
| 170 | |||
| 171 | 65856 | double BoulderLayer::MaxFeatureDiameter() const { | |
| 172 | 65856 | return std::fmax(m_options.generateRandom ? m_options.sizes.maxDiameter : 0.0, m_largestManual); | |
| 173 | } | ||
| 174 | |||
| 175 | 301 | void BoulderLayer::SetUpAbundance() { | |
| 176 | 301 | m_background.emplace(m_options); | |
| 177 | 301 | m_clusterSeed = SubstreamSeed(m_options.seed, kBoulderClusterStream); | |
| 178 | 301 | m_patchSeed = SubstreamSeed(m_options.seed, kBoulderPatchStream); | |
| 179 | 301 | m_candidates = BoulderCandidates(m_options, m_background->max); | |
| 180 | 301 | if (m_options.slopeMap) { | |
| 181 | 3 | m_referenceCount = | |
| 182 | 3 | m_options.sizes.distribution->Cumulative(m_options.slopeMap->referenceDiameter); | |
| 183 | } | ||
| 184 | 301 | if (m_options.clusters.share > 0.0) { | |
| 185 | 10 | m_clusterMean = m_options.clusters.model == BoulderClusterModel::Thomas | |
| 186 | 10 | ? MeanThomasField(m_options.clusters, m_surface.IsBody()) | |
| 187 | 4 | : MeanClusterBump(m_options.clusters); | |
| 188 | } | ||
| 189 | 301 | m_ejectaSizes = m_minCraterDiameter > 0.0 ? EjectaSizes(m_options) : nullptr; | |
| 190 | 301 | } | |
| 191 | |||
| 192 | 2 | void BoulderLayer::SetUpBedrockMeans() { | |
| 193 | 2 | constexpr std::size_t kMeanCells = 1u << 16; | |
| 194 | 2 | const double side = m_options.bedrock.meanSize / m_surface.UnitLength(); | |
| 195 | 2 | m_bedrockCellSize = {side, side}; | |
| 196 | 2 | if (const auto* raster = dynamic_cast<const RasterField*>(m_options.densityMap->field.get())) { | |
| 197 | 1 | const GeoTransform& transform = raster->Values().Transform(); | |
| 198 | 1 | m_bedrockOrigin = {transform.originX, transform.originY}; | |
| 199 | 1 | m_bedrockCellSize = {transform.pixelWidth, transform.pixelHeight}; | |
| 200 | } | ||
| 201 | 2 | m_bedrockMeans = std::make_shared<LruCache<BedrockCell, double, BedrockCellHash>>( | |
| 202 | 2 | kMeanCells, [](const double&) { return std::size_t{1}; }); | |
| 203 | 2 | } | |
| 204 | |||
| 205 | 324 | void BoulderLayer::SetUpShapeCaches() { | |
| 206 | 324 | const std::uint32_t prototypes = m_options.rendering.shapePrototypes; | |
| 207 | 972 | m_shapes = std::make_shared<LruCache<std::uint64_t, BoulderShape>>( | |
| 208 | 327 | prototypes > 0 ? prototypes : 4096, [](const BoulderShape&) { return std::size_t{1}; }); | |
| 209 | 972 | m_fragments = std::make_shared<LruCache<std::uint64_t, std::vector<BoulderFragment>>>( | |
| 210 | 324 | prototypes > 0 ? prototypes : 4096, | |
| 211 | 324 | [](const std::vector<BoulderFragment>&) { return std::size_t{1}; }); | |
| 212 | 324 | } | |
| 213 | |||
| 214 | 16886 | std::shared_ptr<const BoulderShape> BoulderLayer::CachedShape(std::uint64_t shapeSeed) const { | |
| 215 | 16886 | return m_shapes->Get( | |
| 216 | 34764 | shapeSeed, [&] { return GenerateBoulderShape(shapeSeed, m_options.rendering.shapes); }); | |
| 217 | } | ||
| 218 | |||
| 219 | std::shared_ptr<const std::vector<BoulderFragment>> | ||
| 220 | 5412 | BoulderLayer::CachedFragments(std::uint64_t shapeSeed) const { | |
| 221 | 5412 | return m_fragments->Get(shapeSeed, [&] { | |
| 222 | 377 | const BoulderFragmentation& fragmentation = *m_options.fragmentation; | |
| 223 | 377 | const auto span = | |
| 224 | 377 | static_cast<std::uint32_t>(fragmentation.maxPieces - fragmentation.minPieces + 1); | |
| 225 | 377 | RandomStream random = AttributeStream(shapeSeed, BoulderDraw::Fragments); | |
| 226 | 377 | const int pieces = fragmentation.minPieces + static_cast<int>(random.NextU32() % span); | |
| 227 | 377 | const BoulderCracks cracks = random.NextDouble() < fragmentation.slabShare | |
| 228 | 377 | ? BoulderCracks::Slabs | |
| 229 | 377 | : BoulderCracks::Radial; | |
| 230 | 754 | return FragmentBoulderShape(*CachedShape(shapeSeed), pieces, shapeSeed, cracks); | |
| 231 | 10824 | }); | |
| 232 | } | ||
| 233 | |||
| 234 | 16960 | std::shared_ptr<const BoulderShape> BoulderLayer::Shape(const Boulder& boulder) const { | |
| 235 | 16960 | const std::uint64_t seed = ShapeSeed(boulder, m_options.rendering.shapePrototypes); | |
| 236 | 16960 | if (boulder.parent == 0) { | |
| 237 | 16509 | return CachedShape(seed); | |
| 238 | } | ||
| 239 | 451 | const auto fragments = CachedFragments(seed); | |
| 240 | 902 | return {fragments, &(*fragments)[boulder.piece].shape}; | |
| 241 | 451 | } | |
| 242 | |||
| 243 | 636597 | double BoulderLayer::OwnMarginRadii() const { | |
| 244 | 636597 | const double body = m_options.rendering.relief == BoulderRelief::Shape ? kShapeReachRadii : 1.0; | |
| 245 | 1273194 | const double fillet = | |
| 246 | 636597 | m_options.fillet.heightShare > 0.0 ? 1.0 + 2.0 * m_options.fillet.widthRatio : 1.0; | |
| 247 | 636597 | return std::fmax(body, fillet); | |
| 248 | } | ||
| 249 | |||
| 250 | 142694 | double BoulderLayer::BreakMarginRadii() const { | |
| 251 | 142694 | const std::optional<BoulderFragmentation>& fragmentation = m_options.fragmentation; | |
| 252 | 142694 | return fragmentation ? 2.0 * (fragmentation->gapRatio + fragmentation->spreadRatio) : 0.0; | |
| 253 | } | ||
| 254 | |||
| 255 | 4 | double BoulderLayer::GroundReach(double spacing) const { | |
| 256 | 4 | const double rim = m_options.rendering.relief == BoulderRelief::Dome ? 0.5 * spacing : 0.0; | |
| 257 | 4 | const double roll = m_options.pose.rollDistance.value_or(0.0); | |
| 258 | 4 | const double radius = 0.5 * MaxFeatureDiameter(); | |
| 259 | 4 | return rim + 2.0 * roll + radius * (OwnMarginRadii() + BreakMarginRadii()) + | |
| 260 | 4 | std::fmax(radius, m_exposure.Reach()); | |
| 261 | } | ||
| 262 | |||
| 263 | 139074 | CraterList BoulderLayer::ScatterBoulderCandidates(const Bounds2d& region, double smallest) const { | |
| 264 | 139074 | const double margin = OwnMarginRadii() + BreakMarginRadii(); | |
| 265 | 139074 | if (!m_options.generateRandom) { | |
| 266 | 9796 | CraterList manual; | |
| 267 | 19615 | for (const ManualBoulder& boulder : m_options.manual) { | |
| 268 | 9819 | const Vec2d center{boulder.x, boulder.y}; | |
| 269 | 9819 | if (m_surface.Contains(m_surface.Expand(region, 0.5 * margin * boulder.diameter), | |
| 270 | center)) { | ||
| 271 | 9617 | manual.Append( | |
| 272 | 9617 | {boulder.x, boulder.y, boulder.diameter, 0.0, ManualBoulderId(boulder), true}); | |
| 273 | } | ||
| 274 | } | ||
| 275 | 9796 | return manual; | |
| 276 | 9796 | } | |
| 277 | 129278 | const std::uint32_t seed = SubstreamSeed(m_options.seed, kBoulderCandidateStream); | |
| 278 | 129278 | const FreshAges ages; | |
| 279 | 129278 | CraterGenerationOptions generation; | |
| 280 | 129278 | generation.minDiameter = m_options.sizes.minDiameter; | |
| 281 | 129278 | generation.maxDiameter = m_options.sizes.maxDiameter; | |
| 282 | 129278 | generation.marginRadii = margin; | |
| 283 | 129278 | generation.surface = m_surface; | |
| 284 | 129278 | generation.logName = "boulders"; | |
| 285 | 129278 | if (smallest > 0.0) { | |
| 286 | 129105 | generation.minResolvedDiameter = smallest; | |
| 287 | } | ||
| 288 | 129304 | for (const ManualBoulder& boulder : m_options.manual) { | |
| 289 | 26 | generation.manual.Append( | |
| 290 | 29 | {boulder.x, boulder.y, boulder.diameter, 0.0, boulder.id.value_or(0u), true}); | |
| 291 | } | ||
| 292 | 129278 | generation.completeness = m_completeness; | |
| 293 | 129278 | return GenerateCachedCraters(m_cache.get(), *m_cells, region, *m_candidates, ages, seed, | |
| 294 | 129278 | std::move(generation)); | |
| 295 | 129278 | } | |
| 296 | |||
| 297 | 139074 | std::vector<Boulder> BoulderLayer::DrawBoulders(const CraterList& candidates, double spacing, | |
| 298 | const Ground* ground, const EjectaIndex& ejecta, | ||
| 299 | unsigned threads) const { | ||
| 300 | 139074 | std::vector<ThinnedCandidate> thinned; | |
| 301 | 860108 | for (std::size_t n = 0; n < candidates.Size(); ++n) { | |
| 302 | 721034 | if (std::optional<ThinnedCandidate> one = Thin(candidates.At(n), ground, ejecta)) { | |
| 303 | 485622 | thinned.push_back(*one); | |
| 304 | } | ||
| 305 | } | ||
| 306 | 139074 | std::vector<Boulder> boulders; | |
| 307 | 139074 | boulders.reserve(thinned.size()); | |
| 308 | 139074 | if (ground == nullptr) { | |
| 309 | 287219 | for (const ThinnedCandidate& one : thinned) { | |
| 310 | 160262 | if (std::optional<Boulder> boulder = PlaceCandidate(one, spacing, nullptr)) { | |
| 311 | 160262 | boulders.push_back(*boulder); | |
| 312 | } | ||
| 313 | } | ||
| 314 | return boulders; | ||
| 315 | } | ||
| 316 | 12117 | const BoulderPose& pose = m_options.pose; | |
| 317 | 12117 | const std::size_t each = pose.followSlope || pose.maxSlopeDegrees.has_value() ? 5 : 1; | |
| 318 | 12117 | std::vector<Vec2d> points; | |
| 319 | 12117 | points.reserve(thinned.size() * each); | |
| 320 | 337477 | for (const ThinnedCandidate& one : thinned) { | |
| 321 | 325360 | const Crater& c = one.candidate; | |
| 322 | 325360 | points.push_back({c.x, c.y}); | |
| 323 | 325360 | if (each == 5) { | |
| 324 | 310585 | for (const Vec2d& p : GradientPoints(m_surface, {c.x, c.y}, 0.5 * c.diameter)) { | |
| 325 | 248468 | points.push_back(p); | |
| 326 | } | ||
| 327 | } | ||
| 328 | } | ||
| 329 | 12117 | std::vector<float> heights(points.size()); | |
| 330 | 12117 | ground->HeightsAt(points, heights, m_options.groundSpacing, threads); | |
| 331 | 337477 | for (std::size_t n = 0; n < thinned.size(); ++n) { | |
| 332 | 325360 | const CandidateGround under(*ground, Span(points).subspan(n * each, each), | |
| 333 | 650720 | Span(heights).subspan(n * each, each), m_options.groundSpacing); | |
| 334 | 325360 | if (std::optional<Boulder> boulder = PlaceCandidate(thinned[n], spacing, &under)) { | |
| 335 | 323719 | boulders.push_back(*boulder); | |
| 336 | } | ||
| 337 | 325360 | } | |
| 338 | 12117 | return boulders; | |
| 339 | 139074 | } | |
| 340 | |||
| 341 | std::optional<BoulderLayer::ThinnedCandidate> | ||
| 342 | 721033 | BoulderLayer::Thin(const Crater& candidate, const Ground* ground, const EjectaIndex& ejecta) const { | |
| 343 | 721033 | if (!m_options.generateRandom) { | |
| 344 | 9617 | return ThinnedCandidate{candidate, BoulderAbundance{}}; | |
| 345 | } | ||
| 346 | 711416 | ThinnedCandidate thinned{candidate, Abundance(candidate.x, candidate.y, ground, ejecta)}; | |
| 347 | 711417 | if (!candidate.manual) { | |
| 348 | 711392 | thinned.density = Density(candidate.diameter, thinned.abundance); | |
| 349 | 711392 | RandomStream random = AttributeStream(candidate.id, BoulderDraw::Keep); | |
| 350 | 711392 | if (random.NextDouble() >= | |
| 351 | 711392 | thinned.density / m_candidates->Incremental(candidate.diameter)) { | |
| 352 | 235412 | return std::nullopt; | |
| 353 | } | ||
| 354 | } | ||
| 355 | 476005 | return thinned; | |
| 356 | } | ||
| 357 | |||
| 358 | namespace { | ||
| 359 | |||
| 360 | 62420 | BoulderList ToBoulderList(const std::vector<Boulder>& boulders) { | |
| 361 | 62420 | BoulderList list; | |
| 362 | 62420 | list.Reserve(boulders.size()); | |
| 363 | 438518 | for (const Boulder& boulder : boulders) { | |
| 364 | 376098 | list.Append(boulder); | |
| 365 | } | ||
| 366 | 62420 | return list; | |
| 367 | } | ||
| 368 | |||
| 369 | } // namespace | ||
| 370 | |||
| 371 | 62362 | BoulderList BoulderLayer::Boulders(const Bounds2d& region, double spacing, | |
| 372 | const Ground* ground) const { | ||
| 373 | 62362 | return ToBoulderList(BoulderRecords(region, spacing, ground, 0)); | |
| 374 | } | ||
| 375 | |||
| 376 | 139016 | std::vector<Boulder> BoulderLayer::BoulderRecords(const Bounds2d& region, double spacing, | |
| 377 | const Ground* ground, unsigned threads) const { | ||
| 378 | 139016 | const double smallest = spacing > 0.0 ? m_options.fade.startSamples * spacing : 0.0; | |
| 379 | 139016 | return PlaceBoulders(region, spacing, smallest, ground, threads); | |
| 380 | } | ||
| 381 | |||
| 382 | 139075 | std::vector<Boulder> BoulderLayer::PlaceBoulders(const Bounds2d& region, double spacing, | |
| 383 | double smallest, const Ground* ground, | ||
| 384 | unsigned threads) const { | ||
| 385 | 139075 | m_exposure.Require(ground); | |
| 386 | 139074 | const double roll = ground != nullptr ? m_options.pose.rollDistance.value_or(0.0) : 0.0; | |
| 387 | 139074 | const Bounds2d from = roll > 0.0 ? m_surface.Expand(region, roll) : region; | |
| 388 | 617632 | const auto rests = [&](const Boulder& boulder) { | |
| 389 | 478558 | const double own = 0.5 * boulder.diameter * OwnMarginRadii(); | |
| 390 | 478558 | return roll == 0.0 || | |
| 391 | 58377 | m_surface.Contains(m_surface.Expand(region, own), {boulder.x, boulder.y}); | |
| 392 | 139074 | }; | |
| 393 | 139074 | const CraterList candidates = ScatterBoulderCandidates(from, smallest); | |
| 394 | 139074 | if (!m_options.fragmentation) { | |
| 395 | 135458 | std::vector<Boulder> boulders = | |
| 396 | 135458 | DrawBoulders(candidates, spacing, ground, EjectaCraters(from), threads); | |
| 397 | 135458 | boulders.erase(std::remove_if(boulders.begin(), boulders.end(), | |
| 398 | 478558 | [&](const Boulder& boulder) { return !rests(boulder); }), | |
| 399 | 135458 | boulders.end()); | |
| 400 | 135458 | return boulders; | |
| 401 | 135458 | } | |
| 402 | 3616 | const double reach = 0.5 * MaxFeatureDiameter() * (OwnMarginRadii() + BreakMarginRadii()); | |
| 403 | 3616 | const EjectaIndex ejecta = EjectaCraters(m_surface.Expand(from, reach)); | |
| 404 | 3616 | std::vector<Boulder> boulders; | |
| 405 | 3616 | std::vector<std::size_t> pieces; | |
| 406 | 9039 | for (const Boulder& parent : DrawBoulders(candidates, spacing, ground, ejecta, threads)) { | |
| 407 | 20768 | for (const Boulder& boulder : Break(parent, spacing, smallest)) { | |
| 408 | 15345 | const double own = 0.5 * boulder.diameter * OwnMarginRadii(); | |
| 409 | 15345 | if (m_surface.Contains(m_surface.Expand(region, own), {boulder.x, boulder.y})) { | |
| 410 | 13491 | if (boulder.parent != 0) { | |
| 411 | 13053 | pieces.push_back(boulders.size()); | |
| 412 | } | ||
| 413 | 13491 | boulders.push_back(boulder); | |
| 414 | } | ||
| 415 | 5423 | } | |
| 416 | 3616 | } | |
| 417 | 3616 | ReadGrounds(boulders, pieces, ground, threads); | |
| 418 | 3616 | return boulders; | |
| 419 | 139074 | } | |
| 420 | |||
| 421 | 59 | BoulderFeatures BoulderLayer::BouldersOver(const RasterGrid& grid, double spacing, | |
| 422 | const Ground* ground) const { | ||
| 423 | 59 | const std::vector<Boulder> boulders = | |
| 424 | 59 | PlaceBoulders(grid.Bounds(), ListedSpacing(grid, spacing, m_surface), 0.0, ground, 0); | |
| 425 | 59 | return {ToBoulderList(boulders), Models(boulders)}; | |
| 426 | 59 | } | |
| 427 | |||
| 428 | } // namespace terraingen | ||
| 429 |