-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFigure.cpp
More file actions
135 lines (114 loc) · 4.18 KB
/
Figure.cpp
File metadata and controls
135 lines (114 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
#include "Figure.h"
int Figure::num_figures = 0;
std::vector<std::string> Figure::matlab_line_styles = {"--", "-", ":", "-."};
std::vector<std::string> Figure::gnuplot_line_styles = {"2", "1", "3", "5"};
std::vector<std::string> Figure::matlab_line_colours = {"y", "m", "c", "r", "g", "b", "w", "k"};
std::vector<std::string> Figure::gnuplot_line_colours = {"6", "4", "5", "1", "2", "3", "18", "7"};
// MISSING: Left triangle, Right triangle, Pentagram and Hexagram.
std::vector<std::string> Figure::matlab_line_markers = {"o", "+", "*", ".", "x", "s", "d", "^", "v"};
std::vector<std::string> Figure::gnuplot_line_markers = {"6", "1", "3", "0", "2", "4", "12", "8", "10"};
Figure::Figure() {
num_figures++;
id = num_figures;
gnuplot_stream = popen("/usr/bin/gnuplot","w");
}
Figure::~Figure() {
if (gnuplot_stream != NULL) {
if (pclose(gnuplot_stream) == 0) {
for (unsigned int i = 0; i < temp_data_files.size(); i++){
remove(temp_data_files[i].c_str());
}
}
}
num_figures--;
}
void Figure::set_plot_style(std::string plot_style) {
if (plot_style.size() > 4) {
std::cerr << "Too many specifiers." << std::endl;
return;
}
bool line_style_set = false;
bool line_colour_set = false;
bool line_marker_set = false;
for (unsigned int i = 0; i < matlab_line_styles.size(); i++) {
if (plot_style.find(matlab_line_styles[i]) != std::string::npos) {
if (!line_style_set) {
line_styles.push_back(gnuplot_line_styles[i]);
line_style_set = true;
} else {
std::cerr << "Only 1 line style allowed !" << std::endl;
}
}
}
for (unsigned int i =0; i < matlab_line_colours.size(); i++) {
if (plot_style.find(matlab_line_colours[i]) != std::string::npos) {
if (!line_colour_set) {
line_colours.push_back(gnuplot_line_colours[i]);
line_colour_set = true;
} else {
std::cerr << "Only 1 line colour is allowed !" << std::endl;
}
}
}
for (unsigned int i = 0; i < matlab_line_markers.size(); i++) {
if (plot_style.find(matlab_line_markers[i]) != std::string::npos) {
if (!line_marker_set) {
line_markers.push_back(gnuplot_line_markers[i]);
line_marker_set = true;
} else {
std::cerr << "Only 1 line marker is allowed !" << std::endl;
}
}
}
if (!line_colour_set) {
line_colours.push_back(std::to_string(num_plots)); //Cycle through colours
}
if (!line_marker_set) {
line_markers.push_back("-1"); //No marker
}
if (!line_style_set) {
line_styles.push_back("1"); //Solid line
line_spec.push_back("points");
} else {
line_spec.push_back("linespoints");
}
}
void Figure::show() {
if (gnuplot_stream != NULL) {
fprintf(gnuplot_stream, "set term wxt dashed persist\n");
draw();
}
}
void Figure::save(std::string file_name) {
if (gnuplot_stream != NULL) {
fprintf(gnuplot_stream, "set term png\n");
std::string gnuplot_command = "set output '"+file_name+"'\n";
fputs(gnuplot_command.c_str(), gnuplot_stream);
draw();
}
}
void Figure::draw() {
std::string gnuplot_command;
std::string style_string;
if (gnuplot_stream != NULL) {
if (temp_data_files.size() > 1){
for (unsigned int i = 0; i < temp_data_files.size(); i++) {
style_string = temp_data_files[i]+"' using 1:2 with " + line_spec[i] + " lt " + line_styles[i] +" lc "+ line_colours[i]+" pt " + line_markers[i];
if (i == 0) {
gnuplot_command = "plot '" + style_string + ", \\\n";
fputs(gnuplot_command.c_str(), gnuplot_stream);
} else if (i < temp_data_files.size() - 1) {
gnuplot_command = "'"+ style_string + ", \\\n";
fputs(gnuplot_command.c_str(), gnuplot_stream);
} else {
gnuplot_command = "'"+ style_string + "\\\n;";
fputs(gnuplot_command.c_str(), gnuplot_stream);
}
}
} else {
gnuplot_command = "plot '"+ temp_data_files[0]+"' using 1:2 with linespoints lt " + line_styles[0] +" lc "+ line_colours[0]+" pt "+line_markers[0] +" \\\n;";
fputs(gnuplot_command.c_str(), gnuplot_stream);
}
fflush(gnuplot_stream);
}
}