./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
46 / 46
100.0%
Branches:
33 / 37
89.2%
Functions:
5 / 5
100.0%

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
1/1
✓ Branch 2 taken 11 times.
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
6/8
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 10 times.
22 if (!m_evaluated || backend != m_backend || !(terrain.Surface() == m_surface) ||
18
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 10 times.
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
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 1 time.
24 const std::size_t count = terrain.Layers().size();
29
4/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 7 times.
24 const std::size_t stage = m_stage.value_or(count > 0 ? count - 1 : 0);
30
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 22 times.
24 if (stage > count) {
31
1/1
✓ Branch 1 taken 2 times.
4 throw std::invalid_argument("The view keeps the heights before layer " +
32
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
8 std::to_string(stage) + ", but the terrain has " +
33
2/2
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
8 std::to_string(count) + " layers");
34 }
35
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 const Backend backend = m_options.backend.value_or(DefaultBackend());
36 22 const std::optional<std::size_t> same = SameLayers(terrain, backend);
37
5/6
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
22 if (same && *same == count && count == m_layers.size()) {
38 2 return m_heights;
39 }
40 20 m_evaluated = false;
41
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6 times.
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
1/1
✓ Branch 2 taken 14 times.
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