./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
9 / 9
100.0%
Branches:
8 / 15
53.3%
Functions:
2 / 2, 1 excl
100.0%

include/terraingen/layers/roughness.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief `RoughnessLayer`, fine self-affine roughness from octaves of Perlin noise.
5 #pragma once
6
7 #include "terraingen/geometry/surface.h"
8 #include "terraingen/layers/fade.h"
9 #include "terraingen/layers/layer.h"
10
11 #include <array>
12 #include <cstdint>
13 #include <vector>
14
15 namespace terraingen {
16
17 /// The roughness a `RoughnessLayer` adds: RMS slope over a baseline and Hurst exponent, as
18 /// Shepard et al. (2001) give it.
19 4 struct RoughnessOptions {
20 double rmsSlope = 0.15; ///< RMS slope (rise over run) over `baseline`.
21 double baseline = 0.25; ///< Baseline the RMS slope is given over (m).
22 double hurst = 0.8; ///< Hurst exponent, in (0, 1].
23 /// Longest octave (m); by default 16 m, below the LOLA slope baselines (Rosenburg et al. 2011,
24 /// https://doi.org/10.1029/2010JE003716).
25 double maxWavelength = 16.0;
26 double minWavelength = 0.01; ///< Shortest octave (m); octaves halve down to it.
27 std::uint32_t seed = 1; ///< Varies the octaves' offsets and rotations.
28 /// How octaves too fine to resolve fade out, by samples per wavelength.
29 ResolutionFade fade{};
30
31 2 bool operator==(const RoughnessOptions& other) const {
32
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 return rmsSlope == other.rmsSlope && baseline == other.baseline && hurst == other.hurst &&
33
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 maxWavelength == other.maxWavelength && minWavelength == other.minWavelength &&
34
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
4 seed == other.seed && fade == other.fade;
35 }
36 };
37
38 /// Checks roughness options.
39 /// @throws std::invalid_argument for a non-positive `rmsSlope` or `baseline`, a `hurst` outside
40 /// (0, 1], wavelengths that do not satisfy 0 < `minWavelength` <= `maxWavelength` or that span more
41 /// than 40 octaves, or an invalid `fade`.
42 void ValidateRoughnessOptions(const RoughnessOptions& options);
43
44 /// Fine roughness: octaves of Perlin noise from `maxWavelength` to `minWavelength`, amplitudes
45 /// growing as wavelength^H and scaled to `rmsSlope` over `baseline`; unresolved octaves fade.
46 class RoughnessLayer final : public FormulaLayer {
47 public:
48 /// On a body each octave is 3D Perlin noise at the 3D point, its lattice turned by a uniform
49 /// random rotation.
50 /// @throws std::invalid_argument as `ValidateRoughnessOptions`.
51 explicit RoughnessLayer(RoughnessOptions options);
52
53 /// The RMS slope (rise over run) over `baseline` meters on `surface` with every octave
54 /// resolved.
55 double RmsSlope(double baseline, const Surface& surface = {}) const;
56
57 /// The octaves' wavelengths in meters, longest first.
58 std::vector<double> Wavelengths() const;
59
60 /// The options the layer was made with.
61 3 const RoughnessOptions& Options() const {
62
1/1
✓ Branch 1 taken 1 time.
3 return m_options;
63 }
64
65 /// The layer with its seed `ReseedWord(seed, 0)`.
66 LayerPtr Reseeded(std::uint64_t seed) const override;
67
68 protected:
69 float HeightAt(float height, double x, double y, const LayerContext& context) const override;
70
71 private:
72 struct Octave {
73 double wavelength;
74 double amplitude;
75 double cos;
76 double sin;
77 double offsetX;
78 double offsetY;
79 std::uint32_t seed;
80 std::array<double, 9> rotation{};
81 double offsetZ = 0.0;
82 };
83
84 /// Octave `i` of `wavelength`, before its amplitude is scaled to the RMS slope; with its
85 /// rotation on a body.
86 Octave MakeOctave(std::uint32_t i, double wavelength, bool body) const;
87 /// The octaves on a body or a plane, scaled to the RMS slope.
88 std::vector<Octave> MakeOctaves(bool body) const;
89 /// The RMS slope over `baseline` meters of `octaves` on a body or a plane.
90 static double OctavesRmsSlope(const std::vector<Octave>& octaves, double baseline, bool body);
91 /// The noise of one octave at (`x`, `y`) on `surface`, in [-1, 1].
92 static double OctaveNoise(const Octave& octave, const Surface& surface, double x, double y);
93
94 RoughnessOptions m_options;
95 std::vector<Octave> m_planeOctaves;
96 std::vector<Octave> m_bodyOctaves;
97 };
98
99 } // namespace terraingen
100