./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
17 / 18
94.4%
Branches:
13 / 15
86.7%
Functions:
2 / 2, 3 excl
100.0%

include/terraingen/sources/synthetic_source.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief `SyntheticSource`, heights generated from a stack of fractals over an unbounded
5 /// plane or a whole body.
6 #pragma once
7
8 #include "terraingen/geometry/surface.h"
9 #include "terraingen/noise/fractals.h"
10 #include "terraingen/noise/surface_noise.h"
11 #include "terraingen/sources/source.h"
12 #include "terraingen/sources/upsampling.h"
13
14 #include <cstdint>
15 #include <memory>
16 #include <optional>
17 #include <vector>
18
19 namespace terraingen {
20
21 /// Heights generated rather than loaded: `elevation` plus a stack of fractals
22 /// (`FractalStackAt`, measured from the origin on a plane, in 3D on a body), with no bounds.
23 ///
24 /// Without a `pixelSize` every point gets the noise evaluated there, at any spacing, and the
25 /// source has no posts (`PostSpacingAt` is infinite). With one, the noise is sampled only
26 /// at posts `pixelSize` apart, one at the origin, and the upsampler refines them, as for a DEM.
27 /// An empty stack is flat at `elevation`.
28 class SyntheticSource final : public Source {
29 public:
30 /// @param fractals The noise stack, 0 to `kMaxFractals` fractals (`ValidateFractal` each).
31 /// @param elevation Meters added everywhere; finite.
32 /// @param pixelSize Spacing of the posts in meters, converted to the surface's units on a body
33 /// (`Surface::UnitLength`) as `FlatSource` converts it; positive and finite, or unset to
34 /// evaluate the noise at every point.
35 /// @param upsampler How the posts are refined; null for cubic interpolation. Needs a
36 /// `pixelSize`.
37 /// @throws std::invalid_argument for too many or invalid fractals, a non-finite `elevation`, a
38 /// `pixelSize` that is not positive and finite, an upsampler without a `pixelSize`, or as
39 /// `Upsampler::FitTo`.
40 explicit SyntheticSource(std::vector<Fractal> fractals = {}, float elevation = 0.0f,
41 std::optional<double> pixelSize = std::nullopt,
42 std::shared_ptr<const Upsampler> upsampler = nullptr,
43 Surface surface = {});
44
45 SyntheticSource(const SyntheticSource&) = delete;
46 SyntheticSource& operator=(const SyntheticSource&) = delete;
47
48 /// A grid without cells: the source has no bounds. Its transform places the posts, post
49 /// (0, 0) at the origin, or is a unit north-up transform at the origin without a `pixelSize`.
50 RasterGrid Grid() const override;
51
52 /// The pixel size, or infinity without one. `ResolutionAt` stays infinite: generated heights
53 /// hold no measured features.
54 double PostSpacingAt(double x, double y) const override;
55
56 /// The noise stack.
57 3 const std::vector<Fractal>& Fractals() const {
58
9/11
<unknown function 57>:
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 time.
✓ Branch 9 taken 1 time.
<unknown function 58>:
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 12 taken 1 time.
12 return m_fractals;
59 }
60
61 /// The spacing of the posts in the surface's units (degrees on a body), if the noise is
62 /// sampled at posts.
63 3 const std::optional<double>& PixelSize() const {
64
3/3
<unknown function 57>:
✓ Branch 1 taken 1 time.
<unknown function 58>:
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
3 return m_pixelSize;
65 }
66
67 /// The upsampler in use, fitted to the posts; null without a pixel size.
68 7 const std::shared_ptr<const Upsampler>& UpsamplerInUse() const {
69
1/1
✓ Branch 3 taken 1 time.
7 return m_upsampler;
70 }
71
72 /// A copy whose fractal n has seed `ReseedWord(seed, n)` and whose upsampler, if it has
73 /// seeds, is reseeded with `JoinWords(ReseedWord(seed, kMaxFractals),
74 /// ReseedWord(seed, kMaxFractals + 1))` and fitted to the new posts; null for an empty stack
75 /// without a seeded upsampler.
76 std::shared_ptr<const Source> Reseeded(std::uint64_t seed) const override;
77
78 protected:
79 /// The elevation plus the noise at each sample, a grid row by row over threads, or the posts
80 /// upsampled at `spacing`.
81 void FillSamples(SampleBlock& samples, double spacing, unsigned threads) const override;
82
83 private:
84 /// The posts: the noise sampled `pixelSize` apart.
85 ✗ class NoisePosts final : public PostLattice {
86 public:
87 NoisePosts(const SyntheticSource& source, double pixelSize);
88 19325 GeoTransform Transform() const override {
89 17 return m_transform;
90 }
91 void Read(std::int64_t col0, std::int64_t row0, std::size_t width, std::size_t height,
92 Span<float> heights) const override;
93
94 private:
95 const SyntheticSource& m_source;
96 GeoTransform m_transform;
97 };
98
99 /// The elevation plus the noise at each of `points`, into `heights` of the same size.
100 void NoiseAt(Span<const Vec2d> points, Span<float> heights) const;
101
102 std::vector<Fractal> m_fractals;
103 float m_elevation;
104 std::optional<double> m_givenPixelSize;
105 std::optional<double> m_pixelSize;
106 Surface m_surface;
107 std::optional<NoisePosts> m_posts;
108 std::shared_ptr<const Upsampler> m_givenUpsampler;
109 std::shared_ptr<const Upsampler> m_upsampler;
110 };
111
112 } // namespace terraingen
113