./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
10 / 10
100.0%
Branches:
8 / 9
88.9%
Functions:
-
-%

include/terraingen/albedo/albedo.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief The albedo source and layer interfaces, albedo compositions and their evaluation.
5 #pragma once
6
7 #include "terraingen/geometry/geometry.h"
8 #include "terraingen/geometry/surface.h"
9 #include "terraingen/layers/layer.h"
10 #include "terraingen/raster/grid.h"
11 #include "terraingen/raster/raster.h"
12 #include "terraingen/sources/source.h"
13
14 #include <array>
15 #include <cstddef>
16 #include <functional>
17 #include <memory>
18 #include <vector>
19
20 namespace terraingen {
21
22 /// The most bands an albedo has: three for red, green and blue.
23 inline constexpr std::size_t kMaxAlbedoBands = 3;
24
25 /// An albedo at one point: the first `Bands()` values are used, the rest are 0.
26 using AlbedoSample = std::array<float, kMaxAlbedoBands>;
27
28 /// Checks an evaluation context.
29 /// @throws std::invalid_argument if `context.spacing` is not positive and finite.
30 void ValidateAlbedoContext(const LayerContext& context);
31
32 /// Where an albedo starts: a map from orbit, a synthetic color, or a source of your own.
33 /// Values are reflectances (normal albedo, or reflectance at a wavelength), one per band.
34 ///
35 /// Sources must be deterministic and thread safe, and `Fill` must give every cell the bits
36 /// `ValueAt` gives at its center, so tiles agree with the whole.
37
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 23 times.
126 class AlbedoSource {
38 public:
39 69 virtual ~AlbedoSource() = default;
40
41 /// The number of bands: 1 for a single band, or 3 for red, green and blue.
42 virtual std::size_t Bands() const = 0;
43
44 /// The albedo at world point (`x`, `y`); NaN in every band where the source has no data.
45 virtual AlbedoSample ValueAt(double x, double y, const LayerContext& context) const = 0;
46
47 /// Sets every cell of each band to `ValueAt` at its center; the default does exactly
48 /// that, cell by cell, on `context.threads` threads.
49 /// @param bands `Bands()` rasters on one grid.
50 virtual void Fill(std::vector<Raster<float>>& bands, const LayerContext& context) const;
51 };
52
53 /// A change to an albedo: bright ejecta around fresh craters, or a layer of your own.
54 ///
55 /// Layers must be deterministic and thread safe, and `Apply` must give every cell the bits
56 /// `ApplyAt` gives at its center.
57
1/1
✓ Branch 1 taken 412 times.
474 class AlbedoLayer {
58 public:
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
37 virtual ~AlbedoLayer() = default;
60
61 /// Applies the layer in place to `value`, the albedo at world point (`x`, `y`); NaN stays NaN.
62 /// @param bands Bands of `value` in use.
63 virtual void ApplyAt(AlbedoSample& value, std::size_t bands, double x, double y,
64 const LayerContext& context) const = 0;
65
66 /// Applies the layer in place to every cell of `bands` (one raster per band on one grid); the
67 /// default calls `ApplyAt` at each cell center on `context.threads` threads.
68 virtual void Apply(std::vector<Raster<float>>& bands, const LayerContext& context) const;
69 };
70
71 /// An albedo as a composition: values from `source`, then each of `layers` in order.
72
4/4
<unknown function 37>:
✓ Branch 1 taken 1 time.
<unknown function 39>:
✓ Branch 1 taken 2 times.
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 1 time.
34 struct Albedo {
73 std::shared_ptr<const AlbedoSource> source; ///< Where values start; required.
74 std::vector<std::shared_ptr<const AlbedoLayer>> layers; ///< Applied in order; none null.
75 /// Sets how a grid's cell size turns into a spacing in meters.
76 Surface surface{};
77 };
78
79 /// Evaluates `albedo` over `grid`: one raster per band, with no-data NaN.
80 /// @param context `spacing` in meters, raised to the grid's spacing on the albedo's surface
81 /// (`RasterGrid::Spacing`) when smaller or 0; a null `surface` reads the albedo's.
82 /// @throws std::invalid_argument if `albedo` has no source, holds a null layer, the source has a
83 /// band count other than 1 or 3, `grid` is empty, or the spacing is negative or not finite.
84 std::vector<Raster<float>> EvaluateAlbedo(const Albedo& albedo, const RasterGrid& grid,
85 LayerContext context);
86
87 /// The albedo at world point (`x`, `y`): the cell centered there of `EvaluateAlbedo` over a grid
88 /// of `context.spacing`, to the same bits; a null `context.surface` reads the albedo's.
89 /// @throws std::invalid_argument as `EvaluateAlbedo`.
90 AlbedoSample EvaluateAlbedoAt(const Albedo& albedo, double x, double y, LayerContext context);
91
92 /// Visits the albedo at one cell center: `visit(value, x, y)` may change `value`.
93 using AlbedoCellVisitor = std::function<void(AlbedoSample& value, double x, double y)>;
94
95 /// Calls `visit` for the albedo at every cell center, writing back what it leaves in
96 /// `value`: the cell loop shared by `AlbedoSource::Fill` and `AlbedoLayer::Apply`.
97 /// @param bands The bands, one raster per band on one grid.
98 /// @param threads Threads to split the rows over; 0 for every hardware thread.
99 void ForEachAlbedoCell(std::vector<Raster<float>>& bands, unsigned threads,
100 const AlbedoCellVisitor& visit);
101
102 /// Visits the albedo at one cell by its column and row: `visit(value, col, row)` may change
103 /// `value`.
104 using AlbedoPixelVisitor =
105 std::function<void(AlbedoSample& value, std::size_t col, std::size_t row)>;
106
107 /// `ForEachAlbedoCell` with each cell's column and row instead of its center, for layers that
108 /// prepare per-cell values over the whole grid.
109 /// @param threads Threads to split the rows over; 0 for every hardware thread.
110 void ForEachAlbedoPixel(std::vector<Raster<float>>& bands, unsigned threads,
111 const AlbedoPixelVisitor& visit);
112
113 /// `ForEachAlbedoCell` in blocks of rows, for visitors that prepare per block.
114 /// @param threads Threads to split the blocks over; 0 for every hardware thread.
115 /// @param blockRows Rows per block; positive.
116 /// @param makeVisitor Returns the visitor of rows [`rowBegin`, `rowEnd`), once per block.
117 void ForEachAlbedoBlock(
118 std::vector<Raster<float>>& bands, unsigned threads, std::size_t blockRows,
119 const std::function<AlbedoCellVisitor(std::size_t rowBegin, std::size_t rowEnd)>& makeVisitor);
120
121 } // namespace terraingen
122