src/albedo/bands.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Internal helpers of the albedo layers: band scaling, soft edges, lobes and lifetimes. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/albedo/albedo.h" | ||
| 8 | #include "terraingen/geometry/geometry.h" | ||
| 9 | #include "terraingen/random/value_distribution.h" | ||
| 10 | |||
| 11 | #include <cstddef> | ||
| 12 | #include <cstdint> | ||
| 13 | #include <string> | ||
| 14 | |||
| 15 | namespace terraingen { | ||
| 16 | |||
| 17 | /// Ring frequency of edge lobes about 45 degrees wide, for `RingNoise`. | ||
| 18 | inline constexpr double kLobeRing = kRadian / 45.0; | ||
| 19 | |||
| 20 | /// Multiplies the first `bands` values of `value` by `factor`; NaN stays NaN. | ||
| 21 | void ScaleBands(AlbedoSample& value, std::size_t bands, double factor); | ||
| 22 | |||
| 23 | /// Changes the first `bands` values of `value` by the relative reflectance `change`, never below | ||
| 24 | /// 0; NaN stays NaN and a change of 0 leaves them alone. | ||
| 25 | void ChangeBands(AlbedoSample& value, std::size_t bands, double change); | ||
| 26 | |||
| 27 | /// Relative reflectance changes laid over one another, the first on top: each covers its share of | ||
| 28 | /// what the ones before it leave showing. | ||
| 29 | class ChangeOverlay { | ||
| 30 | public: | ||
| 31 | /// Lays down a layer covering `cover` (in [0, 1]) of what shows, changing it by `change` there. | ||
| 32 | 703127 | void Add(double cover, double change) { | |
| 33 | 703128 | m_change += m_open * cover * change; | |
| 34 | 703128 | m_open *= 1.0 - cover; | |
| 35 | 534627 | } | |
| 36 | |||
| 37 | /// The summed relative change. | ||
| 38 | double Change() const { | ||
| 39 | return m_change; | ||
| 40 | } | ||
| 41 | |||
| 42 | private: | ||
| 43 | double m_change = 0.0; | ||
| 44 | double m_open = 1.0; | ||
| 45 | }; | ||
| 46 | |||
| 47 | /// Moves the first `bands` values of `value` from optical maturity `reference` to `maturity` | ||
| 48 | /// (`ReflectanceAtMaturity`), then multiplies them by `factor`; NaN stays NaN. | ||
| 49 | void MatureBands(AlbedoSample& value, std::size_t bands, double reference, double maturity, | ||
| 50 | double originReflectance, double factor = 1.0); | ||
| 51 | |||
| 52 | /// A smoothstep from 0 at `start` to 1 at `start` + `width`. | ||
| 53 | double SmoothRamp(double t, double start, double width); | ||
| 54 | |||
| 55 | /// A smoothstep from 1 at `start` to 0 at `start` + `width`: 1 - `SmoothRamp`. | ||
| 56 | double FadeOut(double t, double start, double width); | ||
| 57 | |||
| 58 | /// An edge at `t` = 1: 1 inside, fading to 0 over the last `feather` of the way out. | ||
| 59 | double SoftEdge(double t, double feather); | ||
| 60 | |||
| 61 | /// Fractal noise of unit wavelength and amplitude with `octaves` octaves at (`ring` cos `angle`, | ||
| 62 | /// `ring` sin `angle`, `z`): noise around a circle, `ring` / (2 pi) features per radian. | ||
| 63 | double RingNoise(double ring, double angle, double z, std::uint32_t seed, int octaves); | ||
| 64 | |||
| 65 | /// The share of its radius a lobed edge keeps at `angle` radians: 1 less `raggedness` times how | ||
| 66 | /// far `RingNoise(ring, angle, z, seed, octaves)` reads below 0.5; 1 without raggedness. | ||
| 67 | double EdgeLobe(double raggedness, double ring, double angle, double z, std::uint32_t seed, | ||
| 68 | int octaves); | ||
| 69 | |||
| 70 | /// Checks a distribution of relative reflectance changes. | ||
| 71 | /// @param what The change, for messages, such as "A dark splotch's change". | ||
| 72 | /// @throws std::invalid_argument for an invalid distribution, or one that can reach -1 or is | ||
| 73 | /// unbounded. | ||
| 74 | void ValidateRelativeChange(const ValueDistribution& change, const std::string& what); | ||
| 75 | |||
| 76 | /// The share of its life left to something `ageGyr` old that lasts `lifetimeMyr`: 1 when new, 0 | ||
| 77 | /// at the end and negative after. | ||
| 78 | double LifeLeft(double ageGyr, double lifetimeMyr); | ||
| 79 | |||
| 80 | } // namespace terraingen | ||
| 81 |