TerrainGen coverage


terraingen/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
25 / 25
100.0%
Branches:
22 / 25
88.0%
Functions:
4 / 4
100.0%

util/planar_cells.cpp
Line Branch Exec Source
1 #include "terraingen/util/cells.h"
2
3 #include "terraingen/util/checks.h"
4
5 #include <cmath>
6 #include <limits>
7 #include <stdexcept>
8
9 namespace terraingen {
10
11 namespace {
12
13 4559583 std::int32_t CellCoordinate(double value) {
14 4559583 constexpr double kLimit = std::numeric_limits<std::int32_t>::max();
15
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4559582 times.
4559583 if (!(std::fabs(value) < kLimit)) {
16
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Region is too far from the origin for its cell size");
17 }
18 4559582 return static_cast<std::int32_t>(value);
19 }
20
21 } // namespace
22
23 1139902 std::vector<CellIndex> PlanarCells::CellsCovering(const Bounds2d& region, double cellSize) const {
24
1/1
✓ Branch 2 taken 1139900 times.
1139902 RequirePositive(cellSize, "Cell size");
25
6/8
✓ Branch 1 taken 1139899 times.
✓ Branch 2 taken 1 time.
✓ Branch 4 taken 1139899 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1139899 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 1139896 times.
2279799 if (!(std::isfinite(region.minX) && std::isfinite(region.minY) && std::isfinite(region.maxX) &&
26
5/6
✓ Branch 1 taken 1139899 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1139897 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1139896 times.
1139899 std::isfinite(region.maxY) && region.maxX > region.minX && region.maxY > region.minY)) {
27
1/1
✓ Branch 2 taken 4 times.
4 throw std::invalid_argument("Region must be finite and not empty");
28 }
29 1139896 const std::int32_t iMin = CellCoordinate(std::floor(region.minX / cellSize));
30 1139896 const std::int32_t jMin = CellCoordinate(std::floor(region.minY / cellSize));
31 1139896 const std::int32_t iMax = CellCoordinate(std::ceil(region.maxX / cellSize) - 1.0);
32 1139895 const std::int32_t jMax = CellCoordinate(std::ceil(region.maxY / cellSize) - 1.0);
33 1139895 std::vector<CellIndex> cells;
34 1139895 cells.reserve(static_cast<std::size_t>(iMax - iMin + 1) *
35
1/1
✓ Branch 1 taken 1139895 times.
1139895 static_cast<std::size_t>(jMax - jMin + 1));
36
2/2
✓ Branch 0 taken 1771359 times.
✓ Branch 1 taken 1139895 times.
2911254 for (std::int32_t j = jMin; j <= jMax; ++j) {
37
2/2
✓ Branch 0 taken 3049262 times.
✓ Branch 1 taken 1771359 times.
4820621 for (std::int32_t i = iMin; i <= iMax; ++i) {
38
1/1
✓ Branch 1 taken 3049262 times.
3049262 cells.push_back({i, j});
39 }
40 }
41 1139895 return cells;
42 }
43
44 3245509 double PlanarCells::CellArea(const CellIndex& /*cell*/, double cellSize) const {
45 3245509 return cellSize * cellSize;
46 }
47
48 2977354 Vec2d PlanarCells::SamplePoint(double u, double v, const CellIndex& cell, double cellSize) const {
49 2977354 return {(cell.i + u) * cellSize, (cell.j + v) * cellSize};
50 }
51
52 } // namespace terraingen
53