./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
24 / 24
100.0%
Branches:
24 / 46
52.2%
Functions:
2 / 2, 4 excl
100.0%

include/terraingen/layers/boulders/boulder_shape.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Procedural boulder meshes: styles, shape options and seeded shape generation.
5 #pragma once
6
7 #include "terraingen/mesh/io/mesh_io.h"
8 #include "terraingen/mesh/mesh.h"
9 #include "terraingen/mesh/polyhedra.h"
10
11 #include <cstddef>
12 #include <cstdint>
13 #include <memory>
14 #include <string>
15 #include <vector>
16
17 namespace terraingen {
18
19 /// An interval to draw a value uniformly from.
20 7 struct ValueRange {
21 double min = 0.0; ///< Lower end.
22 double max = 0.0; ///< Upper end, >= `min`.
23
24 /// The value `u` of the way from `min` to `max`: a uniform draw for a uniform `u` in [0, 1).
25 991515 double Lerp(double u) const {
26 991515 return min + (max - min) * u;
27 }
28
29 135 bool operator==(const ValueRange& other) const {
30
5/10
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 27 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 27 times.
✗ Branch 9 not taken.
135 return min == other.min && max == other.max;
31 }
32 };
33
34 /// One kind of boulder: the solid it starts from and the ranges its shape is drawn from.
35 ///
36 /// The solid is subdivided, stretched, rounded, roughened and cut by fracture planes, every vertex
37 /// moving only along its ray from the origin, so the mesh never folds over itself. Ratios and
38 /// lengths are in long-axis lengths.
39 45903 struct BoulderStyle {
40 std::string name; ///< Label, e.g. "block".
41 double weight = 1.0; ///< Relative share of boulders of this style.
42 /// The solid the shape starts from.
43 BaseSolid solid = BaseSolid::Icosahedron;
44 ValueRange intermediate{0.6, 0.85}; ///< b / a, the intermediate axis over the long one.
45 ValueRange shortAxis{0.4, 0.6}; ///< c / a, the short (vertical) axis over the long one.
46 ValueRange rounding{0.0, 0.0}; ///< Share toward the ellipsoid: 0 keeps corners, 1 is it.
47 int minFractures = 0; ///< Fewest fracture planes.
48 int maxFractures = 0; ///< Most fracture planes.
49 ValueRange fractureDepth{0.6, 0.9}; ///< Plane offset as a share of the shape's reach.
50 double maxFractureTilt = 1.0; ///< Largest |z| of a fracture normal, in [0, 1].
51 double noiseAmplitude = 0.05; ///< Largest relative change of the radius by noise.
52 double noiseWavelength = 0.6; ///< Wavelength of the first noise octave.
53 int noiseOctaves = 3; ///< Noise octaves, each half the wavelength.
54 /// How far edges and corners are worn back where faces meet, as weathering rounds them, in
55 /// long-axis lengths; 0 keeps them sharp.
56 ValueRange edgeSoftness{0.0, 0.0};
57 /// How far fracture faces undulate, as a share of each plane's offset; 0 keeps them flat.
58 double faceRoughness = 0.0;
59
60 27 bool operator==(const BoulderStyle& other) const {
61
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 return name == other.name && weight == other.weight && solid == other.solid &&
62
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
54 intermediate == other.intermediate && shortAxis == other.shortAxis &&
63
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
54 rounding == other.rounding && minFractures == other.minFractures &&
64
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 maxFractures == other.maxFractures && fractureDepth == other.fractureDepth &&
65
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 maxFractureTilt == other.maxFractureTilt && noiseAmplitude == other.noiseAmplitude &&
66
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 noiseWavelength == other.noiseWavelength && noiseOctaves == other.noiseOctaves &&
67
3/6
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 27 times.
81 edgeSoftness == other.edgeSoftness && faceRoughness == other.faceRoughness;
68 }
69 };
70
71 /// The default mix of boulder styles, every one cut irregularly, weathered at its edges and rough
72 /// on its faces: polyhedra from a sphere (angular, platy, subrounded) most of all, then blocks from
73 /// a cube, wedges, pyramids, rounded boulders, shards from a tetrahedron and near-spheres.
74 std::vector<BoulderStyle> DefaultBoulderStyles();
75
76 /// Settings for `GenerateBoulderShape`.
77
2/2
✓ Branch 3 taken 1 time.
✓ Branch 11 taken 1 time.
3997 struct BoulderShapeOptions {
78 std::vector<BoulderStyle> styles = DefaultBoulderStyles(); ///< Styles to pick from by weight.
79 int subdivisions = 3; ///< Times the solid's triangles are split in four.
80 std::uint32_t seed = 0; ///< Seed shared by every shape.
81 /// Scales every style's edge softness: 0 gives sharp edges, 1 the styles' own, more is more
82 /// worn.
83 double weathering = 1.0;
84 /// Rock meshes, such as `ReadMesh` reads, that replace the styles when not empty: each seed
85 /// picks one, normalized to the unit form of `BoulderShape` with its z axis kept vertical.
86 std::vector<std::shared_ptr<const SceneMesh>> library{};
87
88 3 bool operator==(const BoulderShapeOptions& other) const {
89
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 return styles == other.styles && subdivisions == other.subdivisions && seed == other.seed &&
90
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 weathering == other.weathering && library == other.library;
91 }
92 };
93
94 /// Checks boulder shape options against their field ranges (`CheckRanges`) and what ties them
95 /// together: ordered value ranges within their bounds, fracture counts in order and weights not
96 /// all zero.
97 /// @throws std::invalid_argument naming the first field out of its range, or for a value range
98 /// out of order or bounds, zero total weight, or a library mesh that is null, invalid or flat
99 /// along an axis.
100 void ValidateBoulderShapeOptions(const BoulderShapeOptions& options);
101
102 /// A boulder's shape in unit form, with the origin inside it.
103 struct BoulderShape {
104 TriangleMesh mesh; ///< Long axis along x, extent exactly 1.
105 std::size_t style = 0; ///< Index into the options' styles, or its library when it has one.
106 double intermediate = 1.0; ///< Extent along y, in long-axis lengths.
107 double shortAxis = 1.0; ///< Extent along z, in long-axis lengths.
108 MeshMaterial material{"rock", {0.5, 0.5, 0.5}}; ///< The library mesh's, or plain gray.
109 };
110
111 /// `mesh` in the unit form of `BoulderShape` without turning it: centered on its bounding `box`
112 /// and scaled so the longer horizontal side of the box is 1 long; the style and material are left
113 /// as the defaults.
114 BoulderShape UnitBoulderShape(const TriangleMesh& mesh, const Box3d& box);
115
116 /// The shape a seed gives, a pure function of the seed and options; every subdivision level
117 /// gives the same shape in finer detail.
118 /// @param shapeSeed E.g. from `BoulderShapeSeed`.
119 /// @throws std::invalid_argument as `ValidateBoulderShapeOptions`.
120 BoulderShape GenerateBoulderShape(std::uint64_t shapeSeed, const BoulderShapeOptions& options);
121
122 /// The shape seed of the boulder with ID `id`: `id` itself for 0 `prototypes`, otherwise `id`
123 /// hashed into [0, `prototypes`), so a field can be drawn with a small library of instanced meshes.
124 std::uint64_t BoulderShapeSeed(std::uint64_t id, std::uint32_t prototypes = 0);
125
126 } // namespace terraingen
127