./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
29 / 29
100.0%
Branches:
16 / 18
88.9%
Functions:
4 / 4
100.0%

src/raster/cell_loops.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/raster/cell_loops.h"
4
5 #include "raster/mapped_bounds.h"
6 #include "terraingen/util/bits.h"
7
8 #include <cmath>
9
10 namespace terraingen {
11
12 namespace {
13
14 /// `value` clamped to [0, `limit`].
15 179492 std::size_t ClampTo(double value, std::size_t limit) {
16 179492 return ClampIndex(value, limit + 1);
17 }
18
19 } // namespace
20
21 44873 PixelWindow PixelWindowIn(const RasterGrid& grid, const Bounds2d& box) {
22 44873 const Bounds2d pixels =
23 224365 MappedBounds(box, [&](double x, double y) { return grid.transform.WorldToPixel(x, y); });
24 44873 return {ClampTo(std::floor(pixels.minX), grid.width),
25 44873 ClampTo(std::ceil(pixels.maxX), grid.width),
26 44873 ClampTo(std::floor(pixels.minY), grid.height),
27 44873 ClampTo(std::ceil(pixels.maxY), grid.height)};
28 }
29
30 2357 RasterGrid Subgrid(const RasterGrid& grid, const PixelWindow& cells) {
31
1/2
✓ Branch 0 taken 2360 times.
✗ Branch 1 not taken.
4717 RasterGrid result{cells.Empty() ? 0 : cells.colEnd - cells.colBegin,
32
1/2
✓ Branch 0 taken 2360 times.
✗ Branch 1 not taken.
4717 cells.Empty() ? 0 : cells.rowEnd - cells.rowBegin, grid.transform};
33
4/4
✓ Branch 0 taken 1428 times.
✓ Branch 1 taken 929 times.
✓ Branch 2 taken 107 times.
✓ Branch 3 taken 1321 times.
2357 if (cells.colBegin != 0 || cells.rowBegin != 0) {
34 1036 const Vec2d corner = grid.transform.PixelToWorld(static_cast<double>(cells.colBegin),
35 1036 static_cast<double>(cells.rowBegin));
36 1036 result.transform.originX = corner.x;
37 1036 result.transform.originY = corner.y;
38 }
39 2357 return result;
40 }
41
42 33891 std::vector<PixelWindow> PixelWindowsIn(const RasterGrid& grid, const Bounds2d& box,
43 const Surface& surface) {
44
2/2
✓ Branch 0 taken 5232 times.
✓ Branch 1 taken 28659 times.
33891 const std::vector<double> shifts =
45
2/2
✓ Branch 0 taken 5232 times.
✓ Branch 1 taken 28659 times.
33891 surface.IsBody() ? std::vector<double>{-360.0, 0.0, 360.0} : std::vector<double>{0.0};
46 33891 std::vector<PixelWindow> windows;
47
2/2
✓ Branch 0 taken 44355 times.
✓ Branch 1 taken 33891 times.
78246 for (const double shift : shifts) {
48 44355 const PixelWindow window =
49
1/1
✓ Branch 1 taken 44355 times.
44355 PixelWindowIn(grid, {box.minX + shift, box.minY, box.maxX + shift, box.maxY});
50
2/2
✓ Branch 0 taken 29025 times.
✓ Branch 1 taken 15330 times.
73380 if (!window.Empty()) {
51
1/1
✓ Branch 1 taken 27491 times.
27491 windows.push_back(window);
52 }
53 }
54 33891 return windows;
55 33891 }
56
57 } // namespace terraingen
58