include/terraingen/pipeline/terrain_view.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `TerrainView`, a terrain's heights over one grid, updated as the terrain is edited. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/layers/layer.h" | ||
| 8 | #include "terraingen/pipeline/terrain.h" | ||
| 9 | #include "terraingen/raster/grid.h" | ||
| 10 | #include "terraingen/raster/raster.h" | ||
| 11 | #include "terraingen/sources/source.h" | ||
| 12 | |||
| 13 | #include <cstddef> | ||
| 14 | #include <memory> | ||
| 15 | #include <optional> | ||
| 16 | #include <vector> | ||
| 17 | |||
| 18 | namespace terraingen { | ||
| 19 | |||
| 20 | /// The heights of a terrain over one grid, for loops that edit the terrain and evaluate it again. | ||
| 21 | /// | ||
| 22 | /// The view keeps the heights before one layer, the stage; an update after replacing that layer | ||
| 23 | /// or a later one applies only the layers from the stage on, and an update of an unchanged | ||
| 24 | /// terrain evaluates nothing. Parts are told apart by identity, and the view keeps those it last | ||
| 25 | /// evaluated alive. Updates give the bits of `Terrain::Evaluate`; one may run at a time. | ||
| 26 | class TerrainView { | ||
| 27 | public: | ||
| 28 | /// A view of `grid` evaluated with `options`, keeping the heights before layer `stage`: | ||
| 29 | /// unset for the terrain's last layer, 0 for the source's heights. | ||
| 30 | TerrainView(RasterGrid grid, EvaluationOptions options = {}, | ||
| 31 | std::optional<std::size_t> stage = std::nullopt); | ||
| 32 | |||
| 33 | /// The grid the view covers. | ||
| 34 | 16 | const RasterGrid& Grid() const { | |
| 35 | 16 | return m_heights.Grid(); | |
| 36 | } | ||
| 37 | |||
| 38 | /// The terrain's heights over the grid, restarting from the kept heights when the surface, | ||
| 39 | /// the source and the layers before the stage are those of the last update; valid until the | ||
| 40 | /// next update or `Clear`. | ||
| 41 | /// @throws std::invalid_argument for a stage past the terrain's layer count, or as | ||
| 42 | /// `Terrain::Evaluate`. | ||
| 43 | /// @throws std::runtime_error as `Terrain::Evaluate`. | ||
| 44 | const Raster<float>& Update(const Terrain& terrain); | ||
| 45 | |||
| 46 | /// Drops the kept heights and parts, so the next update evaluates the whole terrain. | ||
| 47 | void Clear(); | ||
| 48 | |||
| 49 | private: | ||
| 50 | /// How many layers at the front of `terrain` are those of the last update; unset when there | ||
| 51 | /// was none or the surface, source or backend changed. | ||
| 52 | std::optional<std::size_t> SameLayers(const Terrain& terrain, Backend backend) const; | ||
| 53 | |||
| 54 | /// Evaluates `terrain` from its source, keeping the heights before layer `stage`. | ||
| 55 | void EvaluateKeeping(const Terrain& terrain, std::size_t stage); | ||
| 56 | |||
| 57 | EvaluationOptions m_options; | ||
| 58 | std::optional<std::size_t> m_stage; | ||
| 59 | Raster<float> m_heights; | ||
| 60 | std::vector<float> m_kept; | ||
| 61 | std::size_t m_keptStage = 0; | ||
| 62 | bool m_evaluated = false; | ||
| 63 | Backend m_backend = Backend::Cpu; | ||
| 64 | Surface m_surface{}; | ||
| 65 | std::shared_ptr<const Source> m_source; | ||
| 66 | std::vector<LayerPtr> m_layers; | ||
| 67 | }; | ||
| 68 | |||
| 69 | } // namespace terraingen | ||
| 70 |