1## Copyright (C) 1998-2003  Joao Cardoso.
2## Copyright (C) 2004  Rafael Laboissiere
3##
4## This program is free software; you can redistribute it and/or modify it
5## under the terms of the GNU General Public License as published by the
6## Free Software Foundation; either version 2 of the License, or (at your
7## option) any later version.
8##
9## This program is distributed in the hope that it will be useful, but
10## WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12## General Public License for more details.
13##
14## This file is part of plplot_octave.
15
16## shade(x, y, z [, levels [, contour] )
17## shade(z [, levels [, contour] )
18##
19## plot shade of matrix z versus vectors x,y.
20## level can be a scalar speficying the number of shade levels,
21## or a vector, specifying where the shade level must be
22## if contour exists, each shade level will be contoured with 'countour' color
23
24function shade(x, y, z, levels, cont )
25
26  global __pl
27  strm = __pl_init;
28
29  unwind_protect
30
31  old_empty_list_elements_ok = warning("query", "Octave:empty-list-elements");
32  warning("off","Octave:empty-list-elements");
33
34  if (nargin == 1 && ismatrix(x))
35    levels = 20;
36    cont = 0;
37    z = x; y = 1:rows(z); x = 1:columns(z);
38  elseif (nargin == 2 && ismatrix(x))
39    cont = 0; levels = y;
40    z = x; y = 1:rows(z); x = 1:columns(z);
41  elseif (nargin == 3 && isscalar(z))
42    levels = y; cont = z;
43    z = x; y = 1:rows(z); x = 1:columns(z);
44  elseif (nargin == 3)
45    levels = 20;
46    cont = 0;
47  elseif (nargin == 4)
48    cont = 0;
49  endif
50
51  if (isscalar(levels) && levels == 1)	# segmentation violation!
52    levels = 2;
53  endif
54
55  if (rows(x) > 1 && columns(x) > 1 && rows(y) > 1 && columns(y) > 1)
56    xymat = 1;
57  else
58    xymat = 0;
59  endif
60
61  ## plot color and pen width of boundary of shade region
62
63  max_color = 0; max_width = 0;
64
65  if (cont)
66    cont_color = cont; cont_width = 1;
67  else
68    cont_color = 0; cont_width = 0;
69  endif
70
71  if (ishold == 0)
72    xmm = xm = min(min(x)); xM = max(max(x));
73    ymm = ym = min(min(y)); yM = max(max(y));
74    zm = min(min(z)); zM = max(max(z));
75
76    if (__pl.axis_st(strm))
77      xm = __pl.axis(strm,1); xM = __pl.axis(strm,2);
78      if (xymat == 0)
79        ix = find(x >= xm & x <= xM);
80        x=x(ix); z=z(:,ix);
81        xmm = min(x);
82      endif
83
84      if (length(__pl.axis(strm,:)) >= 4)
85	ym = __pl.axis(strm,3); yM = __pl.axis(strm,4);
86        if (xymat == 0)
87	  iy = find(y >= ym & y <= yM);
88	  y=y(iy); z=z(iy,:);
89	  ymm = min(y);
90        endif
91      else
92	__pl.axis(strm,3) = ym; __pl.axis(strm,4) = yM;
93      endif
94      if (length(__pl.axis(strm,:)) == 6)
95	zm = __pl.axis(strm,5); zM = __pl.axis(strm,6);
96      else
97	__pl.axis(strm,5) = zm; __pl.axis(strm,6) = zM;
98      endif
99    else	# make axis() return current axis
100      __pl.axis(strm,1) = xm; __pl.axis(strm,2) = xM;
101      __pl.axis(strm,3) = ym; __pl.axis(strm,4) = yM;
102      __pl.axis(strm,5) = zm; __pl.axis(strm,6) = zM;
103    endif
104
105    __pl.plcol0(strm) = 1;
106    plcol0(15);pllsty(1);
107    __pl_plenv(xm, xM, ym, yM, 0, -2);
108  else
109    if (columns(__pl.axis(strm,:)) != 6)
110      error("You must contour/shade plot something before entering hold mode");
111    endif
112    xmm = xm = __pl.axis(strm,1); xM = __pl.axis(strm,2);
113    ymm = ym = __pl.axis(strm,3); yM = __pl.axis(strm,4);
114    zm = __pl.axis(strm,5); zM = __pl.axis(strm,6);
115    if (xymat == 0)
116      ix = find(x >= xm & x <= xM);
117      iy = find(y >= ym & y <= yM);
118      z = z( iy, ix );
119      x = x( ix );
120      y = y( iy );
121    endif
122  endif
123
124  maxx = max(max(x)); maxy = max(max(y)); minx = min(min(x)); miny = min(min(y));
125  if (columns(x)>1 && rows(x) == 1)
126    x = x';
127  endif
128  if (columns(y)>1 && rows(y) == 1)
129    y = y';
130  endif
131
132  if (!isscalar(levels))
133    n = length(levels)-1;
134    clevel = levels;
135    cclevel = clevel;
136  else
137    n = levels;
138    clevel = linspace(zm, zM, levels+1);
139    cclevel = linspace(zm, zM, levels);
140  endif
141
142  __pl.type(strm) = -2;
143  __pl.lab_str = "";
144  __pl.plcol0(strm) = 1;
145  __pl.pllsty(strm) = 1;
146  __pl.lab_pos(strm) = 1;
147
148  plpsty(0);
149  if (0) ## plshades() is slower than several calls to plshade() !? and plshades() sometimes fails ?!
150    for i = 1:n
151      plshade1(z', 0, minx, maxx, miny, maxy,
152	      clevel(i), clevel(i+1),
153	      1, (i-1) / (n-1), 1,
154	      cont_color, cont_width, max_color, max_width, 1, x, y);
155    endfor
156  else
157    if (columns(x) == 1 && columns(y) == 1)
158      plshades1(z', minx, maxx, miny, maxy,
159	     clevel', 1, cont_color, cont_width, 1, x, y);
160    else
161      plshades2(z', minx, maxx, miny, maxy,
162	     clevel', 1, cont_color, cont_width, 1, x', y');
163    endif
164  endif
165
166  for i = 1:n
167    __pl.lab_str = [__pl.lab_str; sprintf("%#+.2G", cclevel(i))];
168    __pl.lab_col(strm,__pl.lab_pos(strm)) = __pl.plcol0(strm);
169    __pl.lab_lsty(strm,__pl.lab_pos(strm)) = __pl.pllsty(strm);
170    __pl.lab_pos(strm) = __pl.lab_pos(strm) + 1;
171    __pl.plcol0(strm) = rem(__pl.plcol0(strm), 15)+1;
172    if  (__pl.line_style(strm))
173      __pl.pllsty(strm) = rem(__pl.pllsty(strm), 8)+1;
174    endif
175  endfor
176
177  if (__pl.grid(strm))		# this has to be done after shading
178    plcol0(15);
179    plbox("bcnsgt",0,0,"bcnsgtv",0,0)
180  else
181    plcol0(15);
182    plbox("bcnst",0,0,"bcnstv",0,0)
183  endif
184
185  if (__pl.legend(strm))
186    __pl_draw_legend;
187  endif
188
189  plcol0(15);
190  pllab(tdeblank(__pl.xlabel(strm,:)), tdeblank(__pl.ylabel(strm,:)), tdeblank(__pl.tlabel(strm,:)));
191
192  plflush;
193  __pl.items(strm) = 1; # for now!
194
195  unwind_protect_cleanup
196
197  warning(old_empty_list_elements_ok.state, "Octave:empty-list-elements");
198
199  end_unwind_protect
200
201
202endfunction
203