include/terraingen/sources/upsampling.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Upsamplers: how a source's posts become heights at any point and sample spacing. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/geometry/geometry.h" | ||
| 8 | #include "terraingen/raster/cell_loops.h" | ||
| 9 | #include "terraingen/raster/geo_transform.h" | ||
| 10 | #include "terraingen/raster/grid.h" | ||
| 11 | #include "terraingen/raster/raster.h" | ||
| 12 | #include "terraingen/raster/resample.h" | ||
| 13 | #include "terraingen/sources/sample_block.h" | ||
| 14 | #include "terraingen/util/span.h" | ||
| 15 | |||
| 16 | #include <cstddef> | ||
| 17 | #include <cstdint> | ||
| 18 | #include <functional> | ||
| 19 | #include <memory> | ||
| 20 | #include <optional> | ||
| 21 | #include <string> | ||
| 22 | #include <vector> | ||
| 23 | |||
| 24 | namespace terraingen { | ||
| 25 | |||
| 26 | /// The posts of a source: heights on a regular lattice, such as a DEM's cells or noise sampled at | ||
| 27 | /// a fixed spacing. Implementations must be thread-safe. | ||
| 28 | 452 | class PostLattice { | |
| 29 | public: | ||
| 30 | 300 | virtual ~PostLattice() = default; | |
| 31 | |||
| 32 | /// Where the posts lie: post (col, row) is at the center of cell (col, row) of a grid with | ||
| 33 | /// this transform. | ||
| 34 | virtual GeoTransform Transform() const = 0; | ||
| 35 | |||
| 36 | /// The posts held, which an upsampler may estimate settings from; none for a lattice without | ||
| 37 | /// bounds, which holds a post everywhere. | ||
| 38 | 2 | virtual std::optional<PixelWindow> Extent() const { | |
| 39 | 2 | return std::nullopt; | |
| 40 | } | ||
| 41 | |||
| 42 | /// Reads posts [`col0`, `col0` + `width`) x [`row0`, `row0` + `height`) row by row into | ||
| 43 | /// `heights`, of `width` x `height` values, NaN where a post has no data. Posts beyond a | ||
| 44 | /// bounded lattice repeat its nearest edge post. | ||
| 45 | virtual void Read(std::int64_t col0, std::int64_t row0, std::size_t width, std::size_t height, | ||
| 46 | Span<float> heights) const = 0; | ||
| 47 | }; | ||
| 48 | |||
| 49 | /// Where a raster that is a window of a larger one, such as a window read from a file, lies in | ||
| 50 | /// it. | ||
| 51 | struct RasterPlacement { | ||
| 52 | RasterGrid whole{}; ///< The larger raster's grid. | ||
| 53 | std::size_t col = 0; ///< The window's first column in it. | ||
| 54 | std::size_t row = 0; ///< The window's first row in it. | ||
| 55 | |||
| 56 | /// A raster placed in itself. | ||
| 57 | 139 | static RasterPlacement Whole(const Raster<float>& raster) { | |
| 58 | 139 | return {raster.Grid(), 0, 0}; | |
| 59 | } | ||
| 60 | }; | ||
| 61 | |||
| 62 | /// The posts of a raster that is a window of a larger lattice, such as a window read from a | ||
| 63 | /// file: posts beyond the larger lattice repeat its edge, and those beyond the window its edge, so | ||
| 64 | /// reads within the window give the same values as the whole. | ||
| 65 | 156 | class RasterPosts final : public PostLattice { | |
| 66 | public: | ||
| 67 | /// @param window A non-empty, unrotated raster; its no-data cells read as NaN. | ||
| 68 | /// @param placement Where `window` lies in the whole lattice. | ||
| 69 | /// @param wrapColumns Whether columns repeat with the lattice's width, as longitudes around a | ||
| 70 | /// body do, rather than repeating the edge posts; `window` then spans every column. | ||
| 71 | /// @throws std::invalid_argument for a null or empty window, or a wrapping one that does not | ||
| 72 | /// span every column. | ||
| 73 | RasterPosts(std::shared_ptr<const Raster<float>> window, const RasterPlacement& placement, | ||
| 74 | bool wrapColumns = false); | ||
| 75 | |||
| 76 | /// The whole lattice's transform. | ||
| 77 | 186824 | GeoTransform Transform() const override { | |
| 78 | 186824 | return m_placement.whole.transform; | |
| 79 | } | ||
| 80 | |||
| 81 | /// The window's cells in the whole lattice. | ||
| 82 | std::optional<PixelWindow> Extent() const override; | ||
| 83 | |||
| 84 | void Read(std::int64_t col0, std::int64_t row0, std::size_t width, std::size_t height, | ||
| 85 | Span<float> heights) const override; | ||
| 86 | |||
| 87 | private: | ||
| 88 | std::shared_ptr<const Raster<float>> m_window; | ||
| 89 | RasterPlacement m_placement; | ||
| 90 | bool m_wrapColumns = false; | ||
| 91 | }; | ||
| 92 | |||
| 93 | /// The spacing of a lattice's posts: the longer side of a cell of `transform`, in its units. | ||
| 94 | double PostSpacing(const GeoTransform& transform); | ||
| 95 | |||
| 96 | /// Whether `spacing` is no finer than the posts of a lattice placed by `transform`, to within | ||
| 97 | /// rounding, measuring a post's cell as grids measure theirs (`RasterGrid::PixelSize`). | ||
| 98 | bool AtPostSpacing(const GeoTransform& transform, double spacing); | ||
| 99 | |||
| 100 | /// How a source's posts become heights at any point and at any sample spacing in the posts' | ||
| 101 | /// units: interpolation, or refinement that adds detail as the spacing shrinks. Heights must | ||
| 102 | /// depend only on the posts, the position and the spacing, and implementations must be | ||
| 103 | /// thread-safe. | ||
| 104 | /// | ||
| 105 | /// A post keeps its own height at the posts' spacing or coarser, so an upsampler only decides | ||
| 106 | /// heights off the posts and below their spacing, in `FillSamples`, the one method it evaluates. | ||
| 107 | 372 | class Upsampler { | |
| 108 | public: | ||
| 109 | 318 | virtual ~Upsampler() = default; | |
| 110 | |||
| 111 | /// Sets every sample of `samples` to its height from `posts` at a sample spacing of | ||
| 112 | /// `spacing`, NaN where it has none: a post's own where the sample is centered on one and the | ||
| 113 | /// spacing is no finer than the posts (`AtPostSpacing`), otherwise `FillSamples`'. A grid on | ||
| 114 | /// the posts' lattice at their spacing or coarser copies them. | ||
| 115 | /// @param threads Threads to fill a grid over; 0 for every hardware thread. | ||
| 116 | void Fill(const PostLattice& posts, SampleBlock& samples, double spacing, | ||
| 117 | unsigned threads) const; | ||
| 118 | |||
| 119 | /// `Fill` of every cell of `heights`, placed by its transform. | ||
| 120 | /// @param threads Threads to fill over; 0 for every hardware thread. | ||
| 121 | void Fill(const PostLattice& posts, Raster<float>& heights, double spacing, | ||
| 122 | unsigned threads) const; | ||
| 123 | |||
| 124 | /// The height `Fill` gives a cell centered at world (`x`, `y`), to the same bits. | ||
| 125 | float HeightAt(const PostLattice& posts, double x, double y, double spacing) const; | ||
| 126 | |||
| 127 | /// Sets each of `heights` to `HeightAt` the world point of `points` at its index, to the same | ||
| 128 | /// bits, in one `Fill` on one thread. | ||
| 129 | /// @throws std::invalid_argument for spans of different sizes. | ||
| 130 | void HeightsAt(const PostLattice& posts, Span<const Vec2d> points, Span<float> heights, | ||
| 131 | double spacing) const; | ||
| 132 | |||
| 133 | /// Posts either side of a point that `FillSamples` reads, so that a source reading a window | ||
| 134 | /// of its posts reads enough around it: 2 by default. | ||
| 135 | ✗ | virtual int Reach() const { | |
| 136 | ✗ | return 2; | |
| 137 | } | ||
| 138 | |||
| 139 | /// An upsampler for `posts` with any settings this one leaves to be estimated from them filled | ||
| 140 | /// in, such as diamond-square's roughness; null when this one serves as it is, the default. | ||
| 141 | 333 | virtual std::shared_ptr<const Upsampler> FitTo(const PostLattice& /*posts*/) const { | |
| 142 | 333 | return nullptr; | |
| 143 | } | ||
| 144 | |||
| 145 | /// A copy of the upsampler whose seeds draw from `seed` (`ReseedWord`); null for one without | ||
| 146 | /// seeds, the default, which a reseeded source keeps. | ||
| 147 | 2 | virtual std::shared_ptr<const Upsampler> Reseeded(std::uint64_t /*seed*/) const { | |
| 148 | 2 | return nullptr; | |
| 149 | } | ||
| 150 | |||
| 151 | protected: | ||
| 152 | /// Sets every sample of `samples` to the height from `posts` there at a sample spacing of | ||
| 153 | /// `spacing`, NaN where it has none, the same bits for a grid's cell as for a point at its | ||
| 154 | /// center. | ||
| 155 | /// @param threads Threads to split a grid over; 0 for every hardware thread. | ||
| 156 | virtual void FillSamples(const PostLattice& posts, SampleBlock& samples, double spacing, | ||
| 157 | unsigned threads) const = 0; | ||
| 158 | }; | ||
| 159 | |||
| 160 | /// Interpolation of the posts with a kernel (`ResampleMethod`), at every spacing alike: `Cubic`, | ||
| 161 | /// the default, gives the same bits as `SampleRaster` of the posts. | ||
| 162 | 225 | class KernelUpsampler final : public Upsampler { | |
| 163 | public: | ||
| 164 | 226 | explicit KernelUpsampler(ResampleMethod method = ResampleMethod::Cubic) : m_method(method) {} | |
| 165 | |||
| 166 | /// The kernel's radius (`KernelRadius`). | ||
| 167 | 94 | int Reach() const override { | |
| 168 | 94 | return KernelRadius(m_method); | |
| 169 | } | ||
| 170 | |||
| 171 | protected: | ||
| 172 | /// The posts interpolated at each sample as `SampleRaster` interpolates a raster, NaN where a | ||
| 173 | /// post weighed holds no data. Points close together read the posts once, | ||
| 174 | /// reusing a row's kernel weights in the rows below that share them. | ||
| 175 | /// @throws std::invalid_argument for a rotated lattice. | ||
| 176 | void FillSamples(const PostLattice& posts, SampleBlock& samples, double spacing, | ||
| 177 | unsigned threads) const override; | ||
| 178 | |||
| 179 | private: | ||
| 180 | ResampleMethod m_method; | ||
| 181 | }; | ||
| 182 | |||
| 183 | /// The upsampler sources use unless given one: cubic interpolation, shared. | ||
| 184 | std::shared_ptr<const Upsampler> DefaultUpsampler(); | ||
| 185 | |||
| 186 | /// `upsampler`, or `DefaultUpsampler()` when null, fitted to `posts` (`Upsampler::FitTo`). | ||
| 187 | /// @throws std::invalid_argument as `Upsampler::FitTo`. | ||
| 188 | std::shared_ptr<const Upsampler> FittedUpsampler(std::shared_ptr<const Upsampler> upsampler, | ||
| 189 | const PostLattice& posts); | ||
| 190 | |||
| 191 | /// The built-in kernel upsamplers' names, as configurations give them: nearest, bilinear, cubic, | ||
| 192 | /// catmull_rom, lanczos3 and cubic_bspline. | ||
| 193 | const std::vector<std::string>& KernelUpsamplerNames(); | ||
| 194 | |||
| 195 | /// The kernel a built-in kernel upsampler's name gives, or none for another name. | ||
| 196 | std::optional<ResampleMethod> KernelUpsamplerMethod(const std::string& name); | ||
| 197 | |||
| 198 | /// Makes an upsampler from its settings: a JSON object as text, "{}" without any. | ||
| 199 | using UpsamplerFactory = | ||
| 200 | std::function<std::shared_ptr<const Upsampler>(const std::string& settings)>; | ||
| 201 | |||
| 202 | /// Registers an upsampler type of your own under `name`, replacing any registered under it | ||
| 203 | /// before, so that configurations can name it as `"upsampling": "name"` or | ||
| 204 | /// `{"type": "name", ...settings}`. | ||
| 205 | /// @throws std::invalid_argument for an empty name, a built-in's name (the kernel names and | ||
| 206 | /// "diamond_square"), or an empty factory. | ||
| 207 | void RegisterUpsampler(const std::string& name, UpsamplerFactory factory); | ||
| 208 | |||
| 209 | /// Whether an upsampler type is registered under `name` (`RegisterUpsampler`). | ||
| 210 | bool IsUpsamplerRegistered(const std::string& name); | ||
| 211 | |||
| 212 | /// The names upsampler types are registered under, sorted. | ||
| 213 | std::vector<std::string> RegisteredUpsamplerNames(); | ||
| 214 | |||
| 215 | /// The upsampler the type registered under `name` makes with `settings`. | ||
| 216 | /// @throws std::invalid_argument for a name not registered, or a factory that gives null. | ||
| 217 | std::shared_ptr<const Upsampler> MakeRegisteredUpsampler(const std::string& name, | ||
| 218 | const std::string& settings = "{}"); | ||
| 219 | |||
| 220 | } // namespace terraingen | ||
| 221 |