1 ## Copyright (C) 2007-2012 Kai Habel, David Bateman
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {} slice (@var{x}, @var{y}, @var{z}, @var{v}, @var{sx}, @var{sy}, @var{sz})
21 ## @deftypefnx {Function File} {} slice (@var{x}, @var{y}, @var{z}, @var{v}, @var{xi}, @var{yi}, @var{zi})
22 ## @deftypefnx {Function File} {} slice (@var{v}, @var{sx}, @var{sy}, @var{sz})
23 ## @deftypefnx {Function File} {} slice (@var{v}, @var{xi}, @var{yi}, @var{zi})
24 ## @deftypefnx {Function File} {@var{h} =} slice (@dots{})
25 ## @deftypefnx {Function File} {@var{h} =} slice (@dots{}, @var{method})
26 ## Plot slices of 3-D data/scalar fields. Each element of the 3-dimensional
27 ## array @var{v} represents a scalar value at a location given by the
28 ## parameters @var{x}, @var{y}, and @var{z}. The parameters @var{x},
29 ## @var{x}, and @var{z} are either 3-dimensional arrays of the same size
30 ## as the array @var{v} in the "meshgrid" format or vectors. The
31 ## parameters @var{xi}, etc. respect a similar format to @var{x}, etc.,
32 ## and they represent the points at which the array @var{vi} is
33 ## interpolated using interp3. The vectors @var{sx}, @var{sy}, and
34 ## @var{sz} contain points of orthogonal slices of the respective axes.
36 ## If @var{x}, @var{y}, @var{z} are omitted, they are assumed to be
37 ## @code{x = 1:size (@var{v}, 2)}, @code{y = 1:size (@var{v}, 1)} and
38 ## @code{z = 1:size (@var{v}, 3)}.
40 ## @var{Method} is one of:
44 ## Return the nearest neighbor.
47 ## Linear interpolation from nearest neighbors.
50 ## Cubic interpolation from four nearest neighbors (not implemented yet).
53 ## Cubic spline interpolation---smooth first and second derivatives
54 ## throughout the curve.
57 ## The default method is @code{"linear"}.
59 ## The optional return value @var{h} is a graphics handle to the created
66 ## [x, y, z] = meshgrid (linspace (-8, 8, 32));
67 ## v = sin (sqrt (x.^2 + y.^2 + z.^2)) ./ (sqrt (x.^2 + y.^2 + z.^2));
68 ## slice (x, y, z, v, [], 0, []);
69 ## [xi, yi] = meshgrid (linspace (-7, 7));
71 ## slice (x, y, z, v, xi, yi, zi);
74 ## @seealso{interp3, surface, pcolor}
77 ## Author: Kai Habel <kai.habel@gmx.de>
79 function h = slice (varargin)
84 if (ischar (varargin{end}))
85 method = varargin{end};
92 error ("slice: expect 3-dimensional array of values");
94 [nx, ny, nz] = size (v);
95 [x, y, z] = meshgrid (1:nx, 1:ny, 1:nz);
102 error ("slice: expect 3-dimensional array of values");
107 if (all ([isvector(x), isvector(y), isvector(z)]))
108 [x, y, z] = meshgrid (x, y, z);
109 elseif (ndims (x) == 3 && size_equal (x, y, z))
112 error ("slice: X, Y, Z size mismatch");
121 if (any ([isvector(sx), isvector(sy), isvector(sz)]))
123 elseif (ndims(sx) == 2 && size_equal (sx, sy, sz))
126 error ("slice: dimensional mismatch for (XI, YI, ZI) or (SX, SY, SZ)");
134 set (ax, "clim", [minv, maxv]);
137 ns = length (sx) + length (sy) + length (sz);
139 [ny, nx, nz] = size (v);
142 [xi, yi, zi] = meshgrid (squeeze (x(1,:,1)),
143 squeeze (y(:,1,1)), sz(i));
144 vz = squeeze (interp3 (x, y, z, v, xi, yi, zi, method));
145 tmp(sidx++) = surface (xi, yi, sz(i) * ones (size (yi)), vz);
150 for i = length(sy):-1:1
151 [xi, yi, zi] = meshgrid (squeeze (x(1,:,1)), sy(i), squeeze (z(1,1,:)));
152 vy = squeeze (interp3 (x, y, z, v, xi, yi, zi, method));
153 tmp(sidx++) = surface (squeeze (xi),
154 squeeze (sy(i) * ones (size (zi))),
160 for i = length(sx):-1:1
161 [xi, yi, zi] = meshgrid (sx(i), squeeze (y(:,1,1)), squeeze (z(1,1,:)));
162 vx = squeeze (interp3 (x, y, z, v, xi, yi, zi, method));
163 tmp(sidx++) = surface (squeeze (sx(i) * ones (size (zi))),
164 squeeze (yi), squeeze(zi), vx);
168 vi = interp3 (x, y, z, v, sx, sy, sz);
169 tmp = surface (sx, sy, sz, vi);
173 set (ax, "view", [-37.5, 30.0], "box", "off", "xgrid", "on",
174 "ygrid", "on", "zgrid", "on");
186 %! [x, y, z] = meshgrid (linspace (-8, 8, 32));
187 %! v = sin (sqrt (x.^2 + y.^2 + z.^2)) ./ (sqrt (x.^2 + y.^2 + z.^2));
188 %! slice (x, y, z, v, [], 0, []);
192 %! [x, y, z] = meshgrid (linspace (-8, 8, 32));
193 %! v = sin (sqrt (x.^2 + y.^2 + z.^2)) ./ (sqrt (x.^2 + y.^2 + z.^2));
194 %! [xi, yi] = meshgrid (linspace (-7, 7));
196 %! slice (x, y, z, v, xi, yi, zi);