./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
210 / 212
99.1%
Branches:
144 / 154
93.5%
Functions:
20 / 20
100.0%

src/layers/boulders/boulder_shape.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/boulders/boulder_shape.h"
4
5 #include "terraingen/layers/boulders/boulder_fields.h"
6 #include "terraingen/noise/hash.h"
7 #include "terraingen/noise/perlin.h"
8 #include "terraingen/random/stream.h"
9 #include "terraingen/util/bits.h"
10
11 #include <algorithm>
12 #include <cmath>
13 #include <limits>
14 #include <stdexcept>
15
16 namespace terraingen {
17
18 namespace {
19
20 /// Throws unless `low` <= `range.min` <= `range.max` <= `high`.
21 110596 void CheckStyleRange(const ValueRange& range, double low, double high, const char* what) {
22
6/6
✓ Branch 0 taken 110595 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 110594 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 110592 times.
110596 if (!(range.min >= low && range.min <= range.max && range.max <= high)) {
23
3/3
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
16 throw std::invalid_argument(std::string("Boulder style ") + what + " must satisfy " +
24
1/1
✓ Branch 1 taken 4 times.
12 std::to_string(low) +
25
4/4
✓ Branch 2 taken 4 times.
✓ Branch 5 taken 4 times.
✓ Branch 8 taken 4 times.
✓ Branch 11 taken 4 times.
16 " <= min <= max <= " + std::to_string(high));
26 }
27 110592 }
28
29 /// A uniform draw from `range`, from the next uniform of `random`.
30 17762 double Draw(RandomStream& random, const ValueRange& range) {
31 17762 return range.Lerp(random.NextDouble());
32 }
33
34 /// The surface roughness of a style at direction `p`: `noiseOctaves` octaves of 3D Perlin noise
35 /// halving in wavelength from `noiseWavelength`, each of half the amplitude, in about [-1, 1].
36 1706351 double SurfaceRoughness(const Vec3d& p, const BoulderStyle& style, std::uint32_t seed) {
37 1706351 double sum = 0.0;
38 1706351 double norm = 0.0;
39 1706351 double amplitude = 1.0;
40 1706351 double frequency = 1.0 / style.noiseWavelength;
41
2/2
✓ Branch 0 taken 6718363 times.
✓ Branch 1 taken 1706351 times.
8424714 for (int octave = 0; octave < style.noiseOctaves; ++octave) {
42 6718363 sum += amplitude *
43 6718363 static_cast<double>(PerlinNoise3(static_cast<float>(p.x * frequency + 17.31),
44 6718363 static_cast<float>(p.y * frequency - 5.77),
45 6718363 static_cast<float>(p.z * frequency + 3.13),
46 6718363 seed + static_cast<std::uint32_t>(octave)));
47 6718363 norm += amplitude;
48 6718363 amplitude *= 0.5;
49 6718363 frequency *= 2.0;
50 }
51 1706351 return sum / norm;
52 }
53
54 /// A unit vector, uniform on the sphere when `maxTilt` is 1, with |z| at most `maxTilt`.
55 6795 Vec3d DrawNormal(RandomStream& random, double maxTilt) {
56 6795 const double z = maxTilt * (2.0 * random.NextDouble() - 1.0);
57 6795 const double angle = 2.0 * kPi * random.NextDouble();
58 6795 const double ring = std::sqrt(std::fmax(0.0, 1.0 - z * z));
59 6795 return {ring * std::cos(angle), ring * std::sin(angle), z};
60 }
61
62 /// The extents of a library rock, which must be a valid mesh with depth along every axis.
63 270 Vec3d CheckRock(const TriangleMesh& mesh) {
64 270 ValidateMesh(mesh);
65 270 const Vec3d extents = Bounds(mesh).Extents();
66
4/6
✓ Branch 0 taken 270 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 270 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 269 times.
270 if (!(extents.x > 0.0 && extents.y > 0.0 && extents.z > 0.0)) {
67 1 throw std::invalid_argument(
68
1/1
✓ Branch 2 taken 1 time.
1 "Boulder shape library meshes must have depth along every axis");
69 }
70 269 return extents;
71 }
72
73 /// The index of the style a uniform draw picks, each in proportion to its weight.
74 3243 std::size_t PickStyle(RandomStream& random, const std::vector<BoulderStyle>& styles) {
75 3243 double total = 0.0;
76
2/2
✓ Branch 0 taken 17067 times.
✓ Branch 1 taken 3243 times.
20310 for (const BoulderStyle& style : styles) {
77 17067 total += style.weight;
78 }
79 3243 double pick = total * random.NextDouble();
80 3243 std::size_t index = 0;
81
2/2
✓ Branch 0 taken 7223 times.
✓ Branch 1 taken 28 times.
7251 while (index + 1 < styles.size() &&
82
3/4
✓ Branch 0 taken 7223 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4008 times.
✓ Branch 3 taken 3215 times.
7223 (styles[index].weight == 0.0 || pick >= styles[index].weight)) {
83 4008 pick -= styles[index].weight;
84 4008 ++index;
85 }
86 3243 return index;
87 }
88
89 /// The solid's vertex `p` stretched to `axes`, pulled `rounding` of the way to the ellipsoid of
90 /// those axes and roughened by the style's noise.
91 626142 Vec3d ShapeVertex(const Vec3d& p, const Vec3d& axes, double rounding, const BoulderStyle& style,
92 std::uint32_t noiseSeed) {
93 626142 const Vec3d stretched{p.x * axes.x, p.y * axes.y, p.z * axes.z};
94 626142 const Vec3d ray = Normalized(stretched);
95 626148 const double ellipsoid =
96 626148 1.0 / std::sqrt(std::pow(ray.x / axes.x, 2.0) + std::pow(ray.y / axes.y, 2.0) +
97 626148 std::pow(ray.z / axes.z, 2.0));
98 626148 const double radius = (1.0 - rounding) * Length(stretched) + rounding * ellipsoid;
99 626129 const double rough = 1.0 + style.noiseAmplitude * SurfaceRoughness(ray, style, noiseSeed);
100 626129 return ray * (radius * rough);
101 }
102
103 /// The smooth minimum of `a` and `b` over `k`: their minimum, eased by up to `k` / 4 where they
104 /// are within `k` of each other.
105 853722 double SmoothMin(double a, double b, double k) {
106 853722 const double h = std::fmax(k - std::fabs(a - b), 0.0) / k;
107 853722 return std::fmin(a, b) - 0.25 * k * h * h;
108 }
109
110 /// A fracture plane: points beyond `offset` along `normal` are cut away.
111 struct Fracture {
112 Vec3d normal;
113 double offset;
114 };
115
116 /// The planes of `solid`'s faces once stretched to `axes`, one per face.
117 471 std::vector<Fracture> SolidFaces(const TriangleMesh& solid, const Vec3d& axes) {
118 471 std::vector<Fracture> faces;
119
2/2
✓ Branch 0 taken 3924 times.
✓ Branch 1 taken 471 times.
4395 for (const auto& t : solid.triangles) {
120 3924 const Vec3d& a = solid.positions[t[0]];
121 3924 const Vec3d normal =
122 3924 Normalized(Cross(solid.positions[t[1]] - a, solid.positions[t[2]] - a));
123 3924 const Vec3d stretched{normal.x / axes.x, normal.y / axes.y, normal.z / axes.z};
124 3924 const double length = Length(stretched);
125
2/2
✓ Branch 0 taken 2453 times.
✓ Branch 1 taken 1471 times.
3924 const Fracture face{stretched * (1.0 / length), Dot(normal, a) / length};
126
2/2
✓ Branch 0 taken 2453 times.
✓ Branch 1 taken 1471 times.
3924 const bool seen = std::any_of(faces.begin(), faces.end(), [&](const Fracture& f) {
127
2/2
✓ Branch 0 taken 1471 times.
✓ Branch 1 taken 8831 times.
10302 return Dot(f.normal, face.normal) > 1.0 - 1e-9 &&
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1471 times.
1471 std::fabs(f.offset - face.offset) < 1e-9;
129 });
130
2/2
✓ Branch 0 taken 2453 times.
✓ Branch 1 taken 1471 times.
3924 if (!seen) {
131
1/1
✓ Branch 1 taken 2453 times.
2453 faces.push_back(face);
132 }
133 }
134 471 return faces;
135 }
136
137 /// Cuts `mesh` with `fractures`, each vertex moving along its ray from the origin to the nearest
138 /// plane: rippled by the style's face roughness and met through a smooth minimum over `softness`,
139 /// so faces undulate and edges are worn round. With `polyhedral`, the planes include the solid's
140 /// own faces, which then bound every ray, roughened by the style's noise of `noiseSeed`.
141 /// Planes too far beyond the cut so far to move a vertex are passed over unroughened.
142 1238 void CutSoftly(TriangleMesh& mesh, const std::vector<Fracture>& fractures,
143 const BoulderStyle& style, double softness, std::uint32_t seed, bool polyhedral,
144 std::uint32_t noiseSeed) {
145 1238 const double frequency = 1.5;
146
2/2
✓ Branch 0 taken 1235 times.
✓ Branch 1 taken 3 times.
1238 const double blend = softness > 0.0 ? 4.0 * softness : 0.0;
147
2/2
✓ Branch 0 taken 561558 times.
✓ Branch 1 taken 1238 times.
562796 for (Vec3d& p : mesh.positions) {
148 561558 const double radius = Length(p);
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 561558 times.
561558 if (!(radius > 0.0)) {
150 ✗ continue;
151 }
152
2/2
✓ Branch 0 taken 435532 times.
✓ Branch 1 taken 126026 times.
561558 const Vec3d ray = p * (1.0 / radius);
153
2/2
✓ Branch 0 taken 435532 times.
✓ Branch 1 taken 126026 times.
561558 double reach = polyhedral ? std::numeric_limits<double>::infinity() : radius;
154
2/2
✓ Branch 0 taken 4078909 times.
✓ Branch 1 taken 561558 times.
4640467 for (std::size_t i = 0; i < fractures.size(); ++i) {
155
2/2
✓ Branch 0 taken 2053034 times.
✓ Branch 1 taken 2025875 times.
4078909 const double along = Dot(fractures[i].normal, ray);
156
2/2
✓ Branch 0 taken 2053034 times.
✓ Branch 1 taken 2025875 times.
4078909 if (!(along > 0.0)) {
157 2053034 continue;
158 }
159 2025875 double offset = fractures[i].offset;
160
2/2
✓ Branch 0 taken 1899327 times.
✓ Branch 1 taken 126548 times.
2025875 if (std::isfinite(reach) &&
161
2/2
✓ Branch 0 taken 1044097 times.
✓ Branch 1 taken 855230 times.
1899327 offset * (1.0 - style.faceRoughness) / along >= (reach + blend) * (1.0 + 1e-9)) {
162 1044097 continue;
163 }
164
2/2
✓ Branch 0 taken 954872 times.
✓ Branch 1 taken 26906 times.
981778 if (style.faceRoughness > 0.0) {
165 954872 const double shift = 7.13 * static_cast<double>(i);
166 954872 const Vec3d at{ray.x * frequency + shift, ray.y * frequency - shift,
167 954872 ray.z * frequency + 0.5 * shift};
168 954872 offset *=
169 954872 1.0 + style.faceRoughness *
170 954872 SurfaceRoughness(at, style, seed + static_cast<std::uint32_t>(i));
171 }
172 981778 const double cut = offset / along;
173
4/4
✓ Branch 0 taken 979055 times.
✓ Branch 1 taken 2723 times.
✓ Branch 2 taken 853729 times.
✓ Branch 3 taken 125326 times.
981778 reach = softness > 0.0 && std::isfinite(reach) ? SmoothMin(reach, cut, blend)
174 128049 : std::fmin(reach, cut);
175 }
176
2/2
✓ Branch 0 taken 126125 times.
✓ Branch 1 taken 435433 times.
561558 if (polyhedral) {
177 126125 reach *= 1.0 + style.noiseAmplitude * SurfaceRoughness(ray, style, noiseSeed);
178 }
179 561558 p = ray * reach;
180 }
181 1238 }
182
183 /// The next fracture plane of `random` for `style`: a drawn normal, offset by a drawn share of
184 /// the farthest reach along it of the first `corners` points of `mesh`.
185 6794 Fracture DrawFracture(RandomStream& random, const BoulderStyle& style, const TriangleMesh& mesh,
186 std::size_t corners) {
187 6794 const Vec3d normal = DrawNormal(random, style.maxFractureTilt);
188 6795 const double depth = Draw(random, style.fractureDepth);
189 double reach = 0.0;
190
2/2
✓ Branch 0 taken 71860 times.
✓ Branch 1 taken 6795 times.
78655 for (std::size_t v = 0; v < corners; ++v) {
191 71860 reach = std::fmax(reach, Dot(normal, mesh.positions[v]));
192 }
193 6795 return {normal, depth * reach};
194 }
195
196 /// Moves the points of `mesh` beyond `fracture` along their rays onto it.
197 3 void CutSharply(TriangleMesh& mesh, const Fracture& fracture) {
198
2/2
✓ Branch 0 taken 1414 times.
✓ Branch 1 taken 3 times.
1417 for (Vec3d& p : mesh.positions) {
199
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 1166 times.
1414 const double along = Dot(fracture.normal, p);
200
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 1166 times.
1414 if (along > fracture.offset) {
201 248 p = p * (fracture.offset / along);
202 }
203 }
204 3 }
205
206 /// Cuts `mesh` with the style's fracture planes, each at a drawn share of the reach of the solid's
207 /// original corners. `Subdivide` keeps those corners as the first `corners` vertices.
208 3243 void ApplyFractures(TriangleMesh& mesh, std::size_t corners, const BoulderStyle& style,
209 RandomStream& random, double softness, std::uint32_t seed,
210 const std::vector<Fracture>& faces, std::uint32_t noiseSeed) {
211 3243 const int span = style.maxFractures - style.minFractures + 1;
212 3243 const int fractures =
213
2/2
✓ Branch 1 taken 982 times.
✓ Branch 2 taken 2261 times.
3243 style.minFractures + std::min(span - 1, static_cast<int>(random.NextDouble() * span));
214
4/4
✓ Branch 0 taken 2008 times.
✓ Branch 1 taken 1235 times.
✓ Branch 2 taken 2005 times.
✓ Branch 3 taken 3 times.
3243 const bool soft = softness > 0.0 || style.faceRoughness > 0.0;
215 3243 std::vector<Fracture> planes = soft ? faces : std::vector<Fracture>{};
216
2/2
✓ Branch 0 taken 6792 times.
✓ Branch 1 taken 3243 times.
10035 for (int n = 0; n < fractures; ++n) {
217
1/1
✓ Branch 1 taken 6795 times.
6792 const Fracture fracture = DrawFracture(random, style, mesh, corners);
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6795 times.
6795 if (!(fracture.offset > 0.0)) {
219 ✗ continue;
220 }
221
2/2
✓ Branch 0 taken 6792 times.
✓ Branch 1 taken 3 times.
6795 if (soft) {
222
1/1
✓ Branch 1 taken 6789 times.
6792 planes.push_back(fracture);
223 } else {
224 3 CutSharply(mesh, fracture);
225 }
226 }
227
2/2
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 2005 times.
3243 if (soft) {
228 1238 CutSoftly(mesh, planes, style, softness, seed, !faces.empty(), noiseSeed);
229 }
230 3243 }
231
232 /// `rock` in the unit form of `BoulderShape`: centered on its box, turned about z so its longer
233 /// horizontal side lies along x, and scaled so that side is 1 long.
234 92 BoulderShape NormalizedBoulderShape(const SceneMesh& rock) {
235 92 const Vec3d extents = CheckRock(rock.mesh);
236 92 BoulderShape shape = UnitBoulderShape(rock.mesh, Bounds(rock.mesh));
237
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 91 times.
92 if (extents.y > extents.x) {
238
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 time.
9 for (Vec3d& p : shape.mesh.positions) {
239 8 p = {p.y, -p.x, p.z};
240 }
241 }
242
1/1
✓ Branch 1 taken 92 times.
92 shape.material = rock.material;
243 92 return shape;
244 }
245
246 } // namespace
247
248 557 std::vector<BoulderStyle> DefaultBoulderStyles() {
249 557 constexpr BaseSolid kIcosahedron = BaseSolid::Icosahedron;
250 // Fields in order: name, weight, solid, intermediate, shortAxis, rounding, min and max
251 // fractures, fractureDepth, maxFractureTilt, noise amplitude, wavelength and octaves,
252 // edgeSoftness, faceRoughness.
253 557 return {
254 {"angular",
255 0.2,
256 kIcosahedron,
257 {0.6, 0.9},
258 {0.45, 0.7},
259 {0.0, 0.0},
260 7,
261 12,
262 {0.55, 0.85},
263 1.0,
264 0.06,
265 0.9,
266 4,
267 {0.02, 0.06},
268 0.03},
269 {"platy",
270 0.12,
271 kIcosahedron,
272 {0.6, 0.9},
273 {0.22, 0.38},
274 {0.0, 0.0},
275 5,
276 9,
277 {0.6, 0.88},
278 0.7,
279 0.05,
280 0.9,
281 4,
282 {0.02, 0.05},
283 0.03},
284 {"subrounded",
285 0.15,
286 kIcosahedron,
287 {0.65, 0.9},
288 {0.45, 0.7},
289 {0.0, 0.0},
290 4,
291 7,
292 {0.65, 0.9},
293 1.0,
294 0.07,
295 0.8,
296 4,
297 {0.06, 0.12},
298 0.02},
299 {"rounded",
300 0.1,
301 kIcosahedron,
302 {0.7, 0.95},
303 {0.5, 0.75},
304 {0.0, 0.0},
305 0,
306 3,
307 {0.75, 0.95},
308 1.0,
309 0.1,
310 0.7,
311 4,
312 {0.1, 0.18},
313 0.02},
314 {"block",
315 0.13,
316 BaseSolid::Cube,
317 {0.6, 0.85},
318 {0.4, 0.6},
319 {0.0, 0.15},
320 3,
321 7,
322 {0.6, 0.85},
323 1.0,
324 0.07,
325 0.8,
326 4,
327 {0.06, 0.12},
328 0.06},
329 {"wedge",
330 0.1,
331 BaseSolid::Prism,
332 {0.55, 0.85},
333 {0.4, 0.6},
334 {0.0, 0.2},
335 2,
336 5,
337 {0.65, 0.9},
338 1.0,
339 0.07,
340 0.8,
341 4,
342 {0.06, 0.12},
343 0.06},
344 {"pyramid",
345 0.1,
346 BaseSolid::Pyramid,
347 {0.7, 0.95},
348 {0.45, 0.7},
349 {0.0, 0.2},
350 2,
351 4,
352 {0.7, 0.92},
353 1.0,
354 0.07,
355 0.8,
356 4,
357 {0.06, 0.12},
358 0.06},
359 {"shard",
360 0.05,
361 BaseSolid::Tetrahedron,
362 {0.6, 0.9},
363 {0.4, 0.6},
364 {0.0, 0.2},
365 2,
366 4,
367 {0.7, 0.92},
368 1.0,
369 0.04,
370 0.8,
371 4,
372 {0.02, 0.05},
373 0.03},
374 {"sphere",
375 0.05,
376 kIcosahedron,
377 {0.85, 1.0},
378 {0.7, 0.9},
379 {0.0, 0.0},
380 0,
381 1,
382 {0.8, 0.95},
383 1.0,
384 0.08,
385 0.7,
386 4,
387 {0.12, 0.2},
388 0.02},
389
3/5
✓ Branch 2 taken 557 times.
✓ Branch 3 taken 5013 times.
✓ Branch 4 taken 557 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
5570 };
390
9/11
✓ Branch 1 taken 557 times.
✓ Branch 4 taken 557 times.
✓ Branch 7 taken 557 times.
✓ Branch 10 taken 557 times.
✓ Branch 13 taken 557 times.
✓ Branch 16 taken 557 times.
✓ Branch 19 taken 557 times.
✓ Branch 22 taken 557 times.
✓ Branch 25 taken 557 times.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
1114 }
391
392 3818 void ValidateBoulderShapeOptions(const BoulderShapeOptions& options) {
393 3818 CheckRanges(options);
394 3809 double total = 0.0;
395
2/2
✓ Branch 0 taken 22121 times.
✓ Branch 1 taken 3804 times.
25925 for (const BoulderStyle& style : options.styles) {
396 22121 total += style.weight;
397 22121 CheckStyleRange(style.intermediate, 1e-3, 1.0, "intermediate");
398 22120 CheckStyleRange(style.shortAxis, 1e-3, 1.0, "shortAxis");
399 22119 CheckStyleRange(style.rounding, 0.0, 1.0, "rounding");
400 22118 CheckStyleRange(style.fractureDepth, 1e-3, 1.0, "fractureDepth");
401 22118 CheckStyleRange(style.edgeSoftness, 0.0, 0.5, "edgeSoftness");
402
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 22116 times.
22117 if (!(style.minFractures <= style.maxFractures)) {
403
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Boulder style fractures must satisfy min <= max");
404 }
405 }
406
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3803 times.
3804 if (!(total > 0.0)) {
407
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Boulder style weights must not all be zero");
408 }
409
2/2
✓ Branch 0 taken 179 times.
✓ Branch 1 taken 3801 times.
3980 for (const auto& rock : options.library) {
410
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 178 times.
179 if (rock == nullptr) {
411
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Boulder shape library meshes must not be null");
412 }
413 178 CheckRock(rock->mesh);
414 }
415 3801 }
416
417 3336 BoulderShape GenerateBoulderShape(std::uint64_t shapeSeed, const BoulderShapeOptions& options) {
418 3336 ValidateBoulderShapeOptions(options);
419 3335 const IdWords words = SplitId(shapeSeed);
420 3335 RandomStream random(options.seed, kBoulderShapeStream, words.low, words.high);
421
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 3243 times.
3335 if (!options.library.empty()) {
422 92 const std::size_t index = random.NextU32() % options.library.size();
423 92 BoulderShape shape = NormalizedBoulderShape(*options.library[index]);
424 92 shape.style = index;
425 92 return shape;
426 92 }
427 3243 const std::size_t index = PickStyle(random, options.styles);
428 3243 const BoulderStyle& style = options.styles[index];
429 3243 const double intermediate = Draw(random, style.intermediate);
430 3243 const double shortAxis = std::fmin(Draw(random, style.shortAxis), intermediate);
431 3243 const double rounding = Draw(random, style.rounding);
432 3243 const std::uint32_t noiseSeed = random.NextU32();
433 3243 const double softness =
434
2/2
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 2005 times.
3243 style.edgeSoftness.max > 0.0 ? Draw(random, style.edgeSoftness) * options.weathering : 0.0;
435 3243 const Vec3d axes{1.0, intermediate, shortAxis};
436
437 3243 const TriangleMesh solid = BaseSolidMesh(style.solid);
438 3243 TriangleMesh mesh =
439
1/1
✓ Branch 1 taken 3243 times.
3243 Subdivide(solid, options.subdivisions, style.solid == BaseSolid::Icosahedron);
440
2/2
✓ Branch 0 taken 626141 times.
✓ Branch 1 taken 3243 times.
629384 for (Vec3d& p : mesh.positions) {
441 626141 p = ShapeVertex(p, axes, rounding, style, noiseSeed);
442 }
443
4/4
✓ Branch 0 taken 2008 times.
✓ Branch 1 taken 1235 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2005 times.
3243 const bool soft = softness > 0.0 || style.faceRoughness > 0.0;
444
2/2
✓ Branch 0 taken 471 times.
✓ Branch 1 taken 767 times.
1238 const std::vector<Fracture> faces = soft && style.solid != BaseSolid::Icosahedron
445 ? SolidFaces(solid, axes)
446
1/1
✓ Branch 1 taken 471 times.
3243 : std::vector<Fracture>{};
447
1/1
✓ Branch 1 taken 3243 times.
3243 ApplyFractures(mesh, solid.positions.size(), style, random, softness,
448 SubstreamSeed(noiseSeed, kBoulderFractureStream), faces, noiseSeed);
449
450
1/1
✓ Branch 1 taken 3242 times.
3243 const Vec3d extents = Bounds(mesh).Extents();
451
1/1
✓ Branch 1 taken 3243 times.
3242 mesh = Scaled(mesh, {1.0 / extents.x, intermediate / extents.y, shortAxis / extents.z});
452 3242 return {std::move(mesh), index, intermediate, shortAxis};
453
1/1
✓ Branch 1 taken 3243 times.
9728 }
454
455 1435 BoulderShape UnitBoulderShape(const TriangleMesh& mesh, const Box3d& box) {
456 1435 const Vec3d center = (box.min + box.max) * 0.5;
457 1435 const Vec3d extents = box.max - box.min;
458 1435 const double length = std::fmax(extents.x, extents.y);
459 1435 BoulderShape shape;
460
1/1
✓ Branch 1 taken 1435 times.
1435 shape.mesh.triangles = mesh.triangles;
461
2/2
✓ Branch 0 taken 680650 times.
✓ Branch 1 taken 1435 times.
682085 for (const Vec3d& p : mesh.positions) {
462
1/1
✓ Branch 1 taken 680650 times.
680650 shape.mesh.positions.push_back((p - center) * (1.0 / length));
463 }
464 1435 shape.intermediate = std::fmin(extents.x, extents.y) / length;
465 1435 shape.shortAxis = extents.z / length;
466 1435 return shape;
467 }
468
469 23795 std::uint64_t BoulderShapeSeed(std::uint64_t id, std::uint32_t prototypes) {
470
2/2
✓ Branch 0 taken 23785 times.
✓ Branch 1 taken 10 times.
23795 if (prototypes == 0) {
471 return id;
472 }
473 23785 const IdWords words = SplitId(id);
474 23785 const UVec3 hash = Pcg3dHash({words.low, words.high, kBoulderPrototypeStream});
475 23785 return hash.x % prototypes;
476 }
477
478 } // namespace terraingen
479