1
|
1 // tree-plot.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
|
28 #include <iostream.h> |
86
|
29 #include <fstream.h> |
1
|
30 #include <strstream.h> |
|
31 |
|
32 #include "error.h" |
|
33 #include "utils.h" |
|
34 #include "tree.h" |
|
35 |
87
|
36 extern "C" |
|
37 { |
|
38 char *tilde_expand (char *s); /* From readline's tilde.c */ |
|
39 } |
|
40 |
1
|
41 // The number of lines we\'ve plotted so far. |
|
42 static int plot_line_count; |
|
43 |
|
44 // Is this a parametric plot? Makes a difference for 3D plotting. |
|
45 int parametric_plot = 0; |
|
46 |
|
47 /* |
|
48 * Plotting, eh? |
|
49 */ |
|
50 |
|
51 tree_plot_command::tree_plot_command (void) |
|
52 { |
|
53 range = (tree_plot_limits *) NULL; |
|
54 plot_list = (tree_subplot_list *) NULL; |
|
55 ndim = 0; |
|
56 } |
|
57 |
|
58 tree_plot_command::tree_plot_command (tree_subplot_list *plt, int nd) |
|
59 { |
|
60 range = (tree_plot_limits *) NULL; |
|
61 plot_list = plt; |
|
62 ndim = nd; |
|
63 } |
|
64 |
|
65 tree_plot_command::tree_plot_command (tree_subplot_list *plt, |
|
66 tree_plot_limits *rng, int nd) |
|
67 { |
|
68 range = rng; |
|
69 plot_list = plt; |
|
70 ndim = nd; |
|
71 } |
|
72 |
|
73 tree_plot_command::~tree_plot_command (void) |
|
74 { |
|
75 delete range; |
|
76 delete plot_list; |
|
77 } |
|
78 |
|
79 tree_constant |
|
80 tree_plot_command::eval (int print) |
|
81 { |
|
82 tree_constant retval; |
|
83 |
143
|
84 if (error_state) |
|
85 return retval; |
|
86 |
1
|
87 ostrstream plot_buf; |
|
88 |
|
89 switch (ndim) |
|
90 { |
|
91 case 2: |
|
92 plot_buf << "plot"; |
|
93 break; |
|
94 case 3: |
|
95 plot_buf << "splot"; |
|
96 break; |
|
97 default: |
|
98 panic_impossible (); |
|
99 break; |
|
100 } |
|
101 |
|
102 if (range != (tree_plot_limits *) NULL) |
|
103 range->print (ndim, plot_buf); |
|
104 |
191
|
105 if (error_state) |
|
106 return retval; |
|
107 |
1
|
108 plot_line_count = 0; |
|
109 tree_subplot_list *ptr = plot_list; |
|
110 for ( ; ptr != NULL_TREE ; ptr = ptr->next_elem ()) |
|
111 { |
|
112 plot_line_count++; |
|
113 |
|
114 if (ptr != plot_list) |
|
115 plot_buf << ",\\\n "; |
|
116 |
|
117 int status = ptr->print (ndim, plot_buf); |
|
118 if (status < 0) |
|
119 return retval; |
|
120 } |
|
121 |
|
122 plot_buf << "\n" << ends; |
|
123 |
|
124 // Just testing... |
|
125 // char *message = plot_buf.str (); |
|
126 // cout << "[*]" << message << "[*]\n"; |
|
127 |
|
128 if (parametric_plot && ndim == 2) |
|
129 { |
|
130 warning ("can't make 2D parametric plot -- setting noparametric..."); |
|
131 send_to_plot_stream ("set noparametric\n"); |
|
132 char *message = plot_buf.str (); |
|
133 send_to_plot_stream (message); |
|
134 delete [] message; |
|
135 send_to_plot_stream ("set parametric\n"); |
|
136 } |
|
137 else |
|
138 { |
|
139 char *message = plot_buf.str (); |
|
140 send_to_plot_stream (message); |
|
141 delete [] message; |
|
142 } |
|
143 |
|
144 return retval; |
|
145 } |
|
146 |
|
147 tree_subplot_list::tree_subplot_list (void) |
|
148 { |
|
149 plot_data = NULL_TREE; |
|
150 using = (tree_subplot_using *) NULL; |
|
151 title = NULL_TREE; |
|
152 style = (tree_subplot_style *) NULL; |
|
153 next = (tree_subplot_list *) NULL; |
|
154 } |
|
155 |
|
156 tree_subplot_list::tree_subplot_list (tree *data) |
|
157 { |
|
158 plot_data = data; |
|
159 using = (tree_subplot_using *) NULL; |
|
160 title = NULL_TREE; |
|
161 style = (tree_subplot_style *) NULL; |
|
162 next = (tree_subplot_list *) NULL; |
|
163 } |
|
164 |
|
165 tree_subplot_list::tree_subplot_list (tree_subplot_list *t) |
|
166 { |
|
167 plot_data = t->plot_data; |
|
168 using = t->using; |
|
169 title = t->title; |
|
170 style = t->style; |
|
171 next = t->next; |
|
172 } |
|
173 |
|
174 tree_subplot_list::tree_subplot_list (tree_subplot_using *u, tree *t, |
|
175 tree_subplot_style *s) |
|
176 { |
|
177 plot_data = NULL_TREE; |
|
178 using = u; |
|
179 title = t; |
|
180 style = s; |
|
181 next = (tree_subplot_list *) NULL; |
|
182 } |
|
183 |
|
184 tree_subplot_list::~tree_subplot_list (void) |
|
185 { |
|
186 delete plot_data; |
|
187 delete using; |
|
188 delete title; |
|
189 delete style; |
|
190 delete next; |
|
191 } |
|
192 |
|
193 tree_subplot_list * |
|
194 tree_subplot_list::set_data (tree *data) |
|
195 { |
|
196 plot_data = data; |
|
197 return this; |
|
198 } |
|
199 |
|
200 tree_subplot_list * |
|
201 tree_subplot_list::chain (tree_subplot_list *t) |
|
202 { |
|
203 tree_subplot_list *tmp = new tree_subplot_list (t); |
|
204 tmp->next = this; |
|
205 return tmp; |
|
206 } |
|
207 |
|
208 tree_subplot_list * |
|
209 tree_subplot_list::reverse (void) |
|
210 { |
|
211 tree_subplot_list *list = this; |
|
212 tree_subplot_list *next; |
|
213 tree_subplot_list *prev = (tree_subplot_list *) NULL; |
|
214 |
|
215 while (list != (tree_subplot_list *) NULL) |
|
216 { |
|
217 next = list->next; |
|
218 list->next = prev; |
|
219 prev = list; |
|
220 list = next; |
|
221 } |
|
222 return prev; |
|
223 } |
|
224 |
|
225 tree_subplot_list * |
|
226 tree_subplot_list::next_elem (void) |
|
227 { |
|
228 return next; |
|
229 } |
|
230 |
|
231 tree_constant |
|
232 tree_subplot_list::eval (int print) |
|
233 { |
|
234 return plot_data->eval (0); |
|
235 } |
|
236 |
|
237 int |
|
238 tree_subplot_list::print (int ndim, ostrstream& plot_buf) |
|
239 { |
|
240 int nc = 0; |
|
241 if (plot_data != NULL_TREE) |
|
242 { |
|
243 tree_constant data = plot_data->eval (0); |
191
|
244 if (! error_state && data.is_defined ()) |
1
|
245 { |
86
|
246 char *file = (char *) NULL; |
|
247 if (data.is_string_type ()) |
|
248 { |
87
|
249 file = tilde_expand (data.string_value ()); |
86
|
250 ifstream ftmp (file); |
|
251 if (ftmp) |
|
252 { |
|
253 plot_buf << " \"" << file << '"'; |
87
|
254 free (file); |
134
|
255 goto have_existing_file_or_command; |
86
|
256 } |
|
257 else |
87
|
258 { |
|
259 free (file); |
|
260 file = (char *) NULL; |
134
|
261 |
|
262 // Opening as a file failed. Let's try passing it along as a plot |
|
263 // command. |
|
264 plot_buf << " " << data.string_value (); |
|
265 goto have_existing_file_or_command; |
87
|
266 } |
86
|
267 } |
|
268 |
1
|
269 nc = data.columns (); |
|
270 switch (ndim) |
|
271 { |
|
272 case 2: |
|
273 file = save_in_tmp_file (data, ndim); |
|
274 break; |
|
275 case 3: |
|
276 file = save_in_tmp_file (data, ndim, parametric_plot); |
|
277 break; |
|
278 default: |
|
279 panic_impossible (); |
|
280 break; |
|
281 } |
|
282 |
|
283 if (file) |
|
284 { |
|
285 mark_for_deletion (file); |
|
286 plot_buf << " \"" << file << '"'; |
|
287 } |
|
288 } |
|
289 else |
|
290 return -1; |
|
291 } |
|
292 else |
|
293 return -1; |
|
294 |
134
|
295 have_existing_file_or_command: |
86
|
296 |
1
|
297 if (using != (tree_subplot_using *) NULL) |
|
298 { |
|
299 int status = using->print (ndim, nc, plot_buf); |
|
300 if (status < 0) |
|
301 return -1; |
|
302 } |
|
303 |
|
304 if (title != NULL_TREE) |
|
305 { |
|
306 tree_constant tmp = title->eval (0); |
191
|
307 if (! error_state && tmp.is_string_type ()) |
1
|
308 plot_buf << " title " << '"' << tmp.string_value () << '"'; |
|
309 else |
|
310 { |
|
311 warning ("line title must be a string"); |
|
312 plot_buf << " title " << '"' << "line " << plot_line_count << '"'; |
|
313 } |
|
314 } |
|
315 else |
|
316 plot_buf << " title " << '"' << "line " << plot_line_count << '"'; |
|
317 |
|
318 if (style != (tree_subplot_style *) NULL) |
|
319 { |
|
320 int status = style->print (plot_buf); |
|
321 if (status < 0) |
|
322 return -1; |
|
323 } |
|
324 |
|
325 return 0; |
|
326 } |
|
327 |
|
328 tree_plot_limits::tree_plot_limits (void) |
|
329 { |
|
330 x_range = (tree_plot_range *) NULL; |
|
331 y_range = (tree_plot_range *) NULL; |
|
332 z_range = (tree_plot_range *) NULL; |
|
333 } |
|
334 |
|
335 tree_plot_limits::tree_plot_limits (tree_plot_range *xlim) |
|
336 { |
|
337 x_range = xlim; |
|
338 y_range = (tree_plot_range *) NULL; |
|
339 z_range = (tree_plot_range *) NULL; |
|
340 } |
|
341 |
|
342 tree_plot_limits::tree_plot_limits (tree_plot_range *xlim, |
|
343 tree_plot_range *ylim) |
|
344 { |
|
345 x_range = xlim; |
|
346 y_range = ylim; |
|
347 z_range = (tree_plot_range *) NULL; |
|
348 } |
|
349 |
|
350 tree_plot_limits::tree_plot_limits (tree_plot_range *xlim, |
|
351 tree_plot_range *ylim, |
|
352 tree_plot_range *zlim) |
|
353 { |
|
354 x_range = xlim; |
|
355 y_range = ylim; |
|
356 z_range = zlim; |
|
357 } |
|
358 |
|
359 tree_plot_limits::~tree_plot_limits (void) |
|
360 { |
|
361 delete x_range; |
|
362 delete y_range; |
|
363 delete z_range; |
|
364 } |
|
365 |
|
366 tree_constant |
|
367 tree_plot_limits::eval (int print) |
|
368 { |
|
369 tree_constant retval; |
|
370 return retval; |
|
371 } |
|
372 |
|
373 void |
|
374 tree_plot_limits::print (int ndim, ostrstream& plot_buf) |
|
375 { |
|
376 if (ndim == 2 || ndim == 3) |
|
377 { |
|
378 if (x_range != (tree_plot_range *) NULL) |
|
379 x_range->print (plot_buf); |
|
380 else |
|
381 return; |
|
382 |
|
383 if (y_range != (tree_plot_range *) NULL) |
|
384 y_range->print (plot_buf); |
|
385 else |
|
386 return; |
|
387 } |
|
388 |
|
389 if (ndim == 3 && z_range != (tree_plot_range *) NULL) |
|
390 z_range->print (plot_buf); |
|
391 } |
|
392 |
|
393 tree_plot_range::tree_plot_range (void) |
|
394 { |
|
395 lower = NULL_TREE; |
|
396 upper = NULL_TREE; |
|
397 } |
|
398 |
|
399 tree_plot_range::tree_plot_range (tree *l, tree *u) |
|
400 { |
|
401 lower = l; |
|
402 upper = u; |
|
403 } |
|
404 |
|
405 tree_plot_range::~tree_plot_range (void) |
|
406 { |
|
407 delete lower; |
|
408 delete upper; |
|
409 } |
|
410 |
|
411 tree_constant |
|
412 tree_plot_range::eval (int print) |
|
413 { |
|
414 tree_constant retval; |
|
415 return retval; |
|
416 } |
|
417 |
|
418 void |
|
419 tree_plot_range::print (ostrstream& plot_buf) |
|
420 { |
|
421 plot_buf << " ["; |
|
422 |
|
423 if (lower != NULL_TREE) |
|
424 { |
|
425 tree_constant lower_val = lower->eval (0); |
191
|
426 if (error_state) |
|
427 { |
240
|
428 ::error ("evaluating lower bound of plot range"); |
191
|
429 return; |
|
430 } |
|
431 else |
|
432 { |
|
433 double lo = lower_val.to_scalar (); |
|
434 plot_buf << lo; |
|
435 } |
1
|
436 } |
|
437 |
|
438 plot_buf << ":"; |
|
439 |
|
440 if (upper != NULL_TREE) |
|
441 { |
|
442 tree_constant upper_val = upper->eval (0); |
191
|
443 if (error_state) |
|
444 { |
240
|
445 ::error ("evaluating upper bound of plot range"); |
191
|
446 return; |
|
447 } |
|
448 else |
|
449 { |
|
450 double hi = upper_val.to_scalar (); |
|
451 plot_buf << hi; |
|
452 } |
1
|
453 } |
|
454 |
|
455 plot_buf << "]"; |
|
456 } |
|
457 |
|
458 tree_subplot_using::tree_subplot_using (void) |
|
459 { |
|
460 qualifier_count = 0; |
|
461 x[0] = NULL_TREE; |
|
462 x[1] = NULL_TREE; |
|
463 x[2] = NULL_TREE; |
|
464 x[3] = NULL_TREE; |
|
465 scanf_fmt = NULL_TREE; |
|
466 } |
|
467 |
|
468 tree_subplot_using::tree_subplot_using (tree *fmt) |
|
469 { |
|
470 qualifier_count = 0; |
|
471 x[0] = NULL_TREE; |
|
472 x[1] = NULL_TREE; |
|
473 x[2] = NULL_TREE; |
|
474 x[3] = NULL_TREE; |
|
475 scanf_fmt = fmt; |
|
476 } |
|
477 |
|
478 tree_subplot_using::~tree_subplot_using (void) |
|
479 { |
|
480 delete scanf_fmt; |
|
481 } |
|
482 |
|
483 tree_subplot_using * |
|
484 tree_subplot_using::set_format (tree *fmt) |
|
485 { |
|
486 scanf_fmt = fmt; |
|
487 return this; |
|
488 } |
|
489 |
|
490 tree_subplot_using * |
|
491 tree_subplot_using::add_qualifier (tree *t) |
|
492 { |
|
493 if (qualifier_count < 4) |
|
494 x[qualifier_count] = t; |
|
495 |
|
496 qualifier_count++; |
|
497 |
|
498 return this; |
|
499 } |
|
500 |
|
501 tree_constant |
|
502 tree_subplot_using::eval (int print) |
|
503 { |
|
504 tree_constant retval; |
|
505 return retval; |
|
506 } |
|
507 |
|
508 int |
|
509 tree_subplot_using::print (int ndim, int n_max, ostrstream& plot_buf) |
|
510 { |
|
511 if ((ndim == 2 && qualifier_count > 4) |
|
512 || (ndim == 3 && qualifier_count > 3)) |
|
513 return -1; |
|
514 |
|
515 for (int i = 0; i < qualifier_count; i++) |
|
516 { |
|
517 if (x[i] != NULL_TREE) |
|
518 { |
|
519 tree_constant tmp = x[i]->eval (0); |
191
|
520 if (error_state) |
|
521 { |
240
|
522 ::error ("evaluating plot using command"); |
191
|
523 return -1; |
|
524 } |
|
525 |
1
|
526 double val; |
|
527 if (tmp.is_defined ()) |
|
528 { |
|
529 val = tmp.to_scalar (); |
|
530 if (i == 0) |
|
531 plot_buf << " using "; |
|
532 else |
|
533 plot_buf << ":"; |
|
534 |
|
535 int n = NINT (val); |
|
536 |
|
537 if (n > n_max || n < 1) |
|
538 { |
240
|
539 ::error ("using: column %d out of range", n); |
1
|
540 return -1; |
|
541 } |
|
542 else |
|
543 plot_buf << n; |
|
544 } |
|
545 else |
|
546 return -1; |
|
547 } |
|
548 else |
|
549 return -1; |
|
550 } |
|
551 |
|
552 if (scanf_fmt != NULL_TREE) |
|
553 warning ("ignoring scanf format in plot command"); |
|
554 |
|
555 return 0; |
|
556 } |
|
557 |
|
558 tree_subplot_style::tree_subplot_style (void) |
|
559 { |
|
560 style = (char *) NULL; |
|
561 linetype = NULL_TREE; |
|
562 pointtype = NULL_TREE; |
|
563 } |
|
564 |
|
565 tree_subplot_style::tree_subplot_style (char *s) |
|
566 { |
|
567 style = strsave (s); |
|
568 linetype = NULL_TREE; |
|
569 pointtype = NULL_TREE; |
|
570 } |
|
571 |
|
572 tree_subplot_style::tree_subplot_style (char *s, tree *lt) |
|
573 { |
|
574 style = strsave (s); |
|
575 linetype = lt; |
|
576 pointtype = NULL_TREE; |
|
577 } |
|
578 |
|
579 tree_subplot_style::tree_subplot_style (char *s, tree *lt, tree *pt) |
|
580 { |
|
581 style = strsave (s); |
|
582 linetype = lt; |
|
583 pointtype = pt; |
|
584 } |
|
585 |
|
586 tree_subplot_style::~tree_subplot_style (void) |
|
587 { |
|
588 delete [] style; |
|
589 delete linetype; |
|
590 delete pointtype; |
|
591 } |
|
592 |
|
593 tree_constant |
|
594 tree_subplot_style::eval (int print) |
|
595 { |
|
596 tree_constant retval; |
|
597 return retval; |
|
598 } |
|
599 |
|
600 int |
|
601 tree_subplot_style::print (ostrstream& plot_buf) |
|
602 { |
|
603 if (style != (char *) NULL) |
|
604 { |
|
605 plot_buf << " with " << style; |
|
606 |
|
607 if (linetype != NULL_TREE) |
|
608 { |
|
609 tree_constant tmp = linetype->eval (0); |
191
|
610 if (! error_state && tmp.is_defined ()) |
1
|
611 { |
|
612 double val = tmp.to_scalar (); |
|
613 plot_buf << " " << NINT (val); |
|
614 } |
|
615 else |
191
|
616 { |
240
|
617 ::error ("evaluating plot style command"); |
191
|
618 return -1; |
|
619 } |
1
|
620 } |
|
621 |
|
622 if (pointtype != NULL_TREE) |
|
623 { |
|
624 tree_constant tmp = pointtype->eval (0); |
191
|
625 if (! error_state && tmp.is_defined ()) |
1
|
626 { |
|
627 double val = tmp.to_scalar (); |
|
628 plot_buf << " " << NINT (val); |
|
629 } |
|
630 else |
191
|
631 { |
240
|
632 ::error ("evaluating plot style command"); |
191
|
633 return -1; |
|
634 } |
1
|
635 } |
|
636 } |
|
637 else |
|
638 return -1; |
|
639 |
|
640 return 0; |
|
641 } |
|
642 |
|
643 /* |
|
644 ;;; Local Variables: *** |
|
645 ;;; mode: C++ *** |
|
646 ;;; page-delimiter: "^/\\*" *** |
|
647 ;;; End: *** |
|
648 */ |