src/pipeline/terrain_view.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/pipeline/terrain_view.h" | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <stdexcept> | ||
| 7 | #include <string> | ||
| 8 | #include <utility> | ||
| 9 | |||
| 10 | namespace terraingen { | ||
| 11 | |||
| 12 | 11 | TerrainView::TerrainView(RasterGrid grid, EvaluationOptions options, | |
| 13 | 11 | std::optional<std::size_t> stage) | |
| 14 | 11 | : m_options(std::move(options)), m_stage(stage), m_heights(grid) {} | |
| 15 | |||
| 16 | 22 | std::optional<std::size_t> TerrainView::SameLayers(const Terrain& terrain, Backend backend) const { | |
| 17 | 22 | if (!m_evaluated || backend != m_backend || !(terrain.Surface() == m_surface) || | |
| 18 | 11 | terrain.Source() != m_source) { | |
| 19 | 12 | return std::nullopt; | |
| 20 | } | ||
| 21 | 10 | const std::vector<LayerPtr>& layers = terrain.Layers(); | |
| 22 | 10 | const auto same = | |
| 23 | 10 | std::mismatch(layers.begin(), layers.end(), m_layers.begin(), m_layers.end()).first; | |
| 24 | 10 | return static_cast<std::size_t>(same - layers.begin()); | |
| 25 | } | ||
| 26 | |||
| 27 | 24 | const Raster<float>& TerrainView::Update(const Terrain& terrain) { | |
| 28 | 24 | const std::size_t count = terrain.Layers().size(); | |
| 29 | 24 | const std::size_t stage = m_stage.value_or(count > 0 ? count - 1 : 0); | |
| 30 | 24 | if (stage > count) { | |
| 31 | 4 | throw std::invalid_argument("The view keeps the heights before layer " + | |
| 32 | 8 | std::to_string(stage) + ", but the terrain has " + | |
| 33 | 8 | std::to_string(count) + " layers"); | |
| 34 | } | ||
| 35 | 22 | const Backend backend = m_options.backend.value_or(DefaultBackend()); | |
| 36 | 22 | const std::optional<std::size_t> same = SameLayers(terrain, backend); | |
| 37 | 22 | if (same && *same == count && count == m_layers.size()) { | |
| 38 | 2 | return m_heights; | |
| 39 | } | ||
| 40 | 20 | m_evaluated = false; | |
| 41 | 20 | if (same && *same >= m_keptStage) { | |
| 42 | 6 | std::copy(m_kept.begin(), m_kept.end(), m_heights.Values().begin()); | |
| 43 | 6 | terrain.ApplyFrom(m_keptStage, m_heights, m_options); | |
| 44 | } else { | ||
| 45 | 14 | EvaluateKeeping(terrain, stage); | |
| 46 | } | ||
| 47 | 20 | m_evaluated = true; | |
| 48 | 20 | m_backend = backend; | |
| 49 | 20 | m_surface = terrain.Surface(); | |
| 50 | 20 | m_source = terrain.Source(); | |
| 51 | 20 | m_layers = terrain.Layers(); | |
| 52 | 20 | return m_heights; | |
| 53 | } | ||
| 54 | |||
| 55 | 14 | void TerrainView::EvaluateKeeping(const Terrain& terrain, std::size_t stage) { | |
| 56 | 14 | terrain.Beneath(stage).Fill(Grid(), m_heights.Values(), m_options); | |
| 57 | 14 | const Span<float> values = m_heights.Values(); | |
| 58 | 14 | m_kept.assign(values.begin(), values.end()); | |
| 59 | 14 | m_keptStage = stage; | |
| 60 | 14 | terrain.ApplyFrom(stage, m_heights, m_options); | |
| 61 | 14 | } | |
| 62 | |||
| 63 | 2 | void TerrainView::Clear() { | |
| 64 | 2 | m_evaluated = false; | |
| 65 | 2 | m_kept = {}; | |
| 66 | 2 | m_source.reset(); | |
| 67 | 2 | m_layers = {}; | |
| 68 | 2 | } | |
| 69 | |||
| 70 | } // namespace terraingen | ||
| 71 |