Commit fac4f163 authored by Laurent Belcour's avatar Laurent Belcour

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
parent b68aba5e
...@@ -180,8 +180,8 @@ class arguments ...@@ -180,8 +180,8 @@ class arguments
vec get_vec(const std::string& key, int size, float default_value = 0.0f) const vec get_vec(const std::string& key, int size, float default_value = 0.0f) const
{ {
vec res(size); vec res(size);
for(int i=0; i<size; ++i) for(int i=0; i<size; ++i)
res[i] = default_value; res[i] = default_value;
if(_map.count(key) > 0) if(_map.count(key) > 0)
{ {
...@@ -196,14 +196,14 @@ class arguments ...@@ -196,14 +196,14 @@ class arguments
if(ppos != std::string::npos) 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; pos = ppos+1;
++i; ++i;
} }
else else
{ {
std::string temp = s.substr(pos, std::string::npos); std::string temp = s.substr(pos, std::string::npos);
res[i] = atof(temp.substr(0, temp.size()-1).c_str()); res[i] = atof(temp.substr(0, temp.size()-1).c_str());
pos = ppos; pos = ppos;
++i; ++i;
} }
...@@ -220,6 +220,38 @@ class arguments ...@@ -220,6 +220,38 @@ class arguments
return res; 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<typename T>
std::vector<T> get_vec(const std::string& key) const
{
std::vector<T> 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<std::string> get_vec(const std::string& key) const std::vector<std::string> get_vec(const std::string& key) const
{ {
std::vector<std::string> res; std::vector<std::string> res;
......
...@@ -41,8 +41,6 @@ void data_interpolant::load(const std::string& filename) ...@@ -41,8 +41,6 @@ void data_interpolant::load(const std::string& filename)
memcpy(pts[i], &x[0], dimX()*sizeof(double)); memcpy(pts[i], &x[0], dimX()*sizeof(double));
} }
_kdtree->buildIndex(pts); _kdtree->buildIndex(pts);
std::cout << " " << _kdtree->veclen() << std::endl;
} }
void data_interpolant::load(const std::string& filename, const arguments&) void data_interpolant::load(const std::string& filename, const arguments&)
...@@ -101,9 +99,12 @@ vec data_interpolant::value(vec x) const ...@@ -101,9 +99,12 @@ vec data_interpolant::value(vec x) const
} }
cum_dist += dists[0][i]; cum_dist += dists[0][i];
} }
for(int j=0; j<dimY(); ++j) if(cum_dist > 0.0)
{ {
res[j] /= cum_dist; for(int j=0; j<dimY(); ++j)
{
res[j] /= cum_dist;
}
} }
return res; return res;
......
...@@ -17,8 +17,10 @@ int main(int argc, char** argv) ...@@ -17,8 +17,10 @@ int main(int argc, char** argv)
arguments args(argc, argv) ; arguments args(argc, argv) ;
if(args.is_defined("help")) { if(args.is_defined("help")) {
std::cout << "<<HELP>> data2gnuplot --input data.file --output gnuplot.file --data loader.so" << std::endl ; std::cout << "<<HELP>> 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 << " - 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 ; return 0 ;
} }
...@@ -38,6 +40,24 @@ int main(int argc, char** argv) ...@@ -38,6 +40,24 @@ int main(int argc, char** argv)
// Create output file // Create output file
std::ofstream file(args["output"].c_str(), std::ios_base::trunc); std::ofstream file(args["output"].c_str(), std::ios_base::trunc);
std::vector<int> slice = args.get_vec<int>("slice");
assert(slice.size() <= d->dimX());
if(slice.size() == 0)
{
slice.resize(d->dimX());
for(int u=0; u<d->dimX(); ++u)
{
slice[u] = u;
}
}
else
{
for(int u=0; u<slice.size(); ++u)
{
assert(slice[u] < d->dimX());
}
}
if(d != NULL) if(d != NULL)
{ {
std::cout << "<<INFO>> will export " << d->size() << " elements" << std::endl ; std::cout << "<<INFO>> will export " << d->size() << " elements" << std::endl ;
...@@ -55,25 +75,32 @@ int main(int argc, char** argv) ...@@ -55,25 +75,32 @@ int main(int argc, char** argv)
vec x(d->dimX()); vec x(d->dimX());
x = _min + 0.5*(_max - _min); 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; i<N; ++i) for(int i=0; i<N; ++i)
for(int j=0; j<N; ++j) {
// Set the point of evaluation and get the value
for(int u=0; u<d->dimX(); ++u)
{ {
x[0] = _min[0] + (_max[0] - _min[0]) * double(i) / double(N-1); int j = (i / int(pow(n, u))) % n;
x[1] = _min[1] + (_max[1] - _min[1]) * double(j) / double(N-1); int v = slice[u];
vec v = d->value(x) ; x[v] = _min[v] + (_max[v] - _min[v]) * double(j) / double(n);
for(int u=0; u<d->dimX(); ++u)
{
file << x[u] << "\t";
}
for(int u=0; u<d->dimY(); ++u)
{
file << v[u] << "\t" ;
}
file << std::endl ;
} }
vec v = d->value(x) ;
// Copy the value in the file
for(int u=0; u<d->dimX(); ++u)
{
file << x[u] << "\t";
}
for(int u=0; u<d->dimY(); ++u)
{
file << v[u] << "\t" ;
}
file << std::endl ;
}
} }
else else
{ {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment