./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
151 / 151
100.0%
Branches:
119 / 129
92.2%
Functions:
13 / 13
100.0%

src/mesh/io/icq.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/mesh/io/icq.h"
4
5 #include "terraingen/mesh/shape_model.h"
6 #include "util/text.h"
7
8 #include <array>
9 #include <cmath>
10 #include <cstdint>
11 #include <cstdio>
12 #include <sstream>
13 #include <stdexcept>
14 #include <string>
15
16 namespace terraingen {
17
18 namespace {
19
20 58 void CheckModel(const IcqModel& model) {
21
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 57 times.
58 if (model.q == 0) {
22
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("An ICQ model needs Q > 0");
23 }
24 57 const std::size_t side = model.q + 1;
25
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 1 time.
57 if (model.vertices.size() != 6 * side * side) {
26
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("An ICQ model needs 6 (Q + 1)^2 vertices");
27 }
28
2/2
✓ Branch 0 taken 834784 times.
✓ Branch 1 taken 55 times.
834839 for (const Vec3d& v : model.vertices) {
29
4/6
✓ Branch 0 taken 834784 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 834783 times.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 834783 times.
834784 if (!(std::isfinite(v.x) && std::isfinite(v.y) && std::isfinite(v.z))) {
30
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("An ICQ model's vertices must be finite");
31 }
32 }
33 55 }
34
35 805212 double CrossSquared(const Vec3d& a, const Vec3d& b) {
36 805212 const Vec3d c = Cross(a, b);
37 805212 return Dot(c, c);
38 }
39
40 } // namespace
41
42 102 bool IsIcqPath(const std::filesystem::path& path) {
43 102 return HasExtension(path, {".icq"});
44 }
45
46 25 IcqModel ReadIcq(const std::filesystem::path& path, double metersPerUnit) {
47
1/1
✓ Branch 2 taken 24 times.
25 std::istringstream file(ReadFile(path));
48
1/1
✓ Branch 1 taken 24 times.
24 std::string line;
49 24 long long q = 0;
50
11/13
✓ Branch 1 taken 24 times.
✓ Branch 3 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 24 times.
✓ Branch 9 taken 24 times.
✓ Branch 11 taken 22 times.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 21 times.
✓ Branch 14 taken 1 time.
✓ Branch 15 taken 21 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 3 times.
✓ Branch 19 taken 21 times.
50 if (!std::getline(file, line) || !(std::istringstream(line) >> q) || q <= 0 || q > 1000000) {
51
3/3
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
9 throw std::runtime_error("ICQ file " + path.string() + " does not start with Q > 0");
52 }
53
1/1
✓ Branch 1 taken 21 times.
21 IcqModel model;
54 21 model.q = static_cast<std::size_t>(q);
55 21 const std::size_t count = 6 * (model.q + 1) * (model.q + 1);
56
1/1
✓ Branch 1 taken 21 times.
21 model.vertices.reserve(count);
57 std::size_t number = 1;
58
2/2
✓ Branch 0 taken 213591 times.
✓ Branch 1 taken 19 times.
213610 while (model.vertices.size() < count) {
59
3/3
✓ Branch 1 taken 213591 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 213590 times.
213591 if (!std::getline(file, line)) {
60
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
3 throw std::runtime_error("ICQ file " + path.string() + " has " +
61
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
4 std::to_string(model.vertices.size()) + " of " +
62
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
4 std::to_string(count) + " vertices");
63 }
64 213590 ++number;
65
1/1
✓ Branch 1 taken 213590 times.
213590 std::istringstream values(line);
66 213590 Vec3d v;
67
6/7
✓ Branch 1 taken 213590 times.
✓ Branch 4 taken 213590 times.
✓ Branch 7 taken 213590 times.
✓ Branch 9 taken 213589 times.
✓ Branch 10 taken 1 time.
✓ Branch 11 taken 213589 times.
✗ Branch 12 not taken.
213590 if (!(values >> v.x >> v.y >> v.z) ||
68
3/6
✓ Branch 0 taken 213589 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 213589 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 213589 times.
✗ Branch 5 not taken.
213589 !(std::isfinite(v.x) && std::isfinite(v.y) && std::isfinite(v.z))) {
69
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
3 throw std::runtime_error("ICQ file " + path.string() + " line " +
70
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
4 std::to_string(number) + " is not three finite numbers");
71 }
72
1/1
✓ Branch 1 taken 213589 times.
213589 model.vertices.push_back({v.x * metersPerUnit, v.y * metersPerUnit, v.z * metersPerUnit});
73 213590 }
74 38 return model;
75 26 }
76
77 20 void WriteIcq(const std::filesystem::path& path, const IcqModel& model, double metersPerUnit) {
78 20 CheckModel(model);
79
1/1
✓ Branch 1 taken 18 times.
18 std::string text;
80 18 std::array<char, 64> buffer{};
81
1/1
✓ Branch 1 taken 18 times.
18 std::snprintf(buffer.data(), buffer.size(), "%12zu\n", model.q);
82
1/1
✓ Branch 1 taken 18 times.
18 text += buffer.data();
83
2/2
✓ Branch 0 taken 411132 times.
✓ Branch 1 taken 18 times.
411150 for (const Vec3d& v : model.vertices) {
84 411132 std::snprintf(buffer.data(), buffer.size(), "%18.9f%18.9f%18.9f\n", v.x / metersPerUnit,
85
1/1
✓ Branch 1 taken 411132 times.
411132 v.y / metersPerUnit, v.z / metersPerUnit);
86
1/1
✓ Branch 1 taken 411132 times.
411132 text += buffer.data();
87 }
88
1/1
✓ Branch 1 taken 17 times.
18 WriteFile(path, text);
89 17 }
90
91 namespace {
92
93 /// For each stored vertex, the index of the one copy of it the mesh keeps: shared edge and corner
94 /// labels taken from one face, as Gaskell's converter does.
95 24 std::vector<std::size_t> SharedVertices(const IcqModel& model) {
96 24 const std::size_t q = model.q;
97 24 std::vector<std::size_t> n(model.vertices.size());
98
2/2
✓ Branch 0 taken 410754 times.
✓ Branch 1 taken 24 times.
410778 for (std::size_t k = 0; k < n.size(); ++k) {
99 410754 n[k] = k;
100 }
101 1310 const auto at = [&](std::size_t i, std::size_t j, std::size_t f) -> std::size_t& {
102 1310 return n[model.Index(i, j, f)];
103 };
104
2/2
✓ Branch 0 taken 643 times.
✓ Branch 1 taken 24 times.
667 for (std::size_t i = 1; i < q; ++i) {
105 643 at(i, q, 6) = at(q - i, q, 4);
106 643 at(i, 0, 6) = at(i, q, 2);
107 643 at(i, 0, 5) = at(q, q - i, 1);
108 643 at(i, 0, 4) = at(q - i, 0, 1);
109 643 at(i, 0, 3) = at(0, i, 1);
110 643 at(i, 0, 2) = at(i, q, 1);
111 }
112
2/2
✓ Branch 0 taken 643 times.
✓ Branch 1 taken 24 times.
667 for (std::size_t j = 1; j < q; ++j) {
113 643 at(q, j, 6) = at(j, q, 5);
114 643 at(q, j, 5) = at(0, j, 4);
115 643 at(q, j, 4) = at(0, j, 3);
116 643 at(q, j, 3) = at(0, j, 2);
117 643 at(0, j, 6) = at(q - j, q, 3);
118 643 at(0, j, 5) = at(q, j, 2);
119 }
120 24 at(0, 0, 3) = at(0, 0, 1);
121 24 at(q, 0, 4) = at(0, 0, 1);
122 24 at(0, 0, 2) = at(0, q, 1);
123 24 at(q, 0, 3) = at(0, q, 1);
124 24 at(0, 0, 4) = at(q, 0, 1);
125 24 at(q, 0, 5) = at(q, 0, 1);
126 24 at(0, 0, 5) = at(q, q, 1);
127 24 at(q, 0, 2) = at(q, q, 1);
128 24 at(0, 0, 6) = at(0, q, 2);
129 24 at(q, q, 3) = at(0, q, 2);
130 24 at(0, q, 5) = at(q, q, 2);
131 24 at(q, 0, 6) = at(q, q, 2);
132 24 at(q, q, 4) = at(0, q, 3);
133 24 at(0, q, 6) = at(0, q, 3);
134 24 at(q, q, 5) = at(0, q, 4);
135 24 at(q, q, 6) = at(0, q, 4);
136 24 return n;
137 }
138
139 } // namespace
140
141 25 TriangleMesh IcqToMesh(const IcqModel& model) {
142 25 CheckModel(model);
143 24 const std::size_t q = model.q;
144 24 const std::vector<std::size_t> shared = SharedVertices(model);
145 4026084 const auto at = [&](std::size_t i, std::size_t j, std::size_t f) {
146 4026060 return shared[model.Index(i, j, f)];
147 24 };
148
1/1
✓ Branch 1 taken 24 times.
24 TriangleMesh mesh;
149
2/2
✓ Branch 1 taken 24 times.
✓ Branch 4 taken 24 times.
24 std::vector<std::uint32_t> compact(model.vertices.size(), UINT32_MAX);
150 2415660 const auto vertex = [&](std::size_t i, std::size_t j, std::size_t f) {
151
2/2
✓ Branch 0 taken 402654 times.
✓ Branch 1 taken 2012982 times.
2415636 const std::size_t k = at(i, j, f);
152
2/2
✓ Branch 0 taken 402654 times.
✓ Branch 1 taken 2012982 times.
2415636 if (compact[k] == UINT32_MAX) {
153 402654 compact[k] = static_cast<std::uint32_t>(mesh.positions.size());
154 402654 mesh.positions.push_back(model.vertices[k]);
155 }
156 2415636 return compact[k];
157 24 };
158 1610448 const auto v = [&](std::size_t i, std::size_t j, std::size_t f) {
159 1610424 return model.vertices[at(i, j, f)];
160 24 };
161
1/1
✓ Branch 1 taken 24 times.
24 mesh.triangles.reserve(12 * q * q);
162
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 24 times.
168 for (std::size_t f = 1; f <= 6; ++f) {
163
2/2
✓ Branch 0 taken 4002 times.
✓ Branch 1 taken 144 times.
4146 for (std::size_t i = 0; i < q; ++i) {
164
2/2
✓ Branch 0 taken 402606 times.
✓ Branch 1 taken 4002 times.
406608 for (std::size_t j = 0; j < q; ++j) {
165 402606 const double z1 = CrossSquared(v(i, j, f), v(i + 1, j + 1, f));
166 402606 const double z2 = CrossSquared(v(i + 1, j, f), v(i, j + 1, f));
167
2/2
✓ Branch 0 taken 201287 times.
✓ Branch 1 taken 201319 times.
402606 if (z1 <= z2) {
168
1/1
✓ Branch 1 taken 201287 times.
201287 mesh.triangles.push_back(
169
3/3
✓ Branch 1 taken 201287 times.
✓ Branch 4 taken 201287 times.
✓ Branch 7 taken 201287 times.
201287 {vertex(i, j, f), vertex(i + 1, j + 1, f), vertex(i + 1, j, f)});
170
1/1
✓ Branch 1 taken 201287 times.
201287 mesh.triangles.push_back(
171
3/3
✓ Branch 1 taken 201287 times.
✓ Branch 4 taken 201287 times.
✓ Branch 7 taken 201287 times.
201287 {vertex(i, j, f), vertex(i, j + 1, f), vertex(i + 1, j + 1, f)});
172 } else {
173
1/1
✓ Branch 1 taken 201319 times.
201319 mesh.triangles.push_back(
174
3/3
✓ Branch 1 taken 201319 times.
✓ Branch 4 taken 201319 times.
✓ Branch 7 taken 201319 times.
201319 {vertex(i, j, f), vertex(i, j + 1, f), vertex(i + 1, j, f)});
175
1/1
✓ Branch 1 taken 201319 times.
201319 mesh.triangles.push_back(
176
3/3
✓ Branch 1 taken 201319 times.
✓ Branch 4 taken 201319 times.
✓ Branch 7 taken 201319 times.
201319 {vertex(i + 1, j, f), vertex(i, j + 1, f), vertex(i + 1, j + 1, f)});
177 }
178 }
179 }
180 }
181 24 return mesh;
182 24 }
183
184 13 RadialShape ToRadialShape(const IcqModel& model) {
185 13 CheckModel(model);
186 13 constexpr double kRadial = 1e-6;
187 13 std::vector<double> radii(model.vertices.size());
188 13 bool radial = true;
189
1/1
✓ Branch 1 taken 13 times.
13 ForEachIcqLabel(model.q, [&](std::size_t i, std::size_t j, std::size_t face, std::size_t k) {
190 12894 const Vec3d& v = model.vertices[k];
191 12894 const double r = Length(v);
192 12894 const Vec3d d = IcqDirection(model.q, i, j, face);
193
6/8
✓ Branch 0 taken 12409 times.
✓ Branch 1 taken 485 times.
✓ Branch 2 taken 12409 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12408 times.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 12408 times.
12894 radial = radial && r > 0.0 && Length(Cross(v, d)) <= kRadial * r && Dot(v, d) >= 0.0;
194 12894 radii[k] = r;
195 12894 });
196
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 12 times.
13 if (!radial) {
197
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
2 return ShapeModelFromMesh(IcqToMesh(model), model.q);
198 }
199
1/1
✓ Branch 1 taken 12 times.
12 return RadialShape(model.q, std::move(radii));
200 13 }
201
202 9 IcqModel ToIcq(const RadialShape& shape) {
203
1/1
✓ Branch 1 taken 9 times.
9 IcqModel model;
204
1/1
✓ Branch 1 taken 9 times.
9 model.q = shape.Q();
205
1/1
✓ Branch 1 taken 9 times.
9 model.vertices.resize(shape.Radii().size());
206
1/1
✓ Branch 1 taken 9 times.
9 ForEachIcqLabel(model.q, [&](std::size_t i, std::size_t j, std::size_t face, std::size_t k) {
207 10422 model.vertices[k] = IcqDirection(model.q, i, j, face) * shape.Radii()[k];
208 10422 });
209 9 return model;
210 }
211
212 } // namespace terraingen
213