./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
9 / 11
81.8%
Branches:
1 / 1
100.0%
Functions:
2 / 3, 2 excl
66.7%

include/terraingen/layers/sand_fill.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief `SandFillLayer`, sand pooled in the lows of the ground beneath it.
5 #pragma once
6
7 #include "terraingen/geometry/geometry.h"
8 #include "terraingen/layers/layer.h"
9 #include "terraingen/layers/material.h"
10
11 #include <array>
12
13 namespace terraingen {
14
15 /// How much sand a `SandFillLayer` pools, and where.
16 3 struct SandFillOptions {
17 double reach = 10.0; ///< Radius in meters of the surroundings a low is measured against.
18 /// Share, in [0, 1], of a low's depth below its surroundings that sand fills.
19 double fill = 0.6;
20 double maxThickness = 0.5; ///< The thickest the sand gets, approached smoothly (m).
21 /// Sand thickness in meters at which it fully covers the ground; thinner sand covers part.
22 double coverThickness = 0.02;
23
24 bool operator==(const SandFillOptions& other) const {
25 return reach == other.reach && fill == other.fill && maxThickness == other.maxThickness &&
26 coverThickness == other.coverThickness;
27 }
28 };
29
30 /// Checks sand fill options.
31 /// @throws std::invalid_argument for a reach, maximum or cover thickness that is not positive and
32 /// finite, or a fill outside [0, 1].
33 void ValidateSandFillOptions(const SandFillOptions& options);
34
35 /// Sand pooled in the lows of the ground beneath it, filling `fill` of the depth below the mean
36 /// height on two rings, up to `maxThickness`; it lays sand over the material map.
37 9 class SandFillLayer final : public FormulaLayer, public MaterialSource {
38 public:
39 /// On a body, the rings lie in each point's local frame.
40 /// @throws std::invalid_argument as `ValidateSandFillOptions`.
41 explicit SandFillLayer(SandFillOptions options = {});
42
43 /// The sand thickness at world (x, y) over `context.ground`, read at `context.spacing` on
44 /// its surface, in meters.
45 /// @throws std::invalid_argument without `context.ground`.
46 double ThicknessAt(double x, double y, const LayerContext& context) const;
47
48 /// The options the layer was made with.
49 ✗ const SandFillOptions& Options() const {
50 ✗ return m_options;
51 }
52
53 /// `SandFillOptions::reach`.
54 8 double GroundReach(double /*spacing*/) const override {
55
1/1
✓ Branch 1 taken 1 time.
8 return m_options.reach;
56 }
57
58 6 MaterialClass Material() const override {
59 6 return MaterialClass::Sand;
60 }
61 /// @throws std::invalid_argument without `context.ground`.
62 double MaterialCoverAt(double x, double y, const LayerContext& context) const override;
63
64 protected:
65 float HeightAt(float height, double x, double y, const LayerContext& context) const override;
66 void CheckContext(const LayerContext& context) const override;
67
68 private:
69 /// The thickness at (x, y) where the ground is `height` meters high, read on the context's.
70 double Thickness(double x, double y, double height, const LayerContext& context) const;
71
72 /// Points on each ring the surroundings are read at; the inner ring is turned half a step.
73 static constexpr int kRingPoints = 8;
74
75 SandFillOptions m_options;
76 /// The two rings read around a point, inner first, in meters east and north of it.
77 std::array<Vec2d, 2 * kRingPoints> m_rings{};
78 };
79
80 } // namespace terraingen
81