]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/view.m
update packages
[CreaPhase.git] / octave_packages / m / plot / view.m
1 ## Copyright (C) 2007-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {[@var{azimuth}, @var{elevation}] =} view ()
21 ## @deftypefnx {Function File} {} view (@var{azimuth}, @var{elevation})
22 ## @deftypefnx {Function File} {} view ([@var{azimuth} @var{elevation}])
23 ## @deftypefnx {Function File} {} view ([@var{x} @var{y} @var{z}])
24 ## @deftypefnx {Function File} {} view (@var{dims})
25 ## @deftypefnx {Function File} {} view (@var{ax}, @dots{})
26 ## Query or set the viewpoint for the current axes.  The parameters
27 ## @var{azimuth} and @var{elevation} can be given as two arguments or as
28 ## 2-element vector.
29 ## The viewpoint can also be given with Cartesian coordinates @var{x},
30 ## @var{y}, and @var{z}.
31 ## The call @code{view (2)} sets the viewpoint to @var{azimuth} = 0
32 ## and @var{elevation} = 90, which is the default for 2-D graphs.
33 ## The call @code{view (3)} sets the viewpoint to @var{azimuth} = -37.5
34 ## and @var{elevation} = 30, which is the default for 3-D graphs.
35 ## If @var{ax} is given, the viewpoint is set for this axes, otherwise
36 ## it is set for the current axes.
37 ## @end deftypefn
38
39 ## Author: jwe
40
41 function [azimuth, elevation] = view (varargin)
42
43   if (nargin < 4)
44     if (nargin == 0)
45       args = {get(gca (), "view")};
46     else
47       ax = varargin{1};
48       if (ishandle (ax) && strcmp (get (ax, "type"), "axes"))
49         args = varargin(2:end);
50       else
51         ax = gca;
52         args = varargin;
53       endif
54     endif
55     if (length (args) == 1)
56       x = args{1};
57       if (length (x) == 2)
58         az = x(1);
59         el = x(2);
60       elseif (length (x) == 3)
61         [az, el] = cart2sph (x(1), x(2), x(3));
62         az *= 180/pi;
63         az += 90;
64         el *= 180/pi;
65       elseif (x == 2)
66         az = 0;
67         el = 90;
68       elseif (x == 3)
69         az = -37.5;
70         el = 30;
71       else
72         print_usage ();
73       endif
74     elseif (length (args) == 2)
75       az = args{1};
76       el = args{2};
77     endif
78
79     if (nargin > 0)
80       set (ax, "view", [az, el]);
81     endif
82
83     if (nargout == 1)
84       error ("view: T = view () not implemented");
85     endif
86
87     if (nargout == 2)
88       azimuth = az;
89       elevation = el;
90     endif
91   else
92     print_usage ();
93   endif
94
95 endfunction
96
97 %!test
98 %! hf = figure ("visible", "off");
99 %! unwind_protect
100 %!   plot3 ([0,1], [0,1], [0,1]);
101 %!   [az, el] = view;
102 %!   assert ([az, el], [-37.5, 30], eps);
103 %!   view (2);
104 %!   [az, el] = view;
105 %!   assert ([az, el], [0, 90], eps);
106 %!   view ([1 1 0]);
107 %!   [az, el] = view;
108 %!   assert ([az, el], [135, 0], eps);
109 %! unwind_protect_cleanup
110 %!   close (hf);
111 %! end_unwind_protect
112
113 %!test
114 %! hf = figure ("visible", "off");
115 %! unwind_protect
116 %!   line;
117 %!   [az, el] = view;
118 %!   assert ([az, el], [0, 90], eps);
119 %!   view (3);
120 %!   [az, el] = view;
121 %!   assert ([az, el], [-37.5, 30], eps);
122 %! unwind_protect_cleanup
123 %!   close (hf);
124 %! end_unwind_protect