src/albedo/bands.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "albedo/bands.h" | ||
| 4 | |||
| 5 | #include "terraingen/albedo/maturity.h" | ||
| 6 | #include "terraingen/layers/fade.h" | ||
| 7 | #include "terraingen/noise/fractal.h" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <cmath> | ||
| 11 | #include <stdexcept> | ||
| 12 | |||
| 13 | namespace terraingen { | ||
| 14 | |||
| 15 | 1160426 | void ScaleBands(AlbedoSample& value, std::size_t bands, double factor) { | |
| 16 | 2321104 | for (std::size_t band = 0; band < bands; ++band) { | |
| 17 | 1160678 | if (!std::isnan(value[band])) { | |
| 18 | 1160677 | value[band] = static_cast<float>(factor * static_cast<double>(value[band])); | |
| 19 | } | ||
| 20 | } | ||
| 21 | 1160426 | } | |
| 22 | |||
| 23 | 1017759 | void ChangeBands(AlbedoSample& value, std::size_t bands, double change) { | |
| 24 | 1017759 | if (change != 0.0) { | |
| 25 | 635823 | ScaleBands(value, bands, std::fmax(0.0, 1.0 + change)); | |
| 26 | } | ||
| 27 | 1018869 | } | |
| 28 | |||
| 29 | 159430 | void MatureBands(AlbedoSample& value, std::size_t bands, double reference, double maturity, | |
| 30 | double originReflectance, double factor) { | ||
| 31 | 350353 | for (std::size_t band = 0; band < bands; ++band) { | |
| 32 | 190981 | if (!std::isnan(value[band])) { | |
| 33 | 381900 | value[band] = static_cast<float>( | |
| 34 | 190979 | factor * ReflectanceAtMaturity(static_cast<double>(value[band]), reference, | |
| 35 | maturity, originReflectance)); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | 159372 | } | |
| 39 | |||
| 40 | 1855925 | double SmoothRamp(double t, double start, double width) { | |
| 41 | 1855925 | return Smoothstep((t - start) / width); | |
| 42 | } | ||
| 43 | |||
| 44 | 1838774 | double FadeOut(double t, double start, double width) { | |
| 45 | 1838774 | return 1.0 - SmoothRamp(t, start, width); | |
| 46 | } | ||
| 47 | |||
| 48 | 1229361 | double SoftEdge(double t, double feather) { | |
| 49 | 1229361 | return FadeOut(t, 1.0 - feather, feather); | |
| 50 | } | ||
| 51 | |||
| 52 | 1463884 | double RingNoise(double ring, double angle, double z, std::uint32_t seed, int octaves) { | |
| 53 | 1463884 | FractalParams params; | |
| 54 | 1463884 | params.seed = seed; | |
| 55 | 1463884 | params.wavelength = 1.0f; | |
| 56 | 1463884 | params.amplitude = 1.0f; | |
| 57 | 1463884 | params.octaves = octaves; | |
| 58 | 1463884 | return static_cast<double>(Fractal3(ring * std::cos(angle), ring * std::sin(angle), z, params)); | |
| 59 | } | ||
| 60 | |||
| 61 | 1499445 | double EdgeLobe(double raggedness, double ring, double angle, double z, std::uint32_t seed, | |
| 62 | int octaves) { | ||
| 63 | 1499445 | if (!(raggedness > 0.0)) { | |
| 64 | return 1.0; | ||
| 65 | } | ||
| 66 | 2914624 | return 1.0 - raggedness * std::clamp(0.5 - RingNoise(ring, angle, z, seed, octaves), 0.0, 1.0); | |
| 67 | } | ||
| 68 | |||
| 69 | 1829 | void ValidateRelativeChange(const ValueDistribution& change, const std::string& what) { | |
| 70 | 1829 | ValidateValueDistribution(change); | |
| 71 | 1825 | if (!(change.Lowest() > -1.0 && std::isfinite(change.Highest()))) { | |
| 72 | 12 | throw std::invalid_argument(what + " must be above -1 and bounded"); | |
| 73 | } | ||
| 74 | 1819 | } | |
| 75 | |||
| 76 | 706152 | double LifeLeft(double ageGyr, double lifetimeMyr) { | |
| 77 | 706152 | return 1.0 - 1000.0 * ageGyr / lifetimeMyr; | |
| 78 | } | ||
| 79 | |||
| 80 | } // namespace terraingen | ||
| 81 |