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 | 3298403 | if (!(std::fabs(value) < kLimit)) { | |
| 18 | 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 | 825744 | if (!(region.IsFinite() && region.maxX > region.minX && region.maxY > region.minY)) { | |
| 26 | 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 | 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 | 825495 | std::vector<CellIndex> cells; | |
| 47 | 825495 | cells.reserve(static_cast<std::size_t>(iMax - iMin + 1) * | |
| 48 | 825495 | static_cast<std::size_t>(jMax - jMin + 1)); | |
| 49 | 2198439 | for (std::int32_t j = jMin; j <= jMax; ++j) { | |
| 50 | 4206139 | for (std::int32_t i = iMin; i <= iMax; ++i) { | |
| 51 | 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 |