./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
24 / 24
100.0%
Branches:
18 / 22
81.8%
Functions:
6 / 6, 1 excl
100.0%

src/layers/source_resolved.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/source_resolved.h"
4
5 #include "util/checks.h"
6
7 #include <limits>
8 #include <stdexcept>
9 #include <utility>
10
11 namespace terraingen {
12
13 namespace {
14
15 /// `pixelsAcross` cells of a source's data at each point, in meters.
16
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 class SourceResolvedField final : public SurfaceField {
17 public:
18 69 SourceResolvedField(std::shared_ptr<const Source> source, double metersPerCell)
19 69 : m_source(std::move(source)), m_metersPerCell(metersPerCell) {}
20
21 404476 double ValueAt(double x, double y) const override {
22 404476 return m_metersPerCell * m_source->ResolutionAt(x, y);
23 }
24
25 1 double Max() const override {
26 1 return std::numeric_limits<double>::infinity();
27 }
28
29 private:
30 std::shared_ptr<const Source> m_source;
31 double m_metersPerCell;
32 };
33
34 } // namespace
35
36 75 std::shared_ptr<const SurfaceField> SourceResolvedLimit(std::shared_ptr<const Source> source,
37 double pixelsAcross,
38 const Surface& surface) {
39
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 74 times.
75 if (!source) {
40
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A source resolved limit needs a source");
41 }
42 74 RequirePositive(pixelsAcross, "Pixels across");
43
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 67 times.
138 return std::make_shared<const SourceResolvedField>(std::move(source),
44 69 pixelsAcross * surface.UnitLength());
45 }
46
47 1365 std::shared_ptr<const SurfaceField> LayerResolvedLimit(const std::shared_ptr<const Source>& source,
48 const std::optional<double>& pixelsAcross,
49 const Surface& surface) {
50
4/4
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 1214 times.
✓ Branch 2 taken 92 times.
✓ Branch 3 taken 59 times.
1365 if (!source || !pixelsAcross) {
51 1306 return nullptr;
52 }
53
2/3
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 59 times.
177 return SourceResolvedLimit(source, *pixelsAcross, surface);
54 }
55
56 1560000 double SourceResolvedSize(const Source* source, const std::optional<double>& pixelsAcross,
57 const Surface& surface, double x, double y) {
58
3/4
✓ Branch 0 taken 86481 times.
✓ Branch 1 taken 1473519 times.
✓ Branch 2 taken 86735 times.
✗ Branch 3 not taken.
1560000 if (source == nullptr || !pixelsAcross) {
59 return std::numeric_limits<double>::infinity();
60 }
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86735 times.
86735 return *pixelsAcross * surface.UnitLength() * source->ResolutionAt(x, y);
62 }
63
64 1372 void ValidateSourceResolvesPixels(const std::optional<double>& pixelsAcross) {
65
2/2
✓ Branch 0 taken 925 times.
✓ Branch 1 taken 447 times.
1372 if (pixelsAcross) {
66 925 RequirePositive(*pixelsAcross, "Source resolves pixels");
67 }
68 1367 }
69
70 } // namespace terraingen
71