./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
31 / 31
100.0%
Branches:
19 / 19
100.0%
Functions:
6 / 6
100.0%

src/geometry/planar_cells.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/geometry/cells.h"
4
5 #include "util/checks.h"
6
7 #include <cmath>
8 #include <limits>
9 #include <stdexcept>
10
11 namespace terraingen {
12
13 namespace {
14
15 3298403 std::int32_t CellCoordinate(double value) {
16 3298403 constexpr double kLimit = std::numeric_limits<std::int32_t>::max();
17
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3298402 times.
3298403 if (!(std::fabs(value) < kLimit)) {
18
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Region is too far from the origin for its cell size");
19 }
20 3298402 return static_cast<std::int32_t>(value);
21 }
22
23 /// Checks a region: finite and not empty.
24 825744 void CheckRegion(const Bounds2d& region) {
25
6/6
✓ Branch 0 taken 825743 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 825739 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 825738 times.
✓ Branch 5 taken 1 time.
825744 if (!(region.IsFinite() && region.maxX > region.minX && region.maxY > region.minY)) {
26
1/1
✓ Branch 2 taken 6 times.
6 throw std::invalid_argument("Region must be finite and not empty");
27 }
28 825738 }
29
30 } // namespace
31
32 14388 std::shared_ptr<const SurfaceCells> MakeCells(const Surface& surface) {
33
2/2
✓ Branch 0 taken 6091 times.
✓ Branch 1 taken 8297 times.
14388 if (surface.IsBody()) {
34 6091 return std::make_shared<CubeSphereCells>(surface);
35 }
36 8297 return std::make_shared<PlanarCells>();
37 }
38
39 825663 std::vector<CellIndex> PlanarCells::CellsCovering(const Bounds2d& region, double cellSize) const {
40 825663 RequirePositive(cellSize, "Cell size");
41 825735 CheckRegion(region);
42 825857 const std::int32_t iMin = CellCoordinate(std::floor(region.minX / cellSize));
43 825527 const std::int32_t jMin = CellCoordinate(std::floor(region.minY / cellSize));
44 825526 const std::int32_t iMax = CellCoordinate(std::ceil(region.maxX / cellSize) - 1.0);
45 825519 const std::int32_t jMax = CellCoordinate(std::ceil(region.maxY / cellSize) - 1.0);
46
1/1
✓ Branch 1 taken 825831 times.
825495 std::vector<CellIndex> cells;
47 825495 cells.reserve(static_cast<std::size_t>(iMax - iMin + 1) *
48
1/1
✓ Branch 1 taken 825831 times.
825495 static_cast<std::size_t>(jMax - jMin + 1));
49
2/2
✓ Branch 0 taken 1371917 times.
✓ Branch 1 taken 826522 times.
2198439 for (std::int32_t j = jMin; j <= jMax; ++j) {
50
2/2
✓ Branch 0 taken 2833531 times.
✓ Branch 1 taken 1372608 times.
4206139 for (std::int32_t i = iMin; i <= iMax; ++i) {
51
1/1
✓ Branch 1 taken 2834222 times.
2833531 cells.push_back({i, j});
52 }
53 }
54 826522 return cells;
55 }
56
57 2956276 double PlanarCells::CellArea(const CellIndex& /*cell*/, double cellSize) const {
58 2956276 return cellSize * cellSize;
59 }
60
61 3253906 Vec2d PlanarCells::SamplePoint(double u, double v, const CellIndex& cell, double cellSize) const {
62 3253906 return {(cell.i + u) * cellSize, (cell.j + v) * cellSize};
63 }
64
65 } // namespace terraingen
66