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