include/terraingen/albedo/synthetic.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Synthetic albedos: a base color with noise in hue, saturation and value. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/albedo/albedo.h" | ||
| 8 | #include "terraingen/geometry/geometry.h" | ||
| 9 | #include "terraingen/geometry/surface.h" | ||
| 10 | #include "terraingen/noise/fractals.h" | ||
| 11 | |||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | namespace terraingen { | ||
| 15 | |||
| 16 | /// The default value of a synthetic albedo: the reflectance of mature mare soil at 750 nm. | ||
| 17 | inline constexpr double kDefaultSyntheticAlbedoValue = 0.110; | ||
| 18 | |||
| 19 | /// The color of a `SyntheticAlbedo`: a base color in hue, saturation and value, each | ||
| 20 | /// varied by a stack of fractals (`EvaluateFractals`, with amplitudes in the channel's | ||
| 21 | /// units). | ||
| 22 | 63 | struct SyntheticAlbedoOptions { | |
| 23 | /// Three bands (red, green, blue) from the color; otherwise one band, its value. | ||
| 24 | bool rgb = false; | ||
| 25 | double hueDegrees = 0.0; ///< Degrees; noise added to it wraps around. | ||
| 26 | double saturation = 0.0; ///< In [0, 1]; 0 is gray. | ||
| 27 | /// In [0, 1]: the brightest band's reflectance (`kDefaultSyntheticAlbedoValue` by default). | ||
| 28 | double value = kDefaultSyntheticAlbedoValue; | ||
| 29 | std::vector<Fractal> hueNoiseDegrees; ///< Added to the hue, in degrees. | ||
| 30 | std::vector<Fractal> saturationNoise; ///< Added to the saturation. | ||
| 31 | /// Relative: the value is multiplied by 1 + the noise, so its contrast scales with it. | ||
| 32 | std::vector<Fractal> valueNoise; | ||
| 33 | |||
| 34 | bool operator==(const SyntheticAlbedoOptions& other) const { | ||
| 35 | return rgb == other.rgb && hueDegrees == other.hueDegrees && | ||
| 36 | saturation == other.saturation && value == other.value && | ||
| 37 | hueNoiseDegrees == other.hueNoiseDegrees && | ||
| 38 | saturationNoise == other.saturationNoise && valueNoise == other.valueNoise; | ||
| 39 | } | ||
| 40 | bool operator!=(const SyntheticAlbedoOptions& other) const { | ||
| 41 | return !(*this == other); | ||
| 42 | } | ||
| 43 | }; | ||
| 44 | |||
| 45 | /// Checks synthetic albedo settings. | ||
| 46 | /// @throws std::invalid_argument for a hue, saturation or value that is not finite, a saturation | ||
| 47 | /// or value outside [0, 1], more than `kMaxFractals` fractals in a stack, or an invalid fractal. | ||
| 48 | void ValidateSyntheticAlbedoOptions(const SyntheticAlbedoOptions& options); | ||
| 49 | |||
| 50 | /// Converts a color from hue, saturation and value to red, green and blue in [0, 1] by the | ||
| 51 | /// hexcone model (Smith 1978). | ||
| 52 | /// @param hueDegrees Hue in degrees; any value, wrapped. | ||
| 53 | /// @param saturation Saturation in [0, 1]. | ||
| 54 | /// @param value Value in [0, 1]. | ||
| 55 | AlbedoSample HsvToRgb(double hueDegrees, double saturation, double value); | ||
| 56 | |||
| 57 | /// An albedo for terrain without data: a base color with noise in each channel, the | ||
| 58 | /// saturation and value clamped to [0, 1] after it. Noise is measured from the context's | ||
| 59 | /// `ReferencePoint`, keeping single-precision noise accurate where coordinates are large; on a | ||
| 60 | /// body it is 3D noise at each point's 3D position (`EvaluateFractals3`). | ||
| 61 | 92 | class SyntheticAlbedo final : public AlbedoSource { | |
| 62 | public: | ||
| 63 | /// The synthetic albedo with the color and noise of `options`. | ||
| 64 | /// @throws std::invalid_argument as `ValidateSyntheticAlbedoOptions`. | ||
| 65 | explicit SyntheticAlbedo(SyntheticAlbedoOptions options); | ||
| 66 | |||
| 67 | /// 3 bands when `rgb` is set, otherwise 1. | ||
| 68 | 1725 | std::size_t Bands() const override { | |
| 69 | 1725 | return m_options.rgb ? kMaxAlbedoBands : 1; | |
| 70 | } | ||
| 71 | |||
| 72 | /// The color with noise at world point (`x`, `y`). | ||
| 73 | AlbedoSample ValueAt(double x, double y, const LayerContext& context) const override; | ||
| 74 | |||
| 75 | /// The color and its noise. | ||
| 76 | const SyntheticAlbedoOptions& Options() const { | ||
| 77 | return m_options; | ||
| 78 | } | ||
| 79 | |||
| 80 | private: | ||
| 81 | static double Noise(const std::vector<Fractal>& fractals, double x, double y, | ||
| 82 | const LayerContext& context); | ||
| 83 | |||
| 84 | SyntheticAlbedoOptions m_options; | ||
| 85 | }; | ||
| 86 | |||
| 87 | } // namespace terraingen | ||
| 88 |