layers/fade.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /// @file | ||
| 2 | /// @brief Falloff curves, and the fade of features too small to resolve at a sample spacing. | ||
| 3 | #pragma once | ||
| 4 | |||
| 5 | namespace terraingen { | ||
| 6 | |||
| 7 | /// Shape of a falloff from 1 to 0. | ||
| 8 | enum class BlendShape { | ||
| 9 | Linear, ///< Straight ramp; continuous, with kinks at both ends. | ||
| 10 | Smoothstep, ///< Cubic 3t^2 - 2t^3; continuous first derivative. | ||
| 11 | Smootherstep, ///< Quintic 6t^5 - 15t^4 + 10t^3; continuous first and second derivatives. | ||
| 12 | Gaussian, ///< Gaussian falloff with sigma a third of the band, rescaled to end at zero. | ||
| 13 | }; | ||
| 14 | |||
| 15 | /// The cubic smoothstep 3t^2 - 2t^3, with `t` clamped to [0, 1]. | ||
| 16 | double Smoothstep(double t); | ||
| 17 | |||
| 18 | /// The weight in [0, 1] of a falloff from 1 at or before `start` to 0 at or beyond `end`. | ||
| 19 | /// @param end Above `start`. | ||
| 20 | double Falloff(double x, double start, double end, BlendShape shape); | ||
| 21 | |||
| 22 | /// How features too small to resolve at the sample spacing fade out, by the samples across | ||
| 23 | /// their diameter. | ||
| 24 | struct ResolutionFade { | ||
| 25 | 90 | double startSamples = 2.0; ///< Samples across at or below which the feature is absent. | |
| 26 | 90 | double endSamples = 4.0; ///< Samples across from which it is whole; above `startSamples`. | |
| 27 | 90 | BlendShape shape = BlendShape::Smoothstep; ///< The curve in between. | |
| 28 | |||
| 29 | 90 | bool operator==(const ResolutionFade&) const = default; | |
| 30 | }; | ||
| 31 | |||
| 32 | /// Checks a resolution fade. | ||
| 33 | /// @throws std::invalid_argument unless 0 <= `startSamples` < `endSamples`, both finite. | ||
| 34 | void ValidateResolutionFade(const ResolutionFade& fade); | ||
| 35 | |||
| 36 | /// The weight of a feature at a sample spacing: 0 up to `fade.startSamples` samples across it, 1 | ||
| 37 | /// from `fade.endSamples`, and `fade.shape` in between. | ||
| 38 | /// @param diameter The feature's size, in the units of `spacing`. | ||
| 39 | /// @param spacing Positive. | ||
| 40 | double ResolutionWeight(double diameter, double spacing, const ResolutionFade& fade); | ||
| 41 | |||
| 42 | } // namespace terraingen | ||
| 43 |