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 | 4717 | RasterGrid result{cells.Empty() ? 0 : cells.colEnd - cells.colBegin, | |
| 32 | 4717 | cells.Empty() ? 0 : cells.rowEnd - cells.rowBegin, grid.transform}; | |
| 33 | 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 | 33891 | const std::vector<double> shifts = | |
| 45 | 33891 | surface.IsBody() ? std::vector<double>{-360.0, 0.0, 360.0} : std::vector<double>{0.0}; | |
| 46 | 33891 | std::vector<PixelWindow> windows; | |
| 47 | 78246 | for (const double shift : shifts) { | |
| 48 | 44355 | const PixelWindow window = | |
| 49 | 44355 | PixelWindowIn(grid, {box.minX + shift, box.minY, box.maxX + shift, box.maxY}); | |
| 50 | 73380 | if (!window.Empty()) { | |
| 51 | 27491 | windows.push_back(window); | |
| 52 | } | ||
| 53 | } | ||
| 54 | 33891 | return windows; | |
| 55 | 33891 | } | |
| 56 | |||
| 57 | } // namespace terraingen | ||
| 58 |