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