./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
9 / 11
81.8%
Branches:
16 / 22
72.7%
Functions:
2 / 2, 1 excl
100.0%

include/terraingen/mesh/io/icq.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Gaskell's implicitly connected quadrilateral (ICQ) shape models: reading, writing and
5 /// triangulating them.
6 #pragma once
7
8 #include "terraingen/geometry/radial_shape.h"
9 #include "terraingen/mesh/mesh.h"
10
11 #include <cstddef>
12 #include <filesystem>
13 #include <vector>
14
15 namespace terraingen {
16
17 /// A shape model in Gaskell's implicitly connected quadrilateral (ICQ) format, in meters: one
18 /// vertex per label (I, J, F) of the cube grid (`RadialShape::LabelDirection`), edge and corner
19 /// vertices stored once per face as in the files. R. W. Gaskell, "The Implicitly Connected
20 /// Quadrilateral Format",
21 /// https://sbnarchive.psi.edu/pds3/dawn/fc/DWNCSPC_4_01/DOCUMENT/ICQMODEL.ASC
22
7/7
✓ Branch 1 taken 29 times.
✓ Branch 5 taken 21 times.
✓ Branch 10 taken 1 time.
✓ Branch 17 taken 1 time.
✓ Branch 33 taken 1 time.
✓ Branch 50 taken 1 time.
✓ Branch 57 taken 9 times.
136 struct IcqModel {
23 std::size_t q = 0; ///< Q, the vertex grid's intervals per face edge.
24 std::vector<Vec3d> vertices; ///< 6 (Q + 1)^2 vertices in file order: F, then J, then I.
25
26 /// The index in `vertices` of v(`i`, `j`, `face`) (`IcqLabelIndex`).
27 4031203 std::size_t Index(std::size_t i, std::size_t j, std::size_t face) const {
28
7/7
✓ Branch 0 taken 402654 times.
✓ Branch 1 taken 2013282 times.
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
2420756 return IcqLabelIndex(q, i, j, face);
29 }
30
31 /// Memberwise equality.
32 ✗ bool operator==(const IcqModel& other) const {
33
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return q == other.q && vertices == other.vertices;
34 }
35 bool operator!=(const IcqModel& other) const {
36 return !(*this == other);
37 }
38 };
39
40 /// Whether `path` ends in .icq, in any case.
41 bool IsIcqPath(const std::filesystem::path& path);
42
43 /// The unit direction of ICQ label (`i`, `j`, `face`), as `RadialShape::LabelDirection`.
44 /// @throws std::invalid_argument as `RadialShape::LabelDirection`.
45 830299 inline Vec3d IcqDirection(std::size_t q, std::size_t i, std::size_t j, std::size_t face) {
46
2/6
✓ Branch 1 taken 300 times.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✓ Branch 27 taken 3528 times.
830191 return RadialShape::LabelDirection(q, i, j, face);
47 }
48
49 /// Reads an ICQ file: Q on the first line, then 6 (Q + 1)^2 lines of x, y and z (extra columns,
50 /// such as albedo, are ignored).
51 /// @param metersPerUnit Meters per file unit; Gaskell's models are in kilometers.
52 /// @throws std::runtime_error if the file cannot be read, or holds a bad Q, too few vertices or a
53 /// value that is not a finite number.
54 IcqModel ReadIcq(const std::filesystem::path& path, double metersPerUnit = 1000.0);
55
56 /// Writes `model` as an ICQ file.
57 /// @param metersPerUnit Meters per file unit; Gaskell's models are in kilometers.
58 /// @throws std::invalid_argument for a Q of 0, a vertex count other than 6 (Q + 1)^2 or a vertex
59 /// that is not finite.
60 /// @throws std::runtime_error if the file cannot be written.
61 void WriteIcq(const std::filesystem::path& path, const IcqModel& model,
62 double metersPerUnit = 1000.0);
63
64 /// The model as 12 Q^2 triangles over its 6 Q^2 + 2 distinct vertices, as Gaskell converts it:
65 /// each shared edge and corner vertex taken from one face, each quadrilateral split along its
66 /// shorter diagonal, triangles wound outward.
67 /// @throws std::invalid_argument as `WriteIcq`.
68 TriangleMesh IcqToMesh(const IcqModel& model);
69
70 /// The model as a radial shape of the same Q, resampled along rays (`ShapeModelFromMesh`) when
71 /// its vertices lie off their `IcqDirection`, as in Gaskell's models.
72 /// @throws std::invalid_argument as `WriteIcq`.
73 /// @throws std::runtime_error as `ShapeModelFromMesh`.
74 RadialShape ToRadialShape(const IcqModel& model);
75
76 /// The shape as an ICQ model: each label at its direction times its radius.
77 IcqModel ToIcq(const RadialShape& shape);
78
79 } // namespace terraingen
80