- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgnuplot.hpp
More file actions
Latest commit
205 lines (164 loc) · 4.18 KB
/
Copy pathgnuplot.hpp
File metadata and controls
205 lines (164 loc) · 4.18 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
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
#if0 // examples
// 01-line.cpp
#define GNUPLOT_IMPLEMENTATION
#include "gnuplot.hpp"
int main()
{
gp::Plotter p;
p.set_title("hello");
std::vector<float> x{1, 2, 3};
std::vector<float> y{1, 2, 3};
p.line(x, y);
p.hold(true);
p.function("sin(x)");
return 0;
}
#endif// examples
#ifndef GNUPLOT_HPP_
#defineGNUPLOT_HPP_
#include<cstdio>
namespacegp {
structPlotter
{
explicitPlotter(bool persist=true);
~Plotter();
inlinevoidhold(bool h) { hold_ = h; }
boolset_title(constchar* title);
#ifdef _GLIBCXX_VECTOR
boolline(const std::vector<float>& x, const std::vector<float>& y);
boolline(const std::vector<float>& x);
#endif// _GLIBCXX_VECTOR
boolline(constfloat* x, constfloat* y, size_t len);
boolline(constfloat* x, size_t len);
boolfunction(constchar* fun);
boolplot(constchar* plot_cmd);
boolcmd(constchar* c);
private:
constchar* plot_cmd() const;
FILE *gp_fp_;
bool hold_;
bool has_plot_;
}; // struct Plotter
}; // namespace gp
#endif// GNUPLOT_HPP_
// #define GNUPLOT_IMPLEMENTATION // delete me
#ifdef GNUPLOT_IMPLEMENTATION
#ifndef GNUPLOT_CPP_
#defineGNUPLOT_CPP_
#include<mutex>
#include<sstream>
#include<string>
#include<unistd.h>
namespacegp {
#defineGP_MAX_PLOT_CMD_LEN128
std::string unique_name()
{
staticint cnt{0};
static std::mutex locker;
std::stringstream ss;
{
std::lock_guard<std::mutex> guard{locker};
ss << "gp_" << getpid() << "_" << cnt++ << + "_dat";
}
return ss.str();
}
Plotter::Plotter(bool persist)
: gp_fp_(nullptr)
, hold_(false)
, has_plot_(false)
{
#defineGP_MAX_POPEN_CMD_LEN64
char buff[GP_MAX_POPEN_CMD_LEN];
snprintf(buff, GP_MAX_POPEN_CMD_LEN, "gnuplot %s", (persist ? "-persist" : ""));
gp_fp_ = popen(buff, "w");
#undef GP_MAX_POPEN_CMD_LEN
}
Plotter::~Plotter()
{
if (gp_fp_) {
fclose(gp_fp_);
gp_fp_ = nullptr;
}
}
boolPlotter::set_title(constchar* title)
{
if (!title) returnfalse;
char c[GP_MAX_PLOT_CMD_LEN];
snprintf(c, GP_MAX_PLOT_CMD_LEN, "set title \"%s\"\n", title);
returncmd(c);
}
#ifdef _GLIBCXX_VECTOR
boolPlotter::line(const std::vector<float>& x, const std::vector<float>& y)
{
if (x.size() != y.size()) returnfalse;
returnline(x.data(), y.data(), x.size());
}
boolPlotter::line(const std::vector<float>& x)
{
returnline(x.data(), x.size());
}
#endif// _GLIBCXX_VECTOR
boolPlotter::line(constfloat* x, size_t len)
{
if (!gp_fp_) returnfalse;
if (!x || len == 0) returnfalse;
auto data_name{unique_name()};
fprintf(gp_fp_, "$%s<<EOD\n", data_name.c_str());
for (size_t i = 0; i < len; ++i) {
fprintf(gp_fp_, "%ld %f\n", i, x[i]);
}
fprintf(gp_fp_, "EOD\n");
char c[GP_MAX_PLOT_CMD_LEN];
snprintf(c, GP_MAX_PLOT_CMD_LEN, "%s $%s with line\n", plot_cmd(), data_name.c_str());
returnplot(c);
}
boolPlotter::line(constfloat* x, constfloat* y, size_t len)
{
if (!gp_fp_) returnfalse;
if (!x || !y || len == 0) returnfalse;
auto data_name{unique_name()};
fprintf(gp_fp_, "$%s<<EOD\n", data_name.c_str());
for (size_t i = 0; i < len; ++i) {
fprintf(gp_fp_, "%f %f\n", x[i], y[i]);
}
fprintf(gp_fp_, "EOD\n");
char c[GP_MAX_PLOT_CMD_LEN];
snprintf(c, GP_MAX_PLOT_CMD_LEN, "%s $%s with line\n", plot_cmd(), data_name.c_str());
returnplot(c);
}
boolPlotter::function(constchar* fun)
{
if (!fun) returnfalse;
char c[GP_MAX_PLOT_CMD_LEN];
snprintf(c, GP_MAX_PLOT_CMD_LEN, "%s %s\n", plot_cmd(), fun);
returnplot(c);
}
boolPlotter::plot(constchar* plot_cmd)
{
if (cmd(plot_cmd)) {
has_plot_ = true;
returntrue;
} else {
returnfalse;
}
}
boolPlotter::cmd(constchar* c)
{
if (!gp_fp_) returnfalse;
if (!c) returnfalse;
fprintf(gp_fp_, "%s\n", c);
returntrue;
}
constchar* Plotter::plot_cmd() const
{
staticconstchar* plot{"plot"};
staticconstchar* replot{"replot"};
if (hold_ && has_plot_) {
return replot;
} else {
return plot;
}
}
} // namespace gp
#endif// GNUPLOT_CPP_
#endif// GNUPLOT_IMPLEMENTATION