From fac4f163d062ecf4f9f0a62c380d803a0995c4c2 Mon Sep 17 00:00:00 2001 From: Laurent Belcour Date: Mon, 4 Nov 2013 13:59:52 +0100 Subject: [PATCH] Adding a new interface to extract vectors from a command line argument of any type (templated) Allowing to extract slices of data from the data2gnuplot interface --- sources/core/args.h | 42 ++++++++++++++-- sources/plugins/data_interpolant/data.cpp | 9 ++-- sources/softs/data2gnuplot/main.cpp | 61 ++++++++++++++++------- 3 files changed, 86 insertions(+), 26 deletions(-) diff --git a/sources/core/args.h b/sources/core/args.h index 5ce0949..fb4c184 100644 --- a/sources/core/args.h +++ b/sources/core/args.h @@ -180,8 +180,8 @@ class arguments vec get_vec(const std::string& key, int size, float default_value = 0.0f) const { vec res(size); - for(int i=0; i 0) { @@ -196,14 +196,14 @@ class arguments if(ppos != std::string::npos) { - res[i] = atof(s.substr(pos, ppos-pos).c_str()); + res[i] = atof(s.substr(pos, ppos-pos).c_str()); pos = ppos+1; ++i; } else { - std::string temp = s.substr(pos, std::string::npos); - res[i] = atof(temp.substr(0, temp.size()-1).c_str()); + std::string temp = s.substr(pos, std::string::npos); + res[i] = atof(temp.substr(0, temp.size()-1).c_str()); pos = ppos; ++i; } @@ -220,6 +220,38 @@ class arguments return res; } + //! \brief access a vector of element of type \a T of variable length. + //! the string associated with the key \a key should have the form + //! "[a, b, c]" where a, b, c are of type \a T. + template + std::vector get_vec(const std::string& key) const + { + std::vector res; + + if(_map.count(key) > 0) + { + std::string str = _map.find(key)->second; + if(str[0] == '[' && str[str.size()-1] == ']') // Is an array of type [a, b, c] + { + std::stringstream stream(str.substr(1, str.size()-2)); + char s[256]; + while(stream.good()) + { + // Extract the value v from the stream s + stream.get(s, 256, ','); + stream.ignore(1); + std::stringstream sstream(s); + T v; + sstream >> v; + + // Push the value at the end of the vector + res.push_back(v); + } + } + } + return res; + } + std::vector get_vec(const std::string& key) const { std::vector res; diff --git a/sources/plugins/data_interpolant/data.cpp b/sources/plugins/data_interpolant/data.cpp index d6dcb20..0e0f8cd 100644 --- a/sources/plugins/data_interpolant/data.cpp +++ b/sources/plugins/data_interpolant/data.cpp @@ -41,8 +41,6 @@ void data_interpolant::load(const std::string& filename) memcpy(pts[i], &x[0], dimX()*sizeof(double)); } _kdtree->buildIndex(pts); - - std::cout << " " << _kdtree->veclen() << std::endl; } void data_interpolant::load(const std::string& filename, const arguments&) @@ -101,9 +99,12 @@ vec data_interpolant::value(vec x) const } cum_dist += dists[0][i]; } - for(int j=0; j 0.0) { - res[j] /= cum_dist; + for(int j=0; j> data2gnuplot --input data.file --output gnuplot.file --data loader.so" << std::endl ; + std::cout << "<> data2gnuplot --input data.file --output gnuplot.file --data loader.so --slice [0, 1, 2]" << std::endl ; std::cout << " - input and output are mandatory parameters" << std::endl ; + std::cout << " - if no data parameter is set, the data will be loaded using vertical segments plugin" << std::endl ; + std::cout << " - the slice parameter allows to select the dimensions to output" << std::endl ; return 0 ; } @@ -38,6 +40,24 @@ int main(int argc, char** argv) // Create output file std::ofstream file(args["output"].c_str(), std::ios_base::trunc); + std::vector slice = args.get_vec("slice"); + assert(slice.size() <= d->dimX()); + if(slice.size() == 0) + { + slice.resize(d->dimX()); + for(int u=0; udimX(); ++u) + { + slice[u] = u; + } + } + else + { + for(int u=0; udimX()); + } + } + if(d != NULL) { std::cout << "<> will export " << d->size() << " elements" << std::endl ; @@ -55,25 +75,32 @@ int main(int argc, char** argv) vec x(d->dimX()); x = _min + 0.5*(_max - _min); - const int N = 100; + const int N = 10000; + const int n = int(pow(N, 1.0/slice.size())); + for(int i=0; idimX(); ++u) { - x[0] = _min[0] + (_max[0] - _min[0]) * double(i) / double(N-1); - x[1] = _min[1] + (_max[1] - _min[1]) * double(j) / double(N-1); - vec v = d->value(x) ; - - for(int u=0; udimX(); ++u) - { - file << x[u] << "\t"; - } - for(int u=0; udimY(); ++u) - { - file << v[u] << "\t" ; - } - - file << std::endl ; + int j = (i / int(pow(n, u))) % n; + int v = slice[u]; + x[v] = _min[v] + (_max[v] - _min[v]) * double(j) / double(n); } + vec v = d->value(x) ; + + // Copy the value in the file + for(int u=0; udimX(); ++u) + { + file << x[u] << "\t"; + } + for(int u=0; udimY(); ++u) + { + file << v[u] << "\t" ; + } + + file << std::endl ; + } } else { -- GitLab