-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipegnuplotter.h
More file actions
156 lines (129 loc) · 4.34 KB
/
Copy pathpipegnuplotter.h
File metadata and controls
156 lines (129 loc) · 4.34 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
#ifndef PIPEGNUPLOTTER_H
#define PIPEGNUPLOTTER_H
#include <string>
#include <iostream>
#include <vector>
#include <thread>
#ifdef WIN32
#define GNUPLOT_NAME "pgnuplot -persist"
#else
#define GNUPLOT_NAME "gnuplot"
#endif
void __wait() {
std::cin.clear();
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin.get();
}
namespace PipeGnuplotter {
class Palette {
public:
static std::vector<std::string> set_style_commands;
static void initialize(FILE* pipe, std::vector<std::string> set_style_commands) {
std::string command = "";
for (std::string s : set_style_commands) {
fprintf(pipe, "%s", s.c_str());
}
}
};
std::vector<std::string> Palette::set_style_commands = {
"set style line 1 linecolor rgb '#0000A0' linetype 1 linewidth 1\n", //darkblue
"set style line 2 linecolor rgb '#32ACB4' linetype 1 linewidth 1\n", //cyan
"set style line 3 linecolor rgb '#228B22' linetype 1 linewidth 1\n", //forestgreen
"set style line 4 linecolor rgb '#ff8C00' linetype 1 linewidth 1\n", //orange
"set style line 5 linecolor rgb '#FF4500' linetype 1 linewidth 1\n", //orangered
"set style line 6 linecolor rgb '#DC143C' linetype 1 linewidth 1\n" //crimson
};
void scatter3d(std::string file_name) {
#ifdef WIN32
FILE *pipe = _popen(GNUPLOT_NAME, "w");
#else
FILE *pipe = popen(GNUPLOT_NAME, "w");
#endif
if (pipe) {
fprintf(pipe, "set key off\n");
std::string command = "splot '" +
file_name +
"' using 1:2:3 with points palette pointsize 1 pointtype 7\n";
fprintf(pipe, "%s", command.c_str());
// fprintf(pipe, "%s\n", "e");
fflush(pipe);
__wait();
#ifdef WIN32
_pclose(pipe);
#else
pclose(pipe);
#endif
}
return;
}
//TBD
void _plot2d(std::string file_name) {
#ifdef WIN32
FILE *pipe = _popen(GNUPLOT_NAME, "w");
#else
FILE *pipe = popen(GNUPLOT_NAME, "w");
#endif
if (pipe) {
fprintf(pipe, "set key off\n");
std::string command = "plot '" +
file_name +
"' using 1:2 with lines linestyle 1\n";
fprintf(pipe, "%s", command.c_str());
// fprintf(pipe, "%s\n", "e");
fflush(pipe);
__wait();
#ifdef WIN32
_pclose(pipe);
#else
pclose(pipe);
#endif
}
return;
}
void plot2d(std::string file_name,
std::vector<std::string> set_style_commands = {}) {
if (!set_style_commands.size()) {
_plot2d(file_name);
return;
}
#ifdef WIN32
FILE *pipe = _popen(GNUPLOT_NAME, "w");
#else
FILE *pipe = popen(GNUPLOT_NAME, "w");
#endif
if (pipe) {
fprintf(pipe, "set key off\n");
Palette::initialize(pipe, set_style_commands);
std::string command = "plot '";
for (unsigned int i = 0; i < set_style_commands.size(); i++) {
command += file_name +
"' using 1:" + std::to_string(i+2) +
" with lines linestyle " + std::to_string(i+1);
if (i != set_style_commands.size() - 1) command += " , '";
}
command += "\n";
fprintf(pipe, "%s", command.c_str());
// fprintf(pipe, "%s\n", "e");
fflush(pipe);
__wait();
#ifdef WIN32
_pclose(pipe);
#else
pclose(pipe);
#endif
}
return;
}
namespace Threaded {
std::thread scatter3d(std::string file_name) {
std::thread t(PipeGnuplotter::scatter3d, file_name);
return t;
}
std::thread plot2d(std::string file_name,
std::vector<std::string> set_style_commands = {}) {
std::thread t(PipeGnuplotter::plot2d, file_name, set_style_commands);
return t;
}
}
}
#endif // PIPEGNUPLOTTER_H