-
Notifications
You must be signed in to change notification settings - Fork 0
/
plt.cc
253 lines (208 loc) · 5.58 KB
/
plt.cc
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <cstdlib>
#include <iostream>
#include "plt.hh"
// Plot style
std::string plt::value_of(const plt::plot_style& style) {
switch(style) {
case plt::plot_style::missing:
return "";
case plt::plot_style::lines:
return "with lines";
case plt::plot_style::points:
return "with points";
case plt::plot_style::linespoints:
return "with linespoints";
case plt::plot_style::impulses:
return "with impulses";
case plt::plot_style::dots:
return "with dots";
case plt::plot_style::steps:
return "with steps";
case plt::plot_style::errorbars:
return "with errorbars";
case plt::plot_style::yerrorbars:
return "with yerrorbars";
case plt::plot_style::xerrorbars:
return "with xerrorbars";
case plt::plot_style::xyerrorbars:
return "with xyerrorbars";
case plt::plot_style::boxes:
return "with boxes";
case plt::plot_style::boxerrorbars:
return "with boxerrorbars";
case plt::plot_style::boxxyerrorbars:
return "with boxxyerrorbars";
}
std::cerr << "[gnuplot-wrapper] Received invalid plot_style constant!" << std::endl;
exit(-1);
return "";
}
// Gnu Plot class
plt::gnuplot::gnuplot() {
init();
}
plt::gnuplot::gnuplot(const std::string& fname,
const std::string& fmt,
size_t sz_x,
size_t sz_y) {
init();
set_output(fname, fmt, sz_x, sz_y);
}
plt::gnuplot plt::gnuplot::operator=(const plt::gnuplot& plot) {
return plt::gnuplot(plot); // copy c-tor
}
plt::gnuplot::gnuplot(const plt::gnuplot& plot) {
init();
this->plot_buf = plot.plot_buf;
}
void plt::gnuplot::init() {
this->p = popen("gnuplot", "w");
this->range_flag = 0;
this->plot_flag = 0;
this->plot_buf = "";
this->macro_out = nullptr;
}
plt::gnuplot::~gnuplot() {
send_raw("exit");
pclose(p);
}
void plt::gnuplot::send_raw(const std::string& s) {
fprintf(this->p, "%s\n", s.c_str());
if (macro_out)
fprintf(this->macro_out, "%s\n", s.c_str());
}
void plt::gnuplot::load_file(const std::string& fname) {
send_raw("load \"" + fname + "\"");
}
void plt::gnuplot::flush() {
fflush(p);
if (macro_out) fflush(macro_out);
}
void plt::gnuplot::repaint() {
set_output(term_out);
send_raw(get_plot_header() + plot_buf);
}
void plt::gnuplot::clear() {
send_raw("clear");
}
std::string plt::gnuplot::get_plot_header() {
return "plot "
+ (PLT_RANGE_X & range_flag ? "["
+ std::to_string(xr.start)
+ ":"
+ std::to_string(xr.end)
+ "] "
: "[] "
)
+ (PLT_RANGE_Y & range_flag ? "["
+ std::to_string(yr.start)
+ ":"
+ std::to_string(yr.end)
+ "] "
: "[] "
)
;
}
void plt::gnuplot::plot_function(const std::string& f, const plot_meta& meta) {
plot_buf += (plot_buf == "" ? f : ", " + f)
+ (meta.has_title() ? + " title \"" + meta.get_title() + "\"" : "")
+ value_of(meta.get_style())
;
repaint();
}
void plt::gnuplot::plot_data(const std::string& fname, const plot_meta& meta) {
plot_buf += (plot_buf == "" ? "\"" + fname + "\"" : ", \"" + fname + "\"")
+ (meta.has_title() ? " title \"" + meta.get_title() + "\"" : "")
+ value_of(meta.get_style())
;
repaint();
}
void plt::gnuplot::plot_function(const std::string& func) {
plt::plot_meta empty_meta;
plot_function(func, empty_meta);
}
void plt::gnuplot::plot_data(const std::string& fname) {
plt::plot_meta empty_meta;
plot_data(fname, empty_meta);
}
void plt::gnuplot::set_title(const std::string& s) {
send_raw("set title \"" + s + "\"");
}
void plt::gnuplot::set_xlabel(const std::string& s) {
send_raw("set xlabel \"" + s + "\"");
}
void plt::gnuplot::set_ylabel(const std::string& s) {
send_raw("set ylabel \"" + s + "\"");
}
void plt::gnuplot::set_output(const plt::terminal_output& term_out) {
this->term_out = term_out;
fprintf(p, "set terminal %s size %lu,%lu; set output \"%s\"\n",
term_out.fmt.c_str(),
term_out.sz_x,
term_out.sz_y,
term_out.fname.c_str()
);
if (macro_out) {
fprintf(macro_out, "set terminal %s size %lu,%lu; set output \"%s\"\n",
term_out.fmt.c_str(),
term_out.sz_x,
term_out.sz_y,
term_out.fname.c_str()
);
}
}
void plt::gnuplot::set_output(const std::string& fname, const std::string& fmt, size_t sz_x, size_t sz_y) {
plt::terminal_output term_out(fname, fmt, sz_x, sz_y);
set_output(term_out);
}
void plt::gnuplot::set_macro_output(FILE *macro_out) {
this->macro_out = macro_out;
}
void plt::gnuplot::set_xrange(const plt::axis_range& r) {
xr = r;
range_flag |= PLT_RANGE_X;
}
void plt::gnuplot::set_yrange(const plt::axis_range& r) {
yr = r;
range_flag |= PLT_RANGE_Y;
}
void plt::gnuplot::set_xrange(double xstart, double xend) {
plt::axis_range xr(xstart, xend);
set_xrange(xr);
}
void plt::gnuplot::set_yrange(double ystart, double yend) {
plt::axis_range yr(ystart, yend);
set_yrange(yr);
}
void plt::gnuplot::unset_xrange() {
range_flag &= ~ PLT_RANGE_X;
}
void plt::gnuplot::unset_yrange() {
range_flag &= ~ PLT_RANGE_Y;
}
void plt::gnuplot::set_logscale(const std::string& axes, double base) {
fprintf(p, "set logscale %s %lf\n", axes.c_str(), base);
if (macro_out)
fprintf(macro_out, "set logscale %s %lf\n", axes.c_str(), base);
}
void plt::gnuplot::set_logscale(const std::string& axes) {
set_logscale(axes, 10.);
}
void plt::gnuplot::unset_logscale(const std::string& axes) {
send_raw("unset logscale " + axes);
}
void plt::gnuplot::save_settings(const std::string& fname) {
send_raw("save set \"" + fname + "\"");
}
void plt::gnuplot::save_macro(const std::string& fname) {
send_raw("save \"" + fname + "\"");
}
// Plot Range class
plt::axis_range::axis_range(double start, double end) {
this->start = start;
this->end = end;
}
plt::axis_range::axis_range() {
this->start = 0;
this->end = 0;
}