-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathcxxgplot.h
159 lines (129 loc) · 2.69 KB
/
cxxgplot.h
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
#include "pstreams/pstream.h"
#include <sstream>
#include <vector>
#include <cctype>
#include <cmath>
//Super simple and stupid class for interactive
//plotting and visualisation from C++ using gnuplot
namespace cplot
{
//A plot contains multiple axes
// A plot has a single location so axes are overlaid
//
//Axes contain multiple lines
class Plotter
{
//std::vector<plot> plots;
std::vector<std::string> plots, pdata, extra;
redi::opstream plot;
public:
std::string range;
Plotter()
:plot("gnuplot")
{
plot << "set term x11 noraise\n";
newline("");
}
std::ostream& s()
{
return plot;
}
void add_extra(const std::string& s)
{
extra.push_back(s);
}
Plotter& newline(const std::string& ps)
{
plots.push_back(ps);
pdata.push_back("");
return *this;
}
template<class C> Plotter& addpt(const C& pt)
{
using std::isfinite;
std::ostringstream o;
if(isfinite(pt))
o << pt << std::endl;
else
skip();
pdata.back() += o.str();
return *this;
}
template<class C> Plotter& addpt(C p1, C p2)
{
using std::isfinite;
std::ostringstream o;
if(isfinite(p1) && isfinite(p2))
o << p1 << " " << p2 << std::endl;
else
skip();
pdata.back() += o.str();
return *this;
}
template<class D> Plotter& addpts(const D& pt)
{
using std::isfinite;
std::ostringstream o;
for(unsigned int i=0; i < pt.size(); i++)
if(isfinite(pt[i]))
o << pt[i] << std::endl;
else
skip();
pdata.back() += o.str();
return *this;
}
Plotter& skip()
{
pdata.back() += "\n";
return *this;
}
void draw()
{
using std::cerr;
using std::endl;
bool data=0;
//Check for data
std::vector<int> have_data(plots.size());
for(unsigned int i=0; i < plots.size(); i++)
for(unsigned int j=0; j < pdata[i].size(); j++)
if(!std::isspace(pdata[i][j]))
{
have_data[i] = 1;
data=1;
break;
}
for(unsigned int i=0, first=1; i < plots.size(); i++)
if(have_data[i])
{
if(first)
{
plot << "plot " << range << " \"-\"";
first=0;
}
else
{
plot << ", \"-\"";
}
if(plots[i] != "")
{
plot << " with " << plots[i];
}
}
if(data)
plot << std::endl;
for(unsigned int i=0; i < plots.size(); i++)
if(have_data[i])
{
plot << pdata[i] << "e\n";
}
if(data)
plot << std::flush;
for(unsigned int i=0; i < extra.size(); i++)
plot << extra[i] << "\n";
plots.clear();
extra.clear();
pdata.clear();
newline("");
}
};
}