layers/fade.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "terraingen/layers/fade.h" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <stdexcept> | ||
| 6 | |||
| 7 | namespace terraingen { | ||
| 8 | |||
| 9 | 116031006 | double Smoothstep(double t) { | |
| 10 | 116031006 | t = std::clamp(t, 0.0, 1.0); | |
| 11 | 116031006 | return t * t * (3.0 - 2.0 * t); | |
| 12 | } | ||
| 13 | |||
| 14 | 62635004 | double Falloff(double x, double start, double end, BlendShape shape) { | |
| 15 | 62635004 | if (x <= start) { | |
| 16 | 7610361 | return 1.0; | |
| 17 | } | ||
| 18 | 55024643 | if (x >= end) { | |
| 19 | 48259152 | return 0.0; | |
| 20 | } | ||
| 21 | 6765491 | const double t = (x - start) / (end - start); | |
| 22 | 6765491 | switch (shape) { | |
| 23 | 23241 | case BlendShape::Linear: | |
| 24 | 23241 | return 1.0 - t; | |
| 25 | 6675221 | case BlendShape::Smoothstep: | |
| 26 | 6675221 | return 1.0 - Smoothstep(t); | |
| 27 | 61587 | case BlendShape::Smootherstep: | |
| 28 | 61587 | return 1.0 - t * t * t * (t * (t * 6.0 - 15.0) + 10.0); | |
| 29 | 5442 | case BlendShape::Gaussian: { | |
| 30 | 5442 | constexpr double kSigmas = 3.0; | |
| 31 | 5442 | const double tail = std::exp(-0.5 * kSigmas * kSigmas); | |
| 32 | 5442 | const double z = kSigmas * t; | |
| 33 | 5442 | return (std::exp(-0.5 * z * z) - tail) / (1.0 - tail); | |
| 34 | } | ||
| 35 | } | ||
| 36 | ✗ | return 0.0; | |
| 37 | } | ||
| 38 | |||
| 39 | 247784 | void ValidateResolutionFade(const ResolutionFade& fade) { | |
| 40 | 495567 | if (!(std::isfinite(fade.startSamples) && std::isfinite(fade.endSamples) && | |
| 41 | 247783 | fade.startSamples >= 0.0 && fade.endSamples > fade.startSamples)) { | |
| 42 | 17 | throw std::invalid_argument("Resolution fade must satisfy 0 <= startSamples < endSamples"); | |
| 43 | } | ||
| 44 | 247767 | } | |
| 45 | |||
| 46 | 61983353 | double ResolutionWeight(double diameter, double spacing, const ResolutionFade& fade) { | |
| 47 | 61983353 | return 1.0 - Falloff(diameter / spacing, fade.startSamples, fade.endSamples, fade.shape); | |
| 48 | } | ||
| 49 | |||
| 50 | } // namespace terraingen | ||
| 51 |