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