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 | 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 | 2420756 | return IcqLabelIndex(q, i, j, face); | |
| 29 | } | ||
| 30 | |||
| 31 | /// Memberwise equality. | ||
| 32 | ✗ | bool operator==(const IcqModel& other) const { | |
| 33 | ✗ | 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 | 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 |