./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
65 / 65
100.0%
Branches:
43 / 44
97.7%
Functions:
11 / 11, 1 excl
100.0%

src/layers/craters/generation/spacing.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/craters/generation/spacing.h"
4
5 #include "terraingen/layers/craters/band_buckets.h"
6 #include "terraingen/layers/craters/crater_fields.h"
7
8 #include <cmath>
9
10 namespace terraingen {
11
12 namespace {
13
14 /// Finds whether crater `n` conflicts with a crater that takes precedence over it, through
15 /// buckets per diameter band at least as wide as any distance at which a crater can conflict.
16 86 class ConflictSearch {
17 public:
18 43 ConflictSearch(const CraterList& craters, const CraterSpacing& spacing, const Surface& surface)
19 43 : m_craters(craters), m_spacing(spacing), m_surface(surface), m_buckets(surface) {
20
2/2
✓ Branch 0 taken 29894 times.
✓ Branch 1 taken 43 times.
29937 for (std::size_t n = 0; n < craters.Size(); ++n) {
21
1/1
✓ Branch 1 taken 29894 times.
29894 const Crater crater = craters.At(n);
22 29894 const int band = std::ilogb(crater.diameter);
23 29894 m_buckets.Add(band,
24 29894 spacing.separation * spacing.diameterRatio * std::ldexp(1.0, band + 1),
25
1/1
✓ Branch 1 taken 29894 times.
29894 {crater.x, crater.y}, n);
26 }
27 43 }
28
29 29872 bool Outranked(std::size_t n) const {
30 29872 const Crater crater = m_craters.At(n);
31 29872 const int lowest = std::ilogb(crater.diameter / m_spacing.diameterRatio);
32 29872 const int highest = std::ilogb(crater.diameter * m_spacing.diameterRatio);
33 29872 bool outranked = false;
34
4/4
✓ Branch 0 taken 78963 times.
✓ Branch 1 taken 22054 times.
✓ Branch 2 taken 71145 times.
✓ Branch 3 taken 7818 times.
101017 for (int band = lowest; band <= highest && !outranked; ++band) {
35 142290 m_buckets.ForEachAround(band, {crater.x, crater.y}, 1,
36 71145 [&](const std::vector<std::size_t>& others) {
37
2/2
✓ Branch 0 taken 273199 times.
✓ Branch 1 taken 33178 times.
306377 outranked = outranked || OutrankedBy(others, n, crater);
38 306377 });
39 }
40 29872 return outranked;
41 }
42
43 private:
44 /// Whether one of `others` conflicts with `crater` and takes precedence over it.
45 273199 bool OutrankedBy(const std::vector<std::size_t>& others, std::size_t n,
46 const Crater& crater) const {
47
2/2
✓ Branch 0 taken 578145 times.
✓ Branch 1 taken 262507 times.
840652 for (const std::size_t other : others) {
48
2/2
✓ Branch 0 taken 24429 times.
✓ Branch 1 taken 553716 times.
578145 if (other == n) {
49 24429 continue;
50 }
51 553716 const Crater neighbor = m_craters.At(other);
52
2/2
✓ Branch 1 taken 24585 times.
✓ Branch 2 taken 529131 times.
553716 if (CratersConflict(crater, neighbor, m_spacing, m_surface) &&
53
2/2
✓ Branch 0 taken 10692 times.
✓ Branch 1 taken 13893 times.
24585 CraterTakesPrecedence(neighbor, crater, m_spacing.outrankFrom)) {
54 10692 return true;
55 }
56 }
57 return false;
58 }
59
60 const CraterList& m_craters;
61 const CraterSpacing& m_spacing;
62 const Surface& m_surface;
63 BandBuckets m_buckets;
64 };
65
66 } // namespace
67
68 266036 void ValidateCraterSpacing(const CraterSpacing& spacing) {
69 266036 CheckRanges(spacing);
70 265969 }
71
72 1174178 bool CratersConflict(const Crater& a, const Crater& b, const CraterSpacing& spacing,
73 const Surface& surface) {
74 1174178 const double larger = std::fmax(a.diameter, b.diameter);
75 1174178 const double smaller = std::fmin(a.diameter, b.diameter);
76
2/2
✓ Branch 0 taken 692346 times.
✓ Branch 1 taken 481832 times.
1174178 if (larger > spacing.diameterRatio * smaller) {
77 return false;
78 }
79 692346 const double limit = spacing.separation * larger;
80
2/2
✓ Branch 0 taken 531 times.
✓ Branch 1 taken 691815 times.
692346 if (surface.IsBody()) {
81 531 return surface.Distance({a.x, a.y}, {b.x, b.y}) < limit;
82 }
83 691815 const double dx = a.x - b.x;
84 691815 const double dy = a.y - b.y;
85 691815 return dx * dx + dy * dy < limit * limit;
86 }
87
88 25018 bool CraterTakesPrecedence(const Crater& a, const Crater& b, double outrankFrom) {
89
2/2
✓ Branch 0 taken 25011 times.
✓ Branch 1 taken 7 times.
25018 if (a.manual != b.manual) {
90 return a.manual;
91 }
92
2/2
✓ Branch 0 taken 24561 times.
✓ Branch 1 taken 450 times.
25011 if ((a.diameter >= outrankFrom) != (b.diameter >= outrankFrom)) {
93 return a.diameter >= outrankFrom;
94 }
95
2/2
✓ Branch 0 taken 20011 times.
✓ Branch 1 taken 4550 times.
24561 if (a.ageGyr != b.ageGyr) {
96 20011 return a.ageGyr < b.ageGyr;
97 }
98
2/2
✓ Branch 0 taken 4545 times.
✓ Branch 1 taken 5 times.
4550 if (a.diameter != b.diameter) {
99 4545 return a.diameter > b.diameter;
100 }
101 5 return a.id < b.id;
102 }
103
104 4730 double SpacingReach(double diameter, double maxDiameter, const CraterSpacing& spacing) {
105 4730 return spacing.separation * std::fmin(spacing.diameterRatio * diameter, maxDiameter);
106 }
107
108 48 std::vector<bool> SpacingConflicts(const CraterList& craters, const CraterSpacing& spacing,
109 const Surface& surface) {
110 48 ValidateCraterSpacing(spacing);
111
2/2
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 2 times.
45 std::vector<bool> conflicts(craters.Size(), false);
112
3/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
45 if (!spacing.Enabled() || craters.Size() < 2) {
113 return conflicts;
114 }
115
1/1
✓ Branch 1 taken 43 times.
43 const ConflictSearch search(craters, spacing, surface);
116
2/2
✓ Branch 0 taken 29894 times.
✓ Branch 1 taken 43 times.
29937 for (std::size_t n = 0; n < craters.Size(); ++n) {
117
4/4
✓ Branch 1 taken 29894 times.
✓ Branch 3 taken 29872 times.
✓ Branch 4 taken 22 times.
✓ Branch 6 taken 29872 times.
29894 conflicts[n] = !craters.At(n).manual && search.Outranked(n);
118 }
119 43 return conflicts;
120 43 }
121
122 4 CraterList ThinBySpacing(const CraterList& craters, const CraterSpacing& spacing,
123 const Surface& surface) {
124 4 const std::vector<bool> conflicts = SpacingConflicts(craters, spacing, surface);
125
1/1
✓ Branch 1 taken 4 times.
4 return FilterCraters(craters, [&](std::size_t n, const Crater&) { return !conflicts[n]; });
126 4 }
127
128 } // namespace terraingen
129