./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
23 / 23
100.0%
Branches:
50 / 115
43.5%
Functions:
1 / 1, 3 excl
100.0%

include/terraingen/layers/exposure.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief `ExposureField`, how far the ground is bare bedrock rather than soil, and `PatchNoise`.
5 #pragma once
6
7 #include "terraingen/geometry/surface.h"
8 #include "terraingen/layers/ground.h"
9 #include "terraingen/raster/raster.h"
10
11 #include <array>
12 #include <cstdint>
13 #include <optional>
14
15 namespace terraingen {
16
17 /// Two-octave Perlin patch noise in [-1, 1], about half the ground near either end.
18 /// @param size Typical width of a patch, in meters; positive.
19 /// @param seed Seed of the coarser octave; the finer uses `seed` + 1.
20 double PatchNoise(const Surface& surface, double x, double y, double size, std::uint32_t seed);
21
22 /// The thresholds an `ExposureField` tells soil from bedrock by; a rule of thumb, not a
23 /// measurement.
24 10 struct ExposureParams {
25 double reach = 12.0; ///< How far around a point its surroundings are read (m).
26 /// Standing this share of the reach above the surroundings or less: soil.
27 double riseSoil = 0.010;
28 double riseBoulder = 0.035; ///< Standing this share of the reach above them or more: bedrock.
29 double slopeSoil = 0.20; ///< Slope (rise over run) at or under which soil holds.
30 double slopeBoulder = 0.40; ///< Slope at or over which none does.
31 /// Share of level ground that is bedrock too, in patches; 0 for none.
32 double floorCover = 0.0;
33 double floorPatch = 60.0; ///< How far across those patches are (m).
34 std::uint32_t seed = 0; ///< Varies the floor patches.
35
36 1 bool operator==(const ExposureParams& other) const {
37
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return reach == other.reach && riseSoil == other.riseSoil &&
38
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 riseBoulder == other.riseBoulder && slopeSoil == other.slopeSoil &&
39
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 slopeBoulder == other.slopeBoulder && floorCover == other.floorCover &&
40
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
2 floorPatch == other.floorPatch && seed == other.seed;
41 }
42 };
43
44 /// Checks exposure thresholds.
45 /// @throws std::invalid_argument unless `reach` and `floorPatch` are positive, each boulder
46 /// threshold is over its soil one, and `floorCover` is in [0, 1).
47 void ValidateExposureParams(const ExposureParams& params);
48
49 /// How far the ground at a point is bedrock rather than soil, from its shape alone.
50 ///
51 /// Rises above the mean of eight points a reach around, and steep slopes, grade from soil to
52 /// bedrock by smoothsteps between their thresholds (the larger wins); `floorCover` of level ground
53 /// is bedrock in noise patches. Reads the ground at points, so it agrees on every grid and tile.
54
21/72
<unknown function 24>:
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
<unknown function 54>:
✗ Branch 0 not taken.
✓ Branch 1 taken 13554 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 time.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 time.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 time.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
terraingen::ExposureParams::operator==(terraingen::ExposureParams const&) const:
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 time.
✗ Branch 14 not taken.
✓ Branch 15 taken 1 time.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 1 time.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✓ Branch 25 taken 1 time.
✗ Branch 26 not taken.
✓ Branch 27 taken 1 time.
✗ Branch 28 not taken.
✓ Branch 29 taken 1 time.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✓ Branch 36 taken 1 time.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✓ Branch 40 taken 1 time.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✓ Branch 44 taken 1 time.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
13572 class ExposureField {
55 public:
56 /// Makes a field; on a sphere, surroundings are read in each point's local frame.
57 /// @throws std::invalid_argument as `ValidateExposureParams`.
58 explicit ExposureField(const ExposureParams& params = {}, const Surface& surface = {});
59
60 /// The share of bedrock at world (x, y), in [0, 1] (0 soil, 1 bedrock; 0 without data).
61 /// Neighbors without data count as level with the point.
62 /// @param spacing Sample spacing in world units.
63 double Exposure(const Ground& ground, double x, double y, double spacing) const;
64
65 /// `Exposure` with the surroundings read on `surface` in place of the field's own.
66 /// @param spacing Sample spacing in world units.
67 double Exposure(const Ground& ground, const Surface& surface, double x, double y,
68 double spacing) const;
69
70 /// Fills `raster` with `Exposure` at each cell center.
71 /// @param spacing Sample spacing in world units.
72 /// @param threads Worker threads; 0 for every hardware thread.
73 void Fill(Raster<float>& raster, const Ground& ground, double spacing,
74 unsigned threads = 1) const;
75
76 /// The thresholds the field was made with.
77 1 const ExposureParams& Params() const {
78
1/1
✓ Branch 1 taken 1 time.
1 return m_params;
79 }
80
81 private:
82 ExposureParams m_params;
83 Surface m_surface;
84 /// The surroundings read around a point, in meters east and north of it.
85 std::array<Vec2d, 8> m_ring{};
86 };
87
88 /// The exposure field a layer may read, at a fixed ground spacing, on the surface it is read on;
89 /// empty when the layer has none.
90
6/9
<unknown function 54>:
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 290 times.
✓ Branch 4 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
terraingen::ExposureParams::operator==(terraingen::ExposureParams const&) const:
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 time.
438 class LayerExposure {
91 public:
92
3/3
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 49 times.
✓ Branch 1 taken 356 times.
430 LayerExposure() = default;
93
94 /// @param spacing Sample spacing in meters the ground is read at.
95 /// @param reader What reads the field, which starts the error message without a ground.
96 /// @throws std::invalid_argument as `ValidateExposureParams`.
97 LayerExposure(const std::optional<ExposureParams>& params, double spacing, const char* reader);
98
99 /// Whether the layer reads an exposure field.
100 3880986 explicit operator bool() const {
101
9/10
<unknown function 100>:
✓ Branch 0 taken 124080 times.
✓ Branch 1 taken 3643408 times.
<unknown function 54>:
✓ Branch 0 taken 2518 times.
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 29912 times.
✓ Branch 1 taken 290 times.
✓ Branch 2 taken 2010 times.
✓ Branch 3 taken 78755 times.
terraingen::ExposureParams::operator==(terraingen::ExposureParams const&) const:
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
3880986 return m_field.has_value();
102 }
103
104 /// How far (m) around a point the field reads the ground; 0 when not set.
105 7 double Reach() const {
106
2/4
✓ Branch 0 taken 5 times.
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 1 taken 1 time.
7 return m_field ? m_field->Params().reach : 0.0;
107 }
108
109 /// Checks that a layer reading the field was given a ground.
110 /// @throws std::invalid_argument if the field is set and `ground` is null.
111 void Require(const Ground* ground) const;
112
113 /// The field's exposure at world (x, y) on `ground` lying on `surface`; call only when set.
114 /// @throws std::invalid_argument if `ground` is null.
115 double At(double x, double y, const Ground* ground, const Surface& surface) const;
116
117 private:
118 std::optional<ExposureField> m_field;
119 double m_spacing = 1.0;
120 const char* m_reader = "";
121 };
122
123 } // namespace terraingen
124