./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
34 / 34
100.0%
Branches:
8 / 8
100.0%
Functions:
5 / 5
100.0%

src/layers/boulders/boulder_clusters.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "layers/boulders/boulder_clusters.h"
4
5 #include "terraingen/noise/worley.h"
6 #include "terraingen/util/bits.h"
7
8 #include <cmath>
9
10 namespace terraingen {
11
12 1002384 double ClusterBump(double distance, const BoulderClusters& clusters) {
13 1002384 const double r = distance / clusters.radius;
14 1002384 return std::exp(-0.5 * r * r);
15 }
16
17 12 double MeanClusterBump(const BoulderClusters& clusters) {
18 12 return LatticeMean(16.0, [&](double u, double v) {
19 786432 const WorleyDistances d =
20 786432 Worley2(static_cast<float>(u), static_cast<float>(v), kBoulderClusterStream);
21 786432 return ClusterBump(static_cast<double>(d.f1) * clusters.centerSpacing, clusters);
22 12 });
23 }
24
25 20 double MeanThomasField(const BoulderClusters& clusters, bool body) {
26 20 const double sigma = clusters.radius;
27 20 const double reach = kThomasReachRadii;
28 20 const double perArea = 1.0 / (clusters.centerSpacing * clusters.centerSpacing);
29
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 17 times.
20 if (body) {
30 3 const double perVolume = perArea / (std::sqrt(2.0 * kPi) * sigma);
31 3 const double inside = std::erf(reach / std::sqrt(2.0)) -
32 std::sqrt(2.0 / kPi) * reach * std::exp(-0.5 * reach * reach);
33 3 return perVolume * std::pow(2.0 * kPi * sigma * sigma, 1.5) * inside;
34 }
35 17 return perArea * 2.0 * kPi * sigma * sigma * (1.0 - std::exp(-0.5 * reach * reach));
36 }
37
38 749 double MaxClusterAbundance(const BoulderClusters& clusters) {
39
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 730 times.
749 if (clusters.share <= 0.0) {
40 return 1.0;
41 }
42
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 11 times.
19 if (clusters.model == BoulderClusterModel::Nearest) {
43 8 return 1.0 - clusters.share + clusters.share / MeanClusterBump(clusters);
44 }
45 11 constexpr double kChance = 1e-6;
46 11 const double ratio = clusters.radius / clusters.centerSpacing;
47 11 const double mean = 9.0 * kPi * ratio * ratio;
48 11 double term = std::exp(-mean);
49 11 double above = 1.0 - term;
50 11 int crowd = 0;
51
2/2
✓ Branch 0 taken 323 times.
✓ Branch 1 taken 11 times.
334 while (above >= kChance) {
52 323 ++crowd;
53 323 term *= mean / crowd;
54 323 above -= term;
55 }
56 11 return 1.0 - clusters.share +
57 11 clusters.share * std::fmax(1.0, crowd) / MeanThomasField(clusters);
58 }
59
60 } // namespace terraingen
61