include/terraingen/sources/raster_source.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Heights from a raster held in memory. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/raster/cell_loops.h" | ||
| 8 | #include "terraingen/raster/geotiff/geotiff.h" | ||
| 9 | #include "terraingen/raster/resample.h" | ||
| 10 | #include "terraingen/sources/source.h" | ||
| 11 | #include "terraingen/sources/upsampling.h" | ||
| 12 | |||
| 13 | #include <memory> | ||
| 14 | #include <optional> | ||
| 15 | #include <utility> | ||
| 16 | |||
| 17 | namespace terraingen { | ||
| 18 | |||
| 19 | /// How a `RasterSource` reads its raster. | ||
| 20 | 587 | struct RasterSourceOptions { | |
| 21 | /// The cell size in CRS units the heights resolve, for a DEM that was upsampled or smoothed | ||
| 22 | /// before it was saved: `ResolutionAt` gives it in place of the cells' size. Unset uses the | ||
| 23 | /// cells'. | ||
| 24 | std::optional<double> effectivePixelSize{}; | ||
| 25 | /// How the cells become heights between them and at sample spacings finer than the cells; | ||
| 26 | /// null for cubic interpolation (`DefaultUpsampler`). | ||
| 27 | std::shared_ptr<const Upsampler> upsampler{}; | ||
| 28 | |||
| 29 | /// Memberwise equality, the upsamplers compared by identity. | ||
| 30 | bool operator==(const RasterSourceOptions& other) const { | ||
| 31 | return effectivePixelSize == other.effectivePixelSize && upsampler == other.upsampler; | ||
| 32 | } | ||
| 33 | bool operator!=(const RasterSourceOptions& other) const { | ||
| 34 | return !(*this == other); | ||
| 35 | } | ||
| 36 | }; | ||
| 37 | |||
| 38 | namespace detail { | ||
| 39 | |||
| 40 | class RasterHeights; | ||
| 41 | |||
| 42 | /// A source serving the upsampled heights of a window of a lattice: what `RasterSource` | ||
| 43 | /// and `GeoTiffSource` share. | ||
| 44 | 10 | class RasterHeightsSource : public Source { | |
| 45 | public: | ||
| 46 | /// The lattice's cells covered. | ||
| 47 | 476 | RasterGrid Grid() const final { | |
| 48 | 448 | return Subgrid(m_lattice, m_cells); | |
| 49 | } | ||
| 50 | |||
| 51 | /// The top-left corner of the whole lattice, with or without a window of it. | ||
| 52 | 749 | Vec2d ReferencePoint() const final { | |
| 53 | 749 | return {m_lattice.transform.originX, m_lattice.transform.originY}; | |
| 54 | } | ||
| 55 | |||
| 56 | /// The projection given at construction. | ||
| 57 | 267 | GeoKeys Keys() const final { | |
| 58 | 267 | return m_keys; | |
| 59 | } | ||
| 60 | |||
| 61 | /// The effective pixel size, or else the cell size in CRS units, the larger side of a cell; | ||
| 62 | /// infinity off the covered cells and where the cell under (`x`, `y`) holds no data. | ||
| 63 | /// @throws GeoTiffError if a file read for the heights cannot be read. | ||
| 64 | double ResolutionAt(double x, double y) const final; | ||
| 65 | |||
| 66 | /// The cell size in CRS units, the larger side of a cell; infinity off the covered cells and | ||
| 67 | /// where the cell under (`x`, `y`) holds no data. | ||
| 68 | /// @throws GeoTiffError if a file read for the heights cannot be read. | ||
| 69 | double PostSpacingAt(double x, double y) const final; | ||
| 70 | |||
| 71 | protected: | ||
| 72 | /// @param lattice The whole grid the heights lie on. | ||
| 73 | /// @param cells The window of `lattice` covered. | ||
| 74 | /// @param keys The projection outputs are written with. | ||
| 75 | 285 | RasterHeightsSource(const RasterGrid& lattice, const PixelWindow& cells, GeoKeys keys) | |
| 76 | 285 | : m_lattice(lattice), m_cells(cells), m_keys(std::move(keys)) {} | |
| 77 | |||
| 78 | /// The whole grid the heights lie on. | ||
| 79 | 104 | const RasterGrid& Lattice() const { | |
| 80 | 104 | return m_lattice; | |
| 81 | } | ||
| 82 | |||
| 83 | /// The window of the lattice covered. | ||
| 84 | 208 | const PixelWindow& Cells() const { | |
| 85 | 208 | return m_cells; | |
| 86 | } | ||
| 87 | |||
| 88 | /// Copies cells on the lattice at its cell size or coarser, otherwise upsamples. | ||
| 89 | /// @throws std::invalid_argument for a rotated grid off the lattice. | ||
| 90 | /// @throws GeoTiffError if a file read for the heights cannot be read. | ||
| 91 | void FillSamples(SampleBlock& samples, double spacing, unsigned threads) const final; | ||
| 92 | |||
| 93 | /// The heights served. | ||
| 94 | virtual const RasterHeights& Heights() const = 0; | ||
| 95 | |||
| 96 | private: | ||
| 97 | RasterGrid m_lattice; | ||
| 98 | PixelWindow m_cells; | ||
| 99 | GeoKeys m_keys; | ||
| 100 | }; | ||
| 101 | |||
| 102 | } // namespace detail | ||
| 103 | |||
| 104 | /// Heights from a raster held in memory, in a projected CRS or the surface's coordinates. | ||
| 105 | /// | ||
| 106 | /// The heights are used as given, with the raster's no-data value. On the source's own lattice | ||
| 107 | /// at its cell size or coarser `Fill` copies cells exactly; elsewhere, and at finer spacings, | ||
| 108 | /// the upsampler gives each cell from the raster's cells, its posts. Cells beyond the raster are | ||
| 109 | /// no-data. With geographic `keys` and columns spanning 360 degrees the raster wraps in | ||
| 110 | /// longitude, so a file from 0 to 360 also serves -180 to 0. | ||
| 111 | class RasterSource final : public detail::RasterHeightsSource { | ||
| 112 | public: | ||
| 113 | /// The source of `heights`. | ||
| 114 | /// @param keys The projection outputs are written with. | ||
| 115 | /// @throws std::invalid_argument for an empty raster, invalid options, or as | ||
| 116 | /// `Upsampler::FitTo`. | ||
| 117 | explicit RasterSource(Raster<float> heights, const RasterSourceOptions& options = {}, | ||
| 118 | GeoKeys keys = {}); | ||
| 119 | |||
| 120 | /// The upsampler in use, fitted to the raster (`Upsampler::FitTo`). | ||
| 121 | const std::shared_ptr<const Upsampler>& UpsamplerInUse() const; | ||
| 122 | |||
| 123 | protected: | ||
| 124 | const detail::RasterHeights& Heights() const override; | ||
| 125 | |||
| 126 | private: | ||
| 127 | std::shared_ptr<const detail::RasterHeights> m_heights; | ||
| 128 | }; | ||
| 129 | |||
| 130 | } // namespace terraingen | ||
| 131 |