Query raster source using bilinear interpolation
| 48 | |
| 49 | // Query raster source using bilinear interpolation |
| 50 | RasterDatum RasterSource::GetRasterInterpolate(const int lon, const int lat) const |
| 51 | { |
| 52 | if (lon < xmin || lon > xmax || lat < ymin || lat > ymax) |
| 53 | { |
| 54 | return {}; |
| 55 | } |
| 56 | |
| 57 | const auto xthP = (lon - xmin) / xstep; |
| 58 | const auto ythP = |
| 59 | (ymax - lat) / |
| 60 | ystep; // the raster texture uses a different coordinate system with y pointing downwards |
| 61 | |
| 62 | const std::size_t top = static_cast<std::size_t>(fmax(floor(ythP), 0)); |
| 63 | const std::size_t bottom = static_cast<std::size_t>(fmin(ceil(ythP), height - 1)); |
| 64 | const std::size_t left = static_cast<std::size_t>(fmax(floor(xthP), 0)); |
| 65 | const std::size_t right = static_cast<std::size_t>(fmin(ceil(xthP), width - 1)); |
| 66 | |
| 67 | // Calculate distances from corners for bilinear interpolation |
| 68 | const float fromLeft = xthP - left; // this is the fraction part of xthP |
| 69 | const float fromTop = ythP - top; // this is the fraction part of ythP |
| 70 | const float fromRight = 1 - fromLeft; |
| 71 | const float fromBottom = 1 - fromTop; |
| 72 | |
| 73 | return {static_cast<std::int32_t>(raster_data(left, top) * (fromRight * fromBottom) + |
| 74 | raster_data(right, top) * (fromLeft * fromBottom) + |
| 75 | raster_data(left, bottom) * (fromRight * fromTop) + |
| 76 | raster_data(right, bottom) * (fromLeft * fromTop))}; |
| 77 | } |
| 78 | |
| 79 | // Load raster source into memory |
| 80 | int RasterContainer::LoadRasterSource(const std::string &path_string, |
no outgoing calls
no test coverage detected