./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
3 / 3
100.0%
Branches:
6 / 7
85.7%
Functions:
1 / 1, 1 excl
100.0%

include/terraingen/raster/grid.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Raster grids without values, and resampled grids and spacings derived from them.
5 #pragma once
6
7 #include "terraingen/geometry/geometry.h"
8 #include "terraingen/geometry/surface.h"
9 #include "terraingen/raster/geo_transform.h"
10
11 #include <cstddef>
12
13 namespace terraingen {
14
15 /// A grid of cells without values: its size and pixel-to-world transform.
16 ///
17 /// Terrains are evaluated over grids, so a tile of a larger grid is a grid on the same lattice.
18
3/3
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 546 times.
✓ Branch 4 taken 1 time.
663 struct RasterGrid {
19 std::size_t width = 0; ///< Columns.
20 std::size_t height = 0; ///< Rows.
21 GeoTransform transform{}; ///< Pixel-to-world transform.
22
23 /// The north-up grid of square cells of side `pixelSize` covering `box`, snapped outward to
24 /// whole cells: it starts at the box's min corner and has `SnappedCells` cells along each axis,
25 /// so its max corner lies at or beyond the box's. Configuration outputs snap their extents so.
26 /// @param pixelSize In the box's units: meters on a plane, degrees on a body.
27 /// @throws std::invalid_argument for a box that is not finite or has a max below its min, a
28 /// `pixelSize` that is not positive and finite, or more than 2^31 - 1 cells a side.
29 static RasterGrid Covering(const Bounds2d& box, double pixelSize);
30
31 /// The grid `Covering` the box `size` across centered on `center`.
32 /// @throws std::invalid_argument as `Covering`, or for a negative size.
33 static RasterGrid Centered(const Vec2d& center, const Vec2d& size, double pixelSize);
34
35 /// The axis-aligned box the grid covers, in world coordinates.
36 Bounds2d Bounds() const;
37
38 /// The grid's area in square world units.
39 double Area() const;
40
41 /// The side of a cell in world units: the square root of a cell's area.
42 double PixelSize() const;
43
44 /// The area in m^2 the grid covers on a surface: `Area` on a plane, else the area of its
45 /// longitude-latitude box, clamped to the poles and at most a full turn.
46 double SurfaceArea(const Surface& surface) const;
47
48 /// The grid's sample spacing in meters on a surface: `PixelSize` on a plane, on a sphere the
49 /// latitude step of a cell (its largest side).
50 double Spacing(const Surface& surface) const;
51
52 /// Memberwise equality.
53 438 bool operator==(const RasterGrid& other) const {
54
3/4
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 405 times.
✗ Branch 3 not taken.
438 return width == other.width && height == other.height && transform == other.transform;
55 }
56 bool operator!=(const RasterGrid& other) const {
57 return !(*this == other);
58 }
59 };
60
61 /// Fraction of a cell a span may exceed a whole number of cells by without gaining a cell.
62 inline constexpr double kGridSnapTolerance = 1e-6;
63
64 /// The cells of side `cell` that cover `span` snapped outward: ceil(`span` / `cell` -
65 /// `kGridSnapTolerance`), at least 1, as a double so callers can check it before converting.
66 double SnappedCells(double span, double cell);
67
68 /// The spacing a grid is evaluated at: the larger of `spacing` and the grid's own spacing on
69 /// `surface` (`RasterGrid::Spacing`).
70 /// @param spacing The requested spacing in meters; 0 for the grid's own.
71 /// @throws std::domain_error if the grid's transform is not invertible.
72 double ResolvedSpacing(const RasterGrid& grid, double spacing, const Surface& surface);
73
74 /// The spacing features are listed at over a grid: `spacing` when given, finer than the grid's
75 /// own or not, else the grid's own spacing on `surface`.
76 /// @param spacing The requested spacing in meters; 0 for the grid's own.
77 double ListedSpacing(const RasterGrid& grid, double spacing, const Surface& surface);
78
79 } // namespace terraingen
80