./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
23 / 25
92.0%
Branches:
92 / 109
84.4%
Functions:
2 / 3, 3 excl
66.7%

include/terraingen/geometry/cells.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Surface cells for per-cell feature generation: planar squares and cube-sphere cells.
5 #pragma once
6
7 #include "terraingen/geometry/geometry.h"
8 #include "terraingen/geometry/surface.h"
9
10 #include <array>
11 #include <cstdint>
12 #include <memory>
13 #include <vector>
14
15 namespace terraingen {
16
17 namespace detail {
18 struct CubeFace;
19 } // namespace detail
20
21 /// Integer coordinates of one cell in a grid of square cells.
22 struct CellIndex {
23 std::int32_t i = 0; ///< Column, or face * n + column on a cube sphere.
24 std::int32_t j = 0; ///< Row.
25
26 6852029 bool operator==(const CellIndex& other) const {
27
27/32
✓ Branch 0 taken 1215593 times.
✓ Branch 1 taken 521516 times.
✓ Branch 2 taken 119776 times.
✓ Branch 3 taken 1095817 times.
✓ Branch 4 taken 4412846 times.
✓ Branch 5 taken 400368 times.
✓ Branch 6 taken 617852 times.
✓ Branch 7 taken 3794994 times.
✓ Branch 8 taken 14955 times.
✓ Branch 9 taken 200080 times.
✓ Branch 10 taken 13306 times.
✓ Branch 11 taken 1649 times.
✓ Branch 12 taken 17161 times.
✓ Branch 13 taken 38258 times.
✓ Branch 14 taken 12462 times.
✓ Branch 15 taken 4699 times.
✓ Branch 16 taken 145 times.
✓ Branch 17 taken 26 times.
✓ Branch 18 taken 71 times.
✓ Branch 19 taken 74 times.
✓ Branch 20 taken 97 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 17 times.
✓ Branch 23 taken 80 times.
✓ Branch 24 taken 17 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 17 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 20001 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 20001 times.
✗ Branch 31 not taken.
6666855 return i == other.i && j == other.j;
28 }
29 168933 bool operator!=(const CellIndex& other) const {
30
2/2
✓ Branch 0 taken 157965 times.
✓ Branch 1 taken 10968 times.
319679 return !(*this == other);
31 }
32 /// Orders cells by `i`, then `j`.
33 36654641 bool operator<(const CellIndex& other) const {
34
30/34
✓ Branch 0 taken 380 times.
✓ Branch 0 taken 118434 times.
✓ Branch 1 taken 463 times.
✓ Branch 1 taken 313042 times.
✗ Branch 2 not taken.
✓ Branch 2 taken 212 times.
✓ Branch 3 taken 1 time.
✓ Branch 3 taken 200 times.
✗ Branch 4 not taken.
✓ Branch 4 taken 5511118 times.
✓ Branch 5 taken 178400 times.
✓ Branch 5 taken 8852064 times.
✓ Branch 6 taken 4246913 times.
✓ Branch 7 taken 3415096 times.
✓ Branch 8 taken 4190723 times.
✓ Branch 9 taken 3449646 times.
✓ Branch 10 taken 215565 times.
✓ Branch 11 taken 193586 times.
✓ Branch 12 taken 122167 times.
✓ Branch 13 taken 97277 times.
✓ Branch 14 taken 53539 times.
✓ Branch 15 taken 53917 times.
✓ Branch 16 taken 101680 times.
✓ Branch 17 taken 88027 times.
✓ Branch 18 taken 47968 times.
✓ Branch 19 taken 67722 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 1888904 times.
✓ Branch 23 taken 1071161 times.
✓ Branch 24 taken 1242226 times.
✓ Branch 25 taken 660874 times.
✓ Branch 26 taken 170334 times.
✓ Branch 27 taken 303002 times.
36654641 return i != other.i ? i < other.i : j < other.j;
35 }
36 bool operator>(const CellIndex& other) const {
37 return other < *this;
38 }
39 bool operator<=(const CellIndex& other) const {
40 return !(other < *this);
41 }
42 bool operator>=(const CellIndex& other) const {
43 return !(*this < other);
44 }
45 };
46
47 /// Partitions a surface into cells so features can be generated per cell, each from its
48 /// own random substream, identically however a region is tiled.
49
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
14442 class SurfaceCells {
50 public:
51 8424 virtual ~SurfaceCells() = default;
52
53 /// The cells that intersect `region` (surface coordinates), in a fixed order.
54 /// @param cellSize The cell side in meters.
55 /// @throws std::invalid_argument if `cellSize` is not positive and finite, or `region` is not
56 /// finite or not one the implementation accepts.
57 virtual std::vector<CellIndex> CellsCovering(const Bounds2d& region, double cellSize) const = 0;
58
59 /// The area of a cell in m^2.
60 /// @param cellSize The cell side in meters.
61 virtual double CellArea(const CellIndex& cell, double cellSize) const = 0;
62
63 /// Maps uniform `u` and `v` in [0, 1) to a point uniform over a cell, in surface coordinates;
64 /// uniform over `Sampling`'s measure where that thins.
65 /// @param cellSize The cell side in meters.
66 virtual Vec2d SamplePoint(double u, double v, const CellIndex& cell, double cellSize) const = 0;
67
68 /// How to place features in a cell uniformly by area: draw a Poisson count over `area`
69 /// m^2, place each at `SamplePoint`, and keep it with chance `Weight(point) / bound` when
70 /// `bound` is positive. Without thinning (`bound` 0) `area` is `CellArea` and every point is
71 /// kept, as on a plane or a sphere.
72 struct Sampling {
73 double area = 0.0; ///< Area in m^2 the Poisson count is drawn over, >= `CellArea`.
74 double bound = 0.0; ///< Upper bound of `Weight` over the cell; 0 when nothing is thinned.
75 };
76
77 /// The sampling of a cell, as `Sampling` describes.
78 /// @param cellSize The cell side in meters.
79 123082 virtual Sampling CellSampling(const CellIndex& cell, double cellSize) const {
80 123082 return {CellArea(cell, cellSize), 0.0};
81 }
82
83 /// The weight of a sampled point, kept with chance `Weight(point) / Sampling::bound`.
84 ✗ virtual double Weight(const Vec2d& /*point*/) const {
85 ✗ return 1.0;
86 }
87 };
88
89 /// Square cells on a plane: cell (i, j) of side L covers [i L, (i + 1) L) x
90 /// [j L, (j + 1) L).
91
24/32
<unknown function 26>:
✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 time.
<unknown function 49>:
✓ Branch 1 taken 1 time.
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 2 times.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 2 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✓ Branch 14 taken 1 time.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 18 taken 2 times.
✓ Branch 21 taken 2 times.
✓ Branch 24 taken 2 times.
✗ Branch 27 not taken.
✓ Branch 31 taken 1 time.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✓ Branch 35 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 42 taken 2 times.
✓ Branch 45 taken 4 times.
✓ Branch 48 taken 4 times.
✓ Branch 53 taken 2 times.
✓ Branch 56 taken 2 times.
✓ Branch 59 taken 2 times.
✓ Branch 62 taken 2 times.
8508 class PlanarCells final : public SurfaceCells {
92 public:
93 /// The cells intersecting `region` (meters), in row-major order: increasing j, then i.
94 /// @param cellSize The cell side in meters.
95 /// @throws std::invalid_argument if `cellSize` is not positive and finite, `region` is empty or
96 /// not finite, or a cell index would overflow 32 bits.
97 std::vector<CellIndex> CellsCovering(const Bounds2d& region, double cellSize) const override;
98
99 /// `cellSize` squared, in m^2.
100 double CellArea(const CellIndex& cell, double cellSize) const override;
101
102 /// The point at fractions `u` and `v` in [0, 1) across a cell in x and y, in meters.
103 Vec2d SamplePoint(double u, double v, const CellIndex& cell, double cellSize) const override;
104 };
105
106 /// Near-square cells on a body, in longitude and latitude degrees: each face of a cube split into
107 /// n x n cells along equiangular coordinates of the direction from the center.
108 ///
109 /// For a cell side L, n = ceil(R pi / (2 L)), with R the sphere's radius or an ellipsoid's
110 /// shortest semi-axis, so no cell is narrower than L. Cell (i, j) is column i mod n and row j of
111 /// face i / n (+x, -x, +y, -y, +z, -z). On a sphere cells are near-equal-area; on an ellipsoid
112 /// `CellArea` is each cell's true area and `CellSampling` thins points to be uniform by area.
113 class CubeSphereCells final : public SurfaceCells {
114 public:
115 /// Cells on a sphere of `radius` meters.
116 /// @throws std::invalid_argument unless `radius` is positive and finite.
117 explicit CubeSphereCells(double radius);
118
119 /// Cells on a body, a sphere or an ellipsoid.
120 /// @throws std::invalid_argument for a plane.
121 explicit CubeSphereCells(const Surface& surface);
122
123 /// Every cell reaching into a region, plus some next to them, sorted by i, then j.
124 /// @param region The region in longitude and latitude degrees; longitudes may run past
125 /// [-180, 180].
126 /// @param cellSize The cell side in meters.
127 /// @throws std::invalid_argument if `cellSize` is not positive and finite, the cells would be
128 /// too small for the sphere, or `region` is not finite or inverted; a point is allowed.
129 std::vector<CellIndex> CellsCovering(const Bounds2d& region, double cellSize) const override;
130
131 /// The area of a cell in m^2: exact from its solid angle on a sphere, by quadrature of the area
132 /// element on an ellipsoid.
133 /// @throws std::invalid_argument as `CellsPerEdge`.
134 double CellArea(const CellIndex& cell, double cellSize) const override;
135
136 /// `CellArea` without thinning on a sphere; on an ellipsoid, the cell's solid angle times a
137 /// guaranteed bound of the area per steradian over it (from interval bounds of its directions,
138 /// capped by the bound over the whole body), thinned by `Weight`.
139 /// @throws std::invalid_argument as `CellsPerEdge`.
140 Sampling CellSampling(const CellIndex& cell, double cellSize) const override;
141
142 /// The area per steradian at a point (`Surface::AreaPerSteradian`).
143 double Weight(const Vec2d& point) const override;
144
145 /// Maps uniform `u` and `v` in [0, 1) to a point uniform in solid angle over a cell, as
146 /// longitude in (-180, 180] and latitude, in degrees.
147 /// @throws std::invalid_argument as `CellsPerEdge`.
148 Vec2d SamplePoint(double u, double v, const CellIndex& cell, double cellSize) const override;
149
150 /// The cell holding a point given in degrees.
151 /// @param cellSize The cell side in meters.
152 /// @throws std::invalid_argument as `CellsPerEdge`.
153 CellIndex CellAt(double lonDeg, double latDeg, double cellSize) const;
154
155 /// A cell and the cells up to `rings` steps from it along its face's rows and columns,
156 /// continuing onto neighboring faces past an edge, without repeats, sorted by i, then j.
157 /// @param cellSize The cell side in meters.
158 /// @throws std::invalid_argument as `CellsPerEdge`.
159 std::vector<CellIndex> Neighbors(const CellIndex& cell, double cellSize, int rings) const;
160
161 /// The unit direction of a face at equiangular coordinates.
162 /// @param face The face, 0 to 5: +x, -x, +y, -y, +z, -z.
163 /// @param alpha Angle from the face's center along its first axis, radians in [-pi/4, pi/4].
164 /// @param beta Angle from the face's center along its second axis, radians in [-pi/4, pi/4].
165 static Vec3d Direction(int face, double alpha, double beta);
166
167 /// The number of cells along each edge of a face, n = ceil(R pi / (2 `cellSize`)).
168 /// @param cellSize The cell side in meters.
169 /// @throws std::invalid_argument if `cellSize` is not positive and finite or the cells would be
170 /// too small for the sphere.
171 std::int32_t CellsPerEdge(double cellSize) const;
172
173 /// The radius in meters the cells are sized by: the sphere's, or an ellipsoid's shortest.
174 4380938 double Radius() const {
175
7/7
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 25853 times.
✓ Branch 4 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 4267466 times.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
4380938 return m_sphere.Radius();
176 }
177
178 private:
179 /// The gnomonic bounds a1, a2, b1, b2 of a cell on its face of `n` cells per edge.
180 std::array<double, 4> Bounds(const CellIndex& cell, std::int32_t n) const;
181
182 /// An upper bound of `Weight` over the gnomonic rectangle a1, a2, b1, b2 of `face`, from
183 /// interval bounds of the direction's squared components.
184 double WeightBound(std::size_t face, const std::array<double, 4>& bounds) const;
185
186 /// Adds the cells holding sample points of cell (`col`, `row`) of `face`, which lies past the
187 /// face's edge, so on its neighbors.
188 void AddAcrossEdge(const detail::CubeFace& face, std::int32_t col, std::int32_t row,
189 std::int32_t n, double cellSize, std::vector<CellIndex>& cells) const;
190
191 /// The cells reaching a `box` under half a cell across, found among the one-ring neighbors
192 /// of the cell holding its center; empty when the box is too large, the cells too coarse,
193 /// or the box near a pole, so `CellsCovering` falls back to sampling it.
194 std::vector<CellIndex> CellsNear(const Bounds2d& box, double cellSize) const;
195
196 /// The sorted cells holding a lattice of points over `box`, finer than the cells.
197 std::vector<CellIndex> Seeds(const Bounds2d& box, double cellSize) const;
198
199 /// The `seeds` plus the ring `cells` around them that may reach `box`.
200 std::vector<CellIndex> KeepReaching(const Bounds2d& box, const std::vector<CellIndex>& seeds,
201 const std::vector<CellIndex>& cells, double cellSize) const;
202
203 /// `CellArea` computed rather than recalled.
204 double ComputeCellArea(const CellIndex& cell, double cellSize) const;
205
206 Surface m_surface;
207 Surface m_sphere; ///< The sphere of `Radius`, which cell geometry is computed on.
208 double m_weightCap = 0.0; ///< Bound of `Weight` over the whole ellipsoid; 0 on a sphere.
209 /// These cells among every set made, which the threads' recalled areas key on.
210 std::uint64_t m_id = 0;
211 };
212
213 /// The cells features are generated in on `surface`: `PlanarCells` on a plane, `CubeSphereCells`
214 /// on a body.
215 std::shared_ptr<const SurfaceCells> MakeCells(const Surface& surface);
216
217 } // namespace terraingen
218