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 | 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 | 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 | 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 |