6823
|
1 ## Copyright (C) 1999,2000 Kai Habel |
|
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 2, or (at your option) |
|
8 ## 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, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {@var{zi} =} griddata (@var{x}, @var{y}, @var{z}, @var{xi}, @var{yi}, @var{method}) |
|
22 ## @deftypefnx {Function File} {[@var{xi}, @var{yi}, @var{zi}] =} griddata (@var{x}, @var{y}, @var{z}, @var{xi}, @var{yi}, @var{method}) |
|
23 ## |
|
24 ## Generate a regular mesh from irregular data using interpolation. |
|
25 ## The function is defined by @code{@var{z} = f (@var{x}, @var{y})}. |
|
26 ## The interpolation points are all @code{(@var{xi}, @var{yi})}. If |
|
27 ## @var{xi}, @var{yi} are vectors then they are made into a 2D mesh. |
|
28 ## |
|
29 ## The interpolation method can be 'nearest', 'cubic' or 'linear'. |
|
30 ## If method is omitted it defaults to 'linear'. |
|
31 ## @seealso{delaunay} |
|
32 ## @end deftypefn |
|
33 |
|
34 ## Author: Kai Habel <kai.habel@gmx.de> |
|
35 ## Adapted-by: Alexander Barth <barth.alexander@gmail.com> |
|
36 ## xi and yi are not "meshgridded" if both are vectors |
|
37 ## of the same size (for compatibility) |
|
38 |
|
39 function [rx, ry, rz] = griddata (x,y,z,xi,yi,method) |
|
40 |
|
41 if (nargin == 5) |
|
42 method="linear"; |
|
43 endif |
|
44 if (nargin < 5 || nargin > 7) |
|
45 print_usage(); |
|
46 endif |
|
47 |
|
48 if (ischar(method)) |
|
49 method=tolower(method); |
|
50 endif |
|
51 if (!all( (size(x)==size(y)) & (size(x)==size(z)))) |
|
52 error("griddata: x,y,z must be vectors of same length"); |
|
53 endif |
|
54 |
|
55 ## meshgrid xi and yi if they are vectors unless they |
|
56 ## are vectors of the same length |
|
57 if (isvector(xi) && isvector(yi) && numel(xi) ~= numel(yi)) |
|
58 [xi,yi]=meshgrid(xi,yi); |
|
59 endif |
|
60 |
|
61 if (any(size(xi) != size(yi))) |
|
62 error("griddata: xi and yi must be vectors or matrices of same size"); |
|
63 endif |
|
64 |
|
65 [nr,nc] = size(xi); |
|
66 |
|
67 ## triangulate data |
|
68 tri = delaunay(x,y); |
|
69 zi = nan(size(xi)); |
|
70 |
|
71 if strcmp(method,"cubic") |
|
72 error("griddata(...,'cubic') cubic interpolation not yet implemented\n") |
|
73 |
|
74 elseif strcmp(method,'nearest') |
|
75 ## search index of nearest point |
|
76 idx = dsearch(x,y,tri,xi,yi); |
|
77 valid = !isnan(idx); |
|
78 zi(valid) = z(idx(valid)); |
|
79 |
|
80 elseif strcmp(method,'linear') |
|
81 ## search for every point the enclosing triangle |
|
82 tri_list= tsearch(x,y,tri,xi(:),yi(:)); |
|
83 |
|
84 ## only keep the points within triangles. |
|
85 valid = !isnan(reshape(tri_list,size(xi))); |
|
86 tri_list = tri_list(!isnan(tri_list)); |
|
87 nr_t = rows(tri_list); |
|
88 |
|
89 ## assign x,y,z for each point of triangle |
|
90 x1 = x(tri(tri_list,1));y1=y(tri(tri_list,1));z1=z(tri(tri_list,1)); |
|
91 x2 = x(tri(tri_list,2));y2=y(tri(tri_list,2));z2=z(tri(tri_list,2)); |
|
92 x3 = x(tri(tri_list,3));y3=y(tri(tri_list,3));z3=z(tri(tri_list,3)); |
|
93 |
|
94 ## calculate norm vector |
|
95 N = cross([x2-x1, y2-y1, z2-z1],[x3-x1, y3-y1, z3-z1]); |
|
96 N_norm = sqrt(sumsq(N,2)); |
|
97 N = N ./ N_norm(:,[1,1,1]); |
|
98 |
|
99 ## calculate D of plane equation |
|
100 ## Ax+By+Cz+D=0; |
|
101 D = -(N(:,1) .* x1 + N(:,2) .* y1 + N(:,3) .* z1); |
|
102 |
|
103 ## calculate zi by solving plane equation for xi,yi |
|
104 zi(valid) = -(N(:,1).*xi(valid) + N(:,2).*yi(valid) + D ) ./ N(:,3); |
|
105 |
|
106 else |
|
107 error("griddata: unknown interpolation method"); |
|
108 endif |
|
109 |
|
110 if nargout == 3 |
|
111 rx = xi; |
|
112 ry = yi; |
|
113 rz = zi; |
|
114 elseif nargout == 1 |
|
115 rx = zi; |
|
116 elseif nargout == 0 |
|
117 mesh(xi, yi, zi); |
|
118 endif |
|
119 endfunction |
|
120 |
|
121 %!test |
|
122 %! [xx,yy]=meshgrid(linspace(-1,1,32)); |
|
123 %! x = xx(:); |
|
124 %! x = x + 10 * (2 * round(rand(size(x))) - 1) * eps; |
|
125 %! y = yy(:); |
|
126 %! y = y + 10 * (2 * round(rand(size(y))) - 1) * eps; |
|
127 %! z = sin(2*(x.^2+y.^2)); |
|
128 %! zz = griddata(x,y,z,xx,yy,'linear'); |
|
129 %! zz2 = sin(2*(xx.^2+yy.^2)); |
|
130 %! zz2(isnan(zz)) = NaN; |
|
131 %! assert (zz, zz2, 100 * eps) |
|
132 |
|
133 %!demo |
|
134 %! x=2*rand(100,1)-1; |
|
135 %! y=2*rand(size(x))-1; |
|
136 %! z=sin(2*(x.^2+y.^2)); |
|
137 %! [xx,yy]=meshgrid(linspace(-1,1,32)); |
|
138 %! griddata(x,y,z,xx,yy); |
|
139 %! title('nonuniform grid sampled at 100 points'); |
|
140 |
|
141 %!demo |
|
142 %! x=2*rand(1000,1)-1; |
|
143 %! y=2*rand(size(x))-1; |
|
144 %! z=sin(2*(x.^2+y.^2)); |
|
145 %! [xx,yy]=meshgrid(linspace(-1,1,32)); |
|
146 %! griddata(x,y,z,xx,yy); |
|
147 %! title('nonuniform grid sampled at 1000 points'); |
|
148 |
|
149 %!demo |
|
150 %! x=2*rand(1000,1)-1; |
|
151 %! y=2*rand(size(x))-1; |
|
152 %! z=sin(2*(x.^2+y.^2)); |
|
153 %! [xx,yy]=meshgrid(linspace(-1,1,32)); |
|
154 %! griddata(x,y,z,xx,yy,'nearest'); |
|
155 %! title('nonuniform grid sampled at 1000 points with nearest neighbor'); |