TerrainGen coverage


terraingen/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
50 / 50
100.0%
Branches:
29 / 29
100.0%
Functions:
6 / 6
100.0%

albedo/synthetic.cpp
Line Branch Exec Source
1 #include "terraingen/albedo/synthetic.h"
2
3 #include "terraingen/layers/noise_relief.h"
4 #include "terraingen/sources/synthetic_terrain.h"
5 #include "terraingen/util/checks.h"
6
7 #include <algorithm>
8 #include <cmath>
9 #include <stdexcept>
10 #include <utility>
11
12 namespace terraingen {
13
14 namespace {
15
16 /// Checks one noise stack's size and layers.
17 436 void ValidateStack(const std::vector<NoiseLayer>& layers) {
18
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 435 times.
436 if (layers.size() > kMaxNoiseLayers) {
19
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Too many albedo noise layers");
20 }
21
2/2
✓ Branch 5 taken 78 times.
✓ Branch 6 taken 434 times.
512 for (const NoiseLayer& layer : layers) {
22
1/1
✓ Branch 1 taken 77 times.
78 ValidateNoiseLayer(layer);
23 }
24 434 }
25
26 } // namespace
27
28 151 void ValidateSyntheticAlbedo(const SyntheticAlbedoParams& params) {
29
1/1
✓ Branch 2 taken 150 times.
151 RequireFinite(params.hue, "The albedo hue");
30
6/6
✓ Branch 1 taken 149 times.
✓ Branch 2 taken 1 time.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 146 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 146 times.
150 if (!(IsInUnitInterval(params.saturation) && IsInUnitInterval(params.value))) {
31
1/1
✓ Branch 2 taken 4 times.
4 throw std::invalid_argument("The albedo saturation and value must be in [0, 1]");
32 }
33 146 ValidateStack(params.hueNoise);
34 145 ValidateStack(params.saturationNoise);
35 145 ValidateStack(params.valueNoise);
36 144 }
37
38 385835 AlbedoSample HsvToRgb(double hue, double saturation, double value) {
39 385835 const double h = std::fmod(std::fmod(hue, 360.0) + 360.0, 360.0) / 60.0;
40 385835 const double sector = std::floor(h);
41 385835 const double f = h - sector;
42 385835 const auto p = static_cast<float>(value * (1.0 - saturation));
43 385835 const auto q = static_cast<float>(value * (1.0 - saturation * f));
44 385835 const auto t = static_cast<float>(value * (1.0 - saturation * (1.0 - f)));
45 385835 const auto v = static_cast<float>(value);
46
6/6
✓ Branch 0 taken 385828 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 2 times.
385835 switch (static_cast<int>(sector)) {
47 385828 case 0:
48 385828 return {v, t, p};
49 2 case 1:
50 2 return {q, v, p};
51 1 case 2:
52 1 return {p, v, t};
53 1 case 3:
54 1 return {p, q, v};
55 1 case 4:
56 1 return {t, p, v};
57 2 default:
58 2 return {v, p, q};
59 }
60 }
61
62 84 SyntheticAlbedo::SyntheticAlbedo(SyntheticAlbedoParams params, Vec2d origin, Surface surface)
63 84 : params_(std::move(params)), origin_(origin), surface_(surface) {
64
1/1
✓ Branch 1 taken 79 times.
84 ValidateSyntheticAlbedo(params_);
65 94 }
66
67 9147379 double SyntheticAlbedo::Noise(const std::vector<NoiseLayer>& layers, double x, double y) const {
68
2/2
✓ Branch 1 taken 3686499 times.
✓ Branch 2 taken 5460880 times.
9147379 if (layers.empty()) {
69 3686499 return 0.0;
70 }
71 5460880 return static_cast<double>(NoiseLayersAt(layers, origin_, surface_, x, y));
72 }
73
74 8375727 AlbedoSample SyntheticAlbedo::ValueAt(double x, double y, const AlbedoContext& /*context*/) const {
75 const double value =
76
2/2
✓ Branch 1 taken 8375727 times.
✓ Branch 4 taken 8375727 times.
8375727 std::clamp(params_.value * (1.0 + Noise(params_.valueNoise, x, y)), 0.0, 1.0);
77
2/2
✓ Branch 0 taken 7989901 times.
✓ Branch 1 taken 385826 times.
8375727 if (!params_.rgb) {
78 7989901 return {static_cast<float>(value), 0.0f, 0.0f};
79 }
80 385826 const double hue = params_.hue + Noise(params_.hueNoise, x, y);
81 const double saturation =
82
2/2
✓ Branch 1 taken 385826 times.
✓ Branch 4 taken 385826 times.
385826 std::clamp(params_.saturation + Noise(params_.saturationNoise, x, y), 0.0, 1.0);
83 385826 return HsvToRgb(hue, saturation, value);
84 }
85
86 } // namespace terraingen
87