./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
35 / 36
97.2%
Branches:
11 / 13
84.6%
Functions:
7 / 7
100.0%

src/layers/layer.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/layer.h"
4
5 #include "layers/prefetched_ground.h"
6 #include "terraingen/raster/cell_loops.h"
7 #include "terraingen/raster/grid.h"
8 #include "util/checks.h"
9
10 #include <algorithm>
11 #include <stdexcept>
12 #include <string>
13
14 namespace terraingen {
15
16 152788 void RequireGround(const Ground* ground, std::string_view what) {
17
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 152771 times.
152788 if (ground == nullptr) {
18
3/3
✓ Branch 2 taken 17 times.
✓ Branch 5 taken 17 times.
✓ Branch 8 taken 17 times.
68 throw std::invalid_argument(std::string(what) + " need the ground");
19 }
20 152771 }
21
22 866 void Layer::Apply(Raster<float>& heights, const LayerContext& context) const {
23 866 SampleBlock samples(heights);
24 866 ApplySamples(samples, context);
25 864 }
26
27 4547662 void Layer::Apply(SampleBlock& samples, const LayerContext& context) const {
28
1/2
✓ Branch 0 taken 4549290 times.
✗ Branch 1 not taken.
4547662 if (!samples.IsGrid()) {
29 4549290 RequirePositive(context.spacing, "Sample spacing");
30 }
31 4546505 ApplySamples(samples, context);
32 4549936 }
33
34 1948520 float Layer::ApplyAt(float height, double x, double y, const LayerContext& context) const {
35 1948520 const Vec2d position{x, y};
36 1948520 SampleBlock samples({&position, 1}, {&height, 1});
37 1948520 Apply(samples, context);
38 1948505 return height;
39 }
40
41 2035913 void FormulaLayer::ApplySamples(SampleBlock& samples, const LayerContext& context) const {
42 2035913 CheckContext(context);
43 2035871 LayerContext resolved = context;
44 2035871 resolved.spacing = samples.ResolvedSpacing(context.spacing, context.SurfaceOrPlane());
45
2/2
✓ Branch 0 taken 1867327 times.
✓ Branch 1 taken 168598 times.
2035925 if (context.ground == nullptr) {
46 1867327 MapSamples(samples, context.threads, [&](float height, const Vec2d& p) {
47 6117304 return HeightAt(height, p.x, p.y, resolved);
48 });
49 1867327 return;
50 }
51 168598 MapReadingGround<float>(
52 168598 samples.Size(), samples.IsGrid() ? context.threads : 1, *context.ground,
53 946283 [&](std::size_t i, const Ground& ground) {
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 608953 times.
609004 if (samples.IsNoData(i)) {
55 ✗ return samples.Height(i);
56 }
57 608953 LayerContext at = resolved;
58 608953 at.ground = &ground;
59 608953 const Vec2d p = samples.Position(i);
60 609020 return HeightAt(samples.Height(i), p.x, p.y, at);
61 },
62
2/2
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 168555 times.
920907 [&](std::size_t i, float height) { samples.Height(i) = height; });
63 }
64
65 } // namespace terraingen
66