raster/cell_loops.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "terraingen/raster/cell_loops.h" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <limits> | ||
| 5 | |||
| 6 | namespace terraingen { | ||
| 7 | |||
| 8 | namespace { | ||
| 9 | |||
| 10 | 683900 | std::size_t ClampTo(double value, std::size_t limit) { | |
| 11 | 683900 | return static_cast<std::size_t>(std::clamp(value, 0.0, static_cast<double>(limit))); | |
| 12 | } | ||
| 13 | |||
| 14 | } // namespace | ||
| 15 | |||
| 16 | 170975 | PixelWindow PixelWindowIn(const RasterGrid& grid, const Bounds2d& box) { | |
| 17 | 170975 | const GeoTransform& transform = grid.transform; | |
| 18 | 170975 | constexpr double kInf = std::numeric_limits<double>::infinity(); | |
| 19 | 170975 | double colMin = kInf; | |
| 20 | 170975 | double colMax = -kInf; | |
| 21 | 170975 | double rowMin = kInf; | |
| 22 | 170975 | double rowMax = -kInf; | |
| 23 | 512925 | for (const double wx : {box.minX, box.maxX}) { | |
| 24 | 1025850 | for (const double wy : {box.minY, box.maxY}) { | |
| 25 | 683900 | const Vec2d pixel = transform.WorldToPixel(wx, wy); | |
| 26 | 683900 | colMin = std::fmin(colMin, pixel.x); | |
| 27 | 683900 | colMax = std::fmax(colMax, pixel.x); | |
| 28 | 683900 | rowMin = std::fmin(rowMin, pixel.y); | |
| 29 | 683900 | rowMax = std::fmax(rowMax, pixel.y); | |
| 30 | } | ||
| 31 | } | ||
| 32 | 170975 | return {ClampTo(std::floor(colMin), grid.width), ClampTo(std::ceil(colMax), grid.width), | |
| 33 | 170975 | ClampTo(std::floor(rowMin), grid.height), ClampTo(std::ceil(rowMax), grid.height)}; | |
| 34 | } | ||
| 35 | |||
| 36 | 3 | PixelWindow PixelWindowIn(const Raster<float>& raster, const Bounds2d& box) { | |
| 37 | 3 | return PixelWindowIn(RasterGrid::FromRaster(raster), box); | |
| 38 | } | ||
| 39 | |||
| 40 | 167902 | std::vector<PixelWindow> PixelWindowsIn(const RasterGrid& grid, const Bounds2d& box, | |
| 41 | const Surface& surface) { | ||
| 42 | 167902 | std::vector<PixelWindow> windows; | |
| 43 | 170972 | const auto add = [&](double shift) { | |
| 44 | const PixelWindow window = | ||
| 45 | 170972 | PixelWindowIn(grid, {box.minX + shift, box.minY, box.maxX + shift, box.maxY}); | |
| 46 | 170972 | if (!window.Empty()) { | |
| 47 | 136003 | windows.push_back(window); | |
| 48 | } | ||
| 49 | 170972 | }; | |
| 50 | 167902 | if (!surface.IsBody()) { | |
| 51 | 166367 | add(0.0); | |
| 52 | 166367 | return windows; | |
| 53 | } | ||
| 54 | 6140 | for (const double shift : {-360.0, 0.0, 360.0}) { | |
| 55 | 4605 | add(shift); | |
| 56 | } | ||
| 57 | 1535 | return windows; | |
| 58 | } | ||
| 59 | |||
| 60 | 167227 | std::vector<PixelWindow> PixelWindowsIn(const Raster<float>& raster, const Bounds2d& box, | |
| 61 | const Surface& surface) { | ||
| 62 | 167227 | return PixelWindowsIn(RasterGrid::FromRaster(raster), box, surface); | |
| 63 | } | ||
| 64 | |||
| 65 | } // namespace terraingen | ||
| 66 |