src/raster/grid.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/raster/grid.h" | ||
| 4 | |||
| 5 | #include "raster/mapped_bounds.h" | ||
| 6 | #include "util/checks.h" | ||
| 7 | |||
| 8 | #include <algorithm> | ||
| 9 | #include <cmath> | ||
| 10 | #include <stdexcept> | ||
| 11 | |||
| 12 | namespace terraingen { | ||
| 13 | |||
| 14 | 1823 | Bounds2d RasterGrid::Bounds() const { | |
| 15 | 1823 | return MappedBounds({0.0, 0.0, static_cast<double>(width), static_cast<double>(height)}, | |
| 16 | 7292 | [&](double col, double row) { return transform.PixelToWorld(col, row); }); | |
| 17 | } | ||
| 18 | |||
| 19 | 471 | double RasterGrid::Area() const { | |
| 20 | 471 | return static_cast<double>(width) * static_cast<double>(height) * PixelSize() * PixelSize(); | |
| 21 | } | ||
| 22 | |||
| 23 | 230401 | double RasterGrid::PixelSize() const { | |
| 24 | 230401 | return std::sqrt(std::fabs(transform.pixelWidth * transform.pixelHeight - | |
| 25 | 230401 | transform.rowRotation * transform.colRotation)); | |
| 26 | } | ||
| 27 | |||
| 28 | 229116 | double RasterGrid::Spacing(const Surface& surface) const { | |
| 29 | 229116 | if (!surface.IsBody()) { | |
| 30 | 227185 | return PixelSize(); | |
| 31 | } | ||
| 32 | 3862 | return transform.CellHeight() * surface.UnitLength(); | |
| 33 | } | ||
| 34 | |||
| 35 | 528 | RasterGrid RasterGrid::Covering(const Bounds2d& box, double pixelSize) { | |
| 36 | 528 | RequirePositive(pixelSize, "Pixel size"); | |
| 37 | 526 | if (!box.IsFinite() || box.maxX < box.minX || box.maxY < box.minY) { | |
| 38 | 3 | throw std::invalid_argument( | |
| 39 | 3 | "A grid must cover a finite box with its max at or above its min"); | |
| 40 | } | ||
| 41 | 523 | const double columns = SnappedCells(box.maxX - box.minX, pixelSize); | |
| 42 | 523 | const double rows = SnappedCells(box.maxY - box.minY, pixelSize); | |
| 43 | 523 | if (columns > 2147483647.0 || rows > 2147483647.0) { | |
| 44 | 1 | throw std::invalid_argument("The grid would be too large"); | |
| 45 | } | ||
| 46 | 522 | return {static_cast<std::size_t>(columns), static_cast<std::size_t>(rows), | |
| 47 | 522 | GeoTransform::NorthUp(box.minX, box.minY + rows * pixelSize, pixelSize)}; | |
| 48 | } | ||
| 49 | |||
| 50 | 87 | RasterGrid RasterGrid::Centered(const Vec2d& center, const Vec2d& size, double pixelSize) { | |
| 51 | 87 | RequireNonNegative(size.x, "Grid width"); | |
| 52 | 86 | RequireNonNegative(size.y, "Grid height"); | |
| 53 | 86 | const double halfX = 0.5 * size.x; | |
| 54 | 86 | const double halfY = 0.5 * size.y; | |
| 55 | 86 | return Covering({center.x - halfX, center.y - halfY, center.x + halfX, center.y + halfY}, | |
| 56 | 86 | pixelSize); | |
| 57 | } | ||
| 58 | |||
| 59 | 1896 | double SnappedCells(double span, double cell) { | |
| 60 | 1896 | return std::max(1.0, std::ceil(span / cell - kGridSnapTolerance)); | |
| 61 | } | ||
| 62 | |||
| 63 | 2011 | double ResolvedSpacing(const RasterGrid& grid, double spacing, const Surface& surface) { | |
| 64 | 2011 | if (!(grid.PixelSize() > 0.0)) { | |
| 65 | 2 | throw std::domain_error("The grid's transform is not invertible"); | |
| 66 | } | ||
| 67 | 3012 | return std::max(spacing, grid.Spacing(surface)); | |
| 68 | } | ||
| 69 | |||
| 70 | 446 | double ListedSpacing(const RasterGrid& grid, double spacing, const Surface& surface) { | |
| 71 | 446 | return spacing > 0.0 ? spacing : grid.Spacing(surface); | |
| 72 | } | ||
| 73 | |||
| 74 | 529 | double RasterGrid::SurfaceArea(const Surface& surface) const { | |
| 75 | 529 | if (!surface.IsBody()) { | |
| 76 | 467 | return Area(); | |
| 77 | } | ||
| 78 | 62 | const Bounds2d box = Bounds(); | |
| 79 | 62 | const double turn = std::fmin(box.maxX - box.minX, 360.0) * kDegree; | |
| 80 | 62 | const double south = std::fmax(box.minY, -90.0) * kDegree; | |
| 81 | 62 | const double north = std::fmin(box.maxY, 90.0) * kDegree; | |
| 82 | 62 | if (surface.IsSphere()) { | |
| 83 | 57 | const double radius = surface.Radius(); | |
| 84 | 57 | return radius * radius * turn * std::fmax(0.0, std::sin(north) - std::sin(south)); | |
| 85 | } | ||
| 86 | 5 | if (!(north > south)) { | |
| 87 | return 0.0; | ||
| 88 | } | ||
| 89 | 4 | constexpr int kSteps = 64; | |
| 90 | 4 | const double west = box.minX * kDegree; | |
| 91 | 4 | double area = 0.0; | |
| 92 | 260 | for (int j = 0; j < kSteps; ++j) { | |
| 93 | 256 | const double lat = south + (north - south) * (j + 0.5) / kSteps; | |
| 94 | 16640 | for (int i = 0; i < kSteps; ++i) { | |
| 95 | 16384 | const double lon = west + turn * (i + 0.5) / kSteps; | |
| 96 | 16384 | area += surface.AreaPerSteradian({lon * kRadian, lat * kRadian}) * std::cos(lat); | |
| 97 | } | ||
| 98 | } | ||
| 99 | 4 | return area * turn * (north - south) / (kSteps * kSteps); | |
| 100 | } | ||
| 101 | |||
| 102 | } // namespace terraingen | ||
| 103 |