./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
26 / 26
100.0%
Branches:
13 / 14
92.9%
Functions:
4 / 4
100.0%

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
2/2
✓ Branch 0 taken 2654107 times.
✓ Branch 1 taken 40016505 times.
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
2/2
✓ Branch 0 taken 26245572 times.
✓ Branch 1 taken 486649 times.
26732221 if (x <= start) {
20 return 1.0;
21 }
22
2/2
✓ Branch 0 taken 720686 times.
✓ Branch 1 taken 25524886 times.
26245572 if (x >= end) {
23 return 0.0;
24 }
25 720686 const double t = (x - start) / (end - start);
26
4/5
✓ Branch 0 taken 22686 times.
✓ Branch 1 taken 697916 times.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 42 times.
✗ Branch 4 not taken.
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
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 162864 times.
162876 if (!(fade.endSamples > fade.startSamples)) {
46
1/1
✓ Branch 2 taken 12 times.
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