-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplplot.c
245 lines (196 loc) · 7.96 KB
/
plplot.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <SWI-Prolog.h>
// #ifdef _WIN32
// #include <Windows.h>
// #endif
#define OFFSET 1
static foreign_t plot_point(term_t title, term_t list_x_vals, term_t list_y_vals);
static foreign_t plot_line(term_t title, term_t list_x_vals, term_t list_y_vals);
static foreign_t plot_point_new(term_t title, term_t list);
int write_data_to_file(char *filename, term_t list_x_vals, term_t list_y_vals, int len, int *l_x, int *u_x, int *l_y, int *u_y);
int write_data_to_file(char *filename, term_t list_x_vals, term_t list_y_vals, int len, int *l_x, int *u_x, int *l_y, int *u_y) {
int i;
double current_value;
term_t val = PL_new_term_ref();
FILE * temp = fopen(filename, "w");
for(i = 0; i < len; i++) {
if(PL_get_list(list_x_vals,val,list_x_vals) <= 0) {
printf("error reading head x list.");
return -1;
}
if(PL_get_float(val,¤t_value) <= 0) {
printf("error converting x value.");
return -1;
}
fprintf(temp, "%lf ",current_value);
if((int)current_value > *u_x) {
*u_x = (int)current_value;
}
if((int)current_value < *l_x) {
*l_x = (int)current_value;
}
if(PL_get_list(list_y_vals,val,list_y_vals) <= 0) {
printf("error reading head x list.");
return -1;
}
if(PL_get_float(val,¤t_value) <= 0) {
printf("error converting x value.");
return -1;
}
fprintf(temp, "%lf\n",current_value);
if((int)current_value > *u_y) {
*u_y = (int)current_value;
}
if((int)current_value < *l_y) {
*l_y = (int)current_value;
}
}
fclose(temp);
return 0;
}
static foreign_t plot_point(term_t title, term_t ref_x, term_t ref_y) {
int lower_x = INT_MAX,lower_y = INT_MAX,upper_x = INT_MIN,upper_y = INT_MIN;
char *title_plot;
term_t len_x, len_y, list_x_vals, list_y_vals;
FILE *mycommands;
char *filename = "data.temp";
list_x_vals = PL_copy_term_ref(ref_x);
list_y_vals = PL_copy_term_ref(ref_y);
if(PL_skip_list(list_x_vals,0,&len_x) != PL_LIST) {
return PL_warning("List of X is not a proper list.");
}
if(PL_skip_list(list_y_vals,0,&len_y) != PL_LIST) {
return PL_warning("List of Y is not a proper list.");
}
if(len_x != len_y) {
return PL_warning("len list x != len list y.");
}
if(!PL_get_atom_chars(title, &title_plot)) {
return PL_warning("plot_test/3: instantiation fault title.\nAre you using \" instead of \' ?");
}
if(write_data_to_file(filename,list_x_vals,list_y_vals,(int)len_x,&lower_x,&upper_x,&lower_y,&upper_y) < 0) {
return PL_warning("error in processing data.");
}
mycommands = fopen("commands.txt","wt");
fprintf(mycommands,"set title \" %s \" font \",20\"\n",title_plot);
fprintf(mycommands,"set xrange [%d:%d]\n",lower_x-OFFSET,upper_x+OFFSET);
fprintf(mycommands, "set yrange [%d:%d]\n",lower_y-OFFSET,upper_y+OFFSET);
fprintf(mycommands,"plot'%s'\n","data.temp");
fclose(mycommands);
if(system("gnuplot -p <commands.txt") < 0) {
return PL_warning("unable to open shell.");
}
PL_succeed;
}
static foreign_t plot_line(term_t title, term_t ref_x, term_t ref_y) {
int lower_x = INT_MAX,lower_y = INT_MAX,upper_x = INT_MIN,upper_y = INT_MIN;
char *title_plot;
term_t list_x_vals, list_y_vals;
size_t len_x, len_y;
FILE *mycommands;
char *filename = "data.temp";
list_x_vals = PL_copy_term_ref(ref_x);
list_y_vals = PL_copy_term_ref(ref_y);
if(PL_skip_list(list_x_vals,0,&len_x) != PL_LIST) {
return PL_warning("List of X is not a proper list.");
}
if(PL_skip_list(list_y_vals,0,&len_y) != PL_LIST) {
return PL_warning("List of Y is not a proper list.");
}
if(len_x != len_y) {
return PL_warning("len list x != len list y.");
}
if(!PL_get_atom_chars(title, &title_plot)) {
return PL_warning("plot_test/3: instantiation fault title.\nAre you using \" instead of \' ?");
}
if(write_data_to_file(filename,list_x_vals,list_y_vals,(int)len_x,&lower_x,&upper_x,&lower_y,&upper_y) < 0) {
return PL_warning("error in processing data.");
}
mycommands = fopen("commands.txt","wt");
fprintf(mycommands,"set title \" %s \" font \",20\"\n",title_plot);
fprintf(mycommands,"set xrange [%d:%d]\n",lower_x-OFFSET,upper_x+OFFSET);
fprintf(mycommands, "set yrange [%d:%d]\n",lower_y-OFFSET,upper_y+OFFSET);
fprintf(mycommands,"plot'%s' using 1:2 with linespoints\n","data.temp");
fclose(mycommands);
if(system("gnuplot -p <commands.txt") < 0) {
return PL_warning("unable to open shell.");
}
PL_succeed;
}
// ok, sostituire quello prima con questo
static foreign_t plot_point_new(term_t title, term_t list_in) {
int lower_x = INT_MAX,lower_y = INT_MAX,upper_x = INT_MIN,upper_y = INT_MIN;
char *title_plot;
term_t list, list_pair = PL_new_term_ref(), list_x = PL_new_term_ref(), list_y = PL_new_term_ref();
size_t len_list, len_x, len_y;
FILE *mycommands;
char filename[100];
int i;
list = PL_copy_term_ref(list_in);
if(PL_skip_list(list,0,&len_list) != PL_LIST) {
return PL_warning("List is not a proper list.");
}
if(!PL_get_atom_chars(title, &title_plot)) {
return PL_warning("plot_test/3: instantiation fault title.\nAre you using \" instead of \' ?");
}
// printf("Len list: %d\n",len_list);
mycommands = fopen("commands.txt","wt");
fprintf(mycommands,"set title \" %s \" font \",20\"\n",title_plot);
// fprintf(mycommands,"set xrange [*:*] \n");
// fprintf(mycommands,"set yrange [*:*] \n");
// fprintf(mycommands,"set offset graph 0.10, 0.§10 \n");
fprintf(mycommands,"plot ");
for(i = 0; i < len_list; i++) {
if(PL_get_list(list, list_pair, list) < 0) {
fclose(mycommands);
return PL_warning("Unable to open list.");
}
printf("list pair\n");
if(PL_get_list(list_pair, list_x, list_pair) < 0) {
fclose(mycommands);
return PL_warning("Unable to open list.");
}
printf("list x\n");
if(PL_get_list(list_pair, list_y, list_pair) < 0) {
fclose(mycommands);
return PL_warning("Unable to open list.");
}
printf("list y\n");
if(PL_skip_list(list_x,0,&len_x) != PL_LIST) {
fclose(mycommands);
return PL_warning("List of X is not a proper list.");
}
if(PL_skip_list(list_y,0,&len_y) != PL_LIST) {
fclose(mycommands);
return PL_warning("List of Y is not a proper list.");
}
if(len_x != len_y) {
fclose(mycommands);
return PL_warning("len list x != len list y.");
}
sprintf(filename,"data_%d.temp",i);
printf("here\n");
if(write_data_to_file(filename,list_x,list_y,(int)len_x,&lower_x,&upper_x,&lower_y,&upper_y) < 0) {
fclose(mycommands);
return PL_warning("error in processing data.");
}
fprintf(mycommands,"'%s' using 1:2 title '%d'",filename,i);
if(i < len_list - 1) {
fprintf(mycommands,",");
}
}
fprintf(mycommands,"\n");
fclose(mycommands);
if(system("gnuplot -p <commands.txt") < 0) {
return PL_warning("unable to open shell.");
}
PL_succeed;
}
install_t install() {
PL_register_foreign("plot_point",3,plot_point,0);
PL_register_foreign("plot_line",3,plot_line,0);
PL_register_foreign("plot_point_new",2,plot_point_new,0);
}