include/terraingen/sources/source.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief The source interface: where a terrain's heights come from. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/geometry/geometry.h" | ||
| 8 | #include "terraingen/raster/geotiff/geotiff.h" | ||
| 9 | #include "terraingen/raster/grid.h" | ||
| 10 | #include "terraingen/raster/raster.h" | ||
| 11 | #include "terraingen/sources/sample_block.h" | ||
| 12 | #include "terraingen/util/span.h" | ||
| 13 | |||
| 14 | #include <cstdint> | ||
| 15 | #include <limits> | ||
| 16 | #include <memory> | ||
| 17 | #include <optional> | ||
| 18 | |||
| 19 | namespace terraingen { | ||
| 20 | |||
| 21 | /// Where a terrain's heights come from: a DEM, synthetic noise, or your own. | ||
| 22 | /// | ||
| 23 | /// Heights depend only on position and spacing; every query goes through `FillSamples`. | ||
| 24 | /// Implementations must be deterministic and thread-safe. | ||
| 25 | 1055 | class Source { | |
| 26 | public: | ||
| 27 | 305 | virtual ~Source() = default; | |
| 28 | |||
| 29 | /// The grid the source covers, which a terrain is evaluated over unless another is given. A | ||
| 30 | /// source without bounds, such as synthetic noise, gives a grid without cells whose | ||
| 31 | /// transform places its posts, if it has any (`BoundedGrid`). | ||
| 32 | virtual RasterGrid Grid() const = 0; | ||
| 33 | |||
| 34 | /// A point near the source's data, in world units, that noise layers measure positions from, | ||
| 35 | /// keeping single-precision noise accurate where coordinates are large; the origin by default. | ||
| 36 | 2273380 | virtual Vec2d ReferencePoint() const { | |
| 37 | 2273380 | return {}; | |
| 38 | } | ||
| 39 | |||
| 40 | /// The projection outputs over the source's grid are written with; none by default. | ||
| 41 | 658 | virtual GeoKeys Keys() const { | |
| 42 | 658 | return {}; | |
| 43 | 658 | } | |
| 44 | |||
| 45 | /// Overwrites every sample of `samples` with the source's height there (`FillSamples`); a | ||
| 46 | /// grid's cells without data then hold its no-data value, NaN if it has none and any is | ||
| 47 | /// missing. | ||
| 48 | /// @param spacing Sample spacing in the source's units (meters on a plane, degrees on a | ||
| 49 | /// body): finer than the source's posts, they are refined to it. | ||
| 50 | /// @param threads Threads to fill over; 0 for every hardware thread. | ||
| 51 | void Fill(SampleBlock& samples, double spacing, unsigned threads) const; | ||
| 52 | |||
| 53 | /// `Fill` of every cell of `heights`, placed by its transform. | ||
| 54 | /// @param spacing Sample spacing in the source's units. | ||
| 55 | /// @param threads Threads to fill over; 0 for every hardware thread. | ||
| 56 | void Fill(Raster<float>& heights, double spacing, unsigned threads) const; | ||
| 57 | |||
| 58 | /// `Fill` on every hardware thread. | ||
| 59 | 436 | void Fill(Raster<float>& heights, double spacing) const { | |
| 60 | 436 | Fill(heights, spacing, 0); | |
| 61 | 436 | } | |
| 62 | |||
| 63 | /// `Fill` at the grid's own spacing, its cell size (`RasterGrid::PixelSize`), on every | ||
| 64 | /// hardware thread. | ||
| 65 | 46 | void Fill(Raster<float>& heights) const { | |
| 66 | 46 | Fill(heights, heights.Grid().PixelSize(), 0); | |
| 67 | 44 | } | |
| 68 | |||
| 69 | /// `Fill` of the cells of `grid` straight into `heights`, row-major from row 0, NaN where the | ||
| 70 | /// source has no data. | ||
| 71 | /// @param spacing Sample spacing in the source's units. | ||
| 72 | /// @param threads Threads to fill over; 0 for every hardware thread. | ||
| 73 | /// @throws std::invalid_argument for `heights` not of `grid`'s size. | ||
| 74 | void Fill(const RasterGrid& grid, Span<float> heights, double spacing, unsigned threads) const; | ||
| 75 | |||
| 76 | /// The height in meters at world point (`x`, `y`) at a sample spacing of `spacing` in the | ||
| 77 | /// source's units, or NaN where the source has no data: what `Fill` gives a cell centered | ||
| 78 | /// there at that spacing, to the same bits. | ||
| 79 | float HeightAt(double x, double y, double spacing) const; | ||
| 80 | |||
| 81 | /// Sets each of `heights` to `HeightAt` the point of `points` at its index, to the same bits, | ||
| 82 | /// in one `FillSamples` on one thread. | ||
| 83 | /// @throws std::invalid_argument for spans of different sizes. | ||
| 84 | void HeightsAt(Span<const Vec2d> points, Span<float> heights, double spacing) const; | ||
| 85 | |||
| 86 | /// The cell size in CRS units (degrees in longitude and latitude) of the measured data giving | ||
| 87 | /// the height at world point (`x`, `y`), or its effective pixel size, below which the source | ||
| 88 | /// resolves no features; infinity where no measured data gives it, as for synthetic heights, | ||
| 89 | /// the default, and where the source has no height. | ||
| 90 | 85375 | virtual double ResolutionAt(double /*x*/, double /*y*/) const { | |
| 91 | 85375 | return std::numeric_limits<double>::infinity(); | |
| 92 | } | ||
| 93 | |||
| 94 | /// The spacing in CRS units of the posts giving the height at world point (`x`, `y`) before | ||
| 95 | /// any upsampling, measured or generated; infinity where no posts give it, as where heights | ||
| 96 | /// are evaluated at every point, the default, and where the source has no height. | ||
| 97 | 1 | virtual double PostSpacingAt(double /*x*/, double /*y*/) const { | |
| 98 | 1 | return std::numeric_limits<double>::infinity(); | |
| 99 | } | ||
| 100 | |||
| 101 | /// A copy of the source whose seeds draw from `seed` (`ReseedWord`), sharing any data it | ||
| 102 | /// read; null when the copy would equal the source, as for measured data (the default), so | ||
| 103 | /// `Terrain::Reseed` keeps it. | ||
| 104 | 22 | virtual std::shared_ptr<const Source> Reseeded(std::uint64_t /*seed*/) const { | |
| 105 | 22 | return nullptr; | |
| 106 | } | ||
| 107 | |||
| 108 | protected: | ||
| 109 | /// Sets every sample of `samples` to the source's height there at a sample spacing of | ||
| 110 | /// `spacing`, the same bits for a grid's cell as for a point at its center. A sample without | ||
| 111 | /// data is set to NaN, or a grid's cell to the no-data value the source gives its raster. | ||
| 112 | /// @param threads Threads to split a grid over; 0 for every hardware thread. | ||
| 113 | virtual void FillSamples(SampleBlock& samples, double spacing, unsigned threads) const = 0; | ||
| 114 | |||
| 115 | /// `FillSamples` of `source`, for a source filling from others, which leaves a grid's cells | ||
| 116 | /// without data to its own `Fill` to mark once. | ||
| 117 | /// @param threads Threads to split a grid over; 0 for every hardware thread. | ||
| 118 | 44223 | static void FillUnmarked(const Source& source, SampleBlock& samples, double spacing, | |
| 119 | unsigned threads) { | ||
| 120 | 44223 | source.FillSamples(samples, spacing, threads); | |
| 121 | 30 | } | |
| 122 | }; | ||
| 123 | |||
| 124 | /// The grid `source` covers, or none for a source without bounds (a grid without cells). | ||
| 125 | 824 | inline std::optional<RasterGrid> BoundedGrid(const Source& source) { | |
| 126 | 824 | RasterGrid grid = source.Grid(); | |
| 127 | 824 | if (grid.width == 0 || grid.height == 0) { | |
| 128 | 401 | return std::nullopt; | |
| 129 | } | ||
| 130 | 423 | return grid; | |
| 131 | } | ||
| 132 | |||
| 133 | /// Where noise on a plane is measured from: `source`'s `ReferencePoint`, or the origin when it is | ||
| 134 | /// null. | ||
| 135 | 6126640 | inline Vec2d ReferencePoint(const Source* source) { | |
| 136 | 6126640 | return source != nullptr ? source->ReferencePoint() : Vec2d{}; | |
| 137 | } | ||
| 138 | |||
| 139 | } // namespace terraingen | ||
| 140 |