src/config/outputs.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief A configuration's outputs: their specs, fields, areas and grids, and checks. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "config/body.h" | ||
| 8 | #include "config/layer_context.h" | ||
| 9 | |||
| 10 | #include "terraingen/config/validate.h" | ||
| 11 | #include "terraingen/geometry/surface.h" | ||
| 12 | #include "terraingen/io/io_fields.h" | ||
| 13 | #include "terraingen/pipeline/materials_fields.h" | ||
| 14 | #include "terraingen/raster/geotiff/geotiff.h" | ||
| 15 | #include "terraingen/raster/grid.h" | ||
| 16 | #include "terraingen/raster/raster_fields.h" | ||
| 17 | |||
| 18 | #include <algorithm> | ||
| 19 | #include <array> | ||
| 20 | #include <cstddef> | ||
| 21 | #include <cstdint> | ||
| 22 | #include <optional> | ||
| 23 | #include <string> | ||
| 24 | #include <string_view> | ||
| 25 | #include <variant> | ||
| 26 | #include <vector> | ||
| 27 | |||
| 28 | namespace terraingen { | ||
| 29 | |||
| 30 | /// A box in surface coordinates, given by its `center` and `size` or by its `min` and `max` | ||
| 31 | /// corners, each an [x, y] pair. Surface coordinates are meters on a plane, and longitude and | ||
| 32 | /// latitude in degrees on a body. | ||
| 33 | 4 | struct ExtentSpec { | |
| 34 | std::optional<std::array<double, 2>> center{}; ///< [x, y] of the box's center. | ||
| 35 | std::optional<std::array<double, 2>> size{}; ///< [width, height], both positive. | ||
| 36 | std::optional<std::array<double, 2>> min{}; ///< [x, y] of the lower corner. | ||
| 37 | /// [x, y] of the upper corner, above `min` along both axes. | ||
| 38 | std::optional<std::array<double, 2>> max{}; | ||
| 39 | |||
| 40 | 1 | bool operator==(const ExtentSpec& other) const { | |
| 41 | 1 | return center == other.center && size == other.size && min == other.min && max == other.max; | |
| 42 | } | ||
| 43 | bool operator!=(const ExtentSpec& other) const { | ||
| 44 | return !(*this == other); | ||
| 45 | } | ||
| 46 | }; | ||
| 47 | |||
| 48 | /// The area an output covers and its resolution, snapped to a grid when its output is checked. | ||
| 49 | struct OutputAreaSpec { | ||
| 50 | /// Cell size in meters; on a body, meters along a meridian, the same in degrees of longitude. | ||
| 51 | std::optional<double> pixelSize{}; | ||
| 52 | std::optional<ExtentSpec> extentM{}; ///< The extent on a plane, in meters. | ||
| 53 | std::optional<ExtentSpec> extentDeg{}; ///< The extent on a body, in degrees. | ||
| 54 | }; | ||
| 55 | |||
| 56 | /// A list file to write; the format follows the extension (.json or else CSV) unless given. | ||
| 57 | /// | ||
| 58 | /// The list covers `area`, or the source's grid without an extent, or else the whole body on a | ||
| 59 | /// body; features are listed as a heightmap of `area.pixelSize` applies them. | ||
| 60 | 1375 | struct ListOutput { | |
| 61 | std::string path; ///< The file to write; empty to only return the list. | ||
| 62 | ListFormat format = ListFormat::Csv; | ||
| 63 | OutputAreaSpec area{}; | ||
| 64 | }; | ||
| 65 | |||
| 66 | /// A float32 GeoTIFF file to write over `area`, with the source's projection. | ||
| 67 | 7283 | struct GeoTiffOutput { | |
| 68 | std::string path; ///< The file to write; empty to only return the rasters. | ||
| 69 | TiffCompression compression = TiffCompression::Lzw; ///< None, Lzw or Deflate. | ||
| 70 | std::uint32_t tileSize = 256; ///< Pixels per tile side. | ||
| 71 | OutputAreaSpec area{}; | ||
| 72 | }; | ||
| 73 | |||
| 74 | /// Output "crater_list": writes the generated and manual craters. | ||
| 75 | 898 | struct CraterListOutput : ListOutput {}; | |
| 76 | |||
| 77 | /// Output "heightmap": writes the generated heights. | ||
| 78 | 5091 | struct HeightmapOutput : GeoTiffOutput {}; | |
| 79 | |||
| 80 | /// Output "boulder_list": writes every boulder the layers placed, models and relief alike. | ||
| 81 | 288 | struct BoulderListOutput : ListOutput {}; | |
| 82 | |||
| 83 | /// Output "boulder_models": writes the boulders placed as 3D models (`BoulderModelThreshold`) on | ||
| 84 | /// the ground beneath them, as `WriteBoulderModels` does, over the area a list would cover. | ||
| 85 | 194 | struct BoulderModelsOutput { | |
| 86 | std::string path; ///< The file to write; empty to only return the models. | ||
| 87 | double creaseAngle = 60.0; ///< Degrees between faces above which vertices are split. | ||
| 88 | OutputAreaSpec area{}; ///< As `ListOutput::area`. | ||
| 89 | bool instanced = true; ///< Shared shapes written once and drawn per boulder. | ||
| 90 | }; | ||
| 91 | |||
| 92 | /// Output "albedo": writes the albedo with one band, or three (red, green, blue). | ||
| 93 | 549 | struct AlbedoOutput : GeoTiffOutput {}; | |
| 94 | |||
| 95 | /// Output "mesh": the whole body, as a mesh (`BodyMesh`) with smooth normals in any format | ||
| 96 | /// `WriteMesh` writes, or as an ICQ shape model (`BodyIcq`) for an .icq path. | ||
| 97 | 421 | struct MeshOutput { | |
| 98 | std::string path; ///< The file to write; empty to only return the mesh. | ||
| 99 | std::size_t cellsPerFace = 128; ///< Quads along each edge of the six cube faces. | ||
| 100 | std::optional<double> spacing{}; ///< Sample spacing in meters; unset for a quad's side. | ||
| 101 | }; | ||
| 102 | |||
| 103 | /// Output "terrain_mesh": the heights over `area` as a mesh (`BuildMesh`) with smooth normals, in | ||
| 104 | /// any format `WriteMesh` writes and in the frame boulder models are written in. | ||
| 105 | 251 | struct TerrainMeshOutput { | |
| 106 | std::string path; ///< The file to write; empty to only return the mesh. | ||
| 107 | OutputAreaSpec area{}; | ||
| 108 | /// Boxes in surface coordinates whose quads are left out, such as where a finer mesh covers | ||
| 109 | /// the ground; a quad is left out when all four corners lie in one. | ||
| 110 | std::vector<ExtentSpec> cutouts{}; | ||
| 111 | /// Depth in meters of the skirt hung below every boundary edge, inner ones included, to hide | ||
| 112 | /// gaps between meshes; 0 for none. | ||
| 113 | double skirt = 0.0; | ||
| 114 | /// [longitude, latitude] in degrees of the frame's site on a body (`BuildMesh`); unset for the | ||
| 115 | /// grid's center. | ||
| 116 | std::optional<std::array<double, 2>> siteDeg{}; | ||
| 117 | }; | ||
| 118 | |||
| 119 | /// Output "materials": the share of each material class, one band per `MaterialClass` in its | ||
| 120 | /// order. | ||
| 121 | 225 | struct MaterialsOutput : GeoTiffOutput {}; | |
| 122 | |||
| 123 | /// Output "photometry": the Hapke albedo, b, c and roughness the materials mix to, in four bands. | ||
| 124 | 288 | struct PhotometryOutput : GeoTiffOutput {}; | |
| 125 | |||
| 126 | /// Output "report": a report of what the configuration generates (`GeneratedOutputs::report`), | ||
| 127 | /// written as JSON (`ReportToJson`). | ||
| 128 | 328 | struct ReportOutput { | |
| 129 | std::string path; ///< The file to write; empty to only return the report. | ||
| 130 | }; | ||
| 131 | |||
| 132 | /// An output, named in JSON by its "type". | ||
| 133 | using OutputSpec = std::variant<HeightmapOutput, CraterListOutput, BoulderListOutput, | ||
| 134 | BoulderModelsOutput, AlbedoOutput, MeshOutput, TerrainMeshOutput, | ||
| 135 | MaterialsOutput, PhotometryOutput, ReportOutput>; | ||
| 136 | |||
| 137 | 925 | template <typename IO> void Fields(IO& io, ExtentSpec& extent) { | |
| 138 | 925 | io("center", extent.center, "[x, y] of the box's center; give it with size."); | |
| 139 | 921 | io("size", extent.size, "[width, height] of the box; give it with center.", "", Above(0.0)); | |
| 140 | 918 | io("min", extent.min, "[x, y] of the box's lower corner; give it with max."); | |
| 141 | 918 | io("max", extent.max, "[x, y] of the box's upper corner, above min; give it with min."); | |
| 142 | 918 | } | |
| 143 | |||
| 144 | /// The fields of the area an output covers: its pixel size and its extent. | ||
| 145 | /// @param what What the pixel size is, such as "Cell size in meters of the file". | ||
| 146 | 1154 | template <typename IO> void AreaFields(IO& io, OutputAreaSpec& area, std::string_view what) { | |
| 147 | 2308 | io("pixel_size_m", area.pixelSize, | |
| 148 | 2302 | std::string(what) + | |
| 149 | "; on a body, meters along a meridian, the same in degrees of longitude. Unset for the " | ||
| 150 | "source's cell size; required without a raster or chain source.", | ||
| 151 | 1154 | "m", Above(0.0)); | |
| 152 | 1148 | io("extent_m", area.extentM, | |
| 153 | "Box the output covers on a plane in meters, snapped outward from its min corner to whole " | ||
| 154 | "cells; unset for the source's extent, required without a raster or chain source."); | ||
| 155 | 1140 | io("extent_deg", area.extentDeg, | |
| 156 | "As extent_m on a body, in degrees of longitude (x) and latitude (y)."); | ||
| 157 | 1139 | } | |
| 158 | |||
| 159 | /// The fields of heightmap and albedo outputs. | ||
| 160 | 748 | template <typename IO> void GeoTiffOutputFields(IO& io, GeoTiffOutput& output) { | |
| 161 | 1203 | io("path", output.path, config::OutputPathHelp("GeoTIFF file to write")); | |
| 162 | 748 | AreaFields(io, output.area, "Cell size in meters of the file"); | |
| 163 | 1181 | io("compression", output.compression, | |
| 164 | "Compression of the file; compressed files use the floating-point predictor."); | ||
| 165 | 1467 | io("tile_size_px", output.tileSize, | |
| 166 | "Width and height of the file's tiles in pixels; a multiple of " + | ||
| 167 | std::to_string(kTileSizeMultiple) + ".", | ||
| 168 | 733 | "px", AtLeast(kTileSizeMultiple)); | |
| 169 | 732 | } | |
| 170 | |||
| 171 | /// The fields of where features are listed: the area, whose pixel size is the sample spacing the | ||
| 172 | /// features are listed at, as a heightmap of that pixel size applies them. | ||
| 173 | 323 | template <typename IO> void FeatureAreaFields(IO& io, OutputAreaSpec& area) { | |
| 174 | 323 | AreaFields(io, area, | |
| 175 | "Sample spacing in meters the features are listed at, as a heightmap of that " | ||
| 176 | "pixel size applies them, and the cell size of their grid; without an extent or " | ||
| 177 | "a raster source, a list on a body covers the whole body at it"); | ||
| 178 | 322 | } | |
| 179 | |||
| 180 | /// The fields of crater and boulder list outputs. | ||
| 181 | 263 | template <typename IO> void ListOutputFields(IO& io, ListOutput& output) { | |
| 182 | 342 | io("path", output.path, config::OutputPathHelp("File to write")); | |
| 183 | 263 | io("format", output.format, | |
| 184 | "Format of the file; when not given, .json paths are written as JSON and others as CSV."); | ||
| 185 | 262 | FeatureAreaFields(io, output.area); | |
| 186 | 261 | } | |
| 187 | |||
| 188 | template <> struct SpecName<HeightmapOutput> { | ||
| 189 | static constexpr std::string_view kName = "heightmap"; | ||
| 190 | static constexpr std::string_view kDescription = | ||
| 191 | "Writes the generated heights as a float32 GeoTIFF, with a raster source's projection."; | ||
| 192 | }; | ||
| 193 | 550 | template <typename IO> void Fields(IO& io, HeightmapOutput& output) { | |
| 194 | 550 | GeoTiffOutputFields(io, output); | |
| 195 | 535 | } | |
| 196 | |||
| 197 | template <> struct SpecName<CraterListOutput> { | ||
| 198 | static constexpr std::string_view kName = "crater_list"; | ||
| 199 | static constexpr std::string_view kDescription = | ||
| 200 | "Writes the generated and manual craters as CSV or JSON."; | ||
| 201 | }; | ||
| 202 | 197 | template <typename IO> void Fields(IO& io, CraterListOutput& output) { | |
| 203 | 197 | ListOutputFields(io, output); | |
| 204 | 195 | } | |
| 205 | |||
| 206 | template <> struct SpecName<BoulderListOutput> { | ||
| 207 | static constexpr std::string_view kName = "boulder_list"; | ||
| 208 | static constexpr std::string_view kDescription = | ||
| 209 | "Writes every boulder the layers placed, models and relief alike, as CSV or JSON."; | ||
| 210 | }; | ||
| 211 | 66 | template <typename IO> void Fields(IO& io, BoulderListOutput& output) { | |
| 212 | 66 | ListOutputFields(io, output); | |
| 213 | 66 | } | |
| 214 | |||
| 215 | template <> struct SpecName<BoulderModelsOutput> { | ||
| 216 | static constexpr std::string_view kName = "boulder_models"; | ||
| 217 | static constexpr std::string_view kDescription = | ||
| 218 | "Writes the boulders placed as 3D models on the ground: as a glTF scene (.glb, or .gltf " | ||
| 219 | "with a .bin) drawing each shape once per boulder, as OBJ (.obj with an .mtl) with one " | ||
| 220 | "object per boulder, or as one merged STL mesh (.stl)."; | ||
| 221 | }; | ||
| 222 | 62 | template <typename IO> void Fields(IO& io, BoulderModelsOutput& output) { | |
| 223 | 76 | io("path", output.path, | |
| 224 | 62 | config::OutputPathHelp("File to write, ending in .glb, .gltf, .obj or .stl")); | |
| 225 | 62 | io("crease_angle_deg", output.creaseAngle, | |
| 226 | "Angle in degrees: vertices are split where faces meet at more than it, giving sharp " | ||
| 227 | "edges.", | ||
| 228 | 62 | "deg", Between(0.0, kMaxCreaseAngle)); | |
| 229 | 61 | io("instanced", output.instanced, | |
| 230 | "Writes each shared shape once and draws it per boulder (glTF instances, OBJ objects); " | ||
| 231 | "false writes every boulder as its own placed mesh, for tools without instancing."); | ||
| 232 | 61 | FeatureAreaFields(io, output.area); | |
| 233 | 61 | } | |
| 234 | |||
| 235 | template <> struct SpecName<AlbedoOutput> { | ||
| 236 | static constexpr std::string_view kName = "albedo"; | ||
| 237 | static constexpr std::string_view kDescription = | ||
| 238 | "Writes the albedo as a float32 GeoTIFF of one band or three (red, green, blue)."; | ||
| 239 | }; | ||
| 240 | 88 | template <typename IO> void Fields(IO& io, AlbedoOutput& output) { | |
| 241 | 88 | GeoTiffOutputFields(io, output); | |
| 242 | 87 | } | |
| 243 | |||
| 244 | template <> struct SpecName<MaterialsOutput> { | ||
| 245 | static constexpr std::string_view kName = "materials"; | ||
| 246 | static constexpr std::string_view kDescription = | ||
| 247 | "Writes the material map as a float32 GeoTIFF of five bands: the share of soil, sand, " | ||
| 248 | "dust, bedrock and rock at each cell."; | ||
| 249 | }; | ||
| 250 | 51 | template <typename IO> void Fields(IO& io, MaterialsOutput& output) { | |
| 251 | 51 | GeoTiffOutputFields(io, output); | |
| 252 | 51 | } | |
| 253 | |||
| 254 | template <> struct SpecName<PhotometryOutput> { | ||
| 255 | static constexpr std::string_view kName = "photometry"; | ||
| 256 | static constexpr std::string_view kDescription = | ||
| 257 | "Writes the Hapke parameters the materials mix to as a float32 GeoTIFF of four bands: " | ||
| 258 | "single-scattering albedo, b, c and roughness in degrees."; | ||
| 259 | }; | ||
| 260 | 59 | template <typename IO> void Fields(IO& io, PhotometryOutput& output) { | |
| 261 | 59 | GeoTiffOutputFields(io, output); | |
| 262 | 59 | } | |
| 263 | |||
| 264 | template <> struct SpecName<MeshOutput> { | ||
| 265 | static constexpr std::string_view kName = "mesh"; | ||
| 266 | static constexpr std::string_view kDescription = | ||
| 267 | "Writes the whole body, from six cube faces: a mesh as glTF (.glb or .gltf), OBJ (.obj) or " | ||
| 268 | "STL (.stl), or a Gaskell ICQ shape model in kilometers (.icq), whose Q is " | ||
| 269 | "cells_per_face."; | ||
| 270 | }; | ||
| 271 | 82 | template <typename IO> void Fields(IO& io, MeshOutput& output) { | |
| 272 | 105 | io("path", output.path, | |
| 273 | 82 | config::OutputPathHelp("File to write, ending in .glb, .gltf, .obj, .stl or .icq")); | |
| 274 | 82 | io("cells_per_face", output.cellsPerFace, | |
| 275 | "Quads along each edge of the six cube faces; more give a finer mesh, with two triangles " | ||
| 276 | "per quad.", | ||
| 277 | 82 | "", CubeFaceCellsRange()); | |
| 278 | 81 | io("spacing_m", output.spacing, | |
| 279 | "Sample spacing in meters the heights are evaluated at, which sets the finest features " | ||
| 280 | "kept; unset uses a quad's side at a face's center.", | ||
| 281 | 81 | "m", Above(0.0)); | |
| 282 | 80 | } | |
| 283 | |||
| 284 | template <> struct SpecName<TerrainMeshOutput> { | ||
| 285 | static constexpr std::string_view kName = "terrain_mesh"; | ||
| 286 | static constexpr std::string_view kDescription = | ||
| 287 | "Writes the generated heights as a triangle mesh, a vertex per cell with data, as glTF " | ||
| 288 | "(.glb or .gltf), OBJ (.obj) or STL (.stl), in the frame boulder models are written in."; | ||
| 289 | }; | ||
| 290 | 83 | template <typename IO> void Fields(IO& io, TerrainMeshOutput& output) { | |
| 291 | 105 | io("path", output.path, | |
| 292 | 83 | config::OutputPathHelp("File to write, ending in .glb, .gltf, .obj or .stl")); | |
| 293 | 83 | AreaFields(io, output.area, "Cell size in meters of the mesh, the spacing of its vertices"); | |
| 294 | 83 | io("cutouts", output.cutouts, | |
| 295 | "Boxes in surface coordinates whose quads, all four corners inside one, are left out, such " | ||
| 296 | "as where a finer mesh covers the ground."); | ||
| 297 | 83 | io("skirt_m", output.skirt, | |
| 298 | "Depth in meters of a skirt hung below every boundary edge, around cutouts too, hiding " | ||
| 299 | "gaps between meshes; 0 for none.", | ||
| 300 | 83 | "m", AtLeast(0.0)); | |
| 301 | 83 | io("site_deg", output.siteDeg, | |
| 302 | "[longitude, latitude] in degrees of the site on a body the mesh's frame is centered on; " | ||
| 303 | "unset for the grid's center. Only on a body.", | ||
| 304 | "deg"); | ||
| 305 | 83 | } | |
| 306 | |||
| 307 | template <> struct SpecName<ReportOutput> { | ||
| 308 | static constexpr std::string_view kName = "report"; | ||
| 309 | static constexpr std::string_view kDescription = | ||
| 310 | "A JSON report of what the configuration generates: height, slope, crater bin and " | ||
| 311 | "boulder statistics per evaluated grid, per-layer timings, backend, threads, seeds and " | ||
| 312 | "version."; | ||
| 313 | }; | ||
| 314 | 72 | template <typename IO> void Fields(IO& io, ReportOutput& output) { | |
| 315 | 85 | io("path", output.path, config::OutputPathHelp("File to write the report to")); | |
| 316 | 72 | } | |
| 317 | |||
| 318 | namespace config { | ||
| 319 | |||
| 320 | /// The box a checked extent describes: its `min` and `max` corners, or else `center` - `size` / 2 | ||
| 321 | /// to `center` + `size` / 2. | ||
| 322 | Bounds2d ExtentBox(const ExtentSpec& extent); | ||
| 323 | |||
| 324 | /// The area an output covers, or null for one without an area, a body mesh or a report. | ||
| 325 | const OutputAreaSpec* OutputArea(const OutputSpec& output); | ||
| 326 | |||
| 327 | /// Whether any of `outputs` is of the kinds `Kinds`. | ||
| 328 | 122 | template <typename... Kinds> bool HasOutput(const std::vector<OutputSpec>& outputs) { | |
| 329 | 122 | return std::any_of(outputs.begin(), outputs.end(), | |
| 330 | 185 | [](const OutputSpec& output) { return HoldsAny<Kinds...>(output); }); | |
| 331 | } | ||
| 332 | |||
| 333 | /// What a configuration offers its outputs. | ||
| 334 | struct OutputNeeds { | ||
| 335 | bool albedo = false; ///< Whether the configuration has an albedo. | ||
| 336 | bool materials = false; ///< Whether the configuration has a material map. | ||
| 337 | }; | ||
| 338 | |||
| 339 | /// Checks each output and the grid its area snaps to, and returns one record per output: its | ||
| 340 | /// extent snapped outward to whole cells of its pixel size (`RasterGrid::Covering`), or of | ||
| 341 | /// `source`'s, or `source` itself without either, and for a feature list on a body without an | ||
| 342 | /// extent or source the whole body at its pixel size. | ||
| 343 | /// @param source The source's grid; none without a raster or chain source. | ||
| 344 | /// @throws ConfigError under "outputs" for an invalid output. | ||
| 345 | std::vector<OutputGrid> CheckOutputs(const std::vector<OutputSpec>& outputs, | ||
| 346 | const OutputNeeds& needs, const Surface& surface, | ||
| 347 | const std::optional<RasterGrid>& source); | ||
| 348 | |||
| 349 | } // namespace config | ||
| 350 | |||
| 351 | } // namespace terraingen | ||
| 352 |