./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
298 / 315
94.6%
Branches:
204 / 229
89.1%
Functions:
32 / 32
100.0%

src/layers/boulders/boulder_relief.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/boulders/boulder_layer.h"
4
5 #include "terraingen/raster/cell_loops.h"
6 #include "terraingen/util/bits.h"
7 #include "terraingen/util/parallel.h"
8 #include "util/numerics.h"
9
10 #include <algorithm>
11 #include <array>
12 #include <cmath>
13 #include <cstddef>
14 #include <limits>
15 #include <utility>
16 #include <vector>
17
18 namespace terraingen {
19
20 namespace {
21
22 /// The box the relief of a boulder at (`x`, `y`) reaching `reach` meters is drawn over: on a
23 /// sphere a square in degrees around the box `Surface::Expand` gives.
24 25334 Bounds2d ReliefBox(double x, double y, double reach, const Surface& surface) {
25
2/2
✓ Branch 0 taken 18372 times.
✓ Branch 1 taken 6962 times.
25334 if (!surface.IsBody()) {
26 18372 return {x - reach, y - reach, x + reach, y + reach};
27 }
28 6962 const Bounds2d box = surface.Expand({x, y, x, y}, reach);
29 6962 const double cx = 0.5 * (box.minX + box.maxX);
30 6962 const double cy = 0.5 * (box.minY + box.maxY);
31 6962 const double half = std::fmax(0.5 * (box.maxX - box.minX), 0.5 * (box.maxY - box.minY));
32 6962 return {cx - half, cy - half, cx + half, cy + half};
33 }
34
35 /// How far a placed shape reaches from its boulder's center `cx`, `cy` along either axis.
36 8347 double ShapeReach(const TriangleMesh& placed, double cx, double cy) {
37 8347 const Box3d box = Bounds(placed);
38 8347 return std::fmax(std::fmax(std::fabs(box.min.x - cx), std::fabs(box.max.x - cx)),
39 8347 std::fmax(std::fabs(box.min.y - cy), std::fabs(box.max.y - cy)));
40 }
41
42 /// The height (m) of a boulder's dome `distance` meters from its center, averaged over a band
43 /// `width` meters wide so its rim slopes over a sample; 0 `width` samples it at the point.
44 355408 double DomeHeight(const Boulder& boulder, double distance, double width) {
45 355408 const double radius = 0.5 * boulder.diameter;
46
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 355408 times.
355408 if (!(width > 0.0)) {
47 ✗ const double r2 = (distance * distance) / (radius * radius);
48
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return r2 < 1.0 ? boulder.ExposedHeight() * std::sqrt(1.0 - r2) : 0.0;
49 }
50 774526 const auto area = [](double u) { return 0.5 * (u * std::sqrt(1.0 - u * u) + std::asin(u)); };
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 355408 times.
355408 const double low = std::clamp((distance - 0.5 * width) / radius, -1.0, 1.0);
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 355408 times.
355408 const double high = std::clamp((distance + 0.5 * width) / radius, -1.0, 1.0);
53
2/2
✓ Branch 0 taken 210625 times.
✓ Branch 1 taken 144783 times.
355408 if (!(high > low)) {
54 return 0.0;
55 }
56 210625 return boulder.ExposedHeight() * (area(high) - area(low)) * radius / width;
57 }
58
59 /// Where a fillet `top` meters high meets a round dome averaged over `width` meters
60 /// (`DomeHeight`), the same in every direction and no farther out than its radius.
61 646 FilletContact RoundDomeContact(const Boulder& boulder, double width, double top) {
62 646 const double radius = 0.5 * boulder.diameter;
63 646 const double height = std::fmin(top, DomeHeight(boulder, 0.0, width));
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 646 times.
646 if (!(height > 0.0)) {
65 ✗ return {};
66 }
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 646 times.
646 if (!(width > 0.0)) {
68 ✗ const double share = std::fmin(height / boulder.ExposedHeight(), 1.0);
69 ✗ return {radius * std::sqrt(1.0 - share * share), height};
70 }
71
2/2
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 625 times.
646 if (DomeHeight(boulder, radius, width) >= height) {
72 21 return {radius, height};
73 }
74 625 const BisectionInterval contact = Bisect(0.0, radius, 50, 0.0, [&](double distance) {
75 31250 return !(DomeHeight(boulder, distance, width) >= height);
76 });
77 625 return {contact.low, height};
78 }
79
80 /// Where a fillet `top` meters high meets a dome averaged over `width` meters and cut by the
81 /// boulder's facets along the unit `direction` from its center: the outermost point within its
82 /// radius where the rock stands that high, or as high as it stands along `direction`.
83 642 FilletContact FacetedDomeContact(const Boulder& boulder, const Vec2d& direction, double width,
84 double top) {
85 642 const double outer = 0.5 * boulder.diameter;
86 68052 const auto rock = [&](double distance) {
87 67410 return boulder.CutByFacets({distance * direction.x, distance * direction.y},
88 67410 DomeHeight(boulder, distance, width));
89 642 };
90 642 constexpr int kSteps = 64;
91 642 std::array<double, kSteps + 1> heights{};
92 642 double highest = 0.0;
93
2/2
✓ Branch 0 taken 41730 times.
✓ Branch 1 taken 642 times.
42372 for (std::size_t step = 0; step < heights.size(); ++step) {
94 41730 heights[step] = rock(outer * static_cast<double>(step) / kSteps);
95 41730 highest = std::fmax(highest, heights[step]);
96 }
97 642 const double height = std::fmin(top, highest);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 642 times.
642 if (!(height > 0.0)) {
99 ✗ return {};
100 }
101 int last = 0;
102
2/2
✓ Branch 0 taken 41730 times.
✓ Branch 1 taken 642 times.
42372 for (std::size_t step = 0; step < heights.size(); ++step) {
103
2/2
✓ Branch 0 taken 13281 times.
✓ Branch 1 taken 28449 times.
41730 if (heights[step] >= height) {
104 13281 last = static_cast<int>(step);
105 }
106 }
107 642 const BisectionInterval contact =
108 642 Bisect(outer * last / kSteps, outer * std::min(last + 1, kSteps) / kSteps, 40, 0.0,
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 642 times.
26322 [&](double distance) { return !(rock(distance) >= height); });
110 642 return {contact.low, height};
111 }
112
113 /// How many directions a shape's fillet contacts are found in for a boulder of `diameter` at
114 /// `spacing`: about two per sample around its rim.
115 5640 std::size_t FilletDirections(double diameter, double spacing) {
116 5640 const double around = kPi * diameter / std::fmax(spacing, 1e-9);
117
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5633 times.
5640 return static_cast<std::size_t>(std::clamp(std::ceil(2.0 * around), 16.0, 256.0));
118 }
119
120 /// Where a fillet `top` meters high meets the top surface of `placed` (a shape placed on level
121 /// ground at 0, centered on `center`) in `count` directions at even steps of azimuth from east.
122 5640 std::vector<FilletContact> ShapeContacts(const TriangleMesh& placed, const Vec2d& center,
123 double top, std::size_t count) {
124 5640 struct Piece {
125 double inner;
126 double innerHeight;
127 double outer;
128 double outerHeight;
129 };
130 5640 std::vector<FilletContact> contacts(count);
131 5640 std::vector<Piece> pieces;
132
2/2
✓ Branch 0 taken 1441685 times.
✓ Branch 1 taken 5640 times.
1447325 for (std::size_t k = 0; k < count; ++k) {
133 1441685 const double azimuth = 2.0 * kPi * static_cast<double>(k) / static_cast<double>(count);
134 1441685 const Vec2d along{std::cos(azimuth), std::sin(azimuth)};
135 1441685 pieces.clear();
136 1441685 double highest = 0.0;
137
2/2
✓ Branch 0 taken 1845119232 times.
✓ Branch 1 taken 1441685 times.
1846560917 for (const auto& triangle : placed.triangles) {
138 double a[3];
139 double s[3];
140 double z[3];
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1845119232 times.
✗ for (int j = 0; j < 3; ++j) {
142 ✗ const Vec3d& p = placed.positions[triangle[j]];
143 ✗ a[j] = (p.x - center.x) * along.x + (p.y - center.y) * along.y;
144 ✗ s[j] = (p.y - center.y) * along.x - (p.x - center.x) * along.y;
145 ✗ z[j] = p.z;
146 }
147 1845119232 double lowA = std::numeric_limits<double>::infinity();
148 1845119232 double highA = -lowA;
149 1845119232 double lowZ = 0.0;
150 1845119232 double highZ = 0.0;
151 2109239092 const auto add = [&](double at, double height) {
152
2/2
✓ Branch 0 taken 200863016 times.
✓ Branch 1 taken 63256844 times.
264119860 if (at < lowA) {
153 200863016 lowA = at;
154 200863016 lowZ = height;
155 }
156
2/2
✓ Branch 0 taken 200874274 times.
✓ Branch 1 taken 63245586 times.
264119860 if (at > highA) {
157 200874274 highA = at;
158 200874274 highZ = height;
159 }
160 2109239092 };
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1845119232 times.
✗ for (int j = 0; j < 3; ++j) {
162 ✗ const int n = (j + 1) % 3;
163
1/2
✓ Branch 0 taken 18270000 times.
✗ Branch 1 not taken.
✗ if (s[j] == 0.0) {
164 18270000 add(a[j], z[j]);
165 }
166
8/8
✓ Branch 0 taken 2758543848 times.
✓ Branch 1 taken 2776813848 times.
✓ Branch 2 taken 2635618918 times.
✓ Branch 3 taken 122924930 times.
✓ Branch 4 taken 2758543848 times.
✓ Branch 5 taken 2653888918 times.
✓ Branch 6 taken 122924930 times.
✓ Branch 7 taken 2635618918 times.
✗ if ((s[j] < 0.0 && s[n] > 0.0) || (s[j] > 0.0 && s[n] < 0.0)) {
167 245849860 const double t = s[j] / (s[j] - s[n]);
168 245849860 add(a[j] + t * (a[n] - a[j]), z[j] + t * (z[n] - z[j]));
169 }
170 }
171
2/2
✓ Branch 0 taken 1767449457 times.
✓ Branch 1 taken 77669775 times.
1845119232 if (!(highA >= 0.0)) {
172 1767449457 continue;
173 }
174
2/2
✓ Branch 0 taken 2883370 times.
✓ Branch 1 taken 74786405 times.
77669775 if (lowA < 0.0) {
175
1/2
✓ Branch 0 taken 2883370 times.
✗ Branch 1 not taken.
2883370 lowZ = highA > lowA ? lowZ + (highZ - lowZ) * (-lowA) / (highA - lowA) : highZ;
176 2883370 lowA = 0.0;
177 }
178 77669775 highest = std::fmax(highest, std::fmax(lowZ, highZ));
179
1/1
✓ Branch 1 taken 77669775 times.
77669775 pieces.push_back({lowA, lowZ, highA, highZ});
180 }
181 1441685 const double height = std::fmin(top, highest);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1441685 times.
1441685 if (!(height > 0.0)) {
183 ✗ continue;
184 }
185 1441685 double distance = 0.0;
186
2/2
✓ Branch 0 taken 77669775 times.
✓ Branch 1 taken 1441685 times.
79111460 for (const Piece& piece : pieces) {
187
2/2
✓ Branch 0 taken 42585540 times.
✓ Branch 1 taken 35084235 times.
77669775 if (piece.outerHeight >= height) {
188 42585540 distance = std::fmax(distance, piece.outer);
189
2/2
✓ Branch 0 taken 192458 times.
✓ Branch 1 taken 34891777 times.
35084235 } else if (piece.innerHeight >= height) {
190 192458 const double t =
191 192458 (piece.innerHeight - height) / (piece.innerHeight - piece.outerHeight);
192 192458 distance = std::fmax(distance, piece.inner + t * (piece.outer - piece.inner));
193 }
194 }
195 1441685 contacts[k] = {distance, height};
196 }
197 5640 return contacts;
198 5640 }
199
200 /// Where the fillet meets its boulder in the direction of `offset` (m east and north of its
201 /// center): the one contact of a round dome, those of a shape interpolated in azimuth, or found
202 /// on the dome averaged over `width` meters and cut by its facets.
203 207964 FilletContact SampleContact(const Boulder& boulder, const Vec2d& offset,
204 const std::vector<FilletContact>& contacts, double width, double top) {
205
2/2
✓ Branch 0 taken 103772 times.
✓ Branch 1 taken 104192 times.
207964 if (contacts.size() == 1) {
206 103772 return contacts.front();
207 }
208
2/2
✓ Branch 0 taken 103550 times.
✓ Branch 1 taken 642 times.
104192 if (!contacts.empty()) {
209 103550 double turn = std::atan2(offset.y, offset.x) / (2.0 * kPi);
210 103550 turn -= std::floor(turn);
211 103550 const double at = turn * static_cast<double>(contacts.size());
212 103550 const std::size_t k = static_cast<std::size_t>(at) % contacts.size();
213 103550 const FilletContact& a = contacts[k];
214 103550 const FilletContact& b = contacts[(k + 1) % contacts.size()];
215 103550 const double t = at - std::floor(at);
216 103550 return {a.distance + t * (b.distance - a.distance), a.height + t * (b.height - a.height)};
217 }
218 642 const double distance = std::hypot(offset.x, offset.y);
219 642 const Vec2d direction =
220
2/2
✓ Branch 0 taken 638 times.
✓ Branch 1 taken 4 times.
642 distance > 0.0 ? Vec2d{offset.x / distance, offset.y / distance} : Vec2d{1.0, 0.0};
221 642 return FacetedDomeContact(boulder, direction, width, top);
222 }
223
224 } // namespace
225
226 37087 Boulder BoulderLayer::InLocalFrame(const Boulder& boulder) const {
227 37087 Boulder local = boulder;
228
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 36853 times.
37087 if (m_surface.IsBody()) {
229 234 local.x = 0.0;
230 234 local.y = 0.0;
231 }
232 37087 return local;
233 }
234
235 11113 TriangleMesh BoulderLayer::PlacedOnLevel(const Boulder& boulder) const {
236 11113 const auto shape = Shape(boulder);
237 11113 const BoulderPlacement placement =
238
2/2
✓ Branch 1 taken 11113 times.
✓ Branch 4 taken 11113 times.
11113 PlaceBoulderModel(InLocalFrame(boulder), *shape, 0.0, Settles(boulder));
239
2/4
✓ Branch 1 taken 11113 times.
✓ Branch 4 taken 11113 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
11113 TriangleMesh mesh = PlaceMesh(shape->mesh, placement);
240
1/1
✓ Branch 1 taken 11113 times.
11113 CutFacets(mesh, InLocalFrame(boulder), 0.0);
241
1/2
✓ Branch 0 taken 11113 times.
✗ Branch 1 not taken.
11113 return mesh;
242 11113 }
243
244 332573 float BoulderLayer::AddRelief(float height, float base, double x, double y, const Boulder& boulder,
245 const TriangleMesh* placed, double weight, bool rock, double width,
246 const std::vector<FilletContact>& contacts) const {
247 332573 const Vec2d offset = m_surface.Offset({boulder.x, boulder.y}, {x, y});
248
2/2
✓ Branch 0 taken 2963 times.
✓ Branch 1 taken 329341 times.
332304 const double relief = rock ? RockRelief(x, y, boulder, placed, width) : 0.0;
249 335557 double result = static_cast<double>(height);
250
2/2
✓ Branch 0 taken 109785 times.
✓ Branch 1 taken 222809 times.
335557 if (relief > 0.0) {
251 109785 result = std::fmax(result, static_cast<double>(base) + weight * relief);
252 }
253
2/2
✓ Branch 0 taken 207960 times.
✓ Branch 1 taken 127597 times.
335557 if (m_options.fillet.heightShare > 0.0) {
254 207960 const double top = m_options.fillet.heightShare * boulder.ExposedHeight();
255 207960 const double fillet =
256 207872 FilletHeight(boulder, std::hypot(offset.x, offset.y), m_options.fillet,
257 207960 SampleContact(boulder, offset, contacts, width, top));
258
2/2
✓ Branch 0 taken 117298 times.
✓ Branch 1 taken 90613 times.
207911 if (fillet > 0.0) {
259 117298 result = std::fmax(result, static_cast<double>(base) + weight * fillet);
260 }
261 }
262 335508 return static_cast<float>(result);
263 }
264
265 378017 double BoulderLayer::RockRelief(double x, double y, const Boulder& boulder,
266 const TriangleMesh* placed, double width) const {
267 378017 const Vec2d offset = m_surface.Offset({boulder.x, boulder.y}, {x, y});
268
2/2
✓ Branch 0 taken 255116 times.
✓ Branch 1 taken 123223 times.
378339 if (placed == nullptr) {
269 254196 return boulder.CutByFacets(offset,
270 510548 DomeHeight(boulder, std::hypot(offset.x, offset.y), width));
271 }
272 123223 const std::optional<double> top =
273
2/2
✓ Branch 0 taken 5581 times.
✓ Branch 1 taken 117642 times.
123223 m_surface.IsBody() ? TopSurface(*placed, offset.x, offset.y) : TopSurface(*placed, x, y);
274
2/2
✓ Branch 0 taken 26891 times.
✓ Branch 1 taken 96995 times.
123886 return top ? std::fmax(*top, 0.0) : 0.0;
275 }
276
277 49074 double BoulderLayer::RockCover(const Relief& relief, const Vec2d& p) const {
278 49074 const bool rock =
279
6/6
✓ Branch 0 taken 49039 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 1290 times.
✓ Branch 3 taken 47749 times.
✓ Branch 5 taken 26388 times.
✓ Branch 6 taken 22711 times.
50364 relief.rock && RockRelief(p.x, p.y, *relief.boulder, relief.Shape(), relief.width) > 0.0;
280 26388 return rock ? relief.weight : 0.0;
281 }
282
283 3575 double BoulderLayer::ModelCover(const Boulder& boulder, const Vec2d& p, double spacing) const {
284 3575 const Vec2d offset = m_surface.Offset({boulder.x, boulder.y}, p);
285
4/4
✓ Branch 0 taken 776 times.
✓ Branch 1 taken 2799 times.
✓ Branch 2 taken 429 times.
✓ Branch 3 taken 347 times.
3575 const bool under = boulder.model && std::hypot(offset.x, offset.y) < 0.5 * boulder.diameter;
286 429 return under ? ResolutionWeight(boulder.diameter, spacing, m_options.fade) : 0.0;
287 }
288
289 76654 std::vector<Boulder> BoulderLayer::ReliefBoulders(const Bounds2d& region, double spacing,
290 const Ground* ground, unsigned threads) const {
291
2/2
✓ Branch 0 taken 40760 times.
✓ Branch 1 taken 35894 times.
76654 const double rim = m_options.rendering.relief == BoulderRelief::Dome ? 0.5 * spacing : 0.0;
292 76654 return BoulderRecords(m_surface.Expand(region, rim), spacing, ground, threads);
293 }
294
295 22421 double BoulderLayer::MaterialCoverAt(double x, double y, const LayerContext& context) const {
296 22421 const double spacing = context.spacing;
297 22421 const std::vector<Boulder> boulders =
298 22421 ReliefBoulders({x, y, x, y}, spacing, context.ground, context.threads);
299 22421 double cover = 0.0;
300
3/3
✓ Branch 1 taken 22421 times.
✓ Branch 3 taken 1728 times.
✓ Branch 4 taken 22421 times.
24149 for (const Relief& relief : PrepareRelief(boulders, spacing, false)) {
301
1/1
✓ Branch 1 taken 1728 times.
1728 cover = std::fmax(cover, RockCover(relief, {x, y}));
302 22421 }
303
2/2
✓ Branch 0 taken 3188 times.
✓ Branch 1 taken 22421 times.
25609 for (const Boulder& boulder : boulders) {
304
1/1
✓ Branch 1 taken 3188 times.
3188 cover = std::fmax(cover, ModelCover(boulder, {x, y}, spacing));
305 }
306 22421 return cover;
307 22421 }
308
309 9 Raster<double> BoulderLayer::MaterialCoverOver(const RasterGrid& grid,
310 const LayerContext& context) const {
311 9 const double spacing = context.spacing;
312 9 const unsigned threads = context.threads;
313 9 Raster<double> cover(grid);
314 9 const std::vector<Boulder> boulders =
315
2/2
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
9 ReliefBoulders(grid.Bounds(), spacing, context.ground, context.threads);
316
1/1
✓ Branch 1 taken 9 times.
9 std::vector<Relief> reliefs = PrepareRelief(boulders, spacing, false);
317 9 reliefs.erase(std::remove_if(reliefs.begin(), reliefs.end(),
318 [](const Relief& relief) { return !relief.rock; }),
319 9 reliefs.end());
320
1/1
✓ Branch 1 taken 9 times.
9 ForEachFeatureCell(cover, threads, Boxes(reliefs), m_surface,
321
1/1
✓ Branch 1 taken 9 times.
9 [&](double& cell, const FeatureCell& at) {
322 47355 cell = std::fmax(cell, RockCover(reliefs[at.feature], at.center));
323 47318 });
324 9 std::vector<const Boulder*> models;
325 9 std::vector<Bounds2d> boxes;
326
2/2
✓ Branch 0 taken 3995 times.
✓ Branch 1 taken 9 times.
4004 for (const Boulder& boulder : boulders) {
327
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3984 times.
3995 if (boulder.model) {
328
1/1
✓ Branch 1 taken 11 times.
11 models.push_back(&boulder);
329
1/1
✓ Branch 1 taken 11 times.
22 boxes.push_back(ReliefBox(boulder.x, boulder.y, 0.5 * boulder.diameter, m_surface));
330 }
331 }
332
1/1
✓ Branch 1 taken 9 times.
9 ForEachFeatureCell(cover, threads, boxes, m_surface, [&](double& cell, const FeatureCell& at) {
333 387 cell = std::fmax(cell, ModelCover(*models[at.feature], at.center, spacing));
334 387 });
335 9 return cover;
336 9 }
337
338 11115 void BoulderLayer::CutFacets(TriangleMesh& placed, const Boulder& local, double base) const {
339
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 11115 times.
11142 for (std::uint8_t k = 0; k < local.facetCount; ++k) {
340
1/1
✓ Branch 1 taken 27 times.
27 const BoulderFacet& facet = local.facets[k];
341 27 const Vec3d normal{std::cos(facet.azimuth) * std::sin(facet.angle),
342 27 std::sin(facet.azimuth) * std::sin(facet.angle), std::cos(facet.angle)};
343
1/1
✓ Branch 1 taken 27 times.
27 std::vector<double> distances;
344
1/1
✓ Branch 1 taken 27 times.
27 distances.reserve(placed.positions.size());
345
2/2
✓ Branch 0 taken 15862 times.
✓ Branch 1 taken 27 times.
15889 for (const Vec3d& p : placed.positions) {
346 15862 distances.push_back((p.z - base - facet.HeightAt({p.x - local.x, p.y - local.y})) *
347
1/1
✓ Branch 1 taken 15862 times.
15862 normal.z);
348 }
349
1/1
✓ Branch 1 taken 27 times.
27 CutAtPlane(placed, normal, std::move(distances));
350 27 }
351 11115 }
352
353 2 BoulderShape BoulderLayer::Abraded(const BoulderShape& shape, const BoulderPlacement& placement,
354 const Boulder& local, double base) const {
355 2 BoulderShape cut = shape;
356
1/1
✓ Branch 1 taken 2 times.
4 cut.mesh = PlaceMesh(std::move(cut.mesh), placement);
357
1/1
✓ Branch 1 taken 2 times.
2 CutFacets(cut.mesh, local, base);
358
2/2
✓ Branch 0 taken 719 times.
✓ Branch 1 taken 2 times.
721 for (Vec3d& p : cut.mesh.positions) {
359 719 p = placement.Unapply(p);
360 }
361 2 return cut;
362 }
363
364 54233 std::vector<Bounds2d> BoulderLayer::Boxes(const std::vector<Relief>& reliefs) {
365
1/1
✓ Branch 1 taken 54233 times.
54233 std::vector<Bounds2d> boxes;
366
1/1
✓ Branch 1 taken 54233 times.
54233 boxes.reserve(reliefs.size());
367
2/2
✓ Branch 0 taken 23595 times.
✓ Branch 1 taken 54233 times.
77828 for (const Relief& relief : reliefs) {
368
1/1
✓ Branch 1 taken 23595 times.
23595 boxes.push_back(relief.box);
369 }
370 54233 return boxes;
371 }
372
373 76654 std::vector<BoulderLayer::Relief> BoulderLayer::PrepareRelief(const std::vector<Boulder>& boulders,
374 double spacing, bool fillets) const {
375 76654 const BoulderRelief look = m_options.rendering.relief;
376 76654 const bool filleted = m_options.fillet.heightShare > 0.0;
377 76654 std::vector<Relief> reliefs;
378
2/2
✓ Branch 0 taken 62791 times.
✓ Branch 1 taken 76654 times.
139445 for (const Boulder& boulder : boulders) {
379
1/1
✓ Branch 1 taken 62791 times.
62791 const double weight = ResolutionWeight(boulder.diameter, spacing, m_options.fade);
380
2/2
✓ Branch 0 taken 62767 times.
✓ Branch 1 taken 24 times.
62791 const bool drawn = m_options.rendering.reliefRange.Holds(boulder.diameter, boulder.model);
381 62791 const bool rock = drawn && look != BoulderRelief::None;
382
4/4
✓ Branch 0 taken 7566 times.
✓ Branch 1 taken 55225 times.
✓ Branch 2 taken 2766 times.
✓ Branch 3 taken 4800 times.
62791 const bool apron = filleted && (drawn || boulder.model);
383
4/4
✓ Branch 0 taken 62276 times.
✓ Branch 1 taken 515 times.
✓ Branch 2 taken 36953 times.
✓ Branch 3 taken 25323 times.
62791 if (!(rock || apron) || weight <= 0.0) {
384 37468 continue;
385 }
386
2/2
✓ Branch 0 taken 11113 times.
✓ Branch 1 taken 14210 times.
25323 const double width = look == BoulderRelief::Dome ? spacing : 0.0;
387
2/2
✓ Branch 0 taken 8347 times.
✓ Branch 1 taken 16976 times.
25323 Relief relief{&boulder, weight, rock, width, std::nullopt, {}};
388 25323 double reach = 0.5 * (boulder.diameter + width);
389
2/2
✓ Branch 0 taken 8347 times.
✓ Branch 1 taken 16976 times.
25323 if (rock && look == BoulderRelief::Shape) {
390
1/1
✓ Branch 1 taken 8347 times.
8347 relief.placed = PlacedOnLevel(boulder);
391 8347 const Boulder local = InLocalFrame(boulder);
392
1/1
✓ Branch 1 taken 8347 times.
8347 reach = ShapeReach(*relief.placed, local.x, local.y);
393 }
394
2/2
✓ Branch 0 taken 7566 times.
✓ Branch 1 taken 17757 times.
25323 if (filleted) {
395
1/1
✓ Branch 1 taken 7566 times.
7566 reach = std::fmax(reach, FilletReach(boulder, m_options.fillet));
396 }
397
2/2
✓ Branch 0 taken 6930 times.
✓ Branch 1 taken 18393 times.
25323 if (filleted && fillets) {
398
2/2
✓ Branch 0 taken 1290 times.
✓ Branch 1 taken 5640 times.
6930 const double top = m_options.fillet.heightShare * boulder.ExposedHeight();
399
2/2
✓ Branch 0 taken 1290 times.
✓ Branch 1 taken 5640 times.
6930 if (rock && look == BoulderRelief::Dome) {
400
2/2
✓ Branch 0 taken 646 times.
✓ Branch 1 taken 644 times.
1290 if (boulder.facetCount == 0) {
401
1/1
✓ Branch 2 taken 646 times.
646 relief.contacts.push_back(RoundDomeContact(boulder, width, top));
402 }
403 } else {
404 5640 std::optional<TriangleMesh> own;
405
2/2
✓ Branch 0 taken 2766 times.
✓ Branch 1 taken 2874 times.
5640 const TriangleMesh* shape = relief.Shape();
406 2766 if (shape == nullptr) {
407
1/1
✓ Branch 1 taken 2766 times.
2766 own = PlacedOnLevel(boulder);
408 2766 shape = &*own;
409 }
410 5640 const Boulder local = InLocalFrame(boulder);
411
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ relief.contacts = ShapeContacts(*shape, {local.x, local.y}, top,
412
1/1
✓ Branch 1 taken 5640 times.
11280 FilletDirections(boulder.diameter, spacing));
413 5640 double farthest = 0.5 * boulder.diameter;
414
2/2
✓ Branch 0 taken 1441685 times.
✓ Branch 1 taken 5640 times.
1447325 for (const FilletContact& contact : relief.contacts) {
415 1441685 farthest = std::fmax(farthest, contact.distance);
416 }
417
2/2
✓ Branch 0 taken 2766 times.
✓ Branch 1 taken 2874 times.
5640 reach = std::fmax(reach, farthest + m_options.fillet.widthRatio * boulder.diameter);
418 5640 }
419 }
420
1/1
✓ Branch 1 taken 25323 times.
25323 relief.box = ReliefBox(boulder.x, boulder.y, reach, m_surface);
421
1/1
✓ Branch 1 taken 25323 times.
25323 reliefs.push_back(std::move(relief));
422 25323 }
423 76654 return reliefs;
424 }
425
426 57122 void BoulderLayer::ApplySamples(SampleBlock& samples, const LayerContext& context) const {
427
4/4
✓ Branch 0 taken 5796 times.
✓ Branch 1 taken 51326 times.
✓ Branch 2 taken 2898 times.
✓ Branch 3 taken 2898 times.
57122 if (m_options.rendering.relief == BoulderRelief::None && m_options.fillet.heightShare <= 0.0) {
428 2898 return;
429 }
430 54224 const double spacing = samples.ResolvedSpacing(context.spacing, m_surface);
431 54224 const std::vector<Boulder> boulders =
432 54224 ReliefBoulders(samples.Bounds(), spacing, context.ground, context.threads);
433
1/1
✓ Branch 1 taken 54224 times.
54224 const std::vector<Relief> reliefs = PrepareRelief(boulders, spacing, true);
434
1/1
✓ Branch 1 taken 54224 times.
54224 DrawFeatures(samples, context.threads, Boxes(reliefs), m_surface,
435
1/1
✓ Branch 1 taken 54224 times.
54224 [&](float height, float base, const Vec2d& p, std::size_t n) {
436
2/2
✓ Branch 0 taken 122195 times.
✓ Branch 1 taken 210638 times.
332833 const Relief& relief = reliefs[n];
437 332833 return AddRelief(height, base, p.x, p.y, *relief.boulder, relief.Shape(),
438
2/2
✓ Branch 0 taken 122195 times.
✓ Branch 1 taken 210638 times.
332833 relief.weight, relief.rock, relief.width, relief.contacts);
439 });
440 54224 }
441
442 67 std::vector<BoulderModel> BoulderLayer::Models(const std::vector<Boulder>& boulders) const {
443 67 std::vector<const Boulder*> placed;
444
2/2
✓ Branch 0 taken 47708 times.
✓ Branch 1 taken 67 times.
47775 for (const Boulder& boulder : boulders) {
445
4/4
✓ Branch 0 taken 919 times.
✓ Branch 1 taken 46789 times.
✓ Branch 2 taken 874 times.
✓ Branch 3 taken 45 times.
47708 if (boulder.model && boulder.ground) {
446
1/1
✓ Branch 1 taken 874 times.
874 placed.push_back(&boulder);
447 }
448 }
449
1/1
✓ Branch 1 taken 67 times.
67 std::vector<BoulderModel> models(placed.size());
450
2/2
✓ Branch 1 taken 67 times.
✓ Branch 4 taken 67 times.
67 ParallelFor(placed.size(), 0, [&](std::size_t n) {
451 874 const Boulder& boulder = *placed[n];
452 874 std::shared_ptr<const BoulderShape> shape = Shape(boulder);
453 874 const Boulder local = InLocalFrame(boulder);
454 874 const BoulderPlacement placement =
455
2/2
✓ Branch 1 taken 874 times.
✓ Branch 4 taken 874 times.
874 PlaceBoulderModel(local, *shape, *boulder.ground, Settles(boulder));
456
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 872 times.
874 if (boulder.facetCount > 0) {
457 2 shape = std::make_shared<const BoulderShape>(
458
2/3
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
4 Abraded(*shape, placement, local, *boulder.ground));
459 }
460 1748 models[n] = {boulder, ShapeSeed(boulder, m_options.rendering.shapePrototypes), shape,
461
2/6
✗ Branch 1 not taken.
✓ Branch 2 taken 874 times.
✓ Branch 3 taken 874 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
874 placement};
462
2/3
✓ Branch 1 taken 874 times.
✓ Branch 3 taken 874 times.
✗ Branch 4 not taken.
2622 });
463 67 return models;
464 67 }
465
466 } // namespace terraingen
467