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