Implement bilinear interpolation

This commit is contained in:
robin committed 2024-04-22 00:21:11 +02:00
1 parent a123071fec
commit 51fe302920
12 files changed
+192 -15

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
#include <cmath>
#include <ranges>
#include "interpolate.h"
using namespace std;
// Inspired by https://numerical.recipes/book.html Chapter 3.6
point bilinearInterpolate(const UVGrid &uvGrid, std::tuple<int, double, double> timeLatLon) {
auto [time, lat, lon] = timeLatLon;
double latStep = uvGrid.latStep();
double lonStep = uvGrid.lonStep();
int timeStep = uvGrid.timeStep();
int latIndex = (lat-uvGrid.lats[0])/latStep;
int lonIndex = (lon-uvGrid.lons[0])/lonStep;
int timeIndex = (time-uvGrid.times[0])/timeStep;
double timeRatio = (static_cast<double>(time)-uvGrid.times[timeIndex])/timeStep;
double latRatio = (lat-uvGrid.lats[latIndex]) / latStep;
double lonRatio = (lon-uvGrid.lons[lonIndex]) / lonStep;
point point = {0, 0};
for(int time = 0; time <= 1; time++) {
for(int lat = 0; lat <= 1; lat++) {
for(int lon = 0; lon <= 1; lon++) {
auto vertex = uvGrid.uvMatrix[
timeIndex + time < uvGrid.uvMatrix.extent(0) ? timeIndex + 1 : timeIndex,
latIndex + lat < uvGrid.uvMatrix.extent(1) ? latIndex + 1 : latIndex,
lonIndex + lon < uvGrid.uvMatrix.extent(2) ? lonIndex + 1 : lonIndex
];
double timeRation = (1 - time)*(1-timeRatio) + time*timeRatio;
double latRation = (1 - lat)*(1-latRatio) + lat*latRatio;
double lonRation = (1 - lon)*(1-lonRatio) + lon*lonRatio;
point += timeRation * latRation * lonRation * vertex;
}
}
}
return point;
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.