6406
|
1 /* |
|
2 |
|
3 Copyright (C) 2007 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
6406
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
6406
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cctype> |
7222
|
28 #include <cfloat> |
7286
|
29 #include <cstdlib> |
6406
|
30 |
|
31 #include <algorithm> |
|
32 #include <list> |
|
33 #include <map> |
|
34 #include <set> |
|
35 #include <string> |
|
36 |
7409
|
37 #include "file-ops.h" |
|
38 #include "file-stat.h" |
|
39 |
6705
|
40 #include "defun.h" |
|
41 #include "error.h" |
6595
|
42 #include "graphics.h" |
7409
|
43 #include "input.h" |
6705
|
44 #include "ov.h" |
|
45 #include "oct-obj.h" |
|
46 #include "oct-map.h" |
|
47 #include "ov-fcn-handle.h" |
|
48 #include "parse.h" |
7409
|
49 #include "toplev.h" |
7222
|
50 #include "unwind-prot.h" |
6595
|
51 |
6406
|
52 static void |
|
53 gripe_set_invalid (const std::string& pname) |
|
54 { |
|
55 error ("set: invalid value for %s property", pname.c_str ()); |
|
56 } |
|
57 |
7363
|
58 static Matrix |
|
59 jet_colormap (void) |
|
60 { |
|
61 Matrix cmap (64, 3, 0.0); |
|
62 |
|
63 for (octave_idx_type i = 0; i < 64; i++) |
|
64 { |
|
65 // This is the jet colormap. It would be nice to be able |
|
66 // to feval the jet function but since there is a static |
|
67 // property object that includes a colormap_property |
|
68 // object, we need to initialize this before main is even |
|
69 // called, so calling an interpreted function is not |
|
70 // possible. |
|
71 |
|
72 double x = i / 63.0; |
|
73 |
|
74 if (x >= 3.0/8.0 && x < 5.0/8.0) |
|
75 cmap(i,0) = 4.0 * x - 3.0/2.0; |
|
76 else if (x >= 5.0/8.0 && x < 7.0/8.0) |
|
77 cmap(i,0) = 1.0; |
|
78 else if (x >= 7.0/8.0) |
|
79 cmap(i,0) = -4.0 * x + 9.0/2.0; |
|
80 |
|
81 if (x >= 1.0/8.0 && x < 3.0/8.0) |
|
82 cmap(i,1) = 4.0 * x - 1.0/2.0; |
|
83 else if (x >= 3.0/8.0 && x < 5.0/8.0) |
|
84 cmap(i,1) = 1.0; |
|
85 else if (x >= 5.0/8.0 && x < 7.0/8.0) |
|
86 cmap(i,1) = -4.0 * x + 7.0/2.0; |
|
87 |
|
88 if (x < 1.0/8.0) |
|
89 cmap(i,2) = 4.0 * x + 1.0/2.0; |
|
90 else if (x >= 1.0/8.0 && x < 3.0/8.0) |
|
91 cmap(i,2) = 1.0; |
|
92 else if (x >= 3.0/8.0 && x < 5.0/8.0) |
|
93 cmap(i,2) = -4.0 * x + 5.0/2.0; |
|
94 } |
|
95 |
|
96 return cmap; |
|
97 } |
|
98 |
|
99 static Matrix |
|
100 default_colororder (void) |
|
101 { |
|
102 Matrix retval (7, 3, 0.0); |
|
103 |
|
104 retval(0,2) = 1.0; |
|
105 |
|
106 retval(1,1) = 0.5; |
|
107 |
|
108 retval(2,0) = 1.0; |
|
109 |
|
110 retval(3,1) = 0.75; |
|
111 retval(3,2) = 0.75; |
|
112 |
|
113 retval(4,0) = 0.75; |
|
114 retval(4,2) = 0.75; |
|
115 |
|
116 retval(5,0) = 0.75; |
|
117 retval(5,1) = 0.75; |
|
118 |
|
119 retval(6,0) = 0.25; |
|
120 retval(6,1) = 0.25; |
|
121 retval(6,2) = 0.25; |
|
122 |
|
123 return retval; |
|
124 } |
|
125 |
|
126 static Matrix |
|
127 default_lim (void) |
|
128 { |
|
129 Matrix m (1, 2, 0); |
|
130 m(1) = 1; |
|
131 return m; |
|
132 } |
|
133 |
|
134 static Matrix |
|
135 default_data (void) |
|
136 { |
|
137 Matrix retval (1, 2); |
|
138 |
|
139 retval(0) = 0; |
|
140 retval(1) = 1; |
|
141 |
|
142 return retval; |
|
143 } |
|
144 |
7427
|
145 static Matrix |
|
146 default_axes_position (void) |
|
147 { |
|
148 Matrix m (1, 4, 0.0); |
|
149 m(0) = 0.13; |
|
150 m(1) = 0.11; |
|
151 m(2) = 0.775; |
|
152 m(3) = 0.815; |
|
153 return m; |
|
154 } |
|
155 |
|
156 static Matrix |
|
157 default_axes_outerposition (void) |
|
158 { |
|
159 Matrix m (1, 4, 0.0); |
|
160 m(2) = m(3) = 1.0; |
|
161 return m; |
|
162 } |
|
163 |
7367
|
164 // NOTE: "cb" is passed by value, because "function_value" method |
|
165 // is non-const; passing "cb" by const-reference is not |
|
166 // possible |
|
167 |
|
168 static void |
|
169 execute_callback (octave_value cb, const graphics_handle& h, |
|
170 const octave_value& data) |
|
171 { |
|
172 octave_value_list args; |
|
173 octave_function *fcn = 0; |
|
174 |
|
175 args(0) = h.as_octave_value (); |
|
176 args(1) = data; |
|
177 |
|
178 BEGIN_INTERRUPT_WITH_EXCEPTIONS; |
|
179 |
|
180 if (cb.is_function_handle ()) |
|
181 fcn = cb.function_value (); |
|
182 else if (cb.is_string ()) |
|
183 { |
|
184 std::string s = cb.string_value (); |
|
185 octave_value f = symbol_table::find_function (s); |
|
186 int status; |
|
187 |
|
188 if (f.is_defined ()) |
|
189 fcn = f.function_value (); |
|
190 else |
|
191 { |
|
192 eval_string (s, false, status); |
|
193 return; |
|
194 } |
|
195 } |
|
196 else if (cb.is_cell () && cb.length () > 0 |
|
197 && (cb.rows () == 1 || cb.columns () == 1) |
|
198 && cb.cell_value ()(0).is_function_handle ()) |
|
199 { |
|
200 Cell c = cb.cell_value (); |
|
201 |
|
202 fcn = c(0).function_value (); |
|
203 if (! error_state) |
|
204 { |
|
205 for (int i = 0; i < c.length () ; i++) |
|
206 args(2+i) = c(i); |
|
207 } |
|
208 } |
|
209 else |
7373
|
210 { |
|
211 std::string nm = cb.class_name (); |
|
212 error ("trying to execute non-executable object (class = %s)", |
|
213 nm.c_str ()); |
|
214 } |
7367
|
215 |
|
216 if (! error_state) |
|
217 feval (fcn, args); |
|
218 |
|
219 END_INTERRUPT_WITH_EXCEPTIONS; |
|
220 } |
|
221 |
7427
|
222 static Matrix |
|
223 convert_position (const Matrix& pos, const caseless_str& from_units, |
|
224 const caseless_str& to_units, |
|
225 const Matrix& parent_dim = Matrix (1, 2, 0.0), |
|
226 const graphics_backend& backend = graphics_backend ()) |
|
227 { |
|
228 Matrix retval (1, 4); |
|
229 double res = 0; |
|
230 |
|
231 if (from_units.compare ("pixels")) |
|
232 retval = pos; |
|
233 else if (from_units.compare ("normalized")) |
|
234 { |
|
235 retval(0) = pos(0) * parent_dim(0) + 1; |
|
236 retval(1) = pos(1) * parent_dim(1) + 1; |
|
237 retval(2) = pos(2) * parent_dim(0); |
|
238 retval(3) = pos(3) * parent_dim(1); |
|
239 } |
|
240 else if (from_units.compare ("characters")) |
|
241 { |
|
242 // FIXME: implement this |
|
243 } |
|
244 else |
|
245 { |
|
246 res = backend.get_screen_resolution (); |
|
247 |
|
248 double f = 0.0; |
|
249 |
|
250 if (from_units.compare ("points")) |
|
251 f = res / 72.0; |
|
252 else if (from_units.compare ("inches")) |
|
253 f = res; |
|
254 else if (from_units.compare ("centimeters")) |
|
255 f = res / 2.54; |
|
256 |
|
257 if (f > 0) |
|
258 { |
|
259 retval(0) = pos(0) * f + 1; |
|
260 retval(1) = pos(1) * f + 1; |
|
261 retval(2) = pos(2) * f; |
|
262 retval(3) = pos(3) * f; |
|
263 } |
|
264 } |
|
265 |
|
266 if (! to_units.compare ("pixels")) |
|
267 { |
|
268 if (to_units.compare ("normalized")) |
|
269 { |
|
270 retval(0) = (retval(0) - 1) / parent_dim(0); |
|
271 retval(1) = (retval(1) - 1) / parent_dim(1); |
|
272 retval(2) /= parent_dim(0); |
|
273 retval(3) /= parent_dim(1); |
|
274 } |
|
275 else if (to_units.compare ("characters")) |
|
276 { |
|
277 // FIXME: implement this |
|
278 } |
|
279 else |
|
280 { |
|
281 if (res <= 0) |
|
282 res = backend.get_screen_resolution (); |
|
283 |
|
284 double f = 0.0; |
|
285 |
|
286 if (to_units.compare ("points")) |
|
287 f = res / 72.0; |
|
288 else if (to_units.compare ("inches")) |
|
289 f = res; |
|
290 else if (to_units.compare ("centimeters")) |
|
291 f = res / 2.54; |
|
292 |
|
293 if (f > 0) |
|
294 { |
|
295 retval(0) = (retval(0) - 1) / f; |
|
296 retval(1) = (retval(1) - 1) / f; |
|
297 retval(2) /= f; |
|
298 retval(3) /= f; |
|
299 } |
|
300 } |
|
301 } |
|
302 |
|
303 return retval; |
|
304 } |
|
305 |
6406
|
306 // --------------------------------------------------------------------- |
|
307 |
6705
|
308 radio_values::radio_values (const std::string& opt_string) |
6406
|
309 { |
6705
|
310 size_t beg = 0; |
|
311 size_t len = opt_string.length (); |
|
312 bool done = len == 0; |
6681
|
313 |
6705
|
314 while (! done) |
|
315 { |
|
316 size_t end = opt_string.find ('|', beg); |
6681
|
317 |
6705
|
318 if (end == std::string::npos) |
|
319 { |
|
320 end = len; |
|
321 done = true; |
|
322 } |
6681
|
323 |
6705
|
324 std::string t = opt_string.substr (beg, end-beg); |
6681
|
325 |
6705
|
326 // Might want more error checking here... |
|
327 if (t[0] == '{') |
|
328 { |
|
329 t = t.substr (1, t.length () - 2); |
|
330 default_val = t; |
|
331 } |
|
332 else if (beg == 0) // ensure default value |
|
333 default_val = t; |
6681
|
334 |
6705
|
335 possible_vals.insert (t); |
6681
|
336 |
6705
|
337 beg = end + 1; |
|
338 } |
|
339 } |
6681
|
340 |
6705
|
341 bool |
6761
|
342 color_values::str2rgb (std::string str) |
6681
|
343 { |
6705
|
344 double tmp_rgb[3] = {0, 0, 0}; |
|
345 bool retval = true; |
6761
|
346 unsigned int len = str.length(); |
6681
|
347 |
6925
|
348 std::transform (str.begin (), str.end (), str.begin (), tolower); |
|
349 |
6761
|
350 if (str.compare(0, len, "blue", 0, len) == 0) |
|
351 tmp_rgb[2] = 1; |
6925
|
352 else if (str.compare(0, len, "black", 0, len) == 0 || |
|
353 str.compare(0, len, "k", 0, len) == 0) |
6761
|
354 tmp_rgb[0] = tmp_rgb[1] = tmp_rgb[2] = 0; |
|
355 else if (str.compare(0, len, "red", 0, len) == 0) |
|
356 tmp_rgb[0] = 1; |
|
357 else if (str.compare(0, len, "green", 0, len) == 0) |
|
358 tmp_rgb[1] = 1; |
|
359 else if (str.compare(0, len, "yellow", 0, len) == 0) |
|
360 tmp_rgb[0] = tmp_rgb[1] = 1; |
|
361 else if (str.compare(0, len, "magenta", 0, len) == 0) |
|
362 tmp_rgb[0] = tmp_rgb[2] = 1; |
|
363 else if (str.compare(0, len, "cyan", 0, len) == 0) |
|
364 tmp_rgb[1] = tmp_rgb[2] = 1; |
6925
|
365 else if (str.compare(0, len, "white", 0, len) == 0 || |
|
366 str.compare(0, len, "w", 0, len) == 0) |
6761
|
367 tmp_rgb[0] = tmp_rgb[1] = tmp_rgb[2] = 1; |
|
368 else |
|
369 retval = false; |
6406
|
370 |
6705
|
371 if (retval) |
|
372 { |
|
373 for (int i = 0; i < 3; i++) |
7363
|
374 xrgb(i) = tmp_rgb[i]; |
6705
|
375 } |
6563
|
376 |
6705
|
377 return retval; |
|
378 } |
6681
|
379 |
7363
|
380 void |
|
381 color_property::set (const octave_value& val) |
6790
|
382 { |
|
383 if (val.is_string ()) |
|
384 { |
|
385 std::string s = val.string_value (); |
|
386 |
|
387 if (! s.empty ()) |
|
388 { |
6898
|
389 if (radio_val.contains (s)) |
6790
|
390 { |
|
391 current_val = s; |
|
392 current_type = radio_t; |
|
393 } |
|
394 else |
|
395 { |
|
396 color_values col (s); |
|
397 if (! error_state) |
|
398 { |
|
399 color_val = col; |
|
400 current_type = color_t; |
|
401 } |
|
402 else |
7363
|
403 error ("invalid value for color property \"%s\" (value = %s)", |
|
404 get_name ().c_str (), s.c_str ()); |
6790
|
405 } |
|
406 } |
|
407 else |
7363
|
408 error ("invalid value for color property \"%s\"", |
|
409 get_name ().c_str ()); |
6790
|
410 } |
|
411 else if (val.is_real_matrix ()) |
|
412 { |
|
413 Matrix m = val.matrix_value (); |
|
414 |
|
415 if (m.numel () == 3) |
|
416 { |
|
417 color_values col (m (0), m (1), m(2)); |
|
418 if (! error_state) |
|
419 { |
|
420 color_val = col; |
|
421 current_type = color_t; |
|
422 } |
|
423 } |
|
424 else |
7363
|
425 error ("invalid value for color property \"%s\"", |
|
426 get_name ().c_str ()); |
6790
|
427 } |
|
428 else |
7363
|
429 error ("invalid value for color property \"%s\"", |
|
430 get_name ().c_str ()); |
6790
|
431 } |
|
432 |
7363
|
433 bool |
|
434 array_property::validate (const octave_value& v) |
|
435 { |
7364
|
436 bool xok = false; |
7363
|
437 |
|
438 // FIXME: should we always support []? |
|
439 if (v.is_empty () && v.is_double_type ()) |
|
440 return true; |
|
441 |
|
442 // check value type |
|
443 if (type_constraints.size () > 0) |
|
444 { |
|
445 for (std::list<std::string>::const_iterator it = type_constraints.begin (); |
7364
|
446 ! xok && it != type_constraints.end (); ++it) |
7363
|
447 if ((*it) == v.type_name ()) |
7364
|
448 xok = true; |
7363
|
449 } |
|
450 else |
7364
|
451 xok = v.is_double_type (); |
7363
|
452 |
7364
|
453 if (xok) |
7363
|
454 { |
|
455 dim_vector vdims = v.dims (); |
|
456 int vlen = vdims.length (); |
|
457 |
7364
|
458 xok = false; |
7363
|
459 |
|
460 // check value size |
|
461 if (size_constraints.size () > 0) |
|
462 for (std::list<dim_vector>::const_iterator it = size_constraints.begin (); |
7364
|
463 ! xok && it != size_constraints.end (); ++it) |
7363
|
464 { |
|
465 dim_vector itdims = (*it); |
|
466 |
|
467 if (itdims.length () == vlen) |
|
468 { |
7364
|
469 xok = true; |
7363
|
470 |
7364
|
471 for (int i = 0; xok && i < vlen; i++) |
7363
|
472 if (itdims(i) >= 0 && itdims(i) != vdims(i)) |
7364
|
473 xok = false; |
7363
|
474 } |
|
475 } |
|
476 else |
|
477 return true; |
|
478 } |
|
479 |
7364
|
480 return xok; |
7363
|
481 } |
|
482 |
|
483 void |
|
484 handle_property::set (const octave_value& v) |
|
485 { |
|
486 double dv = v.double_value (); |
|
487 |
|
488 if (! error_state) |
|
489 { |
|
490 graphics_handle gh = gh_manager::lookup (dv); |
|
491 |
|
492 if (xisnan (gh.value ()) || gh.ok ()) |
|
493 current_val = gh; |
|
494 else |
|
495 error ("set: invalid graphics handle (= %g) for property \"%s\"", |
|
496 dv, get_name ().c_str ()); |
|
497 } |
|
498 else |
|
499 error ("set: invalid graphics handle for property \"%s\"", |
|
500 get_name ().c_str ()); |
|
501 } |
|
502 |
|
503 bool |
7367
|
504 callback_property::validate (const octave_value& v) const |
7363
|
505 { |
7367
|
506 // case 1: function handle |
|
507 // case 2: cell array with first element being a function handle |
|
508 // case 3: string corresponding to known function name |
|
509 // case 4: evaluatable string |
|
510 // case 5: empty matrix |
|
511 |
|
512 if (v.is_function_handle ()) |
|
513 return true; |
|
514 else if (v.is_string ()) |
|
515 // complete validation will be done at execution-time |
|
516 return true; |
|
517 else if (v.is_cell () && v.length () > 0 |
|
518 && (v.rows() == 1 || v.columns () == 1) |
|
519 && v.cell_value ()(0).is_function_handle ()) |
|
520 return true; |
|
521 else if (v.is_empty ()) |
|
522 return true; |
|
523 |
|
524 return false; |
7363
|
525 } |
|
526 |
|
527 void |
7367
|
528 callback_property::execute (const octave_value& data) const |
7363
|
529 { |
7367
|
530 if (callback.is_defined () && ! callback.is_empty ()) |
|
531 execute_callback (callback, get_parent (), data); |
7363
|
532 } |
|
533 |
|
534 // --------------------------------------------------------------------- |
6681
|
535 |
6705
|
536 void |
7189
|
537 property_list::set (const caseless_str& name, const octave_value& val) |
6681
|
538 { |
6705
|
539 size_t offset = 0; |
6681
|
540 |
6705
|
541 size_t len = name.length (); |
6681
|
542 |
6705
|
543 if (len > 4) |
|
544 { |
7189
|
545 caseless_str pfx = name.substr (0, 4); |
6681
|
546 |
6705
|
547 if (pfx.compare ("axes") || pfx.compare ("line") |
|
548 || pfx.compare ("text")) |
|
549 offset = 4; |
|
550 else if (len > 5) |
|
551 { |
|
552 pfx = name.substr (0, 5); |
6681
|
553 |
6807
|
554 if (pfx.compare ("image") || pfx.compare ("patch")) |
6705
|
555 offset = 5; |
|
556 else if (len > 6) |
|
557 { |
|
558 pfx = name.substr (0, 6); |
6681
|
559 |
6705
|
560 if (pfx.compare ("figure")) |
|
561 offset = 6; |
|
562 else if (len > 7) |
|
563 { |
|
564 pfx = name.substr (0, 7); |
6681
|
565 |
6705
|
566 if (pfx.compare ("surface")) |
|
567 offset = 7; |
|
568 } |
|
569 } |
|
570 } |
6681
|
571 |
6705
|
572 if (offset > 0) |
|
573 { |
|
574 // FIXME -- should we validate property names and values here? |
6406
|
575 |
6705
|
576 std::string pname = name.substr (offset); |
6406
|
577 |
6705
|
578 std::transform (pfx.begin (), pfx.end (), pfx.begin (), tolower); |
|
579 std::transform (pname.begin (), pname.end (), pname.begin (), tolower); |
6406
|
580 |
6705
|
581 bool remove = false; |
|
582 if (val.is_string ()) |
|
583 { |
7189
|
584 caseless_str tval = val.string_value (); |
6406
|
585 |
6705
|
586 remove = tval.compare ("remove"); |
|
587 } |
6406
|
588 |
6705
|
589 pval_map_type& pval_map = plist_map[pfx]; |
6406
|
590 |
6705
|
591 if (remove) |
|
592 { |
|
593 pval_map_iterator p = pval_map.find (pname); |
6406
|
594 |
6705
|
595 if (p != pval_map.end ()) |
|
596 pval_map.erase (p); |
|
597 } |
|
598 else |
|
599 pval_map[pname] = val; |
|
600 } |
|
601 } |
6406
|
602 |
6705
|
603 if (offset == 0) |
|
604 error ("invalid default property specification"); |
|
605 } |
6406
|
606 |
6705
|
607 octave_value |
7189
|
608 property_list::lookup (const caseless_str& name) const |
6705
|
609 { |
|
610 octave_value retval; |
6406
|
611 |
6705
|
612 size_t offset = 0; |
6406
|
613 |
6705
|
614 size_t len = name.length (); |
6406
|
615 |
6705
|
616 if (len > 4) |
|
617 { |
7189
|
618 caseless_str pfx = name.substr (0, 4); |
6406
|
619 |
6705
|
620 if (pfx.compare ("axes") || pfx.compare ("line") |
|
621 || pfx.compare ("text")) |
|
622 offset = 4; |
|
623 else if (len > 5) |
|
624 { |
|
625 pfx = name.substr (0, 5); |
6406
|
626 |
6807
|
627 if (pfx.compare ("image") || pfx.compare ("patch")) |
6705
|
628 offset = 5; |
|
629 else if (len > 6) |
|
630 { |
|
631 pfx = name.substr (0, 6); |
6406
|
632 |
6705
|
633 if (pfx.compare ("figure")) |
|
634 offset = 6; |
|
635 else if (len > 7) |
|
636 { |
|
637 pfx = name.substr (0, 7); |
6406
|
638 |
6705
|
639 if (pfx.compare ("surface")) |
|
640 offset = 7; |
|
641 } |
|
642 } |
|
643 } |
6406
|
644 |
6705
|
645 if (offset > 0) |
|
646 { |
|
647 std::string pname = name.substr (offset); |
6406
|
648 |
6705
|
649 std::transform (pfx.begin (), pfx.end (), pfx.begin (), tolower); |
|
650 std::transform (pname.begin (), pname.end (), pname.begin (), tolower); |
6406
|
651 |
6705
|
652 plist_map_const_iterator p = find (pfx); |
6432
|
653 |
6705
|
654 if (p != end ()) |
|
655 { |
|
656 const pval_map_type& pval_map = p->second; |
6406
|
657 |
6705
|
658 pval_map_const_iterator q = pval_map.find (pname); |
6406
|
659 |
6705
|
660 if (q != pval_map.end ()) |
|
661 retval = q->second; |
|
662 } |
|
663 } |
|
664 } |
6406
|
665 |
6705
|
666 return retval; |
|
667 } |
6406
|
668 |
6705
|
669 Octave_map |
|
670 property_list::as_struct (const std::string& prefix_arg) const |
|
671 { |
|
672 Octave_map m; |
6406
|
673 |
6705
|
674 for (plist_map_const_iterator p = begin (); p != end (); p++) |
|
675 { |
|
676 std::string prefix = prefix_arg + p->first; |
6406
|
677 |
6705
|
678 const pval_map_type pval_map = p->second; |
6406
|
679 |
6705
|
680 for (pval_map_const_iterator q = pval_map.begin (); |
|
681 q != pval_map.end (); |
|
682 q++) |
|
683 m.assign (prefix + q->first, q->second); |
|
684 } |
6406
|
685 |
6705
|
686 return m; |
|
687 } |
6432
|
688 |
6874
|
689 graphics_handle::graphics_handle (const octave_value& a) |
|
690 : val (octave_NaN) |
|
691 { |
|
692 if (a.is_empty ()) |
|
693 /* do nothing */; |
|
694 else |
|
695 { |
|
696 double tval = a.double_value (); |
|
697 |
|
698 if (! error_state) |
|
699 val = tval; |
|
700 else |
|
701 error ("invalid graphics handle"); |
|
702 } |
|
703 } |
|
704 |
6705
|
705 void |
|
706 graphics_object::set (const octave_value_list& args) |
|
707 { |
|
708 int nargin = args.length (); |
6406
|
709 |
6705
|
710 if (nargin == 0) |
|
711 rep->defaults (); |
|
712 else if (nargin % 2 == 0) |
|
713 { |
|
714 for (int i = 0; i < nargin; i += 2) |
|
715 { |
7189
|
716 caseless_str name = args(i).string_value (); |
6406
|
717 |
6705
|
718 if (! error_state) |
|
719 { |
|
720 octave_value val = args(i+1); |
6406
|
721 |
6705
|
722 if (val.is_string ()) |
|
723 { |
7189
|
724 caseless_str tval = val.string_value (); |
6406
|
725 |
6705
|
726 if (tval.compare ("default")) |
|
727 val = get_default (name); |
|
728 else if (tval.compare ("factory")) |
|
729 val = get_factory_default (name); |
|
730 } |
6406
|
731 |
6705
|
732 if (error_state) |
|
733 break; |
6406
|
734 |
6705
|
735 rep->set (name, val); |
|
736 } |
|
737 else |
|
738 error ("set: expecting argument %d to be a property name", i); |
|
739 } |
|
740 } |
|
741 else |
|
742 error ("set: invalid number of arguments"); |
|
743 } |
6406
|
744 |
|
745 |
6705
|
746 graphics_handle |
|
747 gh_manager::get_handle (const std::string& go_name) |
|
748 { |
|
749 graphics_handle retval; |
6406
|
750 |
6705
|
751 if (go_name == "figure") |
|
752 { |
|
753 // We always want the lowest unused figure number. |
6406
|
754 |
6705
|
755 retval = 1; |
6425
|
756 |
6705
|
757 while (handle_map.find (retval) != handle_map.end ()) |
|
758 retval++; |
|
759 } |
|
760 else |
|
761 { |
|
762 free_list_iterator p = handle_free_list.begin (); |
6406
|
763 |
6705
|
764 if (p != handle_free_list.end ()) |
|
765 { |
|
766 retval = *p; |
|
767 handle_free_list.erase (p); |
|
768 } |
|
769 else |
7286
|
770 { |
|
771 static double maxrand = RAND_MAX + 2.0; |
|
772 |
|
773 retval = graphics_handle (next_handle); |
|
774 |
7304
|
775 next_handle = ceil (next_handle) - 1.0 - (rand () + 1.0) / maxrand; |
7286
|
776 } |
6705
|
777 } |
6406
|
778 |
6705
|
779 return retval; |
|
780 } |
6406
|
781 |
6705
|
782 void |
|
783 gh_manager::do_free (const graphics_handle& h) |
|
784 { |
7056
|
785 if (h.ok ()) |
6705
|
786 { |
6874
|
787 if (h.value () != 0) |
6705
|
788 { |
6874
|
789 iterator p = handle_map.find (h); |
|
790 |
|
791 if (p != handle_map.end ()) |
|
792 { |
7403
|
793 p->second.get_properties ().set_beingdeleted (true); |
|
794 p->second.get_properties ().execute_deletefcn (); |
7367
|
795 |
6874
|
796 handle_map.erase (p); |
|
797 |
|
798 if (h.value () < 0) |
|
799 handle_free_list.insert (h); |
|
800 } |
|
801 else |
|
802 error ("graphics_handle::free: invalid object %g", h.value ()); |
6705
|
803 } |
|
804 else |
6874
|
805 error ("graphics_handle::free: can't delete root figure"); |
6705
|
806 } |
|
807 } |
6406
|
808 |
|
809 gh_manager *gh_manager::instance = 0; |
|
810 |
|
811 static void |
7189
|
812 xset (const graphics_handle& h, const caseless_str& name, |
6406
|
813 const octave_value& val) |
|
814 { |
|
815 graphics_object obj = gh_manager::get_object (h); |
|
816 obj.set (name, val); |
|
817 } |
|
818 |
|
819 static void |
|
820 xset (const graphics_handle& h, const octave_value_list& args) |
|
821 { |
|
822 if (args.length () > 0) |
|
823 { |
|
824 graphics_object obj = gh_manager::get_object (h); |
|
825 obj.set (args); |
|
826 } |
|
827 } |
|
828 |
|
829 |
|
830 static octave_value |
7189
|
831 xget (const graphics_handle& h, const caseless_str& name) |
6406
|
832 { |
|
833 graphics_object obj = gh_manager::get_object (h); |
|
834 return obj.get (name); |
|
835 } |
|
836 |
|
837 static graphics_handle |
|
838 reparent (const octave_value& ov, const std::string& who, |
|
839 const std::string& property, const graphics_handle& new_parent, |
|
840 bool adopt = true) |
|
841 { |
|
842 graphics_handle h = octave_NaN; |
|
843 |
|
844 double val = ov.double_value (); |
|
845 |
|
846 if (! error_state) |
|
847 { |
|
848 h = gh_manager::lookup (val); |
|
849 |
7056
|
850 if (h.ok ()) |
6406
|
851 { |
|
852 graphics_object obj = gh_manager::get_object (h); |
|
853 |
|
854 graphics_handle parent_h = obj.get_parent (); |
|
855 |
|
856 graphics_object parent_obj = gh_manager::get_object (parent_h); |
|
857 |
|
858 parent_obj.remove_child (h); |
|
859 |
|
860 if (adopt) |
6874
|
861 obj.set ("parent", new_parent.value ()); |
6406
|
862 else |
|
863 obj.reparent (new_parent); |
|
864 } |
|
865 else |
|
866 error ("%s: invalid graphics handle (= %g) for %s", |
|
867 who.c_str (), val, property.c_str ()); |
|
868 } |
|
869 else |
|
870 error ("%s: expecting %s to be a graphics handle", |
|
871 who.c_str (), property.c_str ()); |
|
872 |
|
873 return h; |
|
874 } |
|
875 |
|
876 // This function is NOT equivalent to the scripting language function gcf. |
|
877 graphics_handle |
|
878 gcf (void) |
|
879 { |
|
880 octave_value val = xget (0, "currentfigure"); |
|
881 |
|
882 return val.is_empty () ? octave_NaN : val.double_value (); |
|
883 } |
|
884 |
|
885 // This function is NOT equivalent to the scripting language function gca. |
|
886 graphics_handle |
|
887 gca (void) |
|
888 { |
|
889 octave_value val = xget (gcf (), "currentaxes"); |
|
890 |
|
891 return val.is_empty () ? octave_NaN : val.double_value (); |
|
892 } |
|
893 |
|
894 static void |
|
895 adopt (const graphics_handle& p, const graphics_handle& h) |
|
896 { |
|
897 graphics_object parent_obj = gh_manager::get_object (p); |
|
898 |
|
899 parent_obj.adopt (h); |
|
900 } |
|
901 |
|
902 static bool |
7056
|
903 is_handle (const graphics_handle& h) |
|
904 { |
|
905 return h.ok (); |
|
906 } |
|
907 |
|
908 static bool |
6406
|
909 is_handle (double val) |
|
910 { |
6874
|
911 graphics_handle h = gh_manager::lookup (val); |
|
912 |
|
913 return h.ok (); |
6406
|
914 } |
|
915 |
|
916 static bool |
|
917 is_handle (const octave_value& val) |
|
918 { |
7142
|
919 return val.is_real_scalar () && is_handle (val.double_value ()); |
6406
|
920 } |
|
921 |
|
922 static bool |
|
923 is_figure (double val) |
|
924 { |
|
925 graphics_object obj = gh_manager::get_object (val); |
|
926 |
|
927 return obj && obj.isa ("figure"); |
|
928 } |
|
929 |
7370
|
930 static void |
|
931 xcreatefcn (const graphics_handle& h) |
|
932 { |
|
933 graphics_object obj = gh_manager::get_object (h); |
|
934 obj.get_properties ().execute_createfcn (); |
|
935 } |
|
936 |
6406
|
937 // --------------------------------------------------------------------- |
|
938 |
|
939 static int |
|
940 compare (const void *a_arg, const void *b_arg) |
|
941 { |
|
942 double a = *(static_cast<const double *> (a_arg)); |
|
943 double b = *(static_cast<const double *> (b_arg)); |
|
944 |
|
945 return a > b ? 1 : (a < b) ? -1 : 0; |
|
946 } |
|
947 |
|
948 static Matrix |
|
949 maybe_set_children (const Matrix& kids, const octave_value& val) |
|
950 { |
|
951 const Matrix new_kids = val.matrix_value (); |
|
952 |
|
953 bool ok = true; |
|
954 |
|
955 if (! error_state) |
|
956 { |
|
957 if (kids.numel () == new_kids.numel ()) |
|
958 { |
|
959 Matrix t1 = kids; |
|
960 Matrix t2 = new_kids; |
|
961 |
|
962 t1.qsort (compare); |
|
963 t2.qsort (compare); |
|
964 |
|
965 if (t1 != t2) |
|
966 ok = false; |
|
967 } else |
|
968 ok = false; |
|
969 |
|
970 if (! ok) |
|
971 error ("set: new children must be a permutation of existing children"); |
|
972 } |
|
973 else |
|
974 { |
|
975 ok = false; |
|
976 error ("set: expecting children to be array of graphics handles"); |
|
977 } |
|
978 |
|
979 return ok ? new_kids : kids; |
|
980 } |
|
981 |
6705
|
982 void |
|
983 base_properties::set_from_list (base_graphics_object& obj, |
|
984 property_list& defaults) |
6406
|
985 { |
6705
|
986 std::string go_name = graphics_object_name (); |
6406
|
987 |
6705
|
988 property_list::plist_map_const_iterator p = defaults.find (go_name); |
6406
|
989 |
6705
|
990 if (p != defaults.end ()) |
|
991 { |
|
992 const property_list::pval_map_type pval_map = p->second; |
6406
|
993 |
6705
|
994 for (property_list::pval_map_const_iterator q = pval_map.begin (); |
|
995 q != pval_map.end (); |
|
996 q++) |
|
997 { |
|
998 std::string pname = q->first; |
6406
|
999 |
6705
|
1000 obj.set (pname, q->second); |
6406
|
1001 |
6705
|
1002 if (error_state) |
|
1003 { |
|
1004 error ("error setting default property %s", pname.c_str ()); |
|
1005 break; |
|
1006 } |
|
1007 } |
|
1008 } |
|
1009 } |
6406
|
1010 |
7363
|
1011 octave_value |
|
1012 base_properties::get (const caseless_str& name) const |
|
1013 { |
|
1014 octave_value retval; |
|
1015 |
|
1016 if (name.compare ("tag")) |
|
1017 retval = get_tag (); |
|
1018 else if (name.compare ("type")) |
|
1019 retval = get_type (); |
|
1020 else if (name.compare ("__modified__")) |
|
1021 retval = is_modified (); |
|
1022 else if (name.compare ("parent")) |
|
1023 retval = get_parent ().as_octave_value (); |
|
1024 else if (name.compare ("children")) |
|
1025 retval = children; |
7366
|
1026 else if (name.compare ("busyaction")) |
|
1027 retval = get_busyaction (); |
|
1028 else if (name.compare ("buttondownfcn")) |
|
1029 retval = get_buttondownfcn (); |
|
1030 else if (name.compare ("clipping")) |
|
1031 retval = get_clipping (); |
|
1032 else if (name.compare ("createfcn")) |
|
1033 retval = get_createfcn (); |
|
1034 else if (name.compare ("deletefcn")) |
|
1035 retval = get_deletefcn (); |
|
1036 else if (name.compare ("handlevisibility")) |
|
1037 retval = get_handlevisibility (); |
|
1038 else if (name.compare ("hittest")) |
|
1039 retval = get_hittest (); |
|
1040 else if (name.compare ("interruptible")) |
|
1041 retval = get_interruptible (); |
|
1042 else if (name.compare ("selected")) |
|
1043 retval = get_selected (); |
|
1044 else if (name.compare ("selectionhighlight")) |
|
1045 retval = get_selectionhighlight (); |
|
1046 else if (name.compare ("uicontextmenu")) |
|
1047 retval = get_uicontextmenu (); |
|
1048 else if (name.compare ("userdata")) |
|
1049 retval = get_userdata (); |
|
1050 else if (name.compare ("visible")) |
|
1051 retval = get_visible (); |
7403
|
1052 else if (name.compare ("beingdeleted")) |
|
1053 retval = get_beingdeleted (); |
7363
|
1054 else |
|
1055 { |
|
1056 std::map<caseless_str, property>::const_iterator it = all_props.find (name); |
|
1057 |
|
1058 if (it != all_props.end ()) |
|
1059 retval = it->second.get (); |
|
1060 else |
|
1061 error ("get: unknown property \"%s\"", name.c_str ()); |
|
1062 } |
|
1063 |
|
1064 return retval; |
|
1065 } |
|
1066 |
|
1067 octave_value |
7379
|
1068 base_properties::get (bool all) const |
7363
|
1069 { |
|
1070 Octave_map m; |
|
1071 |
|
1072 for (std::map<caseless_str, property>::const_iterator it = all_props.begin (); |
|
1073 it != all_props.end (); ++it) |
7379
|
1074 if (all || ! it->second.is_hidden ()) |
|
1075 m.assign (it->second.get_name (), it->second.get ()); |
7363
|
1076 |
|
1077 m.assign ("tag", get_tag ()); |
|
1078 m.assign ("type", get_type ()); |
7379
|
1079 if (all) |
|
1080 m.assign ("__modified__", is_modified ()); |
7363
|
1081 m.assign ("parent", get_parent ().as_octave_value ()); |
|
1082 m.assign ("children", children); |
7366
|
1083 m.assign ("busyaction", get_busyaction ()); |
|
1084 m.assign ("buttondownfcn", get_buttondownfcn ()); |
|
1085 m.assign ("clipping", get_clipping ()); |
|
1086 m.assign ("createfcn", get_createfcn ()); |
|
1087 m.assign ("deletefcn", get_deletefcn ()); |
|
1088 m.assign ("handlevisibility", get_handlevisibility ()); |
|
1089 m.assign ("hittest", get_hittest ()); |
|
1090 m.assign ("interruptible", get_interruptible ()); |
|
1091 m.assign ("selected", get_selected ()); |
|
1092 m.assign ("selectionhighlight", get_selectionhighlight ()); |
|
1093 m.assign ("uicontextmenu", get_uicontextmenu ()); |
|
1094 m.assign ("userdata", get_userdata ()); |
|
1095 m.assign ("visible", get_visible ()); |
7403
|
1096 m.assign ("beingdeleted", get_beingdeleted ()); |
7363
|
1097 |
|
1098 return m; |
|
1099 } |
|
1100 |
|
1101 void |
|
1102 base_properties::set (const caseless_str& name, const octave_value& val) |
|
1103 { |
|
1104 if (name.compare ("tag")) |
|
1105 set_tag (val); |
|
1106 else if (name.compare ("__modified__")) |
|
1107 __modified__ = val; |
|
1108 else if (name.compare ("parent")) |
|
1109 set_parent (val); |
|
1110 else if (name.compare ("children")) |
|
1111 maybe_set_children (children, val); |
7366
|
1112 else if (name.compare ("busyaction")) |
|
1113 set_busyaction (val); |
|
1114 else if (name.compare ("buttondownfcn")) |
|
1115 set_buttondownfcn (val); |
|
1116 else if (name.compare ("clipping")) |
|
1117 set_clipping (val); |
|
1118 else if (name.compare ("createfcn")) |
|
1119 set_createfcn (val); |
|
1120 else if (name.compare ("deletefcn")) |
|
1121 set_deletefcn (val); |
|
1122 else if (name.compare ("handlevisibility")) |
|
1123 set_handlevisibility (val); |
|
1124 else if (name.compare ("hittest")) |
|
1125 set_hittest (val); |
|
1126 else if (name.compare ("interruptible")) |
|
1127 set_interruptible (val); |
|
1128 else if (name.compare ("selected")) |
|
1129 set_selected (val); |
|
1130 else if (name.compare ("selectionhighlight")) |
|
1131 set_selectionhighlight (val); |
|
1132 else if (name.compare ("uicontextmenu")) |
|
1133 set_uicontextmenu (val); |
|
1134 else if (name.compare ("userdata")) |
|
1135 set_userdata (val); |
|
1136 else if (name.compare ("visible")) |
|
1137 set_visible (val); |
7363
|
1138 else |
|
1139 { |
|
1140 std::map<caseless_str, property>::iterator it = all_props.find (name); |
|
1141 |
|
1142 if (it != all_props.end ()) |
|
1143 it->second.set (val); |
|
1144 else |
|
1145 error ("set: unknown property \"%s\"", name.c_str ()); |
|
1146 } |
|
1147 |
|
1148 if (! error_state && ! name.compare ("__modified__")) |
|
1149 mark_modified (); |
|
1150 } |
|
1151 |
|
1152 property |
|
1153 base_properties::get_property (const caseless_str& name) const |
|
1154 { |
|
1155 std::map<caseless_str, property>::const_iterator it = all_props.find (name); |
|
1156 |
|
1157 if (it == all_props.end ()) |
|
1158 return property (); |
|
1159 else |
|
1160 return it->second; |
|
1161 } |
|
1162 |
6705
|
1163 void |
|
1164 base_properties::remove_child (const graphics_handle& h) |
6406
|
1165 { |
6705
|
1166 octave_idx_type k = -1; |
|
1167 octave_idx_type n = children.numel (); |
|
1168 for (octave_idx_type i = 0; i < n; i++) |
6406
|
1169 { |
6874
|
1170 if (h.value () == children(i)) |
6406
|
1171 { |
6705
|
1172 k = i; |
|
1173 break; |
6406
|
1174 } |
|
1175 } |
|
1176 |
6705
|
1177 if (k >= 0) |
6406
|
1178 { |
6705
|
1179 Matrix new_kids (1, n-1); |
|
1180 octave_idx_type j = 0; |
|
1181 for (octave_idx_type i = 0; i < n; i++) |
|
1182 { |
|
1183 if (i != k) |
|
1184 new_kids(j++) = children(i); |
|
1185 } |
|
1186 children = new_kids; |
7378
|
1187 mark_modified (); |
6406
|
1188 } |
6705
|
1189 } |
6406
|
1190 |
6836
|
1191 void |
|
1192 base_properties::set_parent (const octave_value& val) |
6705
|
1193 { |
|
1194 double tmp = val.double_value (); |
6406
|
1195 |
6705
|
1196 graphics_handle new_parent = octave_NaN; |
6406
|
1197 |
6705
|
1198 if (! error_state) |
|
1199 { |
|
1200 new_parent = gh_manager::lookup (tmp); |
6406
|
1201 |
7056
|
1202 if (new_parent.ok ()) |
6705
|
1203 { |
7363
|
1204 graphics_object parent_obj = gh_manager::get_object (get_parent ()); |
6406
|
1205 |
6705
|
1206 parent_obj.remove_child (__myhandle__); |
6406
|
1207 |
7363
|
1208 parent = new_parent.as_octave_value (); |
6406
|
1209 |
7363
|
1210 ::adopt (parent.handle_value (), __myhandle__); |
6705
|
1211 } |
|
1212 else |
|
1213 error ("set: invalid graphics handle (= %g) for parent", tmp); |
|
1214 } |
|
1215 else |
|
1216 error ("set: expecting parent to be a graphics handle"); |
|
1217 } |
6432
|
1218 |
6705
|
1219 void |
6836
|
1220 base_properties::mark_modified (void) |
|
1221 { |
7363
|
1222 __modified__ = "on"; |
|
1223 graphics_object parent_obj = gh_manager::get_object (get_parent ()); |
7379
|
1224 if (parent_obj) |
|
1225 parent_obj.mark_modified (); |
6836
|
1226 } |
|
1227 |
|
1228 void |
|
1229 base_properties::override_defaults (base_graphics_object& obj) |
|
1230 { |
7363
|
1231 graphics_object parent_obj = gh_manager::get_object (get_parent ()); |
6836
|
1232 parent_obj.override_defaults (obj); |
|
1233 } |
|
1234 |
|
1235 void |
7214
|
1236 base_properties::update_axis_limits (const std::string& axis_type) const |
|
1237 { |
7363
|
1238 graphics_handle h = (get_type () == "axes") ? __myhandle__ : get_parent (); |
7214
|
1239 |
|
1240 graphics_object obj = gh_manager::get_object (h); |
|
1241 |
|
1242 if (obj.isa ("axes")) |
|
1243 obj.update_axis_limits (axis_type); |
|
1244 } |
|
1245 |
|
1246 void |
6836
|
1247 base_properties::delete_children (void) |
|
1248 { |
|
1249 octave_idx_type n = children.numel (); |
|
1250 |
|
1251 for (octave_idx_type i = 0; i < n; i++) |
|
1252 gh_manager::free (children(i)); |
|
1253 } |
|
1254 |
7419
|
1255 graphics_backend |
|
1256 base_properties::get_backend (void) const |
|
1257 { |
|
1258 graphics_object go = gh_manager::get_object (get_parent ()); |
|
1259 |
|
1260 if (go) |
|
1261 return go.get_backend (); |
|
1262 else |
|
1263 return graphics_backend (); |
|
1264 } |
|
1265 |
7363
|
1266 // --------------------------------------------------------------------- |
|
1267 |
7408
|
1268 class gnuplot_backend : public base_graphics_backend |
|
1269 { |
|
1270 public: |
|
1271 gnuplot_backend (void) |
|
1272 : base_graphics_backend ("gnuplot") { } |
|
1273 |
|
1274 ~gnuplot_backend (void) { } |
|
1275 |
|
1276 bool is_valid (void) const { return true; } |
|
1277 |
|
1278 void close_figure (const octave_value& pstream) const |
|
1279 { |
|
1280 if (! pstream.is_empty()) |
|
1281 { |
|
1282 octave_value_list args; |
|
1283 args(1) = "\nquit;\n"; |
|
1284 args(0) = pstream; |
|
1285 feval ("fputs", args); |
|
1286 args.resize (1); |
|
1287 feval ("fflush", args); |
|
1288 feval ("pclose", args); |
|
1289 } |
|
1290 } |
|
1291 |
|
1292 void redraw_figure (const graphics_handle& fh) const |
|
1293 { |
|
1294 octave_value_list args; |
|
1295 args(0) = fh.as_octave_value (); |
|
1296 feval ("gnuplot_drawnow", args); |
|
1297 } |
|
1298 |
|
1299 void print_figure (const graphics_handle& fh, const std::string& term, |
|
1300 const std::string& file, bool mono, |
|
1301 const std::string& debug_file) const |
|
1302 { |
|
1303 octave_value_list args; |
|
1304 if (! debug_file.empty ()) |
|
1305 args(4) = debug_file; |
7409
|
1306 args(3) = mono; |
|
1307 args(2) = file; |
|
1308 args(1) = term; |
|
1309 args(0) = fh.as_octave_value (); |
7408
|
1310 feval ("gnuplot_drawnow", args); |
|
1311 } |
|
1312 |
7409
|
1313 Matrix get_canvas_size (const graphics_handle&) const |
7427
|
1314 { |
|
1315 Matrix sz (1, 2, 0.0); |
|
1316 return sz; |
|
1317 } |
|
1318 |
|
1319 double get_screen_resolution (void) const |
|
1320 { return 72.0; } |
7408
|
1321 }; |
|
1322 |
|
1323 graphics_backend |
|
1324 graphics_backend::default_backend (void) |
|
1325 { |
|
1326 if (available_backends.size () == 0) |
|
1327 register_backend (new gnuplot_backend ()); |
|
1328 |
|
1329 return available_backends["gnuplot"]; |
|
1330 } |
|
1331 |
|
1332 std::map<std::string, graphics_backend> graphics_backend::available_backends; |
|
1333 |
|
1334 // --------------------------------------------------------------------- |
|
1335 |
7363
|
1336 #include "graphics-props.cc" |
|
1337 |
|
1338 // --------------------------------------------------------------------- |
|
1339 |
6836
|
1340 void |
7363
|
1341 root_figure::properties::set_currentfigure (const octave_value& v) |
6874
|
1342 { |
7378
|
1343 graphics_handle val (v); |
7363
|
1344 |
6874
|
1345 if (error_state) |
|
1346 return; |
|
1347 |
7059
|
1348 if (xisnan (val.value ()) || is_handle (val)) |
6874
|
1349 { |
|
1350 currentfigure = val; |
|
1351 |
7363
|
1352 gh_manager::push_figure (val); |
6874
|
1353 } |
|
1354 else |
|
1355 gripe_set_invalid ("currentfigure"); |
|
1356 } |
|
1357 |
6406
|
1358 property_list |
|
1359 root_figure::factory_properties = root_figure::init_factory_properties (); |
|
1360 |
|
1361 // --------------------------------------------------------------------- |
|
1362 |
7363
|
1363 void |
|
1364 figure::properties::set_currentaxes (const octave_value& v) |
|
1365 { |
7378
|
1366 graphics_handle val (v); |
6705
|
1367 |
6874
|
1368 if (error_state) |
|
1369 return; |
|
1370 |
7070
|
1371 if (xisnan (val.value ()) || is_handle (val)) |
6874
|
1372 currentaxes = val; |
|
1373 else |
|
1374 gripe_set_invalid ("currentaxes"); |
|
1375 } |
|
1376 |
|
1377 void |
|
1378 figure::properties::set_visible (const octave_value& val) |
|
1379 { |
|
1380 std::string s = val.string_value (); |
|
1381 |
|
1382 if (! error_state) |
|
1383 { |
|
1384 if (s == "on") |
|
1385 xset (0, "currentfigure", __myhandle__.value ()); |
|
1386 |
|
1387 visible = val; |
|
1388 } |
|
1389 } |
|
1390 |
|
1391 void |
7408
|
1392 figure::properties::close (bool pop) |
6705
|
1393 { |
7408
|
1394 if (backend) |
|
1395 backend.close_figure (get___plot_stream__ ()); |
|
1396 |
|
1397 if (pop) |
6406
|
1398 { |
7408
|
1399 gh_manager::pop_figure (__myhandle__); |
|
1400 |
|
1401 graphics_handle cf = gh_manager::current_figure (); |
|
1402 |
|
1403 xset (0, "currentfigure", cf.value ()); |
6406
|
1404 } |
6705
|
1405 } |
6432
|
1406 |
6836
|
1407 octave_value |
7189
|
1408 figure::get_default (const caseless_str& name) const |
6836
|
1409 { |
|
1410 octave_value retval = default_properties.lookup (name); |
|
1411 |
|
1412 if (retval.is_undefined ()) |
|
1413 { |
|
1414 graphics_handle parent = get_parent (); |
|
1415 graphics_object parent_obj = gh_manager::get_object (parent); |
|
1416 |
|
1417 retval = parent_obj.get_default (name); |
|
1418 } |
|
1419 |
|
1420 return retval; |
|
1421 } |
|
1422 |
6406
|
1423 // --------------------------------------------------------------------- |
|
1424 |
7363
|
1425 void |
|
1426 axes::properties::set_title (const octave_value& v) |
6962
|
1427 { |
7363
|
1428 graphics_handle val = ::reparent (v, "set", "title", __myhandle__, false); |
6962
|
1429 |
6874
|
1430 if (! error_state) |
|
1431 { |
7363
|
1432 gh_manager::free (title.handle_value ()); |
6874
|
1433 title = val; |
|
1434 } |
|
1435 } |
|
1436 |
|
1437 void |
7363
|
1438 axes::properties::set_xlabel (const octave_value& v) |
6874
|
1439 { |
7363
|
1440 graphics_handle val = ::reparent (v, "set", "xlabel", __myhandle__, false); |
6874
|
1441 |
|
1442 if (! error_state) |
|
1443 { |
7363
|
1444 gh_manager::free (xlabel.handle_value ()); |
6874
|
1445 xlabel = val; |
|
1446 } |
|
1447 } |
|
1448 |
|
1449 void |
7363
|
1450 axes::properties::set_ylabel (const octave_value& v) |
6874
|
1451 { |
7363
|
1452 graphics_handle val = ::reparent (v, "set", "ylabel", __myhandle__, false); |
6874
|
1453 |
|
1454 if (! error_state) |
|
1455 { |
7363
|
1456 gh_manager::free (ylabel.handle_value ()); |
6874
|
1457 ylabel = val; |
|
1458 } |
|
1459 } |
|
1460 |
|
1461 void |
7363
|
1462 axes::properties::set_zlabel (const octave_value& v) |
6874
|
1463 { |
7363
|
1464 graphics_handle val = ::reparent (v, "set", "zlabel", __myhandle__, false); |
6874
|
1465 |
|
1466 if (! error_state) |
|
1467 { |
7363
|
1468 gh_manager::free (zlabel.handle_value ()); |
6874
|
1469 zlabel = val; |
|
1470 } |
|
1471 } |
|
1472 |
|
1473 void |
6844
|
1474 axes::properties::set_defaults (base_graphics_object& obj, |
6890
|
1475 const std::string& mode) |
6705
|
1476 { |
7427
|
1477 position = default_axes_position (); |
7363
|
1478 title = graphics_handle (); |
6705
|
1479 box = "on"; |
|
1480 key = "off"; |
|
1481 keybox = "off"; |
7363
|
1482 keypos = 1.0; |
6962
|
1483 colororder = default_colororder (); |
6705
|
1484 dataaspectratio = Matrix (1, 3, 1.0); |
|
1485 dataaspectratiomode = "auto"; |
7363
|
1486 layer = "bottom"; |
6705
|
1487 |
|
1488 Matrix tlim (1, 2, 0.0); |
|
1489 tlim(1) = 1; |
|
1490 xlim = tlim; |
|
1491 ylim = tlim; |
|
1492 zlim = tlim; |
6807
|
1493 |
|
1494 Matrix cl (1, 2, 0); |
|
1495 cl(1) = 1; |
|
1496 clim = cl; |
|
1497 |
7363
|
1498 xlimmode = "auto"; |
|
1499 ylimmode = "auto"; |
|
1500 zlimmode = "auto"; |
|
1501 climmode = "auto"; |
|
1502 xlabel = graphics_handle (); |
|
1503 ylabel = graphics_handle (); |
|
1504 zlabel = graphics_handle (); |
6705
|
1505 xgrid = "off"; |
|
1506 ygrid = "off"; |
|
1507 zgrid = "off"; |
|
1508 xminorgrid = "off"; |
|
1509 yminorgrid = "off"; |
|
1510 zminorgrid = "off"; |
|
1511 xtick = Matrix (); |
|
1512 ytick = Matrix (); |
|
1513 ztick = Matrix (); |
|
1514 xtickmode = "auto"; |
|
1515 ytickmode = "auto"; |
|
1516 ztickmode = "auto"; |
|
1517 xticklabel = ""; |
|
1518 yticklabel = ""; |
|
1519 zticklabel = ""; |
|
1520 xticklabelmode = "auto"; |
|
1521 yticklabelmode = "auto"; |
|
1522 zticklabelmode = "auto"; |
7364
|
1523 color = color_values (0, 0, 0); |
|
1524 xcolor = color_values ("black"); |
|
1525 ycolor = color_values ("black"); |
|
1526 zcolor = color_values ("black"); |
7363
|
1527 xscale = "linear"; |
|
1528 yscale = "linear"; |
|
1529 zscale = "linear"; |
6705
|
1530 xdir = "normal"; |
|
1531 ydir = "normal"; |
|
1532 zdir = "normal"; |
7363
|
1533 yaxislocation = "left"; |
|
1534 xaxislocation = "bottom"; |
6705
|
1535 |
7427
|
1536 // Note: camera properties will be set through update_transform |
|
1537 camerapositionmode = "auto"; |
|
1538 cameratargetmode = "auto"; |
|
1539 cameraupvectormode = "auto"; |
|
1540 cameraviewanglemode = "auto"; |
|
1541 plotboxaspectratio = Matrix (1, 3, 1.0); |
|
1542 drawmode = "normal"; |
|
1543 fontangle = "normal"; |
|
1544 fontname = "Helvetica"; |
|
1545 fontsize = 12; |
|
1546 fontunits = "points"; |
|
1547 fontweight = "normal"; |
|
1548 gridlinestyle = "-"; |
|
1549 linestyleorder = "-"; |
|
1550 linewidth = 0.5; |
|
1551 minorgridlinestyle = "-"; |
|
1552 // Note: plotboxaspectratio will be set through update_aspectratiors |
|
1553 plotboxaspectratiomode = "auto"; |
|
1554 projection = "orthographic"; |
|
1555 tickdir = "in"; |
|
1556 tickdirmode = "auto"; |
|
1557 ticklength = Matrix (1, 2, 0.1); |
|
1558 tightinset = Matrix (1, 4, 0.0); |
|
1559 |
|
1560 sx = "linear"; |
|
1561 sy = "linear"; |
|
1562 sz = "linear"; |
|
1563 |
6705
|
1564 Matrix tview (1, 2, 0.0); |
|
1565 tview(1) = 90; |
|
1566 view = tview; |
|
1567 |
6765
|
1568 visible = "on"; |
6705
|
1569 nextplot = "replace"; |
|
1570 |
|
1571 // FIXME -- this is not quite right; we should preserve |
|
1572 // "position" and "units". |
|
1573 |
|
1574 if (mode != "replace") |
|
1575 { |
6406
|
1576 Matrix touterposition (1, 4, 0.0); |
|
1577 touterposition(2) = 1; |
|
1578 touterposition(3) = 1; |
|
1579 outerposition = touterposition; |
|
1580 } |
|
1581 |
7363
|
1582 activepositionproperty = "outerposition"; |
|
1583 __colorbar__ = "none"; |
7189
|
1584 |
6705
|
1585 delete_children (); |
6406
|
1586 |
6705
|
1587 children = Matrix (); |
6432
|
1588 |
7427
|
1589 update_transform (); |
|
1590 |
6705
|
1591 override_defaults (obj); |
|
1592 } |
|
1593 |
6874
|
1594 graphics_handle |
|
1595 axes::properties::get_title (void) const |
|
1596 { |
7363
|
1597 if (! title.handle_value ().ok ()) |
6874
|
1598 title = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1599 |
7363
|
1600 return title.handle_value (); |
6874
|
1601 } |
|
1602 |
|
1603 graphics_handle |
|
1604 axes::properties::get_xlabel (void) const |
|
1605 { |
7363
|
1606 if (! xlabel.handle_value ().ok ()) |
6874
|
1607 xlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1608 |
7363
|
1609 return xlabel.handle_value (); |
6874
|
1610 } |
|
1611 |
|
1612 graphics_handle |
|
1613 axes::properties::get_ylabel (void) const |
|
1614 { |
7363
|
1615 if (! ylabel.handle_value ().ok ()) |
6874
|
1616 ylabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1617 |
7363
|
1618 return ylabel.handle_value (); |
6874
|
1619 } |
|
1620 |
|
1621 graphics_handle |
|
1622 axes::properties::get_zlabel (void) const |
|
1623 { |
7363
|
1624 if (! zlabel.handle_value ().ok ()) |
6874
|
1625 zlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1626 |
7363
|
1627 return zlabel.handle_value (); |
6705
|
1628 } |
6406
|
1629 |
6705
|
1630 void |
6844
|
1631 axes::properties::remove_child (const graphics_handle& h) |
6705
|
1632 { |
7363
|
1633 if (title.handle_value ().ok () && h == title.handle_value ()) |
6705
|
1634 title = gh_manager::make_graphics_handle ("text", __myhandle__); |
7363
|
1635 else if (xlabel.handle_value ().ok () && h == xlabel.handle_value ()) |
6705
|
1636 xlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
7363
|
1637 else if (ylabel.handle_value ().ok () && h == ylabel.handle_value ()) |
6705
|
1638 ylabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
7363
|
1639 else if (zlabel.handle_value ().ok () && h == zlabel.handle_value ()) |
6705
|
1640 zlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1641 else |
|
1642 base_properties::remove_child (h); |
|
1643 } |
6406
|
1644 |
6705
|
1645 void |
6844
|
1646 axes::properties::delete_children (void) |
6705
|
1647 { |
|
1648 base_properties::delete_children (); |
|
1649 |
7363
|
1650 gh_manager::free (title.handle_value ()); |
|
1651 gh_manager::free (xlabel.handle_value ()); |
|
1652 gh_manager::free (ylabel.handle_value ()); |
|
1653 gh_manager::free (zlabel.handle_value ()); |
6705
|
1654 } |
6406
|
1655 |
7427
|
1656 inline Matrix |
|
1657 xform_matrix (void) |
|
1658 { |
|
1659 Matrix m (4, 4, 0.0); |
|
1660 for (int i = 0; i < 4; i++) |
|
1661 m(i,i) = 1; |
|
1662 return m; |
|
1663 } |
|
1664 |
|
1665 inline ColumnVector |
|
1666 xform_vector (void) |
|
1667 { |
|
1668 ColumnVector v (4, 0.0); |
|
1669 v(3) = 1; |
|
1670 return v; |
|
1671 } |
|
1672 |
|
1673 inline ColumnVector |
|
1674 xform_vector (double x, double y, double z) |
|
1675 { |
|
1676 ColumnVector v (4, 1.0); |
|
1677 v(0) = x; v(1) = y; v(2) = z; |
|
1678 return v; |
|
1679 } |
|
1680 |
|
1681 inline ColumnVector |
|
1682 transform (const Matrix& m, double x, double y, double z) |
|
1683 { |
|
1684 return (m * xform_vector (x, y, z)); |
|
1685 } |
|
1686 |
|
1687 inline Matrix |
|
1688 xform_scale (double x, double y, double z) |
|
1689 { |
|
1690 Matrix m (4, 4, 0.0); |
|
1691 m(0,0) = x; m(1,1) = y; m(2,2) = z; m(3,3) = 1; |
|
1692 return m; |
|
1693 } |
|
1694 |
|
1695 inline Matrix |
|
1696 xform_translate (double x, double y, double z) |
|
1697 { |
|
1698 Matrix m = xform_matrix (); |
|
1699 m(0,3) = x; m(1,3) = y; m(2,3) = z; m(3,3) = 1; |
|
1700 return m; |
|
1701 } |
|
1702 |
|
1703 inline void |
|
1704 scale (Matrix& m, double x, double y, double z) |
|
1705 { |
|
1706 m = m * xform_scale (x, y, z); |
|
1707 } |
|
1708 |
|
1709 inline void |
|
1710 translate (Matrix& m, double x, double y, double z) |
|
1711 { |
|
1712 m = m * xform_translate (x, y, z); |
|
1713 } |
|
1714 |
|
1715 inline void |
|
1716 xform (ColumnVector& v, const Matrix& m) |
|
1717 { |
|
1718 v = m*v; |
|
1719 } |
|
1720 |
|
1721 inline void |
|
1722 scale (ColumnVector& v, double x, double y, double z) |
|
1723 { |
|
1724 v(0) *= x; |
|
1725 v(1) *= y; |
|
1726 v(2) *= z; |
|
1727 } |
|
1728 |
|
1729 inline void |
|
1730 translate (ColumnVector& v, double x, double y, double z) |
|
1731 { |
|
1732 v(0) += x; |
|
1733 v(1) += y; |
|
1734 v(2) += z; |
|
1735 } |
|
1736 |
|
1737 inline void |
|
1738 normalize (ColumnVector& v) |
|
1739 { |
|
1740 double fact = 1.0/sqrt(v(0)*v(0)+v(1)*v(1)+v(2)*v(2)); |
|
1741 scale (v, fact, fact, fact); |
|
1742 } |
|
1743 |
|
1744 inline double |
|
1745 dot (const ColumnVector& v1, const ColumnVector& v2) |
|
1746 { |
|
1747 return (v1(0)*v2(0)+v1(1)*v2(1)+v1(2)*v2(2)); |
|
1748 } |
|
1749 |
|
1750 inline double |
|
1751 norm (const ColumnVector& v) |
|
1752 { |
|
1753 return sqrt (dot (v, v)); |
|
1754 } |
|
1755 |
|
1756 inline ColumnVector |
|
1757 cross (const ColumnVector& v1, const ColumnVector& v2) |
|
1758 { |
|
1759 ColumnVector r = xform_vector (); |
|
1760 r(0) = v1(1)*v2(2)-v1(2)*v2(1); |
|
1761 r(1) = v1(2)*v2(0)-v1(0)*v2(2); |
|
1762 r(2) = v1(0)*v2(1)-v1(1)*v2(0); |
|
1763 return r; |
|
1764 } |
|
1765 |
|
1766 inline Matrix |
|
1767 unit_cube (void) |
|
1768 { |
|
1769 static double data[32] = { |
|
1770 0,0,0,1, |
|
1771 1,0,0,1, |
|
1772 0,1,0,1, |
|
1773 0,0,1,1, |
|
1774 1,1,0,1, |
|
1775 1,0,1,1, |
|
1776 0,1,1,1, |
|
1777 1,1,1,1}; |
|
1778 Matrix m (4, 8); |
|
1779 memcpy (m.fortran_vec (), data, sizeof(double)*32); |
|
1780 return m; |
|
1781 } |
|
1782 |
|
1783 inline ColumnVector |
|
1784 cam2xform (const Array<double>& m) |
|
1785 { |
|
1786 ColumnVector retval (4, 1.0); |
|
1787 memcpy (retval.fortran_vec (), m.fortran_vec (), sizeof(double)*3); |
|
1788 return retval; |
|
1789 } |
|
1790 |
|
1791 inline RowVector |
|
1792 xform2cam (const ColumnVector& v) |
|
1793 { |
|
1794 return v.extract_n (0, 3).transpose (); |
|
1795 } |
|
1796 |
|
1797 void |
|
1798 axes::properties::update_camera (void) |
|
1799 { |
|
1800 double xd = (xdir_is ("normal") ? 1 : -1); |
|
1801 double yd = (ydir_is ("normal") ? 1 : -1); |
|
1802 double zd = (zdir_is ("normal") ? 1 : -1); |
|
1803 |
|
1804 Matrix xlim = sx.scale (get_xlim ().matrix_value ()); |
|
1805 Matrix ylim = sy.scale (get_ylim ().matrix_value ()); |
|
1806 Matrix zlim = sz.scale (get_zlim ().matrix_value ()); |
|
1807 |
|
1808 double xo = xlim(xd > 0 ? 0 : 1); |
|
1809 double yo = ylim(yd > 0 ? 0 : 1); |
|
1810 double zo = zlim(zd > 0 ? 0 : 1); |
|
1811 |
|
1812 Matrix pb = get_plotboxaspectratio ().matrix_value (); |
|
1813 |
|
1814 bool autocam = (camerapositionmode_is ("auto") |
|
1815 && cameratargetmode_is ("auto") |
|
1816 && cameraupvectormode_is ("auto") |
|
1817 && cameraviewanglemode_is ("auto")); |
|
1818 bool dowarp = (autocam && dataaspectratiomode_is("auto") |
|
1819 && plotboxaspectratiomode_is ("auto")); |
|
1820 |
|
1821 ColumnVector c_eye (xform_vector ()); |
|
1822 ColumnVector c_center (xform_vector ()); |
|
1823 ColumnVector c_upv (xform_vector ()); |
|
1824 |
|
1825 if (cameratargetmode_is ("auto")) |
|
1826 { |
|
1827 c_center(0) = (xlim(0)+xlim(1))/2; |
|
1828 c_center(1) = (ylim(0)+ylim(1))/2; |
|
1829 c_center(2) = (zlim(0)+zlim(1))/2; |
|
1830 |
|
1831 cameratarget = xform2cam (c_center); |
|
1832 } |
|
1833 else |
|
1834 c_center = cam2xform (get_cameratarget ().matrix_value ()); |
|
1835 |
|
1836 if (camerapositionmode_is ("auto")) |
|
1837 { |
|
1838 Matrix view = get_view ().matrix_value (); |
|
1839 double az = view(0), el = view(1); |
|
1840 double d = 5*sqrt(pb(0)*pb(0)+pb(1)*pb(1)+pb(2)*pb(2)); |
|
1841 |
|
1842 if (el == 90 || el == -90) |
|
1843 c_eye(2) = d*signum(el); |
|
1844 else |
|
1845 { |
|
1846 az *= M_PI/180.0; |
|
1847 el *= M_PI/180.0; |
|
1848 c_eye(0) = d*cos(el)*sin(az); |
|
1849 c_eye(1) = -d*cos(el)*cos(az); |
|
1850 c_eye(2) = d*sin(el); |
|
1851 } |
|
1852 c_eye(0) = c_eye(0)*(xlim(1)-xlim(0))/(xd*pb(0))+c_center(0); |
|
1853 c_eye(1) = c_eye(1)*(ylim(1)-ylim(0))/(yd*pb(1))+c_center(1); |
|
1854 c_eye(2) = c_eye(2)*(zlim(1)-zlim(0))/(zd*pb(2))+c_center(2); |
|
1855 |
|
1856 cameraposition = xform2cam (c_eye); |
|
1857 } |
|
1858 else |
|
1859 c_eye = cam2xform (get_cameraposition ().matrix_value ()); |
|
1860 |
|
1861 if (cameraupvectormode_is ("auto")) |
|
1862 { |
|
1863 Matrix view = get_view ().matrix_value (); |
|
1864 double az = view(0), el = view(1); |
|
1865 |
|
1866 if (el == 90 || el == -90) |
|
1867 { |
|
1868 c_upv(0) = -sin(az*M_PI/180.0)*(xlim(1)-xlim(0))/pb(0); |
|
1869 c_upv(1) = cos(az*M_PI/180.0)*(ylim(1)-ylim(0))/pb(1); |
|
1870 } |
|
1871 else |
|
1872 c_upv(2) = 1; |
|
1873 |
|
1874 cameraupvector = xform2cam (c_upv); |
|
1875 } |
|
1876 else |
|
1877 c_upv = cam2xform (get_cameraupvector ().matrix_value ()); |
|
1878 |
|
1879 Matrix x_view = xform_matrix (); |
|
1880 Matrix x_projection = xform_matrix (); |
|
1881 Matrix x_viewport = xform_matrix (); |
|
1882 Matrix x_normrender = xform_matrix (); |
|
1883 Matrix x_pre = xform_matrix (); |
|
1884 |
|
1885 x_render = xform_matrix (); |
|
1886 x_render_inv = xform_matrix (); |
|
1887 |
|
1888 scale (x_pre, pb(0), pb(1), pb(2)); |
|
1889 translate (x_pre, -0.5, -0.5, -0.5); |
|
1890 scale (x_pre, xd/(xlim(1)-xlim(0)), yd/(ylim(1)-ylim(0)), |
|
1891 zd/(zlim(1)-zlim(0))); |
|
1892 translate (x_pre, -xo, -yo, -zo); |
|
1893 |
|
1894 xform (c_eye, x_pre); |
|
1895 xform (c_center, x_pre); |
|
1896 scale (c_upv, pb(0)/(xlim(1)-xlim(0)), pb(1)/(ylim(1)-ylim(0)), |
|
1897 pb(2)/(zlim(1)-zlim(0))); |
|
1898 translate (c_center, -c_eye(0), -c_eye(1), -c_eye(2)); |
|
1899 |
|
1900 ColumnVector F (c_center), f (F), UP (c_upv); |
|
1901 normalize (f); |
|
1902 normalize (UP); |
|
1903 |
7440
|
1904 if (std::abs (dot (f, UP)) > 1e-15) |
7427
|
1905 { |
|
1906 double fa = 1/sqrt(1-f(2)*f(2)); |
|
1907 scale (UP, fa, fa, fa); |
|
1908 } |
|
1909 |
|
1910 ColumnVector s = cross (f, UP); |
|
1911 ColumnVector u = cross (s, f); |
|
1912 |
|
1913 scale (x_view, 1, 1, -1); |
|
1914 Matrix l = xform_matrix (); |
|
1915 l(0,0) = s(0); l(0,1) = s(1); l(0,2) = s(2); |
|
1916 l(1,0) = u(0); l(1,1) = u(1); l(1,2) = u(2); |
|
1917 l(2,0) = -f(0); l(2,1) = -f(1); l(2,2) = -f(2); |
|
1918 x_view = x_view * l; |
|
1919 translate (x_view, -c_eye(0), -c_eye(1), -c_eye(2)); |
|
1920 scale (x_view, pb(0), pb(1), pb(2)); |
|
1921 translate (x_view, -0.5, -0.5, -0.5); |
|
1922 |
|
1923 Matrix x_cube = x_view * unit_cube (); |
|
1924 ColumnVector cmin = x_cube.row_min (), cmax = x_cube.row_max (); |
|
1925 double xM = cmax(0)-cmin(0); |
|
1926 double yM = cmax(1)-cmin(1); |
|
1927 |
|
1928 Matrix bb = get_boundingbox (); |
|
1929 Matrix cs = get_backend ().get_canvas_size (__myhandle__); |
|
1930 double fh = cs(1); |
|
1931 |
|
1932 double v_angle; |
|
1933 |
|
1934 if (cameraviewanglemode_is ("auto")) |
|
1935 { |
|
1936 double af; |
|
1937 |
|
1938 // FIXME: Was this really needed? When compared to Matlab, it |
|
1939 // does not seem to be required. Need investigation with concrete |
|
1940 // backend to see results visually. |
|
1941 if (false && dowarp) |
|
1942 af = 1.0 / (xM > yM ? xM : yM); |
|
1943 else |
|
1944 { |
|
1945 if ((bb(2)/bb(3)) > (xM/yM)) |
|
1946 af = 1.0 / yM; |
|
1947 else |
|
1948 af = 1.0 / xM; |
|
1949 } |
|
1950 v_angle = 2 * (180.0 / M_PI) * atan (1 / (2 * af * norm (F))); |
|
1951 |
|
1952 cameraviewangle = v_angle; |
|
1953 } |
|
1954 else |
|
1955 v_angle = get_cameraviewangle (); |
|
1956 |
|
1957 double pf = 1 / (2 * tan ((v_angle / 2) * M_PI / 180.0) * norm (F)); |
|
1958 scale (x_projection, pf, pf, 1); |
|
1959 |
|
1960 if (dowarp) |
|
1961 { |
|
1962 xM *= pf; |
|
1963 yM *= pf; |
|
1964 translate (x_viewport, bb(0)+bb(2)/2, fh-(bb(1)+bb(3)/2)+1, 0); |
|
1965 scale (x_viewport, bb(2)/xM, -bb(3)/yM, 1); |
|
1966 } |
|
1967 else |
|
1968 { |
|
1969 double pix = 1; |
|
1970 if (autocam) |
|
1971 { |
|
1972 if ((bb(2)/bb(3)) > (xM/yM)) |
|
1973 pix = bb(3); |
|
1974 else |
|
1975 pix = bb(2); |
|
1976 } |
|
1977 else |
|
1978 pix = (bb(2) < bb(3) ? bb(2) : bb(3)); |
|
1979 translate (x_viewport, bb(0)+bb(2)/2, fh-(bb(1)+bb(3)/2)+1, 0); |
|
1980 scale (x_viewport, pix, -pix, 1); |
|
1981 } |
|
1982 |
|
1983 x_normrender = x_viewport * x_projection * x_view; |
|
1984 |
|
1985 x_cube = x_normrender * unit_cube (); |
|
1986 cmin = x_cube.row_min (); |
|
1987 cmax = x_cube.row_max (); |
|
1988 x_zlim.resize (1, 2); |
|
1989 x_zlim(0) = cmin(2); |
|
1990 x_zlim(1) = cmax(2); |
|
1991 |
|
1992 x_render = x_normrender; |
|
1993 scale (x_render, xd/(xlim(1)-xlim(0)), yd/(ylim(1)-ylim(0)), |
|
1994 zd/(zlim(1)-zlim(0))); |
|
1995 translate (x_render, -xo, -yo, -zo); |
|
1996 |
|
1997 x_viewtransform = x_view; |
|
1998 x_projectiontransform = x_projection; |
|
1999 x_viewporttransform = x_viewport; |
|
2000 x_normrendertransform = x_normrender; |
|
2001 x_rendertransform = x_render; |
|
2002 |
|
2003 x_render_inv = x_render.inverse (); |
|
2004 |
|
2005 // Note: these matrices are a slight modified version of the regular |
|
2006 // matrices, more suited for OpenGL rendering (x_gl_mat1 => light |
|
2007 // => x_gl_mat2) |
|
2008 x_gl_mat1 = x_view; |
|
2009 scale (x_gl_mat1, xd/(xlim(1)-xlim(0)), yd/(ylim(1)-ylim(0)), |
|
2010 zd/(zlim(1)-zlim(0))); |
|
2011 translate (x_gl_mat1, -xo, -yo, -zo); |
|
2012 x_gl_mat2 = x_viewport * x_projection; |
|
2013 } |
|
2014 |
|
2015 void |
|
2016 axes::properties::update_aspectratios (void) |
|
2017 { |
|
2018 Matrix xlim = get_xlim ().matrix_value (); |
|
2019 Matrix ylim = get_ylim ().matrix_value (); |
|
2020 Matrix zlim = get_zlim ().matrix_value (); |
|
2021 |
|
2022 double dx = (xlim(1)-xlim(0)); |
|
2023 double dy = (ylim(1)-ylim(0)); |
|
2024 double dz = (zlim(1)-zlim(0)); |
|
2025 |
|
2026 if (dataaspectratiomode_is ("auto")) |
|
2027 { |
|
2028 double dmin = xmin (xmin (dx, dy), dz); |
|
2029 Matrix da (1, 3, 0.0); |
|
2030 |
|
2031 da(0) = dx/dmin; |
|
2032 da(1) = dy/dmin; |
|
2033 da(2) = dz/dmin; |
|
2034 |
|
2035 dataaspectratio = da; |
|
2036 } |
|
2037 |
|
2038 if (plotboxaspectratiomode_is ("auto")) |
|
2039 { |
|
2040 if (dataaspectratiomode_is ("auto")) |
|
2041 plotboxaspectratio = Matrix (1, 3, 1.0); |
|
2042 else |
|
2043 { |
|
2044 Matrix da = get_dataaspectratio ().matrix_value (); |
|
2045 Matrix pba (1, 3, 0.0); |
|
2046 |
|
2047 pba(0) = dx/da(0); |
|
2048 pba(1) = dy/da(1); |
|
2049 pba(2) = dz/da(2); |
|
2050 } |
|
2051 } |
|
2052 |
|
2053 // FIXME: if plotboxaspectratiomode is "manual", limits |
|
2054 // and/or dataaspectratio might be adapted |
|
2055 } |
|
2056 |
|
2057 Matrix |
|
2058 axes::properties::get_boundingbox (void) const |
|
2059 { |
|
2060 graphics_backend b = get_backend (); |
|
2061 Matrix pos; |
|
2062 |
|
2063 pos = convert_position (get_position ().matrix_value (), get_units (), |
|
2064 "pixels", b.get_canvas_size (__myhandle__), b); |
|
2065 pos(0)--; |
|
2066 pos(1)--; |
|
2067 |
|
2068 return pos; |
|
2069 } |
|
2070 |
7435
|
2071 ColumnVector |
|
2072 graphics_xform::xform_vector (double x, double y, double z) |
|
2073 { return ::xform_vector (x, y, z); } |
|
2074 |
|
2075 Matrix |
|
2076 graphics_xform::xform_eye (void) |
|
2077 { return ::xform_matrix (); } |
|
2078 |
|
2079 ColumnVector |
|
2080 graphics_xform::transform (double x, double y, double z, |
|
2081 bool use_scale) const |
|
2082 { |
|
2083 if (use_scale) |
|
2084 { |
|
2085 x = sx.scale (x); |
|
2086 y = sy.scale (y); |
|
2087 z = sz.scale (z); |
|
2088 } |
|
2089 |
|
2090 return ::transform (xform, x, y, z); |
|
2091 } |
|
2092 |
|
2093 ColumnVector |
|
2094 graphics_xform::untransform (double x, double y, double z, |
|
2095 bool use_scale) const |
|
2096 { |
|
2097 ColumnVector v = ::transform (xform_inv, x, y, z); |
|
2098 |
|
2099 if (use_scale) |
|
2100 { |
|
2101 v(0) = sx.unscale (v(0)); |
|
2102 v(1) = sy.unscale (v(1)); |
|
2103 v(2) = sz.unscale (v(2)); |
|
2104 } |
|
2105 |
|
2106 return v; |
|
2107 } |
|
2108 |
6836
|
2109 octave_value |
7189
|
2110 axes::get_default (const caseless_str& name) const |
6836
|
2111 { |
|
2112 octave_value retval = default_properties.lookup (name); |
|
2113 |
|
2114 if (retval.is_undefined ()) |
|
2115 { |
|
2116 graphics_handle parent = get_parent (); |
|
2117 graphics_object parent_obj = gh_manager::get_object (parent); |
|
2118 |
|
2119 retval = parent_obj.get_default (name); |
|
2120 } |
|
2121 |
|
2122 return retval; |
|
2123 } |
|
2124 |
7222
|
2125 static void |
|
2126 check_limit_vals (double& min_val, double& max_val, double& min_pos, |
|
2127 const data_property& data) |
|
2128 { |
7267
|
2129 double val = data.min_val (); |
|
2130 if (! (xisinf (val) || xisnan (val)) && val < min_val) |
|
2131 min_val = val; |
|
2132 val = data.max_val (); |
|
2133 if (! (xisinf (val) || xisnan (val)) && val > max_val) |
|
2134 max_val = val; |
|
2135 val = data.min_pos (); |
|
2136 if (! (xisinf (val) || xisnan (val)) && val > 0 && val < min_pos) |
|
2137 min_pos = val; |
7222
|
2138 } |
|
2139 |
|
2140 // Attempt to make "nice" limits from the actual max and min of the |
|
2141 // data. For log plots, we will also use the smallest strictly positive |
|
2142 // value. |
|
2143 |
|
2144 static Matrix |
|
2145 get_axis_limits (double xmin, double xmax, double min_pos, bool logscale) |
|
2146 { |
|
2147 Matrix retval; |
|
2148 |
|
2149 double min_val = xmin; |
|
2150 double max_val = xmax; |
|
2151 |
|
2152 if (! (xisinf (min_val) || xisinf (max_val))) |
|
2153 { |
|
2154 if (logscale) |
|
2155 { |
|
2156 if (xisinf (min_pos)) |
|
2157 { |
|
2158 // warning ("axis: logscale with no positive values to plot"); |
|
2159 return retval; |
|
2160 } |
|
2161 |
|
2162 if (min_val <= 0) |
|
2163 { |
|
2164 warning ("axis: omitting nonpositive data in log plot"); |
|
2165 min_val = min_pos; |
|
2166 } |
|
2167 // FIXME -- maybe this test should also be relative? |
|
2168 if (std::abs (min_val - max_val) < sqrt (DBL_EPSILON)) |
|
2169 { |
|
2170 min_val *= 0.9; |
|
2171 max_val *= 1.1; |
|
2172 } |
|
2173 min_val = pow (10, floor (log10 (min_val))); |
|
2174 max_val = pow (10, ceil (log10 (max_val))); |
|
2175 } |
|
2176 else |
|
2177 { |
|
2178 if (min_val == 0 && max_val == 0) |
|
2179 { |
|
2180 min_val = -1; |
|
2181 max_val = 1; |
|
2182 } |
|
2183 // FIXME -- maybe this test should also be relative? |
|
2184 else if (std::abs (min_val - max_val) < sqrt (DBL_EPSILON)) |
|
2185 { |
|
2186 min_val -= 0.1 * std::abs (min_val); |
|
2187 max_val += 0.1 * std::abs (max_val); |
|
2188 } |
|
2189 // FIXME -- to do a better job, we should consider the tic spacing. |
|
2190 double scale = pow (10, floor (log10 (max_val - min_val) - 1)); |
|
2191 min_val = scale * floor (min_val / scale); |
|
2192 max_val = scale * ceil (max_val / scale); |
|
2193 } |
|
2194 } |
|
2195 |
|
2196 retval.resize (1, 2); |
|
2197 |
|
2198 retval(0) = min_val; |
|
2199 retval(1) = max_val; |
|
2200 |
|
2201 return retval; |
|
2202 } |
|
2203 |
|
2204 static bool updating_axis_limits = false; |
|
2205 |
7214
|
2206 void |
7222
|
2207 axes::update_axis_limits (const std::string& axis_type) |
7214
|
2208 { |
7222
|
2209 if (updating_axis_limits) |
|
2210 return; |
|
2211 |
|
2212 Matrix kids = xproperties.get_children (); |
|
2213 |
|
2214 octave_idx_type n = kids.numel (); |
|
2215 |
|
2216 double min_val = octave_Inf; |
|
2217 double max_val = -octave_Inf; |
|
2218 double min_pos = octave_Inf; |
|
2219 |
|
2220 char update_type = 0; |
|
2221 |
|
2222 Matrix limits; |
|
2223 |
|
2224 if (axis_type == "xdata" || axis_type == "xscale" |
|
2225 || axis_type == "xldata" || axis_type == "xudata" |
|
2226 || axis_type == "xlimmode") |
|
2227 { |
7363
|
2228 if (xproperties.xlimmode_is ("auto")) |
7222
|
2229 { |
|
2230 for (octave_idx_type i = 0; i < n; i++) |
|
2231 { |
|
2232 graphics_object obj = gh_manager::get_object (kids(i)); |
|
2233 |
|
2234 if (obj.isa ("line") || obj.isa ("image") |
|
2235 || obj.isa ("patch") || obj.isa ("surface")) |
|
2236 { |
7363
|
2237 data_property xdata = obj.get_xdata_property (); |
7222
|
2238 |
|
2239 check_limit_vals (min_val, max_val, min_pos, xdata); |
|
2240 |
|
2241 if (obj.isa ("line")) |
|
2242 { |
7363
|
2243 data_property xldata = obj.get_xldata_property (); |
|
2244 data_property xudata = obj.get_xudata_property (); |
7222
|
2245 |
|
2246 check_limit_vals (min_val, max_val, min_pos, xldata); |
|
2247 check_limit_vals (min_val, max_val, min_pos, xudata); |
|
2248 } |
|
2249 } |
|
2250 } |
|
2251 |
|
2252 limits = get_axis_limits (min_val, max_val, min_pos, |
7363
|
2253 xproperties.xscale_is ("log")); |
7222
|
2254 |
|
2255 update_type = 'x'; |
|
2256 } |
|
2257 } |
|
2258 else if (axis_type == "ydata" || axis_type == "yscale" |
|
2259 || axis_type == "ldata" || axis_type == "udata" |
|
2260 || axis_type == "ylimmode") |
|
2261 { |
7363
|
2262 if (xproperties.ylimmode_is ("auto")) |
7222
|
2263 { |
|
2264 for (octave_idx_type i = 0; i < n; i++) |
|
2265 { |
|
2266 graphics_object obj = gh_manager::get_object (kids(i)); |
|
2267 |
|
2268 if (obj.isa ("line") || obj.isa ("image") |
|
2269 || obj.isa ("patch") || obj.isa ("surface")) |
|
2270 { |
7363
|
2271 data_property ydata = obj.get_ydata_property (); |
7222
|
2272 |
|
2273 check_limit_vals (min_val, max_val, min_pos, ydata); |
|
2274 |
|
2275 if (obj.isa ("line")) |
|
2276 { |
7363
|
2277 data_property ldata = obj.get_ldata_property (); |
|
2278 data_property udata = obj.get_udata_property (); |
7222
|
2279 |
|
2280 check_limit_vals (min_val, max_val, min_pos, ldata); |
|
2281 check_limit_vals (min_val, max_val, min_pos, udata); |
|
2282 } |
|
2283 } |
|
2284 } |
|
2285 |
|
2286 limits = get_axis_limits (min_val, max_val, min_pos, |
7363
|
2287 xproperties.yscale_is ("log")); |
7222
|
2288 |
|
2289 update_type = 'y'; |
|
2290 } |
|
2291 } |
|
2292 else if (axis_type == "zdata" || axis_type == "zscale" |
|
2293 || axis_type == "zlimmode") |
|
2294 { |
7363
|
2295 if (xproperties.zlimmode_is ("auto")) |
7222
|
2296 { |
|
2297 for (octave_idx_type i = 0; i < n; i++) |
|
2298 { |
|
2299 graphics_object obj = gh_manager::get_object (kids(i)); |
|
2300 |
|
2301 if (obj.isa ("line") || obj.isa ("patch") || obj.isa ("surface")) |
|
2302 { |
7363
|
2303 data_property zdata = obj.get_zdata_property (); |
7222
|
2304 |
|
2305 check_limit_vals (min_val, max_val, min_pos, zdata); |
|
2306 } |
|
2307 } |
|
2308 |
|
2309 limits = get_axis_limits (min_val, max_val, min_pos, |
7363
|
2310 xproperties.zscale_is ("log")); |
7222
|
2311 |
|
2312 update_type = 'z'; |
|
2313 } |
|
2314 } |
|
2315 else if (axis_type == "cdata" || axis_type == "climmode") |
|
2316 { |
7363
|
2317 if (xproperties.climmode_is ("auto")) |
7222
|
2318 { |
|
2319 for (octave_idx_type i = 0; i < n; i++) |
|
2320 { |
|
2321 graphics_object obj = gh_manager::get_object (kids(i)); |
|
2322 |
|
2323 if (obj.isa ("image") || obj.isa ("patch") || obj.isa ("surface")) |
|
2324 { |
7363
|
2325 data_property cdata = obj.get_cdata_property (); |
7222
|
2326 |
|
2327 check_limit_vals (min_val, max_val, min_pos, cdata); |
|
2328 } |
|
2329 } |
|
2330 |
|
2331 if (min_val == max_val) |
|
2332 max_val = min_val + 1; |
|
2333 |
|
2334 limits.resize (1, 2); |
|
2335 |
|
2336 limits(0) = min_val; |
|
2337 limits(1) = max_val; |
|
2338 |
|
2339 update_type = 'c'; |
|
2340 } |
|
2341 |
|
2342 } |
|
2343 |
|
2344 unwind_protect_bool (updating_axis_limits); |
|
2345 updating_axis_limits = true; |
|
2346 |
|
2347 switch (update_type) |
|
2348 { |
|
2349 case 'x': |
|
2350 xproperties.set_xlim (limits); |
|
2351 xproperties.set_xlimmode ("auto"); |
|
2352 break; |
|
2353 |
|
2354 case 'y': |
|
2355 xproperties.set_ylim (limits); |
|
2356 xproperties.set_ylimmode ("auto"); |
|
2357 break; |
|
2358 |
|
2359 case 'z': |
|
2360 xproperties.set_zlim (limits); |
|
2361 xproperties.set_zlimmode ("auto"); |
|
2362 break; |
|
2363 |
|
2364 case 'c': |
|
2365 xproperties.set_clim (limits); |
|
2366 xproperties.set_climmode ("auto"); |
|
2367 break; |
|
2368 |
|
2369 default: |
|
2370 break; |
|
2371 } |
|
2372 |
7427
|
2373 xproperties.update_transform (); |
|
2374 |
7222
|
2375 unwind_protect::run (); |
7214
|
2376 } |
|
2377 |
7363
|
2378 // --------------------------------------------------------------------- |
|
2379 |
|
2380 // Note: "line" code is entirely auto-generated |
6406
|
2381 |
|
2382 // --------------------------------------------------------------------- |
|
2383 |
7363
|
2384 // Note: "text" code is entirely auto-generated |
6406
|
2385 |
|
2386 // --------------------------------------------------------------------- |
|
2387 |
7363
|
2388 // Note: "image" code is entirely auto-generated |
6406
|
2389 |
|
2390 // --------------------------------------------------------------------- |
|
2391 |
7363
|
2392 // Note: "patch" code is entirely auto-generated |
6807
|
2393 |
|
2394 // --------------------------------------------------------------------- |
|
2395 |
7363
|
2396 // Note: "surface" code is entirely auto-generated |
6406
|
2397 |
|
2398 // --------------------------------------------------------------------- |
|
2399 |
|
2400 octave_value |
7189
|
2401 base_graphics_object::get_default (const caseless_str& name) const |
6406
|
2402 { |
|
2403 graphics_handle parent = get_parent (); |
|
2404 graphics_object parent_obj = gh_manager::get_object (parent); |
|
2405 |
|
2406 return parent_obj.get_default (type () + name); |
|
2407 } |
|
2408 |
|
2409 octave_value |
7189
|
2410 base_graphics_object::get_factory_default (const caseless_str& name) const |
6406
|
2411 { |
|
2412 graphics_object parent_obj = gh_manager::get_object (0); |
|
2413 |
|
2414 return parent_obj.get_factory_default (type () + name); |
|
2415 } |
|
2416 |
7286
|
2417 // We use a random value for the handle to avoid issues with plots and |
|
2418 // scalar values for the first argument. |
6406
|
2419 gh_manager::gh_manager (void) |
7286
|
2420 : handle_map (), handle_free_list (), |
|
2421 next_handle (-1.0 - (rand () + 1.0) / (RAND_MAX + 2.0)) |
6406
|
2422 { |
|
2423 handle_map[0] = graphics_object (new root_figure ()); |
|
2424 } |
|
2425 |
|
2426 graphics_handle |
|
2427 gh_manager::do_make_graphics_handle (const std::string& go_name, |
7370
|
2428 const graphics_handle& p, bool do_createfcn) |
6406
|
2429 { |
|
2430 graphics_handle h = get_handle (go_name); |
|
2431 |
|
2432 base_graphics_object *go = 0; |
|
2433 |
|
2434 if (go_name == "figure") |
|
2435 go = new figure (h, p); |
|
2436 else if (go_name == "axes") |
|
2437 go = new axes (h, p); |
|
2438 else if (go_name == "line") |
|
2439 go = new line (h, p); |
|
2440 else if (go_name == "text") |
|
2441 go = new text (h, p); |
|
2442 else if (go_name == "image") |
|
2443 go = new image (h, p); |
6807
|
2444 else if (go_name == "patch") |
|
2445 go = new patch (h, p); |
6406
|
2446 else if (go_name == "surface") |
|
2447 go = new surface (h, p); |
|
2448 if (go) |
7370
|
2449 { |
|
2450 handle_map[h] = graphics_object (go); |
|
2451 if (do_createfcn) |
|
2452 go->get_properties ().execute_createfcn (); |
|
2453 } |
6406
|
2454 else |
|
2455 error ("gh_manager::do_make_graphics_handle: invalid object type `%s'", |
|
2456 go_name.c_str ()); |
|
2457 |
|
2458 return h; |
|
2459 } |
|
2460 |
|
2461 graphics_handle |
|
2462 gh_manager::do_make_figure_handle (double val) |
|
2463 { |
|
2464 graphics_handle h = val; |
|
2465 |
|
2466 handle_map[h] = graphics_object (new figure (h, 0)); |
|
2467 |
|
2468 return h; |
|
2469 } |
|
2470 |
|
2471 void |
|
2472 gh_manager::do_push_figure (const graphics_handle& h) |
|
2473 { |
|
2474 do_pop_figure (h); |
|
2475 |
|
2476 figure_list.push_front (h); |
|
2477 } |
|
2478 |
|
2479 void |
|
2480 gh_manager::do_pop_figure (const graphics_handle& h) |
|
2481 { |
|
2482 for (figure_list_iterator p = figure_list.begin (); |
|
2483 p != figure_list.end (); |
|
2484 p++) |
|
2485 { |
|
2486 if (*p == h) |
|
2487 { |
|
2488 figure_list.erase (p); |
|
2489 break; |
|
2490 } |
|
2491 } |
|
2492 } |
|
2493 |
|
2494 property_list::plist_map_type |
|
2495 root_figure::init_factory_properties (void) |
|
2496 { |
|
2497 property_list::plist_map_type plist_map; |
|
2498 |
6844
|
2499 plist_map["figure"] = figure::properties::factory_defaults (); |
|
2500 plist_map["axes"] = axes::properties::factory_defaults (); |
|
2501 plist_map["line"] = line::properties::factory_defaults (); |
|
2502 plist_map["text"] = text::properties::factory_defaults (); |
|
2503 plist_map["image"] = image::properties::factory_defaults (); |
|
2504 plist_map["patch"] = patch::properties::factory_defaults (); |
|
2505 plist_map["surface"] = surface::properties::factory_defaults (); |
6406
|
2506 |
|
2507 return plist_map; |
|
2508 } |
|
2509 |
|
2510 // --------------------------------------------------------------------- |
|
2511 |
|
2512 DEFUN (ishandle, args, , |
|
2513 "-*- texinfo -*-\n\ |
6678
|
2514 @deftypefn {Built-in Function} {} ishandle (@var{h})\n\ |
6406
|
2515 Return true if @var{h} is a graphics handle and false otherwise.\n\ |
|
2516 @end deftypefn") |
|
2517 { |
|
2518 octave_value retval; |
|
2519 |
|
2520 if (args.length () == 1) |
|
2521 retval = is_handle (args(0)); |
|
2522 else |
|
2523 print_usage (); |
|
2524 |
|
2525 return retval; |
|
2526 } |
|
2527 |
|
2528 DEFUN (set, args, , |
|
2529 "-*- texinfo -*-\n\ |
6678
|
2530 @deftypefn {Built-in Function} {} set (@var{h}, @var{p}, @var{v}, @dots{})\n\ |
6732
|
2531 Set the named property value or vector @var{p} to the value @var{v}\n\ |
6894
|
2532 for the graphics handle @var{h}.\n\ |
6406
|
2533 @end deftypefn") |
|
2534 { |
|
2535 octave_value retval; |
|
2536 |
|
2537 int nargin = args.length (); |
|
2538 |
|
2539 if (nargin > 0) |
|
2540 { |
6732
|
2541 ColumnVector hcv (args(0).vector_value ()); |
6406
|
2542 |
|
2543 if (! error_state) |
6732
|
2544 { |
6733
|
2545 bool request_drawnow = false; |
|
2546 |
6732
|
2547 for (octave_idx_type n = 0; n < hcv.length (); n++) |
|
2548 { |
|
2549 graphics_object obj = gh_manager::get_object (hcv(n)); |
6406
|
2550 |
6732
|
2551 if (obj) |
|
2552 { |
|
2553 obj.set (args.splice (0, 1)); |
6406
|
2554 |
6733
|
2555 request_drawnow = true; |
6732
|
2556 } |
|
2557 else |
6733
|
2558 { |
|
2559 error ("set: invalid handle (= %g)", hcv(n)); |
|
2560 break; |
|
2561 } |
6732
|
2562 } |
6733
|
2563 |
|
2564 if (! error_state && request_drawnow) |
7409
|
2565 Vdrawnow_requested = true; |
6732
|
2566 } |
6406
|
2567 else |
6732
|
2568 error ("set: expecting graphics handle as first argument"); |
6406
|
2569 } |
|
2570 else |
|
2571 print_usage (); |
|
2572 |
|
2573 return retval; |
|
2574 } |
|
2575 |
|
2576 DEFUN (get, args, , |
|
2577 "-*- texinfo -*-\n\ |
6678
|
2578 @deftypefn {Built-in Function} {} get (@var{h}, @var{p})\n\ |
6406
|
2579 Return the named property @var{p} from the graphics handle @var{h}.\n\ |
|
2580 If @var{p} is omitted, return the complete property list for @var{h}.\n\ |
6732
|
2581 If @var{h} is a vector, return a cell array including the property\n\ |
|
2582 values or lists respectively.\n\ |
6406
|
2583 @end deftypefn") |
|
2584 { |
|
2585 octave_value retval; |
6732
|
2586 octave_value_list vlist; |
6406
|
2587 |
|
2588 int nargin = args.length (); |
|
2589 |
|
2590 if (nargin == 1 || nargin == 2) |
|
2591 { |
6732
|
2592 ColumnVector hcv (args(0).vector_value ()); |
6406
|
2593 |
|
2594 if (! error_state) |
6732
|
2595 { |
6733
|
2596 octave_idx_type len = hcv.length (); |
|
2597 |
|
2598 vlist.resize (len); |
|
2599 |
|
2600 for (octave_idx_type n = 0; n < len; n++) |
6732
|
2601 { |
|
2602 graphics_object obj = gh_manager::get_object (hcv(n)); |
6406
|
2603 |
6732
|
2604 if (obj) |
|
2605 { |
|
2606 if (nargin == 1) |
|
2607 vlist(n) = obj.get (); |
|
2608 else |
|
2609 { |
7189
|
2610 caseless_str property = args(1).string_value (); |
6406
|
2611 |
6732
|
2612 if (! error_state) |
|
2613 vlist(n) = obj.get (property); |
|
2614 else |
6733
|
2615 { |
|
2616 error ("get: expecting property name as second argument"); |
|
2617 break; |
|
2618 } |
6732
|
2619 } |
|
2620 } |
|
2621 else |
6733
|
2622 { |
|
2623 error ("get: invalid handle (= %g)", hcv(n)); |
|
2624 break; |
|
2625 } |
6732
|
2626 } |
|
2627 } |
6406
|
2628 else |
6732
|
2629 error ("get: expecting graphics handle as first argument"); |
6406
|
2630 } |
|
2631 else |
|
2632 print_usage (); |
|
2633 |
6733
|
2634 if (! error_state) |
6732
|
2635 { |
6733
|
2636 octave_idx_type len = vlist.length (); |
|
2637 |
|
2638 if (len > 1) |
|
2639 retval = Cell (vlist); |
|
2640 else if (len == 1) |
|
2641 retval = vlist(0); |
6732
|
2642 } |
|
2643 |
6406
|
2644 return retval; |
|
2645 } |
|
2646 |
7379
|
2647 DEFUN (__get__, args, , |
|
2648 "-*- texinfo -*-\n\ |
|
2649 @deftypefn {Built-in Function} {} __get__ (@var{h})\n\ |
|
2650 Return all properties from the graphics handle @var{h}.\n\ |
|
2651 If @var{h} is a vector, return a cell array including the property\n\ |
|
2652 values or lists respectively.\n\ |
|
2653 @end deftypefn") |
|
2654 { |
|
2655 octave_value retval; |
|
2656 octave_value_list vlist; |
|
2657 |
|
2658 int nargin = args.length (); |
|
2659 |
|
2660 if (nargin == 1) |
|
2661 { |
|
2662 ColumnVector hcv (args(0).vector_value ()); |
|
2663 |
|
2664 if (! error_state) |
|
2665 { |
|
2666 octave_idx_type len = hcv.length (); |
|
2667 |
|
2668 vlist.resize (len); |
|
2669 |
|
2670 for (octave_idx_type n = 0; n < len; n++) |
|
2671 { |
|
2672 graphics_object obj = gh_manager::get_object (hcv(n)); |
|
2673 |
|
2674 if (obj) |
|
2675 vlist(n) = obj.get (true); |
|
2676 else |
|
2677 { |
|
2678 error ("get: invalid handle (= %g)", hcv(n)); |
|
2679 break; |
|
2680 } |
|
2681 } |
|
2682 } |
|
2683 else |
|
2684 error ("get: expecting graphics handle as first argument"); |
|
2685 } |
|
2686 else |
|
2687 print_usage (); |
|
2688 |
|
2689 if (! error_state) |
|
2690 { |
|
2691 octave_idx_type len = vlist.length (); |
|
2692 |
|
2693 if (len > 1) |
|
2694 retval = Cell (vlist); |
|
2695 else if (len == 1) |
|
2696 retval = vlist(0); |
|
2697 } |
|
2698 |
|
2699 return retval; |
|
2700 } |
|
2701 |
6406
|
2702 static octave_value |
|
2703 make_graphics_object (const std::string& go_name, |
6874
|
2704 const octave_value_list& args) |
6406
|
2705 { |
|
2706 octave_value retval; |
|
2707 |
|
2708 double val = args(0).double_value (); |
|
2709 |
|
2710 if (! error_state) |
|
2711 { |
|
2712 graphics_handle parent = gh_manager::lookup (val); |
|
2713 |
7056
|
2714 if (parent.ok ()) |
6406
|
2715 { |
|
2716 graphics_handle h |
7370
|
2717 = gh_manager::make_graphics_handle (go_name, parent, false); |
6406
|
2718 |
|
2719 if (! error_state) |
|
2720 { |
|
2721 adopt (parent, h); |
|
2722 |
|
2723 xset (h, args.splice (0, 1)); |
7370
|
2724 xcreatefcn (h); |
6406
|
2725 |
6874
|
2726 retval = h.value (); |
7296
|
2727 |
|
2728 if (! error_state) |
7409
|
2729 Vdrawnow_requested = true; |
6406
|
2730 } |
|
2731 else |
|
2732 error ("__go%s__: unable to create graphics handle", |
|
2733 go_name.c_str ()); |
|
2734 } |
|
2735 else |
|
2736 error ("__go_%s__: invalid parent", go_name.c_str ()); |
|
2737 } |
|
2738 else |
|
2739 error ("__go_%s__: invalid parent", go_name.c_str ()); |
|
2740 |
|
2741 return retval; |
|
2742 } |
|
2743 |
|
2744 DEFUN (__go_figure__, args, , |
|
2745 "-*- texinfo -*-\n\ |
|
2746 @deftypefn {Built-in Function} {} __go_figure__ (@var{fignum})\n\ |
6945
|
2747 Undocumented internal function.\n\ |
6406
|
2748 @end deftypefn") |
|
2749 { |
|
2750 octave_value retval; |
|
2751 |
|
2752 if (args.length () > 0) |
|
2753 { |
|
2754 double val = args(0).double_value (); |
|
2755 |
|
2756 if (! error_state) |
|
2757 { |
|
2758 if (is_figure (val)) |
|
2759 { |
|
2760 graphics_handle h = gh_manager::lookup (val); |
|
2761 |
|
2762 xset (h, args.splice (0, 1)); |
|
2763 |
6874
|
2764 retval = h.value (); |
6406
|
2765 } |
|
2766 else |
|
2767 { |
|
2768 graphics_handle h = octave_NaN; |
|
2769 |
|
2770 if (xisnan (val)) |
7370
|
2771 h = gh_manager::make_graphics_handle ("figure", 0, false); |
6406
|
2772 else if (val > 0 && D_NINT (val) == val) |
|
2773 h = gh_manager::make_figure_handle (val); |
|
2774 else |
|
2775 error ("__go_figure__: invalid figure number"); |
|
2776 |
7056
|
2777 if (! error_state && h.ok ()) |
6406
|
2778 { |
|
2779 adopt (0, h); |
|
2780 |
|
2781 xset (h, args.splice (0, 1)); |
7370
|
2782 xcreatefcn (h); |
6406
|
2783 |
6874
|
2784 retval = h.value (); |
6406
|
2785 } |
|
2786 else |
|
2787 error ("__go_figure__: failed to create figure handle"); |
|
2788 } |
|
2789 } |
|
2790 else |
|
2791 error ("__go_figure__: expecting figure number to be double value"); |
|
2792 } |
|
2793 else |
|
2794 print_usage (); |
|
2795 |
|
2796 return retval; |
|
2797 } |
|
2798 |
|
2799 #define GO_BODY(TYPE) \ |
|
2800 octave_value retval; \ |
|
2801 \ |
|
2802 if (args.length () > 0) \ |
|
2803 retval = make_graphics_object (#TYPE, args); \ |
|
2804 else \ |
|
2805 print_usage (); \ |
|
2806 \ |
|
2807 return retval |
|
2808 |
|
2809 DEFUN (__go_axes__, args, , |
|
2810 "-*- texinfo -*-\n\ |
|
2811 @deftypefn {Built-in Function} {} __go_axes__ (@var{parent})\n\ |
6945
|
2812 Undocumented internal function.\n\ |
6406
|
2813 @end deftypefn") |
|
2814 { |
|
2815 GO_BODY (axes); |
|
2816 } |
|
2817 |
|
2818 DEFUN (__go_line__, args, , |
|
2819 "-*- texinfo -*-\n\ |
|
2820 @deftypefn {Built-in Function} {} __go_line__ (@var{parent})\n\ |
6945
|
2821 Undocumented internal function.\n\ |
6406
|
2822 @end deftypefn") |
|
2823 { |
|
2824 GO_BODY (line); |
|
2825 } |
|
2826 |
|
2827 DEFUN (__go_text__, args, , |
|
2828 "-*- texinfo -*-\n\ |
|
2829 @deftypefn {Built-in Function} {} __go_text__ (@var{parent})\n\ |
6945
|
2830 Undocumented internal function.\n\ |
6406
|
2831 @end deftypefn") |
|
2832 { |
|
2833 GO_BODY (text); |
|
2834 } |
|
2835 |
|
2836 DEFUN (__go_image__, args, , |
|
2837 "-*- texinfo -*-\n\ |
|
2838 @deftypefn {Built-in Function} {} __go_image__ (@var{parent})\n\ |
6945
|
2839 Undocumented internal function.\n\ |
6406
|
2840 @end deftypefn") |
|
2841 { |
|
2842 GO_BODY (image); |
|
2843 } |
|
2844 |
|
2845 DEFUN (__go_surface__, args, , |
|
2846 "-*- texinfo -*-\n\ |
|
2847 @deftypefn {Built-in Function} {} __go_surface__ (@var{parent})\n\ |
6945
|
2848 Undocumented internal function.\n\ |
6406
|
2849 @end deftypefn") |
|
2850 { |
|
2851 GO_BODY (surface); |
|
2852 } |
|
2853 |
6807
|
2854 DEFUN (__go_patch__, args, , |
|
2855 "-*- texinfo -*-\n\ |
|
2856 @deftypefn {Built-in Function} {} __go_patch__ (@var{parent})\n\ |
6945
|
2857 Undocumented internal function.\n\ |
6807
|
2858 @end deftypefn") |
|
2859 { |
|
2860 GO_BODY (patch); |
|
2861 } |
|
2862 |
6406
|
2863 DEFUN (__go_delete__, args, , |
|
2864 "-*- texinfo -*-\n\ |
|
2865 @deftypefn {Built-in Function} {} __go_delete__ (@var{h})\n\ |
6945
|
2866 Undocumented internal function.\n\ |
6406
|
2867 @end deftypefn") |
|
2868 { |
|
2869 octave_value_list retval; |
|
2870 |
|
2871 if (args.length () == 1) |
|
2872 { |
|
2873 graphics_handle h = octave_NaN; |
|
2874 |
|
2875 double val = args(0).double_value (); |
|
2876 |
|
2877 if (! error_state) |
|
2878 { |
|
2879 h = gh_manager::lookup (val); |
|
2880 |
7056
|
2881 if (h.ok ()) |
6406
|
2882 { |
|
2883 graphics_object obj = gh_manager::get_object (h); |
|
2884 |
|
2885 graphics_handle parent_h = obj.get_parent (); |
|
2886 |
|
2887 graphics_object parent_obj = gh_manager::get_object (parent_h); |
|
2888 |
7367
|
2889 // NOTE: free the handle before removing it from its parent's |
|
2890 // children, such that the object's state is correct when |
|
2891 // the deletefcn callback is executed |
6406
|
2892 |
|
2893 gh_manager::free (h); |
7367
|
2894 |
|
2895 parent_obj.remove_child (h); |
6406
|
2896 } |
|
2897 else |
|
2898 error ("delete: invalid graphics object (= %g)", val); |
|
2899 } |
|
2900 else |
|
2901 error ("delete: invalid graphics object"); |
|
2902 } |
|
2903 else |
|
2904 print_usage (); |
|
2905 |
|
2906 return retval; |
|
2907 } |
|
2908 |
|
2909 DEFUN (__go_axes_init__, args, , |
|
2910 "-*- texinfo -*-\n\ |
|
2911 @deftypefn {Built-in Function} {} __go_axes_init__ (@var{h}, @var{mode})\n\ |
6945
|
2912 Undocumented internal function.\n\ |
6406
|
2913 @end deftypefn") |
|
2914 { |
|
2915 octave_value retval; |
|
2916 |
|
2917 int nargin = args.length (); |
|
2918 |
|
2919 std::string mode = ""; |
|
2920 |
|
2921 if (nargin == 2) |
|
2922 { |
|
2923 mode = args(1).string_value (); |
|
2924 |
|
2925 if (error_state) |
|
2926 return retval; |
|
2927 } |
|
2928 |
|
2929 if (nargin == 1 || nargin == 2) |
|
2930 { |
|
2931 graphics_handle h = octave_NaN; |
|
2932 |
|
2933 double val = args(0).double_value (); |
|
2934 |
|
2935 if (! error_state) |
|
2936 { |
|
2937 h = gh_manager::lookup (val); |
|
2938 |
7056
|
2939 if (h.ok ()) |
6406
|
2940 { |
|
2941 graphics_object obj = gh_manager::get_object (h); |
|
2942 |
|
2943 obj.set_defaults (mode); |
|
2944 } |
|
2945 else |
|
2946 error ("__go_axes_init__: invalid graphics object (= %g)", val); |
|
2947 } |
|
2948 else |
|
2949 error ("__go_axes_init__: invalid graphics object"); |
|
2950 } |
|
2951 else |
|
2952 print_usage (); |
|
2953 |
|
2954 return retval; |
|
2955 } |
|
2956 |
|
2957 DEFUN (__go_handles__, , , |
|
2958 "-*- texinfo -*-\n\ |
|
2959 @deftypefn {Built-in Function} {} __go_handles__ ()\n\ |
6945
|
2960 Undocumented internal function.\n\ |
6406
|
2961 @end deftypefn") |
|
2962 { |
6425
|
2963 return octave_value (gh_manager::handle_list ()); |
|
2964 } |
|
2965 |
|
2966 DEFUN (__go_figure_handles__, , , |
|
2967 "-*- texinfo -*-\n\ |
|
2968 @deftypefn {Built-in Function} {} __go_figure_handles__ ()\n\ |
6945
|
2969 Undocumented internal function.\n\ |
6425
|
2970 @end deftypefn") |
|
2971 { |
|
2972 return octave_value (gh_manager::figure_handle_list ()); |
6406
|
2973 } |
|
2974 |
7409
|
2975 static void |
|
2976 clear_drawnow_request (void *) |
|
2977 { |
|
2978 Vdrawnow_requested = false; |
|
2979 } |
|
2980 |
7408
|
2981 DEFUN (drawnow, args, , |
|
2982 "-*- texinfo -*-\n\ |
|
2983 @deftypefn {Built-in Function} {} __go_drawnow__ ()\n\ |
|
2984 @deftypefnx {Built-in Function} {} __go_drawnow__ (@var{term}, @var{file}, @var{mono}, @var{debug_file})\n\ |
|
2985 Undocumented internal function.\n\ |
|
2986 @end deftypefn") |
|
2987 { |
|
2988 static int drawnow_executing = 0; |
|
2989 static bool __go_close_all_registered__ = false; |
|
2990 |
|
2991 octave_value retval; |
|
2992 |
7409
|
2993 unwind_protect::begin_frame ("Fdrawnow"); |
|
2994 unwind_protect::add (clear_drawnow_request); |
|
2995 |
|
2996 unwind_protect_int (drawnow_executing); |
|
2997 |
|
2998 if (++drawnow_executing <= 1) |
7408
|
2999 { |
7409
|
3000 if (! __go_close_all_registered__) |
|
3001 { |
|
3002 octave_add_atexit_function ("__go_close_all__"); |
|
3003 |
|
3004 __go_close_all_registered__ = true; |
|
3005 } |
|
3006 |
|
3007 if (args.length () == 0) |
7408
|
3008 { |
7409
|
3009 Matrix hlist = gh_manager::figure_handle_list (); |
|
3010 |
|
3011 for (int i = 0; ! error_state && i < hlist.length (); i++) |
7408
|
3012 { |
7409
|
3013 graphics_handle h = gh_manager::lookup (hlist(i)); |
|
3014 |
|
3015 if (h.ok () && h != 0) |
7408
|
3016 { |
7409
|
3017 graphics_object go = gh_manager::get_object (h); |
|
3018 figure::properties& fprops = dynamic_cast <figure::properties&> (go.get_properties ()); |
|
3019 |
|
3020 if (fprops.is_modified ()) |
7408
|
3021 { |
7409
|
3022 if (fprops.is_visible ()) |
|
3023 fprops.get_backend ().redraw_figure (h); |
|
3024 else if (! fprops.get___plot_stream__ ().is_empty ()) |
|
3025 { |
|
3026 fprops.close (false); |
|
3027 fprops.set___plot_stream__ (Matrix ()); |
|
3028 fprops.set___enhanced__ (false); |
|
3029 } |
|
3030 fprops.set_modified (false); |
7408
|
3031 } |
|
3032 } |
|
3033 } |
|
3034 } |
7409
|
3035 else if (args.length () >= 2 && args.length () <= 4) |
7408
|
3036 { |
7409
|
3037 std::string term, file, debug_file; |
|
3038 bool mono; |
|
3039 |
|
3040 term = args(0).string_value (); |
7408
|
3041 |
|
3042 if (! error_state) |
|
3043 { |
7409
|
3044 file = args(1).string_value (); |
7408
|
3045 |
|
3046 if (! error_state) |
|
3047 { |
7409
|
3048 size_t pos = file.find_last_of (file_ops::dir_sep_chars); |
|
3049 |
|
3050 if (pos != NPOS) |
|
3051 { |
|
3052 file_stat fs (file.substr (0, pos)); |
|
3053 |
|
3054 if (! (fs && fs.is_dir ())) |
|
3055 error ("drawnow: nonexistent directory `%s'", |
|
3056 file.substr (0, pos).c_str ()); |
|
3057 } |
|
3058 |
|
3059 mono = (args.length () >= 3 ? args(2).bool_value () : false); |
7408
|
3060 |
|
3061 if (! error_state) |
|
3062 { |
7409
|
3063 debug_file = (args.length () > 3 ? args(3).string_value () |
|
3064 : ""); |
|
3065 |
|
3066 if (! error_state) |
7408
|
3067 { |
7409
|
3068 graphics_handle h = gcf (); |
|
3069 |
|
3070 if (h.ok ()) |
|
3071 { |
|
3072 graphics_object go = gh_manager::get_object (h); |
|
3073 |
7419
|
3074 go.get_backend () |
7409
|
3075 .print_figure (h, term, file, mono, debug_file); |
|
3076 } |
|
3077 else |
|
3078 error ("drawnow: nothing to draw"); |
7408
|
3079 } |
|
3080 else |
7409
|
3081 error ("drawnow: invalid debug_file, expected a string value"); |
7408
|
3082 } |
|
3083 else |
7409
|
3084 error ("drawnow: invalid colormode, expected a boolean value"); |
7408
|
3085 } |
|
3086 else |
7409
|
3087 error ("drawnow: invalid file, expected a string value"); |
7408
|
3088 } |
|
3089 else |
7409
|
3090 error ("drawnow: invalid terminal, expected a string value"); |
7408
|
3091 } |
|
3092 else |
7409
|
3093 print_usage (); |
7408
|
3094 } |
7409
|
3095 |
|
3096 unwind_protect::run_frame ("Fdrawnow"); |
7408
|
3097 |
|
3098 return retval; |
|
3099 } |
|
3100 |
6595
|
3101 octave_value |
|
3102 get_property_from_handle (double handle, const std::string &property, |
|
3103 const std::string &func) |
|
3104 { |
|
3105 graphics_object obj = gh_manager::get_object (handle); |
|
3106 octave_value retval; |
|
3107 |
|
3108 if (obj) |
|
3109 { |
7189
|
3110 caseless_str p = std::string (property); |
6595
|
3111 retval = obj.get (p); |
|
3112 } |
|
3113 else |
|
3114 error ("%s: invalid handle (= %g)", func.c_str(), handle); |
|
3115 |
|
3116 return retval; |
|
3117 } |
|
3118 |
|
3119 bool |
|
3120 set_property_in_handle (double handle, const std::string &property, |
|
3121 const octave_value &arg, const std::string &func) |
|
3122 { |
|
3123 graphics_object obj = gh_manager::get_object (handle); |
|
3124 int ret = false; |
|
3125 |
|
3126 if (obj) |
|
3127 { |
7189
|
3128 caseless_str p = std::string (property); |
6595
|
3129 obj.set (p, arg); |
|
3130 if (!error_state) |
|
3131 ret = true; |
|
3132 } |
|
3133 else |
|
3134 error ("%s: invalid handle (= %g)", func.c_str(), handle); |
|
3135 |
|
3136 return ret; |
|
3137 } |
|
3138 |
6406
|
3139 /* |
|
3140 ;;; Local Variables: *** |
|
3141 ;;; mode: C++ *** |
|
3142 ;;; End: *** |
|
3143 */ |