include/terraingen/layers/field.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Scalar fields over a surface, such as density maps and catalog completeness limits. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/raster/raster.h" | ||
| 8 | #include "terraingen/raster/resample.h" | ||
| 9 | |||
| 10 | #include <cstddef> | ||
| 11 | #include <filesystem> | ||
| 12 | #include <limits> | ||
| 13 | #include <memory> | ||
| 14 | |||
| 15 | namespace terraingen { | ||
| 16 | |||
| 17 | /// A scalar quantity over a surface, read at surface coordinates: meters on a plane, longitude | ||
| 18 | /// and latitude in degrees on a sphere. Values depend only on position, so tiles, point queries | ||
| 19 | /// and threads agree; implementations must be thread-safe. | ||
| 20 | 141 | class SurfaceField { | |
| 21 | public: | ||
| 22 | 136 | virtual ~SurfaceField() = default; | |
| 23 | |||
| 24 | /// The value at surface (x, y); NaN where the field has none. | ||
| 25 | virtual double ValueAt(double x, double y) const = 0; | ||
| 26 | |||
| 27 | /// The largest value the field takes anywhere, a bound for drawing features from it; NaN when | ||
| 28 | /// it has no values. | ||
| 29 | virtual double Max() const = 0; | ||
| 30 | }; | ||
| 31 | |||
| 32 | /// The same value everywhere. | ||
| 33 | 45 | class ConstantField final : public SurfaceField { | |
| 34 | public: | ||
| 35 | /// @param value Any value; NaN gives a field with none. | ||
| 36 | 41 | explicit ConstantField(double value) : m_value(value) {} | |
| 37 | |||
| 38 | 2098952 | double ValueAt(double /*x*/, double /*y*/) const override { | |
| 39 | 2098952 | return m_value; | |
| 40 | } | ||
| 41 | 29 | double Max() const override { | |
| 42 | 29 | return m_value; | |
| 43 | } | ||
| 44 | |||
| 45 | private: | ||
| 46 | double m_value; | ||
| 47 | }; | ||
| 48 | |||
| 49 | /// The smaller of two fields at each point, the one that has a value where the other is NaN: a | ||
| 50 | /// completeness limit that two limits give, such as a catalog's and a DEM's | ||
| 51 | /// (`SourceResolvedLimit`). | ||
| 52 | ✗ | class MinimumField final : public SurfaceField { | |
| 53 | public: | ||
| 54 | /// @throws std::invalid_argument for a null field. | ||
| 55 | MinimumField(std::shared_ptr<const SurfaceField> a, std::shared_ptr<const SurfaceField> b); | ||
| 56 | |||
| 57 | double ValueAt(double x, double y) const override; | ||
| 58 | |||
| 59 | /// The larger of the two fields' maxima, NaN only when both are, a bound for the minimum. | ||
| 60 | double Max() const override; | ||
| 61 | |||
| 62 | private: | ||
| 63 | std::shared_ptr<const SurfaceField> m_a; | ||
| 64 | std::shared_ptr<const SurfaceField> m_b; | ||
| 65 | }; | ||
| 66 | |||
| 67 | /// How a `RasterField` reads its raster. | ||
| 68 | 3 | struct RasterFieldOptions { | |
| 69 | /// `Nearest`, or `Bilinear` over the valid cells around a point, weights renormalized, so a | ||
| 70 | /// no-data cell only removes itself; both stay within the range of the cells they read. | ||
| 71 | ResampleMethod method = ResampleMethod::Bilinear; | ||
| 72 | /// Multiplies every cell, such as 0.001 for a map stored in thousandths; positive. | ||
| 73 | double scale = 1.0; | ||
| 74 | /// The value where the raster has none: outside its extent, and where every cell read is | ||
| 75 | /// no-data or NaN. NaN leaves those points without a value. | ||
| 76 | double fallback = std::numeric_limits<double>::quiet_NaN(); | ||
| 77 | /// The raster's x is longitude in degrees, repeating every 360: points outside its extent are | ||
| 78 | /// read 360 degrees east or west, so a map from 0 to 360 serves -180 to 180. | ||
| 79 | bool periodicLongitude = false; | ||
| 80 | |||
| 81 | /// Memberwise equality. | ||
| 82 | bool operator==(const RasterFieldOptions& other) const { | ||
| 83 | return method == other.method && scale == other.scale && fallback == other.fallback && | ||
| 84 | periodicLongitude == other.periodicLongitude; | ||
| 85 | } | ||
| 86 | bool operator!=(const RasterFieldOptions& other) const { | ||
| 87 | return !(*this == other); | ||
| 88 | } | ||
| 89 | }; | ||
| 90 | |||
| 91 | /// Checks raster field options. | ||
| 92 | /// @throws std::invalid_argument for a method other than nearest or bilinear, or a scale that is | ||
| 93 | /// not positive and finite. | ||
| 94 | void ValidateRasterFieldOptions(const RasterFieldOptions& options); | ||
| 95 | |||
| 96 | /// A field interpolated from a raster in the surface's coordinates. | ||
| 97 | 11 | class RasterField final : public SurfaceField { | |
| 98 | public: | ||
| 99 | /// @throws std::invalid_argument for an empty or rotated raster, or invalid options. | ||
| 100 | explicit RasterField(Raster<float> raster, RasterFieldOptions options = {}); | ||
| 101 | |||
| 102 | double ValueAt(double x, double y) const override; | ||
| 103 | |||
| 104 | /// The largest finite cell times the scale, or the fallback if larger. | ||
| 105 | 10 | double Max() const override { | |
| 106 | 10 | return m_max; | |
| 107 | } | ||
| 108 | |||
| 109 | /// The raster read. | ||
| 110 | ✗ | const Raster<float>& Values() const { | |
| 111 | 1 | return m_raster; | |
| 112 | } | ||
| 113 | |||
| 114 | /// How it is read. | ||
| 115 | ✗ | const RasterFieldOptions& Options() const { | |
| 116 | ✗ | return m_options; | |
| 117 | } | ||
| 118 | |||
| 119 | private: | ||
| 120 | /// The raster at (x, y) before scaling: its nearest cell, or the bilinear mean of the valid | ||
| 121 | /// cells around it; NaN outside its extent or where every cell read is invalid. | ||
| 122 | double Sample(double x, double y) const; | ||
| 123 | /// The cell holding `pixel` (pixel coordinates inside the raster), NaN where invalid. | ||
| 124 | double SampleNearest(const Vec2d& pixel) const; | ||
| 125 | /// The weighted mean of the valid cells around `pixel`, NaN where none is. | ||
| 126 | double SampleBilinear(const Vec2d& pixel) const; | ||
| 127 | |||
| 128 | Raster<float> m_raster; | ||
| 129 | RasterFieldOptions m_options; | ||
| 130 | double m_max = std::numeric_limits<double>::quiet_NaN(); | ||
| 131 | }; | ||
| 132 | |||
| 133 | /// A field read from one band of a GeoTIFF in the surface's coordinates. | ||
| 134 | /// @param band The band, from 0. | ||
| 135 | /// @throws GeoTiffError if the file cannot be read. | ||
| 136 | /// @throws std::invalid_argument as `RasterField`. | ||
| 137 | std::shared_ptr<const RasterField> ReadRasterField(const std::filesystem::path& path, | ||
| 138 | RasterFieldOptions options = {}, | ||
| 139 | std::size_t band = 0); | ||
| 140 | |||
| 141 | } // namespace terraingen | ||
| 142 |