./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
14 / 14
100.0%
Branches:
16 / 24
66.7%
Functions:
2 / 2, 2 excl
100.0%

include/terraingen/layers/spectral_continuation.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief `SpectralContinuationLayer`, detail finer than a base DEM that continues the base's
5 /// measured power-law spectrum (`EstimatePowerLawSpectrum`).
6 #pragma once
7
8 #include "terraingen/analysis/spectrum.h"
9 #include "terraingen/geometry/geometry.h"
10 #include "terraingen/geometry/surface.h"
11 #include "terraingen/layers/fade.h"
12 #include "terraingen/layers/layer.h"
13 #include "terraingen/raster/raster.h"
14
15 #include <cstdint>
16 #include <vector>
17
18 namespace terraingen {
19
20 /// The spectrum a `SpectralContinuationLayer` continues and the wavelengths it adds.
21 2 struct SpectralContinuationOptions {
22 double pixelSize = 1.0; ///< Cell size of the base in meters; detail starts at twice it.
23 double slope = 3.0; ///< Spectral slope beta, in [2, 4] (`PowerLawSpectrum::slope`).
24 double octaveRms = 0.1; ///< Meters (`PowerLawSpectrum::octaveRms`).
25 double minWavelength = 0.05; ///< Shortest wavelength added, in meters.
26 /// Octaves above the base's Nyquist frequency over which the detail tapers in, from none at
27 /// the Nyquist frequency to the whole power law; positive.
28 double taperOctaves = 1.0;
29 std::uint32_t seed = 1; ///< Varies the detail.
30 /// How wavelengths too fine to resolve fade out, by samples per wavelength.
31 ResolutionFade fade{};
32
33 2 bool operator==(const SpectralContinuationOptions& other) const {
34
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return pixelSize == other.pixelSize && slope == other.slope &&
35
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 octaveRms == other.octaveRms && minWavelength == other.minWavelength &&
36
5/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
4 taperOctaves == other.taperOctaves && seed == other.seed && fade == other.fade;
37 }
38 1 bool operator!=(const SpectralContinuationOptions& other) const {
39
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return !(*this == other);
40 }
41 };
42
43 /// Checks spectral continuation options.
44 /// @throws std::invalid_argument for a `pixelSize`, `minWavelength` or `taperOctaves` that is not
45 /// positive and finite, a `slope` outside [2, 4], a negative or non-finite `octaveRms`, or an
46 /// invalid `fade`.
47 void ValidateSpectralContinuationOptions(const SpectralContinuationOptions& options);
48
49 /// Detail finer than a base DEM that continues its power-law spectrum with random phases (Saupe
50 /// 1988), as sparse Gabor noise (Lagae et al. 2009, https://doi.org/10.1145/1531326.1531360) so
51 /// grids, tiles and points agree; it adds nothing at wavelengths the base resolves.
52
4/5
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 25 taken 6 times.
✓ Branch 32 taken 1 time.
27 class SpectralContinuationLayer final : public Layer {
53 public:
54 /// @throws std::invalid_argument as `ValidateSpectralContinuationOptions`.
55 explicit SpectralContinuationLayer(SpectralContinuationOptions options);
56
57 /// The options the layer was made with.
58 35052 const SpectralContinuationOptions& Options() const {
59
3/3
✓ Branch 1 taken 35000 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 36 times.
35052 return m_options;
60 }
61
62 /// The layer with its seed `ReseedWord(seed, 0)`.
63 LayerPtr Reseeded(std::uint64_t seed) const override;
64
65 protected:
66 void ApplySamples(SampleBlock& samples, const LayerContext& context) const override;
67
68 private:
69 /// One octave band of impulses.
70 struct Band {
71 double frequency = 0.0; ///< Lowest frequency, in cycles per meter.
72 double bandwidth = 0.0; ///< Gaussian envelope parameter a, in 1 / m.
73 double reach = 0.0; ///< Impulse radius and lattice cell side, in meters.
74 std::uint32_t seed = 0; ///< Seed of the band's lattice streams.
75 };
76
77 struct ImpulseTable;
78
79 /// The target power spectral density at `frequency` cycles per meter, in m^4.
80 double TargetPsd(double frequency) const;
81
82 /// The impulses of `band` over the lattice cells that reach the box [`minX`, `maxX`] x
83 /// [`minY`, `maxY`] meters, faded for `spacing` meters.
84 ImpulseTable Table(const Band& band, double minX, double minY, double maxX, double maxY,
85 double spacing) const;
86 /// The detail at `p` meters from the impulses of `tables`.
87 static double DetailAt(const std::vector<ImpulseTable>& tables, const Vec2d& p);
88 /// Adds the detail at `positions` (meters) to `heights` at `spacing` meters.
89 void AddDetail(const std::vector<Vec2d>& positions, const std::vector<float*>& heights,
90 double spacing) const;
91
92 SpectralContinuationOptions m_options;
93 double m_coefficient = 0.0; ///< C in m^(4 - beta), P(f) = C f^-beta.
94 std::vector<Band> m_bands;
95 };
96
97 } // namespace terraingen
98