src/pipeline/materials.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/pipeline/materials.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/fade.h" | ||
| 6 | #include "terraingen/pipeline/materials_fields.h" | ||
| 7 | #include "terraingen/util/log.h" | ||
| 8 | #include "terraingen/util/parallel.h" | ||
| 9 | #include "util/checks.h" | ||
| 10 | |||
| 11 | #include <cstddef> | ||
| 12 | #include <optional> | ||
| 13 | #include <utility> | ||
| 14 | |||
| 15 | namespace terraingen { | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | |||
| 19 | /// Width of the patch noise band over which dust comes in. | ||
| 20 | constexpr double kDustEdge = 0.2; | ||
| 21 | |||
| 22 | /// The layers before the first that lays a material: the bare ground's. | ||
| 23 | 13523 | std::size_t BareLayers(const Terrain& terrain) { | |
| 24 | 13523 | const std::vector<LayerPtr>& layers = terrain.Layers(); | |
| 25 | 13523 | std::size_t n = 0; | |
| 26 | 27046 | while (n < layers.size() && dynamic_cast<const MaterialSource*>(layers[n].get()) == nullptr) { | |
| 27 | 13523 | ++n; | |
| 28 | } | ||
| 29 | 13523 | return n; | |
| 30 | } | ||
| 31 | |||
| 32 | /// `options`, after checking them. | ||
| 33 | 13523 | const MaterialOptions& Checked(const MaterialOptions& options) { | |
| 34 | 13523 | ValidateMaterialOptions(options); | |
| 35 | 13523 | return options; | |
| 36 | } | ||
| 37 | |||
| 38 | /// The shares of a terrain before any material layer: soil, bedrock where the bare ground is | ||
| 39 | /// exposed and dust patches. | ||
| 40 | 13523 | class GroundCover { | |
| 41 | public: | ||
| 42 | /// @throws std::invalid_argument as `ValidateMaterialOptions`. | ||
| 43 | 13523 | GroundCover(const Terrain& terrain, const MaterialOptions& options) | |
| 44 | 13523 | : m_surface(terrain.Surface()), m_options(Checked(options)), | |
| 45 | 13523 | m_bare(terrain.Beneath(BareLayers(terrain))) { | |
| 46 | 13523 | if (options.exposure) { | |
| 47 | 13522 | m_field.emplace(*options.exposure, m_surface); | |
| 48 | } | ||
| 49 | 13523 | } | |
| 50 | |||
| 51 | /// The shares at surface point (`x`, `y`), reading the ground at `spacing` meters. | ||
| 52 | 108104 | MaterialShares At(double x, double y, double spacing) const { | |
| 53 | 108104 | MaterialShares shares = SoilShares(); | |
| 54 | 108107 | if (m_field) { | |
| 55 | 108115 | CoverWith(shares, MaterialClass::Bedrock, m_field->Exposure(m_bare, x, y, spacing)); | |
| 56 | } | ||
| 57 | 108111 | if (m_options.dustCover > 0.0) { | |
| 58 | 98011 | const double noise = PatchNoise(m_surface, x, y, m_options.dustPatch, m_options.seed); | |
| 59 | 98002 | const double threshold = 1.0 - 2.0 * m_options.dustCover; | |
| 60 | 98002 | CoverWith(shares, MaterialClass::Dust, Smoothstep((noise - threshold) / kDustEdge)); | |
| 61 | } | ||
| 62 | 108100 | return shares; | |
| 63 | } | ||
| 64 | |||
| 65 | private: | ||
| 66 | const Surface& m_surface; | ||
| 67 | const MaterialOptions& m_options; | ||
| 68 | Terrain m_bare; | ||
| 69 | std::optional<ExposureField> m_field; | ||
| 70 | }; | ||
| 71 | |||
| 72 | } // namespace | ||
| 73 | |||
| 74 | 13535 | void ValidateMaterialOptions(const MaterialOptions& options) { | |
| 75 | 13535 | CheckRanges(options); | |
| 76 | 13532 | if (options.exposure) { | |
| 77 | 13526 | ValidateExposureParams(*options.exposure); | |
| 78 | } | ||
| 79 | 13532 | } | |
| 80 | |||
| 81 | 13512 | MaterialShares MaterialsAt(const Terrain& terrain, const MaterialOptions& options, double x, | |
| 82 | double y, double spacing) { | ||
| 83 | 13512 | const GroundCover ground(terrain, options); | |
| 84 | 13512 | RequirePositive(spacing, "Material spacing"); | |
| 85 | 13511 | MaterialShares shares = ground.At(x, y, spacing); | |
| 86 | 13512 | ForEachLayer<MaterialSource>(terrain, [&](const MaterialSource& source, std::size_t n) { | |
| 87 | 6581 | const Terrain beneath = terrain.Beneath(n); | |
| 88 | 6581 | CoverWith(shares, source.Material(), | |
| 89 | 6581 | source.MaterialCoverAt(x, y, beneath.Context(spacing, 1))); | |
| 90 | 6581 | }); | |
| 91 | 13511 | return shares; | |
| 92 | 13511 | } | |
| 93 | |||
| 94 | 11 | std::vector<Raster<float>> EvaluateMaterials(const Terrain& terrain, const MaterialOptions& options, | |
| 95 | const RasterGrid& grid, | ||
| 96 | const EvaluationOptions& evaluation) { | ||
| 97 | 11 | const GroundCover ground(terrain, options); | |
| 98 | 11 | RequireNonNegative(evaluation.spacing, "Material spacing"); | |
| 99 | 10 | const double resolved = ResolvedSpacing(grid, evaluation.spacing, terrain.Surface()); | |
| 100 | 12 | const Backend backend = ResolveBackend(evaluation.backend.value_or(DefaultBackend())); | |
| 101 | 10 | std::vector<std::pair<MaterialClass, Raster<double>>> covers; | |
| 102 | 10 | ForEachLayer<MaterialSource>(terrain, [&](const MaterialSource& source, std::size_t n) { | |
| 103 | 13 | const Stopwatch stopwatch; | |
| 104 | 13 | const Terrain beneath = terrain.Beneath(n); | |
| 105 | 13 | LayerContext context = beneath.Context(resolved, evaluation.threads); | |
| 106 | 13 | context.backend = backend; | |
| 107 | 13 | covers.emplace_back(source.Material(), source.MaterialCoverOver(grid, context)); | |
| 108 | 13 | if (evaluation.onLayer) { | |
| 109 | 2 | evaluation.onLayer(n, stopwatch.Milliseconds()); | |
| 110 | } | ||
| 111 | 13 | }); | |
| 112 | 20 | std::vector<Raster<float>> bands(kMaterialClasses, Raster<float>(grid)); | |
| 113 | 10 | ParallelFor(grid.height, evaluation.threads, [&](std::size_t row) { | |
| 114 | 95225 | for (std::size_t col = 0; col < grid.width; ++col) { | |
| 115 | 94584 | const Vec2d center = bands[0].CellCenter(col, row); | |
| 116 | 94584 | MaterialShares shares = ground.At(center.x, center.y, resolved); | |
| 117 | 192891 | for (const auto& [material, cover] : covers) { | |
| 118 | 98295 | CoverWith(shares, material, cover(col, row)); | |
| 119 | } | ||
| 120 | 563616 | for (std::size_t k = 0; k < kMaterialClasses; ++k) { | |
| 121 | 469032 | bands[k](col, row) = static_cast<float>(shares[k]); | |
| 122 | } | ||
| 123 | } | ||
| 124 | 641 | }); | |
| 125 | 20 | return bands; | |
| 126 | 20 | } | |
| 127 | |||
| 128 | } // namespace terraingen | ||
| 129 |