src/config/sources.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "config/sources.h" | ||
| 4 | |||
| 5 | #include "terraingen/sources/shape_model_source.h" | ||
| 6 | #include "terraingen/sources/upsampling.h" | ||
| 7 | #include "util/overloaded.h" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <utility> | ||
| 11 | |||
| 12 | namespace terraingen::config { | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | |||
| 16 | /// `seams` with their lengths, given in meters, in `surface`'s units. | ||
| 17 | 19 | ChainSeams SurfaceSeams(ChainSeams seams, const Surface& surface) { | |
| 18 | 19 | seams.width /= surface.UnitLength(); | |
| 19 | 19 | seams.decayLength /= surface.UnitLength(); | |
| 20 | 19 | return seams; | |
| 21 | } | ||
| 22 | |||
| 23 | /// The upsampler a spec names, its lengths in the surface's units: a kernel, diamond-square or a | ||
| 24 | /// type registered with `RegisterUpsampler`. | ||
| 25 | /// @throws ConfigError at `path` for an unknown type or settings its type rejects. | ||
| 26 | 109 | std::shared_ptr<const Upsampler> BuildUpsampler(const UpsamplingSpec& spec, const Surface& surface, | |
| 27 | const std::string& path) { | ||
| 28 | 109 | if (const std::optional<ResampleMethod> method = KernelUpsamplerMethod(spec.type)) { | |
| 29 | 88 | return std::make_shared<KernelUpsampler>(*method); | |
| 30 | } | ||
| 31 | 21 | if (spec.type == "diamond_square") { | |
| 32 | 9 | DiamondSquareOptions options = spec.diamondSquare; | |
| 33 | 9 | options.minPixelSize /= surface.UnitLength(); | |
| 34 | 18 | return At(path, [&]() -> std::shared_ptr<const Upsampler> { | |
| 35 | 9 | ValidateDiamondSquareOptions(options); | |
| 36 | 8 | return std::make_shared<DiamondSquareUpsampler>(options); | |
| 37 | 9 | }); | |
| 38 | } | ||
| 39 | 12 | if (IsUpsamplerRegistered(spec.type)) { | |
| 40 | 14 | return At(path, [&] { | |
| 41 | 14 | return MakeRegisteredUpsampler(spec.type, spec.settings.empty() ? "{}" : spec.settings); | |
| 42 | 7 | }); | |
| 43 | } | ||
| 44 | 5 | std::string expected; | |
| 45 | 35 | for (const std::string& name : KernelUpsamplerNames()) { | |
| 46 | 65 | expected += name + ", "; | |
| 47 | } | ||
| 48 | 5 | expected += "diamond_square"; | |
| 49 | 7 | for (const std::string& name : RegisteredUpsamplerNames()) { | |
| 50 | 4 | expected += ", " + name; | |
| 51 | 5 | } | |
| 52 | 10 | throw ConfigError(path, "unknown upsampling type \"" + spec.type + | |
| 53 | 20 | "\"; expected one of: " + expected); | |
| 54 | 5 | } | |
| 55 | |||
| 56 | /// A raster source: a path to a readable GeoTIFF with the band, in coordinates that suit the | ||
| 57 | /// surface, over a grid of a size a configuration may request. | ||
| 58 | 103 | std::shared_ptr<const Source> BuildRaster(const RasterSourceSpec& raster, | |
| 59 | const std::filesystem::path& directory, | ||
| 60 | const std::string& path, const Surface& surface) { | ||
| 61 | 103 | GeoTiffSourceOptions read = raster.read; | |
| 62 | 103 | if (read.upsampler) { | |
| 63 | 2 | throw ConfigError(FieldPath(path, "upsampling"), | |
| 64 | 3 | "gives the raster's upsampler; leave read.upsampler unset"); | |
| 65 | } | ||
| 66 | 126 | const std::string filePath = FieldPath(path, "path"); | |
| 67 | 102 | CheckNonEmpty(raster.path, filePath); | |
| 68 | 100 | const std::filesystem::path file = Resolve(raster.path, directory); | |
| 69 | 200 | const GeoTiffInfo info = AtFile(filePath, [&] { return ReadGeoTiffInfo(file); }); | |
| 70 | 97 | CheckBand(read.band, info, FieldPath(path, "band")); | |
| 71 | 95 | if (info.geoKeys.IsGeographic() && !surface.IsBody()) { | |
| 72 | 1 | throw ConfigError(filePath, | |
| 73 | "the raster's CRS is geographic (degrees); craters are sized in " | ||
| 74 | "meters, so reproject it first, for example with gdalwarp -t_srs, " | ||
| 75 | 2 | "or set the body to a sphere"); | |
| 76 | } | ||
| 77 | 94 | if (surface.IsBody() && !info.geoKeys.Empty() && !info.geoKeys.IsGeographic()) { | |
| 78 | 1 | throw ConfigError( | |
| 79 | 2 | filePath, "on a body the raster must be in longitude and latitude (a geographic CRS)"); | |
| 80 | } | ||
| 81 | 93 | if (read.effectivePixelSize) { | |
| 82 | 1 | *read.effectivePixelSize /= surface.UnitLength(); | |
| 83 | } | ||
| 84 | 186 | read.upsampler = BuildUpsampler(raster.upsampling, surface, FieldPath(path, "upsampling")); | |
| 85 | 105 | const std::string gridPath = read.crop ? FieldPath(path, "crop") : filePath; | |
| 86 | 88 | std::shared_ptr<const Source> source = | |
| 87 | 178 | At(gridPath, [&] { return std::make_shared<GeoTiffSource>(file, read); }); | |
| 88 | 81 | const RasterGrid grid = source->Grid(); | |
| 89 | 81 | CheckGridSize(grid.width, grid.height, gridPath); | |
| 90 | 79 | return source; | |
| 91 | 371 | } | |
| 92 | |||
| 93 | /// A chain of sources, each checked and built in turn, with a seam lattice of a size a | ||
| 94 | /// configuration may request after each source with bounds but the last. | ||
| 95 | 19 | std::shared_ptr<const Source> BuildChain(const ChainSourceSpec& chain, | |
| 96 | const std::filesystem::path& directory, | ||
| 97 | const std::string& path, const Surface& surface) { | ||
| 98 | 19 | const std::string sources = FieldPath(path, "sources"); | |
| 99 | 19 | const ChainSeams seams = SurfaceSeams(chain.seams, surface); | |
| 100 | 19 | const std::string reachPath = | |
| 101 | ✗ | FieldPath(FieldPath(path, "seams"), | |
| 102 | 42 | seams.mode == ChainSeamMode::Feather ? "width_m" : "decay_length_m"); | |
| 103 | 19 | std::vector<std::shared_ptr<const Source>> members; | |
| 104 | 46 | for (std::size_t n = 0; n < chain.sources.size(); ++n) { | |
| 105 | 66 | members.push_back(BuildSource(chain.sources[n], directory, ItemPath(sources, n), surface)); | |
| 106 | 29 | const std::optional<RasterGrid> grid = BoundedGrid(*members.back()); | |
| 107 | 29 | if (grid && seams.mode != ChainSeamMode::None && n + 1 < chain.sources.size()) { | |
| 108 | 14 | const RasterGrid lattice = At(reachPath, [&] { return SeamLattice(*grid, seams); }); | |
| 109 | 7 | CheckGridSize(lattice.width, lattice.height, reachPath); | |
| 110 | } | ||
| 111 | } | ||
| 112 | 26 | return At(path, [&] { return std::make_shared<SourceChain>(std::move(members), seams); }); | |
| 113 | 25 | } | |
| 114 | |||
| 115 | /// Synthetic noise: its stack, and upsampling only with a pixel size. | ||
| 116 | 370 | std::shared_ptr<const Source> BuildSynthetic(const SyntheticSourceSpec& synthetic, | |
| 117 | const std::string& path, const Surface& surface) { | ||
| 118 | 370 | CheckNoiseStack(synthetic.stack, FieldPath(path, "stack")); | |
| 119 | 370 | std::shared_ptr<const Upsampler> upsampler; | |
| 120 | 370 | if (synthetic.upsampling) { | |
| 121 | 8 | const std::string at = FieldPath(path, "upsampling"); | |
| 122 | 8 | if (!synthetic.pixelSize) { | |
| 123 | 4 | throw ConfigError(at, "needs pixel_size_m: only noise sampled at posts is upsampled"); | |
| 124 | } | ||
| 125 | 13 | upsampler = BuildUpsampler(*synthetic.upsampling, surface, at); | |
| 126 | 8 | } | |
| 127 | 370 | return At(path, [&] { | |
| 128 | ✗ | return std::make_shared<SyntheticSource>(StackFractals(synthetic.stack), | |
| 129 | 734 | static_cast<float>(synthetic.elevation), | |
| 130 | 734 | synthetic.pixelSize, upsampler, surface); | |
| 131 | 367 | }); | |
| 132 | 367 | } | |
| 133 | |||
| 134 | /// A shape model body's own labels, refined by the spec's upsampler. | ||
| 135 | 10 | std::shared_ptr<const Source> BuildShapeModel(const ShapeModelSourceSpec& shape, | |
| 136 | const std::string& path, const Surface& surface) { | ||
| 137 | 10 | const std::shared_ptr<const Upsampler> upsampler = | |
| 138 | 10 | BuildUpsampler(shape.upsampling, surface, FieldPath(path, "upsampling")); | |
| 139 | 20 | return At(path, [&] { return std::make_shared<ShapeModelSource>(surface, upsampler); }); | |
| 140 | 8 | } | |
| 141 | |||
| 142 | } // namespace | ||
| 143 | |||
| 144 | 502 | std::shared_ptr<const Source> BuildSource(const SourceSpec& spec, | |
| 145 | const std::filesystem::path& directory, | ||
| 146 | const std::string& path, const Surface& surface) { | ||
| 147 | 502 | using Result = std::shared_ptr<const Source>; | |
| 148 | 969 | return std::visit(Overloaded{ | |
| 149 | 103 | [&](const RasterSourceSpec& s) -> Result { | |
| 150 | 103 | return BuildRaster(s, directory, path, surface); | |
| 151 | }, | ||
| 152 | 19 | [&](const ChainSourceSpec& s) -> Result { | |
| 153 | 19 | return BuildChain(s, directory, path, surface); | |
| 154 | }, | ||
| 155 | 370 | [&](const SyntheticSourceSpec& s) -> Result { | |
| 156 | 370 | return BuildSynthetic(s, path, surface); | |
| 157 | }, | ||
| 158 | 10 | [&](const ShapeModelSourceSpec& s) -> Result { | |
| 159 | 10 | return BuildShapeModel(s, path, surface); | |
| 160 | }, | ||
| 161 | }, | ||
| 162 | 502 | spec); | |
| 163 | } | ||
| 164 | |||
| 165 | 369 | bool HasPosts(const SourceSpec& source) { | |
| 166 | 369 | return std::visit(Overloaded{ | |
| 167 | [](const RasterSourceSpec&) { return true; }, | ||
| 168 | [](const ShapeModelSourceSpec&) { return true; }, | ||
| 169 | 19 | [](const ChainSourceSpec& s) { | |
| 170 | 19 | return !s.sources.empty() && HasPosts(s.sources.front()); | |
| 171 | }, | ||
| 172 | 242 | [](const SyntheticSourceSpec& s) { return s.pixelSize.has_value(); }, | |
| 173 | }, | ||
| 174 | 369 | source); | |
| 175 | } | ||
| 176 | |||
| 177 | 333 | bool HasMeasuredData(const SourceSpec& source) { | |
| 178 | 333 | return std::visit(Overloaded{ | |
| 179 | [](const RasterSourceSpec&) { return true; }, | ||
| 180 | [](const SyntheticSourceSpec&) { return false; }, | ||
| 181 | [](const ShapeModelSourceSpec&) { return true; }, | ||
| 182 | 13 | [](const ChainSourceSpec& s) { | |
| 183 | 13 | return std::any_of(s.sources.begin(), s.sources.end(), | |
| 184 | 13 | HasMeasuredData); | |
| 185 | }, | ||
| 186 | }, | ||
| 187 | 333 | source); | |
| 188 | } | ||
| 189 | |||
| 190 | } // namespace terraingen::config | ||
| 191 |