./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
37 / 37
100.0%
Branches:
33 / 35
94.3%
Functions:
5 / 5
100.0%

src/pipeline/body_mesh.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/pipeline/body_mesh.h"
4
5 #include "terraingen/geometry/geometry.h"
6 #include "terraingen/util/span.h"
7 #include "util/checks.h"
8
9 #include <cmath>
10 #include <stdexcept>
11 #include <string>
12 #include <vector>
13
14 namespace terraingen {
15
16 namespace {
17
18 /// Checks a whole-body request: a terrain on a body, at least one cell per face edge and a finite
19 /// spacing that is not negative.
20 /// @param what The result, starting the messages, such as "A body mesh".
21 /// @param cells The cells (or ICQ intervals) per face edge, and how they are called.
22 39 void CheckBodyRequest(const Terrain& terrain, std::size_t cells, double spacing,
23 const std::string& what, const std::string& cellsNeeded) {
24
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 36 times.
39 if (!terrain.Surface().IsBody()) {
25
2/2
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
6 throw std::invalid_argument(what + " needs a terrain on a body");
26 }
27
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 34 times.
36 if (cells == 0) {
28
3/3
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
4 throw std::invalid_argument(what + " needs " + cellsNeeded);
29 }
30 34 RequireNonNegative(spacing, "Sample spacing");
31 32 }
32
33 /// The body-fixed positions along `directions`: the body's radius in each direction plus the
34 /// terrain's height there, on the datum where the terrain has no data.
35 20 std::vector<Vec3d> BodyPositions(const Terrain& terrain, Span<const Vec3d> directions,
36 double spacing, unsigned threads) {
37 20 const Surface& surface = terrain.Surface();
38 20 std::vector<Vec2d> points(directions.size());
39
2/2
✓ Branch 0 taken 802776 times.
✓ Branch 1 taken 20 times.
802796 for (std::size_t k = 0; k < directions.size(); ++k) {
40
1/1
✓ Branch 2 taken 802776 times.
802776 points[k] = surface.Coordinates(directions[k]);
41 }
42
1/1
✓ Branch 1 taken 20 times.
20 std::vector<float> heights(points.size());
43
1/1
✓ Branch 3 taken 20 times.
20 terrain.HeightsAt(points, heights, spacing, threads);
44
1/1
✓ Branch 1 taken 20 times.
20 std::vector<Vec3d> positions(directions.size());
45
2/2
✓ Branch 0 taken 802776 times.
✓ Branch 1 taken 20 times.
802796 for (std::size_t k = 0; k < directions.size(); ++k) {
46
2/3
✓ Branch 1 taken 802776 times.
✓ Branch 3 taken 802776 times.
✗ Branch 4 not taken.
802776 const double r = surface.RadiusAt(points[k]) +
47
1/2
✓ Branch 0 taken 802776 times.
✗ Branch 1 not taken.
802776 (std::isnan(heights[k]) ? 0.0 : static_cast<double>(heights[k]));
48 802776 positions[k] = directions[k] * r;
49 }
50 20 return positions;
51 20 }
52
53 } // namespace
54
55 16 TriangleMesh BodyMesh(const Terrain& terrain, std::size_t cellsPerFace, double spacing,
56 unsigned threads) {
57
2/2
✓ Branch 2 taken 16 times.
✓ Branch 5 taken 12 times.
32 CheckBodyRequest(terrain, cellsPerFace, spacing, "A body mesh", "at least one cell per face");
58
1/1
✓ Branch 2 taken 12 times.
24 return IcqToMesh(BodyIcq(terrain, cellsPerFace, spacing, threads));
59 }
60
61 23 IcqModel BodyIcq(const Terrain& terrain, std::size_t q, double spacing, unsigned threads) {
62
4/4
✓ Branch 2 taken 23 times.
✓ Branch 5 taken 20 times.
✓ Branch 8 taken 14 times.
✓ Branch 9 taken 6 times.
46 CheckBodyRequest(terrain, q, spacing, "An ICQ model", "Q > 0");
63 20 const double sampled =
64
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 6 times.
20 spacing > 0.0 ? spacing : terrain.Surface().Radius() * kPi / 2.0 / static_cast<double>(q);
65
1/1
✓ Branch 1 taken 20 times.
20 IcqModel model;
66 20 model.q = q;
67
1/1
✓ Branch 1 taken 20 times.
20 std::vector<Vec3d> directions(6 * (q + 1) * (q + 1));
68
1/1
✓ Branch 1 taken 20 times.
20 ForEachIcqLabel(q, [&](std::size_t i, std::size_t j, std::size_t face, std::size_t k) {
69 802776 directions[k] = IcqDirection(q, i, j, face);
70 802776 });
71
1/1
✓ Branch 2 taken 20 times.
20 model.vertices = BodyPositions(terrain, directions, sampled, threads);
72 20 return model;
73 20 }
74
75 } // namespace terraingen
76