./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
44 / 44
100.0%
Branches:
16 / 18
88.9%
Functions:
5 / 5
100.0%

src/albedo/synthetic.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/albedo/synthetic.h"
4
5 #include "terraingen/noise/surface_noise.h"
6 #include "terraingen/noise/validate.h"
7 #include "util/checks.h"
8
9 #include <algorithm>
10 #include <cmath>
11 #include <stdexcept>
12 #include <utility>
13
14 namespace terraingen {
15
16 148 void ValidateSyntheticAlbedoOptions(const SyntheticAlbedoOptions& options) {
17 148 RequireFinite(options.hueDegrees, "The albedo hue");
18
3/4
✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 145 times.
✓ Branch 3 taken 1 time.
293 if (!(IsInUnitInterval(options.saturation) && IsInUnitInterval(options.value))) {
19
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("The albedo saturation and value must be in [0, 1]");
20 }
21 145 ValidateFractals(options.hueNoiseDegrees);
22 144 ValidateFractals(options.saturationNoise);
23 144 ValidateFractals(options.valueNoise);
24 143 }
25
26 222742 AlbedoSample HsvToRgb(double hue, double saturation, double value) {
27 222742 const double h = std::fmod(std::fmod(hue, 360.0) + 360.0, 360.0) / 60.0;
28 222742 const double sector = std::floor(h);
29 222742 const double f = h - sector;
30 222742 const auto p = static_cast<float>(value * (1.0 - saturation));
31 222742 const auto q = static_cast<float>(value * (1.0 - saturation * f));
32 222742 const auto t = static_cast<float>(value * (1.0 - saturation * (1.0 - f)));
33 222742 const auto v = static_cast<float>(value);
34
6/6
✓ Branch 0 taken 222733 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 3 times.
222742 switch (static_cast<int>(sector)) {
35 222733 case 0:
36 222733 return {v, t, p};
37 2 case 1:
38 2 return {q, v, p};
39 2 case 2:
40 2 return {p, v, t};
41 1 case 3:
42 1 return {p, q, v};
43 1 case 4:
44 1 return {t, p, v};
45 3 default:
46 3 return {v, p, q};
47 }
48 }
49
50 98 SyntheticAlbedo::SyntheticAlbedo(SyntheticAlbedoOptions options) : m_options(std::move(options)) {
51
1/1
✓ Branch 1 taken 93 times.
98 ValidateSyntheticAlbedoOptions(m_options);
52 98 }
53
54 3940071 double SyntheticAlbedo::Noise(const std::vector<Fractal>& fractals, double x, double y,
55 const LayerContext& context) {
56 3940071 return static_cast<double>(
57 3938999 FractalStackAt(fractals, context.ReferencePoint(), context.SurfaceOrPlane(), x, y));
58 }
59
60 3495568 AlbedoSample SyntheticAlbedo::ValueAt(double x, double y, const LayerContext& context) const {
61 3495568 const double value =
62
2/2
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 3519922 times.
3495568 std::clamp(m_options.value * (1.0 + Noise(m_options.valueNoise, x, y, context)), 0.0, 1.0);
63
2/2
✓ Branch 0 taken 3297157 times.
✓ Branch 1 taken 222822 times.
3519979 if (!m_options.rgb) {
64 3297157 return {static_cast<float>(value), 0.0f, 0.0f};
65 }
66 222822 const double hue = m_options.hueDegrees + Noise(m_options.hueNoiseDegrees, x, y, context);
67 445546 const double saturation = std::clamp(
68
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 222766 times.
222780 m_options.saturation + Noise(m_options.saturationNoise, x, y, context), 0.0, 1.0);
69 222766 return HsvToRgb(hue, saturation, value);
70 }
71
72 } // namespace terraingen
73