./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
11 / 13
84.6%
Branches:
7 / 13
53.8%
Functions:
3 / 4, 1 excl
75.0%

include/terraingen/layers/sand_shadow.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief `SandShadowLayer`, sand tails in the lee of a boulder layer's boulders.
5 #pragma once
6
7 #include "terraingen/layers/boulders/boulder_layer.h"
8 #include "terraingen/layers/fade.h"
9 #include "terraingen/layers/layer.h"
10 #include "terraingen/layers/material.h"
11
12 #include <memory>
13 #include <vector>
14
15 namespace terraingen {
16
17 /// The sand shadows a `SandShadowLayer` lays behind boulders. Angles are in degrees.
18 2 struct SandShadowOptions {
19 /// Where the wind blows toward, degrees counter-clockwise from +x (local east on a body).
20 double windAzimuthDegrees = 0.0;
21 /// Length of a shadow beyond the boulder over the boulder's width. 1.1 is the reattachment
22 /// length behind a dome (Savory and Toy 1986, after Hesp and Smyth 2017); their modeled
23 /// shadow dunes run 1.3 to 5.6 widths, longer in lighter winds.
24 double lengthRatio = 1.1;
25 /// Slope of a shadow's flanks, below 90 deg; at most the angle of repose, about 32 deg for dry
26 /// medium sand (Hesp 1981).
27 double flankAngleDegrees = 32.0;
28 double minDiameter = 0.0; ///< Smallest boulder (m) with a shadow.
29 /// Sand depth (m) that fully covers the ground in the material map.
30 double coverThickness = 0.02;
31 ResolutionFade fade{}; ///< Fade of shadows behind boulders too small for the spacing.
32
33 1 bool operator==(const SandShadowOptions& other) const {
34
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return windAzimuthDegrees == other.windAzimuthDegrees && lengthRatio == other.lengthRatio &&
35
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 flankAngleDegrees == other.flankAngleDegrees && minDiameter == other.minDiameter &&
36
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 coverThickness == other.coverThickness && fade == other.fade;
37 }
38 };
39
40 /// Checks sand shadow options.
41 /// @throws std::invalid_argument for a non-finite wind azimuth, a length ratio or cover thickness
42 /// that is not positive and finite, a flank angle outside (0, 90) deg, a negative minimum
43 /// diameter, or an invalid fade.
44 void ValidateSandShadowOptions(const SandShadowOptions& options);
45
46 /// Sand shadows: a pyramidal sand tail in the lee of each boulder of a boulder layer, as wide as
47 /// the boulder and `lengthRatio` widths long (Hesp 1981); place it after the boulder layer.
48 class SandShadowLayer final : public Layer, public MaterialSource {
49 public:
50 /// @param boulders The boulder layer whose boulders cast shadows.
51 /// @throws std::invalid_argument for invalid options or a null boulder layer.
52 SandShadowLayer(std::shared_ptr<const BoulderLayer> boulders, SandShadowOptions options);
53
54 /// The sand depth (m) the shadows lay at world (x, y), evaluated as in `context`: its spacing
55 /// picks the boulders as the boulder layer does (0 keeps every one), and its ground is needed
56 /// with the boulder layer's exposure field.
57 double ThicknessAt(double x, double y, const LayerContext& context = {}) const;
58
59 /// The options the layer was made with.
60 1 const SandShadowOptions& Options() const {
61
1/1
✓ Branch 1 taken 1 time.
1 return m_options;
62 }
63
64 /// The boulder layer the shadows trail, which must come before this layer.
65 4 std::vector<const Layer*> Dependencies() const override {
66 4 return {m_boulders.get()};
67 }
68
69 /// The longest shadow plus the boulder layer's reach, for the boulders it lists.
70 double GroundReach(double spacing) const override;
71
72 /// A copy trailing the replacement of its boulder layer; null when it is not replaced.
73 LayerPtr Rebound(const LayerReplacements& replaced) const override;
74
75 3073 MaterialClass Material() const override {
76 3073 return MaterialClass::Sand;
77 }
78 /// The sand depth over `coverThickness`, smoothed, in [0, 1].
79 double MaterialCoverAt(double x, double y, const LayerContext& context) const override;
80 /// `MaterialCoverAt` over `grid`, the boulders found once for the whole grid.
81 Raster<double> MaterialCoverOver(const RasterGrid& grid,
82 const LayerContext& context) const override;
83
84 protected:
85 void ApplySamples(SampleBlock& samples, const LayerContext& context) const override;
86
87 private:
88 /// One boulder's shadow, ready to draw.
89 struct Shadow {
90 Vec2d center{};
91 double width = 0.0;
92 double cap = 0.0;
93 double weight = 1.0;
94 Bounds2d box{};
95 };
96 /// How far (m) the largest boulder's shadow reaches from its center.
97 double LongestShadow() const;
98 std::vector<Shadow> ShadowsOver(const Bounds2d& region, double spacing, const Ground* ground,
99 const Surface& surface) const;
100 double ShadowDepth(const Shadow& shadow, double x, double y, const Surface& surface) const;
101 static std::vector<Bounds2d> Boxes(const std::vector<Shadow>& shadows);
102
103 std::shared_ptr<const BoulderLayer> m_boulders;
104 SandShadowOptions m_options;
105 Vec2d m_downwind{1.0, 0.0};
106 double m_flankSlope = 0.0;
107 };
108
109 } // namespace terraingen
110