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