~ubuntu-branches/debian/sid/octave3.0/sid

« back to all changes in this revision

Viewing changes to scripts/plot/slice.m

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere
  • Date: 2007-12-23 16:04:15 UTC
  • Revision ID: james.westby@ubuntu.com-20071223160415-n4gk468dihy22e9v
Tags: upstream-3.0.0
ImportĀ upstreamĀ versionĀ 3.0.0

Show diffs side-by-side

added added

removed removed

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