./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
6 / 8
75.0%
Branches:
3 / 5
60.0%
Functions:
2 / 3, 1 excl
66.7%

include/terraingen/layers/pavement.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief `PavementLayer`, fractured bedrock: polygonal plates parted by cracks or raised ridges.
5 #pragma once
6
7 #include "terraingen/geometry/geometry.h"
8 #include "terraingen/layers/exposure.h"
9 #include "terraingen/layers/fade.h"
10 #include "terraingen/layers/layer.h"
11 #include "terraingen/layers/material.h"
12
13 #include <cstdint>
14 #include <optional>
15 #include <vector>
16
17 namespace terraingen {
18
19 /// The most crack octaves of a `PavementLayer` (`PavementOptions::octaves`).
20 inline constexpr int kMaxPavementOctaves = 8;
21
22 /// The plates and cracks of a `PavementLayer`. Lengths are in meters, angles in degrees.
23 struct PavementOptions {
24 double plateSize = 1.0; ///< Mean plate width: the spacing of the plates' Voronoi sites.
25 double jitter = 1.0; ///< How far sites stray from a square lattice, in [0, 1].
26 double warp = 0.15; ///< Bend of the plate edges, in plate sizes.
27 double crackWidth = 0.05; ///< Width of the crack between two plates.
28 double crackDepth = 0.05; ///< Depth of a crack, or height of a ridge with `raised`.
29 bool raised = false; ///< Whether plates are parted by raised ridges, not cracks.
30 double edgeRounding = 0.02; ///< Width of the rounded shoulder beside each crack.
31 double maxTiltDegrees = 0.05 * kRadian; ///< Largest tilt of a plate.
32 double maxStep = 0.03; ///< Largest height offset of a plate, up or down.
33 /// Crack octaves: each finer octave splits the plates again into plates `lacunarity` times
34 /// smaller, with cracks, rounding, tilts and steps `gain` times the octave before.
35 int octaves = 1;
36 double lacunarity = 2.0; ///< Plate size ratio between octaves, above 1.
37 double gain = 0.5; ///< Crack and plate relief ratio between octaves, in (0, 1].
38 std::uint32_t seed = 1; ///< Varies the plates.
39 /// When set, plates show where the ground beneath is bedrock (`ExposureField`), fading out
40 /// over soil.
41 std::optional<ExposureParams> exposure;
42 double exposureSpacing = 1.0; ///< Spacing the ground is read at for the exposure field.
43 /// On a body, the surface point whose tangent frame the plates are laid out in.
44 Vec2d anchor{};
45 ResolutionFade fade{}; ///< How plates and cracks too fine for the spacing fade out.
46
47 bool operator==(const PavementOptions& other) const {
48 return plateSize == other.plateSize && jitter == other.jitter && warp == other.warp &&
49 crackWidth == other.crackWidth && crackDepth == other.crackDepth &&
50 raised == other.raised && edgeRounding == other.edgeRounding &&
51 maxTiltDegrees == other.maxTiltDegrees && maxStep == other.maxStep &&
52 octaves == other.octaves && lacunarity == other.lacunarity && gain == other.gain &&
53 seed == other.seed && exposure == other.exposure &&
54 exposureSpacing == other.exposureSpacing && anchor == other.anchor &&
55 fade == other.fade;
56 }
57 };
58
59 /// Checks pavement options.
60 /// @throws std::invalid_argument for a plate size, crack width or exposure spacing that is not
61 /// positive and finite, a negative crack depth, edge rounding, tilt or step, a tilt of 90 deg or
62 /// more, a jitter outside [0, 1], a negative warp, octaves outside 1 to `kMaxPavementOctaves`, a
63 /// lacunarity not above 1, a gain outside (0, 1], or invalid exposure or fade.
64 void ValidatePavementOptions(const PavementOptions& options);
65
66 /// The desiccation ridges of the Old Soaker slab in Gale crater (Stein et al. 2018): 2 cm
67 /// polygons parted by 5 mm high ridges about 2 mm wide (from ridges covering about 20 % of the
68 /// slab), flat and untilted. The polygons there are mostly four-sided with T-junctions; Voronoi
69 /// plates meet at Y-junctions.
70 PavementOptions MarsDesiccationRidges();
71
72 /// Fractured bedrock pavement added to the terrain: Voronoi plates, each flat with its own tilt
73 /// and height step, parted by cracks (or ridges) with rounded shoulders, and split again by finer
74 /// octaves of cracks that end at the cracks before them; each octave fades out where too fine for
75 /// the spacing. Plate heights blend across each crack, so the surface has no steps; it lays
76 /// bedrock over the material map.
77 class PavementLayer final : public FormulaLayer, public MaterialSource {
78 public:
79 /// On a body, plates are laid out in the tangent frame of `options.anchor`. Applying the layer
80 /// with the exposure field enabled needs `context.ground`.
81 /// @throws std::invalid_argument as `ValidatePavementOptions`.
82 explicit PavementLayer(PavementOptions options);
83
84 /// The options the layer was made with.
85 ✗ const PavementOptions& Options() const {
86 ✗ return m_options;
87 }
88
89 /// The layer with its seed `ReseedWord(seed, 0)`.
90 LayerPtr Reseeded(std::uint64_t seed) const override;
91
92 /// The exposure field's reach; 0 without one.
93 2 double GroundReach(double /*spacing*/) const override {
94
3/5
<unknown function 93>:
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
terraingen::PavementLayer::GroundReach(double) const:
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 return m_exposure.Reach();
95 }
96
97 1 MaterialClass Material() const override {
98 1 return MaterialClass::Bedrock;
99 }
100 /// How fully the pavement covers (x, y): 1, or the exposure of the ground beneath.
101 /// @throws std::invalid_argument if the exposure field is enabled and `context.ground` is
102 /// null.
103 double MaterialCoverAt(double x, double y, const LayerContext& context) const override;
104
105 protected:
106 float HeightAt(float height, double x, double y, const LayerContext& context) const override;
107 void CheckContext(const LayerContext& context) const override;
108
109 private:
110 /// One crack octave: its plate size, crack and plate relief, and seed.
111 struct Level {
112 double size = 1.0;
113 double crackWidth = 0.0;
114 double crackDepth = 0.0;
115 double rounding = 0.0;
116 double maxTilt = 0.0;
117 double maxStep = 0.0;
118 std::uint32_t seed = 0;
119 };
120 /// The plate of one octave containing planar point `q`: its relief, how far it lies inside the
121 /// plate rather than in its crack (0 on an edge, 1 past the shoulder), and an id that seeds the
122 /// finer octave's plates within it.
123 struct Plate {
124 double relief = 0.0;
125 double interior = 1.0;
126 std::uint32_t id = 0;
127 };
128 /// Planar point `q` moved by the edge warp of plates `size` across.
129 Vec2d Warp(const Vec2d& q, double size, std::uint32_t seed) const;
130 Plate PlateAt(const Vec2d& q, const Level& level, std::uint32_t seed, double crackWeight) const;
131 /// The relief of every octave at planar point `q`, each faded at `spacing`.
132 double Relief(const Vec2d& q, double spacing) const;
133 double ExposureWeight(double x, double y, const LayerContext& context) const;
134
135 PavementOptions m_options;
136 LayerExposure m_exposure;
137 std::vector<Level> m_levels;
138 };
139
140 } // namespace terraingen
141