src/sources/raster_heights.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `RasterHeights`, the upsampled heights of a window of a post lattice, which raster and | ||
| 5 | /// GeoTIFF sources serve. | ||
| 6 | #pragma once | ||
| 7 | |||
| 8 | #include "terraingen/raster/cell_loops.h" | ||
| 9 | #include "terraingen/raster/geotiff/geotiff.h" | ||
| 10 | #include "terraingen/raster/grid.h" | ||
| 11 | #include "terraingen/sources/raster_source.h" | ||
| 12 | #include "terraingen/sources/sample_block.h" | ||
| 13 | #include "terraingen/sources/upsampling.h" | ||
| 14 | |||
| 15 | #include <memory> | ||
| 16 | #include <optional> | ||
| 17 | #include <string> | ||
| 18 | |||
| 19 | namespace terraingen::detail { | ||
| 20 | |||
| 21 | /// Checks raster source options. | ||
| 22 | /// @throws std::invalid_argument for an `effectivePixelSize` that is not positive and finite. | ||
| 23 | void ValidateRasterSourceOptions(const RasterSourceOptions& options); | ||
| 24 | |||
| 25 | /// The cells of the whole grid `native` that give the heights of its cells `cells` to the same | ||
| 26 | /// bits as the whole: those under `cells` with a margin of `reach` cells and one more for | ||
| 27 | /// rounding, clamped to `native`. | ||
| 28 | PixelWindow NativeWindow(const RasterGrid& native, int reach, const PixelWindow& cells); | ||
| 29 | |||
| 30 | /// Whether a raster source with keys `keys` on `lattice` wraps in longitude: a geographic CRS | ||
| 31 | /// and an unrotated lattice whose columns span exactly 360 degrees. | ||
| 32 | bool WrapsLongitude(const GeoKeys& keys, const RasterGrid& lattice); | ||
| 33 | |||
| 34 | /// The heights of a raster source: the posts of a lattice giving its cells `cells`, upsampled | ||
| 35 | /// between them as the options say. | ||
| 36 | class RasterHeights { | ||
| 37 | public: | ||
| 38 | /// @param posts The posts on `lattice`, holding at least `NativeWindow` of `cells`. | ||
| 39 | /// @param lattice The whole grid the posts lie on. | ||
| 40 | /// @param noData The value of cells copied where the posts have no data, and of cells beyond | ||
| 41 | /// those covered; NaN without one. | ||
| 42 | /// @param cells A non-empty window of the lattice; the whole lattice gives the heights over | ||
| 43 | /// every cell, and also interpolates over the whole of the posts. | ||
| 44 | /// @param name What the source is called in errors. | ||
| 45 | /// @param wrapLongitude Whether x is a longitude repeating every 360 degrees | ||
| 46 | /// (`WrapsLongitude`); the posts then wrap their columns too. | ||
| 47 | /// @throws std::invalid_argument as `Upsampler::FitTo`. | ||
| 48 | RasterHeights(std::shared_ptr<const PostLattice> posts, const RasterGrid& lattice, | ||
| 49 | std::optional<float> noData, const RasterSourceOptions& options, | ||
| 50 | const PixelWindow& cells, std::string name, bool wrapLongitude = false); | ||
| 51 | |||
| 52 | /// Sets each sample to the height of the lattice cell centered there at the lattice's spacing | ||
| 53 | /// or coarser, otherwise the upsampler's (`Upsampler::Fill`); NaN beyond the cells covered and | ||
| 54 | /// where there is no data, a grid taking the no-data value for `Source::Fill` to mark. A | ||
| 55 | /// wrapping source reads each sample a whole number of turns of longitude into the lattice's | ||
| 56 | /// longitudes. | ||
| 57 | /// @param threads Threads to fill a grid over; 0 for every hardware thread. | ||
| 58 | /// @throws std::invalid_argument for a rotated grid off the lattice. | ||
| 59 | void Fill(SampleBlock& samples, double spacing, unsigned threads) const; | ||
| 60 | |||
| 61 | /// The effective pixel size, or else the native cell size, at a covered point whose cell holds | ||
| 62 | /// data; infinity elsewhere. | ||
| 63 | double ResolutionAt(double x, double y) const; | ||
| 64 | |||
| 65 | /// The native cell size at a covered point whose cell holds data; infinity elsewhere. | ||
| 66 | double PostSpacingAt(double x, double y) const; | ||
| 67 | |||
| 68 | /// The upsampler in use, fitted to the cells (`Upsampler::FitTo`). | ||
| 69 | 1 | const std::shared_ptr<const Upsampler>& UpsamplerInUse() const { | |
| 70 | 1 | return m_upsampler; | |
| 71 | } | ||
| 72 | |||
| 73 | private: | ||
| 74 | /// Whether lattice position (`col`, `row`), in cells, is covered. | ||
| 75 | bool Covers(double col, double row) const; | ||
| 76 | /// Whether (`x`, `y`) is covered and the lattice cell under it holds data. | ||
| 77 | bool HasHeight(double x, double y) const; | ||
| 78 | /// The cells of `grid` that may be covered: those meeting the covered cells, and one more on | ||
| 79 | /// every side. | ||
| 80 | PixelWindow CoveredWindow(const RasterGrid& grid) const; | ||
| 81 | /// The height `FillUnwrapped` gives cell (`col`, `row`) of `heights`: NaN beyond `window` or | ||
| 82 | /// the cells covered, otherwise the cell of `covered`, a fill of `window`, or of `heights` | ||
| 83 | /// filled whole when it is null. | ||
| 84 | float HeightIn(const Raster<float>& heights, std::size_t col, std::size_t row, | ||
| 85 | const PixelWindow& window, const Raster<float>* covered) const; | ||
| 86 | /// `point` moved a whole number of turns of longitude onto the lattice (`Turns`). | ||
| 87 | Vec2d Unwrapped(const Vec2d& point) const; | ||
| 88 | /// The turns of longitude to take from `x` to bring it onto the lattice; 0 unless wrapping. | ||
| 89 | double Turns(double x) const; | ||
| 90 | /// `Fill` of a grid, each run of columns a turn of longitude away from the lattice filled | ||
| 91 | /// as the lattice's own longitudes. | ||
| 92 | void FillGrid(Raster<float>& heights, double spacing, unsigned threads) const; | ||
| 93 | /// `Fill` of a grid without wrapping, upsampling only the cells that may be covered when a | ||
| 94 | /// fill of them alone gives the same bits (`ExactSubgrid`). | ||
| 95 | void FillUnwrapped(Raster<float>& heights, double spacing, unsigned threads) const; | ||
| 96 | /// `Fill` of `points`, the points covered upsampled at once. | ||
| 97 | void FillPoints(Span<const Vec2d> points, Span<float> heights, double spacing) const; | ||
| 98 | |||
| 99 | std::shared_ptr<const PostLattice> m_posts; | ||
| 100 | RasterGrid m_lattice; | ||
| 101 | std::optional<float> m_noData; | ||
| 102 | PixelWindow m_cells; | ||
| 103 | std::shared_ptr<const Upsampler> m_upsampler; | ||
| 104 | double m_cellSpacing = 0.0; | ||
| 105 | double m_resolution = 0.0; | ||
| 106 | std::string m_name; | ||
| 107 | bool m_wrapLongitude = false; | ||
| 108 | }; | ||
| 109 | |||
| 110 | } // namespace terraingen::detail | ||
| 111 |