csv - c++ explore a dataset stored in a std::vector with bilinear interpolation -
i have vector std::vector <int> density;
stores set of values imported input.csv
file.
the values varying between 0 , 2^16, , ordered on regular grid of int c
columns , int l
lines.
now, want use bilinear interpolation compute new set of values std::vector <float> d
these new values arranged in regular grid of int x
columns , int y
lines.
.
my problem:
to perform bilinear interpolation based on squared grid of data have know each position (x,y)
local 4 surrounding density
values for:
d1 (c,l), d2 (c,l), d3 (c,l), d4 (c,l)
in other words, each interpolated point of d
position (dx,dy)
- dx , dy being inferior or equal 1 - located within squared "cell" of original dataset defined 4 density
values relative position (c,l)
original grid.
how can define, arbitrary position (dx,dy)
within range of c
, l
(not outside grid!) 4 surrounding density
values d1, d2, d3, d4
?
and how can define, within (d1, d2, d3, d4)
cell, positions (u,v)
defined point d
(see code understanding point d
) within cell
if please me improve code... :) thanks!
//
to make easier conceptualize, here's example numerical values:
input.csv
200; 300; 400
100; 100; 100
0; 100; 200
number of columns:
c = 3
number of lines :
l = 3
after pushing these, vector contains following:
200, 300, 400, 100, 100, 100, 0, 100, 200
number of columns of new file: x = 4
number of lines of new file : y = 4
the algorithm should following:
for position (dx, dy) = (0, 0)
(u, v) = (0, 0)
we in 1st cell so
(d1, d2, d3, d4) = (200, 300, 100, 100)
and since (u, v) = (0, 0)
the density equal d1 = 200
so b = 200
now, pass next xalue of dx
> dx = x+1
(returns 1)
c
equal3
,x
equal4
, ,dx * c / x = 1 * 2 / 3 = 0.666666667
or2/3
2/3 < 1
still in first cell
for position (dx, dy) = (2/3, 0)
(u, v) = (2/3, 0)
since u > 0
, v = 0
density between d1
, d2
d = u * d2 + (1-u) * d1 (returns 266.666666667)
lets imagine file output.csv
containing 4 col , 4 lines, result, should contain: (spaces here reading...)
200; 266.6666667; 333.3333333; 400 133.3333333; 155.5555556; 177.7777778; 200 66.66666667; 66.88888889; 111.1111111; 133,3333333 0; 66.66666667; 133,3333333; 200
input 3 x 3 was:
200; 300; 400
100; 100; 100
0; 100; 200
//
code:
#include <iostream> #include <fstream> #include <vector> using namespace std; //_____________________________________________________________________________ int main() { /// original grid int c = 0; // amount of columns in .csv int l = 0; // amount of lines in .csv int = 0; // variable temporarily stores .csv value std::vector <int> density; // stores dataset .csv //_____________________________________________________________________________ ifstream ifs ("input.csv"); // importing .csv cout << "number of columns?" << endl; // specifying number of columns cin >> c; cout << endl; cout << "number of lines?" << endl; // specifying number of lines cin >> l; cout << endl; char dummy; (int = 0; < l; ++i){ // pushing values of .csv vector (int = 0; < c; ++i){ ifs >> a; density.push_back(a); if (i < (c - 1)) ifs >> dummy; } //_______________________________________________________________________________ /// output grid int x = 0; // coordinate x int y = 0; // coordiante y int dx = 0; // horizontal position int dy = 0; // vertical position int b = 0; // interpolated density std::vector <float> d; // stores interpolated values cout << "number of columns of new file?" << endl; // specifying number of columns cin >> x; cout << endl; cout << "number of lines of new file?" << endl; // specifying number of lines cin >> y; cout << endl; } /// diagram of cell position of: b (u,v) //__________________________________________________ // // d1 ---u--- d2 // | | | // v----b | // | | // d3 ------- d4 // //__________________________________________________ int d1 = 0; // densities of 4 points of cell containing (x,y) int d2 = 0; int d3 = 0; int d4 = 0; float u = 0; // horizontal , vertical positions of b within cell float v = 0; //_______________________________________________________________________ /// part missing here: how d1, d2, d3, d4, u, v ??? //_______________________________________________________________________ while (x<c, y<l) { // formulae bilinear interpolation double dl = d1 - d1 * v + d3 * v; // vertical linear interpolation right side double dr = d2 - d2 * v + d4 * v; // vertical linear interpolation left side double d = dl - dl * u + dr * u; // horizontal linear interpolation d.push_back (b); x++; // next interpolation if (x>c) { y = y++; } } }
Comments
Post a Comment