include/terraingen/raster/raster.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief A single-band georeferenced grid of values. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/raster/geo_transform.h" | ||
| 8 | #include "terraingen/raster/grid.h" | ||
| 9 | #include "terraingen/util/span.h" | ||
| 10 | |||
| 11 | #include <cassert> | ||
| 12 | #include <cmath> | ||
| 13 | #include <cstddef> | ||
| 14 | #include <optional> | ||
| 15 | #include <stdexcept> | ||
| 16 | #include <type_traits> | ||
| 17 | #include <utility> | ||
| 18 | #include <vector> | ||
| 19 | |||
| 20 | namespace terraingen { | ||
| 21 | |||
| 22 | /// A single-band, georeferenced grid of values stored row-major, in cells of its own or in a | ||
| 23 | /// caller's buffer it borrows. | ||
| 24 | /// | ||
| 25 | /// Row 0 is the first row in the file, the northern edge for north-up rasters. Cells are | ||
| 26 | /// addressed as (column, row), matching the (x, y) order of `GeoTransform`. A copy always owns | ||
| 27 | /// its cells; a move keeps borrowing what the raster borrowed. | ||
| 28 | /// @tparam T Cell type, usually `float` heights in meters. | ||
| 29 | template <typename T> class Raster { | ||
| 30 | public: | ||
| 31 | /// An empty raster with the identity transform and no no-data value. | ||
| 32 | 451 | Raster() = default; | |
| 33 | |||
| 34 | /// A raster of `width` columns and `height` rows with the identity transform, every cell | ||
| 35 | /// set to `fill`. | ||
| 36 | 553560 | Raster(std::size_t width, std::size_t height, T fill = T{}) | |
| 37 | 553560 | : Raster(RasterGrid{width, height, GeoTransform{}}, fill) {} | |
| 38 | |||
| 39 | /// A raster over `grid`, every cell set to `fill`. | ||
| 40 | 556342 | explicit Raster(const RasterGrid& grid, T fill = T{}) | |
| 41 | 556342 | : m_grid(grid), m_owned(grid.width * grid.height, fill), m_cells(m_owned.data()) {} | |
| 42 | |||
| 43 | /// A raster over `grid` whose cells are `cells`, which must outlive it; writes go to | ||
| 44 | /// `cells`. | ||
| 45 | /// @throws std::invalid_argument unless `cells` holds one value per cell of `grid`. | ||
| 46 | 451 | Raster(const RasterGrid& grid, Span<T> cells) : m_grid(grid), m_cells(cells.data()) { | |
| 47 | 451 | if (cells.size() != grid.width * grid.height) { | |
| 48 | 1 | throw std::invalid_argument("A raster borrowing cells needs one value per cell"); | |
| 49 | } | ||
| 50 | 451 | } | |
| 51 | |||
| 52 | 380 | Raster(const Raster& other) | |
| 53 | 380 | : m_grid(other.m_grid), m_owned(other.m_cells, other.m_cells + other.Size()), | |
| 54 | 380 | m_cells(m_owned.data()), m_noData(other.m_noData) {} | |
| 55 | |||
| 56 | 1182 | Raster(Raster&& other) noexcept | |
| 57 | 1182 | : m_grid(other.m_grid), m_owned(std::move(other.m_owned)), m_cells(other.m_cells), | |
| 58 | 1182 | m_noData(other.m_noData) { | |
| 59 | 1182 | other.m_grid.width = 0; | |
| 60 | 1182 | other.m_grid.height = 0; | |
| 61 | 1182 | other.m_cells = nullptr; | |
| 62 | 1182 | } | |
| 63 | |||
| 64 | 229 | Raster& operator=(Raster other) noexcept { | |
| 65 | 229 | std::swap(m_grid, other.m_grid); | |
| 66 | 229 | std::swap(m_owned, other.m_owned); | |
| 67 | 229 | std::swap(m_cells, other.m_cells); | |
| 68 | 229 | std::swap(m_noData, other.m_noData); | |
| 69 | 229 | return *this; | |
| 70 | } | ||
| 71 | |||
| 72 | 558218 | ~Raster() = default; | |
| 73 | |||
| 74 | /// The raster's size and pixel-to-world transform. | ||
| 75 | 229004 | const RasterGrid& Grid() const { | |
| 76 | 228760 | return m_grid; | |
| 77 | } | ||
| 78 | |||
| 79 | /// The number of columns. | ||
| 80 | 464226340 | std::size_t Width() const { | |
| 81 | 458570202 | return m_grid.width; | |
| 82 | } | ||
| 83 | |||
| 84 | /// The number of rows. | ||
| 85 | 34969181 | std::size_t Height() const { | |
| 86 | 34968762 | return m_grid.height; | |
| 87 | } | ||
| 88 | |||
| 89 | /// The number of cells, `Width() * Height()`. | ||
| 90 | 11203650 | std::size_t Size() const { | |
| 91 | 5940471 | return m_grid.width * m_grid.height; | |
| 92 | } | ||
| 93 | |||
| 94 | /// Whether the raster has no cells. | ||
| 95 | 518283 | bool Empty() const { | |
| 96 | 518284 | return Size() == 0; | |
| 97 | } | ||
| 98 | |||
| 99 | /// Unchecked cell access; asserts `col < Width()` and `row < Height()` in debug builds. | ||
| 100 | 522329813 | T& operator()(std::size_t col, std::size_t row) { | |
| 101 | 522329813 | assert(col < m_grid.width && row < m_grid.height); | |
| 102 | 522329813 | return m_cells[row * m_grid.width + col]; | |
| 103 | } | ||
| 104 | |||
| 105 | /// Unchecked read-only cell access; asserts in debug builds. | ||
| 106 | 2730331669 | const T& operator()(std::size_t col, std::size_t row) const { | |
| 107 | 2730331669 | assert(col < m_grid.width && row < m_grid.height); | |
| 108 | 2730331669 | return m_cells[row * m_grid.width + col]; | |
| 109 | } | ||
| 110 | |||
| 111 | /// Bounds-checked cell access. | ||
| 112 | /// @throws std::out_of_range if `col` or `row` is outside the raster. | ||
| 113 | 3 | T& At(std::size_t col, std::size_t row) { | |
| 114 | 3 | CheckBounds(col, row); | |
| 115 | 1 | return m_cells[row * m_grid.width + col]; | |
| 116 | } | ||
| 117 | |||
| 118 | /// Bounds-checked read-only cell access. | ||
| 119 | /// @throws std::out_of_range if `col` or `row` is outside the raster. | ||
| 120 | const T& At(std::size_t col, std::size_t row) const { | ||
| 121 | CheckBounds(col, row); | ||
| 122 | return m_cells[row * m_grid.width + col]; | ||
| 123 | } | ||
| 124 | |||
| 125 | /// All cells in row-major order. | ||
| 126 | 6421634 | Span<T> Values() { | |
| 127 | 3960560 | return {m_cells, Size()}; | |
| 128 | } | ||
| 129 | |||
| 130 | /// All cells in row-major order, read-only. | ||
| 131 | 4648328 | Span<const T> Values() const { | |
| 132 | 2840121 | return {m_cells, Size()}; | |
| 133 | } | ||
| 134 | |||
| 135 | /// The cells of one row, west to east for north-up rasters; asserts in debug builds. | ||
| 136 | 36494 | Span<T> Row(std::size_t row) { | |
| 137 | 36494 | assert(row < m_grid.height); | |
| 138 | 36494 | return {m_cells + row * m_grid.width, m_grid.width}; | |
| 139 | } | ||
| 140 | |||
| 141 | /// The cells of one row, read-only; asserts in debug builds. | ||
| 142 | 1564 | Span<const T> Row(std::size_t row) const { | |
| 143 | 1564 | assert(row < m_grid.height); | |
| 144 | 1564 | return {m_cells + row * m_grid.width, m_grid.width}; | |
| 145 | } | ||
| 146 | |||
| 147 | /// The pixel-to-world transform. | ||
| 148 | 840571 | const GeoTransform& Transform() const { | |
| 149 | 1358008 | return m_grid.transform; | |
| 150 | } | ||
| 151 | |||
| 152 | /// Whether `value` is the no-data value; a NaN no-data value matches every NaN, and nothing | ||
| 153 | /// matches without a no-data value. | ||
| 154 | 1974784084 | bool IsNoData(const T& value) const { | |
| 155 | 1974784084 | if (!m_noData) { | |
| 156 | return false; | ||
| 157 | } | ||
| 158 | if constexpr (std::is_floating_point_v<T>) { | ||
| 159 | 6534129 | if (std::isnan(*m_noData)) { | |
| 160 | 5700153 | return std::isnan(value); | |
| 161 | } | ||
| 162 | } | ||
| 163 | 833976 | return value == *m_noData; | |
| 164 | } | ||
| 165 | |||
| 166 | /// Whether `value` holds no data: not finite, or the no-data value. | ||
| 167 | 1952722580 | bool IsMissing(const T& value) const { | |
| 168 | if constexpr (std::is_floating_point_v<T>) { | ||
| 169 | 1952722584 | if (!std::isfinite(value)) { | |
| 170 | return true; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | 1952722581 | return IsNoData(value); | |
| 174 | } | ||
| 175 | |||
| 176 | /// The value marking cells without data, if any. | ||
| 177 | 967 | const std::optional<T>& NoData() const { | |
| 178 | 1527 | return m_noData; | |
| 179 | } | ||
| 180 | |||
| 181 | /// Sets the value marking cells without data, or none. | ||
| 182 | 2352 | void SetNoData(std::optional<T> value) { | |
| 183 | 2352 | m_noData = value; | |
| 184 | 4 | } | |
| 185 | |||
| 186 | /// The world coordinates of a cell's center. | ||
| 187 | 32956202 | Vec2d CellCenter(std::size_t col, std::size_t row) const { | |
| 188 | 26774473 | return m_grid.transform.PixelToWorld(static_cast<double>(col) + 0.5, | |
| 189 | 26654003 | static_cast<double>(row) + 0.5); | |
| 190 | } | ||
| 191 | |||
| 192 | private: | ||
| 193 | 3 | void CheckBounds(std::size_t col, std::size_t row) const { | |
| 194 | 3 | if (col >= m_grid.width || row >= m_grid.height) { | |
| 195 | 2 | throw std::out_of_range("Raster cell out of range"); | |
| 196 | } | ||
| 197 | 1 | } | |
| 198 | |||
| 199 | RasterGrid m_grid; | ||
| 200 | std::vector<T> m_owned; | ||
| 201 | T* m_cells = nullptr; | ||
| 202 | std::optional<T> m_noData; | ||
| 203 | }; | ||
| 204 | |||
| 205 | } // namespace terraingen | ||
| 206 |