src/config/sources.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Where a configuration's heights come from: source specs, their fields and building them. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "config/common.h" | ||
| 8 | |||
| 9 | #include "terraingen/geometry/surface.h" | ||
| 10 | #include "terraingen/sources/diamond_square.h" | ||
| 11 | #include "terraingen/sources/geotiff_source.h" | ||
| 12 | #include "terraingen/sources/source.h" | ||
| 13 | #include "terraingen/sources/source_chain.h" | ||
| 14 | #include "terraingen/sources/sources_fields.h" | ||
| 15 | #include "terraingen/sources/synthetic_source.h" | ||
| 16 | |||
| 17 | #include <array> | ||
| 18 | #include <filesystem> | ||
| 19 | #include <memory> | ||
| 20 | #include <optional> | ||
| 21 | #include <string> | ||
| 22 | #include <string_view> | ||
| 23 | #include <variant> | ||
| 24 | #include <vector> | ||
| 25 | |||
| 26 | namespace terraingen { | ||
| 27 | |||
| 28 | /// How a source's posts become heights between them and at sample spacings finer than them | ||
| 29 | /// (`Upsampler`): `type` names a kernel upsampler (`KernelUpsamplerNames`), "diamond_square", or a | ||
| 30 | /// type registered with `RegisterUpsampler`. In JSON a name alone, or an object with the "type" | ||
| 31 | /// and the type's settings. | ||
| 32 | 378 | struct UpsamplingSpec { | |
| 33 | std::string type = "cubic"; | ||
| 34 | DiamondSquareOptions diamondSquare{}; ///< The settings of "diamond_square"; meters. | ||
| 35 | /// A registered type's settings, a JSON object as text without the "type"; empty for none. | ||
| 36 | std::string settings{}; | ||
| 37 | }; | ||
| 38 | |||
| 39 | /// Source "raster": a heightmap read from a GeoTIFF, in its own projected coordinates | ||
| 40 | /// (`GeoTiffSource`), upsampled as `upsampling` says. Relative paths resolve against the | ||
| 41 | /// configuration's folder. | ||
| 42 | struct RasterSourceSpec { | ||
| 43 | std::string path; | ||
| 44 | /// How the band is read: band, scale, offset, crop and `effectivePixelSize` (meters); its | ||
| 45 | /// upsampler must stay unset, as `upsampling` gives it. | ||
| 46 | GeoTiffSourceOptions read{}; | ||
| 47 | UpsamplingSpec upsampling{}; | ||
| 48 | }; | ||
| 49 | |||
| 50 | /// Source "synthetic": `elevation` plus a stack of noise layers (`SyntheticSource`), evaluated at | ||
| 51 | /// every point, or with `pixelSize` sampled at posts that `upsampling` refines (cubic unset). | ||
| 52 | struct SyntheticSourceSpec { | ||
| 53 | double elevation = 0.0; ///< Meters. | ||
| 54 | /// 0 to `kMaxFractals` layers, by default `DefaultBaseNoise`; an empty stack is flat. | ||
| 55 | std::vector<FractalSpec> stack{DefaultBaseNoise()}; | ||
| 56 | std::optional<double> pixelSize{}; ///< Meters between posts; unset for none. | ||
| 57 | std::optional<UpsamplingSpec> upsampling{}; ///< Needs `pixelSize`. | ||
| 58 | }; | ||
| 59 | |||
| 60 | /// Source "shape_model": a "shape_model" body's own labels refined by `upsampling` | ||
| 61 | /// (`ShapeModelSource`), the source a shape-model body has unless the configuration gives one. | ||
| 62 | 442 | struct ShapeModelSourceSpec { | |
| 63 | UpsamplingSpec upsampling{}; | ||
| 64 | }; | ||
| 65 | |||
| 66 | struct ChainSourceSpec; | ||
| 67 | |||
| 68 | /// Where a terrain's heights come from, its source, named in JSON by its "type". | ||
| 69 | using SourceSpec = | ||
| 70 | std::variant<RasterSourceSpec, ChainSourceSpec, SyntheticSourceSpec, ShapeModelSourceSpec>; | ||
| 71 | |||
| 72 | /// Source "chain": `sources` tried in order, each filling where those before it have no data | ||
| 73 | /// (`SourceChain`), joined as `seams` say; its grid is the first source's. | ||
| 74 | 660 | struct ChainSourceSpec { | |
| 75 | std::vector<SourceSpec> sources; | ||
| 76 | ChainSeams seams{}; ///< `width` and `decayLength` in meters. | ||
| 77 | }; | ||
| 78 | |||
| 79 | /// Checks the ranges of an upsampling: diamond-square's settings when it is that type. | ||
| 80 | 132 | inline void CheckValueRanges(UpsamplingSpec& value, const std::string& path, | |
| 81 | std::string_view /*unit*/, const FieldRange& /*range*/) { | ||
| 82 | 132 | if (value.type == "diamond_square") { | |
| 83 | 26 | RangeChecker checker(path); | |
| 84 | 13 | Fields(checker, value.diamondSquare); | |
| 85 | 9 | } | |
| 86 | 128 | } | |
| 87 | |||
| 88 | /// The description of a source's upsampling. | ||
| 89 | inline constexpr std::string_view kUpsamplingHelp = | ||
| 90 | "How the posts are refined between them and at finer spacings: a kernel name such as cubic " | ||
| 91 | "or lanczos3, or an object with a \"type\" and its settings, such as {\"type\": " | ||
| 92 | "\"diamond_square\"} for fractal detail."; | ||
| 93 | |||
| 94 | template <> struct SpecName<RasterSourceSpec> { | ||
| 95 | static constexpr std::string_view kName = "raster"; | ||
| 96 | static constexpr std::string_view kDescription = | ||
| 97 | "A heightmap read from a GeoTIFF in its own projected coordinates, its cells upsampled to " | ||
| 98 | "finer spacings, to add relief to a real DEM."; | ||
| 99 | }; | ||
| 100 | 281 | template <typename IO> void Fields(IO& io, RasterSourceSpec& source) { | |
| 101 | 401 | io("path", source.path, config::InputPathHelp("GeoTIFF to read the heights from"), "", | |
| 102 | 281 | Required()); | |
| 103 | 281 | GeoTiffReadFields(io, source.read); | |
| 104 | 275 | io("upsampling", source.upsampling, kUpsamplingHelp); | |
| 105 | 265 | } | |
| 106 | |||
| 107 | template <> struct SpecName<ChainSourceSpec> { | ||
| 108 | static constexpr std::string_view kName = "chain"; | ||
| 109 | static constexpr std::string_view kDescription = | ||
| 110 | "Several sources tried in order, each filling where those before it have no data; its " | ||
| 111 | "grid is the first one's."; | ||
| 112 | }; | ||
| 113 | |||
| 114 | 109 | template <typename IO> void Fields(IO& io, ChainSourceSpec& source) { | |
| 115 | 109 | io("sources", source.sources, | |
| 116 | "Sources tried in order, each filling where those before it have no data, each upsampled " | ||
| 117 | "as it says; the chain's grid is the first one's.", | ||
| 118 | 109 | "", MinLength(1)); | |
| 119 | 107 | io("seams", source.seams, | |
| 120 | "How each source is matched to the sources before it where it fills their gaps; by " | ||
| 121 | "default each switches outright."); | ||
| 122 | 101 | } | |
| 123 | |||
| 124 | template <> struct SpecName<SyntheticSourceSpec> { | ||
| 125 | static constexpr std::string_view kName = "synthetic"; | ||
| 126 | static constexpr std::string_view kDescription = | ||
| 127 | "Heights generated from a stack of fractal noise layers over an unbounded plane or the " | ||
| 128 | "whole body; outputs give their own extents and pixel sizes."; | ||
| 129 | }; | ||
| 130 | 445 | template <typename IO> void Fields(IO& io, SyntheticSourceSpec& source) { | |
| 131 | 445 | io("elevation_m", source.elevation, "Height in meters added everywhere.", "m"); | |
| 132 | 890 | io("stack", source.stack, | |
| 133 | 890 | std::string(kNoiseStackHelp) + | |
| 134 | " Unset is one layer of fBm noise 400 m in wavelength and 12 m in amplitude; an empty " | ||
| 135 | "stack gives a flat source.", | ||
| 136 | 889 | "", NoiseStackLength(0)); | |
| 137 | 444 | io("pixel_size_m", source.pixelSize, | |
| 138 | "Spacing in meters of posts the noise is sampled at, as for a DEM of that cell size, " | ||
| 139 | "which upsampling refines. Unset evaluates the noise at every point, at any spacing.", | ||
| 140 | 888 | "m", Above(0.0)); | |
| 141 | 886 | io("upsampling", source.upsampling, | |
| 142 | 885 | std::string(kUpsamplingHelp) + " Needs pixel_size_m; unset is cubic."); | |
| 143 | 442 | } | |
| 144 | |||
| 145 | template <> struct SpecName<ShapeModelSourceSpec> { | ||
| 146 | static constexpr std::string_view kName = "shape_model"; | ||
| 147 | static constexpr std::string_view kDescription = | ||
| 148 | "The shape-model body's own labels refined by an upsampler; the default source on such a " | ||
| 149 | "body."; | ||
| 150 | }; | ||
| 151 | 50 | template <typename IO> void Fields(IO& io, ShapeModelSourceSpec& source) { | |
| 152 | 100 | io("upsampling", source.upsampling, std::string(kUpsamplingHelp) + " Unset is cubic."); | |
| 153 | 50 | } | |
| 154 | |||
| 155 | namespace config { | ||
| 156 | |||
| 157 | /// The source a spec describes, its lengths in the surface's units, each part checked before it | ||
| 158 | /// is built: a raster's file and grid, a chain's sources and seams, a synthetic stack and every | ||
| 159 | /// upsampling. Its grid is `BoundedGrid` of it: a raster's, a chain's first source's, none for | ||
| 160 | /// synthetic noise or a shape model. | ||
| 161 | /// @param path The spec's JSON path, which errors are reported under. | ||
| 162 | /// @throws ConfigError at `path`, or below it, for what the checks or the sources reject. | ||
| 163 | std::shared_ptr<const Source> BuildSource(const SourceSpec& spec, | ||
| 164 | const std::filesystem::path& directory, | ||
| 165 | const std::string& path, const Surface& surface); | ||
| 166 | |||
| 167 | /// Whether a source has posts that layers can estimate settings from: a raster, a chain whose | ||
| 168 | /// first source has, a shape model, or synthetic noise with a pixel size. | ||
| 169 | bool HasPosts(const SourceSpec& source); | ||
| 170 | |||
| 171 | /// Whether a source holds measured data whose resolution layers read for what it already holds | ||
| 172 | /// (`sourceResolvesPixels`): a raster or a shape model, or a chain holding one. | ||
| 173 | bool HasMeasuredData(const SourceSpec& source); | ||
| 174 | |||
| 175 | } // namespace config | ||
| 176 | |||
| 177 | } // namespace terraingen | ||
| 178 |