src/layers/fade.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/layers/fade.h" | ||
| 4 | |||
| 5 | #include "terraingen/layers/layer_fields.h" | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <cmath> | ||
| 9 | #include <stdexcept> | ||
| 10 | |||
| 11 | namespace terraingen { | ||
| 12 | |||
| 13 | 42670612 | double Smoothstep(double t) { | |
| 14 | 42670612 | t = std::clamp(t, 0.0, 1.0); | |
| 15 | 42670612 | return t * t * (3.0 - 2.0 * t); | |
| 16 | } | ||
| 17 | |||
| 18 | 26732221 | double Falloff(double x, double start, double end, BlendShape shape) { | |
| 19 | 26732221 | if (x <= start) { | |
| 20 | return 1.0; | ||
| 21 | } | ||
| 22 | 26245572 | if (x >= end) { | |
| 23 | return 0.0; | ||
| 24 | } | ||
| 25 | 720686 | const double t = (x - start) / (end - start); | |
| 26 | 720686 | switch (shape) { | |
| 27 | 22686 | case BlendShape::Linear: | |
| 28 | 22686 | return 1.0 - t; | |
| 29 | 697916 | case BlendShape::Smoothstep: | |
| 30 | 697916 | return 1.0 - Smoothstep(t); | |
| 31 | 42 | case BlendShape::Smootherstep: | |
| 32 | 42 | return 1.0 - t * t * t * (t * (t * 6.0 - 15.0) + 10.0); | |
| 33 | 42 | case BlendShape::Gaussian: { | |
| 34 | 42 | constexpr double kSigmas = 3.0; | |
| 35 | 42 | const double tail = std::exp(-0.5 * kSigmas * kSigmas); | |
| 36 | 42 | const double z = kSigmas * t; | |
| 37 | 42 | return (std::exp(-0.5 * z * z) - tail) / (1.0 - tail); | |
| 38 | } | ||
| 39 | } | ||
| 40 | return 0.0; | ||
| 41 | } | ||
| 42 | |||
| 43 | 162793 | void ValidateResolutionFade(const ResolutionFade& fade) { | |
| 44 | 162793 | CheckRanges(fade); | |
| 45 | 162876 | if (!(fade.endSamples > fade.startSamples)) { | |
| 46 | 12 | throw std::invalid_argument("Resolution fade must satisfy startSamples < endSamples"); | |
| 47 | } | ||
| 48 | 162864 | } | |
| 49 | |||
| 50 | 26695529 | double ResolutionWeight(double diameter, double spacing, const ResolutionFade& fade) { | |
| 51 | 26695529 | return 1.0 - Falloff(diameter / spacing, fade.startSamples, fade.endSamples, fade.shape); | |
| 52 | } | ||
| 53 | |||
| 54 | } // namespace terraingen | ||
| 55 |