./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
58 / 58
100.0%
Branches:
32 / 35
91.4%
Functions:
10 / 10
100.0%

src/raster/grid.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/raster/grid.h"
4
5 #include "raster/mapped_bounds.h"
6 #include "util/checks.h"
7
8 #include <algorithm>
9 #include <cmath>
10 #include <stdexcept>
11
12 namespace terraingen {
13
14 1823 Bounds2d RasterGrid::Bounds() const {
15 1823 return MappedBounds({0.0, 0.0, static_cast<double>(width), static_cast<double>(height)},
16 7292 [&](double col, double row) { return transform.PixelToWorld(col, row); });
17 }
18
19 471 double RasterGrid::Area() const {
20 471 return static_cast<double>(width) * static_cast<double>(height) * PixelSize() * PixelSize();
21 }
22
23 230401 double RasterGrid::PixelSize() const {
24 230401 return std::sqrt(std::fabs(transform.pixelWidth * transform.pixelHeight -
25 230401 transform.rowRotation * transform.colRotation));
26 }
27
28 229116 double RasterGrid::Spacing(const Surface& surface) const {
29
2/2
✓ Branch 0 taken 227185 times.
✓ Branch 1 taken 1931 times.
229116 if (!surface.IsBody()) {
30 227185 return PixelSize();
31 }
32
1/2
✓ Branch 0 taken 1931 times.
✗ Branch 1 not taken.
3862 return transform.CellHeight() * surface.UnitLength();
33 }
34
35 528 RasterGrid RasterGrid::Covering(const Bounds2d& box, double pixelSize) {
36 528 RequirePositive(pixelSize, "Pixel size");
37
5/6
✓ Branch 0 taken 525 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 523 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 523 times.
✗ Branch 5 not taken.
526 if (!box.IsFinite() || box.maxX < box.minX || box.maxY < box.minY) {
38 3 throw std::invalid_argument(
39
1/1
✓ Branch 2 taken 3 times.
3 "A grid must cover a finite box with its max at or above its min");
40 }
41 523 const double columns = SnappedCells(box.maxX - box.minX, pixelSize);
42 523 const double rows = SnappedCells(box.maxY - box.minY, pixelSize);
43
3/4
✓ Branch 0 taken 522 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 522 times.
523 if (columns > 2147483647.0 || rows > 2147483647.0) {
44
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("The grid would be too large");
45 }
46 522 return {static_cast<std::size_t>(columns), static_cast<std::size_t>(rows),
47 522 GeoTransform::NorthUp(box.minX, box.minY + rows * pixelSize, pixelSize)};
48 }
49
50 87 RasterGrid RasterGrid::Centered(const Vec2d& center, const Vec2d& size, double pixelSize) {
51 87 RequireNonNegative(size.x, "Grid width");
52 86 RequireNonNegative(size.y, "Grid height");
53 86 const double halfX = 0.5 * size.x;
54 86 const double halfY = 0.5 * size.y;
55 86 return Covering({center.x - halfX, center.y - halfY, center.x + halfX, center.y + halfY},
56 86 pixelSize);
57 }
58
59 1896 double SnappedCells(double span, double cell) {
60
2/2
✓ Branch 0 taken 1881 times.
✓ Branch 1 taken 15 times.
1896 return std::max(1.0, std::ceil(span / cell - kGridSnapTolerance));
61 }
62
63 2011 double ResolvedSpacing(const RasterGrid& grid, double spacing, const Surface& surface) {
64
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2009 times.
2011 if (!(grid.PixelSize() > 0.0)) {
65
1/1
✓ Branch 2 taken 2 times.
2 throw std::domain_error("The grid's transform is not invertible");
66 }
67
2/2
✓ Branch 1 taken 1003 times.
✓ Branch 2 taken 1006 times.
3012 return std::max(spacing, grid.Spacing(surface));
68 }
69
70 446 double ListedSpacing(const RasterGrid& grid, double spacing, const Surface& surface) {
71
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 206 times.
446 return spacing > 0.0 ? spacing : grid.Spacing(surface);
72 }
73
74 529 double RasterGrid::SurfaceArea(const Surface& surface) const {
75
2/2
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 62 times.
529 if (!surface.IsBody()) {
76 467 return Area();
77 }
78 62 const Bounds2d box = Bounds();
79 62 const double turn = std::fmin(box.maxX - box.minX, 360.0) * kDegree;
80 62 const double south = std::fmax(box.minY, -90.0) * kDegree;
81 62 const double north = std::fmin(box.maxY, 90.0) * kDegree;
82
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 5 times.
62 if (surface.IsSphere()) {
83 57 const double radius = surface.Radius();
84 57 return radius * radius * turn * std::fmax(0.0, std::sin(north) - std::sin(south));
85 }
86
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 if (!(north > south)) {
87 return 0.0;
88 }
89 4 constexpr int kSteps = 64;
90 4 const double west = box.minX * kDegree;
91 4 double area = 0.0;
92
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 4 times.
260 for (int j = 0; j < kSteps; ++j) {
93 256 const double lat = south + (north - south) * (j + 0.5) / kSteps;
94
2/2
✓ Branch 0 taken 16384 times.
✓ Branch 1 taken 256 times.
16640 for (int i = 0; i < kSteps; ++i) {
95 16384 const double lon = west + turn * (i + 0.5) / kSteps;
96 16384 area += surface.AreaPerSteradian({lon * kRadian, lat * kRadian}) * std::cos(lat);
97 }
98 }
99 4 return area * turn * (north - south) / (kSteps * kSteps);
100 }
101
102 } // namespace terraingen
103