]> Creatis software - CreaPhase.git/blob - octave_packages/m/geometry/griddatan.m
update packages
[CreaPhase.git] / octave_packages / m / geometry / griddatan.m
1 ## Copyright (C) 2007-2012 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} {@var{yi} =} griddatan (@var{x}, @var{y}, @var{xi}, @var{method}, @var{options})
21 ##
22 ## Generate a regular mesh from irregular data using interpolation.
23 ## The function is defined by @code{@var{y} = f (@var{x})}.
24 ## The interpolation points are all @var{xi}.
25 ##
26 ## The interpolation method can be @code{"nearest"} or @code{"linear"}.
27 ## If method is omitted it defaults to @code{"linear"}.
28 ## @seealso{griddata, delaunayn}
29 ## @end deftypefn
30
31 ## Author: David Bateman <dbateman@free.fr>
32
33 function yi = griddatan (x, y, xi, method, varargin)
34
35   if (nargin == 3)
36     method = "linear";
37   endif
38   if (nargin < 3)
39     print_usage ();
40   endif
41
42   if (ischar (method))
43     method = tolower (method);
44   endif
45
46   [m, n] = size (x);
47   [mi, ni] = size (xi);
48
49   if (n != ni || size (y, 1) != m || size (y, 2) != 1)
50     error ("griddatan: dimensional mismatch");
51   endif
52
53   ## triangulate data
54   ## tri = delaunayn(x, varargin{:});
55   tri = delaunayn (x);
56
57   yi = NaN (mi, 1);
58
59   if (strcmp (method, "nearest"))
60     ## search index of nearest point
61     idx = dsearchn (x, tri, xi);
62     valid = !isnan (idx);
63     yi(valid) = y(idx(valid));
64
65   elseif (strcmp (method, "linear"))
66     ## search for every point the enclosing triangle
67     [tri_list, bary_list] = tsearchn (x, tri, xi);
68
69     ## only keep the points within triangles.
70     valid = !isnan (tri_list);
71     tri_list = tri_list(!isnan (tri_list));
72     bary_list = bary_list(!isnan (tri_list), :);
73     nr_t = rows (tri_list);
74
75     ## assign x,y for each point of simplex
76     xt =  reshape (x(tri(tri_list,:),:), [nr_t, n+1, n]);
77     yt = y(tri(tri_list,:));
78
79     ## Use barycentric coordinate of point to calculate yi
80     yi(valid) = sum (y(tri(tri_list,:)) .* bary_list, 2);
81
82   else
83     error ("griddatan: unknown interpolation METHOD");
84   endif
85
86 endfunction
87
88 %!testif HAVE_QHULL
89 %! [xx,yy] = meshgrid(linspace(-1,1,32));
90 %! xi = [xx(:), yy(:)];
91 %! x = (2 * rand(100,2) - 1);
92 %! x = [x;1,1;1,-1;-1,-1;-1,1];
93 %! y = sin(2*(sum(x.^2,2)));
94 %! zz = griddatan(x,y,xi,'linear');
95 %! zz2 = griddata(x(:,1),x(:,2),y,xi(:,1),xi(:,2),'linear');
96 %! assert (zz, zz2, 1e-10)
97
98 %!testif HAVE_QHULL
99 %! [xx,yy] = meshgrid(linspace(-1,1,32));
100 %! xi = [xx(:), yy(:)];
101 %! x = (2 * rand(100,2) - 1);
102 %! x = [x;1,1;1,-1;-1,-1;-1,1];
103 %! y = sin(2*(sum(x.^2,2)));
104 %! zz = griddatan(x,y,xi,'nearest');
105 %! zz2 = griddata(x(:,1),x(:,2),y,xi(:,1),xi(:,2),'nearest');
106 %! assert (zz, zz2, 1e-10)