-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl1.cpp
More file actions
134 lines (116 loc) · 2.96 KB
/
l1.cpp
File metadata and controls
134 lines (116 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <string>
#include "plstream.h"
// std::ofstream ofs("dbg");
using namespace std;
vector<string> split(const string &str, char delim){
vector<string> res;
size_t current = 0, found;
while((found = str.find_first_of(delim, current)) != string::npos){
res.push_back(string(str, current, found - current));
current = found + 1;
}
res.push_back(string(str, current, str.size() - current));
return res;
}
std::string basename(const std::string& path) {
return path.substr(path.find_last_of('/') + 1);
}
struct DataOpts
{
int nummax;
double xmin;
double xmax;
double ymin;
double ymax;
};
DataOpts filecheck(std::vector<string> const& fs)
{
DataOpts opts = {0, 0, 0, 0, 0};
for (std::vector<string>::const_iterator it = fs.begin(); it != fs.end(); ++it)
{
std::string const& f = *it;
std::ifstream ifs(f.c_str());
int num;
while (ifs)
{
double x, y;
ifs >> x >> y;
opts.xmin = std::min(opts.xmin, x);
opts.ymin = std::min(opts.ymin, y);
opts.xmax = std::max(opts.xmax, x);
opts.ymax = std::max(opts.ymax, y);
++num;
}
opts.nummax = std::max(opts.nummax, num);
}
return opts;
}
void plot( int argc, const char ** argv, char const* device, char const* inpath, char const* outpath, DataOpts const& opts)
{
plstream *pls;
PLFLT **z;
PLcGrid2 cgrid2;
pls = new plstream();
pls->sdev(device);
pls->sfnam(outpath);
pls->parseopts(&argc,argv,PL_PARSE_FULL);
pls->scolbg(255, 255, 255);
pls ->scol0(15, 0, 0, 0);
// Initialize plplot
pls->init();
{
//set color map
PLFLT y[4]={-1.0,-1.0, 1.0, 1.0};
PLFLT r[2]={0.0,1.0};
PLFLT g[2]={0.0,0.0};
PLFLT b[2]={1.0,0.0};
PLFLT pos[2]={0.0,1.0};
pls->scmap1l(true,2,pos,r,g,b,NULL);
}
{ // plot
double xmin=opts.xmin;
double xmax=opts.xmax;
double ymin=opts.ymin;
double ymax=opts.ymax;
std::vector<double> ax; ax.reserve(opts.nummax);
std::vector<double> ay; ay.reserve(opts.nummax);
int num = 0;
{
std::ifstream ifs(inpath);
double x, y;
while (ifs >> x >> y){
ax.push_back(x);
ay.push_back(y);
++num;
}
}
//Plot
pls->col0(15);
pls->env(xmin,xmax,ymin,ymax,1,0);
pls->lab(getenv("X"),getenv("Y"),getenv("TITLE"));
pls->line(num,ax.data(),ay.data());
delete pls;
}
}
int main( int argc, const char ** argv )
{
std::vector<string> const fs = split(getenv("F"), ',');
DataOpts const opts = filecheck(fs);
for (std::vector<string>::const_iterator it = fs.begin(); it != fs.end(); ++it)
{
std::string const& f = *it;
std::string const outpath = ("public/"+basename(f)+".png");
plot( argc, argv, getenv("D"), f.c_str(), outpath.c_str(), opts);
std::cout << outpath << ",";
}
return 0;
}