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 | 4559583 | if (!(std::fabs(value) < kLimit)) { | |
| 16 | 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 | 1139902 | RequirePositive(cellSize, "Cell size"); | |
| 25 | 2279799 | if (!(std::isfinite(region.minX) && std::isfinite(region.minY) && std::isfinite(region.maxX) && | |
| 26 | 1139899 | std::isfinite(region.maxY) && region.maxX > region.minX && region.maxY > region.minY)) { | |
| 27 | 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 | 1139895 | static_cast<std::size_t>(jMax - jMin + 1)); | |
| 36 | 2911254 | for (std::int32_t j = jMin; j <= jMax; ++j) { | |
| 37 | 4820621 | for (std::int32_t i = iMin; i <= iMax; ++i) { | |
| 38 | 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 |