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