./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
187 / 190
98.4%
Branches:
140 / 146
95.9%
Functions:
23 / 23
100.0%

src/mesh/mesh_build.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/mesh/mesh_build.h"
4
5 #include "util/checks.h"
6
7 #include <algorithm>
8 #include <array>
9 #include <cstddef>
10 #include <cstdint>
11 #include <limits>
12 #include <stdexcept>
13 #include <string>
14 #include <type_traits>
15 #include <vector>
16
17 namespace terraingen {
18
19 namespace {
20
21 static_assert(std::is_standard_layout_v<Vec3d> && sizeof(Vec3d) == 3 * sizeof(double),
22 "BuildMesh fills Vec3d positions as packed x, y, z doubles");
23 static_assert(sizeof(std::array<std::uint32_t, 3>) == 3 * sizeof(std::int32_t),
24 "BuildMesh fills triangles as packed index triples");
25
26 constexpr std::int32_t kNoVertex = -1;
27
28 /// Whether the square of cells from (`col`, `row`) to the next column and row lies in a cutout.
29 113610 bool CutOut(const Raster<float>& heights, std::size_t col, std::size_t row,
30 const std::vector<Bounds2d>& cutouts) {
31 113610 const Vec2d a = heights.CellCenter(col, row);
32 113610 const Vec2d b = heights.CellCenter(col + 1, row + 1);
33 147076 return std::any_of(cutouts.begin(), cutouts.end(), [&](const Bounds2d& box) {
34 67390 const auto inside = [&](const Vec2d& p) {
35
8/8
✓ Branch 0 taken 33157 times.
✓ Branch 1 taken 767 times.
✓ Branch 2 taken 2243 times.
✓ Branch 3 taken 30914 times.
✓ Branch 4 taken 783 times.
✓ Branch 5 taken 1460 times.
✓ Branch 6 taken 202 times.
✓ Branch 7 taken 581 times.
33924 return p.x >= box.minX && p.x <= box.maxX && p.y >= box.minY && p.y <= box.maxY;
36 33466 };
37
5/6
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 33254 times.
✓ Branch 2 taken 123 times.
✓ Branch 3 taken 89 times.
✓ Branch 4 taken 123 times.
✗ Branch 5 not taken.
33466 return inside(a) && inside(b) && inside({a.x, b.y}) && inside({b.x, a.y});
38 113610 });
39 }
40
41 /// The squares of a grid that get two triangles, and the mesh they make.
42 19 struct MeshLayout {
43 std::size_t width = 0; ///< Cells per row.
44 std::size_t height = 0; ///< Rows.
45 /// 1 for each square, row-major over (width - 1) x (height - 1), whose four cells have data
46 /// and that lies outside the cutouts.
47 std::vector<unsigned char> squares;
48 MeshSize size; ///< What the mesh holds.
49
50 /// Whether the square from cell (`col`, `row`) is meshed; false outside the grid.
51 359426 bool Meshed(std::ptrdiff_t col, std::ptrdiff_t row) const {
52
4/4
✓ Branch 0 taken 357249 times.
✓ Branch 1 taken 2177 times.
✓ Branch 2 taken 356184 times.
✓ Branch 3 taken 1065 times.
359426 if (col < 0 || row < 0 || static_cast<std::size_t>(col) + 1 >= width ||
53
2/2
✓ Branch 0 taken 355080 times.
✓ Branch 1 taken 1104 times.
356184 static_cast<std::size_t>(row) + 1 >= height) {
54 return false;
55 }
56 355080 return squares[static_cast<std::size_t>(row) * (width - 1) +
57 355080 static_cast<std::size_t>(col)] != 0;
58 }
59 };
60
61 /// Calls `visit(a, b)` with the cell indices of each boundary edge `a` to `b` of the surface
62 /// triangles, in the order of the triangles and their edges, each edge as its triangle runs.
63 54 template <typename Visit> void ForEachBoundaryEdge(const MeshLayout& layout, const Visit& visit) {
64 54 const std::size_t width = layout.width;
65
4/4
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2} const&):
✓ Branch 0 taken 688 times.
✓ Branch 1 taken 35 times.
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1} const&):
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 19 times.
1127 for (std::size_t row = 0; row + 1 < layout.height; ++row) {
66
4/4
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2} const&):
✓ Branch 0 taken 37528 times.
✓ Branch 1 taken 688 times.
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1} const&):
✓ Branch 0 taken 20880 times.
✓ Branch 1 taken 385 times.
59481 for (std::size_t col = 0; col + 1 < width; ++col) {
67 58408 const auto c = static_cast<std::ptrdiff_t>(col);
68 58408 const auto r = static_cast<std::ptrdiff_t>(row);
69
4/4
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2} const&):
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 37402 times.
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1} const&):
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 20806 times.
58408 if (!layout.Meshed(c, r)) {
70 200 continue;
71 }
72 58208 const std::size_t nw = row * width + col;
73 58208 const std::size_t ne = nw + 1;
74 58208 const std::size_t sw = nw + width;
75 58208 const std::size_t se = sw + 1;
76
4/4
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2} const&):
✓ Branch 0 taken 733 times.
✓ Branch 1 taken 36669 times.
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1} const&):
✓ Branch 0 taken 412 times.
✓ Branch 1 taken 20394 times.
58208 if (!layout.Meshed(c - 1, r)) {
77 1145 visit(nw, sw);
78 }
79
4/4
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2} const&):
✓ Branch 0 taken 772 times.
✓ Branch 1 taken 36630 times.
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1} const&):
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 20374 times.
58208 if (!layout.Meshed(c, r + 1)) {
80 1204 visit(sw, se);
81 }
82
4/4
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2} const&):
✓ Branch 0 taken 733 times.
✓ Branch 1 taken 36669 times.
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1} const&):
✓ Branch 0 taken 412 times.
✓ Branch 1 taken 20394 times.
58208 if (!layout.Meshed(c + 1, r)) {
83 1145 visit(se, ne);
84 }
85
4/4
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::Layout(terraingen::Raster<float> const&, terraingen::BuildMeshOptions const&)::{lambda(unsigned long, unsigned long)#2} const&):
✓ Branch 0 taken 772 times.
✓ Branch 1 taken 36630 times.
void terraingen::(anonymous namespace)::ForEachBoundaryEdge<terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1}>(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::WriteSkirt(terraingen::(anonymous namespace)::MeshLayout const&, terraingen::(anonymous namespace)::SurfaceVertices const&, double, terraingen::Span<double>, terraingen::(anonymous namespace)::TriangleWriter&)::{lambda(unsigned long, unsigned long)#1} const&):
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 20374 times.
58208 if (!layout.Meshed(c, r - 1)) {
86 1204 visit(ne, nw);
87 }
88 }
89 }
90 54 }
91
92 /// The squares `BuildMesh` meshes and the size of its mesh.
93 /// @throws std::invalid_argument as `GetMeshSize`.
94 83 MeshLayout Layout(const Raster<float>& heights, const BuildMeshOptions& options) {
95 83 RequireNonNegative(options.skirt, "Height mesh skirt");
96 81 MeshLayout layout;
97 81 layout.width = heights.Width();
98 81 layout.height = heights.Height();
99
2/2
✓ Branch 0 taken 117768 times.
✓ Branch 1 taken 81 times.
117849 for (const float h : heights.Values()) {
100
4/4
✓ Branch 0 taken 68242 times.
✓ Branch 1 taken 49526 times.
✓ Branch 2 taken 68190 times.
✓ Branch 3 taken 52 times.
186010 layout.size.vertices += heights.IsNoData(h) ? 0u : 1u;
101 }
102 81 std::size_t meshed = 0;
103
3/4
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 77 times.
✗ Branch 3 not taken.
81 if (layout.width >= 2 && layout.height >= 2) {
104
1/1
✓ Branch 1 taken 77 times.
77 layout.squares.assign((layout.width - 1) * (layout.height - 1), 0);
105 454881 const auto data = [&](std::size_t col, std::size_t row) {
106
2/2
✓ Branch 1 taken 263752 times.
✓ Branch 2 taken 191052 times.
454804 return !heights.IsNoData(heights(col, row));
107 77 };
108
2/2
✓ Branch 0 taken 1882 times.
✓ Branch 1 taken 77 times.
1959 for (std::size_t row = 0; row + 1 < layout.height; ++row) {
109
2/2
✓ Branch 0 taken 113755 times.
✓ Branch 1 taken 1882 times.
115637 for (std::size_t col = 0; col + 1 < layout.width; ++col) {
110
6/6
✓ Branch 2 taken 113683 times.
✓ Branch 3 taken 31 times.
✓ Branch 5 taken 113652 times.
✓ Branch 6 taken 31 times.
✓ Branch 7 taken 113610 times.
✓ Branch 8 taken 42 times.
341121 if (data(col, row) && data(col + 1, row) && data(col, row + 1) &&
111
4/4
✓ Branch 0 taken 113714 times.
✓ Branch 1 taken 41 times.
✓ Branch 3 taken 113487 times.
✓ Branch 4 taken 123 times.
341017 data(col + 1, row + 1) && !CutOut(heights, col, row, options.cutouts)) {
112 113487 layout.squares[row * (layout.width - 1) + col] = 1;
113 113487 ++meshed;
114 }
115 }
116 }
117 }
118
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 76 times.
77 if (meshed == 0) {
119
1/1
✓ Branch 2 taken 5 times.
5 throw std::invalid_argument("A height mesh needs a square of four cells with data");
120 }
121 76 layout.size.triangles = 2 * meshed;
122
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 41 times.
76 if (options.skirt > 0.0) {
123
1/1
✓ Branch 1 taken 35 times.
40 std::vector<unsigned char> lowered(heights.Size(), 0);
124 35 ForEachBoundaryEdge(layout, [&](std::size_t a, std::size_t b) {
125 3010 layout.size.triangles += 2;
126
2/2
✓ Branch 0 taken 6020 times.
✓ Branch 1 taken 3010 times.
9030 for (const std::size_t cell : {a, b}) {
127
2/2
✓ Branch 0 taken 3010 times.
✓ Branch 1 taken 3010 times.
6020 layout.size.vertices += lowered[cell] == 0 ? 1u : 0u;
128 6020 lowered[cell] = 1;
129 }
130 3010 });
131 35 }
132 76 return layout;
133 5 }
134
135 /// Where a cell center at `height` lies in the frame of `BuildMesh`.
136 class SiteFrame {
137 public:
138 45 SiteFrame(const Surface& surface, const Vec2d& site)
139 45 : m_surface(surface), m_frame(surface.Frame(site)), m_origin(surface.Position(site)) {}
140
141 70522 Vec3d Place(const Vec2d& point, double height) const {
142
2/2
✓ Branch 0 taken 70264 times.
✓ Branch 1 taken 258 times.
70522 if (!m_surface.IsBody()) {
143 70264 return {point.x, point.y, height};
144 }
145 258 const LocalFrame at = m_surface.Frame(point);
146 258 return m_frame.Components(m_surface.Position(point) + at.up * height - m_origin);
147 }
148
149 private:
150 const Surface& m_surface;
151 LocalFrame m_frame;
152 Vec3d m_origin;
153 };
154
155 /// Throws unless `buffer` holds 3 * `count` values.
156
4/4
void terraingen::(anonymous namespace)::RequireTriples<double>(terraingen::Span<double>, unsigned long, char const*):
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
void terraingen::(anonymous namespace)::RequireTriples<int>(terraingen::Span<int>, unsigned long, char const*):
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 12 times.
34 template <typename T> void RequireTriples(Span<T> buffer, std::size_t count, const char* what) {
157
4/4
void terraingen::(anonymous namespace)::RequireTriples<double>(terraingen::Span<double>, unsigned long, char const*):
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
void terraingen::(anonymous namespace)::RequireTriples<int>(terraingen::Span<int>, unsigned long, char const*):
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 12 times.
34 if (buffer.size() != 3 * count) {
158
16/16
void terraingen::(anonymous namespace)::RequireTriples<double>(terraingen::Span<double>, unsigned long, char const*):
✓ Branch 2 taken 4 times.
✓ Branch 5 taken 4 times.
✓ Branch 8 taken 4 times.
✓ Branch 11 taken 4 times.
✓ Branch 14 taken 4 times.
✓ Branch 17 taken 4 times.
✓ Branch 20 taken 4 times.
✓ Branch 23 taken 4 times.
void terraingen::(anonymous namespace)::RequireTriples<int>(terraingen::Span<int>, unsigned long, char const*):
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 11 taken 3 times.
✓ Branch 14 taken 3 times.
✓ Branch 17 taken 3 times.
✓ Branch 20 taken 3 times.
✓ Branch 23 taken 3 times.
35 throw std::invalid_argument(std::string("A height mesh needs ") +
159 std::to_string(3 * count) + " " + what + ", not " +
160 std::to_string(buffer.size()));
161 }
162 27 }
163
164 /// The vertex of each cell with data, in row-major order, and how many there are.
165 45 struct SurfaceVertices {
166 std::vector<std::int32_t> index; ///< Per cell; `kNoVertex` for cells without data.
167 std::int32_t count = 0; ///< Vertices written.
168 };
169
170 /// Index triples written in order into a caller buffer.
171 struct TriangleWriter {
172 Span<std::int32_t> triangles;
173 std::size_t written = 0;
174
175 139462 void Add(std::int32_t a, std::int32_t b, std::int32_t c) {
176 139462 triangles[written] = a;
177 139462 triangles[written + 1] = b;
178 139462 triangles[written + 2] = c;
179 139462 written += 3;
180 139462 }
181 };
182
183 /// Writes a vertex at the center of every cell with data, placed in `frame`, into `positions`.
184 45 SurfaceVertices WriteVertices(const Raster<float>& heights, const SiteFrame& frame,
185 Span<double> positions) {
186
1/1
✓ Branch 1 taken 45 times.
45 SurfaceVertices vertices;
187
1/1
✓ Branch 1 taken 45 times.
45 vertices.index.assign(heights.Size(), kNoVertex);
188
2/2
✓ Branch 0 taken 1136 times.
✓ Branch 1 taken 45 times.
1181 for (std::size_t row = 0; row < heights.Height(); ++row) {
189
2/2
✓ Branch 0 taken 70552 times.
✓ Branch 1 taken 1136 times.
71688 for (std::size_t col = 0; col < heights.Width(); ++col) {
190 70552 const float h = heights(col, row);
191
4/4
✓ Branch 0 taken 34190 times.
✓ Branch 1 taken 36362 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 34160 times.
104742 if (heights.IsNoData(h)) {
192 30 continue;
193 }
194
1/1
✓ Branch 1 taken 70522 times.
70522 const Vec3d p = frame.Place(heights.CellCenter(col, row), static_cast<double>(h));
195 70522 const std::size_t at = 3 * static_cast<std::size_t>(vertices.count);
196 70522 positions[at] = p.x;
197 70522 positions[at + 1] = p.y;
198 70522 positions[at + 2] = p.z;
199 70522 vertices.index[row * heights.Width() + col] = vertices.count++;
200 }
201 }
202 45 return vertices;
203 }
204
205 /// Writes two triangles facing up for every meshed square of `layout`.
206 45 void WriteSurfaceTriangles(const MeshLayout& layout, const SurfaceVertices& vertices,
207 TriangleWriter& out) {
208 45 const std::size_t width = layout.width;
209
2/2
✓ Branch 0 taken 1091 times.
✓ Branch 1 taken 45 times.
1136 for (std::size_t row = 0; row + 1 < layout.height; ++row) {
210
2/2
✓ Branch 0 taken 68186 times.
✓ Branch 1 taken 1091 times.
69277 for (std::size_t col = 0; col + 1 < width; ++col) {
211
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 68043 times.
68186 if (!layout.Meshed(static_cast<std::ptrdiff_t>(col),
212 static_cast<std::ptrdiff_t>(row))) {
213 143 continue;
214 }
215 68043 const std::int32_t nw = vertices.index[row * width + col];
216 68043 const std::int32_t ne = vertices.index[row * width + col + 1];
217 68043 const std::int32_t sw = vertices.index[(row + 1) * width + col];
218 68043 const std::int32_t se = vertices.index[(row + 1) * width + col + 1];
219 68043 out.Add(nw, sw, se);
220 68043 out.Add(nw, se, ne);
221 }
222 }
223 45 }
224
225 /// Writes a vertex `skirt` meters below each boundary vertex, after the surface vertices, and two
226 /// triangles facing out below each boundary edge.
227 19 void WriteSkirt(const MeshLayout& layout, const SurfaceVertices& vertices, double skirt,
228 Span<double> positions, TriangleWriter& out) {
229 19 std::vector<std::int32_t> lowered(vertices.index.size(), kNoVertex);
230 19 std::int32_t next = vertices.count;
231 3395 const auto below = [&](std::size_t cell) {
232
2/2
✓ Branch 0 taken 1688 times.
✓ Branch 1 taken 1688 times.
3376 if (lowered[cell] == kNoVertex) {
233 1688 const auto from = 3 * static_cast<std::size_t>(vertices.index[cell]);
234 1688 const std::size_t to = 3 * static_cast<std::size_t>(next);
235 1688 positions[to] = positions[from];
236 1688 positions[to + 1] = positions[from + 1];
237 1688 positions[to + 2] = positions[from + 2] - skirt;
238 1688 lowered[cell] = next++;
239 }
240 3376 return lowered[cell];
241 19 };
242 19 ForEachBoundaryEdge(layout, [&](std::size_t a, std::size_t b) {
243 1688 const std::int32_t lowA = below(a);
244 1688 const std::int32_t lowB = below(b);
245 1688 out.Add(vertices.index[b], vertices.index[a], lowA);
246 1688 out.Add(vertices.index[b], lowA, lowB);
247 1688 });
248 19 }
249
250 /// Writes the mesh `layout` describes into buffers of its size.
251 /// @throws std::invalid_argument for a mesh of more than INT32_MAX vertices.
252 45 void FillFromLayout(const MeshLayout& layout, const Raster<float>& heights, Span<double> positions,
253 Span<std::int32_t> triangles, const Surface& surface, const Vec2d& site,
254 double skirt) {
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (layout.size.vertices > static_cast<std::size_t>(std::numeric_limits<std::int32_t>::max())) {
256
0/2
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✗ throw std::invalid_argument("A height mesh of " + std::to_string(layout.size.vertices) +
257
0/1
✗ Branch 2 not taken.
✗ " vertices has more than int32 indices can address");
258 }
259 45 const SurfaceVertices vertices = WriteVertices(heights, SiteFrame(surface, site), positions);
260 45 TriangleWriter out{triangles};
261 45 WriteSurfaceTriangles(layout, vertices, out);
262
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 26 times.
45 if (skirt > 0.0) {
263
1/1
✓ Branch 1 taken 19 times.
19 WriteSkirt(layout, vertices, skirt, positions, out);
264 }
265 45 }
266
267 } // namespace
268
269 27 MeshSize GetMeshSize(const Raster<float>& heights, const BuildMeshOptions& options) {
270 27 return Layout(heights, options).size;
271 }
272
273 20 void FillMesh(const Raster<float>& heights, Span<double> positions, Span<std::int32_t> triangles,
274 const Surface& surface, const Vec2d& site, const BuildMeshOptions& options) {
275 20 const MeshLayout layout = Layout(heights, options);
276
1/1
✓ Branch 1 taken 15 times.
19 RequireTriples(positions, layout.size.vertices, "position coordinates");
277
1/1
✓ Branch 1 taken 12 times.
15 RequireTriples(triangles, layout.size.triangles, "triangle indices");
278
1/1
✓ Branch 1 taken 12 times.
12 FillFromLayout(layout, heights, positions, triangles, surface, site, options.skirt);
279 12 }
280
281 36 TriangleMesh BuildMesh(const Raster<float>& heights, const Surface& surface, const Vec2d& site,
282 const BuildMeshOptions& options) {
283 36 const MeshLayout layout = Layout(heights, options);
284
1/1
✓ Branch 1 taken 33 times.
33 TriangleMesh mesh;
285
1/1
✓ Branch 1 taken 33 times.
33 mesh.positions.resize(layout.size.vertices);
286
1/1
✓ Branch 1 taken 33 times.
33 mesh.triangles.resize(layout.size.triangles);
287 ✗ FillFromLayout(
288 layout, heights,
289
1/1
✓ Branch 1 taken 33 times.
33 Span<double>(reinterpret_cast<double*>(mesh.positions.data()), 3 * layout.size.vertices),
290 33 Span<std::int32_t>(reinterpret_cast<std::int32_t*>(mesh.triangles.data()),
291 33 3 * layout.size.triangles),
292
1/1
✓ Branch 1 taken 33 times.
33 surface, site, options.skirt);
293 33 return mesh;
294 33 }
295
296 } // namespace terraingen
297