./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
56 / 59
94.9%
Branches:
48 / 64
75.0%
Functions:
11 / 12, 7 excl
91.7%

include/terraingen/layers/layer.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief `Layer`, relief added on top of a source, and `FormulaLayer` for point-wise layers.
5 #pragma once
6
7 #include "terraingen/geometry/geometry.h"
8 #include "terraingen/geometry/surface.h"
9 #include "terraingen/gpu/backend.h"
10 #include "terraingen/layers/ground.h"
11 #include "terraingen/layers/terrain_base.h"
12 #include "terraingen/raster/raster.h"
13 #include "terraingen/sources/sample_block.h"
14 #include "terraingen/sources/source.h"
15
16 #include <cstdint>
17 #include <memory>
18 #include <optional>
19 #include <string_view>
20 #include <utility>
21 #include <vector>
22
23 namespace terraingen {
24
25 class EvaluationCache;
26
27 /// How a layer or an albedo is evaluated: where, at what spacing, and on what terrain, as
28 /// `Ground::Context` gives it.
29 14 struct LayerContext {
30 /// Sample spacing in world units; features unresolvable at it are left out. At least the cell
31 /// size in `Layer::Apply`; positive in `Layer::ApplyAt`.
32 double spacing = 0.0;
33 unsigned threads = 1; ///< Worker threads, 0 for all; the result does not depend on it.
34 const Ground* ground = nullptr; ///< The terrain beneath; null where none is needed.
35 /// Where grids are evaluated, already resolved to `Cpu` or `Vulkan`. Layers whose relief is a
36 /// kernel follow it; other layers, and every point query, run on the CPU.
37 Backend backend = Backend::Cpu;
38 /// Values the batches of one evaluation of points share, such as crater datums; null for none.
39 EvaluationCache* cache = nullptr;
40 const Surface* surface = nullptr; ///< What the terrain lies on; null for a plane.
41 const Source* source = nullptr; ///< The terrain's starting heights; null for none.
42
43 /// The surface the terrain lies on: `*surface`, or a plane without one.
44 13388206 const Surface& SurfaceOrPlane() const {
45
1/1
✓ Branch 2 taken 96022 times.
15424077 return terraingen::SurfaceOrPlane(surface);
46 }
47
48 /// Where noise on a plane is measured from: the source's `ReferencePoint`, or the origin
49 /// without a source.
50 6134692 Vec2d ReferencePoint() const {
51 6134692 return terraingen::ReferencePoint(source);
52 }
53 };
54
55 /// Checks that a layer that reads the ground beneath it was given one.
56 /// @param what What reads it, which starts the error message ("<what> need the ground").
57 /// @throws std::invalid_argument if `ground` is null.
58 void RequireGround(const Ground* ground, std::string_view what);
59
60 class Layer;
61
62 /// A shared, immutable layer, as a `Terrain` holds them.
63 using LayerPtr = std::shared_ptr<const Layer>;
64
65 /// The layers of a terrain that an update replaced, each with its replacement, in the terrain's
66 /// order; layers that read other layers rebind to them (`Layer::Rebound`).
67 using LayerReplacements = std::vector<std::pair<const Layer*, LayerPtr>>;
68
69 /// Relief added on top of a source: noise, craters, or a layer of your own.
70 /// Implementations of `ApplySamples` must be deterministic and thread-safe, and give a point the
71 /// height of a grid cell centered there.
72
2/2
<unknown function 44>:
✓ Branch 1 taken 84 times.
<unknown function 72>:
✓ Branch 1 taken 55 times.
2318 class Layer {
73 public:
74
4/5
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
✓ Branch 9 taken 6 times.
✓ Branch 12 taken 1 time.
278 virtual ~Layer() = default;
75
76 /// Adds the layer's relief to the cells of `heights` that hold data.
77 void Apply(Raster<float>& heights, const LayerContext& context) const;
78
79 /// Adds the layer's relief to every sample of `samples` that holds data.
80 /// @throws std::invalid_argument for points without a positive `context.spacing`.
81 void Apply(SampleBlock& samples, const LayerContext& context) const;
82
83 /// The height at one point after this layer, from `height` before it.
84 /// @throws std::invalid_argument without a positive `context.spacing`.
85 float ApplyAt(float height, double x, double y, const LayerContext& context) const;
86
87 /// The base a layer that derives state from it when built (craters, boulders) was built on,
88 /// whose surface and source must be its terrain's; unset for a layer that reads them from
89 /// its context.
90 549 virtual std::optional<TerrainBase> LayerBase() const {
91 548 return std::nullopt;
92 }
93
94 /// The other layers this one reads, which must come before it in its terrain's layers.
95 932 virtual std::vector<const Layer*> Dependencies() const {
96 932 return {};
97 }
98
99 /// The farthest distance (m) from a sample at which evaluating it at `spacing` reads the
100 /// ground beneath the layer; 0 (the default) for a layer that reads it only at its samples or
101 /// not at all. A tile evaluated alone needs the ground this far around it.
102 ✗ virtual double GroundReach(double /*spacing*/) const {
103 ✗ return 0.0;
104 }
105
106 /// A copy of the layer whose seeds draw from `seed` (`ReseedWord`); null for a layer without
107 /// seeds (the default), which `Terrain::Reseed` keeps.
108 7 virtual LayerPtr Reseeded(std::uint64_t /*seed*/) const {
109
1/1
✓ Branch 1 taken 1 time.
6 return nullptr;
110 }
111
112 /// A copy of the layer reading the layers `replaced` gives in place of those it reads
113 /// (`Replacement`); null when it reads none of them, as for a layer without
114 /// `Dependencies` (the default), which `Terrain::SetLayer` and `Terrain::Reseed` keep.
115 22 virtual LayerPtr Rebound(const LayerReplacements& /*replaced*/) const {
116
2/2
<unknown function 115>:
✓ Branch 1 taken 1 time.
terraingen::Layer::Rebound(std::vector<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> >, std::allocator<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> > > > const&) const:
✓ Branch 1 taken 1 time.
22 return nullptr;
117 }
118
119 /// A copy of a layer built on a base (`LayerBase`) rebuilt from its models and options on
120 /// `base`; null for a layer without one (the default), which `Terrain::SetSource` and
121 /// `Terrain::Reseed` keep.
122 2 virtual LayerPtr Rebased(const TerrainBase& /*base*/) const {
123
1/1
✓ Branch 1 taken 1 time.
2 return nullptr;
124 }
125
126 protected:
127 /// Adds the layer's relief to `samples`. A grid's spacing is `context.spacing` raised to the
128 /// grid's own; points come with a positive one.
129 virtual void ApplySamples(SampleBlock& samples, const LayerContext& context) const = 0;
130 };
131
132 /// The layer `replaced` gives in place of `layer`, or `layer` itself where it gives none or one
133 /// of another type.
134 template <typename T>
135 27 std::shared_ptr<const T> Replacement(const std::shared_ptr<const T>& layer,
136 const LayerReplacements& replaced) {
137
6/6
std::shared_ptr<(anonymous namespace)::Plus const> terraingen::Replacement<(anonymous namespace)::Plus>(std::shared_ptr<(anonymous namespace)::Plus const> const&, std::vector<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> >, std::allocator<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> > > > const&):
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
std::shared_ptr<terraingen::BoulderLayer const> terraingen::Replacement<terraingen::BoulderLayer>(std::shared_ptr<terraingen::BoulderLayer const> const&, std::vector<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> >, std::allocator<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> > > > const&):
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
std::shared_ptr<terraingen::CraterLayer const> terraingen::Replacement<terraingen::CraterLayer>(std::shared_ptr<terraingen::CraterLayer const> const&, std::vector<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> >, std::allocator<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> > > > const&):
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 8 times.
35 for (const auto& entry : replaced) {
138
6/6
std::shared_ptr<(anonymous namespace)::Plus const> terraingen::Replacement<(anonymous namespace)::Plus>(std::shared_ptr<(anonymous namespace)::Plus const> const&, std::vector<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> >, std::allocator<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> > > > const&):
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
std::shared_ptr<terraingen::BoulderLayer const> terraingen::Replacement<terraingen::BoulderLayer>(std::shared_ptr<terraingen::BoulderLayer const> const&, std::vector<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> >, std::allocator<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> > > > const&):
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
std::shared_ptr<terraingen::CraterLayer const> terraingen::Replacement<terraingen::CraterLayer>(std::shared_ptr<terraingen::CraterLayer const> const&, std::vector<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> >, std::allocator<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> > > > const&):
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5 times.
23 if (entry.first == layer.get()) {
139
4/6
std::shared_ptr<(anonymous namespace)::Plus const> terraingen::Replacement<(anonymous namespace)::Plus>(std::shared_ptr<(anonymous namespace)::Plus const> const&, std::vector<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> >, std::allocator<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> > > > const&):
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
std::shared_ptr<terraingen::BoulderLayer const> terraingen::Replacement<terraingen::BoulderLayer>(std::shared_ptr<terraingen::BoulderLayer const> const&, std::vector<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> >, std::allocator<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> > > > const&):
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
std::shared_ptr<terraingen::CraterLayer const> terraingen::Replacement<terraingen::CraterLayer>(std::shared_ptr<terraingen::CraterLayer const> const&, std::vector<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> >, std::allocator<std::pair<terraingen::Layer const*, std::shared_ptr<terraingen::Layer const> > > > const&):
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
17 if (auto same = std::dynamic_pointer_cast<const T>(entry.second)) {
140 15 return same;
141 }
142 }
143 }
144 27 return layer;
145 }
146
147 /// A layer whose height at a point depends only on the height there and the point (noise,
148 /// roughness, transforms); the base applies `HeightAt` to every sample in parallel.
149
21/34
<unknown function 74>:
✗ Branch 0 not taken.
✓ Branch 0 taken 1 time.
✗ Branch 0 not taken.
✗ Branch 0 not taken.
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 2 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
✓ Branch 3 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✓ Branch 5 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 time.
terraingen::Layer::Rebased(terraingen::TerrainBase const&) const:
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 34 times.
✓ Branch 5 taken 1 time.
527 class FormulaLayer : public Layer {
150 protected:
151 /// The height at (x, y) after the layer, from `height` before it.
152 /// @param context The layer's context with `spacing` resolved to the sample spacing in meters
153 /// and `ground` the one to read at this point, null when the context gives none.
154 virtual float HeightAt(float height, double x, double y, const LayerContext& context) const = 0;
155
156 /// Checks the context before any sample is evaluated; accepts any by default.
157 1985921 virtual void CheckContext(const LayerContext& /*context*/) const {}
158
159 void ApplySamples(SampleBlock& samples, const LayerContext& context) const final;
160 };
161
162 } // namespace terraingen
163