./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
189 / 190
99.5%
Branches:
113 / 183
61.7%
Functions:
15 / 15
100.0%

src/layers/boulders/boulder_models.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/boulders/boulder_models.h"
4
5 #include "terraingen/util/bits.h"
6
7 #include <algorithm>
8 #include <array>
9 #include <cmath>
10 #include <limits>
11 #include <utility>
12 #include <vector>
13
14 namespace terraingen {
15
16 namespace {
17
18 /// Axis `index` of `frame`: 0 east, 1 north, 2 up.
19 1098 const Vec3d& Axis(const LocalFrame& frame, int index) {
20
4/4
✓ Branch 0 taken 366 times.
✓ Branch 1 taken 732 times.
✓ Branch 2 taken 366 times.
✓ Branch 3 taken 366 times.
1098 return index == 0 ? frame.east : (index == 1 ? frame.north : frame.up);
21 }
22
23 /// The rotation turning the shape's axis that `rest` names to the vertical.
24 17079 RigidTransform RestRotation(BoulderRest rest) {
25
3/3
✓ Branch 0 taken 608 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 16421 times.
17079 switch (rest) {
26 608 case BoulderRest::Side:
27 608 return RigidTransform::RotationX(0.5 * kPi);
28 50 case BoulderRest::End:
29 50 return RigidTransform::RotationY(-0.5 * kPi);
30 case BoulderRest::Face:
31 break;
32 }
33 16421 return {};
34 }
35
36 /// The center of mass of a closed mesh of uniform density, from its signed tetrahedra.
37 8511 Vec3d MassCenter(const std::vector<Vec3d>& points,
38 const std::vector<std::array<std::uint32_t, 3>>& triangles) {
39 8511 Vec3d sum{};
40 8511 double volume = 0.0;
41
2/2
✓ Branch 0 taken 6982814 times.
✓ Branch 1 taken 8511 times.
6991325 for (const auto& t : triangles) {
42 6982814 const Vec3d& a = points[t[0]];
43 6982814 const Vec3d& b = points[t[1]];
44 6982814 const Vec3d& c = points[t[2]];
45 6982814 const double v = Dot(a, Cross(b, c));
46 6982814 sum = sum + (a + b + c) * v;
47 6982814 volume += v;
48 }
49 8511 return sum * (1.0 / (4.0 * volume));
50 }
51
52 /// The convex hull of points in the plane, counter-clockwise, without collinear points.
53 51837 std::vector<Vec2d> Hull2d(std::vector<Vec2d> points) {
54 51837 std::sort(points.begin(), points.end(), [](const Vec2d& a, const Vec2d& b) {
55
6/72
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 8101 times.
✓ Branch 13 taken 39676 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 39676 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 53 not taken.
✗ Branch 54 not taken.
✗ Branch 55 not taken.
✗ Branch 56 not taken.
✗ Branch 57 not taken.
✗ Branch 58 not taken.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
✗ Branch 61 not taken.
✗ Branch 62 not taken.
✗ Branch 63 not taken.
✗ Branch 64 not taken.
✗ Branch 65 not taken.
✓ Branch 66 taken 39676 times.
✓ Branch 67 taken 29762 times.
✗ Branch 68 not taken.
✓ Branch 69 taken 39676 times.
✗ Branch 70 not taken.
✗ Branch 71 not taken.
117215 return a.x < b.x || (a.x == b.x && a.y < b.y);
56 });
57
2/2
✓ Branch 0 taken 25757 times.
✓ Branch 1 taken 26081 times.
51838 if (points.size() < 3) {
58 25757 return points;
59 }
60 78289 const auto turn = [](const Vec2d& o, const Vec2d& a, const Vec2d& b) {
61 52208 return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
62 };
63 26081 std::vector<Vec2d> hull(2 * points.size());
64 26081 std::size_t k = 0;
65
2/2
✓ Branch 0 taken 78259 times.
✓ Branch 1 taken 26081 times.
104340 for (std::size_t i = 0; i < points.size(); ++i) {
66
4/4
✓ Branch 0 taken 26104 times.
✓ Branch 1 taken 65194 times.
✓ Branch 2 taken 13039 times.
✓ Branch 3 taken 13065 times.
91298 while (k >= 2 && turn(hull[k - 2], hull[k - 1], points[i]) <= 0.0) {
67 --k;
68 }
69 78259 hull[k++] = points[i];
70 }
71
2/2
✓ Branch 0 taken 52176 times.
✓ Branch 1 taken 26081 times.
78257 for (std::size_t i = points.size() - 1, lower = k + 1; i-- > 0;) {
72
4/4
✓ Branch 0 taken 26104 times.
✓ Branch 1 taken 39129 times.
✓ Branch 2 taken 13057 times.
✓ Branch 3 taken 13047 times.
65233 while (k >= lower && turn(hull[k - 2], hull[k - 1], points[i]) <= 0.0) {
73 --k;
74 }
75 52176 hull[k++] = points[i];
76 }
77
1/1
✓ Branch 1 taken 26081 times.
26081 hull.resize(k - 1);
78 26081 return hull;
79 26081 }
80
81 /// The point of `hull`'s boundary nearest `p`, and whether `p` lies inside a hull of 3 or more
82 /// points.
83 51839 std::pair<Vec2d, bool> NearestOnHull(const std::vector<Vec2d>& hull, const Vec2d& p) {
84
2/2
✓ Branch 0 taken 8495 times.
✓ Branch 1 taken 43344 times.
51839 if (hull.size() == 1) {
85 8495 return {hull[0], false};
86 }
87 43344 bool inside = hull.size() >= 3;
88 43344 Vec2d best = hull[0];
89 43344 double bestDistance = std::numeric_limits<double>::infinity();
90
2/2
✓ Branch 0 taken 112785 times.
✓ Branch 1 taken 43344 times.
156129 for (std::size_t i = 0; i < hull.size(); ++i) {
91
2/2
✓ Branch 0 taken 38635 times.
✓ Branch 1 taken 74150 times.
112785 const Vec2d& a = hull[i];
92 112785 const Vec2d& b = hull[(i + 1) % hull.size()];
93 112785 const double ex = b.x - a.x;
94 112785 const double ey = b.y - a.y;
95
2/2
✓ Branch 0 taken 38635 times.
✓ Branch 1 taken 74150 times.
112785 if (ex * (p.y - a.y) - ey * (p.x - a.x) < 0.0) {
96 38635 inside = false;
97 }
98 112785 const double t =
99
2/2
✓ Branch 0 taken 15105 times.
✓ Branch 1 taken 97680 times.
112785 std::clamp(((p.x - a.x) * ex + (p.y - a.y) * ey) / (ex * ex + ey * ey), 0.0, 1.0);
100 112785 const Vec2d q{a.x + t * ex, a.y + t * ey};
101 112785 const double d = std::hypot(p.x - q.x, p.y - q.y);
102
2/2
✓ Branch 0 taken 66918 times.
✓ Branch 1 taken 45867 times.
112785 if (d < bestDistance) {
103 66918 bestDistance = d;
104 66918 best = q;
105 }
106 }
107 43344 return {best, inside};
108 }
109
110 /// One tip of a settling shape: a turn by `angle` radians about the horizontal axis `axis`
111 /// through `origin`, the side `toward` (a horizontal unit vector square to it) going down.
112 struct PivotStep {
113 Vec3d origin;
114 Vec3d axis;
115 Vec3d toward;
116 double cs = 1.0;
117 double sn = 0.0;
118
119 /// Where the step turns `p`.
120 20134438 Vec3d Turn(const Vec3d& p) const {
121 20134438 const Vec3d up{0.0, 0.0, 1.0};
122 20134438 const Vec3d v = p - origin;
123 20134438 const double u = Dot(v, toward);
124 20134438 const double w = v.z;
125 20134438 return origin + axis * Dot(v, axis) + toward * (u * cs + w * sn) + up * (w * cs - u * sn);
126 }
127
128 /// The step's rotation as a row-major matrix.
129 43328 std::array<double, 9> Rotation() const {
130 43328 std::array<double, 9> m{};
131 43328 const std::array<double, 3> av{axis.x, axis.y, axis.z};
132 43328 const std::array<double, 3> dv{toward.x, toward.y, toward.z};
133 43328 const std::array<double, 3> zv{0.0, 0.0, 1.0};
134
2/2
✓ Branch 0 taken 129984 times.
✓ Branch 1 taken 43328 times.
173312 for (std::size_t r = 0; r < 3; ++r) {
135
2/2
✓ Branch 0 taken 389952 times.
✓ Branch 1 taken 129984 times.
519936 for (std::size_t k = 0; k < 3; ++k) {
136 389952 m[3 * r + k] = av[r] * av[k] + cs * (dv[r] * dv[k] + zv[r] * zv[k]) +
137 389952 sn * (dv[r] * zv[k] - zv[r] * dv[k]);
138 }
139 }
140 43328 return m;
141 }
142 };
143
144 /// The next tip of `points` resting on the plane through their lowest point toward `center`,
145 /// about the edge or corner of the contact nearest it, until another point touches; none once
146 /// the center stands over the contact, within `tolerance`.
147 51839 std::optional<PivotStep> NextPivot(const std::vector<Vec3d>& points, const Vec3d& center,
148 double tolerance) {
149 23697451 const double low = ZRange(points, [](const Vec3d& p) { return p; }).first;
150 51839 std::vector<Vec2d> contact;
151
2/2
✓ Branch 0 taken 23726520 times.
✓ Branch 1 taken 51839 times.
23778359 for (const Vec3d& p : points) {
152
2/2
✓ Branch 0 taken 121277 times.
✓ Branch 1 taken 23605243 times.
23726520 if (p.z <= low + tolerance) {
153
1/1
✓ Branch 1 taken 121277 times.
121277 contact.push_back({p.x, p.y});
154 }
155 }
156 51839 const Vec2d c{center.x, center.y};
157
4/4
✓ Branch 1 taken 51837 times.
✓ Branch 4 taken 51838 times.
✓ Branch 9 taken 43327 times.
✓ Branch 10 taken 8512 times.
51839 const auto [pivot, inside] = NearestOnHull(Hull2d(contact), c);
158 51839 const double distance = std::hypot(c.x - pivot.x, c.y - pivot.y);
159
3/4
✓ Branch 0 taken 43327 times.
✓ Branch 1 taken 8512 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 43328 times.
51839 if (inside || distance <= tolerance) {
160 8511 return std::nullopt;
161 }
162 43328 PivotStep step;
163 43328 step.toward = {(c.x - pivot.x) / distance, (c.y - pivot.y) / distance, 0.0};
164 43328 step.axis = {-step.toward.y, step.toward.x, 0.0};
165 43328 step.origin = {pivot.x, pivot.y, low};
166 43328 double rise = 0.0;
167 43328 double run = 0.0;
168
2/2
✓ Branch 0 taken 20182772 times.
✓ Branch 1 taken 43328 times.
20226100 for (const Vec3d& p : points) {
169
2/2
✓ Branch 0 taken 12403941 times.
✓ Branch 1 taken 7778831 times.
20182772 const double u = Dot(p - step.origin, step.toward);
170
6/6
✓ Branch 0 taken 12403941 times.
✓ Branch 1 taken 7778831 times.
✓ Branch 2 taken 12360684 times.
✓ Branch 3 taken 43257 times.
✓ Branch 4 taken 189529 times.
✓ Branch 5 taken 12171155 times.
20182772 if (u > tolerance && (run == 0.0 || (p.z - low) * run < rise * u)) {
171 232786 rise = p.z - low;
172 232786 run = u;
173 }
174 }
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43328 times.
43328 if (run == 0.0) {
176 ✗ return std::nullopt;
177 }
178 43328 const double angle = std::atan2(rise, run);
179 43328 step.cs = std::cos(angle);
180 43328 step.sn = std::sin(angle);
181 43328 return step;
182 51839 }
183
184 /// The rotation that tips `points` over on a level plane until they rest: each step pivots about
185 /// the edge or corner of the ground contact nearest the center of mass, toward it, until another
186 /// point touches, and stops once the center of mass stands over the contact.
187 8511 std::array<double, 9> SettleRotation(std::vector<Vec3d> points, Vec3d center) {
188 8511 RigidTransform total;
189 8511 double extent = 0.0;
190
2/2
✓ Branch 0 taken 3513479 times.
✓ Branch 1 taken 8511 times.
3521990 for (const Vec3d& p : points) {
191 3513479 extent =
192 3513479 std::fmax(extent, std::fmax(std::fabs(p.x), std::fmax(std::fabs(p.y), std::fabs(p.z))));
193 }
194 8511 const double tolerance = 1e-7 * extent;
195
1/2
✓ Branch 0 taken 51839 times.
✗ Branch 1 not taken.
51839 for (int n = 0; n < 64; ++n) {
196 51839 const std::optional<PivotStep> step = NextPivot(points, center, tolerance);
197
2/2
✓ Branch 0 taken 43327 times.
✓ Branch 1 taken 8511 times.
51838 if (!step) {
198 break;
199 }
200
2/2
✓ Branch 0 taken 20091362 times.
✓ Branch 1 taken 43327 times.
20134689 for (Vec3d& p : points) {
201 20091362 p = step->Turn(p);
202 }
203 43327 center = step->Turn(center);
204 43327 RigidTransform turn;
205 43327 turn.rotation = step->Rotation();
206 43327 total = turn.After(total);
207 }
208 8511 return total.rotation;
209 }
210
211 } // namespace
212
213 2450 RigidTransform TiltRotation(double tilt, double azimuth) {
214 2450 const double ax = -std::sin(azimuth);
215 2450 const double ay = std::cos(azimuth);
216 2450 const double c = std::cos(tilt);
217 2450 const double s = std::sin(tilt);
218 2450 const double t = 1.0 - c;
219 2450 RigidTransform turn;
220 2450 turn.rotation = {c + t * ax * ax, t * ax * ay, s * ay, t * ax * ay, c + t * ay * ay, -s * ax,
221 -s * ay, s * ax, c};
222 2450 return turn;
223 }
224
225 311 BoulderPlacement PlacementAtSite(const BoulderPlacement& placement, const Vec2d& boulder,
226 const Vec2d& site, const Surface& surface) {
227
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 61 times.
311 if (!surface.IsBody()) {
228 250 return placement;
229 }
230 61 const LocalFrame from = surface.Frame(boulder);
231 61 const LocalFrame to = surface.Frame(site);
232 61 std::array<double, 9> local{};
233
2/2
✓ Branch 0 taken 183 times.
✓ Branch 1 taken 61 times.
244 for (int r = 0; r < 3; ++r) {
234
2/2
✓ Branch 0 taken 549 times.
✓ Branch 1 taken 183 times.
732 for (int c = 0; c < 3; ++c) {
235 549 local[static_cast<std::size_t>(3 * r + c)] = Dot(Axis(to, r), Axis(from, c));
236 }
237 }
238 61 RigidTransform toSite;
239 61 toSite.rotation = local;
240 61 toSite.translation = to.Components(surface.Position(boulder) - surface.Position(site));
241 61 BoulderPlacement moved = placement;
242 61 moved.pose = toSite.After(placement.pose);
243 61 return moved;
244 }
245
246 17079 BoulderPlacement PlaceBoulderModel(const Boulder& boulder, const BoulderShape& shape,
247 double groundHeight, bool settle) {
248 17079 BoulderPlacement placement;
249 17079 placement.scale = {boulder.diameter, boulder.diameter, boulder.height / shape.shortAxis};
250 17079 placement.pose = RigidTransform::RotationZ(boulder.yaw)
251 17079 .After(RigidTransform::RotationX(boulder.lean))
252 17079 .After(RestRotation(boulder.rest));
253
3/4
✓ Branch 0 taken 8511 times.
✓ Branch 1 taken 8568 times.
✓ Branch 2 taken 8511 times.
✗ Branch 3 not taken.
17079 if (settle && !shape.mesh.triangles.empty()) {
254
1/1
✓ Branch 2 taken 8511 times.
17022 const std::vector<Vec3d> points = PlaceMesh(shape.mesh, placement).positions;
255 8511 RigidTransform rest;
256
2/2
✓ Branch 1 taken 8511 times.
✓ Branch 4 taken 8511 times.
8511 rest.rotation = SettleRotation(points, MassCenter(points, shape.mesh.triangles));
257
1/1
✓ Branch 1 taken 8511 times.
8511 placement.pose = rest.After(placement.pose);
258 8511 }
259
2/2
✓ Branch 0 taken 1292 times.
✓ Branch 1 taken 15787 times.
17079 if (boulder.tilt != 0.0) {
260 1292 placement.pose = TiltRotation(boulder.tilt, boulder.tiltAzimuth).After(placement.pose);
261 }
262 34158 const auto [low, high] =
263 9021539 ZRange(shape.mesh.positions, [&](const Vec3d& p) { return placement.Apply(p); });
264 17079 placement.pose.translation = {boulder.x, boulder.y,
265 17079 groundHeight - boulder.burial * (high - low) - low};
266 17079 return placement;
267 }
268
269 122790 std::optional<double> TopSurface(const TriangleMesh& placed, double x, double y) {
270 122790 std::optional<double> top;
271
2/2
✓ Branch 0 taken 53342458 times.
✓ Branch 1 taken 122790 times.
53465248 for (const auto& t : placed.triangles) {
272
2/2
✓ Branch 0 taken 18696441 times.
✓ Branch 1 taken 34646017 times.
53342458 const Vec3d& a = placed.positions[t[0]];
273 53342458 const Vec3d& b = placed.positions[t[1]];
274 53342458 const Vec3d& c = placed.positions[t[2]];
275 53342458 const double area = (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);
276
2/2
✓ Branch 0 taken 18696441 times.
✓ Branch 1 taken 34646017 times.
53342458 if (!(area > 0.0)) {
277 53342458 continue;
278 }
279 34646017 const double u = ((b.x - x) * (c.y - y) - (c.x - x) * (b.y - y)) / area;
280 34646017 const double v = ((c.x - x) * (a.y - y) - (a.x - x) * (c.y - y)) / area;
281 34646017 const double w = 1.0 - u - v;
282
6/6
✓ Branch 0 taken 21127640 times.
✓ Branch 1 taken 13518377 times.
✓ Branch 2 taken 13060280 times.
✓ Branch 3 taken 8067360 times.
✓ Branch 4 taken 13040874 times.
✓ Branch 5 taken 19406 times.
34646017 if (u < 0.0 || v < 0.0 || w < 0.0) {
283 34626611 continue;
284 }
285 19406 const double z = u * a.z + v * b.z + w * c.z;
286
4/4
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 19196 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 188 times.
19406 if (!top || z > *top) {
287 19218 top = z;
288 }
289 }
290 122790 return top;
291 }
292
293 22801 std::uint64_t ShapeSeed(const Boulder& boulder, std::uint32_t prototypes) {
294
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 22792 times.
22801 if (boulder.shapeSeed) {
295 9 return *boulder.shapeSeed;
296 }
297
2/2
✓ Branch 0 taken 22341 times.
✓ Branch 1 taken 451 times.
22792 return BoulderShapeSeed(boulder.parent != 0 ? boulder.parent : boulder.id, prototypes);
298 }
299
300 19635 TriangleMesh PlaceMesh(TriangleMesh mesh, const BoulderPlacement& placement) {
301
2/2
✓ Branch 0 taken 9725793 times.
✓ Branch 1 taken 19635 times.
9745428 for (Vec3d& p : mesh.positions) {
302 9725793 p = placement.Apply(p);
303 }
304 19635 return mesh;
305 }
306
307 } // namespace terraingen
308