./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
72 / 72
100.0%
Branches:
44 / 52
84.6%
Functions:
12 / 12, 1 excl
100.0%

src/config/body.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief The body a configuration's terrain lies on: its specs, fields and surface.
5 #pragma once
6
7 #include "config/common.h"
8
9 #include "terraingen/geometry/surface.h"
10
11 #include <cstddef>
12 #include <filesystem>
13 #include <optional>
14 #include <string>
15 #include <string_view>
16 #include <variant>
17 #include <vector>
18
19 namespace terraingen {
20
21 /// Body "plane": a projected frame in meters, the default.
22 struct PlaneSurfaceSpec {};
23
24 /// Body "sphere": a body such as a planet or moon (`Surface`); grids and positions are longitude
25 /// and latitude in degrees, and features are generated over the whole body.
26 struct SphereSurfaceSpec {
27 double radius = 0.0; ///< Meters; must be given.
28 };
29
30 /// Body "ellipsoid": a body shaped as a triaxial ellipsoid (`Surface::Ellipsoid`), with
31 /// planetocentric longitude and latitude in degrees and heights along the direction from the
32 /// center.
33
9/9
✓ Branch 4 taken 25 times.
✓ Branch 6 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 11 taken 13 times.
✓ Branch 14 taken 1 time.
✓ Branch 16 taken 8 times.
✓ Branch 19 taken 1 time.
250 struct EllipsoidSurfaceSpec {
34 std::vector<double> radii; ///< Semi-axes a, b, c in meters along x, y and z; exactly three.
35 };
36
37 /// Body "shape_model": a star-shaped body from a shape model file (`Surface::ShapeModel`), an ICQ
38 /// model or a closed mesh around its origin in body-fixed coordinates, as a `RadialShape`.
39
30/38
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 6 times.
✓ Branch 7 taken 24 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 6 times.
✓ Branch 11 taken 1 time.
✗ Branch 14 not taken.
✓ Branch 15 taken 1 time.
✓ Branch 15 taken 1 time.
✓ Branch 15 taken 13 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 1 time.
✓ Branch 21 taken 8 times.
✓ Branch 22 taken 1 time.
✗ Branch 25 not taken.
✓ Branch 26 taken 1 time.
✓ Branch 29 taken 1 time.
✗ Branch 32 not taken.
✓ Branch 33 taken 1 time.
✓ Branch 36 taken 1 time.
✗ Branch 39 not taken.
✓ Branch 40 taken 1 time.
✓ Branch 43 taken 1 time.
✗ Branch 46 not taken.
✓ Branch 47 taken 1 time.
✓ Branch 50 taken 1 time.
✗ Branch 53 not taken.
✓ Branch 54 taken 1 time.
✓ Branch 57 taken 1 time.
✗ Branch 60 not taken.
✓ Branch 61 taken 1 time.
✓ Branch 76 taken 1 time.
✓ Branch 82 taken 1 time.
✓ Branch 85 taken 1 time.
✓ Branch 90 taken 1 time.
✓ Branch 93 taken 1 time.
✓ Branch 97 taken 1 time.
✓ Branch 103 taken 1 time.
354 struct ShapeModelSurfaceSpec {
40 std::string path;
41 /// Intervals per face edge of the radial grid; unset keeps an ICQ model's own Q, and uses 256
42 /// for a mesh.
43 std::optional<std::size_t> q{};
44 /// Meters per file unit; unset is 1000 for an ICQ model (kilometers) and 1 for a mesh.
45 std::optional<double> metersPerUnit{};
46 };
47
48 /// The body a terrain lies on, a plane or a whole body, named in JSON by its "type".
49 using SurfaceSpec =
50 std::variant<PlaneSurfaceSpec, SphereSurfaceSpec, EllipsoidSurfaceSpec, ShapeModelSurfaceSpec>;
51
52 /// The cells along a cube face edge a body mesh or shape model may have.
53 164 inline FieldRange CubeFaceCellsRange() {
54 106 return Between(1.0, static_cast<double>(kMaxCubeFaceCells));
55 }
56
57 template <> struct SpecName<PlaneSurfaceSpec> {
58 static constexpr std::string_view kName = "plane";
59 static constexpr std::string_view kDescription =
60 "A flat projected frame in meters, for local sites.";
61 };
62 template <typename IO> void Fields(IO& /*io*/, PlaneSurfaceSpec& /*spec*/) {}
63
64 template <> struct SpecName<SphereSurfaceSpec> {
65 static constexpr std::string_view kName = "sphere";
66 static constexpr std::string_view kDescription =
67 "A whole body such as a planet or moon: positions in longitude and latitude (degrees) and "
68 "features generated over the whole body.";
69 };
70 207 template <typename IO> void Fields(IO& io, SphereSurfaceSpec& spec) {
71 207 io("radius_m", spec.radius, "Radius of the body's datum sphere, in meters.", "m",
72 207 Above(0.0).Required());
73 198 }
74
75 template <> struct SpecName<EllipsoidSurfaceSpec> {
76 static constexpr std::string_view kName = "ellipsoid";
77 static constexpr std::string_view kDescription =
78 "A whole body shaped as a triaxial ellipsoid, such as an elongated moon or asteroid: "
79 "planetocentric longitude and latitude (degrees), heights along the direction from the "
80 "center, and features generated over the whole body.";
81 };
82 59 template <typename IO> void Fields(IO& io, EllipsoidSurfaceSpec& spec) {
83 59 io("radii_m", spec.radii,
84 "Semi-axes a, b and c of the datum ellipsoid in meters, along x (longitude 0), y "
85 "(longitude 90) and z (the north pole).",
86 59 "m", Above(0.0).Length(3).Required());
87 51 }
88
89 template <> struct SpecName<ShapeModelSurfaceSpec> {
90 static constexpr std::string_view kName = "shape_model";
91 static constexpr std::string_view kDescription =
92 "A whole star-shaped body such as an asteroid from a shape model, resampled along rays "
93 "from its origin; heights are radial, in longitude and latitude degrees.";
94 };
95 82 template <typename IO> void Fields(IO& io, ShapeModelSurfaceSpec& spec) {
96
5/5
void terraingen::Fields<terraingen::RangeChecker>(terraingen::RangeChecker&, terraingen::ShapeModelSurfaceSpec&):
✓ Branch 2 taken 31 times.
void terraingen::Fields<terraingen::config::(anonymous namespace)::SchemaWriter>(terraingen::config::(anonymous namespace)::SchemaWriter&, terraingen::ShapeModelSurfaceSpec&):
✓ Branch 1 taken 13 times.
void terraingen::Fields<terraingen::config::Reader>(terraingen::config::Reader&, terraingen::ShapeModelSurfaceSpec&):
✓ Branch 1 taken 8 times.
void terraingen::Fields<terraingen::config::Writer>(terraingen::config::Writer&, terraingen::ShapeModelSurfaceSpec&):
✓ Branch 1 taken 6 times.
✓ Branch 1 taken 24 times.
113 io("path", spec.path,
97 82 config::InputPathHelp("Shape model in body-fixed coordinates around its origin: a Gaskell "
98 "ICQ model (.icq) or a closed mesh (.obj, .gltf, .glb or .stl)"),
99 164 "", Required());
100 82 io("q", spec.q,
101 "Intervals per cube face edge of the radial grid the model is resampled on; unset keeps "
102 "an ICQ model's own Q and uses 256 for a mesh.",
103 82 "", CubeFaceCellsRange());
104 79 io("meters_per_unit", spec.metersPerUnit,
105 "Meters per unit of the file's coordinates; unset is 1000 for an ICQ model, whose "
106 "coordinates are kilometers, and 1 for a mesh.",
107 79 "m/unit", Above(0.0));
108 78 }
109
110 /// The `Surface` a body spec describes, its field ranges checked first; a shape model is loaded
111 /// once per file, modification time, Q and unit, so surfaces built from one spec compare equal.
112 /// @throws ConfigError at the field of "body" out of its range or empty, or at "body.path" for a
113 /// shape model that cannot be loaded.
114 Surface BuildSurface(const SurfaceSpec& spec, const std::filesystem::path& directory = {});
115
116 } // namespace terraingen
117