src/pipeline/terrain.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/pipeline/terrain.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/evaluation_cache.h" | ||
| 6 | #include "terraingen/random/stream.h" | ||
| 7 | #include "terraingen/util/bits.h" | ||
| 8 | #include "terraingen/util/log.h" | ||
| 9 | #include "terraingen/util/parallel.h" | ||
| 10 | #include "util/checks.h" | ||
| 11 | |||
| 12 | #include <algorithm> | ||
| 13 | #include <cmath> | ||
| 14 | #include <cstdint> | ||
| 15 | #include <memory> | ||
| 16 | #include <optional> | ||
| 17 | #include <stdexcept> | ||
| 18 | #include <string> | ||
| 19 | #include <utility> | ||
| 20 | #include <vector> | ||
| 21 | |||
| 22 | namespace terraingen { | ||
| 23 | |||
| 24 | namespace { | ||
| 25 | |||
| 26 | /// Values the point queries of a terrain and its copies keep, such as fitted crater datums. | ||
| 27 | constexpr std::size_t kPointQueryValues = std::size_t{1} << 17U; | ||
| 28 | |||
| 29 | /// Points evaluated together, sharing the features they prepare. | ||
| 30 | constexpr std::size_t kBatch = 256; | ||
| 31 | |||
| 32 | /// The side in cells of the tiles a grid read of the ground is evaluated in, `kBatch` cells each. | ||
| 33 | constexpr std::size_t kTileSide = 16; | ||
| 34 | |||
| 35 | /// The 64-bit seed of part `part` of a terrain reseeded with `seed`: 0 the source, n + 1 layer n. | ||
| 36 | 66 | std::uint64_t PartSeed(std::uint64_t seed, std::size_t part) { | |
| 37 | 66 | const auto word = static_cast<std::uint32_t>(2 * part); | |
| 38 | 66 | return JoinWords(ReseedWord(seed, word), ReseedWord(seed, word + 1)); | |
| 39 | } | ||
| 40 | |||
| 41 | /// The sample spacing in meters `options` evaluate `grid` at on `surface`. | ||
| 42 | /// @throws std::invalid_argument for a negative or non-finite spacing. | ||
| 43 | 842 | double EvaluatedSpacing(const RasterGrid& grid, const EvaluationOptions& options, | |
| 44 | const Surface& surface) { | ||
| 45 | 842 | RequireNonNegative(options.spacing, "Sample spacing"); | |
| 46 | 840 | return ResolvedSpacing(grid, options.spacing, surface); | |
| 47 | } | ||
| 48 | |||
| 49 | void ReadStack(const Terrain& terrain, std::size_t layers, SampleBlock& samples, double spacing, | ||
| 50 | unsigned threads, EvaluationCache* cache); | ||
| 51 | |||
| 52 | /// The ground beneath layer `layers` of a terrain during one evaluation, whose reads share | ||
| 53 | /// `cache`. | ||
| 54 | 2508480 | class StackGround final : public Ground { | |
| 55 | public: | ||
| 56 | 2505853 | StackGround(const Terrain& terrain, std::size_t layers, EvaluationCache* cache) | |
| 57 | 57 | : m_terrain(terrain), m_layers(layers), m_cache(cache) {} | |
| 58 | |||
| 59 | 2504900 | LayerContext Context(double spacing, unsigned threads) const override { | |
| 60 | ✗ | LayerContext context = m_terrain.Context(spacing, threads); | |
| 61 | 2503879 | context.ground = this; | |
| 62 | 2503879 | return context; | |
| 63 | } | ||
| 64 | |||
| 65 | protected: | ||
| 66 | 43811 | void FillSamples(SampleBlock& samples, double spacing, unsigned threads) const override { | |
| 67 | 43811 | ReadStack(m_terrain, m_layers, samples, spacing, threads, m_cache); | |
| 68 | 43812 | } | |
| 69 | |||
| 70 | private: | ||
| 71 | const Terrain& m_terrain; | ||
| 72 | std::size_t m_layers; | ||
| 73 | EvaluationCache* m_cache; | ||
| 74 | }; | ||
| 75 | |||
| 76 | /// Sets `heights` to the terrain's source at `points`, then applies its first `layers` layers to | ||
| 77 | /// them all at once, each on the ground beneath it, which shares `cache`. | ||
| 78 | 2366249 | void EvaluateStack(const Terrain& terrain, std::size_t layers, Span<const Vec2d> points, | |
| 79 | Span<float> heights, double spacing, EvaluationCache* cache) { | ||
| 80 | 2366249 | if (terrain.Source()) { | |
| 81 | 2033305 | terrain.Source()->HeightsAt(points, heights, spacing / terrain.Surface().UnitLength()); | |
| 82 | } else { | ||
| 83 | 336493 | std::fill(heights.begin(), heights.end(), 0.0f); | |
| 84 | } | ||
| 85 | 4880771 | for (std::size_t n = 0; n < layers; ++n) { | |
| 86 | 2505140 | const StackGround ground(terrain, n, cache); | |
| 87 | 2505140 | SampleBlock samples(points, heights); | |
| 88 | 2504244 | LayerContext context = ground.Context(spacing, 1); | |
| 89 | 2503223 | context.cache = cache; | |
| 90 | 2503223 | terrain.Layers()[n]->Apply(samples, context); | |
| 91 | 2507824 | } | |
| 92 | 2375631 | } | |
| 93 | |||
| 94 | /// The Morton code of `point` on a 2^16 lattice over `bounds`. | ||
| 95 | 1281699 | std::uint32_t MortonKey(const Vec2d& point, const Bounds2d& bounds) { | |
| 96 | 3845097 | const auto quantize = [](double value, double low, double high) { | |
| 97 | 2563398 | const double share = high > low ? (value - low) / (high - low) : 0.0; | |
| 98 | 2563398 | return static_cast<std::uint32_t>(std::clamp(share, 0.0, 1.0) * 65535.0); | |
| 99 | }; | ||
| 100 | 3845097 | const auto spread = [](std::uint32_t v) { | |
| 101 | 2563398 | v = (v | (v << 8u)) & 0x00FF00FFu; | |
| 102 | 2563398 | v = (v | (v << 4u)) & 0x0F0F0F0Fu; | |
| 103 | 2563398 | v = (v | (v << 2u)) & 0x33333333u; | |
| 104 | 2563398 | v = (v | (v << 1u)) & 0x55555555u; | |
| 105 | 2563398 | return v; | |
| 106 | }; | ||
| 107 | 1281699 | return spread(quantize(point.x, bounds.minX, bounds.maxX)) | | |
| 108 | 1281699 | (spread(quantize(point.y, bounds.minY, bounds.maxY)) << 1u); | |
| 109 | } | ||
| 110 | |||
| 111 | /// The indices of `points`, fewer than 2^32, sorted by their Morton codes over the points' | ||
| 112 | /// bounds, ties by index. | ||
| 113 | 95 | std::vector<std::size_t> MortonOrder(Span<const Vec2d> points) { | |
| 114 | 95 | Bounds2d bounds{points[0].x, points[0].y, points[0].x, points[0].y}; | |
| 115 | 1281794 | for (const Vec2d& p : points) { | |
| 116 | 1281699 | bounds = {std::fmin(bounds.minX, p.x), std::fmin(bounds.minY, p.y), | |
| 117 | 1281699 | std::fmax(bounds.maxX, p.x), std::fmax(bounds.maxY, p.y)}; | |
| 118 | } | ||
| 119 | 95 | std::vector<std::uint64_t> keyed(points.size()); | |
| 120 | 1281794 | for (std::size_t i = 0; i < points.size(); ++i) { | |
| 121 | 1281699 | keyed[i] = (std::uint64_t{MortonKey(points[i], bounds)} << 32U) | std::uint64_t{i}; | |
| 122 | } | ||
| 123 | 95 | std::sort(keyed.begin(), keyed.end()); | |
| 124 | 95 | std::vector<std::size_t> order(points.size()); | |
| 125 | 1281794 | for (std::size_t i = 0; i < points.size(); ++i) { | |
| 126 | 1281699 | order[i] = static_cast<std::size_t>(keyed[i] & 0xFFFFFFFFU); | |
| 127 | } | ||
| 128 | 95 | return order; | |
| 129 | 95 | } | |
| 130 | |||
| 131 | /// `EvaluateStack` over `points` in batches on `threads` threads, the points taken in Morton order | ||
| 132 | /// so each batch covers a small area and prepares only the features there. | ||
| 133 | 2365136 | void EvaluatePoints(const Terrain& terrain, std::size_t layers, Span<const Vec2d> points, | |
| 134 | Span<float> heights, double spacing, unsigned threads, EvaluationCache* cache) { | ||
| 135 | 2365136 | if (points.size() <= kBatch) { | |
| 136 | 2365041 | EvaluateStack(terrain, layers, points, heights, spacing, cache); | |
| 137 | 2371049 | return; | |
| 138 | } | ||
| 139 | 95 | const std::vector<std::size_t> order = MortonOrder(points); | |
| 140 | 95 | std::vector<Vec2d> sorted(points.size()); | |
| 141 | 1281794 | for (std::size_t i = 0; i < points.size(); ++i) { | |
| 142 | 1281699 | sorted[i] = points[order[i]]; | |
| 143 | } | ||
| 144 | 95 | std::vector<float> values(points.size()); | |
| 145 | 95 | ParallelFor(CeilDiv(points.size(), kBatch), threads, [&](std::size_t batch) { | |
| 146 | 5051 | const std::size_t begin = batch * kBatch; | |
| 147 | 5051 | const std::size_t count = std::min(kBatch, points.size() - begin); | |
| 148 | 10108 | EvaluateStack(terrain, layers, Span<const Vec2d>(sorted).subspan(begin, count), | |
| 149 | 5053 | Span<float>(values).subspan(begin, count), spacing, cache); | |
| 150 | 10104 | }); | |
| 151 | 1281794 | for (std::size_t i = 0; i < points.size(); ++i) { | |
| 152 | 1281699 | heights[order[i]] = values[i]; | |
| 153 | } | ||
| 154 | 95 | } | |
| 155 | |||
| 156 | /// `EvaluateStack` over the cells of the grid block `samples` in tiles of `kTileSide` squared | ||
| 157 | /// cells, which are already near each other, on `threads` threads. | ||
| 158 | ✗ | void EvaluateTiles(const Terrain& terrain, std::size_t layers, const SampleBlock& samples, | |
| 159 | double spacing, unsigned threads, EvaluationCache* cache) { | ||
| 160 | ✗ | const std::size_t width = samples.Grid()->Width(); | |
| 161 | ✗ | const std::size_t height = samples.Grid()->Height(); | |
| 162 | ✗ | const std::size_t across = CeilDiv(width, kTileSide); | |
| 163 | ✗ | ParallelFor(across * CeilDiv(height, kTileSide), threads, [&](std::size_t tile) { | |
| 164 | ✗ | const std::size_t col = (tile % across) * kTileSide; | |
| 165 | ✗ | const std::size_t row = (tile / across) * kTileSide; | |
| 166 | ✗ | const std::size_t cols = std::min(kTileSide, width - col); | |
| 167 | ✗ | const std::size_t rows = std::min(kTileSide, height - row); | |
| 168 | ✗ | std::vector<Vec2d> points(cols * rows); | |
| 169 | ✗ | for (std::size_t k = 0; k < points.size(); ++k) { | |
| 170 | ✗ | points[k] = samples.Position((row + k / cols) * width + col + k % cols); | |
| 171 | } | ||
| 172 | ✗ | std::vector<float> values(points.size()); | |
| 173 | ✗ | EvaluateStack(terrain, layers, points, values, spacing, cache); | |
| 174 | ✗ | for (std::size_t k = 0; k < points.size(); ++k) { | |
| 175 | ✗ | samples.Height((row + k / cols) * width + col + k % cols) = values[k]; | |
| 176 | } | ||
| 177 | ✗ | }); | |
| 178 | } | ||
| 179 | |||
| 180 | /// Sets `samples` to the terrain's source and first `layers` layers on `threads` threads; the | ||
| 181 | /// reads share `cache`, or one of their own when it is null. | ||
| 182 | 2362504 | void ReadStack(const Terrain& terrain, std::size_t layers, SampleBlock& samples, double spacing, | |
| 183 | unsigned threads, EvaluationCache* cache) { | ||
| 184 | 2362504 | std::optional<EvaluationCache> own; | |
| 185 | 2362504 | EvaluationCache* shared = cache != nullptr ? cache : &own.emplace(); | |
| 186 | 2362504 | if (samples.IsGrid()) { | |
| 187 | ✗ | EvaluateTiles(terrain, layers, samples, spacing, threads, shared); | |
| 188 | } else { | ||
| 189 | 2362504 | EvaluatePoints(terrain, layers, samples.Points(), samples.Heights(), spacing, threads, | |
| 190 | shared); | ||
| 191 | } | ||
| 192 | 2372025 | } | |
| 193 | |||
| 194 | /// Checks that `source` can be the source of a terrain on `surface`. | ||
| 195 | 719 | void ValidateSource(const Surface& surface, const Source* source) { | |
| 196 | 719 | if (source == nullptr) { | |
| 197 | 41 | return; | |
| 198 | } | ||
| 199 | 678 | const GeoKeys keys = source->Keys(); | |
| 200 | 678 | if (keys.IsGeographic() && !surface.IsBody()) { | |
| 201 | 3 | throw std::invalid_argument( | |
| 202 | "The source is in longitude and latitude (a geographic CRS); craters are sized in " | ||
| 203 | "meters, so reproject it first, for example with gdalwarp -t_srs, or set the " | ||
| 204 | 3 | "terrain's surface to a sphere or ellipsoid"); | |
| 205 | } | ||
| 206 | 675 | if (surface.IsBody() && !keys.Empty() && !keys.IsGeographic()) { | |
| 207 | 1 | throw std::invalid_argument( | |
| 208 | 1 | "On a body the source must be in longitude and latitude (a geographic CRS)"); | |
| 209 | } | ||
| 210 | 678 | } | |
| 211 | |||
| 212 | /// Checks that `layers` on `surface` over `source` can be evaluated. | ||
| 213 | 719 | void Validate(const Surface& surface, const std::shared_ptr<const Source>& source, | |
| 214 | const std::vector<LayerPtr>& layers) { | ||
| 215 | 719 | ValidateSource(surface, source.get()); | |
| 216 | 1794 | for (std::size_t i = 0; i < layers.size(); ++i) { | |
| 217 | 1088 | if (!layers[i]) { | |
| 218 | 2 | throw std::invalid_argument("A terrain's layers must not be null"); | |
| 219 | } | ||
| 220 | 1086 | const std::optional<TerrainBase> base = layers[i]->LayerBase(); | |
| 221 | 1086 | if (base && !(base->surface == surface)) { | |
| 222 | 8 | throw std::invalid_argument("Layer " + std::to_string(i) + | |
| 223 | 12 | " was built on another surface than the terrain's"); | |
| 224 | } | ||
| 225 | 1082 | if (base && base->source && base->source != source) { | |
| 226 | 8 | throw std::invalid_argument("Layer " + std::to_string(i) + | |
| 227 | 12 | " was built on another source than the terrain's"); | |
| 228 | } | ||
| 229 | 1078 | const auto before = layers.begin() + static_cast<std::ptrdiff_t>(i); | |
| 230 | 1142 | for (const Layer* dependency : layers[i]->Dependencies()) { | |
| 231 | 67 | if (std::none_of(layers.begin(), before, | |
| 232 | 99 | [dependency](const LayerPtr& l) { return l.get() == dependency; })) { | |
| 233 | 3 | throw std::invalid_argument( | |
| 234 | 6 | "Layer " + std::to_string(i) + | |
| 235 | 9 | " reads a layer that does not come before it in the terrain's layers"); | |
| 236 | } | ||
| 237 | 1078 | } | |
| 238 | 1075 | } | |
| 239 | 702 | } | |
| 240 | |||
| 241 | } // namespace | ||
| 242 | |||
| 243 | 16 | Terrain::Terrain() : Terrain(terraingen::Surface{}, nullptr, {}) {} | |
| 244 | |||
| 245 | 654 | Terrain::Terrain(terraingen::Surface surface, std::shared_ptr<const terraingen::Source> source, | |
| 246 | 654 | std::vector<LayerPtr> layers) | |
| 247 | 654 | : m_surface(std::move(surface)), m_source(std::move(source)), m_layers(std::move(layers)), | |
| 248 | 654 | m_cache(std::make_shared<EvaluationCache>(kPointQueryValues)) { | |
| 249 | 654 | Validate(m_surface, m_source, m_layers); | |
| 250 | 691 | } | |
| 251 | |||
| 252 | 3 | double Terrain::GroundReach(double spacing) const { | |
| 253 | 3 | double reach = 0.0; | |
| 254 | 8 | for (const LayerPtr& layer : m_layers) { | |
| 255 | 5 | reach += layer->GroundReach(spacing); | |
| 256 | } | ||
| 257 | 3 | return reach; | |
| 258 | } | ||
| 259 | |||
| 260 | 207 | std::optional<RasterGrid> Terrain::Grid() const { | |
| 261 | 207 | return m_source ? BoundedGrid(*m_source) : std::nullopt; | |
| 262 | } | ||
| 263 | |||
| 264 | 157 | RasterGrid Terrain::SourceGrid() const { | |
| 265 | 157 | const std::optional<RasterGrid> grid = Grid(); | |
| 266 | 157 | if (!grid) { | |
| 267 | 9 | throw std::invalid_argument( | |
| 268 | "A terrain without a source, or with a source without bounds such as synthetic " | ||
| 269 | 9 | "noise, needs a grid"); | |
| 270 | } | ||
| 271 | 148 | return *grid; | |
| 272 | } | ||
| 273 | |||
| 274 | 118 | GeoKeys Terrain::Keys() const { | |
| 275 | 118 | GeoKeys keys = m_source ? m_source->Keys() : GeoKeys{}; | |
| 276 | 118 | if (m_surface.IsBody() && keys.Empty()) { | |
| 277 | 12 | keys = GeographicSphereKeys(m_surface.Radius()); | |
| 278 | } | ||
| 279 | 118 | return keys; | |
| 280 | } | ||
| 281 | |||
| 282 | 2506575 | LayerContext Terrain::Context(double spacing, unsigned threads) const { | |
| 283 | 2506575 | LayerContext context = Ground::Context(spacing, threads); | |
| 284 | 2510879 | context.surface = &m_surface; | |
| 285 | 2510879 | context.source = m_source.get(); | |
| 286 | 2510879 | return context; | |
| 287 | } | ||
| 288 | |||
| 289 | 391 | Raster<float> Terrain::Evaluate(const RasterGrid& grid, const EvaluationOptions& options) const { | |
| 290 | 391 | Raster<float> heights(grid); | |
| 291 | 391 | Fill(grid, heights.Values(), options); | |
| 292 | 388 | return heights; | |
| 293 | 3 | } | |
| 294 | |||
| 295 | 126 | Raster<float> Terrain::Evaluate(const EvaluationOptions& options) const { | |
| 296 | 126 | return Evaluate(SourceGrid(), options); | |
| 297 | } | ||
| 298 | |||
| 299 | 413 | void Terrain::Fill(const RasterGrid& grid, Span<float> heights, | |
| 300 | const EvaluationOptions& options) const { | ||
| 301 | 413 | if (heights.size() != grid.width * grid.height) { | |
| 302 | 4 | throw std::invalid_argument("The grid needs " + std::to_string(grid.width * grid.height) + | |
| 303 | 8 | " heights, not " + std::to_string(heights.size())); | |
| 304 | } | ||
| 305 | 411 | Raster<float> view(grid, heights); | |
| 306 | 411 | const double spacing = EvaluatedSpacing(grid, options, m_surface); | |
| 307 | 409 | if (m_source) { | |
| 308 | 425 | m_source->Fill(view, spacing / m_surface.UnitLength(), options.threads); | |
| 309 | } else { | ||
| 310 | 14 | std::fill(heights.begin(), heights.end(), 0.0f); | |
| 311 | } | ||
| 312 | 409 | ApplyFrom(0, view, options); | |
| 313 | 408 | } | |
| 314 | |||
| 315 | 432 | void Terrain::ApplyFrom(std::size_t first, Raster<float>& heights, | |
| 316 | const EvaluationOptions& options) const { | ||
| 317 | 432 | if (first > m_layers.size()) { | |
| 318 | 2 | throw std::invalid_argument("The terrain has " + std::to_string(m_layers.size()) + | |
| 319 | 4 | " layers, so it has none from layer " + std::to_string(first)); | |
| 320 | } | ||
| 321 | 431 | const double spacing = EvaluatedSpacing(heights.Grid(), options, m_surface); | |
| 322 | 547 | const Backend backend = ResolveBackend(options.backend.value_or(DefaultBackend())); | |
| 323 | 1086 | for (std::size_t n = first; n < m_layers.size(); ++n) { | |
| 324 | 656 | const Stopwatch stopwatch; | |
| 325 | 656 | EvaluationCache cache; | |
| 326 | 656 | const StackGround ground(*this, n, &cache); | |
| 327 | 656 | LayerContext context = ground.Context(spacing, options.threads); | |
| 328 | 656 | context.backend = backend; | |
| 329 | 656 | m_layers[n]->Apply(heights, context); | |
| 330 | 655 | if (options.onLayer) { | |
| 331 | 226 | options.onLayer(n, stopwatch.Milliseconds()); | |
| 332 | } | ||
| 333 | 656 | } | |
| 334 | 430 | } | |
| 335 | |||
| 336 | 2323739 | void Terrain::FillSamples(SampleBlock& samples, double spacing, unsigned threads) const { | |
| 337 | 2323739 | RequirePositive(spacing, "Sample spacing"); | |
| 338 | 2319438 | ReadStack(*this, m_layers.size(), samples, spacing, threads, m_cache.get()); | |
| 339 | 2327259 | } | |
| 340 | |||
| 341 | 116 | CraterFeatures Terrain::Craters(const RasterGrid& grid, double spacing) const { | |
| 342 | 116 | const double resolved = ListedSpacing(grid, spacing, m_surface); | |
| 343 | 116 | CraterFeatures placed; | |
| 344 | 116 | ForEachLayer<CraterPlacer>(*this, [&](const CraterPlacer& placer, std::size_t) { | |
| 345 | 123 | placed.Append(placer.CratersOver(grid, resolved)); | |
| 346 | 123 | }); | |
| 347 | 116 | return placed; | |
| 348 | } | ||
| 349 | |||
| 350 | 14 | CraterFeatures Terrain::Craters(double spacing) const { | |
| 351 | 14 | return Craters(SourceGrid(), spacing); | |
| 352 | } | ||
| 353 | |||
| 354 | 58 | BoulderFeatures Terrain::Boulders(const RasterGrid& grid, double spacing) const { | |
| 355 | 58 | const double resolved = ListedSpacing(grid, spacing, m_surface); | |
| 356 | 58 | BoulderFeatures placed; | |
| 357 | 58 | ForEachLayer<BoulderPlacer>(*this, [&](const BoulderPlacer& placer, std::size_t n) { | |
| 358 | 57 | const StackGround ground(*this, n, m_cache.get()); | |
| 359 | 114 | placed.Append(placer.BouldersOver(grid, resolved, &ground)); | |
| 360 | 57 | }); | |
| 361 | 58 | return placed; | |
| 362 | } | ||
| 363 | |||
| 364 | 17 | BoulderFeatures Terrain::Boulders(double spacing) const { | |
| 365 | 17 | return Boulders(SourceGrid(), spacing); | |
| 366 | } | ||
| 367 | |||
| 368 | 20169 | Terrain Terrain::Beneath(std::size_t layers) const { | |
| 369 | 20169 | if (layers > m_layers.size()) { | |
| 370 | 4 | throw std::invalid_argument("The terrain has " + std::to_string(m_layers.size()) + | |
| 371 | 4 | " layers, so it has no ground beneath layer " + | |
| 372 | 6 | std::to_string(layers)); | |
| 373 | } | ||
| 374 | 20167 | Terrain prefix = *this; | |
| 375 | 20167 | prefix.m_layers.resize(layers); | |
| 376 | 20167 | return prefix; | |
| 377 | } | ||
| 378 | |||
| 379 | 65 | void Terrain::Replace(std::shared_ptr<const terraingen::Source> source, | |
| 380 | const LayerReplacer& replace) { | ||
| 381 | 65 | std::vector<LayerPtr> layers = m_layers; | |
| 382 | 127 | const TerrainBase base{m_surface, source}; | |
| 383 | 65 | LayerReplacements replaced; | |
| 384 | 196 | for (std::size_t n = 0; n < layers.size(); ++n) { | |
| 385 | 131 | LayerPtr layer = replace(n, layers[n]); | |
| 386 | 188 | layer = layer ? std::move(layer) : layers[n]; | |
| 387 | 131 | const std::optional<TerrainBase> built = layer->LayerBase(); | |
| 388 | 131 | if (source != m_source && built && built->source == m_source) { | |
| 389 | 11 | if (LayerPtr rebased = layer->Rebased(base)) { | |
| 390 | 11 | layer = std::move(rebased); | |
| 391 | 11 | } | |
| 392 | } | ||
| 393 | 131 | if (LayerPtr rebound = replaced.empty() ? nullptr : layer->Rebound(replaced)) { | |
| 394 | 8 | layer = std::move(rebound); | |
| 395 | 131 | } | |
| 396 | 131 | if (layer != layers[n]) { | |
| 397 | 82 | replaced.emplace_back(layers[n].get(), layer); | |
| 398 | 82 | layers[n] = std::move(layer); | |
| 399 | } | ||
| 400 | 180 | } | |
| 401 | 65 | Validate(m_surface, source, layers); | |
| 402 | 61 | m_source = std::move(source); | |
| 403 | 61 | m_layers = std::move(layers); | |
| 404 | 61 | m_cache = std::make_shared<EvaluationCache>(kPointQueryValues); | |
| 405 | 73 | } | |
| 406 | |||
| 407 | 36 | void Terrain::SetLayer(std::size_t index, LayerPtr layer) { | |
| 408 | 36 | if (index >= m_layers.size()) { | |
| 409 | 4 | throw std::invalid_argument("The terrain has " + std::to_string(m_layers.size()) + | |
| 410 | 8 | " layers, so it has no layer " + std::to_string(index)); | |
| 411 | } | ||
| 412 | 34 | if (!layer) { | |
| 413 | 1 | throw std::invalid_argument("A terrain's layers must not be null"); | |
| 414 | } | ||
| 415 | 174 | Replace(m_source, [&](std::size_t n, const LayerPtr&) { return n == index ? layer : nullptr; }); | |
| 416 | 30 | } | |
| 417 | |||
| 418 | 8 | void Terrain::SetSource(std::shared_ptr<const terraingen::Source> source) { | |
| 419 | 16 | Replace(std::move(source), [](std::size_t, const LayerPtr&) { return nullptr; }); | |
| 420 | 7 | } | |
| 421 | |||
| 422 | 24 | Terrain Terrain::Reseed(std::uint64_t seed) const { | |
| 423 | 24 | Terrain reseeded = *this; | |
| 424 | 24 | std::shared_ptr<const terraingen::Source> source = | |
| 425 | 24 | m_source ? m_source->Reseeded(PartSeed(seed, 0)) : nullptr; | |
| 426 | 62 | reseeded.Replace(source ? std::move(source) : m_source, | |
| 427 | 24 | [seed](std::size_t n, const LayerPtr& layer) { | |
| 428 | 43 | return layer->Reseeded(PartSeed(seed, n + 1)); | |
| 429 | }); | ||
| 430 | 24 | return reseeded; | |
| 431 | 24 | } | |
| 432 | |||
| 433 | } // namespace terraingen | ||
| 434 |