7183
|
1 ## Copyright (C) 2007 Kai Habel, David Bateman |
|
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 -*- |
7184
|
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 3D 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. |
7183
|
35 ## |
|
36 ## If @var{x}, @var{y}, @var{z} are omitted, they are assumed to be |
7184
|
37 ## @code{x = 1:size (@var{v}, 2)}, @code{y = 1:size (@var{v}, 1)} and |
|
38 ## @code{z = 1:size (@var{v}, 3)}. |
7183
|
39 ## |
|
40 ## @var{Method} is one of: |
|
41 ## |
7184
|
42 ## @table @code |
|
43 ## @item "nearest" |
7183
|
44 ## Return the nearest neighbour. |
7184
|
45 ## @item "linear" |
7183
|
46 ## Linear interpolation from nearest neighbours. |
7184
|
47 ## @item "cubic" |
7183
|
48 ## Cubic interpolation from four nearest neighbours (not implemented yet). |
7184
|
49 ## @item "spline" |
|
50 ## Cubic spline interpolation---smooth first and second derivatives |
7183
|
51 ## throughout the curve. |
|
52 ## @end table |
|
53 ## |
7184
|
54 ## The default method is @code{"linear"}. |
|
55 ## The optional return value @var{h} is a vector of handles to the |
|
56 ## surface graphic objects. |
7183
|
57 ## |
|
58 ## Examples: |
|
59 ## @example |
7184
|
60 ## [x, y, z] = meshgrid (linspace (-8, 8, 32)); |
|
61 ## v = sin (sqrt (x.^2 + y.^2 + z.^2)) ./ (sqrt (x.^2 + y.^2 + z.^2)); |
|
62 ## slice (x, y, z, v, [], 0, []); |
|
63 ## [xi, yi] = meshgrid (linspace (-7, 7)); |
|
64 ## zi = xi + yi; |
|
65 ## slice (x, y, z, v, xi, yi, zi); |
7183
|
66 ## @end example |
|
67 ## @seealso{interp3, surface, pcolor} |
|
68 ## @end deftypefn |
|
69 |
7184
|
70 ## Author: Kai Habel <kai.habel@gmx.de> |
7183
|
71 |
7184
|
72 function h = slice (varargin) |
7183
|
73 |
|
74 method = "linear"; |
|
75 extrapval = NA; |
|
76 nargs = nargin; |
|
77 |
|
78 if (ischar (varargin{end})) |
|
79 method = varargin{end}; |
|
80 nargs -= 1; |
|
81 endif |
|
82 |
|
83 if (nargs == 4) |
7184
|
84 v = varargin{1}; |
|
85 if (ndims (v) != 3) |
7183
|
86 error ("slice: expect 3-dimensional array of values"); |
|
87 endif |
7184
|
88 [nx, ny, nz] = size (v); |
|
89 [x, y, z] = meshgrid (1:nx, 1:ny, 1:nz); |
7183
|
90 sx = varargin{2}; |
|
91 sy = varargin{3}; |
|
92 sz = varargin{4}; |
|
93 elseif (nargs == 7) |
7184
|
94 v = varargin{4}; |
|
95 if (ndims (v) != 3) |
7183
|
96 error ("slice: expect 3-dimensional array of values"); |
|
97 endif |
7184
|
98 x = varargin{1}; |
|
99 y = varargin{2}; |
|
100 z = varargin{3}; |
|
101 if (all ([isvector(x), isvector(y), isvector(z)])) |
|
102 [x, y, z] = meshgrid (x, y, z); |
|
103 elseif (ndims (x) == 3 && size_equal (x, y) && size_equal (x, z)) |
|
104 ## Do nothing. |
7183
|
105 else |
7184
|
106 error ("slice: X, Y, Z size mismatch") |
7183
|
107 endif |
|
108 sx = varargin{5}; |
|
109 sy = varargin{6}; |
|
110 sz = varargin{7}; |
|
111 else |
7184
|
112 print_usage (); |
7183
|
113 endif |
|
114 |
7184
|
115 if (any ([isvector(sx), isvector(sy), isvector(sz)])) |
|
116 have_sval = true; |
|
117 elseif (ndims(sx) == 2 && size_equal (sx, sy) && size_equal (sx, sz)) |
|
118 have_sval = false; |
7183
|
119 else |
7184
|
120 error ("slice: dimensional mismatch for (XI, YI, ZI) or (SX, SY, SZ)"); |
7183
|
121 endif |
|
122 |
|
123 newplot (); |
7184
|
124 ax = gca (); |
7183
|
125 sidx = 1; |
7184
|
126 maxv = max (v(:)); |
|
127 minv = min (v(:)); |
|
128 set (ax, "clim", [minv, maxv]); |
7183
|
129 |
|
130 if (have_sval) |
7184
|
131 ns = length (sx) + length (sy) + length (sz); |
7183
|
132 hs = zeros(ns,1); |
7184
|
133 [ny, nx, nz] = size (v); |
7183
|
134 if (length(sz) > 0) |
7184
|
135 for i = 1:length(sz) |
|
136 [xi, yi, zi] = meshgrid (squeeze (x(1,:,1)), |
|
137 squeeze (y(:,1,1)), sz(i)); |
|
138 vz = squeeze (interp3 (x, y, z, v, xi, yi, zi, method)); |
|
139 tmp(sidx++) = surface (xi, yi, sz(i) * ones (size (yi)), vz); |
7183
|
140 endfor |
|
141 endif |
|
142 |
7184
|
143 if (length (sy) > 0) |
|
144 for i = length(sy):-1:1 |
|
145 [xi, yi, zi] = meshgrid (squeeze (x(1,:,1)), sy(i), squeeze (z(1,1,:))); |
|
146 vy = squeeze (interp3 (x, y, z, v, xi, yi, zi, method)); |
|
147 tmp(sidx++) = surface (squeeze (xi), |
|
148 squeeze (sy(i) * ones (size (zi))), |
|
149 squeeze (zi), vy); |
7183
|
150 endfor |
|
151 endif |
|
152 |
7184
|
153 if (length (sx) > 0) |
|
154 for i = length(sx):-1:1 |
|
155 [xi, yi, zi] = meshgrid (sx(i), squeeze (y(:,1,1)), squeeze (z(1,1,:))); |
|
156 vx = squeeze (interp3 (x, y, z, v, xi, yi, zi, method)); |
|
157 tmp(sidx++) = surface (squeeze (sx(i) * ones (size (zi))), |
|
158 squeeze (yi), squeeze(zi), vx); |
7183
|
159 endfor |
|
160 endif |
|
161 else |
7184
|
162 vi = interp3 (x, y, z, v, sx, sy, sz); |
|
163 tmp(sidx++) = surface (sx, sy, sz, vi); |
7183
|
164 endif |
|
165 |
|
166 if (! ishold ()) |
|
167 set (ax, "view", [-37.5, 30.0]); |
|
168 endif |
|
169 |
|
170 if (nargout > 0) |
|
171 h = tmp; |
|
172 endif |
|
173 |
7184
|
174 endfunction |