include/terraingen/albedo/raster_albedo.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `RasterAlbedo`, an albedo map upscaled with fractal detail that continues its roughness. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/albedo/albedo.h" | ||
| 8 | #include "terraingen/geometry/surface.h" | ||
| 9 | #include "terraingen/layers/fade.h" | ||
| 10 | #include "terraingen/raster/resample.h" | ||
| 11 | |||
| 12 | #include <cstddef> | ||
| 13 | #include <cstdint> | ||
| 14 | #include <optional> | ||
| 15 | #include <vector> | ||
| 16 | |||
| 17 | namespace terraingen { | ||
| 18 | |||
| 19 | /// Detail below a map's resolution: fractal noise that continues the map's own measured | ||
| 20 | /// roughness to finer scales, multiplying the albedo. Octaves too fine for the sample spacing fade | ||
| 21 | /// out. | ||
| 22 | 4 | struct AlbedoDetail { | |
| 23 | bool enabled = true; ///< Whether detail is added. | ||
| 24 | std::uint32_t seed = 0; ///< Noise seed. | ||
| 25 | int octaves = 4; ///< Octaves of detail; 1 to 16. | ||
| 26 | double scale = 1.0; ///< Multiplies the RMS of every octave; 0 adds none. | ||
| 27 | /// The Hurst exponent to use instead of the measured one, in [0, 1]. | ||
| 28 | std::optional<double> hurst; | ||
| 29 | ResolutionFade fade{}; ///< How octaves too fine for the sample spacing fade out. | ||
| 30 | |||
| 31 | bool operator==(const AlbedoDetail& other) const { | ||
| 32 | return enabled == other.enabled && seed == other.seed && octaves == other.octaves && | ||
| 33 | scale == other.scale && hurst == other.hurst && fade == other.fade; | ||
| 34 | } | ||
| 35 | bool operator!=(const AlbedoDetail& other) const { | ||
| 36 | return !(*this == other); | ||
| 37 | } | ||
| 38 | }; | ||
| 39 | |||
| 40 | /// Checks albedo detail settings. | ||
| 41 | /// @throws std::invalid_argument naming the first field out of its range, or for an invalid fade. | ||
| 42 | void ValidateAlbedoDetail(const AlbedoDetail& detail); | ||
| 43 | |||
| 44 | /// How a `RasterAlbedo` interpolates its map and adds detail. | ||
| 45 | 3 | struct RasterAlbedoOptions { | |
| 46 | ResampleMethod method = ResampleMethod::Cubic; ///< Interpolation of the map. | ||
| 47 | AlbedoDetail detail{}; ///< Sub-cell detail. | ||
| 48 | |||
| 49 | bool operator==(const RasterAlbedoOptions& other) const { | ||
| 50 | return method == other.method && detail == other.detail; | ||
| 51 | } | ||
| 52 | bool operator!=(const RasterAlbedoOptions& other) const { | ||
| 53 | return !(*this == other); | ||
| 54 | } | ||
| 55 | }; | ||
| 56 | |||
| 57 | /// An albedo map, such as a normal albedo mosaic from orbit, upscaled to the grid: | ||
| 58 | /// interpolated with `method` (`SampleRaster`), then given sub-cell detail (`AlbedoDetail`). | ||
| 59 | /// Outside the map, or where it has no data, the albedo is NaN. | ||
| 60 | 14 | class RasterAlbedo final : public AlbedoSource { | |
| 61 | public: | ||
| 62 | /// Builds the albedo and measures the map's roughness; on a body the map is in longitude and | ||
| 63 | /// latitude, its cells sized on the context's surface. | ||
| 64 | /// @param bands 1 or 3 rasters on one grid. | ||
| 65 | /// @throws std::invalid_argument for a bad band count, mismatched rasters or invalid options. | ||
| 66 | RasterAlbedo(std::vector<Raster<float>> bands, RasterAlbedoOptions options = {}); | ||
| 67 | |||
| 68 | /// 1 or 3 bands, as the map has. | ||
| 69 | 12 | std::size_t Bands() const override { | |
| 70 | 12 | return m_bands.size(); | |
| 71 | } | ||
| 72 | |||
| 73 | /// The map interpolated at world (`x`, `y`) (longitude and latitude on a sphere), with detail | ||
| 74 | /// resolved at `context.spacing`; NaN outside the map or where it has no data. | ||
| 75 | AlbedoSample ValueAt(double x, double y, const LayerContext& context) const override; | ||
| 76 | |||
| 77 | /// The Hurst exponent the detail uses: `detail.hurst`, or the one measured. | ||
| 78 | 10 | double Hurst() const { | |
| 79 | 10 | return m_hurst; | |
| 80 | } | ||
| 81 | |||
| 82 | /// The map's roughness at one cell, sqrt(D(c) / 2) of the logarithm of the brightness; 0 when | ||
| 83 | /// the map is too small or too uniform to measure. | ||
| 84 | 5 | double CellRoughness() const { | |
| 85 | 5 | return m_roughness; | |
| 86 | } | ||
| 87 | |||
| 88 | /// Interpolation and detail. | ||
| 89 | const RasterAlbedoOptions& Options() const { | ||
| 90 | return m_options; | ||
| 91 | } | ||
| 92 | |||
| 93 | private: | ||
| 94 | /// Measures `m_roughness` and `m_hurst` from the map. | ||
| 95 | void MeasureRoughness(); | ||
| 96 | /// The sum of the detail octaves at (`x`, `y`) on `surface` resolved at `spacing`. | ||
| 97 | double DetailSum(double x, double y, double spacing, const Surface& surface) const; | ||
| 98 | |||
| 99 | std::vector<Raster<float>> m_bands; | ||
| 100 | RasterAlbedoOptions m_options; | ||
| 101 | double m_hurst = 0.5; | ||
| 102 | double m_roughness = 0.0; | ||
| 103 | }; | ||
| 104 | |||
| 105 | } // namespace terraingen | ||
| 106 |