├── LICENSE ├── Makefile ├── README.md ├── anc ├── adaptiveNC.m ├── donc.m ├── mex │ ├── anisotropic_filter │ │ ├── Makefile │ │ ├── anisoDouble.c │ │ ├── anisoDouble.h │ │ ├── anisoDoubleFilter.c │ │ ├── anisoDoubleFilter.mexglx │ │ ├── anisoDoubleFilter.mexmaci │ │ ├── common.c │ │ ├── common.h │ │ └── defs.h │ └── gst_eigenvalue │ │ ├── Makefile │ │ ├── common.c │ │ ├── common.h │ │ ├── defs.h │ │ ├── gstEig.c │ │ ├── gstEig.mexglx │ │ └── gstEig.mexmaci ├── ndc.m └── private │ ├── fastGenerateGradients.m │ ├── gauss.m │ ├── gauss2.m │ ├── gstAnisotropy.m │ ├── gstEigenvalues.m │ ├── gstOrientations.m │ ├── gstSigmas.m │ ├── ndcGaussMask.m │ └── sepConv2.m ├── contents.py ├── kriging └── kriging.m ├── natural_neighbour ├── .indent.pro ├── CHANGELOG ├── CUSTOMISE ├── README ├── config.h ├── config.h.in ├── config.log ├── config.status ├── configure ├── configure.in ├── delaunay.c ├── delaunay.h ├── examples │ ├── 1 │ │ ├── README │ │ ├── data-100.txt │ │ ├── data-1000.txt │ │ ├── data-300.txt │ │ ├── generate.awk │ │ ├── makefile │ │ ├── out-100.txt │ │ ├── out-1000.txt │ │ ├── out-300.txt │ │ ├── suptitle.m │ │ ├── test.sh │ │ └── viewexample.m │ ├── 2 │ │ ├── README │ │ ├── data.txt │ │ ├── makefile │ │ ├── suptitle.m │ │ ├── test.sh │ │ ├── viewdata.m │ │ ├── viewexample.m │ │ └── viewinterp.m │ ├── 3 │ │ ├── README │ │ ├── generate-data.awk │ │ ├── generate-points.awk │ │ ├── makefile │ │ └── test.sh │ ├── 4 │ │ ├── README │ │ ├── data.txt │ │ ├── makefile │ │ ├── test.sh │ │ ├── viewdata.m │ │ ├── viewexample.m │ │ └── viewinterp.m │ ├── 5 │ │ ├── README │ │ ├── data.txt │ │ ├── makefile │ │ ├── test.sh │ │ ├── viewexample.m │ │ ├── viewinterp.m │ │ └── viewinterp2.m │ ├── 6 │ │ ├── README │ │ ├── data.txt │ │ ├── makefile │ │ ├── test.sh │ │ └── viewinterp.m │ └── README ├── hash.c ├── hash.h ├── install-sh ├── istack.c ├── istack.h ├── libnn.dvi ├── libnn.log ├── lpi.c ├── makefile ├── makefile.example ├── makefile.in ├── mex_nn.c ├── mex_nn.mexmaci ├── minell.c ├── minell.dSYM │ └── Contents │ │ ├── Info.plist │ │ └── Resources │ │ └── DWARF │ │ └── minell ├── minell.h ├── mkinstalldirs ├── nan.h ├── natural_neighbour.m ├── nn.h ├── nn_internal.h ├── nnai.c ├── nnbathy.c ├── nnbathy.dSYM │ └── Contents │ │ ├── Info.plist │ │ └── Resources │ │ └── DWARF │ │ └── nnbathy ├── nncommon-vulnerable.c ├── nncommon.c ├── nnconfig.h.in ├── nnpi.c ├── preader.c ├── preader.h ├── tags ├── test ├── triangle.c ├── triangle.h └── version.h ├── rbf └── rbf.m └── simulation ├── corr_data.m └── corr_data_uni.m /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2006-2009 Matt Foster 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 | * Neither the name of the Matt Foster nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 9 | 10 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 11 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 12 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 13 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 14 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 15 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 16 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 17 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 18 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 19 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 20 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Matlab interpolation kit 2 | 3 | # This may need setting manually 4 | # EXT = /usr/local/matlab2006b/bin/mexext 5 | # EXT = /Applications/MATLAB_R2008b.app/bin/mexext 6 | 7 | all: anc natural_neighbour toolkit contents 8 | 9 | anc: 10 | make -C anc/mex/gst_eigenvalue all 11 | make -C anc/mex/anisotropic_filter all 12 | 13 | natural_neighbour: 14 | $(MAKE) -C natural_neighbour 15 | 16 | toolkit: 17 | mkdir -p toolkit/private 18 | find . -path './toolkit' -prune -o -name '*.mex*' -exec cp {} toolkit/private \; 19 | find . -path './toolkit' -prune -o -maxdepth 2 -name '*.m' -exec cp {} toolkit \; 20 | cp anc/private/*.m toolkit/private/ 21 | 22 | contents: toolkit 23 | python contents.py 24 | 25 | clean: 26 | $(MAKE) -C anc/mex/gst_eigenvalue clean 27 | $(MAKE) -C anc/mex/anisotropic_filter clean 28 | $(MAKE) -C natural_neighbour clean 29 | rm -r toolkit 30 | 31 | .PHONY: clean anc natural_neighbour toolkit 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MATLAB Multivariate Interpolation Toolbox 2 | ========================================= 3 | 4 | This toolbox contains code for 2D multivariate interpolation in MATLAB. 5 | 6 | Contents 7 | ======== 8 | 9 | * Adaptive Normalised Convolution (ANC) 10 | * Radial Basis Function Interpolation (RBF) 11 | * Kriging 12 | * Natural Neighbour Interpolation 13 | * The included Natural Neighbours interpolation software was provided by 14 | Pavel Sakov and is available at URL: 15 | http://www.marine.csiro.au/~sakov/nn.tar.gz 16 | 17 | Unless otherwise stated, all code is Copyright Matt Foster 18 | 19 | Installation 20 | ============ 21 | 22 | Some functions require MEX functions to built. To compile all of the necessary 23 | Makefiles in UNIX, type `make`. Some of the Makefiles may require alterations 24 | for your system, however, provided you have a complete build environment, and 25 | the binaries `mex` and `mexext` are available, the process should be fairly 26 | simple. You should then find a directory named `toolkit`. Copy this directory somewhere in your MATLAB work area, and finally, from within MATLAB, issue the command: 27 | 28 | addpath('interpolation_toolkit'); 29 | 30 | It is not necessary to use `genpath`, or include the `private` directory 31 | 32 | Usage 33 | ===== 34 | 35 | With the exception of ANC, which interpolates onto matrices, all of the 36 | functions provided have prototypes of the following form: 37 | 38 | [xx, yy, zz] = function(xi, yi, zi, xx, yy, [optional args]) 39 | 40 | or more simply 41 | 42 | zz = function(xi, yi, zi, xx, yy, [optional args]) 43 | 44 | The help within the various functions gives more information on the input and 45 | output arguments. For example, issuing the command: 46 | 47 | help rbf 48 | 49 | Will print the built in help for the RBF interpolation command. 50 | 51 | Running the command: 52 | 53 | help interpolation_toolkit 54 | 55 | Where `interpolation_toolkit` is the directory name, will give basic help for the toolkit, including some usage instructions. 56 | 57 | ANC 58 | === 59 | 60 | ANC operates using matrices, and requires arguments of the following form: 61 | 62 | out = adaptiveNC(si, cm, [optional args]) 63 | 64 | Full help is available within the m-file. 65 | 66 | Bugs 67 | ==== 68 | 69 | Please report bugs to Matt Foster 70 | 71 | History 72 | ======= 73 | 74 | Moved from local SVN to GitHub: 20090120 75 | -------------------------------------------------------------------------------- /anc/adaptiveNC.m: -------------------------------------------------------------------------------- 1 | function [op, time, inform] = adaptiveNC(si, cm, varargin) 2 | % Interpolation using adaptive GST based normalised convolution (ANC) 3 | % [op, time] = adaptiveNC(si, cm, varargin) 4 | % 5 | % Required arguments: 6 | % si = sampled image. 7 | % cm = confidence map. 8 | % Optional arguments: 9 | % (if left unspecified, defaults will be used): 10 | % 'pad_amt' = Amount to pad data. 11 | % 'max_s' = Maximum filter standard deviation. 12 | % 'min_s' = Minimum filter standard deviation. 13 | % 'max_m' = Maximum DoNC/NDC filter dimension. 14 | % 'c' = Standard deviation multiplier. 15 | % 'alpha' = Standard deviation exponent. 16 | % 'anisoFiltSize' = Anisotropy filter standard deviation. 17 | % 'gradFiltSize' = Gradient filter standard deviation. 18 | % 'lamFiltSize' = Eigenvalue filter standard deviation. 19 | % 'sigFiltSize' = Standard deviation filter standard deviation(!). 20 | % 'phiFiltSize' = Orientation filter standard deviation. 21 | % 'gradType' = Type of filter to use for estimating gradients: 22 | % 'donc' or 'ndc' 23 | % 'gradFiltType' = DONC filter type: 24 | % can be 'gauss', 'gauss_sig' or 'ndc'. 25 | % 'verbose' = Increase verbosity. 26 | % 'type' = What type of adaptive filtering to perform: 27 | % 'shape + scale' or 'scale'. 28 | % Output Arguments: 29 | % op = Output data. 30 | % time = Time taken to process data. 31 | % 32 | % See Also: 33 | % griddata, kriging, rbf, natural_neighbour 34 | % 35 | % Matt Foster 36 | 37 | % Linted: 20060120 / 20090120 38 | 39 | tic; 40 | 41 | % Defaults: 42 | opts = struct( 'pad_amt' , 0, ... 43 | 'max_s' , 50, ... 44 | 'min_s' , 1.7, ... 45 | 'max_m' , 199, ... 46 | 'c' , 1, ... 47 | 'alpha' , 1.1, ... 48 | 'anisoFiltSize' , 41, ... 49 | 'gradFiltSize' , 201, ... 50 | 'lamFiltSize' , 0, ... 51 | 'sigFiltSize' , 0, ... 52 | 'phiFiltSize' , 0, ... 53 | 'gradFiltType' , 'gauss', ... 54 | 'verbose' , 0, ... 55 | 'gradType' , 'donc', ... 56 | 'type' , 'shape + scale', ... 57 | 'padding' , 'symmetric' ); 58 | 59 | % Note: using struct here makes this function incompatible with octave 60 | error(nargchk(2, 30, nargin, 'struct')); 61 | 62 | % Parse the input arguments: 63 | opts = args(varargin, opts, mfilename); 64 | 65 | if sum(cm(:)) < 5 66 | warning('ANC:NotEnoughInputData', 'Input is very sparse'); 67 | op = zeros(size(si)); 68 | time = toc; 69 | return; 70 | end 71 | 72 | % Check for overridden pad_amt, and calculate if it hasn't been changed. 73 | if ~opts.pad_amt 74 | opts.pad_amt = 7.* opts.max_s; 75 | elseif opts.pad_amt < 6 * opts.max_s; 76 | warning('ANC:InsufficientPadding', 'Padding is probably too small.'); 77 | end 78 | 79 | if opts.verbose 80 | fprintf(1, 'Padding: %d\n', opts.pad_amt); 81 | end 82 | 83 | % Image size 84 | [rows, cols] = size(si); 85 | 86 | % Padding: 87 | si = padarray(si, [opts.pad_amt, opts.pad_amt], opts.padding); 88 | cm = padarray(cm, [opts.pad_amt, opts.pad_amt], opts.padding); 89 | 90 | % Calculate bwdist: 91 | sig_a = bwdist(cm); 92 | sig_a(sig_a == 0) = 1; 93 | 94 | if strfind(opts.type, 'shape + scale') 95 | % Find filter size for donc. 96 | m_dist = ceil(max(max(sig_a)) + 2); 97 | if rem(m_dist, 2) == 0 98 | m_dist = m_dist + 5; 99 | end 100 | 101 | m_dist(m_dist > opts.max_m) = opts.max_m; 102 | 103 | if strfind(opts.gradType, 'donc') 104 | if opts.verbose; 105 | fprintf(1, 'Running DoNC Edge detection. '); 106 | fprintf(1, 'Filter size: %d\n', m_dist); 107 | end 108 | [gx, gy] = donc(si, cm, m_dist, opts.gradFiltType); 109 | elseif strfind(opts.gradType, 'ndc') 110 | if opts.verbose 111 | fprintf(1, 'Running NDC Edge detection. '); 112 | fprintf(1, 'Filter size: %d\n', m_dist); 113 | end 114 | [gx, gy] = ndc(si, cm, m_dist, opts.gradFiltType); 115 | else 116 | error('Misunderstood edge detector type.'); 117 | end 118 | 119 | if opts.verbose; 120 | fprintf(1, 'Gradient Filter Size: %d.\n', opts.gradFiltSize); 121 | fprintf(1, 'Generating gradient products.\n'); 122 | end 123 | 124 | [gxy, gx2, gy2] = fastGenerateGradients(gx, gy, opts.gradFiltSize); 125 | 126 | if opts.verbose 127 | fprintf(1, 'Computing eigenvalues.\n'); 128 | end 129 | 130 | [lam_1, lam_2] = gstEig(gxy, gx2, gy2); 131 | % Don't want to use the mex function? uncomment this: 132 | %[lam_1, lam_2] = gstEigenvalues(gxy, gx2, gy2); 133 | 134 | if opts.lamFiltSize 135 | if opts.verbose 136 | fprintf(1, 'Filtering Eigenvalues\n'); 137 | end 138 | lf = gauss(opts.lamFiltSize, 'dim'); 139 | lam_1 = sepConv2(lam_1, lf); 140 | lam_2 = sepConv2(lam_2, lf); 141 | end 142 | 143 | if opts.verbose 144 | fprintf(1, 'Computing anisotropy.\n'); 145 | end 146 | aniso = gstAnisotropy(lam_1, lam_2); 147 | 148 | if opts.anisoFiltSize 149 | if opts.verbose 150 | fprintf(1, 'Filtering anisotropy.\n'); 151 | end 152 | af = gauss(opts.anisoFiltSize, 'dim'); 153 | aniso = sepConv2(aniso, af); 154 | end 155 | 156 | if opts.verbose; 157 | fprintf(1, 'Computing stdevs. c = %3.2f, alpha = %3.2f\n', ... 158 | opts.c, opts.alpha); 159 | end 160 | 161 | [sig_u, sig_v] = gstSigmas(aniso, sig_a, opts.max_s, ... 162 | opts.min_s, opts.c, opts.alpha); 163 | 164 | clear aniso; 165 | 166 | if opts.sigFiltSize 167 | if opts.verbose 168 | fprintf(1, 'Filtering Sigma values.\n'); 169 | end 170 | sf = gauss(opts.anisoFiltSize, 'dim'); 171 | sig_u = sepConv2(sig_u, sf); 172 | sig_v = sepConv2(sig_v, sf); 173 | end 174 | 175 | if opts.verbose 176 | fprintf(1, 'Computing orientations.\n'); 177 | end 178 | 179 | phi_2 = atan(gxy ./ (lam_2 - gy2)); 180 | phi_2(isnan(phi_2)) = 0; 181 | 182 | if opts.phiFiltSize 183 | if opts.verbose 184 | fprintf(1, 'Filtering Phi values.\n'); 185 | end 186 | pf = gauss(opts.phiFiltSize, 'dim'); 187 | phi_2 = sepConv2(phi_2, pf); 188 | end 189 | 190 | sig_u = sig_u(opts.pad_amt:opts.pad_amt+rows-1, ... 191 | opts.pad_amt:opts.pad_amt+cols-1); 192 | sig_v = sig_v(opts.pad_amt:opts.pad_amt+rows-1, ... 193 | opts.pad_amt:opts.pad_amt+cols-1); 194 | phi_2 = phi_2(opts.pad_amt:opts.pad_amt+rows-1, ... 195 | opts.pad_amt:opts.pad_amt+cols-1); 196 | end 197 | 198 | % Remove Padding: 199 | sig_a = sig_a(opts.pad_amt:opts.pad_amt+rows-1, ... 200 | opts.pad_amt:opts.pad_amt+cols-1); 201 | 202 | % Construct filter arguments: 203 | if strfind(opts.type, 'shape + scale') 204 | sig_x = sig_v; 205 | sig_y = sig_u; 206 | phi = phi_2; 207 | else 208 | sig_x = sig_a; 209 | sig_y = sig_x; 210 | phi = zeros(size(sig_x)); % Cheat. 211 | end 212 | 213 | % Preallocate memory for outputs: 214 | op_1 = zeros(rows, cols); 215 | op_2 = zeros(rows, cols); 216 | 217 | op = nan; 218 | 219 | % check for nans 220 | iterations = 1; 221 | mult = 1:0.1:5; 222 | 223 | while any(isnan(op(:))) && iterations <= length(mult) 224 | % Non-looping mex function. 225 | [op_1, op_2] = anisoDoubleFilter(double(si), double(cm), ... 226 | sig_y*mult(iterations), sig_x*mult(iterations), phi, opts.pad_amt); 227 | % Normalise the convolutions. 228 | op = op_1./op_2; 229 | iterations = iterations + 1; 230 | end 231 | 232 | inform.iterations = iterations; 233 | inform.mult = mult(iterations-1); 234 | 235 | %op(isnan(op)) = op_2(isnan(op)); 236 | 237 | if any(isnan(op(:))) 238 | warning('ANC:GappyOutput', 'Output probably contains gaps') 239 | end 240 | 241 | time = toc; 242 | 243 | end % of ANC 244 | 245 | %---------------------------------------------------- 246 | % Utility subfunctions 247 | %---------------------------------------------------- 248 | function [sig_u, sig_v] = gstSigmas(aniso, sig_a, max_s, min_s, c, alpha) 249 | % function [sig_u, sig_v] = gstSigmas(aniso, sig_a, min_s, c, alpha) 250 | % Compute gst standard deviations. 251 | 252 | if nargin < 5 253 | c = 0.5; 254 | end 255 | if nargin < 6 256 | alpha = 0.5; 257 | end 258 | 259 | sig_u = c.*(1+aniso).^alpha.*sig_a; 260 | sig_v = c.*(1-aniso).^alpha.*sig_a; 261 | 262 | sig_u(isnan(sig_u)) = 0; 263 | sig_v(isnan(sig_v)) = 0; 264 | sig_u(sig_u > max_s) = max_s; 265 | sig_v(sig_v > max_s) = max_s; 266 | sig_u(sig_u < min_s) = min_s; 267 | sig_v(sig_v < min_s) = min_s; 268 | 269 | end % gstSigmas 270 | 271 | function [aniso, energy] = gstAnisotropy(lam_1, lam_2) 272 | % function [aniso, energy] = gstAnisotropy(lam_1, lam_2) 273 | % Compute anisotropy and energy from given eigenvaulues. 274 | 275 | energy = lam_1 + lam_2; 276 | diff = lam_1 - lam_2; 277 | aniso = diff ./ energy; 278 | 279 | aniso(energy == 0) = 0; % necessary? 280 | aniso(diff == 0) = 0; 281 | 282 | end % gstAnisotropy 283 | 284 | % Only used when mex is commented. 285 | function [lam_1, lam_2] = gstEigenvalues(gxy, gx2, gy2) 286 | % function [lam_1, lam_2] = gstEigenValues(gxy, gx2, gy2) 287 | % Create gst eigenvalues, and sort them into lam_1 and lam_2. 288 | 289 | [rr, cc] = size(gxy); 290 | 291 | % Preallocation for speed: 292 | lam_1 = zeros(size(gxy)); 293 | lam_2 = zeros(size(gxy)); 294 | 295 | % Loop, calculating the eigenvalues. 296 | for i = 1:rr 297 | for j = 1:cc 298 | eigenValues = eig([gx2(i,j), gxy(i,j); gxy(i,j), gy2(i,j)]); 299 | lam_1(i,j) = max(eigenValues); 300 | lam_2(i,j) = min(eigenValues); 301 | end 302 | end 303 | 304 | end % gstEigenvlaues 305 | 306 | function opts = args(varargin, opts, scriptname) 307 | % opts = args(varargin, opts, scriptname) 308 | % Parse input arguments into a struct. 309 | 310 | if mod(length(varargin),2) 311 | error('Arguments should be in pairs.'); 312 | end 313 | 314 | % Argument handler. 315 | for i = 1:2:length(varargin) 316 | name = varargin{i}; 317 | value = varargin{i+1}; 318 | 319 | ccmd = sprintf('cls = class(opts.%s);', name); 320 | eval(ccmd); 321 | if ~strcmpi(class(value), cls) 322 | warning('ANC:Arguments:IncorrectClass', ... 323 | 'Wrong class in option %s. Expected %s', ... 324 | name, cls); 325 | end 326 | 327 | % Nice way of accessing struct elements. 328 | opts.(name) = value; 329 | end 330 | 331 | if isfield(opts, 'verbose') 332 | if opts.verbose > 0 333 | fprintf(1, '\n%s options:\n\n', scriptname); 334 | disp(opts) 335 | end 336 | end 337 | 338 | end % args 339 | 340 | -------------------------------------------------------------------------------- /anc/donc.m: -------------------------------------------------------------------------------- 1 | function [gx, gy] = donc(si, cm, r, type, al, be) 2 | % Calculate the derivative of a sampled image using DoNC. 3 | % [gx, gy] = donc(si, cm, r, type, al, be) 4 | % DoNC has a lower complexity than plain NDC, and a slightly lower output 5 | % quality. 6 | % 7 | % Required arguments: 8 | % si = sampled image. 9 | % cm = confidence map. 10 | % Optional arguments: 11 | % r = mask radius (default 11). 12 | % 'type' = set the filter type: 13 | % 'gauss' = gaussian. 14 | % 'kuttson' = Knuttson style raised cosine. 15 | % 'al' = Knuttson filter alpha param. 16 | % 'be' = Knuttson filter beta param. 17 | % 18 | % See also: 19 | % ADAPTIVENC, NDC 20 | % 21 | % Matt Foster 22 | 23 | error(nargchk(2, 6, nargin)); 24 | 25 | if nargin < 3 26 | r = 11; 27 | end 28 | 29 | if nargin < 4 30 | type = 'gauss'; 31 | end 32 | 33 | if strfind(type, 'knuttson') > 0 34 | if nargin < 5 35 | al = 0; 36 | be = 2; 37 | end 38 | % Create a Knuttson mask 39 | [m, m_x, m_y] = ndcMask(al, be, r); 40 | elseif strfind(type, 'gauss') > 0 41 | if strfind(type, 'gauss_sig') > 0 42 | g_type = 'sigma'; 43 | else 44 | g_type = 'dim'; 45 | end 46 | % Create a guassian mask 47 | [m, m_x, m_y] = ndcGaussMask(r, g_type); 48 | else 49 | error('Misunderstood type argument: %s', type); 50 | end 51 | 52 | cm = double(cm); 53 | 54 | nc = conv2(cm, m, 'same'); 55 | cc = conv2(si, m, 'same'); 56 | 57 | c_x = conv2(si, m_x, 'same'); 58 | c_y = conv2(si, m_y, 'same'); 59 | 60 | nc_x = conv2(cm, m_x, 'same'); 61 | nc_y = conv2(cm, m_y, 'same'); 62 | 63 | % Not actually used: 64 | %---------------------------------------------------- 65 | % d_x = nc .* c_x - nc_x .* cc; 66 | % d_y = nc .* c_y - nc_y .* cc; 67 | %---------------------------------------------------- 68 | 69 | gx = (c_x .* nc - nc_x .* cc) ./ (nc.^2); 70 | gy = (c_y .* nc - nc_y .* cc) ./ (nc.^2); 71 | 72 | gx(isnan(gx)) = 0; 73 | gy(isnan(gy)) = 0; 74 | -------------------------------------------------------------------------------- /anc/mex/anisotropic_filter/Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | ## Mex Options: 3 | ## 4 | 5 | # Will probably need customising for your system 6 | 7 | CC = gcc 8 | MEX = /Applications/MATLAB_R2008b.app/bin/mex 9 | DEBUG = -g -O -v 10 | MEXSUFFIX = $(/Applications/MATLAB_R2008b.app/bin/mexext) 11 | 12 | ## 13 | ## Linker/Loader flags 14 | ## 15 | 16 | LDFLAGS = 17 | INCLUDES = -lm 18 | CFLAGS = -g $(INCLUDES) 19 | PROGRAM = anisoDoubleFilter 20 | FUNCTION = $(PROGRAM)$(MEXSUFFIX) 21 | MAKEFILE = Makefile 22 | HDRS = defs.h 23 | SRCS = anisoDouble.c common.c 24 | MAINSRC = $(PROGRAM).c 25 | 26 | # Script to generate headers. 27 | #HEADER = include.sh # requires cproto. 28 | LIBS = $(LOCAL_LIBS) $(SYS_LIBS) 29 | 30 | ## 31 | ## Targets 32 | ## 33 | 34 | #all: include $(FUNCTION) #standalone 35 | all: $(FUNCTION) #standalone 36 | 37 | $(FUNCTION): $(MAKEFILE) 38 | 39 | sources : $(SRCS) $(HDRS) 40 | 41 | include: $(SRCS) 42 | @echo 'Generating headers for $+' 43 | $(HEADER) $+ 44 | 45 | test : $(TESTSCRIPT) $(FUNCTION) 46 | cat $(TESTSCRIPT) 47 | $(MATLAB)/bin/matlab < $(TESTSCRIPT) 48 | 49 | $(FUNCTION): $(MAINSRC) $(SRCS) $(HDRS) Makefile 50 | $(MEX) $(DEBUG) $(CFLAGS) $(LDFLAGS) $(SRCS) $(MAINSRC) \ 51 | $(LIBS) -o $(FUNCTION) 52 | 53 | standalone: main.c $(SRCS) 54 | $(CC) -o anisoGauss -Wall -ansi $(CFLAGS) $(LDFLAGS) $+ 55 | 56 | delmex: 57 | rm -f $(FUNCTION) 58 | 59 | clean : delmex 60 | rm -f *~ core 61 | 62 | tags : sources 63 | etags -t $(SRCS) $(HDRS) 64 | 65 | .PHONY: all sources test delmex full 66 | .PHONY: clean tags 67 | -------------------------------------------------------------------------------- /anc/mex/anisotropic_filter/anisoDouble.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "defs.h" 6 | 7 | /* Create 1/2 a gaussian function, using the given dimension and sigma */ 8 | void createGauss(int dim, double sigma, double g[]) 9 | { 10 | int i; 11 | double e; 12 | 13 | if (DEBUG) 14 | printf("Creating gaussian (size: %d, sig: %2.4f)\n", dim, sigma); 15 | 16 | e = sqrt(2*PI) * sigma; 17 | 18 | for (i = 0; i < dim; i++) { 19 | g[i] = (double) exp(-0.5 * pow((double) i, 2) / pow(sigma, 2)) / e; 20 | } 21 | } 22 | 23 | /* Return x filtered value for a given point. */ 24 | double g(double img[], int x, int y, int cols, int rows, double *g1, int g1Dim) 25 | { 26 | int k; 27 | double out; 28 | 29 | if ( x-g1Dim < 0 || x > cols || y < 0 || y > rows) { 30 | if (DEBUG) printf("Index outside array.\n"); 31 | return 0.0; 32 | } 33 | 34 | out = g1[0]*img[coord(x, y, cols, rows)]; 35 | 36 | for (k = 1; k < g1Dim; k++) { /* Loop over filter */ 37 | out += 38 | g1[k] * ((double)img[coord(x-k, y, cols, rows)] + 39 | (double)img[coord(x+k, y, cols, rows)]); 40 | } 41 | 42 | return out; 43 | } 44 | 45 | void anisoGauss(double img1[], double img2[], int rows, int cols, 46 | int x, int y, double sig_u, double sig_v, double a, 47 | double angle, double *out1, double *out2) 48 | { 49 | 50 | double den, t_phi, phi, sig_x, sig_phi, den2; 51 | double *w, *w1, *g1, *g2; 52 | int i, k, xmfk, xpfk; 53 | int g1Dim, g2Dim; 54 | 55 | /* Compute required parameters: */ 56 | if (0 == angle) { 57 | t_phi = 0; 58 | phi = 0; 59 | sig_phi = sig_v; 60 | sig_x = sig_u; 61 | } else { 62 | den = pow(sig_v, 2) * pow(cos(angle), 2) + 63 | pow(sig_u, 2) * pow(sin(angle), 2); 64 | 65 | den2 = (pow(sig_u, 2) - pow(sig_v, 2)) * 66 | cos(angle)*sin(angle); 67 | 68 | if (fabs(den2) < TOL) 69 | t_phi = 0; 70 | else 71 | t_phi = den/den2; 72 | 73 | phi = atan(t_phi); 74 | 75 | /* Filter standard deviations */ 76 | if (fabs(phi) < TOL) 77 | sig_phi = sig_u; 78 | else 79 | sig_phi = fabs(1/sin(phi) * sqrt(den)); 80 | 81 | sig_x = fabs((sig_u * sig_v) / sqrt(den)); 82 | 83 | sig_x = MIN(sig_x, SIG_CAP); 84 | sig_phi = MIN(sig_phi, SIG_CAP); 85 | 86 | if (DEBUG) { 87 | printf("i: %d, j: %d\n", x, y); 88 | 89 | printf("sig_u: %2.4f, sig_v: %2.4f, sig_x: %2.4f\n", 90 | sig_u, sig_v, sig_x); 91 | 92 | printf("sig_phi: %2.4f, phi: %2.4f, t_phi: %2.4f\n", 93 | sig_phi, phi, t_phi); 94 | } 95 | } 96 | 97 | /* Create gaussians (use 3 sigma + 1 rule)*/ 98 | g1Dim = 3 * sig_x + 1; 99 | g1 = (double *) calloc(g1Dim, sizeof(double)); 100 | createGauss(g1Dim, sig_x, g1); 101 | 102 | g2Dim = 3 * sig_phi + 1; 103 | g2 = (double *) calloc(g2Dim, sizeof(double)); 104 | createGauss(g2Dim, sig_phi, g2); 105 | 106 | if ( x-g1Dim < 0 || y-g2Dim < 0 ) { 107 | /*-------------------------------------------------- 108 | * printf("Error: x and/or y coordinates are too small.\n"); 109 | *--------------------------------------------------*/ 110 | return; 111 | } 112 | 113 | /* Allocate memory for premultiplied factors */ 114 | w = (double *) calloc(g2Dim, sizeof (double)); 115 | w1 = (double *) calloc(g2Dim, sizeof (double)); 116 | 117 | /* Premultiply the interpolation factors */ 118 | for (i = 0; i < g2Dim; i++) { 119 | w[i] = g2[i] * a; 120 | w1[i] = g2[i] * (1-a); 121 | } 122 | 123 | /*-------------------------------------------------- 124 | * Only filter in x... 125 | *--------------------------------------------------*/ 126 | /*-------------------------------------------------- 127 | * out[0] = g(img, x, y, cols, rows, g1, g1Dim); 128 | *--------------------------------------------------*/ 129 | 130 | out1[0] = g2[0]*g(img1, x, y, cols, rows, g1, g1Dim); 131 | out2[0] = g2[0]*g(img2, x, y, cols, rows, g1, g1Dim); 132 | 133 | /* Filter in u */ 134 | for (k = 1; k < g2Dim; k++) { 135 | 136 | if (fabs(t_phi) < TOL) { 137 | xpfk = x; 138 | xmfk = x; 139 | if (DEBUG) 140 | printf("Set interpolation factor to 0\n"); 141 | } else { 142 | xpfk = (int)floor(x + (double)k/t_phi); 143 | xmfk = (int)floor(x - (double)k/t_phi); 144 | } 145 | 146 | /*-------------------------------------------------- 147 | * out[0] += 148 | * w[k] * (g(img, xmfk, y-k, cols, rows, g1, g1Dim) + 149 | * g(img, xpfk, y+k, cols, rows, g1, g1Dim))+ 150 | * w1[k]* (g(img, xmfk-1, y-k, cols, rows, g1, g1Dim) + 151 | * g(img, xpfk+1, y+k, cols, rows, g1, g1Dim)); 152 | *--------------------------------------------------*/ 153 | 154 | out1[0] += 155 | w[k] * (g(img1, xmfk, y-k, cols, rows, g1, g1Dim) + 156 | g(img1, xpfk, y+k, cols, rows, g1, g1Dim))+ 157 | w1[k]* (g(img1, xmfk, y-k, cols, rows, g1, g1Dim) + 158 | g(img1, xpfk, y+k, cols, rows, g1, g1Dim)); 159 | out2[0] += 160 | w[k] * (g(img2, xmfk, y-k, cols, rows, g1, g1Dim) + 161 | g(img2, xpfk, y+k, cols, rows, g1, g1Dim))+ 162 | w1[k]* (g(img2, xmfk, y-k, cols, rows, g1, g1Dim) + 163 | g(img2, xpfk, y+k, cols, rows, g1, g1Dim)); 164 | } 165 | 166 | if (DEBUG) printf("out: %2.4f\n", out1[0]); 167 | if (DEBUG) printf("out: %2.4f\n", out2[0]); 168 | 169 | free(g1); 170 | free(g2); 171 | free(w); 172 | free(w1); 173 | } 174 | -------------------------------------------------------------------------------- /anc/mex/anisotropic_filter/anisoDouble.h: -------------------------------------------------------------------------------- 1 | /* anisoDouble.c */ 2 | void createGauss(int dim, double sigma, double g[]); 3 | double g(double img[], int x, int y, int cols, int rows, double *g1, int g1Dim); 4 | void anisoGauss(double img1[], double img2[], int rows, int cols, int x, int y, double sig_u, double sig_v, double a, double angle, double *out1, double *out2); 5 | -------------------------------------------------------------------------------- /anc/mex/anisotropic_filter/anisoDoubleFilter.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "anisoDouble.h" 3 | 4 | #include "defs.h" 5 | 6 | #define NARGS 6 /* Number of input arguments */ 7 | 8 | #define ROWS rows 9 | #define COLS cols 10 | #define PAD (int)pad_amt[0] 11 | 12 | /* Looping anisotropic gaussian filtering. */ 13 | 14 | void mexFunction(int nlhs, mxArray *plhs[], int nrhs, 15 | const mxArray *prhs[]) 16 | { 17 | /* Declarations*/ 18 | char *msg; 19 | mxArray *gauss1, *gauss2, *t; 20 | double *out, *out_2, *tmp, *angle, *g1, *g2; 21 | double *img, *img_2, *pad_amt, *pad_pr; 22 | int i, j, ni, nj, crd; 23 | int cc = 0; 24 | int rows, cols, rows_2, cols_2, g1Dim, g2Dim; 25 | double *avg, *sig_u, *sig_v; 26 | 27 | if (nrhs != NARGS) { 28 | sprintf(msg, "%d input arguments required.\n", NARGS); 29 | mexErrMsgTxt( msg); 30 | } else if (nlhs > 2) { 31 | mexErrMsgTxt("Too many output arguments.\n"); 32 | } 33 | 34 | /* We only handle doubles */ 35 | if (!mxIsDouble(prhs[0]) 36 | || !mxIsDouble(prhs[1]) 37 | || !mxIsDouble(prhs[2]) 38 | || !mxIsDouble(prhs[3]) 39 | || !mxIsDouble(prhs[4])) { 40 | mexErrMsgTxt("Input image and stdevs should be double.\n"); 41 | } 42 | 43 | img = mxGetPr(prhs[0]); 44 | cols = mxGetN(prhs[0]); 45 | rows = mxGetM(prhs[0]); 46 | 47 | img_2 = mxGetPr(prhs[1]); 48 | cols_2 = mxGetN(prhs[1]); 49 | rows_2 = mxGetM(prhs[1]); 50 | 51 | sig_u = mxGetPr(prhs[2]); 52 | sig_v = mxGetPr(prhs[3]); 53 | angle = mxGetPr(prhs[4]); 54 | 55 | /* Pad amount */ 56 | pad_amt = mxGetPr(prhs[5]); 57 | 58 | if (cols != cols_2 || cols != cols_2) { 59 | mexErrMsgTxt("Input images should be the same size.\n"); 60 | return; 61 | } 62 | 63 | if (cols != mxGetN(prhs[2]) + (int)pad_amt[0] * 2 64 | || cols != mxGetN(prhs[3]) + (int)pad_amt[0] * 2 65 | || cols != mxGetN(prhs[4]) + (int)pad_amt[0] * 2 66 | || rows != mxGetM(prhs[2]) + (int)pad_amt[0] * 2 67 | || rows != mxGetM(prhs[3]) + (int)pad_amt[0] * 2 68 | || rows != mxGetM(prhs[4]) + (int)pad_amt[0] * 2) { 69 | mexErrMsgTxt("Std devs. should be same size as image - (2*pad)\n"); 70 | return; 71 | } 72 | 73 | if (DEBUG) { 74 | printf("r: %d, c: %d\n", rows, cols); 75 | } 76 | 77 | /* Create output */ 78 | plhs[0] = mxCreateDoubleMatrix(rows-(int)pad_amt[0] * 2, 79 | cols-(int)pad_amt[0] * 2, mxREAL); 80 | out = mxGetPr(plhs[0]); 81 | 82 | plhs[1] = mxCreateDoubleMatrix(rows-(int)pad_amt[0] * 2, 83 | cols-(int)pad_amt[0] * 2, mxREAL); 84 | out_2 = mxGetPr(plhs[1]); 85 | 86 | /* Loop over inputs, filtering */ 87 | for (i = (int)PAD; i < ROWS-(int)PAD; i++) { 88 | for (j = (int)PAD; j < COLS-(int)PAD; j++) { 89 | /*-------------------------------------------------- 90 | * Calculate coords of angle/sigs. 91 | *--------------------------------------------------*/ 92 | ni = i-(int)PAD; 93 | nj = j-(int)PAD; 94 | crd = coord(ni, nj, COLS - (int)PAD * 2, ROWS - (int)PAD * 2); 95 | if (DEBUG) 96 | printf("i: %d, j: %d, ni: %d, nj: %d, crd: %d\n", 97 | i, j, ni, nj, crd); 98 | anisoGauss(img, img_2, ROWS, COLS, i, j, sig_u[crd], sig_v[crd], 99 | 0.5, angle[crd], &out[crd], &out_2[crd]); 100 | 101 | } 102 | } 103 | 104 | return; 105 | } 106 | -------------------------------------------------------------------------------- /anc/mex/anisotropic_filter/anisoDoubleFilter.mexglx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattfoster/matlab-interpolation-toolkit/0d4d7af6aa1bddb6cda0ec18049c6141ea55df6b/anc/mex/anisotropic_filter/anisoDoubleFilter.mexglx -------------------------------------------------------------------------------- /anc/mex/anisotropic_filter/anisoDoubleFilter.mexmaci: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattfoster/matlab-interpolation-toolkit/0d4d7af6aa1bddb6cda0ec18049c6141ea55df6b/anc/mex/anisotropic_filter/anisoDoubleFilter.mexmaci -------------------------------------------------------------------------------- /anc/mex/anisotropic_filter/common.c: -------------------------------------------------------------------------------- 1 | /* Functions to implement fast anisotropic gaussian filtering */ 2 | 3 | /* Return the index for the given coordinates (assuming matlab 4 | * style indexing 5 | * */ 6 | int coord(int i, int j, int cols, int rows) 7 | { 8 | int c; 9 | c = j * rows + i; 10 | /*-------------------------------------------------- 11 | * c = i * cols + j; 12 | *--------------------------------------------------*/ 13 | 14 | /*-------------------------------------------------- 15 | * return c < rows * cols ? c : rows * cols -1; 16 | *--------------------------------------------------*/ 17 | return c; 18 | } 19 | -------------------------------------------------------------------------------- /anc/mex/anisotropic_filter/common.h: -------------------------------------------------------------------------------- 1 | /* common.c */ 2 | int coord(int i, int j, int cols, int rows); 3 | -------------------------------------------------------------------------------- /anc/mex/anisotropic_filter/defs.h: -------------------------------------------------------------------------------- 1 | #if !defined(MAX) 2 | #define MAX(A, B) ((A) > (B) ? (A) : (B)) 3 | #endif 4 | 5 | #if !defined(MIN) 6 | #define MIN(A, B) ((A) < (B) ? (A) : (B)) 7 | #endif 8 | 9 | #define DEBUG 0 10 | #define SHOW 0 11 | #define SIG_CAP 50 12 | #define MAX_C 10 13 | #define TOL 1e-6 14 | #define PI 3.14159265358979323846 15 | 16 | #include "common.h" 17 | -------------------------------------------------------------------------------- /anc/mex/gst_eigenvalue/Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | ## Mex Options: 3 | ## 4 | 5 | # MEX=/Applications/MATLAB_R2008b.app/bin/mex 6 | MEX = mex 7 | DEBUG = -g -O -v 8 | MEXSUFFIX = $(/Applications/MATLAB_R2008b.app/bin/mexext) 9 | 10 | ## 11 | ## Linker/Loader flags 12 | ## 13 | 14 | LDFLAGS = 15 | INCLUDES = -lm 16 | CFLAGS = -g $(INCLUDES) 17 | PROGRAM = gstEig 18 | FUNCTION = $(PROGRAM)$(MEXSUFFIX) 19 | MAKEFILE = Makefile 20 | HDRS = defs.h 21 | SRCS = common.c 22 | MAINSRC = $(PROGRAM).c 23 | # MFILE = $(PROGRAM).m 24 | # Test script 25 | # TESTSCRIPT = test.m 26 | # Script to generate headers. 27 | #HEADER = include.sh 28 | LIBS = $(LOCAL_LIBS) $(SYS_LIBS) 29 | 30 | ## 31 | ## Targets 32 | ## 33 | 34 | #all : include $(FUNCTION) 35 | 36 | all: $(FUNCTION) 37 | 38 | $(FUNCTION): $(MAKEFILE) 39 | 40 | sources : $(SRCS) $(HDRS) 41 | 42 | include: $(SRCS) 43 | @echo 'Generating headers for $+' 44 | $(HEADER) $+ 45 | 46 | test : $(TESTSCRIPT) $(FUNCTION) 47 | cat $(TESTSCRIPT) 48 | $(MATLAB)/bin/matlab < $(TESTSCRIPT) 49 | 50 | $(FUNCTION): $(MAINSRC) $(SRCS) $(HDRS) Makefile 51 | $(MEX) $(DEBUG) $(CFLAGS) $(LDFLAGS) $(SRCS) $(MAINSRC) \ 52 | $(LIBS) -o $(FUNCTION) 53 | 54 | standalone: main.c $(SRCS) 55 | $(CC) -o anisoGauss -Wall -ansi $(CFLAGS) $(LDFLAGS) $+ 56 | 57 | delmex: 58 | rm -f $(FUNCTION) 59 | 60 | clean : delmex 61 | rm -f *~ core .depend.$(OSTYPE) 62 | 63 | tags : sources 64 | etags -t $(SRCS) $(HDRS) 65 | 66 | .PHONY: all sources test delmex full 67 | .PHONY: clean tags 68 | -------------------------------------------------------------------------------- /anc/mex/gst_eigenvalue/common.c: -------------------------------------------------------------------------------- 1 | /* Functions to implement fast anisotropic gaussian filtering */ 2 | 3 | /* Return the index for the given coordinates (assuming matlab 4 | * style indexing 5 | * */ 6 | int coord(int i, int j, int cols, int rows) 7 | { 8 | int c; 9 | c = j * rows + i; 10 | /*-------------------------------------------------- 11 | * c = i * cols + j; 12 | *--------------------------------------------------*/ 13 | 14 | /*-------------------------------------------------- 15 | * return c < rows * cols ? c : rows * cols -1; 16 | *--------------------------------------------------*/ 17 | return c; 18 | } 19 | -------------------------------------------------------------------------------- /anc/mex/gst_eigenvalue/common.h: -------------------------------------------------------------------------------- 1 | /* common.c */ 2 | int coord(int i, int j, int cols, int rows); 3 | -------------------------------------------------------------------------------- /anc/mex/gst_eigenvalue/defs.h: -------------------------------------------------------------------------------- 1 | #if !defined(MAX) 2 | #define MAX(A, B) ((A) > (B) ? (A) : (B)) 3 | #endif 4 | 5 | #if !defined(MIN) 6 | #define MIN(A, B) ((A) < (B) ? (A) : (B)) 7 | #endif 8 | 9 | #define DEBUG 0 10 | #define SHOW 0 11 | #define SIG_CAP 50 12 | #define MAX_C 10 13 | #define TOL 1e-6 14 | #define PI 3.14159265358979323846 15 | 16 | #include "common.h" 17 | -------------------------------------------------------------------------------- /anc/mex/gst_eigenvalue/gstEig.c: -------------------------------------------------------------------------------- 1 | /* Calculate the eigenvalues of a given set of multiplied gradients. */ 2 | /* These results aren't the same as matlabs..... */ 3 | 4 | #include 5 | #include 6 | 7 | #include "defs.h" 8 | #include "common.h" 9 | 10 | #define NARGS 3 /* Number of input arguments */ 11 | 12 | #define ROWS rows 13 | #define COLS cols 14 | 15 | /* Looping anisotropic gaussian filtering. */ 16 | 17 | void mexFunction(int nlhs, mxArray *plhs[], int nrhs, 18 | const mxArray *prhs[]) 19 | { 20 | /* Declarations*/ 21 | 22 | char *msg; 23 | double *gx2, *gy2, *gxy, *lam_1, *lam_2; 24 | double pre, post; 25 | int i, j, rows, cols; 26 | 27 | if (nrhs != NARGS) { 28 | sprintf(msg, "%d input arguments required.\n", NARGS); 29 | mexErrMsgTxt( msg); 30 | } else if (nlhs > 2) { 31 | mexErrMsgTxt("Too many output arguments.\n"); 32 | } 33 | 34 | /* We only handle doubles */ 35 | if (!mxIsDouble(prhs[0]) 36 | || !mxIsDouble(prhs[1]) 37 | || !mxIsDouble(prhs[2])) { 38 | mexErrMsgTxt("Input image and stdevs should be double.\n"); 39 | } 40 | 41 | gxy = mxGetPr(prhs[0]); 42 | cols = mxGetN(prhs[0]); 43 | rows = mxGetM(prhs[0]); 44 | 45 | gx2 = mxGetPr(prhs[1]); 46 | gy2 = mxGetPr(prhs[2]); 47 | 48 | if (cols != mxGetN(prhs[1]) || cols != mxGetN(prhs[2]) 49 | || rows != mxGetM(prhs[1]) || rows != mxGetM(prhs[2])) { 50 | mexErrMsgTxt("Input vectors should be of equal size.\n"); 51 | } 52 | 53 | 54 | plhs[0] = mxCreateDoubleMatrix(rows, cols, mxREAL); 55 | lam_1 = mxGetPr(plhs[0]); 56 | plhs[1] = mxCreateDoubleMatrix(rows, cols, mxREAL); 57 | lam_2 = mxGetPr(plhs[1]); 58 | 59 | for (i = 0; i < ROWS * COLS; i++) { 60 | pre = 0.5 * (gx2[i] + gy2[i]); 61 | post = 0.5 * sqrt(pow(gx2[i] - gy2[i], 2.0) + 4.0 * pow(gxy[i], 2.0)); 62 | lam_1[i] = pre + post; 63 | lam_2[i] = pre - post; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /anc/mex/gst_eigenvalue/gstEig.mexglx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattfoster/matlab-interpolation-toolkit/0d4d7af6aa1bddb6cda0ec18049c6141ea55df6b/anc/mex/gst_eigenvalue/gstEig.mexglx -------------------------------------------------------------------------------- /anc/mex/gst_eigenvalue/gstEig.mexmaci: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattfoster/matlab-interpolation-toolkit/0d4d7af6aa1bddb6cda0ec18049c6141ea55df6b/anc/mex/gst_eigenvalue/gstEig.mexmaci -------------------------------------------------------------------------------- /anc/ndc.m: -------------------------------------------------------------------------------- 1 | function [gx, gy] = ndc(si, cm, r, type, al, be) 2 | % Calculate the derivative of a sampled image using NDC. 3 | % [xx, yy] = ndc(si, cm, al, be, r) 4 | % 5 | % Required arguments: 6 | % si = sampled image. 7 | % cm = confidence map. 8 | % Optional arguments: 9 | % r = mask radius (default 11). 10 | % 'type' = set the filter type: 11 | % 'gauss' = gaussian. 12 | % 'kuttson' = Knuttson style raised cosine. 13 | % 'al' = Knuttson filter alpha param. 14 | % 'be' = Knuttson filter beta param. 15 | % 16 | % See also: 17 | % ADAPTIVENC, DONC 18 | % 19 | % Matt Foster 20 | 21 | error(nargchk(2, 6, nargin)); 22 | 23 | if nargin < 3 24 | r = 11; 25 | end 26 | 27 | if nargin < 4 28 | type = 'gauss'; 29 | end 30 | 31 | if strfind(type, 'knuttson') > 0 32 | if nargin < 5 33 | al = 3; 34 | be = 0.5; 35 | end 36 | % Create a Knuttson mask 37 | [m, m_x, m_y, m_xy, m_xx, m_yy] = ndcMask(al, be, r); 38 | elseif strfind(type, 'gauss') > 0 39 | if strfind(type, 'gauss_sig') > 0 40 | g_type = 'sigma'; 41 | else 42 | g_type = 'dim'; 43 | end 44 | % Create a guassian mask 45 | [m, m_x, m_y, m_xy, m_xx, m_yy] = ndcGaussMask(r, g_type); 46 | 47 | else 48 | error('Misunderstood type argument: %s', type); 49 | end 50 | 51 | cm = double(cm); 52 | 53 | nc = conv2(cm, m, 'same'); 54 | cc = conv2(si, m, 'same'); 55 | 56 | c_x = conv2(si, m_x, 'same'); 57 | c_y = conv2(si, m_y, 'same'); 58 | 59 | nc_x = conv2(cm, m_x, 'same'); 60 | nc_y = conv2(cm, m_y, 'same'); 61 | 62 | d_x = nc .* c_x - nc_x .* cc; 63 | d_y = nc .* c_y - nc_y .* cc; 64 | 65 | n_xx = nc .* conv2(cm, m_xx, 'same') - nc_x.^2; 66 | n_xy = nc .* conv2(cm, m_xy, 'same') - nc_x .* nc_y; 67 | n_yx = n_xy; 68 | n_yy = nc .* conv2(cm, m_yy, 'same') - nc_y.^2; 69 | 70 | [rows, cols] = size(si); 71 | for i = 1:rows 72 | for j = 1:cols 73 | D = [d_x(i, j); d_y(i, j)]; 74 | N = [n_xx(i, j), n_xy(i, j); n_yx(i, j), n_yy(i, j)]; 75 | G = inv(N)*D; 76 | gx(i, j) = G(1, 1); 77 | gy(i, j) = G(2, 1); 78 | end 79 | end 80 | 81 | 82 | -------------------------------------------------------------------------------- /anc/private/fastGenerateGradients.m: -------------------------------------------------------------------------------- 1 | function [gxy, gx2, gy2] = fastGenerateGradients(gx, gy, filtSize); 2 | % [gxy, gx2, gy2] = fastGenerateGradients(gx, gy, filtSize) 3 | % Find gradiants using seperable filtering. 4 | % 5 | % Matt Foster 6 | 7 | % $LastChangedDate$ 8 | 9 | gxy = gx.*gy; 10 | gx2 = gx.^2; 11 | gy2 = gy.^2; 12 | 13 | gf = gauss(filtSize, 'dim'); % 1D gaussian 14 | 15 | gxy = sepConv2(gxy, gf); 16 | gx2 = sepConv2(gx2, gf); 17 | gy2 = sepConv2(gy2, gf); 18 | -------------------------------------------------------------------------------- /anc/private/gauss.m: -------------------------------------------------------------------------------- 1 | function g = gauss(x, type, norm) 2 | % g = gauss(x, type) 3 | % Create a 1D gaussian. 4 | 5 | if nargin < 2 6 | type = 'sigma'; 7 | end 8 | 9 | if nargin < 3 10 | norm = 1; 11 | end 12 | 13 | if strfind(type, 'sigma') 14 | sig_x = x; 15 | lim_x = 3.*sig_x; 16 | else 17 | lim_x = floor(x/2); 18 | sig_x = (lim_x - 2)/6; 19 | end 20 | 21 | xx = -lim_x:lim_x; 22 | 23 | if norm 24 | px = (sqrt(2*pi) * sig_x).^-1; 25 | else 26 | px = 1; 27 | end 28 | g = px .* exp(-0.5 .* (xx.^2 ./ sig_x.^2)); 29 | -------------------------------------------------------------------------------- /anc/private/gauss2.m: -------------------------------------------------------------------------------- 1 | function g = gauss2(x, y, type, norm) 2 | % g = gauss2(x, y, type) 3 | % Create a 2D gaussian by convolution. 4 | 5 | if nargin < 3 6 | type = 'sigma'; 7 | end 8 | 9 | if strfind(type, 'sigma') 10 | sig_x = x; 11 | sig_y = y; 12 | lim_x = 3.*sig_x; 13 | lim_y = 3.*sig_y; 14 | else 15 | lim_x = floor(x/2); 16 | lim_y = floor(y/2); 17 | sig_x = (lim_x - 2)/6; 18 | sig_y = (lim_y - 2)/6; 19 | end 20 | 21 | xx = -lim_x:lim_x; 22 | yy = -lim_y:lim_y; 23 | 24 | px = (sqrt(2*pi) * sig_x).^-1; 25 | fx = px .* exp(-0.5 .* (xx.^2 ./ sig_x.^2)); 26 | 27 | py = (sqrt(2*pi) * sig_y).^-1; 28 | fy = py .* exp(-0.5 .* (yy.^2 ./ sig_y.^2)); 29 | 30 | g = conv2(fx, fy'); 31 | -------------------------------------------------------------------------------- /anc/private/gstAnisotropy.m: -------------------------------------------------------------------------------- 1 | function [aniso, energy] = gstAnisotropy(lam_1, lam_2) 2 | % function [aniso, energy] = gstAnisotropy(lam_1, lam_2) 3 | % Compute anisotropy and energy from given eigenvaulues. 4 | 5 | energy = lam_1 + lam_2; 6 | diff = lam_1 - lam_2; 7 | aniso = diff ./ energy; 8 | 9 | aniso(energy == 0) = 0; % necessary? 10 | aniso(diff == 0) = 0; 11 | -------------------------------------------------------------------------------- /anc/private/gstEigenvalues.m: -------------------------------------------------------------------------------- 1 | function [lam_1, lam_2] = gstEigenvalues(gxy, gx2, gy2) 2 | % function [lam_1, lam_2] = gstEigenValues(gxy, gx2, gy2) 3 | % Create gst eigenvalues, and sort them into lam_1 and lam_2. 4 | 5 | [rr, cc] = size(gxy); 6 | 7 | % Preallocation for speed: 8 | lam_1 = zeros(size(gxy)); 9 | lam_2 = zeros(size(gxy)); 10 | 11 | % Loop, calculating the eigenvalues. 12 | for i = 1:rr 13 | for j = 1:cc 14 | eigenValues = eig([gx2(i,j), gxy(i,j); gxy(i,j), gy2(i,j)]); 15 | lam_1(i,j) = max(eigenValues); 16 | lam_2(i,j) = min(eigenValues); 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /anc/private/gstOrientations.m: -------------------------------------------------------------------------------- 1 | function phi_2 = gstOrientations(lam_1, lam_2, gxy, gx2, gy2) 2 | % function [phi_1, phi_2] = gstOrientations(lam_1, lam_2, gxy, gx2, gy2) 3 | % Calculate gst orientations from eigenvalues and orientation prod. 4 | %---------------------------------------------------- 5 | % 6 | % phi_1 = atan((lam_1 - gx2) ./ gxy); 7 | % phi_1(gxy == 0) = 0; 8 | % 9 | phi_2 = atan(gxy ./ (lam_2 - gy2)); 10 | phi_2(isnan(phi_2)) = 0; 11 | -------------------------------------------------------------------------------- /anc/private/gstSigmas.m: -------------------------------------------------------------------------------- 1 | function [sig_u, sig_v] = gstSigmas(aniso, sig_a, max_s, min_s, c, alpha) 2 | % function [sig_u, sig_v] = gstSigmas(aniso, sig_a, min_s, c, alpha) 3 | % Compute gst standard deviations. 4 | 5 | if nargin < 5 6 | c = 0.5; 7 | end 8 | if nargin < 6 9 | alpha = 0.5; 10 | end 11 | 12 | sig_u = c.*(1+aniso).^alpha.*sig_a; 13 | sig_v = c.*(1-aniso).^alpha.*sig_a; 14 | 15 | %---------------------------------------------------- 16 | % sig_u = sig_a .* alpha ./ (alpha + aniso); 17 | % sig_v = sig_a .* (alpha + aniso) ./ aniso; 18 | %---------------------------------------------------- 19 | 20 | sig_u(isnan(sig_u)) = 0; 21 | sig_v(isnan(sig_v)) = 0; 22 | sig_u(sig_u > max_s) = max_s; 23 | sig_v(sig_v > max_s) = max_s; 24 | sig_u(sig_u < min_s) = min_s; 25 | sig_v(sig_v < min_s) = min_s; 26 | -------------------------------------------------------------------------------- /anc/private/ndcGaussMask.m: -------------------------------------------------------------------------------- 1 | function [m, m_x, m_y, m_xy, m_xx, m_yy] = ndcGaussMask(s, type) 2 | %function [m, m_x, m_y, m_xy, m_xx, m_yy] = ndcMask(s, type) 3 | % Generate a mask for Normalised Convolution: 4 | 5 | % Parse input arguments. 6 | if nargin < 2 7 | type = 'dim'; 8 | end 9 | 10 | if strfind(type, 'sigma') > 0 11 | lim = round(3*s); 12 | elseif strfind(type, 'dim') > 0 13 | lim = s; 14 | else 15 | error('Misunderstood type argument: %s', type); 16 | end 17 | 18 | if ~rem(lim, 2) 19 | lim = lim + 1; 20 | end 21 | 22 | [x, y] = meshgrid(-lim:lim, -lim:lim); 23 | 24 | % Note: using gauss2 instead of knuttson. 25 | m = gauss2(2*lim+1, 2*lim+1, 'dim'); 26 | 27 | m_x = m.*x; 28 | m_y = m.*y; 29 | m_xy = m.*(x.*y); 30 | m_xx = m.*(x.^2); 31 | m_yy = m.*(y.^2); 32 | -------------------------------------------------------------------------------- /anc/private/sepConv2.m: -------------------------------------------------------------------------------- 1 | function op = sepConv2(in, filt) 2 | % op = sepConv2(in, fs, type) 3 | % Convolve a matrix with a 2D gaussian using a separable transform. 4 | % 5 | op = conv2(in, filt, 'same'); 6 | op = conv2(op, filt', 'same'); 7 | -------------------------------------------------------------------------------- /contents.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from __future__ import with_statement 4 | 5 | import glob 6 | import os.path 7 | import sys 8 | 9 | header = """\ 10 | % Matlab Scattered Data Interpolation Toolbox 11 | % 12 | % Author: Matt Foster , CSAOS, University of Bath 13 | % License: BSD (Except: natural neighbour -- see readme for license) 14 | % 15 | % Interpolation Functions:""" 16 | 17 | footer = """\ 18 | % 19 | % Demos: 20 | % 21 | % To generate correlated test data, run: 22 | % 23 | % [yy, xx] = meshgrid(1:100, 1:100); 24 | % % Make some correlated data. 25 | % input = corr_data(100, 15); 26 | % cm = rand(100) > 0.95; 27 | % sampled = input.*cm; 28 | % [xi, yi, zi] = find(sampled); 29 | % 30 | % figure 31 | % imagesc(sampled) 32 | % 33 | % This will generate some sparse test data suitable for both adaptiveNC and 34 | % griddata style methods. To interpolate this using ANC, run: 35 | % 36 | % anc_out = adaptiveNC(sampled, cm) 37 | % figure 38 | % imagesc(anc_out) 39 | % 40 | % To interpolate the data using griddata style functions (RBF, kriging, 41 | % natural neighbour, etc.), run: 42 | % 43 | % nat_out = natural_neighbour(xx, yy, zz, xx, yy); 44 | % rbf_out = rbf(xx, yy, zz, xx, yy); 45 | % kriging_out = kriging(xx, yy, zz, xx, yy); 46 | % figure 47 | % imagesc(nat_out) % etc. 48 | % 49 | % Finally, calculate RMSE values, using: 50 | % anc_rmse = sqrt(mean((input(:) - anc_out(:)).^2)) 51 | % nat_rmse = sqrt(mean((input(:) - nat_out(:)).^2)) 52 | % % etc. 53 | % 54 | % Please note: Kriging and RBF will probably not work on large data sets due to 55 | % large matrix inversions. You mileage may vary. 56 | % """ 57 | 58 | with open('toolkit/Contents.m', 'w') as output: 59 | sys.stdout = output # redirect stdout to output file 60 | print header 61 | for fname in glob.glob('toolkit/*.m'): 62 | # Skip this iteration if we're reading the output file 63 | if fname.endswith('Contents.m'): 64 | continue 65 | with open(fname,'r') as f: 66 | f.readline() # read past the first line 67 | line = f.readline() # read the second 68 | base = os.path.basename(fname) 69 | name = os.path.splitext(base)[0] 70 | print "%% %-20s\t- %s" % (name, line.strip('%').rstrip()) 71 | print footer 72 | 73 | -------------------------------------------------------------------------------- /kriging/kriging.m: -------------------------------------------------------------------------------- 1 | function [z, varargout] = kriging(xi, yi, zi, x, y, pl) 2 | % Perform interpolation using Ordinary kriging. 3 | % [z, z_var] = kriging(xi, yi, zi, x, y) 4 | % 5 | % [x, y, z] = kriging(...) 6 | % [x, y, z, z_var] = kriging(...) 7 | % [x, y, z, z_var, time] = kriging(...) 8 | % 9 | % Matt Foster 10 | 11 | % Code heavily based on recipe from MATLAB recipes for Earth sciences. Martin 12 | % H. Trauth, Springer 2006 13 | 14 | warning off all; 15 | 16 | tic; 17 | 18 | if nargin < 6 19 | pl = 0; 20 | end 21 | 22 | % Create Variogram -- warning.. even this is slow! 23 | 24 | [X1,X2] = meshgrid(xi); 25 | [Y1,Y2] = meshgrid(yi); 26 | [Z1,Z2] = meshgrid(zi); 27 | 28 | D = sqrt((X1 - X2).^2 + (Y1 - Y2).^2); 29 | 30 | G = 0.5*(Z1 - Z2).^2; 31 | 32 | D2 = D.*(diag(xi*NaN)+1); 33 | lag = mean(min(D2)); 34 | hmd = max(D(:))/2; 35 | max_lags = floor(hmd/lag); 36 | LAGS = ceil(D/lag); 37 | 38 | for i = 1:max_lags 39 | SEL = (LAGS == i); 40 | DE(i) = mean(mean(D(SEL))); 41 | PN(i) = sum(sum(SEL == 1))/2; 42 | GE(i) = mean(mean(G(SEL))); 43 | end 44 | lags=0:max(DE); 45 | 46 | 47 | mod_func = fit_spherical(DE, GE, var(zi(:))); 48 | 49 | model = mod_func(D); 50 | 51 | if pl 52 | plot(DE, GE, '.'); 53 | hold on 54 | plot(DE, mod_func(DE)); 55 | hold off 56 | 57 | dlmwrite('variogram.dat', [DE', GE', mod_func(DE)'], '\t'); 58 | 59 | end 60 | 61 | % add zeros as the lat for and col of the variogram model. 62 | n = length(xi); 63 | model(:,n+1) = 1; 64 | model(n+1,:) = 1; 65 | % add a zero to the bottom right corner 66 | model(n+1,n+1) = 0; 67 | 68 | % Invert the variogram model 69 | model_inv = inv(model); 70 | 71 | Zg = nan(size(x)); 72 | s2_k = nan(size(x)); 73 | 74 | % Perform the Kriging 75 | for k = 1:length(Zg(:)); 76 | DOR = ((xi - x(k)).^2+(yi - y(k)).^2).^0.5; 77 | G_R = mod_func(DOR); 78 | G_R(n+1) = 1; 79 | E = model_inv*G_R; 80 | Zg(k) = sum(E(1:n,1).*zi); 81 | s2_k(k) = sum(E(1:n,1).*G_R(1:n,1))+E(n+1,1); 82 | end 83 | 84 | whos z 85 | z = Zg; 86 | z_var = s2_k; 87 | 88 | time = toc; 89 | 90 | if nargout == 2 || nargout == 3 91 | varargout{1} = z_var; 92 | end 93 | 94 | if nargout == 3 95 | varargout{2} = time; 96 | end 97 | 98 | if nargout >= 4 99 | tmp = z; 100 | z = xx; 101 | varargout{1} = yy; 102 | varargout{2} = tmp; 103 | varargout{3} = z_var; 104 | end 105 | 106 | if nargout == 5 107 | varargout{4} = time; 108 | end 109 | 110 | warning on all; 111 | 112 | end 113 | 114 | function mod_func = fit_spherical(DE, GE, var_z) 115 | 116 | % Only fit to the section below 90% of the variance 117 | ind = 1:min(find(GE > var_z * 0.9)); 118 | coef = [DE(ind);DE(ind).^3]' \ GE(ind)'; 119 | c = sqrt((-0.5 .* (1.5 ./ coef(1)).^-3)./coef(2)); 120 | a = 1.5 .* c / coef(1); 121 | 122 | %---------------------------------------------------- 123 | % lag_var = DE(min(find(GE > var_z))); 124 | % if a > lag_var; 125 | % a = lag_var; 126 | % end 127 | % 128 | % if c > var_z 129 | % c = mean(GE(min(find(GE > var_z)):end)); 130 | % end 131 | %---------------------------------------------------- 132 | 133 | if isreal(a) 134 | % Return an anonymous function that models the variogram 135 | mod_func = @(h) (coef(1) .* h + coef(2) .* h.^3) .* (h <= a) ... 136 | + c.*ones(size(h)) .* (h > a); 137 | else 138 | mod_func = @(h) (coef(1) .* h + coef(2) .* h.^3); 139 | end 140 | 141 | end 142 | 143 | -------------------------------------------------------------------------------- /natural_neighbour/.indent.pro: -------------------------------------------------------------------------------- 1 | --original 2 | 3 | // overwrites to --original 4 | --blank-lines-after-declarations 5 | --blank-lines-after-procedures 6 | --no-blank-lines-after-commas 7 | --declaration-indentation1 8 | --comment-line-length78 9 | --dont-break-procedure-type 10 | --dont-break-function-decl-args 11 | --line-length999 12 | --pointers-to-type-definitions 13 | // works only with the tweaked version of indent 14 | // available from: 15 | // http://www.marine.csiro.au/~sakov/indent-2.2.8a-mod.tar.gz 16 | --swallow-optional-blank-lines 17 | --no-tabs 18 | 19 | // add-ons for my code 20 | --dont-format-first-column-comments // or there will be a mess 21 | -------------------------------------------------------------------------------- /natural_neighbour/CHANGELOG: -------------------------------------------------------------------------------- 1 | v. 1.66, 26 June 2006 2 | -- Introduced this file 3 | -- Fixed error in reading the command-line input after "-L" 4 | -- Made a few cosmetic changes 5 | v. 1.67, 30 August 2006 6 | -- Introduced -% option (available only when built with -DNN_SERIAL) 7 | v. 1.68, 28 September 2006 8 | -- Edited README 9 | v. 1.69, 22 November 2006 10 | -- Substantial changes in processing of the degenerate case for Sibson 11 | interpolation. This is the case when an interpolation point is close 12 | to being in between data points. It is no longer handled by a pair 13 | of symmetric displacements of this point; instead, the center of the 14 | corresponding circumcircle is moved elsewhere in a specific way. 15 | v. 1.70, 24 November 2006 16 | -- Added example 6. 17 | v. 1.71, 17 January 2006 18 | -- Made the test on whether an output point exactly coincides with 19 | an input point in nnpi_triangle_process() approximate. -------------------------------------------------------------------------------- /natural_neighbour/CUSTOMISE: -------------------------------------------------------------------------------- 1 | # This file is for customizing the configuation process performed by 2 | # `./configure'. This file consists of sh(1) variable-definition lines. 3 | # The value given to those variables by this file will override their default 4 | # values. 5 | # 6 | # Be sure to test whether the variable doesn't exists before setting it. 7 | # 8 | # You can also customize the configuration process via the environment 9 | # variables seen by ./configure. For example: 10 | # 11 | # In csh(1): 12 | # % setenv CC acc 13 | # & setenv CFLAGS -g 14 | # % ./configure 15 | # 16 | # In sh(1) or ksh(1): 17 | # $ CC=acc CFLAGS=-g ./configure 18 | # 19 | # Variables in this file override the environmental ones. 20 | # 21 | ############################################################################# 22 | 23 | # C compiler 24 | if [ -z "$CC" ]; then 25 | CC=gcc 26 | fi 27 | 28 | # C compiler flags 29 | if [ -z "$CFLAGS" ]; then 30 | CFLAGS="-g -O2 -Wall -pedantic" 31 | fi 32 | 33 | CFLAGS_TRIANGLE="$CFLAGS -w -ffloat-store" 34 | CFLAGS_VULNERABLE="-fno-force-mem -ffloat-store" 35 | 36 | # Installation prefix (default is /usr/local) 37 | if [ "${prefix}" = "NONE" ]; then 38 | prefix=/usr/local 39 | fi 40 | 41 | if [ "${libdir}" = "\${exec_prefix}/lib" ]; then 42 | libdir="${prefix}/lib${LIBEXT}" 43 | fi 44 | 45 | echo "Using prefix ${prefix}" 46 | echo "Using libdir ${libdir}" 47 | -------------------------------------------------------------------------------- /natural_neighbour/README: -------------------------------------------------------------------------------- 1 | nn 2 | Natural Neighbours interpolation library 3 | Version 1.71 4 | 5 | Provides Natural Neighbours interpolation library "libnn.a" and a command line 6 | Natural Neighbours interpolation utility "nnbathy". 7 | 8 | Copyright 2000-2005 CSIRO Marine Research 9 | GPO 1538 Hobart 10 | TAS 7001 11 | Australia 12 | Please send comments and bugs to Pavel.Sakov@csiro.au 13 | 14 | There is no warranty whatsoever. Use at your own risk. 15 | 16 | This code may be freely redistributed under the condition that the copyright 17 | notices are not removed. You may distribute modified versions of this code 18 | UNDER THE CONDITION THAT THIS CODE AND ANY MODIFICATIONS MADE TO IT IN THE 19 | SAME FILE REMAIN UNDER COPYRIGHT OF CSIRO, BOTH SOURCE AND OBJECT CODE ARE 20 | MADE FREELY AVAILABLE WITHOUT CHARGE, AND CLEAR NOTICE IS GIVEN OF THE 21 | MODIFICATIONS. 22 | 23 | This code has been developed and used mainly on pc-linux platform, however, 24 | it should compile on other platforms. Beware that the configure script 25 | currently does not do much more than a couple of checks. It should be viewed 26 | rather as a prototype for the future than a multi-platform configuration tool. 27 | 28 | To compile, run: 29 | 30 | configure 31 | make 32 | (make install) 33 | 34 | For a few quick tests, run: 35 | 36 | make tests 37 | ./nnphi_test 38 | ./nnai_test 39 | ./ht_test 40 | 41 | Apart from the `nn' library, this code contains `nnbathy' -- a simple 42 | interpolation utility/example based on `nn'. 43 | 44 | There are a number of examples of using `nnbathy' in "examples" directory: 45 | 46 | examples/1 -- reconstruction of Franke test function 47 | examples/2 -- reconstruction of bathymetry from sonar data 48 | examples/3 -- performance on degenerate data 49 | examples/4 -- reconstruction of topography from satellite altimeter data 50 | examples/5 -- reconstruction of topography from digitised contours 51 | 52 | For a basic description of structures and functions available from `nn', have 53 | a look at "nn.h". 54 | 55 | Acknowledgments: 56 | 57 | This library uses the following public code/algorithms: 58 | 1. `triangle' by Jonathan Richard Shewchuk -- for Delaunay triangulation; 59 | 2. Dave Watson's algorithm for Sibson interpolation; 60 | 3. Belikov and Semenov's formulas for non-Sibsonian interpolation. 61 | 62 | Please acknowledge the use of this software in any publications: 63 | "Natural Neighbours interpolation software was provided by Pavel Sakov and 64 | is available at URL: http://www.marine.csiro.au/~sakov/nn.tar.gz". 65 | 66 | Good luck! 67 | Pavel Sakov 68 | 69 | -------------------------------------------------------------------------------- /natural_neighbour/config.h: -------------------------------------------------------------------------------- 1 | /* config.h. Generated by configure. */ 2 | #if defined(_WIN32) 3 | #define isnan _isnan 4 | #define copysign _copysign 5 | #define rint (int) 6 | #define M_PI 3.14159265358979323846 7 | #define TRILIBRARY 8 | #define NO_TIMER 9 | #endif 10 | -------------------------------------------------------------------------------- /natural_neighbour/config.h.in: -------------------------------------------------------------------------------- 1 | #if defined(_WIN32) 2 | #define isnan _isnan 3 | #define copysign _copysign 4 | #define rint (int) 5 | #define M_PI 3.14159265358979323846 6 | #define TRILIBRARY 7 | #define NO_TIMER 8 | #endif 9 | -------------------------------------------------------------------------------- /natural_neighbour/config.log: -------------------------------------------------------------------------------- 1 | This file contains any messages produced by compilers while 2 | running configure, to aid debugging if configure makes a mistake. 3 | 4 | It was created by configure, which was 5 | generated by GNU Autoconf 2.59. Invocation command line was 6 | 7 | $ ./configure 8 | 9 | ## --------- ## 10 | ## Platform. ## 11 | ## --------- ## 12 | 13 | hostname = eepc-tsar16 14 | uname -m = i686 15 | uname -r = 2.6.16-1-686-smp 16 | uname -s = Linux 17 | uname -v = #2 SMP Thu May 4 18:28:53 UTC 2006 18 | 19 | /usr/bin/uname -p = unknown 20 | /bin/uname -X = unknown 21 | 22 | /bin/arch = i686 23 | /usr/bin/arch -k = unknown 24 | /usr/convex/getsysinfo = unknown 25 | hostinfo = unknown 26 | /bin/machine = unknown 27 | /usr/bin/oslevel = unknown 28 | /bin/universe = unknown 29 | 30 | PATH: /home/ee1mpf/bin 31 | PATH: /bin 32 | PATH: /sbin 33 | PATH: /usr/local/bin 34 | PATH: /usr/local/sbin 35 | PATH: /usr/bin 36 | PATH: /usr/sbin 37 | PATH: /usr/X11R6/bin 38 | PATH: /usr/games 39 | PATH: /usr/NX/bin 40 | PATH: /usr/local/mindi1.0/bin 41 | 42 | 43 | ## ----------- ## 44 | ## Core tests. ## 45 | ## ----------- ## 46 | 47 | configure:1347: checking for gcc 48 | configure:1373: result: gcc 49 | configure:1617: checking for C compiler version 50 | configure:1620: gcc --version &5 51 | gcc (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21) 52 | Copyright (C) 2006 Free Software Foundation, Inc. 53 | This is free software; see the source for copying conditions. There is NO 54 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 55 | 56 | configure:1623: $? = 0 57 | configure:1625: gcc -v &5 58 | Using built-in specs. 59 | Target: i486-linux-gnu 60 | Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --with-tune=i686 --enable-checking=release i486-linux-gnu 61 | Thread model: posix 62 | gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21) 63 | configure:1628: $? = 0 64 | configure:1630: gcc -V &5 65 | gcc: '-V' option must have argument 66 | configure:1633: $? = 1 67 | configure:1656: checking for C compiler default output file name 68 | configure:1659: gcc -g -O2 -Wall -pedantic conftest.c >&5 69 | configure:1662: $? = 0 70 | configure:1708: result: a.out 71 | configure:1713: checking whether the C compiler works 72 | configure:1719: ./a.out 73 | configure:1722: $? = 0 74 | configure:1739: result: yes 75 | configure:1746: checking whether we are cross compiling 76 | configure:1748: result: no 77 | configure:1751: checking for suffix of executables 78 | configure:1753: gcc -o conftest -g -O2 -Wall -pedantic conftest.c >&5 79 | configure:1756: $? = 0 80 | configure:1781: result: 81 | configure:1787: checking for suffix of object files 82 | configure:1808: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 83 | configure:1811: $? = 0 84 | configure:1833: result: o 85 | configure:1837: checking whether we are using the GNU C compiler 86 | configure:1861: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 87 | configure:1867: $? = 0 88 | configure:1871: test -z 89 | || test ! -s conftest.err 90 | configure:1874: $? = 0 91 | configure:1877: test -s conftest.o 92 | configure:1880: $? = 0 93 | configure:1893: result: yes 94 | configure:1899: checking whether gcc accepts -g 95 | configure:1920: gcc -c -g conftest.c >&5 96 | configure:1926: $? = 0 97 | configure:1930: test -z 98 | || test ! -s conftest.err 99 | configure:1933: $? = 0 100 | configure:1936: test -s conftest.o 101 | configure:1939: $? = 0 102 | configure:1950: result: yes 103 | configure:1967: checking for gcc option to accept ANSI C 104 | configure:2037: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 105 | configure:2043: $? = 0 106 | configure:2047: test -z 107 | || test ! -s conftest.err 108 | configure:2050: $? = 0 109 | configure:2053: test -s conftest.o 110 | configure:2056: $? = 0 111 | configure:2074: result: none needed 112 | configure:2092: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 113 | conftest.c:2: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'me' 114 | configure:2098: $? = 1 115 | configure: failed program was: 116 | | #ifndef __cplusplus 117 | | choke me 118 | | #endif 119 | configure:2269: checking for a BSD-compatible install 120 | configure:2324: result: /usr/bin/install -c 121 | configure:2337: checking for ar 122 | configure:2353: found /usr/bin/ar 123 | configure:2364: result: ar 124 | configure:2378: checking how to run the C preprocessor 125 | configure:2413: gcc -E conftest.c 126 | configure:2419: $? = 0 127 | configure:2451: gcc -E conftest.c 128 | conftest.c:9:28: error: ac_nonexistent.h: No such file or directory 129 | configure:2457: $? = 1 130 | configure: failed program was: 131 | | /* confdefs.h. */ 132 | | 133 | | #define PACKAGE_NAME "" 134 | | #define PACKAGE_TARNAME "" 135 | | #define PACKAGE_VERSION "" 136 | | #define PACKAGE_STRING "" 137 | | #define PACKAGE_BUGREPORT "" 138 | | /* end confdefs.h. */ 139 | | #include 140 | configure:2496: result: gcc -E 141 | configure:2520: gcc -E conftest.c 142 | configure:2526: $? = 0 143 | configure:2558: gcc -E conftest.c 144 | conftest.c:9:28: error: ac_nonexistent.h: No such file or directory 145 | configure:2564: $? = 1 146 | configure: failed program was: 147 | | /* confdefs.h. */ 148 | | 149 | | #define PACKAGE_NAME "" 150 | | #define PACKAGE_TARNAME "" 151 | | #define PACKAGE_VERSION "" 152 | | #define PACKAGE_STRING "" 153 | | #define PACKAGE_BUGREPORT "" 154 | | /* end confdefs.h. */ 155 | | #include 156 | configure:2608: checking for egrep 157 | configure:2618: result: grep -E 158 | configure:2623: checking for ANSI C header files 159 | configure:2648: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 160 | configure:2654: $? = 0 161 | configure:2658: test -z 162 | || test ! -s conftest.err 163 | configure:2661: $? = 0 164 | configure:2664: test -s conftest.o 165 | configure:2667: $? = 0 166 | configure:2756: gcc -o conftest -g -O2 -Wall -pedantic conftest.c >&5 167 | conftest.c: In function 'main': 168 | conftest.c:26: warning: implicit declaration of function 'exit' 169 | conftest.c:26: warning: incompatible implicit declaration of built-in function 'exit' 170 | configure:2759: $? = 0 171 | configure:2761: ./conftest 172 | configure:2764: $? = 0 173 | configure:2779: result: yes 174 | configure:2803: checking for sys/types.h 175 | configure:2819: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 176 | configure:2825: $? = 0 177 | configure:2829: test -z 178 | || test ! -s conftest.err 179 | configure:2832: $? = 0 180 | configure:2835: test -s conftest.o 181 | configure:2838: $? = 0 182 | configure:2849: result: yes 183 | configure:2803: checking for sys/stat.h 184 | configure:2819: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 185 | configure:2825: $? = 0 186 | configure:2829: test -z 187 | || test ! -s conftest.err 188 | configure:2832: $? = 0 189 | configure:2835: test -s conftest.o 190 | configure:2838: $? = 0 191 | configure:2849: result: yes 192 | configure:2803: checking for stdlib.h 193 | configure:2819: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 194 | configure:2825: $? = 0 195 | configure:2829: test -z 196 | || test ! -s conftest.err 197 | configure:2832: $? = 0 198 | configure:2835: test -s conftest.o 199 | configure:2838: $? = 0 200 | configure:2849: result: yes 201 | configure:2803: checking for string.h 202 | configure:2819: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 203 | configure:2825: $? = 0 204 | configure:2829: test -z 205 | || test ! -s conftest.err 206 | configure:2832: $? = 0 207 | configure:2835: test -s conftest.o 208 | configure:2838: $? = 0 209 | configure:2849: result: yes 210 | configure:2803: checking for memory.h 211 | configure:2819: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 212 | configure:2825: $? = 0 213 | configure:2829: test -z 214 | || test ! -s conftest.err 215 | configure:2832: $? = 0 216 | configure:2835: test -s conftest.o 217 | configure:2838: $? = 0 218 | configure:2849: result: yes 219 | configure:2803: checking for strings.h 220 | configure:2819: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 221 | configure:2825: $? = 0 222 | configure:2829: test -z 223 | || test ! -s conftest.err 224 | configure:2832: $? = 0 225 | configure:2835: test -s conftest.o 226 | configure:2838: $? = 0 227 | configure:2849: result: yes 228 | configure:2803: checking for inttypes.h 229 | configure:2819: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 230 | configure:2825: $? = 0 231 | configure:2829: test -z 232 | || test ! -s conftest.err 233 | configure:2832: $? = 0 234 | configure:2835: test -s conftest.o 235 | configure:2838: $? = 0 236 | configure:2849: result: yes 237 | configure:2803: checking for stdint.h 238 | configure:2819: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 239 | configure:2825: $? = 0 240 | configure:2829: test -z 241 | || test ! -s conftest.err 242 | configure:2832: $? = 0 243 | configure:2835: test -s conftest.o 244 | configure:2838: $? = 0 245 | configure:2849: result: yes 246 | configure:2803: checking for unistd.h 247 | configure:2819: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 248 | configure:2825: $? = 0 249 | configure:2829: test -z 250 | || test ! -s conftest.err 251 | configure:2832: $? = 0 252 | configure:2835: test -s conftest.o 253 | configure:2838: $? = 0 254 | configure:2849: result: yes 255 | configure:2875: checking math.h usability 256 | configure:2887: gcc -c -g -O2 -Wall -pedantic conftest.c >&5 257 | configure:2893: $? = 0 258 | configure:2897: test -z 259 | || test ! -s conftest.err 260 | configure:2900: $? = 0 261 | configure:2903: test -s conftest.o 262 | configure:2906: $? = 0 263 | configure:2916: result: yes 264 | configure:2920: checking math.h presence 265 | configure:2930: gcc -E conftest.c 266 | configure:2936: $? = 0 267 | configure:2956: result: yes 268 | configure:2991: checking for math.h 269 | configure:2998: result: yes 270 | configure:3018: checking for main in -lm 271 | configure:3042: gcc -o conftest -g -O2 -Wall -pedantic conftest.c -lm >&5 272 | configure:3048: $? = 0 273 | configure:3052: test -z 274 | || test ! -s conftest.err 275 | configure:3055: $? = 0 276 | configure:3058: test -s conftest 277 | configure:3061: $? = 0 278 | configure:3074: result: yes 279 | configure:3098: checking for realloc 280 | configure:3155: gcc -o conftest -g -O2 -Wall -pedantic conftest.c -lm >&5 281 | configure:3161: $? = 0 282 | configure:3165: test -z 283 | || test ! -s conftest.err 284 | configure:3168: $? = 0 285 | configure:3171: test -s conftest 286 | configure:3174: $? = 0 287 | configure:3186: result: yes 288 | configure:3098: checking for strtod 289 | configure:3155: gcc -o conftest -g -O2 -Wall -pedantic conftest.c -lm >&5 290 | configure:3161: $? = 0 291 | configure:3165: test -z 292 | || test ! -s conftest.err 293 | configure:3168: $? = 0 294 | configure:3171: test -s conftest 295 | configure:3174: $? = 0 296 | configure:3186: result: yes 297 | configure:3098: checking for strtok 298 | configure:3155: gcc -o conftest -g -O2 -Wall -pedantic conftest.c -lm >&5 299 | configure:3161: $? = 0 300 | configure:3165: test -z 301 | || test ! -s conftest.err 302 | configure:3168: $? = 0 303 | configure:3171: test -s conftest 304 | configure:3174: $? = 0 305 | configure:3186: result: yes 306 | configure:3098: checking for hypot 307 | configure:3155: gcc -o conftest -g -O2 -Wall -pedantic conftest.c -lm >&5 308 | conftest.c:48: warning: conflicting types for built-in function 'hypot' 309 | configure:3161: $? = 0 310 | configure:3165: test -z 311 | || test ! -s conftest.err 312 | configure:3168: $? = 0 313 | configure:3171: test -s conftest 314 | configure:3174: $? = 0 315 | configure:3186: result: yes 316 | configure:3303: creating ./config.status 317 | 318 | ## ---------------------- ## 319 | ## Running config.status. ## 320 | ## ---------------------- ## 321 | 322 | This file was extended by config.status, which was 323 | generated by GNU Autoconf 2.59. Invocation command line was 324 | 325 | CONFIG_FILES = 326 | CONFIG_HEADERS = 327 | CONFIG_LINKS = 328 | CONFIG_COMMANDS = 329 | $ ./config.status 330 | 331 | on eepc-tsar16 332 | 333 | config.status:652: creating makefile 334 | config.status:756: creating config.h 335 | config.status:870: config.h is unchanged 336 | 337 | ## ---------------- ## 338 | ## Cache variables. ## 339 | ## ---------------- ## 340 | 341 | ac_cv_c_compiler_gnu=yes 342 | ac_cv_env_CC_set= 343 | ac_cv_env_CC_value= 344 | ac_cv_env_CFLAGS_set= 345 | ac_cv_env_CFLAGS_value= 346 | ac_cv_env_CPPFLAGS_set= 347 | ac_cv_env_CPPFLAGS_value= 348 | ac_cv_env_CPP_set= 349 | ac_cv_env_CPP_value= 350 | ac_cv_env_LDFLAGS_set= 351 | ac_cv_env_LDFLAGS_value= 352 | ac_cv_env_build_alias_set= 353 | ac_cv_env_build_alias_value= 354 | ac_cv_env_host_alias_set= 355 | ac_cv_env_host_alias_value= 356 | ac_cv_env_target_alias_set= 357 | ac_cv_env_target_alias_value= 358 | ac_cv_exeext= 359 | ac_cv_func_hypot=yes 360 | ac_cv_func_realloc=yes 361 | ac_cv_func_strtod=yes 362 | ac_cv_func_strtok=yes 363 | ac_cv_header_inttypes_h=yes 364 | ac_cv_header_math_h=yes 365 | ac_cv_header_memory_h=yes 366 | ac_cv_header_stdc=yes 367 | ac_cv_header_stdint_h=yes 368 | ac_cv_header_stdlib_h=yes 369 | ac_cv_header_string_h=yes 370 | ac_cv_header_strings_h=yes 371 | ac_cv_header_sys_stat_h=yes 372 | ac_cv_header_sys_types_h=yes 373 | ac_cv_header_unistd_h=yes 374 | ac_cv_lib_m=ac_cv_lib_m_main 375 | ac_cv_lib_m_main=yes 376 | ac_cv_objext=o 377 | ac_cv_path_install='/usr/bin/install -c' 378 | ac_cv_prog_AR=ar 379 | ac_cv_prog_CPP='gcc -E' 380 | ac_cv_prog_ac_ct_CC=gcc 381 | ac_cv_prog_cc_g=yes 382 | ac_cv_prog_cc_stdc= 383 | ac_cv_prog_egrep='grep -E' 384 | 385 | ## ----------------- ## 386 | ## Output variables. ## 387 | ## ----------------- ## 388 | 389 | AR='ar' 390 | CC='gcc' 391 | CFLAGS='-g -O2 -Wall -pedantic' 392 | CFLAGS_TRIANGLE='-g -O2 -Wall -pedantic -w -ffloat-store' 393 | CFLAGS_VULNERABLE='-fno-force-mem -ffloat-store' 394 | CPP='gcc -E' 395 | CPPFLAGS='' 396 | DEFS='-DHAVE_CONFIG_H' 397 | ECHO_C='' 398 | ECHO_N='-n' 399 | ECHO_T='' 400 | EGREP='grep -E' 401 | EXEEXT='' 402 | INSTALL_DATA='${INSTALL} -m 644' 403 | INSTALL_PROGRAM='${INSTALL}' 404 | INSTALL_SCRIPT='${INSTALL}' 405 | LDFLAGS='' 406 | LIBOBJS='' 407 | LIBS='-lm ' 408 | LTLIBOBJS='' 409 | OBJEXT='o' 410 | PACKAGE_BUGREPORT='' 411 | PACKAGE_NAME='' 412 | PACKAGE_STRING='' 413 | PACKAGE_TARNAME='' 414 | PACKAGE_VERSION='' 415 | PATH_SEPARATOR=':' 416 | SHELL='/bin/sh' 417 | ac_ct_CC='gcc' 418 | bindir='${exec_prefix}/bin' 419 | build_alias='' 420 | datadir='${prefix}/share' 421 | exec_prefix='${prefix}' 422 | host_alias='' 423 | includedir='${prefix}/include' 424 | infodir='${prefix}/info' 425 | libdir='/usr/local/lib' 426 | libexecdir='${exec_prefix}/libexec' 427 | localstatedir='${prefix}/var' 428 | mandir='${prefix}/man' 429 | oldincludedir='/usr/include' 430 | prefix='/usr/local' 431 | program_transform_name='s,x,x,' 432 | sbindir='${exec_prefix}/sbin' 433 | sharedstatedir='${prefix}/com' 434 | sysconfdir='${prefix}/etc' 435 | target_alias='' 436 | 437 | ## ----------- ## 438 | ## confdefs.h. ## 439 | ## ----------- ## 440 | 441 | #define HAVE_HYPOT 1 442 | #define HAVE_INTTYPES_H 1 443 | #define HAVE_LIBM 1 444 | #define HAVE_MATH_H 1 445 | #define HAVE_MEMORY_H 1 446 | #define HAVE_REALLOC 1 447 | #define HAVE_STDINT_H 1 448 | #define HAVE_STDLIB_H 1 449 | #define HAVE_STRINGS_H 1 450 | #define HAVE_STRING_H 1 451 | #define HAVE_STRTOD 1 452 | #define HAVE_STRTOK 1 453 | #define HAVE_SYS_STAT_H 1 454 | #define HAVE_SYS_TYPES_H 1 455 | #define HAVE_UNISTD_H 1 456 | #define PACKAGE_BUGREPORT "" 457 | #define PACKAGE_NAME "" 458 | #define PACKAGE_STRING "" 459 | #define PACKAGE_TARNAME "" 460 | #define PACKAGE_VERSION "" 461 | #define STDC_HEADERS 1 462 | 463 | configure: exit 0 464 | -------------------------------------------------------------------------------- /natural_neighbour/configure.in: -------------------------------------------------------------------------------- 1 | dnl Configure.in for nn 2 | 3 | AC_INIT(CUSTOMISE) 4 | AC_CONFIG_HEADER(config.h) 5 | 6 | AC_DEFUN(AC_FIND_HEADER, 7 | [AC_MSG_CHECKING(for $1) 8 | header_path= 9 | found_header=no 10 | # Look for the header file in a standard set of common directories. 11 | for ac_dir in \ 12 | $includedir \ 13 | $prefix/include \ 14 | $secondary_prefix/include \ 15 | /usr/include \ 16 | /usr/include/sys \ 17 | /opt/gnu/include \ 18 | /opt/misc/include \ 19 | /usr/local/include \ 20 | ; \ 21 | do 22 | if test -r "$ac_dir/$1"; then 23 | header_path=$ac_dir 24 | found_header=yes 25 | break 26 | fi 27 | done 28 | AC_MSG_RESULT($found_header) 29 | 30 | test "$found_header" = yes && $2 31 | test "$found_header" = no && $3 32 | ]) 33 | 34 | AC_DEFUN(AC_FIND_LIB, 35 | [AC_MSG_CHECKING(for -l$1) 36 | rqst_lib=$1 37 | lib_path= 38 | found_lib=no 39 | # Look for the library file in a standard set of common directories. 40 | for ac_dir in \ 41 | $libdir \ 42 | $prefix/lib${LIBEXT} \ 43 | $secondary_prefix/lib${LIBEXT} \ 44 | /usr/lib${LIBEXT} \ 45 | /usr/unsupported/lib${LIBEXT} \ 46 | /opt/gnu/lib${LIBEXT} \ 47 | /opt/misc/lib${LIBEXT} \ 48 | /usr/local/lib${LIBEXT} \ 49 | ; \ 50 | do 51 | for ac_extension in a so sl; do 52 | if test -r $ac_dir/lib${rqst_lib}.$ac_extension; then 53 | lib_path=$ac_dir 54 | found_lib=yes 55 | break 2 56 | fi 57 | done 58 | done 59 | AC_MSG_RESULT($found_lib) 60 | 61 | test "$found_lib" = yes && $2 62 | test "$found_lib" = no && $3 63 | ]) 64 | 65 | dnl Get the shell variable to override local customisations. 66 | AC_DEFUN([AC_CUSTOMISE], 67 | [dnl 68 | AC_BEFORE([$0], [AC_DEFAULT])dnl 69 | if test -r CUSTOMISE; then 70 | . ./CUSTOMISE 71 | fi 72 | ]) 73 | 74 | AC_CUSTOMISE 75 | 76 | dnl Checks for programs. 77 | AC_PROG_CC 78 | AC_PROG_INSTALL 79 | AC_CHECK_PROG(AR, ar, ar, :) 80 | 81 | dnl Checks for header files. 82 | AC_HEADER_STDC 83 | AC_CHECK_HEADERS(math.h,,AC_MSG_ERROR([unable to find header])) 84 | 85 | dnl Checks for typedefs, structures, and compiler characteristics. 86 | 87 | dnl Check for libraries. 88 | AC_HAVE_LIBRARY(m,,AC_MSG_ERROR([unable to find library])) 89 | 90 | dnl Checks for library functions. 91 | AC_CHECK_FUNCS(realloc strtod strtok hypot,,AC_MSG_ERROR([unable to find function])) 92 | 93 | AC_SUBST(CC) 94 | AC_SUBST(CFLAGS) 95 | AC_SUBST(LDFLAGS) 96 | AC_SUBST(CFLAGS_TRIANGLE) 97 | AC_SUBST(CFLAGS_VULNERABLE) 98 | 99 | AC_OUTPUT(makefile) 100 | -------------------------------------------------------------------------------- /natural_neighbour/delaunay.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: delaunay.h 4 | * 5 | * Created: 04/08/2000 6 | * 7 | * Author: Pavel Sakov 8 | * CSIRO Marine Research 9 | * 10 | * Purpose: Header for delaunay triangulation wrapper 11 | * 12 | * Description: None 13 | * 14 | * Revisions: None 15 | * 16 | *****************************************************************************/ 17 | 18 | #if !defined(_DELAUNAY_H) 19 | #define _DELAUNAY_H 20 | 21 | #include "nn.h" 22 | 23 | typedef struct { 24 | int vids[3]; 25 | } triangle; 26 | 27 | typedef struct { 28 | int tids[3]; 29 | } triangle_neighbours; 30 | 31 | typedef struct { 32 | double x; 33 | double y; 34 | double r; 35 | } circle; 36 | 37 | #if !defined(_ISTACK_STRUCT) 38 | #define _ISTACK_STRUCT 39 | struct istack; 40 | typedef struct istack istack; 41 | #endif 42 | 43 | #if !defined(_DELAUNAY_STRUCT) 44 | #define _DELAUNAY_STRUCT 45 | struct delaunay; 46 | typedef struct delaunay delaunay; 47 | #endif 48 | 49 | /** Structure to perform the Delaunay triangulation of a given array of points. 50 | * 51 | * Contains a deep copy of the input array of points. 52 | * Contains triangles, circles and edges resulted from the triangulation. 53 | * Contains neighbour triangles for each triangle. 54 | * Contains point to triangle map. 55 | */ 56 | struct delaunay { 57 | int npoints; 58 | point* points; 59 | double xmin; 60 | double xmax; 61 | double ymin; 62 | double ymax; 63 | 64 | int ntriangles; 65 | triangle* triangles; 66 | circle* circles; 67 | triangle_neighbours* neighbours; /* for delaunay_xytoi() */ 68 | 69 | int* n_point_triangles; /* n_point_triangles[i] is number of 70 | * triangles i-th point belongs to */ 71 | int** point_triangles; /* point_triangles[i][j] is index of j-th 72 | * triangle i-th point belongs to */ 73 | 74 | int nedges; 75 | int* edges; /* n-th edge is formed by points[edges[n*2]] 76 | * and points[edges[n*2+1]] */ 77 | 78 | /* 79 | * Work data for delaunay_circles_find(). Placed here for efficiency 80 | * reasons. Should be moved to the procedure if parallelizable code 81 | * needed. 82 | */ 83 | int* flags; 84 | int first_id; /* last search result, used in start up of a 85 | * new search */ 86 | istack* t_in; 87 | istack* t_out; 88 | }; 89 | 90 | /* 91 | * delaunay_build() and delaunay_destroy() belong to "nn.h" 92 | */ 93 | void delaunay_circles_find(delaunay* d, point* p, int* n, int** out); 94 | int delaunay_xytoi(delaunay* d, point* p, int seed); 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /natural_neighbour/examples/1/README: -------------------------------------------------------------------------------- 1 | This example reconstructs the Franke test function from 100, 300 and 1000 2 | random data points in [0,1]^2 square. 3 | 4 | To generate and approximate the data, run ./test.sh. 5 | To visualize, in Matlab run "viewexample". 6 | 7 | Good luck! 8 | Pavel Sakov 9 | -------------------------------------------------------------------------------- /natural_neighbour/examples/1/data-100.txt: -------------------------------------------------------------------------------- 1 | 0.0353607 0.7910449 0.4248156309 2 | 0.0139543 0.3381601 0.8662857365 3 | 0.7784807 0.2248849 0.8440807254 4 | 0.8748343 0.5323201 0.4659344491 5 | 0.1136007 0.0027249 1.118798369 6 | 0.5437143 0.1824801 0.8488054504 7 | 0.2407207 0.5245649 0.6886479542 8 | 0.8205943 0.8886401 0.2287236039 9 | 0.3598407 0.1904049 1.258346278 10 | 0.5054743 0.2508001 0.8825131013 11 | 0.6709607 0.4002449 0.8271951036 12 | 0.3983543 0.8689601 0.3124077981 13 | 0.3740807 0.5540849 0.5906338822 14 | 0.2992343 0.3431201 1.16566718 15 | 0.6692007 0.0519249 0.7148539032 16 | 0.0081143 0.2732801 0.9452174649 17 | 0.7563207 0.2937649 0.9208350136 18 | 0.3249943 0.2594401 1.308618961 19 | 0.8354407 0.6796049 0.3081219235 20 | 0.0498743 0.9016001 0.3874640326 21 | 0.1065607 0.6094449 0.5452059326 22 | 0.9827543 0.7997601 0.1740319516 23 | 0.7696807 0.4832849 0.6784230846 24 | 0.9236343 0.5539201 0.3680535802 25 | 0.0248007 0.7011249 0.4602526114 26 | 0.6725143 0.7640801 0.3358282519 27 | 0.0719207 0.6629649 0.4952200552 28 | 0.0293943 0.0302401 1.005778303 29 | 0.1110407 0.7688049 0.4506648024 30 | 0.7942743 0.9524001 0.2265332477 31 | 0.3421607 0.4186449 0.8795435144 32 | 0.7671543 0.1305601 0.7170491241 33 | 0.9652807 0.0124849 0.394139226 34 | 0.7480343 0.1647201 0.7788470606 35 | 0.1804007 0.9503249 0.3882958754 36 | 0.5369143 0.6548801 0.4232474299 37 | 0.1875207 0.6321649 0.5418162436 38 | 0.9337943 0.2010401 0.5453138885 39 | 0.1866407 0.4580049 0.8427835976 40 | 0.7386743 0.4032001 0.8509256427 41 | 0.3777607 0.8278449 0.3043459713 42 | 0.7515543 0.8613601 0.2672031731 43 | 0.9608807 0.1416849 0.4477597028 44 | 0.7724343 0.1755201 0.7775614756 45 | 0.1360007 0.7995249 0.4413038246 46 | 0.6013143 0.9456801 0.3057577989 47 | 0.1031207 0.2013649 1.304342697 48 | 0.0381943 0.7718401 0.4332686431 49 | 0.0622407 0.7472049 0.4499006566 50 | 0.8830743 0.2540001 0.7058432285 51 | 0.2133607 0.8370449 0.4295822313 52 | 0.9359543 0.9921601 0.1616318755 53 | 0.7564807 0.8708849 0.2623669328 54 | 0.9968343 0.5863201 0.2522110798 55 | 0.8916007 0.2487249 0.6819651799 56 | 0.8657143 0.6364801 0.3270327077 57 | 0.8187207 0.3705649 0.8347403737 58 | 0.3425943 0.7426401 0.3829806252 59 | 0.7378407 0.6364049 0.4083245817 60 | 0.2274743 0.5048001 0.7309358311 61 | 0.8489607 0.4462449 0.6689218867 62 | 0.3203543 0.5229601 0.6651570691 63 | 0.3520807 0.2000849 1.280728147 64 | 0.4212343 0.3971201 0.8162719539 65 | 0.4472007 0.2979249 0.9373083805 66 | 0.3301143 0.7272801 0.4143036376 67 | 0.3343207 0.1397649 1.302238219 68 | 0.8469943 0.1134401 0.6045275904 69 | 0.2134407 0.1256049 1.438213386 70 | 0.7718743 0.1556001 0.7483438937 71 | 0.2845607 0.6554449 0.5130300235 72 | 0.9047543 0.4537601 0.5508633289 73 | 0.7476807 0.1292849 0.7302336855 74 | 0.0456343 0.6079201 0.523020593 75 | 0.8028007 0.9471249 0.2239741856 76 | 0.9945143 0.2180801 0.4284464441 77 | 0.6499207 0.5089649 0.6239619 78 | 0.5513943 0.8842401 0.3143237805 79 | 0.4890407 0.2148049 0.9179441912 80 | 0.5162743 0.2064001 0.8793184993 81 | 0.5201607 0.4646449 0.6504266555 82 | 0.6891543 0.7845601 0.3206740808 83 | 0.9432807 0.6584849 0.2481790364 84 | 0.8700343 0.2187201 0.6986659882 85 | 0.9584007 0.1963249 0.4900073079 86 | 0.8589143 0.1088801 0.5828731697 87 | 0.7655207 0.4781649 0.6921621338 88 | 0.4557943 0.0550401 0.9504484175 89 | 0.5646407 0.9040049 0.3177084255 90 | 0.4606743 0.6572001 0.4141982834 91 | 0.5557607 0.8738449 0.3129342524 92 | 0.6735543 0.5153601 0.6186359528 93 | 0.9388807 0.7876849 0.1974158989 94 | 0.8944343 0.2295201 0.6582509981 95 | 0.9140007 0.0455249 0.4628122936 96 | 0.9233143 0.3996801 0.5815347305 97 | 0.6811207 0.0473649 0.7044338845 98 | 0.5601943 0.6258401 0.4580375853 99 | 0.4402407 0.1932049 1.026717775 100 | 0.6050743 0.5080001 0.6097852461 101 | -------------------------------------------------------------------------------- /natural_neighbour/examples/1/data-300.txt: -------------------------------------------------------------------------------- 1 | 0.0353607 0.7910449 0.4248156309 2 | 0.0139543 0.3381601 0.8662857365 3 | 0.7784807 0.2248849 0.8440807254 4 | 0.8748343 0.5323201 0.4659344491 5 | 0.1136007 0.0027249 1.118798369 6 | 0.5437143 0.1824801 0.8488054504 7 | 0.2407207 0.5245649 0.6886479542 8 | 0.8205943 0.8886401 0.2287236039 9 | 0.3598407 0.1904049 1.258346278 10 | 0.5054743 0.2508001 0.8825131013 11 | 0.6709607 0.4002449 0.8271951036 12 | 0.3983543 0.8689601 0.3124077981 13 | 0.3740807 0.5540849 0.5906338822 14 | 0.2992343 0.3431201 1.16566718 15 | 0.6692007 0.0519249 0.7148539032 16 | 0.0081143 0.2732801 0.9452174649 17 | 0.7563207 0.2937649 0.9208350136 18 | 0.3249943 0.2594401 1.308618961 19 | 0.8354407 0.6796049 0.3081219235 20 | 0.0498743 0.9016001 0.3874640326 21 | 0.1065607 0.6094449 0.5452059326 22 | 0.9827543 0.7997601 0.1740319516 23 | 0.7696807 0.4832849 0.6784230846 24 | 0.9236343 0.5539201 0.3680535802 25 | 0.0248007 0.7011249 0.4602526114 26 | 0.6725143 0.7640801 0.3358282519 27 | 0.0719207 0.6629649 0.4952200552 28 | 0.0293943 0.0302401 1.005778303 29 | 0.1110407 0.7688049 0.4506648024 30 | 0.7942743 0.9524001 0.2265332477 31 | 0.3421607 0.4186449 0.8795435144 32 | 0.7671543 0.1305601 0.7170491241 33 | 0.9652807 0.0124849 0.394139226 34 | 0.7480343 0.1647201 0.7788470606 35 | 0.1804007 0.9503249 0.3882958754 36 | 0.5369143 0.6548801 0.4232474299 37 | 0.1875207 0.6321649 0.5418162436 38 | 0.9337943 0.2010401 0.5453138885 39 | 0.1866407 0.4580049 0.8427835976 40 | 0.7386743 0.4032001 0.8509256427 41 | 0.3777607 0.8278449 0.3043459713 42 | 0.7515543 0.8613601 0.2672031731 43 | 0.9608807 0.1416849 0.4477597028 44 | 0.7724343 0.1755201 0.7775614756 45 | 0.1360007 0.7995249 0.4413038246 46 | 0.6013143 0.9456801 0.3057577989 47 | 0.1031207 0.2013649 1.304342697 48 | 0.0381943 0.7718401 0.4332686431 49 | 0.0622407 0.7472049 0.4499006566 50 | 0.8830743 0.2540001 0.7058432285 51 | 0.2133607 0.8370449 0.4295822313 52 | 0.9359543 0.9921601 0.1616318755 53 | 0.7564807 0.8708849 0.2623669328 54 | 0.9968343 0.5863201 0.2522110798 55 | 0.8916007 0.2487249 0.6819651799 56 | 0.8657143 0.6364801 0.3270327077 57 | 0.8187207 0.3705649 0.8347403737 58 | 0.3425943 0.7426401 0.3829806252 59 | 0.7378407 0.6364049 0.4083245817 60 | 0.2274743 0.5048001 0.7309358311 61 | 0.8489607 0.4462449 0.6689218867 62 | 0.3203543 0.5229601 0.6651570691 63 | 0.3520807 0.2000849 1.280728147 64 | 0.4212343 0.3971201 0.8162719539 65 | 0.4472007 0.2979249 0.9373083805 66 | 0.3301143 0.7272801 0.4143036376 67 | 0.3343207 0.1397649 1.302238219 68 | 0.8469943 0.1134401 0.6045275904 69 | 0.2134407 0.1256049 1.438213386 70 | 0.7718743 0.1556001 0.7483438937 71 | 0.2845607 0.6554449 0.5130300235 72 | 0.9047543 0.4537601 0.5508633289 73 | 0.7476807 0.1292849 0.7302336855 74 | 0.0456343 0.6079201 0.523020593 75 | 0.8028007 0.9471249 0.2239741856 76 | 0.9945143 0.2180801 0.4284464441 77 | 0.6499207 0.5089649 0.6239619 78 | 0.5513943 0.8842401 0.3143237805 79 | 0.4890407 0.2148049 0.9179441912 80 | 0.5162743 0.2064001 0.8793184993 81 | 0.5201607 0.4646449 0.6504266555 82 | 0.6891543 0.7845601 0.3206740808 83 | 0.9432807 0.6584849 0.2481790364 84 | 0.8700343 0.2187201 0.6986659882 85 | 0.9584007 0.1963249 0.4900073079 86 | 0.8589143 0.1088801 0.5828731697 87 | 0.7655207 0.4781649 0.6921621338 88 | 0.4557943 0.0550401 0.9504484175 89 | 0.5646407 0.9040049 0.3177084255 90 | 0.4606743 0.6572001 0.4141982834 91 | 0.5557607 0.8738449 0.3129342524 92 | 0.6735543 0.5153601 0.6186359528 93 | 0.9388807 0.7876849 0.1974158989 94 | 0.8944343 0.2295201 0.6582509981 95 | 0.9140007 0.0455249 0.4628122936 96 | 0.9233143 0.3996801 0.5815347305 97 | 0.6811207 0.0473649 0.7044338845 98 | 0.5601943 0.6258401 0.4580375853 99 | 0.4402407 0.1932049 1.026717775 100 | 0.6050743 0.5080001 0.6097852461 101 | 0.3913607 0.8830449 0.3299040754 102 | 0.8579543 0.6461601 0.3230608939 103 | 0.7344807 0.5168849 0.6167823297 104 | 0.1188343 0.6403201 0.5233181306 105 | 0.6696007 0.4947249 0.6572523525 106 | 0.1877143 0.0904801 1.358055254 107 | 0.3967207 0.2165649 1.141438439 108 | 0.8645943 0.5966401 0.3761296869 109 | 0.1158407 0.0824049 1.23625913 110 | 0.9494743 0.7588001 0.2000745201 111 | 0.0269607 0.4922449 0.6314318753 112 | 0.2423543 0.1769601 1.495218667 113 | 0.3300807 0.8460849 0.3723810059 114 | 0.5432343 0.4511201 0.6708938032 115 | 0.2252007 0.5439249 0.6540428999 116 | 0.6521143 0.1812801 0.8178751713 117 | 0.9123207 0.9857649 0.1717434828 118 | 0.3689943 0.9674401 0.3632640708 119 | 0.5914407 0.5716049 0.5184473027 120 | 0.4938743 0.4096001 0.7303936255 121 | 0.4625607 0.7014449 0.3279175213 122 | 0.8267543 0.1077601 0.6247190607 123 | 0.7256807 0.7752849 0.3086035096 124 | 0.1676343 0.6619201 0.5163847225 125 | 0.5808007 0.1931249 0.8300953074 126 | 0.3165143 0.6720801 0.4825649178 127 | 0.2279207 0.3549649 1.191174597 128 | 0.0733943 0.7382401 0.4566078438 129 | 0.8670407 0.6608049 0.3026842782 130 | 0.2382743 0.4604001 0.8425630336 131 | 0.6981607 0.5106449 0.6319165753 132 | 0.6111543 0.4385601 0.7220533027 133 | 0.9212807 0.3044849 0.6345429778 134 | 0.9920343 0.2727201 0.4522899642 135 | 0.7364007 0.4423249 0.7784326616 136 | 0.1809143 0.5628801 0.6196176683 137 | 0.3435207 0.3241649 1.129866363 138 | 0.9777943 0.9090401 0.1575812187 139 | 0.9426407 0.3500049 0.5702339276 140 | 0.1826743 0.9112001 0.4022495927 141 | 0.7337607 0.9198449 0.2601423335 142 | 0.5955543 0.1693601 0.814908548 143 | 0.9168807 0.4336849 0.5548300596 144 | 0.0164343 0.2835201 0.9565995619 145 | 0.6920007 0.2915249 0.9128583967 146 | 0.2453143 0.8536801 0.4196719601 147 | 0.2591207 0.8933649 0.4049195834 148 | 0.0821943 0.4798401 0.7073533255 149 | 0.8182407 0.6392049 0.3592064003 150 | 0.3270743 0.7620001 0.3907160831 151 | 0.5693607 0.9290449 0.3165991785 152 | 0.7799543 0.3001601 0.9082526255 153 | 0.7124807 0.1628849 0.7917816453 154 | 0.2408343 0.6943201 0.494410727 155 | 0.4476007 0.7407249 0.2567044449 156 | 0.5097143 0.5444801 0.5522721563 157 | 0.9747207 0.0625649 0.3912382747 158 | 0.3865943 0.4506401 0.7518726249 159 | 0.4938407 0.5284049 0.5733607502 160 | 0.6714743 0.0128001 0.7035109881 161 | 0.2049607 0.5382449 0.6629779746 162 | 0.1643543 0.8309601 0.4313549377 163 | 0.3080807 0.4920849 0.7323676111 164 | 0.6652343 0.5051201 0.635944027 165 | 0.0032007 0.7899249 0.4160654402 166 | 0.9741143 0.6352801 0.2392698827 167 | 0.4903207 0.8317649 0.2522983984 168 | 0.8909943 0.8214401 0.2119095307 169 | 0.9694407 0.0176049 0.3898337771 170 | 0.2158743 0.6636001 0.5176787799 171 | 0.6405607 0.7474449 0.3524942472 172 | 0.7487543 0.7617601 0.3036872195 173 | 0.7036807 0.4212849 0.8130030188 174 | 0.2896343 0.7159201 0.461409671 175 | 0.3588007 0.4391249 0.8079173319 176 | 0.6385143 0.1260801 0.7741547359 177 | 0.8059207 0.2009649 0.7811090475 178 | 0.5953943 0.5922401 0.493483983 179 | 0.2450407 0.1068049 1.39861483 180 | 0.9602743 0.7144001 0.2091733485 181 | 0.8761607 0.5566449 0.423476634 182 | 0.5331543 0.0925601 0.8396896782 183 | 0.8992807 0.9504849 0.182685828 184 | 0.1140343 0.3267201 1.144006045 185 | 0.5144007 0.6883249 0.3689748873 186 | 0.5029143 0.0168801 0.8710781386 187 | 0.9215207 0.1701649 0.5421684452 188 | 0.4997943 0.7630401 0.2554275162 189 | 0.3206407 0.7960049 0.3850131309 190 | 0.9046743 0.1652001 0.5692251891 191 | 0.9117607 0.9658449 0.1751153138 192 | 0.5175543 0.8233601 0.269430393 193 | 0.8948807 0.0796849 0.5065746624 194 | 0.1384343 0.3375201 1.165463974 195 | 0.4700007 0.5375249 0.5688979109 196 | 0.5673143 0.3076801 0.8295925349 197 | 0.8371207 0.7393649 0.2685741216 198 | 0.6041943 0.3338401 0.8349471632 199 | 0.1962407 0.0852049 1.353484864 200 | 0.0490743 0.0160001 1.032038449 201 | 0.7473607 0.9750449 0.2415634823 202 | 0.7019543 0.9541601 0.2654141229 203 | 0.6904807 0.8088849 0.3108832712 204 | 0.3628343 0.7483201 0.3457208454 205 | 0.2256007 0.9867249 0.3767935673 206 | 0.8317143 0.9984801 0.2018775112 207 | 0.5527207 0.9085649 0.3184471635 208 | 0.9085943 0.3046401 0.6673862182 209 | 0.8718407 0.9744049 0.1898141511 210 | 0.3934743 0.2668001 1.107182851 211 | 0.3829607 0.5842449 0.5489519002 212 | 0.0863543 0.4849601 0.7018189741 213 | 0.2860807 0.1380849 1.40362197 214 | 0.7872343 0.5591201 0.5046146502 215 | 0.7812007 0.0359249 0.6131561297 216 | 0.2961143 0.0892801 1.308569501 217 | 0.0683207 0.6777649 0.4855782908 218 | 0.4129943 0.6754401 0.3993914032 219 | 0.3474407 0.4636049 0.7641987541 220 | 0.9378743 0.9176001 0.1723150543 221 | 0.8185607 0.7934449 0.2558081105 222 | 0.6707543 0.4157601 0.8033446478 223 | 0.6816807 0.0672849 0.7135640925 224 | 0.4116343 0.7699201 0.2508937453 225 | 0.1368007 0.6851249 0.496933668 226 | 0.9605143 0.5800801 0.2948416921 227 | 0.3839207 0.0469649 1.08223445 228 | 0.1173943 0.4462401 0.8193864647 229 | 0.6230407 0.5528049 0.5460901835 230 | 0.6822743 0.9684001 0.2700707686 231 | 0.0541607 0.6026449 0.5308576677 232 | 0.4551543 0.7465601 0.2474826289 233 | 0.8772807 0.5964849 0.364136574 234 | 0.2360343 0.3807201 1.10055635 235 | 0.2924007 0.9343249 0.3877153521 236 | 0.8249143 0.4708801 0.6549327247 237 | 0.4995207 0.0161649 0.8755479973 238 | 0.0217943 0.6170401 0.5060826202 239 | 0.6986407 0.2420049 0.885772547 240 | 0.6266743 0.4192001 0.7630032573 241 | 0.0897607 0.0118449 1.094979132 242 | 0.4395543 0.4773601 0.6607372509 243 | 0.8728807 0.7256849 0.255212312 244 | 0.2604343 0.3915201 1.052044491 245 | 0.2480007 0.7835249 0.4445798548 246 | 0.8893143 0.7616801 0.2306309289 247 | 0.4151207 0.5853649 0.5334087204 248 | 0.1261943 0.1878401 1.369458937 249 | 0.5742407 0.5312049 0.5689770519 250 | 0.7710743 0.2700001 0.8988191792 251 | 0.9253607 0.0210449 0.4416243344 252 | 0.6239543 0.6081601 0.4728890718 253 | 0.6684807 0.4548849 0.7331718676 254 | 0.4848343 0.8023201 0.2313456094 255 | 0.0036007 0.2327249 0.9707929397 256 | 0.1537143 0.4524801 0.8381381112 257 | 0.1307207 0.7545649 0.4600916469 258 | 0.4305943 0.1586401 1.049491913 259 | 0.2498407 0.4204049 0.9615837161 260 | 0.1154743 0.5208001 0.6605154105 261 | 0.5609607 0.6302449 0.4532155106 262 | 0.0083543 0.1389601 1.00762604 263 | 0.2640807 0.7840849 0.4379680698 264 | 0.9092343 0.6131201 0.3139897354 265 | 0.5592007 0.2819249 0.8392343119 266 | 0.6181143 0.5432801 0.5594714154 267 | 0.6463207 0.5237649 0.5968404364 268 | 0.9349943 0.5294401 0.3847319866 269 | 0.7254407 0.9096049 0.2663083849 270 | 0.6598743 0.1716001 0.808812903 271 | 0.9965607 0.8394449 0.1607760289 272 | 0.5927543 0.0697601 0.7759083763 273 | 0.6596807 0.7132849 0.3683772089 274 | 0.5336343 0.8239201 0.2845986253 275 | 0.9148007 0.9311249 0.1795613207 276 | 0.2825143 0.0340801 1.223870933 277 | 0.9619207 0.8929649 0.1664327884 278 | 0.6393943 0.3002401 0.8757366642 279 | 0.0010407 0.9988049 0.3438766345 280 | 0.4042743 0.2224001 1.116357132 281 | 0.2321607 0.6486449 0.5285300901 282 | 0.3771543 0.4005601 0.873869451 283 | 0.8552807 0.2424849 0.7545037788 284 | 0.3580343 0.4347201 0.8191798817 285 | 0.0704007 0.1803249 1.203567 286 | 0.1469143 0.9248801 0.3947750112 287 | 0.0775207 0.8621649 0.4074054324 288 | 0.5437943 0.4710401 0.6440392365 289 | 0.0766407 0.6880049 0.4822331662 290 | 0.3486743 0.6732001 0.4575062578 291 | 0.2677607 0.0578449 1.283261249 292 | 0.3615543 0.1313601 1.223036977 293 | 0.8508807 0.3716849 0.7768330064 294 | 0.3824343 0.4455201 0.7667946897 295 | 0.0260007 0.0295249 0.9991654063 296 | 0.2113143 0.2156801 1.502396735 297 | 0.9931207 0.4313649 0.3935614782 298 | 0.6481943 0.0418401 0.7271095074 299 | 0.9522407 0.9772049 0.1575928887 300 | 0.4930743 0.5240001 0.5786824894 301 | -------------------------------------------------------------------------------- /natural_neighbour/examples/1/generate.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | r = 1; 3 | T = 10000000; 4 | N = 100; 5 | } 6 | 7 | { 8 | for (i = 0; i < N; ++i) { 9 | x = random(); 10 | y = random(); 11 | xx = x * 9.0; 12 | yy = y * 9.0; 13 | z = 0.75 * exp(- (xx-2) * (xx-2) / 4 - (yy-2) * (yy-2) / 4) + 0.75 * exp(- (xx-2) * (xx-2) / 49 - (yy-2) / 10) + 0.5 * exp(- (xx-7) * (xx-7) / 4 - (yy-3) * (yy-3) / 4) - 0.2 * exp(- (xx-4) * (xx-4) - (yy-7)*(yy-7)); 14 | printf("%.10g %.10g %.10g\n", x, y, z); 15 | } 16 | } 17 | 18 | END { 19 | } 20 | 21 | # One could use in-built generator rand(), but its output may depend on the 22 | # awk implementation used... 23 | # 24 | function random() 25 | { 26 | r = (r * 40353607) % T; 27 | return r / T; 28 | } 29 | -------------------------------------------------------------------------------- /natural_neighbour/examples/1/makefile: -------------------------------------------------------------------------------- 1 | all: 2 | ./test.sh 3 | clean: 4 | rm -f data-*.txt out-*.txt *~ core *.ps 5 | -------------------------------------------------------------------------------- /natural_neighbour/examples/1/suptitle.m: -------------------------------------------------------------------------------- 1 | function hout=suptitle(str) 2 | %SUPTITLE Puts a title above all subplots. 3 | % SUPTITLE('text') adds text to the top of the figure 4 | % above all subplots (a "super title"). Use this function 5 | % after all subplot commands. 6 | 7 | % Drea Thomas 6/15/95 drea@mathworks.com 8 | % John Cristion 12/13/00 modified 9 | % Mark Histed 03/13/04 histed@mit.edu: fix disappearing legend on last plot 10 | % 11 | % $Id$ 12 | 13 | % Warning: If the figure or axis units are non-default, this 14 | % will break. 15 | 16 | 17 | 18 | % Parameters used to position the supertitle. 19 | 20 | % Amount of the figure window devoted to subplots 21 | plotregion = .92; 22 | 23 | % Y position of title in normalized coordinates 24 | titleypos = .95; 25 | 26 | % Fontsize for supertitle 27 | %fs = get(gcf,'defaultaxesfontsize')+4; 28 | 29 | fs = get(gcf,'defaultaxesfontsize'); 30 | 31 | % Fudge factor to adjust y spacing between subplots 32 | fudge=1; 33 | 34 | haold = gca; 35 | figunits = get(gcf,'units'); 36 | 37 | % Get the (approximate) difference between full height (plot + title 38 | % + xlabel) and bounding rectangle. 39 | 40 | if (~strcmp(figunits,'pixels')), 41 | set(gcf,'units','pixels'); 42 | pos = get(gcf,'position'); 43 | set(gcf,'units',figunits); 44 | else, 45 | pos = get(gcf,'position'); 46 | end 47 | ff = (fs-4)*1.27*5/pos(4)*fudge; 48 | 49 | % The 5 here reflects about 3 characters of height below 50 | % an axis and 2 above. 1.27 is pixels per point. 51 | 52 | % Determine the bounding rectange for all the plots 53 | 54 | % h = findobj('Type','axes'); 55 | 56 | % findobj is a 4.2 thing.. if you don't have 4.2 comment out 57 | % the next line and uncomment the following block. 58 | 59 | h = findobj(gcf,'Type','axes'); % Change suggested by Stacy J. Hills 60 | 61 | % If you don't have 4.2, use this code instead 62 | %ch = get(gcf,'children'); 63 | %h=[]; 64 | %for i=1:length(ch), 65 | % if strcmp(get(ch(i),'type'),'axes'), 66 | % h=[h,ch(i)]; 67 | % end 68 | %end 69 | 70 | 71 | 72 | 73 | max_y=0; 74 | min_y=1; 75 | 76 | oldtitle =0; 77 | for i=1:length(h), 78 | if (~strcmp(get(h(i),'Tag'),'suptitle')), 79 | pos=get(h(i),'pos'); 80 | if (pos(2) < min_y), min_y=pos(2)-ff/5*3;end; 81 | if (pos(4)+pos(2) > max_y), max_y=pos(4)+pos(2)+ff/5*2;end; 82 | else, 83 | oldtitle = h(i); 84 | end 85 | end 86 | 87 | if max_y > plotregion, 88 | scale = (plotregion-min_y)/(max_y-min_y); 89 | for i=1:length(h), 90 | pos = get(h(i),'position'); 91 | pos(2) = (pos(2)-min_y)*scale+min_y; 92 | pos(4) = pos(4)*scale-(1-scale)*ff/5*3; 93 | set(h(i),'position',pos); 94 | end 95 | end 96 | 97 | np = get(gcf,'nextplot'); 98 | set(gcf,'nextplot','add'); 99 | if (oldtitle), 100 | delete(oldtitle); 101 | end 102 | ha=axes('pos',[0 1 1 1],'visible','off','Tag','suptitle'); 103 | ht=text(.5,titleypos-1,str);set(ht,'horizontalalignment','center','fontsize',fs); 104 | set(gcf,'nextplot',np); 105 | axes(haold); 106 | 107 | % fix legend if one exists 108 | legH = legend; 109 | if ~isempty(legH) 110 | axes(legH); 111 | end 112 | 113 | if nargout, 114 | hout=ht; 115 | end 116 | 117 | -------------------------------------------------------------------------------- /natural_neighbour/examples/1/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ ! -x ../../nnbathy ] 4 | then 5 | echo "error: no executable found" 6 | echo 'Run "./configure" and "make" in the source directory' 7 | exit 1 8 | fi 9 | 10 | echo " Example of Natural Neighbours interpolation:" 11 | echo " Franke test function reconstruction by 100, 300 and 1000 random points" 12 | 13 | for N in 100 300 1000 14 | do 15 | echo " N = $N:" 16 | echo -n " Generating..." 17 | echo "1" | awk -f ./generate.awk N=$N > data-${N}.txt 18 | echo "done" 19 | echo -n " Interpolating into 256x256 grid..." 20 | ../../nnbathy -i data-$N.txt -n 256x256 > out-${N}.txt 21 | echo "done" 22 | done 23 | 24 | echo " Finished" 25 | echo " To visualize, in Matlab run \"viewexample\"" 26 | -------------------------------------------------------------------------------- /natural_neighbour/examples/1/viewexample.m: -------------------------------------------------------------------------------- 1 | function viewexample() 2 | 3 | subplot(3, 1, 1); 4 | view('data-100.txt', 'out-100.txt'); 5 | title('100 data points'); 6 | pause(0.1); 7 | 8 | subplot(3, 1, 2); 9 | view('data-300.txt', 'out-300.txt'); 10 | title('300 data points'); 11 | pause(0.1); 12 | 13 | subplot(3, 1, 3); 14 | view('data-1000.txt', 'out-1000.txt'); 15 | title('1000 data points'); 16 | 17 | suptitle('Interpolation of Franke test function using nnbathy'); 18 | 19 | return 20 | 21 | function view(data, output) 22 | 23 | N = 256; 24 | for i = 0:15 25 | V(i+1) = i * 0.1; 26 | end 27 | points = load(output); 28 | k = 1; 29 | x = zeros(N, N); 30 | y = zeros(N, N); 31 | z = zeros(N, N); 32 | z1 = zeros(N, N); 33 | for j = N:-1:1 34 | for i = 1:N 35 | x(j, i) = points(k, 1); 36 | y(j, i) = points(k, 2); 37 | z(j, i) = points(k, 3); 38 | xx = x(j, i) * 9.0; 39 | yy = y(j, i) * 9.0; 40 | z1(j, i) = 0.75 * exp(- (xx-2.0) * (xx-2.0) / 4.0 - (yy-2.0) * (yy-2.0) / 4.0) + 0.75 * exp(- (xx-2.0) * (xx-2.0) / 49.0 - (yy-2.0) / 10.0) + 0.5 * exp(- (xx-7.0) * (xx-7.0) / 4.0 - (yy-3.0) * (yy-3.0) / 4.0) - 0.2 * exp(- (xx-4.0) * (xx-4.0) - (yy-7.0)*(yy-7.0)); 41 | k = k + 1; 42 | end 43 | end 44 | 45 | contour(x, y, z1, V, 'k'); 46 | hold on; 47 | [c, h] = contour(x, y, z, V); 48 | clabel(c, h); 49 | 50 | points = load(data); 51 | x = points(:, 1); 52 | y = points(:, 2); 53 | plot(x, y, 'k+', 'markersize', 3); 54 | axis([0 1 0 1]); 55 | axis square; 56 | 57 | return 58 | -------------------------------------------------------------------------------- /natural_neighbour/examples/2/README: -------------------------------------------------------------------------------- 1 | This example represents interpolation of some real-life bathymetry data 2 | along the shiptrack. 3 | 4 | It is quite tough for interpolation because of the instrumental round-up 5 | of the position of the ship (resulting in a lot of close/duplicate data 6 | points and in data points aligned to nodes of a regular grid) and because 7 | of clustering of data along the track (resulting in a lot of very thin 8 | triangles). 9 | 10 | Early versions if `nn' miserably failed for this data set when interpolating 11 | the raw data, and were dealt with by replacing data in each cell of a 256x256 12 | subgrid by an averaged value by means of "-d" option of `nnbathy'. The current 13 | version of `nn' is doing much better job; still "-d" option is used because 14 | it speeds up things and because this is the right way to deal with such 15 | clustered data. 16 | 17 | I need to add that `triangle' really shines in this example. 18 | 19 | To reproduce the example, run "./test.sh" or "make". 20 | To visualize, in Matlab run "viewexample". 21 | To clean up, run "make clean" 22 | 23 | Good luck! 24 | Pavel Sakov 25 | -------------------------------------------------------------------------------- /natural_neighbour/examples/2/makefile: -------------------------------------------------------------------------------- 1 | all: 2 | ./test.sh 3 | clean: 4 | rm -f lin*.txt nn-*.txt *~ core 5 | -------------------------------------------------------------------------------- /natural_neighbour/examples/2/suptitle.m: -------------------------------------------------------------------------------- 1 | function hout=suptitle(str) 2 | %SUPTITLE Puts a title above all subplots. 3 | % SUPTITLE('text') adds text to the top of the figure 4 | % above all subplots (a "super title"). Use this function 5 | % after all subplot commands. 6 | 7 | % Drea Thomas 6/15/95 drea@mathworks.com 8 | % John Cristion 12/13/00 modified 9 | % Mark Histed 03/13/04 histed@mit.edu: fix disappearing legend on last plot 10 | % 11 | % $Id$ 12 | 13 | % Warning: If the figure or axis units are non-default, this 14 | % will break. 15 | 16 | 17 | 18 | % Parameters used to position the supertitle. 19 | 20 | % Amount of the figure window devoted to subplots 21 | plotregion = .92; 22 | 23 | % Y position of title in normalized coordinates 24 | titleypos = .95; 25 | 26 | % Fontsize for supertitle 27 | %fs = get(gcf,'defaultaxesfontsize')+4; 28 | 29 | fs = get(gcf,'defaultaxesfontsize'); 30 | 31 | % Fudge factor to adjust y spacing between subplots 32 | fudge=1; 33 | 34 | haold = gca; 35 | figunits = get(gcf,'units'); 36 | 37 | % Get the (approximate) difference between full height (plot + title 38 | % + xlabel) and bounding rectangle. 39 | 40 | if (~strcmp(figunits,'pixels')), 41 | set(gcf,'units','pixels'); 42 | pos = get(gcf,'position'); 43 | set(gcf,'units',figunits); 44 | else, 45 | pos = get(gcf,'position'); 46 | end 47 | ff = (fs-4)*1.27*5/pos(4)*fudge; 48 | 49 | % The 5 here reflects about 3 characters of height below 50 | % an axis and 2 above. 1.27 is pixels per point. 51 | 52 | % Determine the bounding rectange for all the plots 53 | 54 | % h = findobj('Type','axes'); 55 | 56 | % findobj is a 4.2 thing.. if you don't have 4.2 comment out 57 | % the next line and uncomment the following block. 58 | 59 | h = findobj(gcf,'Type','axes'); % Change suggested by Stacy J. Hills 60 | 61 | % If you don't have 4.2, use this code instead 62 | %ch = get(gcf,'children'); 63 | %h=[]; 64 | %for i=1:length(ch), 65 | % if strcmp(get(ch(i),'type'),'axes'), 66 | % h=[h,ch(i)]; 67 | % end 68 | %end 69 | 70 | 71 | 72 | 73 | max_y=0; 74 | min_y=1; 75 | 76 | oldtitle =0; 77 | for i=1:length(h), 78 | if (~strcmp(get(h(i),'Tag'),'suptitle')), 79 | pos=get(h(i),'pos'); 80 | if (pos(2) < min_y), min_y=pos(2)-ff/5*3;end; 81 | if (pos(4)+pos(2) > max_y), max_y=pos(4)+pos(2)+ff/5*2;end; 82 | else, 83 | oldtitle = h(i); 84 | end 85 | end 86 | 87 | if max_y > plotregion, 88 | scale = (plotregion-min_y)/(max_y-min_y); 89 | for i=1:length(h), 90 | pos = get(h(i),'position'); 91 | pos(2) = (pos(2)-min_y)*scale+min_y; 92 | pos(4) = pos(4)*scale-(1-scale)*ff/5*3; 93 | set(h(i),'position',pos); 94 | end 95 | end 96 | 97 | np = get(gcf,'nextplot'); 98 | set(gcf,'nextplot','add'); 99 | if (oldtitle), 100 | delete(oldtitle); 101 | end 102 | ha=axes('pos',[0 1 1 1],'visible','off','Tag','suptitle'); 103 | ht=text(.5,titleypos-1,str);set(ht,'horizontalalignment','center','fontsize',fs); 104 | set(gcf,'nextplot',np); 105 | axes(haold); 106 | 107 | % fix legend if one exists 108 | legH = legend; 109 | if ~isempty(legH) 110 | axes(legH); 111 | end 112 | 113 | if nargout, 114 | hout=ht; 115 | end 116 | 117 | -------------------------------------------------------------------------------- /natural_neighbour/examples/2/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ ! -x ../../nnbathy ] 4 | then 5 | echo "error: no executable found" 6 | echo 'Run "./configure" and "make" in the source directory' 7 | exit 1 8 | fi 9 | 10 | echo "" 11 | echo -n "Linear interpolation..." 12 | ../../nnbathy -i data.txt -n 256x256 -P alg=l > lin.txt 13 | echo "done" 14 | echo -n "Natural Neighbours Sibson interpolation..." 15 | ../../nnbathy -i data.txt -n 256x256 > nn-inf.txt 16 | echo "done" 17 | echo -n "Natural Neighbours Sibson interpolation with wmin = 0..." 18 | ../../nnbathy -i data.txt -n 256x256 -W 0 > nn-0.txt 19 | echo "done" 20 | echo -n "Natural Neighbours Non-Sibsonian interpolation with wmin = 0..." 21 | ../../nnbathy -i data.txt -n 256x256 -W 0 -P alg=ns > nn-ns.txt 22 | echo "done" 23 | echo "" 24 | echo 'To visualize, in Matlab run "viewexample"' 25 | -------------------------------------------------------------------------------- /natural_neighbour/examples/2/viewdata.m: -------------------------------------------------------------------------------- 1 | function [] = viewdata(fname, verbose) 2 | 3 | if nargin == 1 4 | verbose = 1; 5 | end 6 | 7 | if verbose 8 | fprintf('plotting data from "%s"\n', fname); 9 | fprintf(' reading %s...', fname); 10 | end 11 | data = load(fname); 12 | x = data(:, 1); 13 | y = data(:, 2); 14 | z = data(:, 3); 15 | clear data; 16 | 17 | if verbose 18 | fprintf('\n'); 19 | end 20 | 21 | if verbose 22 | fprintf(' plotting...'); 23 | end 24 | 25 | xmin = min(x); 26 | xmax = max(x); 27 | ymin = min(y); 28 | ymax = max(y); 29 | zmin = min(z); 30 | zmax = max(z); 31 | n = length(z); 32 | 33 | map = colormap; 34 | axis([xmin xmax ymin ymax]); 35 | axis square; 36 | set(gca, 'box', 'on'); 37 | hold on; 38 | for i = 1 : n 39 | plot(x(i), y(i), 's-', 'color', zcolor(z(i), zmin, zmax, map), 'markersize', 2); 40 | end 41 | 42 | if verbose 43 | fprintf('\n'); 44 | end 45 | 46 | return 47 | 48 | function c = zcolor(z, zmin, zmax, map) 49 | 50 | ind = floor((z - zmin) / (zmax - zmin) * 64 + 1); 51 | ind = min(ind, 64); 52 | c = map(ind, :); 53 | 54 | return 55 | 56 | 57 | -------------------------------------------------------------------------------- /natural_neighbour/examples/2/viewexample.m: -------------------------------------------------------------------------------- 1 | figure 2 | 3 | subplot(3, 2, 1); 4 | fname = 'data.txt'; 5 | fprintf('plotting data points from "%s"\n', fname); 6 | fprintf(' reading %s...', fname); 7 | data = load(fname); 8 | xrange = range(data(:, 1)); 9 | yrange = range(data(:, 2)); 10 | zrange = range(data(:, 3)); 11 | fprintf('\n'); 12 | fprintf(' plotting...'); 13 | axis([xrange yrange]); 14 | axis tight; 15 | axis square; 16 | set(gca, 'box', 'on'); 17 | hold on; 18 | plot(data(:, 1), data(:, 2), 'k.', 'markersize', 1); 19 | fprintf('\n'); 20 | clear data; 21 | title('Data points'); 22 | pause(0.1); 23 | 24 | subplot(3, 2, 2); 25 | viewdata('data.txt'); 26 | title('Data'); 27 | pause(0.1); 28 | 29 | subplot(3, 2, 3); 30 | viewinterp('data.txt', 'lin.txt'); 31 | title('Linear interpolation'); 32 | pause(0.1); 33 | 34 | subplot(3, 2, 4); 35 | viewinterp('data.txt', 'nn-inf.txt'); 36 | caxis(zrange); 37 | title(sprintf('Natural Neighbours interpolation\n(extrapolation allowed)')); 38 | pause(0.1); 39 | 40 | subplot(3, 2, 5); 41 | viewinterp('data.txt', 'nn-0.txt'); 42 | caxis(zrange); 43 | title(sprintf('Natural Neighbours interpolation\n(interpolation only)')); 44 | pause(0.1); 45 | 46 | subplot(3, 2, 6); 47 | viewinterp('data.txt', 'nn-ns.txt'); 48 | caxis(zrange); 49 | title(sprintf('Non-Sibsonian NN interpolation\n(interpolation only)')); 50 | pause(0.1); 51 | 52 | suptitle('Interpolation of bathymetry data from sonar using nnbathy'); 53 | -------------------------------------------------------------------------------- /natural_neighbour/examples/2/viewinterp.m: -------------------------------------------------------------------------------- 1 | function [h] = viewinterp(fin, fout, verbose) 2 | 3 | if nargin == 2 4 | verbose = 1; 5 | end 6 | 7 | if verbose 8 | fprintf('plotting data from "%s" and "%s"\n', fin, fout); 9 | fprintf(' reading "%s"...', fin); 10 | end 11 | 12 | data = load(fin); 13 | xin = data(:, 1); 14 | yin = data(:, 2); 15 | 16 | if verbose 17 | fprintf('\n reading "%s"...', fout); 18 | end 19 | 20 | data = load(fout); 21 | x = data(:, 1); 22 | y = data(:, 2); 23 | z = data(:, 3); 24 | clear data; 25 | 26 | if verbose 27 | fprintf('\n'); 28 | end 29 | 30 | if verbose 31 | fprintf(' working out the grid dimensions...') 32 | end 33 | n = length(x); 34 | if x(2) - x(1) ~= 0 & y(2) - y(1) == 0 35 | xfirst = 1; 36 | xinc = x(2) > x(1); 37 | if xinc 38 | nx = min(find(diff(x) < 0)); 39 | else 40 | nx = min(find(diff(x) > 0)); 41 | end 42 | if mod(n, nx) ~= 0 43 | error(sprintf('\n Error: could not work out the grid size, n = %d, nx = %d, n / nx = %f\n', n, nx, n / nx)); 44 | end 45 | ny = n / nx; 46 | x = x(1 : nx); 47 | y = y(1 : nx : n); 48 | z = reshape(z, nx, ny)'; 49 | elseif x(2) - x(1) == 0 & y(2) - y(1) ~= 0 50 | xfirst = 0; 51 | yinc = y(2) > y(1); 52 | if yinc 53 | ny = min(find(diff(y) < 0)); 54 | else 55 | ny = min(find(diff(y) > 0)); 56 | end 57 | if mod(n, ny) ~= 0 58 | error(sprintf('\n Error: could not work out the grid size, n = %d, ny = %d, n / ny = %.3f\n', n, ny, n / ny)); 59 | end 60 | nx = n / ny; 61 | y = y(1 : ny); 62 | x = x(1 : ny : n); 63 | z = reshape(z, ny, nx); 64 | else 65 | error(' Error: not a rectangular grid'); 66 | end 67 | if verbose 68 | if xfirst 69 | fprintf('%d x %d, stored by rows\n', nx, ny); 70 | else 71 | fprintf('%d x %d, stored by columns\n', nx, ny); 72 | end 73 | end 74 | 75 | if verbose 76 | fprintf(' plotting...'); 77 | end 78 | 79 | h = pcolor_ps(x, y, z); 80 | caxis(range(z)); 81 | set(h, 'LineStyle', 'none'); 82 | axis square; 83 | hold on; 84 | plot(xin, yin, 'w.', 'markersize', 1); 85 | 86 | if verbose 87 | fprintf('\n'); 88 | end 89 | 90 | return 91 | 92 | function [h] = pcolor_ps(x, y, A); 93 | 94 | if nargin == 1 95 | A = x; 96 | [n, m] = size(A); 97 | xx = (0.5 : m + 0.5)'; 98 | yy = (0.5 : n + 0.5)'; 99 | elseif nargin == 3 100 | n = length(y); 101 | m = length(x); 102 | A = reshape(A, n, m); % just in case 103 | xx = getcorners(x); 104 | yy = getcorners(y); 105 | else 106 | error(sprintf('\n Error: pcolor_ps(): nargin = %d (expected 1 or 3)\n', nargin)); 107 | end 108 | 109 | TMP = zeros(n + 1, m + 1); 110 | TMP(1 : n, 1 : m) = A; 111 | 112 | if nargout == 0 113 | pcolor(xx, yy, TMP); 114 | else 115 | h = pcolor(xx, yy, TMP); 116 | end 117 | 118 | return 119 | 120 | function [c] = getcorners(x) 121 | 122 | n = length(x); 123 | c = zeros(n + 1, 1); 124 | c(2 : n) = (x(2 : n) + x(1 : n - 1)) / 2; 125 | c(1) = 2 * x(1) - c(2); 126 | c(n + 1) = 2 * x(n) - c(n); 127 | 128 | return 129 | -------------------------------------------------------------------------------- /natural_neighbour/examples/3/README: -------------------------------------------------------------------------------- 1 | This example tests interpolation of degenerate data, when both input and 2 | output points belong to nodes of 101 x 101 regular grid. As a consequence, 3 | a lot of output points turn out to be exactly on edges of Delaunay 4 | triangulation. In this case, Watson's algorithm used for Sibson interpolation 5 | in this package does not work, and the output vertices have to be perturbed. 6 | (The same applies to Belikov and Semenov's formulas for non-Sibsonian 7 | interpolation.) 8 | 9 | A linear function z = 5x - 3y has been chosen for interpolation because 10 | both Sibsonian and non-Sibsonian interpolation should (in theory) reproduce 11 | it exactly, and for both Sibsonian and non-Sibsonian interpolation sum of 12 | absolute discrepancy is calculated. 13 | 14 | I think that this example is a good test of numerical robustness of the 15 | NN interpolation. 16 | 17 | To conduct the test, run "./test.sh" or "make". 18 | To clean up, run "make clean". 19 | 20 | On pc-linux platform v. 1.55 yields: 21 | sum(|z_sibson_i - z_i|) = 4.91393e-10 22 | sum(|z_nonsibson_i - z_i|) = 3.28898e-14 23 | 24 | For comparison, libnn version 1.52 gives: 25 | sum(|z_sibson_i - z_i|) = 312.976 26 | sum(|z_nonsibson_i - z_i|) = 489.882 27 | 28 | Update 24 November 2006: version 1.69 gives now 29 | sum(|z_sibson_i - z_i|) = 2.07804e-12 30 | sum(|z_nonsibson_i - z_i|) = 3.53031e-14 31 | on my Linux box. 32 | 33 | Good luck! 34 | Pavel Sakov 35 | -------------------------------------------------------------------------------- /natural_neighbour/examples/3/generate-data.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | r = 1; 3 | T = 10000000; 4 | N = 300; 5 | } 6 | 7 | { 8 | for (i = 0; i < N; ++i) { 9 | x = (int(random() * 121.0) - 10.0) / 100.0; 10 | y = (int(random() * 121.0) - 10.0) / 100.0; 11 | z = 5.0 * x - 3.0 * y; 12 | printf("%.10g %.10g %.10g\n", x, y, z); 13 | } 14 | } 15 | 16 | END { 17 | } 18 | 19 | # One could use in-built generator rand(), but its output may depend on the 20 | # awk implementation used... 21 | # 22 | function random() 23 | { 24 | r = (r * 40353607) % T; 25 | return r / T; 26 | } 27 | -------------------------------------------------------------------------------- /natural_neighbour/examples/3/generate-points.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | N = 101; 3 | inc = 1.0 / (N - 1); 4 | } 5 | 6 | { 7 | y = 0.0; 8 | for (j = 0; j < N; ++j) { 9 | x = 0.0; 10 | for (i = 0; i < N; ++i) { 11 | printf("%.2f %.2f %.2f\n", x, y, 5.0 * x - 3.0 * y); 12 | x += inc; 13 | } 14 | y += inc; 15 | } 16 | } 17 | 18 | END { 19 | } 20 | -------------------------------------------------------------------------------- /natural_neighbour/examples/3/makefile: -------------------------------------------------------------------------------- 1 | all: 2 | @./test.sh 3 | clean: 4 | @rm -f *.txt *~ core 5 | -------------------------------------------------------------------------------- /natural_neighbour/examples/3/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ ! -x ../../nnbathy ] 4 | then 5 | echo "error: no executable found" 6 | echo 'Run "./configure" and "make" in the source directory' 7 | exit 1 8 | fi 9 | 10 | echo | awk -f generate-data.awk > data.txt 11 | echo | awk -f generate-points.awk > points.txt 12 | 13 | ../../nnbathy -i data.txt -o points.txt > results.txt 14 | ../../nnbathy -i data.txt -o points.txt -P alg=ns > results-ns.txt 15 | 16 | paste -d" " results.txt results-ns.txt points.txt |\ 17 | cut -f"1 2 3 6 9" -d" " |\ 18 | awk '{a = $3 - $5; s1 += (a > 0) ? a : -a; b = $4 - $5; s2 += (b > 0) ? b : -b;} END {print " sum(|z_sibson_i - z_i|) =", s1; print " sum(|z_nonsibson_i - z_i|) =", s2;}' 19 | -------------------------------------------------------------------------------- /natural_neighbour/examples/4/README: -------------------------------------------------------------------------------- 1 | This real-life example is run on a topographic data from satellite altimeter. 2 | (Thanks to Prof. David A. Paige, Dept. of Earth and Space Sciences, UCLA.) 3 | Similar to example 3, the data points belong to nodes of a rectangular grid, 4 | but also there is a large gap between two main clusters of data. 5 | 6 | To reproduce the example, run "./test.sh" or "make". 7 | To visualize, in Matlab run "viewexample" or 8 | "viewinterp('data.txt', 'nn-0.txt');". 9 | To clean up, run "make clean". 10 | 11 | Good luck! 12 | Pavel Sakov 13 | -------------------------------------------------------------------------------- /natural_neighbour/examples/4/makefile: -------------------------------------------------------------------------------- 1 | all: 2 | ./test.sh 3 | clean: 4 | rm -f lin*.txt nn-*.txt *~ core 5 | -------------------------------------------------------------------------------- /natural_neighbour/examples/4/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | N=201 4 | 5 | if [ ! -x ../../nnbathy ] 6 | then 7 | echo "error: no executable found" 8 | echo 'Run "./configure" and "make" in the source directory' 9 | exit 1 10 | fi 11 | 12 | echo "" 13 | echo -n "Linear interpolation..." 14 | ../../nnbathy -i data.txt -n "$N"x"$N" -P alg=l > lin.txt 15 | echo "done" 16 | echo -n "Natural Neighbours Sibson interpolation..." 17 | ../../nnbathy -i data.txt -n "$N"x"$N" > nn-inf.txt 18 | echo "done" 19 | echo -n "Natural Neighbours Sibson interpolation with wmin = 0..." 20 | ../../nnbathy -i data.txt -n "$N"x"$N" -W 0 > nn-0.txt 21 | echo "done" 22 | echo -n "Natural Neighbours Non-Sibsonian interpolation with wmin = 0..." 23 | ../../nnbathy -i data.txt -n "$N"x"$N" -P alg=ns -W 0 > nn-ns.txt 24 | echo "done" 25 | echo "" 26 | echo 'To visualize, in Matlab run "viewexample"' 27 | -------------------------------------------------------------------------------- /natural_neighbour/examples/4/viewdata.m: -------------------------------------------------------------------------------- 1 | function [] = viewdata(fname, verbose) 2 | 3 | if nargin == 1 4 | verbose = 1; 5 | end 6 | 7 | if verbose 8 | fprintf('plotting data from "%s"\n', fname); 9 | fprintf(' reading %s...', fname); 10 | end 11 | data = load(fname); 12 | x = data(:, 1); 13 | y = data(:, 2); 14 | z = data(:, 3); 15 | clear data; 16 | 17 | if verbose 18 | fprintf('\n'); 19 | end 20 | 21 | if verbose 22 | fprintf(' plotting...'); 23 | end 24 | 25 | xmin = min(x); 26 | xmax = max(x); 27 | ymin = min(y); 28 | ymax = max(y); 29 | zmin = min(z); 30 | zmax = max(z); 31 | n = length(z); 32 | 33 | map = colormap; 34 | axis([xmin xmax ymin ymax]); 35 | axis square; 36 | set(gca, 'box', 'on'); 37 | hold on; 38 | for i = 1 : n 39 | plot(x(i), y(i), 's-', 'color', zcolor(z(i), zmin, zmax, map), 'markersize', 2); 40 | end 41 | 42 | if verbose 43 | fprintf('\n'); 44 | end 45 | 46 | return 47 | 48 | function c = zcolor(z, zmin, zmax, map) 49 | 50 | ind = floor((z - zmin) / (zmax - zmin) * 64 + 1); 51 | ind = min(ind, 64); 52 | c = map(ind, :); 53 | 54 | return 55 | 56 | 57 | -------------------------------------------------------------------------------- /natural_neighbour/examples/4/viewexample.m: -------------------------------------------------------------------------------- 1 | figure 2 | 3 | subplot(3, 2, 1); 4 | fname = 'data.txt'; 5 | fprintf('plotting data points from "%s"\n', fname); 6 | fprintf(' reading %s...', fname); 7 | data = load(fname); 8 | xrange = range(data(:, 1)); 9 | yrange = range(data(:, 2)); 10 | zrange = range(data(:, 3)); 11 | fprintf('\n'); 12 | fprintf(' plotting...'); 13 | axis([xrange yrange]); 14 | axis tight; 15 | axis square; 16 | set(gca, 'box', 'on'); 17 | hold on; 18 | plot(data(:, 1), data(:, 2), 'k.', 'markersize', 1); 19 | fprintf('\n'); 20 | clear data; 21 | title('Data points'); 22 | pause(0.1); 23 | 24 | subplot(3, 2, 2); 25 | viewdata('data.txt'); 26 | title('Data'); 27 | pause(0.1); 28 | 29 | subplot(3, 2, 3); 30 | viewinterp('data.txt', 'lin.txt'); 31 | title('Linear interpolation'); 32 | pause(0.1); 33 | 34 | subplot(3, 2, 4); 35 | viewinterp('data.txt', 'nn-inf.txt'); 36 | caxis(zrange); 37 | title(sprintf('Natural Neighbours interpolation\n(extrapolation allowed)')); 38 | pause(0.1); 39 | 40 | subplot(3, 2, 5); 41 | viewinterp('data.txt', 'nn-0.txt'); 42 | caxis(zrange); 43 | title(sprintf('Natural Neighbours interpolation\n(interpolation only)')); 44 | pause(0.1); 45 | 46 | subplot(3, 2, 6); 47 | viewinterp('data.txt', 'nn-ns.txt'); 48 | caxis(zrange); 49 | title(sprintf('Non-Sibsonian NN interpolation\n(interpolation only)')); 50 | pause(0.1); 51 | 52 | suptitle('Interpolation of topographic data from satellite altimeter using nnbathy'); 53 | -------------------------------------------------------------------------------- /natural_neighbour/examples/4/viewinterp.m: -------------------------------------------------------------------------------- 1 | function [h] = viewinterp(fin, fout, verbose) 2 | 3 | if nargin == 2 4 | verbose = 1; 5 | end 6 | 7 | if verbose 8 | fprintf('plotting data from "%s" and "%s"\n', fin, fout); 9 | fprintf(' reading "%s"...', fin); 10 | end 11 | 12 | data = load(fin); 13 | xin = data(:, 1); 14 | yin = data(:, 2); 15 | 16 | if verbose 17 | fprintf('\n reading "%s"...', fout); 18 | end 19 | 20 | data = load(fout); 21 | x = data(:, 1); 22 | y = data(:, 2); 23 | z = data(:, 3); 24 | clear data; 25 | 26 | if verbose 27 | fprintf('\n'); 28 | end 29 | 30 | if verbose 31 | fprintf(' working out the grid dimensions...') 32 | end 33 | n = length(x); 34 | if x(2) - x(1) ~= 0 & y(2) - y(1) == 0 35 | xfirst = 1; 36 | xinc = x(2) > x(1); 37 | if xinc 38 | nx = min(find(diff(x) < 0)); 39 | else 40 | nx = min(find(diff(x) > 0)); 41 | end 42 | if mod(n, nx) ~= 0 43 | error(sprintf('\n Error: could not work out the grid size, n = %d, nx = %d, n / nx = %f\n', n, nx, n / nx)); 44 | end 45 | ny = n / nx; 46 | x = x(1 : nx); 47 | y = y(1 : nx : n); 48 | z = reshape(z, nx, ny)'; 49 | elseif x(2) - x(1) == 0 & y(2) - y(1) ~= 0 50 | xfirst = 0; 51 | yinc = y(2) > y(1); 52 | if yinc 53 | ny = min(find(diff(y) < 0)); 54 | else 55 | ny = min(find(diff(y) > 0)); 56 | end 57 | if mod(n, ny) ~= 0 58 | error(sprintf('\n Error: could not work out the grid size, n = %d, ny = %d, n / ny = %.3f\n', n, ny, n / ny)); 59 | end 60 | nx = n / ny; 61 | y = y(1 : ny); 62 | x = x(1 : ny : n); 63 | z = reshape(z, ny, nx); 64 | else 65 | error(' Error: not a rectangular grid'); 66 | end 67 | if verbose 68 | if xfirst 69 | fprintf('%d x %d, stored by rows\n', nx, ny); 70 | else 71 | fprintf('%d x %d, stored by columns\n', nx, ny); 72 | end 73 | end 74 | 75 | if verbose 76 | fprintf(' plotting...'); 77 | end 78 | 79 | h = pcolor_ps(x, y, z); 80 | caxis(range(z)); 81 | set(h, 'LineStyle', 'none'); 82 | axis square; 83 | hold on; 84 | plot(xin, yin, 'w.', 'markersize', 1); 85 | 86 | if verbose 87 | fprintf('\n'); 88 | end 89 | 90 | return 91 | 92 | function [h] = pcolor_ps(x, y, A); 93 | 94 | if nargin == 1 95 | A = x; 96 | [n, m] = size(A); 97 | xx = (0.5 : m + 0.5)'; 98 | yy = (0.5 : n + 0.5)'; 99 | elseif nargin == 3 100 | n = length(y); 101 | m = length(x); 102 | A = reshape(A, n, m); % just in case 103 | xx = getcorners(x); 104 | yy = getcorners(y); 105 | else 106 | error(sprintf('\n Error: pcolor_ps(): nargin = %d (expected 1 or 3)\n', nargin)); 107 | end 108 | 109 | TMP = zeros(n + 1, m + 1); 110 | TMP(1 : n, 1 : m) = A; 111 | 112 | if nargout == 0 113 | pcolor(xx, yy, TMP); 114 | else 115 | h = pcolor(xx, yy, TMP); 116 | end 117 | 118 | return 119 | 120 | function [c] = getcorners(x) 121 | 122 | n = length(x); 123 | c = zeros(n + 1, 1); 124 | c(2 : n) = (x(2 : n) + x(1 : n - 1)) / 2; 125 | c(1) = 2 * x(1) - c(2); 126 | c(n + 1) = 2 * x(n) - c(n); 127 | 128 | return 129 | -------------------------------------------------------------------------------- /natural_neighbour/examples/5/README: -------------------------------------------------------------------------------- 1 | This example is run on elevation data obtained from digitised contours. 2 | (Thanks to Maciej Sieczka, Institute of Plant Biology, Wroclaw University.) 3 | It is characterised by strong clustering of data points and big gaps between 4 | clusters. 5 | 6 | To reproduce the example, run "./test.sh" or "make". 7 | To visualize, in Matlab run "viewexample" (which takes time to plot) or 8 | "viewinterp('data.txt', 'nn.txt');". 9 | To clean up, run "make clean". 10 | 11 | Good luck! 12 | Pavel Sakov 13 | -------------------------------------------------------------------------------- /natural_neighbour/examples/5/makefile: -------------------------------------------------------------------------------- 1 | all: 2 | ./test.sh 3 | clean: 4 | rm -f lin*.txt nn.txt *~ core 5 | -------------------------------------------------------------------------------- /natural_neighbour/examples/5/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | N=373 4 | M=455 5 | 6 | if [ ! -x ../../nnbathy ] 7 | then 8 | echo "error: no executable found" 9 | echo 'Run "./configure" and "make" in the source directory' 10 | exit 1 11 | fi 12 | 13 | echo "" 14 | echo -n "Linear interpolation..." 15 | ../../nnbathy -i data.txt -n "$N"x"$M" -P alg=l > lin.txt 16 | echo "done" 17 | echo -n "Natural Neighbours Sibson interpolation..." 18 | ../../nnbathy -i data.txt -n "$N"x"$M" -W 0 > nn.txt 19 | echo "done" 20 | echo "" 21 | echo 'To visualize, in Matlab run "viewexample"' 22 | -------------------------------------------------------------------------------- /natural_neighbour/examples/5/viewexample.m: -------------------------------------------------------------------------------- 1 | figure 2 | 3 | subplot(2, 2, 1); 4 | viewinterp2('data.txt', 'lin.txt'); 5 | title('Linear interpolation (contours)'); 6 | pause(0.1); 7 | 8 | subplot(2, 2, 2); 9 | viewinterp('data.txt', 'lin.txt'); 10 | title('Linear interpolation (colour map)'); 11 | pause(0.1); 12 | 13 | subplot(2, 2, 3); 14 | viewinterp2('data.txt', 'nn.txt'); 15 | title(sprintf('NN interpolation (contours)')); 16 | pause(0.1); 17 | 18 | subplot(2, 2, 4); 19 | viewinterp('data.txt', 'nn.txt'); 20 | title(sprintf('NN interpolation (colour map)')); 21 | pause(0.1); 22 | 23 | suptitle('Interpolation from elevation contours using nnbathy'); 24 | -------------------------------------------------------------------------------- /natural_neighbour/examples/5/viewinterp.m: -------------------------------------------------------------------------------- 1 | function [h] = viewinterp(fin, fout, verbose) 2 | 3 | if nargin == 2 4 | verbose = 1; 5 | end 6 | 7 | if verbose 8 | fprintf('plotting data from "%s" and "%s"\n', fin, fout); 9 | fprintf(' reading "%s"...', fin); 10 | end 11 | 12 | data = load(fin); 13 | xin = data(:, 1); 14 | yin = data(:, 2); 15 | 16 | if verbose 17 | fprintf('\n reading "%s"...', fout); 18 | end 19 | 20 | data = load(fout); 21 | x = data(:, 1); 22 | y = data(:, 2); 23 | z = data(:, 3); 24 | clear data; 25 | 26 | if verbose 27 | fprintf('\n'); 28 | end 29 | 30 | if verbose 31 | fprintf(' working out the grid dimensions...') 32 | end 33 | n = length(x); 34 | if x(2) - x(1) ~= 0 & y(2) - y(1) == 0 35 | xfirst = 1; 36 | xinc = x(2) > x(1); 37 | if xinc 38 | nx = min(find(diff(x) < 0)); 39 | else 40 | nx = min(find(diff(x) > 0)); 41 | end 42 | if mod(n, nx) ~= 0 43 | error(sprintf('\n Error: could not work out the grid size, n = %d, nx = %d, n / nx = %f\n', n, nx, n / nx)); 44 | end 45 | ny = n / nx; 46 | x = x(1 : nx); 47 | y = y(1 : nx : n); 48 | z = reshape(z, nx, ny)'; 49 | elseif x(2) - x(1) == 0 & y(2) - y(1) ~= 0 50 | xfirst = 0; 51 | yinc = y(2) > y(1); 52 | if yinc 53 | ny = min(find(diff(y) < 0)); 54 | else 55 | ny = min(find(diff(y) > 0)); 56 | end 57 | if mod(n, ny) ~= 0 58 | error(sprintf('\n Error: could not work out the grid size, n = %d, ny = %d, n / ny = %.3f\n', n, ny, n / ny)); 59 | end 60 | nx = n / ny; 61 | y = y(1 : ny); 62 | x = x(1 : ny : n); 63 | z = reshape(z, ny, nx); 64 | else 65 | error(' Error: not a rectangular grid'); 66 | end 67 | if verbose 68 | if xfirst 69 | fprintf('%d x %d, stored by rows\n', nx, ny); 70 | else 71 | fprintf('%d x %d, stored by columns\n', nx, ny); 72 | end 73 | end 74 | 75 | if verbose 76 | fprintf(' plotting...'); 77 | end 78 | 79 | h = pcolor_ps(x, y, z); 80 | caxis(range(z)); 81 | set(h, 'LineStyle', 'none'); 82 | axis equal; 83 | axis tight; 84 | hold on; 85 | plot(xin, yin, 'w.', 'markersize', 1); 86 | 87 | if verbose 88 | fprintf('\n'); 89 | end 90 | 91 | return 92 | 93 | function [h] = pcolor_ps(x, y, A); 94 | 95 | if nargin == 1 96 | A = x; 97 | [n, m] = size(A); 98 | xx = (0.5 : m + 0.5)'; 99 | yy = (0.5 : n + 0.5)'; 100 | elseif nargin == 3 101 | n = length(y); 102 | m = length(x); 103 | A = reshape(A, n, m); % just in case 104 | xx = getcorners(x); 105 | yy = getcorners(y); 106 | else 107 | error(sprintf('\n Error: pcolor_ps(): nargin = %d (expected 1 or 3)\n', nargin)); 108 | end 109 | 110 | TMP = zeros(n + 1, m + 1); 111 | TMP(1 : n, 1 : m) = A; 112 | 113 | if nargout == 0 114 | pcolor(xx, yy, TMP); 115 | else 116 | h = pcolor(xx, yy, TMP); 117 | end 118 | 119 | return 120 | 121 | function [c] = getcorners(x) 122 | 123 | n = length(x); 124 | c = zeros(n + 1, 1); 125 | c(2 : n) = (x(2 : n) + x(1 : n - 1)) / 2; 126 | c(1) = 2 * x(1) - c(2); 127 | c(n + 1) = 2 * x(n) - c(n); 128 | 129 | return 130 | -------------------------------------------------------------------------------- /natural_neighbour/examples/5/viewinterp2.m: -------------------------------------------------------------------------------- 1 | function [h] = viewinterp2(fin, fout, verbose) 2 | 3 | if nargin == 2 4 | verbose = 1; 5 | end 6 | 7 | if verbose 8 | fprintf('plotting data from "%s" and "%s"\n', fin, fout); 9 | fprintf(' reading "%s"...', fin); 10 | end 11 | 12 | data = load(fin); 13 | xin = data(:, 1); 14 | yin = data(:, 2); 15 | zin = data(:, 3); 16 | zmin = min(zin); 17 | zmax = max(zin); 18 | nz = length(zin); 19 | 20 | if verbose 21 | fprintf('\n reading "%s"...', fout); 22 | end 23 | 24 | data = load(fout); 25 | x = data(:, 1); 26 | y = data(:, 2); 27 | z = data(:, 3); 28 | clear data; 29 | 30 | if verbose 31 | fprintf('\n'); 32 | end 33 | 34 | if verbose 35 | fprintf(' working out the grid dimensions...') 36 | end 37 | n = length(x); 38 | if x(2) - x(1) ~= 0 & y(2) - y(1) == 0 39 | xfirst = 1; 40 | xinc = x(2) > x(1); 41 | if xinc 42 | nx = min(find(diff(x) < 0)); 43 | else 44 | nx = min(find(diff(x) > 0)); 45 | end 46 | if mod(n, nx) ~= 0 47 | error(sprintf('\n Error: could not work out the grid size, n = %d, nx = %d, n / nx = %f\n', n, nx, n / nx)); 48 | end 49 | ny = n / nx; 50 | x = x(1 : nx); 51 | y = y(1 : nx : n); 52 | z = reshape(z, nx, ny)'; 53 | elseif x(2) - x(1) == 0 & y(2) - y(1) ~= 0 54 | xfirst = 0; 55 | yinc = y(2) > y(1); 56 | if yinc 57 | ny = min(find(diff(y) < 0)); 58 | else 59 | ny = min(find(diff(y) > 0)); 60 | end 61 | if mod(n, ny) ~= 0 62 | error(sprintf('\n Error: could not work out the grid size, n = %d, ny = %d, n / ny = %.3f\n', n, ny, n / ny)); 63 | end 64 | nx = n / ny; 65 | y = y(1 : ny); 66 | x = x(1 : ny : n); 67 | z = reshape(z, ny, nx); 68 | else 69 | error(' Error: not a rectangular grid'); 70 | end 71 | if verbose 72 | if xfirst 73 | fprintf('%d x %d, stored by rows\n', nx, ny); 74 | else 75 | fprintf('%d x %d, stored by columns\n', nx, ny); 76 | end 77 | end 78 | 79 | if verbose 80 | fprintf(sprintf(' plotting "%s"...', fin)); 81 | end 82 | 83 | map = colormap; 84 | hold on; 85 | for i = 1 : nz 86 | plot(xin(i), yin(i), '.', 'color', zcolor(zin(i), zmin, zmax, map), 'markersize', 2); 87 | end 88 | axis equal; 89 | axis tight; 90 | set(gca, 'box', 'on'); 91 | 92 | if verbose 93 | fprintf(sprintf('\n plotting "%s"...', fout)); 94 | end 95 | 96 | dv = floor((zmax - zmin) / 15); 97 | V = [floor(zmin) : dv : ceil(zmax)]; 98 | contour(x, y, z, V, 'k'); 99 | 100 | if verbose 101 | fprintf('\n'); 102 | end 103 | 104 | return 105 | 106 | function c = zcolor(z, zmin, zmax, map) 107 | 108 | ind = floor((z - zmin) / (zmax - zmin) * 64 + 1); 109 | ind = min(ind, 64); 110 | c = map(ind, :); 111 | 112 | return 113 | -------------------------------------------------------------------------------- /natural_neighbour/examples/6/README: -------------------------------------------------------------------------------- 1 | This is yet another example based on digitised elevation contours. It provides 2 | a particularly good torture test for the numerics involved in Sibson 3 | interpolation using Warson's method. In fact, it forced me to make changes to 4 | the code that handles degenerate cases during Sibson interpolation in v. 1.69. 5 | Many thanks to Nick Cahill for identifying the problem and providing the data 6 | and to Maciej Sieczka for putting up the bug report. 7 | 8 | To reproduce the example, run "./test.sh" or "make". 9 | To visualize, in Matlab run "viewinterp('data.txt', 'nn.txt');". 10 | To clean up, run "make clean". 11 | 12 | Good luck! 13 | Pavel Sakov 14 | -------------------------------------------------------------------------------- /natural_neighbour/examples/6/makefile: -------------------------------------------------------------------------------- 1 | all: 2 | ./test.sh 3 | clean: 4 | rm -f nn.txt *~ core 5 | -------------------------------------------------------------------------------- /natural_neighbour/examples/6/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ ! -x ../../nnbathy ] 4 | then 5 | echo "error: no executable found" 6 | echo 'Run "./configure" and "make" in the source directory' 7 | exit 1 8 | fi 9 | 10 | echo "" 11 | echo -n "Natural Neighbours Sibson interpolation..." 12 | ../../nnbathy -W 0 -n 152x114 -x 591020.57127923 591321.93673645 -y 4260093.61151167 4259867.85217794 -i data.txt > nn.txt 13 | echo "done" 14 | echo "" 15 | echo 'To visualize, in Matlab run "viewinterp('"'data.txt'"", 'nn.txt');"'"' 16 | -------------------------------------------------------------------------------- /natural_neighbour/examples/6/viewinterp.m: -------------------------------------------------------------------------------- 1 | function [h] = viewinterp(fin, fout, verbose) 2 | 3 | if nargin == 2 4 | verbose = 1; 5 | end 6 | 7 | if verbose 8 | fprintf('plotting data from "%s" and "%s"\n', fin, fout); 9 | fprintf(' reading "%s"...', fin); 10 | end 11 | 12 | data = load(fin); 13 | xin = data(:, 1); 14 | yin = data(:, 2); 15 | 16 | if verbose 17 | fprintf('\n reading "%s"...', fout); 18 | end 19 | 20 | data = load(fout); 21 | x = data(:, 1); 22 | y = data(:, 2); 23 | z = data(:, 3); 24 | clear data; 25 | 26 | if verbose 27 | fprintf('\n'); 28 | end 29 | 30 | if verbose 31 | fprintf(' working out the grid dimensions...') 32 | end 33 | n = length(x); 34 | if x(2) - x(1) ~= 0 & y(2) - y(1) == 0 35 | xfirst = 1; 36 | xinc = x(2) > x(1); 37 | if xinc 38 | nx = min(find(diff(x) < 0)); 39 | else 40 | nx = min(find(diff(x) > 0)); 41 | end 42 | if mod(n, nx) ~= 0 43 | error(sprintf('\n Error: could not work out the grid size, n = %d, nx = %d, n / nx = %f\n', n, nx, n / nx)); 44 | end 45 | ny = n / nx; 46 | x = x(1 : nx); 47 | y = y(1 : nx : n); 48 | z = reshape(z, nx, ny)'; 49 | elseif x(2) - x(1) == 0 & y(2) - y(1) ~= 0 50 | xfirst = 0; 51 | yinc = y(2) > y(1); 52 | if yinc 53 | ny = min(find(diff(y) < 0)); 54 | else 55 | ny = min(find(diff(y) > 0)); 56 | end 57 | if mod(n, ny) ~= 0 58 | error(sprintf('\n Error: could not work out the grid size, n = %d, ny = %d, n / ny = %.3f\n', n, ny, n / ny)); 59 | end 60 | nx = n / ny; 61 | y = y(1 : ny); 62 | x = x(1 : ny : n); 63 | z = reshape(z, ny, nx); 64 | else 65 | error(' Error: not a rectangular grid'); 66 | end 67 | if verbose 68 | if xfirst 69 | fprintf('%d x %d, stored by rows\n', nx, ny); 70 | else 71 | fprintf('%d x %d, stored by columns\n', nx, ny); 72 | end 73 | end 74 | 75 | if verbose 76 | fprintf(' plotting...'); 77 | end 78 | 79 | h = pcolor_ps(x, y, z); 80 | caxis(range(z)); 81 | set(h, 'LineStyle', 'none'); 82 | axis equal; 83 | axis tight; 84 | hold on; 85 | plot(xin, yin, 'k.', 'markersize', 1); 86 | 87 | if verbose 88 | fprintf('\n'); 89 | end 90 | 91 | return 92 | 93 | function [h] = pcolor_ps(x, y, A); 94 | 95 | if nargin == 1 96 | A = x; 97 | [n, m] = size(A); 98 | xx = (0.5 : m + 0.5)'; 99 | yy = (0.5 : n + 0.5)'; 100 | elseif nargin == 3 101 | n = length(y); 102 | m = length(x); 103 | A = reshape(A, n, m); % just in case 104 | xx = getcorners(x); 105 | yy = getcorners(y); 106 | else 107 | error(sprintf('\n Error: pcolor_ps(): nargin = %d (expected 1 or 3)\n', nargin)); 108 | end 109 | 110 | TMP = zeros(n + 1, m + 1); 111 | TMP(1 : n, 1 : m) = A; 112 | 113 | if nargout == 0 114 | pcolor(xx, yy, TMP); 115 | else 116 | h = pcolor(xx, yy, TMP); 117 | end 118 | 119 | return 120 | 121 | function [c] = getcorners(x) 122 | 123 | n = length(x); 124 | c = zeros(n + 1, 1); 125 | c(2 : n) = (x(2 : n) + x(1 : n - 1)) / 2; 126 | c(1) = 2 * x(1) - c(2); 127 | c(n + 1) = 2 * x(n) - c(n); 128 | 129 | return 130 | -------------------------------------------------------------------------------- /natural_neighbour/examples/README: -------------------------------------------------------------------------------- 1 | Example 1: uniformly distributed random measurements of Franke test fuction. 2 | 3 | Example 2: topographic data from sonar. 4 | 5 | Example 3: both interpolation points and data points lie in nodes of a 6 | rectangular grid. 7 | 8 | Example 4: topographic data from satellite altimeter. 9 | 10 | Example 5: topographic data obtained from digitised contours. 11 | -------------------------------------------------------------------------------- /natural_neighbour/hash.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: hash.h 4 | * 5 | * Purpose: Hash table header 6 | * 7 | * Author: Jerry Coffin 8 | * 9 | * Description: Public domain code by Jerry Coffin, with improvements by 10 | * HenkJan Wolthuis. 11 | * Date last modified: 05-Jul-1997 12 | * 13 | * Revisions: 18-09-2002 -- modified by Pavel Sakov 14 | * 15 | *****************************************************************************/ 16 | 17 | #ifndef _HASH_H 18 | #define _HASH_H 19 | 20 | struct hashtable; 21 | typedef struct hashtable hashtable; 22 | 23 | /** Copies a key. The key must be able to be deallocated by free(). 24 | */ 25 | typedef void* (*ht_keycp) (void*); 26 | 27 | /** Returns 1 if two keys are equal, 0 otherwise. 28 | */ 29 | typedef int (*ht_keyeq) (void*, void*); 30 | 31 | /** Converts key to an unsigned integer (not necessarily unique). 32 | */ 33 | typedef unsigned int (*ht_key2hash) (void*); 34 | 35 | /** Creates a hash table of specified size. 36 | * 37 | * @param size Size of hash table for output points 38 | * @param cp Key copy function 39 | * @param eq Key equality check function 40 | * @param hash Hash value calculation function 41 | */ 42 | hashtable* ht_create(int size, ht_keycp cp, ht_keyeq eq, ht_key2hash hash); 43 | 44 | /** Create a hash table of specified size and key type. 45 | */ 46 | hashtable* ht_create_d1(int size); /* double[1] */ 47 | hashtable* ht_create_d2(int size); /* double[2] */ 48 | hashtable* ht_create_str(int size); /* char* */ 49 | hashtable* ht_create_i1(int size); /* int[1] */ 50 | hashtable* ht_create_i2(int size); /* int[2] */ 51 | 52 | /** Destroys a hash table. 53 | * (Take care of deallocating data by ht_process() prior to destroying the 54 | * table if necessary.) 55 | * 56 | * @param table Hash table to be destroyed 57 | */ 58 | void ht_destroy(hashtable* table); 59 | 60 | /** Inserts a new entry into the hash table. 61 | * 62 | * @param table The hash table 63 | * @param key Ponter to entry's key 64 | * @param data Pointer to associated data 65 | * @return Pointer to the old data associated with the key, NULL if the key 66 | * wasn't in the table previously 67 | */ 68 | void* ht_insert(hashtable* table, void* key, void* data); 69 | 70 | /** Returns a pointer to the data associated with a key. If the key has 71 | * not been inserted in the table, returns NULL. 72 | * 73 | * @param table The hash table 74 | * @param key The key 75 | * @return The associated data or NULL 76 | */ 77 | void* ht_find(hashtable* table, void* key); 78 | 79 | /** Deletes an entry from the table. Returns a pointer to the data that 80 | * was associated with the key so that the calling code can dispose it 81 | * properly. 82 | * 83 | * @param table The hash table 84 | * @param key The key 85 | * @return The associated data or NULL 86 | */ 87 | void* ht_delete(hashtable* table, void* key); 88 | 89 | /** For each entry, calls a specified function with corresponding data as a 90 | * parameter. 91 | * 92 | * @param table The hash table 93 | * @param func The action function 94 | */ 95 | void ht_process(hashtable* table, void (*func) (void*)); 96 | 97 | /** Get the number of committed entries. 98 | * 99 | * @param table The hash table 100 | * @return The number of committed entries 101 | */ 102 | int ht_getnentries(hashtable* table); 103 | 104 | /** Get the size of the table. 105 | * 106 | * @param table The hash table 107 | * @return The size of the table 108 | */ 109 | int ht_getsize(hashtable* table); 110 | 111 | /** Get the number of table elements filled. 112 | * 113 | * @param table The hash table 114 | * @return The number of table elements filled 115 | */ 116 | int ht_getnfilled(hashtable* table); 117 | 118 | #endif /* _HASH_H */ 119 | -------------------------------------------------------------------------------- /natural_neighbour/install-sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # 3 | # install - install a program, script, or datafile 4 | # This comes from X11R5. 5 | # 6 | # Calling this script install-sh is preferred over install.sh, to prevent 7 | # `make' implicit rules from creating a file called install from it 8 | # when there is no Makefile. 9 | # 10 | # This script is compatible with the BSD install script, but was written 11 | # from scratch. 12 | # 13 | 14 | 15 | # set DOITPROG to echo to test this script 16 | 17 | # Don't use :- since 4.3BSD and earlier shells don't like it. 18 | doit="${DOITPROG-}" 19 | 20 | 21 | # put in absolute paths if you don't have them in your path; or use env. vars. 22 | 23 | mvprog="${MVPROG-mv}" 24 | cpprog="${CPPROG-cp}" 25 | chmodprog="${CHMODPROG-chmod}" 26 | chownprog="${CHOWNPROG-chown}" 27 | chgrpprog="${CHGRPPROG-chgrp}" 28 | stripprog="${STRIPPROG-strip}" 29 | rmprog="${RMPROG-rm}" 30 | mkdirprog="${MKDIRPROG-mkdir}" 31 | 32 | tranformbasename="" 33 | transform_arg="" 34 | instcmd="$mvprog" 35 | chmodcmd="$chmodprog 0755" 36 | chowncmd="" 37 | chgrpcmd="" 38 | stripcmd="" 39 | rmcmd="$rmprog -f" 40 | mvcmd="$mvprog" 41 | src="" 42 | dst="" 43 | dir_arg="" 44 | 45 | while [ x"$1" != x ]; do 46 | case $1 in 47 | -c) instcmd="$cpprog" 48 | shift 49 | continue;; 50 | 51 | -d) dir_arg=true 52 | shift 53 | continue;; 54 | 55 | -m) chmodcmd="$chmodprog $2" 56 | shift 57 | shift 58 | continue;; 59 | 60 | -o) chowncmd="$chownprog $2" 61 | shift 62 | shift 63 | continue;; 64 | 65 | -g) chgrpcmd="$chgrpprog $2" 66 | shift 67 | shift 68 | continue;; 69 | 70 | -s) stripcmd="$stripprog" 71 | shift 72 | continue;; 73 | 74 | -t=*) transformarg=`echo $1 | sed 's/-t=//'` 75 | shift 76 | continue;; 77 | 78 | -b=*) transformbasename=`echo $1 | sed 's/-b=//'` 79 | shift 80 | continue;; 81 | 82 | *) if [ x"$src" = x ] 83 | then 84 | src=$1 85 | else 86 | # this colon is to work around a 386BSD /bin/sh bug 87 | : 88 | dst=$1 89 | fi 90 | shift 91 | continue;; 92 | esac 93 | done 94 | 95 | if [ x"$src" = x ] 96 | then 97 | echo "install: no input file specified" 98 | exit 1 99 | else 100 | true 101 | fi 102 | 103 | if [ x"$dir_arg" != x ]; then 104 | dst=$src 105 | src="" 106 | 107 | if [ -d $dst ]; then 108 | instcmd=: 109 | else 110 | instcmd=mkdir 111 | fi 112 | else 113 | 114 | # Waiting for this to be detected by the "$instcmd $src $dsttmp" command 115 | # might cause directories to be created, which would be especially bad 116 | # if $src (and thus $dsttmp) contains '*'. 117 | 118 | if [ -f $src -o -d $src ] 119 | then 120 | true 121 | else 122 | echo "install: $src does not exist" 123 | exit 1 124 | fi 125 | 126 | if [ x"$dst" = x ] 127 | then 128 | echo "install: no destination specified" 129 | exit 1 130 | else 131 | true 132 | fi 133 | 134 | # If destination is a directory, append the input filename; if your system 135 | # does not like double slashes in filenames, you may need to add some logic 136 | 137 | if [ -d $dst ] 138 | then 139 | dst="$dst"/`basename $src` 140 | else 141 | true 142 | fi 143 | fi 144 | 145 | ## this sed command emulates the dirname command 146 | dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` 147 | 148 | # Make sure that the destination directory exists. 149 | # this part is taken from Noah Friedman's mkinstalldirs script 150 | 151 | # Skip lots of stat calls in the usual case. 152 | if [ ! -d "$dstdir" ]; then 153 | defaultIFS=' 154 | ' 155 | IFS="${IFS-${defaultIFS}}" 156 | 157 | oIFS="${IFS}" 158 | # Some sh's can't handle IFS=/ for some reason. 159 | IFS='%' 160 | set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'` 161 | IFS="${oIFS}" 162 | 163 | pathcomp='' 164 | 165 | while [ $# -ne 0 ] ; do 166 | pathcomp="${pathcomp}${1}" 167 | shift 168 | 169 | if [ ! -d "${pathcomp}" ] ; 170 | then 171 | $mkdirprog "${pathcomp}" 172 | else 173 | true 174 | fi 175 | 176 | pathcomp="${pathcomp}/" 177 | done 178 | fi 179 | 180 | if [ x"$dir_arg" != x ] 181 | then 182 | $doit $instcmd $dst && 183 | 184 | if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi && 185 | if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi && 186 | if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi && 187 | if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi 188 | else 189 | 190 | # If we're going to rename the final executable, determine the name now. 191 | 192 | if [ x"$transformarg" = x ] 193 | then 194 | dstfile=`basename $dst` 195 | else 196 | dstfile=`basename $dst $transformbasename | 197 | sed $transformarg`$transformbasename 198 | fi 199 | 200 | # don't allow the sed command to completely eliminate the filename 201 | 202 | if [ x"$dstfile" = x ] 203 | then 204 | dstfile=`basename $dst` 205 | else 206 | true 207 | fi 208 | 209 | # Make a temp file name in the proper directory. 210 | 211 | dsttmp=$dstdir/#inst.$$# 212 | 213 | # Move or copy the file name to the temp name 214 | 215 | $doit $instcmd $src $dsttmp && 216 | 217 | trap "rm -f ${dsttmp}" 0 && 218 | 219 | # and set any options; do chmod last to preserve setuid bits 220 | 221 | # If any of these fail, we abort the whole thing. If we want to 222 | # ignore errors from any of these, just make sure not to ignore 223 | # errors from the above "$doit $instcmd $src $dsttmp" command. 224 | 225 | if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi && 226 | if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi && 227 | if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi && 228 | if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi && 229 | 230 | # Now rename the file to the real destination. 231 | 232 | $doit $rmcmd -f $dstdir/$dstfile && 233 | $doit $mvcmd $dsttmp $dstdir/$dstfile 234 | 235 | fi && 236 | 237 | 238 | exit 0 239 | -------------------------------------------------------------------------------- /natural_neighbour/istack.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: istack.c 4 | * 5 | * Created: 06/06/2001 6 | * 7 | * Author: Pavel Sakov 8 | * CSIRO Marine Research 9 | * 10 | * Purpose: Handling stack of integers 11 | * 12 | * Description: None 13 | * 14 | * Revisions: None 15 | * 16 | *****************************************************************************/ 17 | 18 | #define STACK_NSTART 50 19 | #define STACK_NINC 50 20 | 21 | #include 22 | #include 23 | #include "istack.h" 24 | 25 | istack* istack_create(void) 26 | { 27 | istack* s = malloc(sizeof(istack)); 28 | 29 | s->n = 0; 30 | s->nallocated = STACK_NSTART; 31 | s->v = malloc(STACK_NSTART * sizeof(int)); 32 | return s; 33 | } 34 | 35 | void istack_destroy(istack* s) 36 | { 37 | if (s != NULL) { 38 | free(s->v); 39 | free(s); 40 | } 41 | } 42 | 43 | void istack_reset(istack* s) 44 | { 45 | s->n = 0; 46 | } 47 | 48 | int istack_contains(istack* s, int v) 49 | { 50 | int i; 51 | 52 | for (i = 0; i < s->n; ++i) 53 | if (s->v[i] == v) 54 | return 1; 55 | return 0; 56 | } 57 | 58 | void istack_push(istack* s, int v) 59 | { 60 | if (s->n == s->nallocated) { 61 | s->nallocated *= 2; 62 | s->v = realloc(s->v, s->nallocated * sizeof(int)); 63 | } 64 | 65 | s->v[s->n] = v; 66 | s->n++; 67 | } 68 | 69 | int istack_pop(istack* s) 70 | { 71 | s->n--; 72 | return s->v[s->n]; 73 | } 74 | 75 | int istack_getnentries(istack* s) 76 | { 77 | return s->n; 78 | } 79 | 80 | int* istack_getentries(istack* s) 81 | { 82 | return s->v; 83 | } 84 | -------------------------------------------------------------------------------- /natural_neighbour/istack.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: istack.h 4 | * 5 | * Created: 06/06/2001 6 | * 7 | * Author: Pavel Sakov 8 | * CSIRO Marine Research 9 | * 10 | * Purpose: Header for handling stack of integers. 11 | * 12 | * Description: None 13 | * 14 | * Revisions: None 15 | * 16 | *****************************************************************************/ 17 | 18 | #if !defined(_ISTACK_H) 19 | #define _ISTACK_H 20 | 21 | #if !defined(_ISTACK_STRUCT) 22 | #define _ISTACK_STRUCT 23 | struct istack; 24 | typedef struct istack istack; 25 | #endif 26 | 27 | struct istack { 28 | int n; 29 | int nallocated; 30 | int* v; 31 | }; 32 | 33 | istack* istack_create(void); 34 | void istack_destroy(istack* s); 35 | void istack_push(istack* s, int v); 36 | int istack_pop(istack* s); 37 | int istack_contains(istack* s, int v); 38 | void istack_reset(istack* s); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /natural_neighbour/libnn.dvi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattfoster/matlab-interpolation-toolkit/0d4d7af6aa1bddb6cda0ec18049c6141ea55df6b/natural_neighbour/libnn.dvi -------------------------------------------------------------------------------- /natural_neighbour/libnn.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattfoster/matlab-interpolation-toolkit/0d4d7af6aa1bddb6cda0ec18049c6141ea55df6b/natural_neighbour/libnn.log -------------------------------------------------------------------------------- /natural_neighbour/lpi.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: linear.c 4 | * 5 | * Created: 04/08/2000 6 | * 7 | * Author: Pavel Sakov 8 | * CSIRO Marine Research 9 | * 10 | * Purpose: 2D linear interpolation 11 | * 12 | * Description: `lpi' -- "Linear Point Interpolator" -- is 13 | * a structure for conducting linear interpolation on a given 14 | * data on a "point-to-point" basis. It interpolates linearly 15 | * within each triangle resulted from the Delaunay 16 | * triangluation of input data. `lpi' is much 17 | * faster than all Natural Neighbours interpolators in `nn' 18 | * library. 19 | * 20 | * Revisions: None 21 | * 22 | *****************************************************************************/ 23 | 24 | #include 25 | #include 26 | #include "nan.h" 27 | #include "delaunay.h" 28 | #include "nn.h" 29 | #include "nn_internal.h" 30 | 31 | typedef struct { 32 | double w[3]; 33 | } lweights; 34 | 35 | struct lpi { 36 | delaunay* d; 37 | lweights* weights; 38 | }; 39 | 40 | int delaunay_xytoi(delaunay* d, point* p, int seed); 41 | 42 | /* Builds linear interpolator. 43 | * 44 | * @param d Delaunay triangulation 45 | * @return Linear interpolator 46 | */ 47 | lpi* lpi_build(delaunay* d) 48 | { 49 | int i; 50 | lpi* l = malloc(sizeof(lpi)); 51 | 52 | l->d = d; 53 | l->weights = malloc(d->ntriangles * sizeof(lweights)); 54 | 55 | for (i = 0; i < d->ntriangles; ++i) { 56 | triangle* t = &d->triangles[i]; 57 | lweights* lw = &l->weights[i]; 58 | double x0 = d->points[t->vids[0]].x; 59 | double y0 = d->points[t->vids[0]].y; 60 | double z0 = d->points[t->vids[0]].z; 61 | double x1 = d->points[t->vids[1]].x; 62 | double y1 = d->points[t->vids[1]].y; 63 | double z1 = d->points[t->vids[1]].z; 64 | double x2 = d->points[t->vids[2]].x; 65 | double y2 = d->points[t->vids[2]].y; 66 | double z2 = d->points[t->vids[2]].z; 67 | double x02 = x0 - x2; 68 | double y02 = y0 - y2; 69 | double z02 = z0 - z2; 70 | double x12 = x1 - x2; 71 | double y12 = y1 - y2; 72 | double z12 = z1 - z2; 73 | 74 | if (y12 != 0.0) { 75 | double y0212 = y02 / y12; 76 | 77 | lw->w[0] = (z02 - z12 * y0212) / (x02 - x12 * y0212); 78 | lw->w[1] = (z12 - lw->w[0] * x12) / y12; 79 | lw->w[2] = (z2 - lw->w[0] * x2 - lw->w[1] * y2); 80 | } else { 81 | double x0212 = x02 / x12; 82 | 83 | lw->w[1] = (z02 - z12 * x0212) / (y02 - y12 * x0212); 84 | lw->w[0] = (z12 - lw->w[1] * y12) / x12; 85 | lw->w[2] = (z2 - lw->w[0] * x2 - lw->w[1] * y2); 86 | } 87 | } 88 | 89 | return l; 90 | } 91 | 92 | /* Destroys linear interpolator. 93 | * 94 | * @param l Structure to be destroyed 95 | */ 96 | void lpi_destroy(lpi* l) 97 | { 98 | free(l->weights); 99 | free(l); 100 | } 101 | 102 | /* Finds linearly interpolated value in a point. 103 | * 104 | * @param l Linear interpolation 105 | * @param p Point to be interpolated (p->x, p->y -- input; p->z -- output) 106 | */ 107 | void lpi_interpolate_point(lpi* l, point* p) 108 | { 109 | delaunay* d = l->d; 110 | int tid = delaunay_xytoi(d, p, d->first_id); 111 | 112 | if (tid >= 0) { 113 | lweights* lw = &l->weights[tid]; 114 | 115 | d->first_id = tid; 116 | p->z = p->x * lw->w[0] + p->y * lw->w[1] + lw->w[2]; 117 | } else 118 | p->z = NaN; 119 | } 120 | 121 | /* Linearly interpolates data in an array of points. 122 | * 123 | * @param nin Number of input points 124 | * @param pin Array of input points [pin] 125 | * @param nout Number of ouput points 126 | * @param pout Array of output points [nout] 127 | */ 128 | void lpi_interpolate_points(int nin, point pin[], int nout, point pout[]) 129 | { 130 | delaunay* d = delaunay_build(nin, pin, 0, NULL, 0, NULL); 131 | lpi* l = lpi_build(d); 132 | int seed = 0; 133 | int i; 134 | 135 | if (nn_verbose) { 136 | fprintf(stderr, "xytoi:\n"); 137 | for (i = 0; i < nout; ++i) { 138 | point* p = &pout[i]; 139 | 140 | fprintf(stderr, "(%.7g,%.7g) -> %d\n", p->x, p->y, delaunay_xytoi(d, p, seed)); 141 | } 142 | } 143 | 144 | for (i = 0; i < nout; ++i) 145 | lpi_interpolate_point(l, &pout[i]); 146 | 147 | if (nn_verbose) { 148 | fprintf(stderr, "output:\n"); 149 | for (i = 0; i < nout; ++i) { 150 | point* p = &pout[i];; 151 | fprintf(stderr, " %d:%15.7g %15.7g %15.7g\n", i, p->x, p->y, p->z); 152 | } 153 | } 154 | 155 | lpi_destroy(l); 156 | delaunay_destroy(d); 157 | } 158 | -------------------------------------------------------------------------------- /natural_neighbour/makefile: -------------------------------------------------------------------------------- 1 | SHELL = /bin/sh 2 | 3 | prefix = /usr/local 4 | exec_prefix = ${prefix} 5 | bindir = ${exec_prefix}/bin 6 | libdir = /usr/local/lib 7 | includedir = ${prefix}/include 8 | 9 | INSTALLDIRS =\ 10 | $(bindir)\ 11 | $(libdir)\ 12 | $(includedir) 13 | 14 | DISTDIR = ~/pub_web 15 | 16 | INSTALL = /usr/bin/install -c 17 | INSTALL_PROGRAM = ${INSTALL} 18 | INSTALL_DATA = ${INSTALL} -m 644 19 | 20 | CC = gcc 21 | CFLAGS = -g -O2 -Wall -pedantic 22 | CFLAGS_TRIANGLE = -g -O2 -Wall -pedantic -w -ffloat-store 23 | CFLAGS_VULNERABLE = -fno-force-mem -ffloat-store 24 | LDFLAGS = 25 | 26 | AR = ar 27 | ARFLAGS = cru 28 | 29 | MLIB = -lm 30 | 31 | LIBSRC =\ 32 | delaunay.c\ 33 | hash.c\ 34 | istack.c\ 35 | lpi.c\ 36 | minell.c\ 37 | nnai.c\ 38 | nnpi.c\ 39 | nncommon.c\ 40 | nncommon-vulnerable.c\ 41 | preader.c 42 | 43 | UTILSRC=\ 44 | nnbathy.c 45 | 46 | HEADERS=\ 47 | delaunay.h\ 48 | hash.h\ 49 | istack.h\ 50 | minell.h\ 51 | nan.h\ 52 | nn.h\ 53 | nn_internal.h\ 54 | preader.h\ 55 | version.h 56 | 57 | PROGRAMS =\ 58 | minell\ 59 | nnbathy\ 60 | 61 | TESTS=\ 62 | nnai_test\ 63 | nnphi_test\ 64 | ht_test 65 | 66 | LIBOBJS = $(LIBSRC:.c=.o) 67 | 68 | MEX = /Applications/MATLAB_R2008b.app/bin/mex 69 | 70 | all: libnn.a $(PROGRAMS) mex 71 | 72 | minell: minell.c 73 | $(CC) -o minell minell.c -DME_STANDALONE $(CFLAGS) -I. $(LDFLAGS) $(MLIB) 74 | 75 | nnbathy: libnn.a nnbathy.c 76 | $(CC) -o nnbathy nnbathy.c $(CFLAGS) -I. $(LDFLAGS) libnn.a $(MLIB) 77 | 78 | standalone: override LDFLAGS+=-static 79 | standalone: $(PROGRAMS) 80 | strip $(PROGRAMS) 81 | 82 | libnn.a: triangle.o $(LIBOBJS) 83 | $(AR) $(ARFLAGS) libnn.a $(LIBOBJS) triangle.o 84 | chmod go+r libnn.a 85 | 86 | nncommon-vulnerable.o: override CFLAGS+=$(CFLAGS_VULNERABLE) 87 | 88 | triangle.o: triangle.c 89 | $(CC) -c -DTRILIBRARY $(CFLAGS_TRIANGLE) -I. triangle.c 90 | 91 | tests: libnn.a $(TESTS) 92 | 93 | nnai_test: 94 | $(CC) -o nnai_test nnai.c -DNNAI_TEST $(CFLAGS) -I. $(MLIB) libnn.a 95 | 96 | nnphi_test: 97 | $(CC) -o nnphi_test nnpi.c -DNNPHI_TEST $(CFLAGS) -I. $(MLIB) libnn.a 98 | 99 | ht_test: 100 | $(CC) -o ht_test hash.c -DHT_TEST $(CFLAGS) -I. $(MLIB) 101 | 102 | 103 | mex: 104 | $(MEX) $(MLIB) libnn.a mex_nn.c -o mex_nn 105 | 106 | .c.o: 107 | $(CC) $(CFLAGS) -c $*.c 108 | 109 | installdirs: 110 | $(SHELL) mkinstalldirs $(INSTALLDIRS) 111 | 112 | install: all installdirs 113 | for i in libnn.a; do \ 114 | $(INSTALL_DATA) $$i $(libdir)/$$i; \ 115 | done 116 | 117 | for i in nn.h; do \ 118 | $(INSTALL_DATA) $$i $(includedir); \ 119 | done 120 | 121 | for i in $(PROGRAMS); do \ 122 | fname=`basename $$i`; \ 123 | $(INSTALL_PROGRAM) $$i $(bindir); \ 124 | done 125 | 126 | clean: 127 | rm -f *.o libnn.a $(PROGRAMS) $(TESTS) *~ core mex_nn.mexglx 128 | 129 | configclean: 130 | rm -f config.h makefile config.cache config.status config.log 131 | 132 | ex1clean: 133 | cd examples/1; make clean; 134 | 135 | ex2clean: 136 | cd examples/2; make clean; 137 | 138 | ex3clean: 139 | cd examples/3; make clean; 140 | 141 | ex4clean: 142 | cd examples/4; make clean; 143 | 144 | ex5clean: 145 | cd examples/5; make clean; 146 | 147 | distclean: clean configclean ex1clean ex2clean ex3clean ex4clean ex5clean 148 | 149 | indent: 150 | indent -T FILE -T NN_ALGORITHM -T point -T delaunay -T lpi -T nnpi -T nnhpi -T indexedpoint -T nnai -T ht_bucket -T hashtable -T istack -T triangle -T triangle_neighbours -T circle -T nn_weights -T lweights -T minell $(LIBSRC) $(UTILSRC) $(HEADERS) 151 | rm -f *~ 152 | 153 | dist: 154 | WDIR=`pwd`;\ 155 | DIR=`basename $$WDIR`;\ 156 | cp -r $$WDIR /tmp;\ 157 | cd /tmp/$$DIR/examples/1;\ 158 | make clean;\ 159 | rm -f example.pdf;\ 160 | cd /tmp/$$DIR/examples/2;\ 161 | make clean;\ 162 | rm -f example.pdf;\ 163 | cd /tmp/$$DIR/examples/3;\ 164 | make clean;\ 165 | cd /tmp/$$DIR;\ 166 | rm -rf `find /tmp/$$DIR -name CVS`;\ 167 | if [ -e makefile ] ; then\ 168 | make distclean;\ 169 | fi;\ 170 | /tmp/"$$DIR"/configure;\ 171 | make standalone;\ 172 | chmod a+r+x $(PROGRAMS);\ 173 | mv -f $(PROGRAMS) $(DISTDIR);\ 174 | make distclean;\ 175 | cd $(DISTDIR);\ 176 | mv -f $$DIR".tar.gz" $$DIR".tar.gz.prev";\ 177 | mv -f $$DIR"-noexamples.tar.gz" $$DIR"-noexamples.tar.gz.prev";\ 178 | gzip -f $(PROGRAMS);\ 179 | cd /tmp;\ 180 | tar czvf $$DIR".tar.gz" $$DIR;\ 181 | chmod a+r $$DIR".tar.gz";\ 182 | mv -f $$DIR".tar.gz" $(DISTDIR)/$$DIR".tar.gz";\ 183 | tar czvf $$DIR"-noexamples.tar.gz" --exclude=$$DIR/examples $$DIR;\ 184 | chmod a+r $$DIR"-noexamples.tar.gz";\ 185 | mv -f $$DIR"-noexamples.tar.gz" $(DISTDIR)/$$DIR"-noexamples.tar.gz";\ 186 | rm -rf $$DIR 187 | -------------------------------------------------------------------------------- /natural_neighbour/makefile.example: -------------------------------------------------------------------------------- 1 | SHELL = /bin/sh 2 | 3 | prefix = /usr/local 4 | exec_prefix = ${prefix} 5 | bindir = ${exec_prefix}/bin 6 | libdir = /usr/local/lib 7 | includedir = ${prefix}/include 8 | 9 | INSTALLDIRS =\ 10 | $(bindir)\ 11 | $(libdir)\ 12 | $(includedir) 13 | 14 | DISTDIR = ~/pub_web 15 | 16 | INSTALL = /usr/bin/ginstall -c 17 | INSTALL_PROGRAM = ${INSTALL} 18 | INSTALL_DATA = ${INSTALL} -m 644 19 | 20 | CC = gcc 21 | CFLAGS = -g -O2 -Wall -pedantic 22 | CFLAGS_TRIANGLE = -w -ffloat-store 23 | CFLAGS_VULNERABLE = -fno-force-mem -ffloat-store 24 | LDFLAGS = 25 | 26 | AR = ar 27 | ARFLAGS = cru 28 | 29 | MLIB = -lm 30 | 31 | LIBSRC =\ 32 | delaunay.c\ 33 | hash.c\ 34 | istack.c\ 35 | lpi.c\ 36 | minell.c\ 37 | nnai.c\ 38 | nnpi.c\ 39 | nncommon.c\ 40 | nncommon-vulnerable.c 41 | 42 | UTILSRC=\ 43 | nnbathy.c 44 | 45 | HEADERS=\ 46 | delaunay.h\ 47 | hash.h\ 48 | istack.h\ 49 | minell.h\ 50 | nan.h\ 51 | nn.h\ 52 | nn_internal.h\ 53 | version.h 54 | 55 | PROGRAMS =\ 56 | minell\ 57 | nnbathy 58 | 59 | TESTS=\ 60 | nnai_test\ 61 | nnphi_test\ 62 | ht_test 63 | 64 | LIBOBJS = $(LIBSRC:.c=.o) 65 | 66 | all: libnn.a $(PROGRAMS) 67 | 68 | minell: minell.c 69 | $(CC) -o minell minell.c -DME_STANDALONE $(CFLAGS) -I. $(LDFLAGS) $(MLIB) 70 | 71 | nnbathy: libnn.a nnbathy.c 72 | $(CC) -o nnbathy nnbathy.c $(CFLAGS) -I. $(LDFLAGS) libnn.a $(MLIB) 73 | 74 | standalone: override LDFLAGS+=-static 75 | standalone: $(PROGRAMS) 76 | strip $(PROGRAMS) 77 | 78 | libnn.a: triangle.o $(LIBOBJS) 79 | $(AR) $(ARFLAGS) libnn.a $(LIBOBJS) triangle.o 80 | chmod go+r libnn.a 81 | 82 | nncommon-vulnerable.o: override CFLAGS+=$(CFLAGS_VULNERABLE) 83 | 84 | triangle.o: triangle.c 85 | $(CC) -c -DTRILIBRARY $(CFLAGS_TRIANGLE) -I. triangle.c 86 | 87 | tests: libnn.a $(TESTS) 88 | 89 | nnai_test: 90 | $(CC) -o nnai_test nnai.c -DNNAI_TEST $(CFLAGS) -I. $(MLIB) libnn.a 91 | 92 | nnphi_test: 93 | $(CC) -o nnphi_test nnpi.c -DNNPHI_TEST $(CFLAGS) -I. $(MLIB) libnn.a 94 | 95 | ht_test: 96 | $(CC) -o ht_test hash.c -DHT_TEST $(CFLAGS) -I. $(MLIB) 97 | 98 | .c.o: 99 | $(CC) $(CFLAGS) -c $*.c 100 | 101 | installdirs: 102 | $(SHELL) mkinstalldirs $(INSTALLDIRS) 103 | 104 | install: all installdirs 105 | for i in libnn.a; do \ 106 | $(INSTALL_DATA) $$i $(libdir)/$$i; \ 107 | done 108 | 109 | for i in nn.h; do \ 110 | $(INSTALL_DATA) $$i $(includedir); \ 111 | done 112 | 113 | for i in $(PROGRAMS); do \ 114 | fname=`basename $$i`; \ 115 | $(INSTALL_PROGRAM) $$i $(bindir); \ 116 | done 117 | 118 | clean: 119 | rm -f *.o libnn.a $(PROGRAMS) $(TESTS) *~ core 120 | 121 | configclean: 122 | rm -f config.h makefile config.cache config.status config.log 123 | 124 | ex1clean: 125 | cd examples/1; make clean; 126 | 127 | ex2clean: 128 | cd examples/2; make clean; 129 | 130 | ex3clean: 131 | cd examples/3; make clean; 132 | 133 | distclean: clean configclean ex1clean ex2clean ex3clean 134 | 135 | indent: 136 | indent -T FILE -T NN_ALGORITHM -T point -T delaunay -T lpi -T nnpi -T nnhpi -T indexedpoint -T nnai -T ht_bucket -T hashtable -T istack -T triangle -T triangle_neighbours -T circle -T nn_weights -T lweights -T minell $(LIBSRC) $(UTILSRC) $(HEADERS) 137 | rm -f *~ 138 | 139 | dist: 140 | WDIR=`pwd`;\ 141 | DIR=`basename $$WDIR`;\ 142 | cp -r $$WDIR /tmp;\ 143 | cd /tmp/$$DIR/examples/1;\ 144 | make clean;\ 145 | rm -f example.pdf;\ 146 | cd /tmp/$$DIR/examples/2;\ 147 | make clean;\ 148 | rm -f example.pdf;\ 149 | cd /tmp/$$DIR/examples/3;\ 150 | make clean;\ 151 | cd /tmp/$$DIR;\ 152 | rm -rf `find /tmp/$$DIR -name CVS`;\ 153 | if [ -e makefile ] ; then\ 154 | make distclean;\ 155 | fi;\ 156 | /tmp/"$$DIR"/configure;\ 157 | make standalone;\ 158 | chmod a+r+x $(PROGRAMS);\ 159 | mv -f $(PROGRAMS) $(DISTDIR);\ 160 | make distclean;\ 161 | cd $(DISTDIR);\ 162 | mv -f $$DIR".tar.gz" $$DIR".tar.gz.prev";\ 163 | gzip -f $(PROGRAMS);\ 164 | cd /tmp;\ 165 | tar czvf $$DIR".tar.gz" $$DIR;\ 166 | chmod a+r $$DIR".tar.gz";\ 167 | mv -f $$DIR".tar.gz" $(DISTDIR)/$$DIR".tar.gz";\ 168 | rm -rf $$DIR 169 | -------------------------------------------------------------------------------- /natural_neighbour/makefile.in: -------------------------------------------------------------------------------- 1 | SHELL = /bin/sh 2 | 3 | prefix = @prefix@ 4 | exec_prefix = @exec_prefix@ 5 | bindir = @bindir@ 6 | libdir = @libdir@ 7 | includedir = @includedir@ 8 | 9 | INSTALLDIRS =\ 10 | $(bindir)\ 11 | $(libdir)\ 12 | $(includedir) 13 | 14 | DISTDIR = ~/pub_web 15 | 16 | INSTALL = @INSTALL@ 17 | INSTALL_PROGRAM = @INSTALL_PROGRAM@ 18 | INSTALL_DATA = @INSTALL_DATA@ 19 | 20 | CC = @CC@ 21 | CFLAGS = @CFLAGS@ 22 | CFLAGS_TRIANGLE = @CFLAGS_TRIANGLE@ 23 | CFLAGS_VULNERABLE = @CFLAGS_VULNERABLE@ 24 | LDFLAGS = @LDFLAGS@ 25 | 26 | AR = @AR@ 27 | ARFLAGS = cru 28 | 29 | MLIB = -lm 30 | 31 | LIBSRC =\ 32 | delaunay.c\ 33 | hash.c\ 34 | istack.c\ 35 | lpi.c\ 36 | minell.c\ 37 | nnai.c\ 38 | nnpi.c\ 39 | nncommon.c\ 40 | nncommon-vulnerable.c\ 41 | preader.c 42 | 43 | UTILSRC=\ 44 | nnbathy.c 45 | 46 | HEADERS=\ 47 | delaunay.h\ 48 | hash.h\ 49 | istack.h\ 50 | minell.h\ 51 | nan.h\ 52 | nn.h\ 53 | nn_internal.h\ 54 | preader.h\ 55 | version.h 56 | 57 | PROGRAMS =\ 58 | minell\ 59 | nnbathy\ 60 | 61 | TESTS=\ 62 | nnai_test\ 63 | nnphi_test\ 64 | ht_test 65 | 66 | LIBOBJS = $(LIBSRC:.c=.o) 67 | 68 | all: libnn.a $(PROGRAMS) mex 69 | 70 | minell: minell.c 71 | $(CC) -o minell minell.c -DME_STANDALONE $(CFLAGS) -I. $(LDFLAGS) $(MLIB) 72 | 73 | nnbathy: libnn.a nnbathy.c 74 | $(CC) -o nnbathy nnbathy.c $(CFLAGS) -I. $(LDFLAGS) libnn.a $(MLIB) 75 | 76 | standalone: override LDFLAGS+=-static 77 | standalone: $(PROGRAMS) 78 | strip $(PROGRAMS) 79 | 80 | libnn.a: triangle.o $(LIBOBJS) 81 | $(AR) $(ARFLAGS) libnn.a $(LIBOBJS) triangle.o 82 | chmod go+r libnn.a 83 | 84 | nncommon-vulnerable.o: override CFLAGS+=$(CFLAGS_VULNERABLE) 85 | 86 | triangle.o: triangle.c 87 | $(CC) -c -DTRILIBRARY $(CFLAGS_TRIANGLE) -I. triangle.c 88 | 89 | tests: libnn.a $(TESTS) 90 | 91 | nnai_test: 92 | $(CC) -o nnai_test nnai.c -DNNAI_TEST $(CFLAGS) -I. $(MLIB) libnn.a 93 | 94 | nnphi_test: 95 | $(CC) -o nnphi_test nnpi.c -DNNPHI_TEST $(CFLAGS) -I. $(MLIB) libnn.a 96 | 97 | ht_test: 98 | $(CC) -o ht_test hash.c -DHT_TEST $(CFLAGS) -I. $(MLIB) 99 | 100 | 101 | mex: 102 | mex $(MLIB) libnn.a mex_nn.c -o mex_nn 103 | 104 | .c.o: 105 | $(CC) $(CFLAGS) -c $*.c 106 | 107 | installdirs: 108 | $(SHELL) mkinstalldirs $(INSTALLDIRS) 109 | 110 | install: all installdirs 111 | for i in libnn.a; do \ 112 | $(INSTALL_DATA) $$i $(libdir)/$$i; \ 113 | done 114 | 115 | for i in nn.h; do \ 116 | $(INSTALL_DATA) $$i $(includedir); \ 117 | done 118 | 119 | for i in $(PROGRAMS); do \ 120 | fname=`basename $$i`; \ 121 | $(INSTALL_PROGRAM) $$i $(bindir); \ 122 | done 123 | 124 | clean: 125 | rm -f *.o libnn.a $(PROGRAMS) $(TESTS) *~ core mex_nn.mexglx 126 | 127 | configclean: 128 | rm -f config.h makefile config.cache config.status config.log 129 | 130 | ex1clean: 131 | cd examples/1; make clean; 132 | 133 | ex2clean: 134 | cd examples/2; make clean; 135 | 136 | ex3clean: 137 | cd examples/3; make clean; 138 | 139 | ex4clean: 140 | cd examples/4; make clean; 141 | 142 | ex5clean: 143 | cd examples/5; make clean; 144 | 145 | distclean: clean configclean ex1clean ex2clean ex3clean ex4clean ex5clean 146 | 147 | indent: 148 | indent -T FILE -T NN_ALGORITHM -T point -T delaunay -T lpi -T nnpi -T nnhpi -T indexedpoint -T nnai -T ht_bucket -T hashtable -T istack -T triangle -T triangle_neighbours -T circle -T nn_weights -T lweights -T minell $(LIBSRC) $(UTILSRC) $(HEADERS) 149 | rm -f *~ 150 | 151 | dist: 152 | WDIR=`pwd`;\ 153 | DIR=`basename $$WDIR`;\ 154 | cp -r $$WDIR /tmp;\ 155 | cd /tmp/$$DIR/examples/1;\ 156 | make clean;\ 157 | rm -f example.pdf;\ 158 | cd /tmp/$$DIR/examples/2;\ 159 | make clean;\ 160 | rm -f example.pdf;\ 161 | cd /tmp/$$DIR/examples/3;\ 162 | make clean;\ 163 | cd /tmp/$$DIR;\ 164 | rm -rf `find /tmp/$$DIR -name CVS`;\ 165 | if [ -e makefile ] ; then\ 166 | make distclean;\ 167 | fi;\ 168 | /tmp/"$$DIR"/configure;\ 169 | make standalone;\ 170 | chmod a+r+x $(PROGRAMS);\ 171 | mv -f $(PROGRAMS) $(DISTDIR);\ 172 | make distclean;\ 173 | cd $(DISTDIR);\ 174 | mv -f $$DIR".tar.gz" $$DIR".tar.gz.prev";\ 175 | mv -f $$DIR"-noexamples.tar.gz" $$DIR"-noexamples.tar.gz.prev";\ 176 | gzip -f $(PROGRAMS);\ 177 | cd /tmp;\ 178 | tar czvf $$DIR".tar.gz" $$DIR;\ 179 | chmod a+r $$DIR".tar.gz";\ 180 | mv -f $$DIR".tar.gz" $(DISTDIR)/$$DIR".tar.gz";\ 181 | tar czvf $$DIR"-noexamples.tar.gz" --exclude=$$DIR/examples $$DIR;\ 182 | chmod a+r $$DIR"-noexamples.tar.gz";\ 183 | mv -f $$DIR"-noexamples.tar.gz" $(DISTDIR)/$$DIR"-noexamples.tar.gz";\ 184 | rm -rf $$DIR 185 | -------------------------------------------------------------------------------- /natural_neighbour/mex_nn.c: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include "nan.h" 3 | #include "minell.h" 4 | #include "nn.h" 5 | 6 | #include 7 | 8 | #if !defined(_DBL_ARRAY_) 9 | #define _DBL_ARRAY 10 | typedef struct { 11 | double *pr; 12 | int n; 13 | int m; 14 | int s; 15 | } dbl_array; 16 | #endif 17 | 18 | void load_dbl_array (const mxArray *pr, dbl_array *arr) 19 | { 20 | arr->pr = mxGetPr(pr); 21 | arr->m = mxGetM(pr); 22 | arr->n = mxGetN(pr); 23 | arr->s = arr->n * arr->m; 24 | } 25 | 26 | void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 27 | { 28 | dbl_array x_in, y_in, z_in, x_out, y_out; 29 | point *in_points = NULL; 30 | point *out_points = NULL; 31 | delaunay *d = NULL; 32 | void *interpolator = NULL; 33 | double *x_out_pr, *y_out_pr, *z_out_pr; 34 | 35 | int ii; 36 | 37 | /* Declarations */ 38 | if (nrhs != 5) { 39 | mexErrMsgTxt("Five input arguments required.\n"); 40 | } else if (nlhs > 3) { 41 | mexErrMsgTxt("Too many output arguments.\n"); 42 | } 43 | 44 | /* We only handle doubles */ 45 | if (!mxIsDouble(prhs[0])) { 46 | mexErrMsgTxt("Input image should be double.\n"); 47 | } 48 | 49 | /* load array and size into struct */ 50 | load_dbl_array(prhs[0], &x_in); 51 | load_dbl_array(prhs[1], &y_in); 52 | load_dbl_array(prhs[2], &z_in); 53 | load_dbl_array(prhs[3], &x_out); 54 | load_dbl_array(prhs[4], &y_out); 55 | 56 | /* Check intput sizes */ 57 | if (x_in.s != y_in.s || x_in.s != z_in.s) 58 | mexErrMsgTxt("x, y, z should be equal sized.\n"); 59 | 60 | /* Check output sizes */ 61 | if (x_out.s != y_out.s) 62 | mexErrMsgTxt("output coordinates should be equal sized.\n"); 63 | 64 | in_points = malloc(x_in.s * sizeof(point)); 65 | 66 | /* Convert inputs to array of points. */ 67 | for (ii = 0; ii < x_in.s; ii++) { 68 | in_points[ii].x = x_in.pr[ii]; 69 | in_points[ii].y = y_in.pr[ii]; 70 | in_points[ii].z = z_in.pr[ii]; 71 | } 72 | 73 | out_points = malloc(x_out.s * sizeof(point)); 74 | 75 | /* Convert outputs to array of points. */ 76 | for (ii = 0; ii < x_out.s; ii++) { 77 | out_points[ii].x = x_out.pr[ii]; 78 | out_points[ii].y = y_out.pr[ii]; 79 | out_points[ii].z = NaN; 80 | } 81 | 82 | /* Create delaunay triangulation*/ 83 | d = delaunay_build(x_in.s, in_points, 0, NULL, 0, NULL); 84 | 85 | 86 | /* Create interpolator */ 87 | interpolator = nnpi_create(d); 88 | nnpi_setwmin(interpolator, -DBL_MAX); 89 | 90 | /* Serially interpolate */ 91 | for (ii = 0; ii < x_out.s; ii++) { 92 | nnpi_interpolate_point(interpolator, &out_points[ii]); 93 | } 94 | 95 | /* Convert back to mxArray format */ 96 | 97 | /* Allocate everything */ 98 | 99 | /*-------------------------------------------------- 100 | * if (nlhs == 3) { 101 | * plhs[0] = mxCreateDoubleMatrix(x_out.m, x_out.n, mxREAL); 102 | * plhs[1] = mxCreateDoubleMatrix(y_out.m, y_out.n, mxREAL); 103 | * plhs[2] = mxCreateDoubleMatrix(z_out.m, z_out.n, mxREAL); 104 | * x_out_pr = mxGetPr(plhs[0]); 105 | * y_out_pr = mxGetPr(plhs[1]); 106 | * z_out_pr = mxGetPr(plhs[2]); 107 | * for (ii = 0; ii < x_out.s; ii++) { 108 | * x_out_pr[ii] = out_points[ii].x; 109 | * y_out_pr[ii] = out_points[ii].y; 110 | * z_out_pr[ii] = out_points[ii].z; 111 | * } 112 | * } else 113 | *--------------------------------------------------*/ 114 | if (nlhs == 1) { 115 | plhs[0] = mxCreateDoubleMatrix(x_out.m, x_out.n, mxREAL); 116 | z_out_pr = mxGetPr(plhs[0]); 117 | 118 | for (ii = 0; ii < x_out.s; ii++) { 119 | z_out_pr[ii] = out_points[ii].z; 120 | } 121 | } else { 122 | mexErrMsgTxt("Number of output arguments should be 1 or 3.\n"); 123 | } 124 | 125 | 126 | 127 | 128 | /* Free everything */ 129 | nnpi_destroy(interpolator); 130 | delaunay_destroy(d); 131 | free(in_points); 132 | free(out_points); 133 | } 134 | -------------------------------------------------------------------------------- /natural_neighbour/mex_nn.mexmaci: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattfoster/matlab-interpolation-toolkit/0d4d7af6aa1bddb6cda0ec18049c6141ea55df6b/natural_neighbour/mex_nn.mexmaci -------------------------------------------------------------------------------- /natural_neighbour/minell.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.minell 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleShortVersionString 14 | 1.0 15 | CFBundleSignature 16 | ???? 17 | CFBundleVersion 18 | 1.0 19 | dSYM_UUID 20 | 21 | i386 22 | e1 a6 c4 4a b2 fd cd 2c e2 8a f4 40 62 d8 55 36 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /natural_neighbour/minell.dSYM/Contents/Resources/DWARF/minell: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattfoster/matlab-interpolation-toolkit/0d4d7af6aa1bddb6cda0ec18049c6141ea55df6b/natural_neighbour/minell.dSYM/Contents/Resources/DWARF/minell -------------------------------------------------------------------------------- /natural_neighbour/minell.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: minell.h 4 | * 5 | * Created: 24/02/2003 6 | * 7 | * Author: Pavel Sakov 8 | * CSIRO Marine Research 9 | * 10 | * Purpose: A header for the minimal ellipse stuff 11 | * 12 | * Revisions: None 13 | * 14 | *****************************************************************************/ 15 | 16 | #if !defined(_MINELL_H) 17 | #define _MINELL_H 18 | 19 | #if !defined(_POINT_STRUCT) 20 | #define _POINT_STRUCT 21 | typedef struct { 22 | double x; 23 | double y; 24 | double z; 25 | } point; 26 | #endif 27 | 28 | #if !defined(_MINELL_STRUCT) 29 | #define _MINELL_STRUCT 30 | struct minell; 31 | typedef struct minell minell; 32 | #endif 33 | 34 | /* Note that minell_build() shuffles the input point array */ 35 | minell* minell_build(int n, point p[]); 36 | void minell_destroy(minell* me); 37 | void minell_scalepoints(minell* me, int n, point p[]); 38 | void minell_rescalepoints(minell* me, int n, point p[]); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /natural_neighbour/mkinstalldirs: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # mkinstalldirs --- make directory hierarchy 3 | # Author: Noah Friedman 4 | # Created: 1993-05-16 5 | # Public domain 6 | 7 | errstatus=0 8 | 9 | for file 10 | do 11 | set fnord `echo ":$file" | sed -ne 's/^:\//#/;s/^://;s/\// /g;s/^#/\//;p'` 12 | shift 13 | 14 | pathcomp= 15 | for d 16 | do 17 | pathcomp="$pathcomp$d" 18 | case "$pathcomp" in 19 | -* ) pathcomp=./$pathcomp ;; 20 | esac 21 | 22 | if test ! -d "$pathcomp"; then 23 | echo "mkdir $pathcomp" 1>&2 24 | 25 | mkdir "$pathcomp" || lasterr=$? 26 | 27 | if test ! -d "$pathcomp"; then 28 | errstatus=$lasterr 29 | fi 30 | fi 31 | 32 | pathcomp="$pathcomp/" 33 | done 34 | done 35 | 36 | exit $errstatus 37 | 38 | # mkinstalldirs ends here 39 | -------------------------------------------------------------------------------- /natural_neighbour/nan.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: nan.h 4 | * 5 | * Created: 18/10/2001 6 | * 7 | * Author: Pavel Sakov 8 | * CSIRO Marine Research 9 | * 10 | * Purpose: NaN definition 11 | * 12 | * Description: Should cover machines with 64 bit doubles or other machines 13 | * with GCC 14 | * 15 | * Revisions: None 16 | * 17 | *****************************************************************************/ 18 | 19 | #if !defined(_NAN_H) 20 | #define _NAN_H 21 | 22 | #if defined(__GNUC__) 23 | 24 | static const double NaN = 0.0 / 0.0; 25 | 26 | #elif defined(_WIN32) 27 | 28 | static unsigned _int64 lNaN = ((unsigned _int64) 1 << 63) - 1; 29 | 30 | #define NaN (*(double*)&lNaN) 31 | 32 | #else 33 | 34 | static const long long lNaN = ((unsigned long long) 1 << 63) - 1; 35 | 36 | #define NaN (*(double*)&lNaN) 37 | 38 | #endif 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /natural_neighbour/natural_neighbour.m: -------------------------------------------------------------------------------- 1 | function zi = natural_neighbour(xx, yy, zz, xi, yi) 2 | % Perform interpolation using the natural neighbour method. 3 | % zi = natural_neighbour(xx, yy, zz, xi, yi) 4 | % 5 | % Perform natural neighbour interpolation using code from 6 | % http://www.marine.csiro.au/~sak007/ 7 | % 8 | % Arguments: 9 | % xx, yy, zz = scattered input data 10 | % xi, yi = output positions 11 | % 12 | % See also: 13 | % griddata, adaptiveNC, rbf, kriging 14 | % 15 | % Mex gateway by Matt P. Foster 16 | 17 | error(nargchk(5, 5, nargin)); 18 | zi = mex_nn(xx, yy, zz, xi, yi); 19 | 20 | end % function -------------------------------------------------------------------------------- /natural_neighbour/nn.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: nn.h 4 | * 5 | * Created: 04/08/2000 6 | * 7 | * Author: Pavel Sakov 8 | * CSIRO Marine Research 9 | * 10 | * Purpose: Header file for nn library 11 | * 12 | * Description: None 13 | * 14 | * Revisions: None 15 | * 16 | *****************************************************************************/ 17 | 18 | #if !defined(_NN_H) 19 | #define _NN_H 20 | 21 | typedef enum { SIBSON, NON_SIBSONIAN } NN_RULE; 22 | 23 | /* "point" is a basic data structure in this package. 24 | */ 25 | #if !defined(_POINT_STRUCT) 26 | #define _POINT_STRUCT 27 | typedef struct { 28 | double x; 29 | double y; 30 | double z; 31 | } point; 32 | #endif 33 | 34 | /* Constructors for interpolators in this package require Delaunay 35 | * triangulation of the input data. 36 | */ 37 | #if !defined(_DELAUNAY_STRUCT) 38 | #define _DELAUNAY_STRUCT 39 | struct delaunay; 40 | typedef struct delaunay delaunay; 41 | #endif 42 | 43 | /** Builds Delaunay triangulation of the given array of points. 44 | * 45 | * @param np Number of points 46 | * @param points Array of points [np] (input) 47 | * @param ns Number of forced segments 48 | * @param segments Array of (forced) segment endpoint indices [2*ns] 49 | * @param nh Number of holes 50 | * @param holes Array of hole (x,y) coordinates [2*nh] 51 | * @return Delaunay triangulation structure with triangulation results 52 | */ 53 | delaunay* delaunay_build(int np, point points[], int ns, int segments[], int nh, double holes[]); 54 | 55 | /** Destroys Delaunay triangulation. 56 | * 57 | * @param d Structure to be destroyed 58 | */ 59 | void delaunay_destroy(delaunay* d); 60 | 61 | /** Smoothes the input point array by averaging the input x,y and z values 62 | ** for each cell within virtual rectangular nx by ny grid. The corners of the 63 | ** grid are created from min and max values of the input array. It also frees 64 | ** the original array and returns results and new dimension via original 65 | ** data and size pointers. 66 | * 67 | * @param n Pointer to number of points (input/output) 68 | * @param p Pointer to array of points (input/output) [*n] 69 | * @param nx Number of x nodes in decimation 70 | * @param ny Number of y nodes in decimation 71 | */ 72 | void points_thingrid(int* n, point** p, int nx, int ny); 73 | 74 | /** Smoothes the input point array by averaging the input data (X,Y and Z 75 | ** values) until the sum of the distances between points does not exceed the 76 | ** specified maximum value. It also frees the original array and returns 77 | ** results and new dimension via original data and size pointers. 78 | * 79 | * @param n Pointer to number of points (input/output) 80 | * @param p Pointer to array of points (input/output) [*n] 81 | * @param rmax Maximum allowed accumulated distance 82 | */ 83 | void points_thinlin(int* n, point** p, double rmax); 84 | 85 | /* Calculates X and/or Y ranges of the input array of points. If necessary, 86 | * adjusts the range according to the zoom value. 87 | * 88 | * @param n Number of points 89 | * @param points Array of points 90 | * @param xmin Min X value if *xmin = NaN on input, not changed otherwise 91 | * @param xmax Max X value if *xmax = NaN on input, not changed otherwise 92 | * @param ymin Min Y value if *ymin = NaN on input, not changed otherwise 93 | * @param ymax Max Y value if *ymax = NaN on input, not changed otherwise 94 | */ 95 | void points_getrange(int n, point points[], double zoom, double* xmin, double* xmax, double* ymin, double* ymax); 96 | 97 | /** Generates rectangular grid nx by ny using specified min and max x and y 98 | ** values. Allocates space for the output point array, be sure to free it 99 | ** when necessary! 100 | * 101 | * @param xmin Min x value 102 | * @param xmax Max x value 103 | * @param ymin Min y value 104 | * @param ymax Max y value 105 | * @param nx Number of x nodes 106 | * @param ny Number of y nodes 107 | * @param zoom Zoom coefficient 108 | * @param nout Pointer to number of output points 109 | * @param pout Pointer to array of output points [*nout] 110 | */ 111 | void points_generate(double xmin, double xmax, double ymin, double ymax, int nx, int ny, int* nout, point** pout); 112 | 113 | /** Reads array of points from a columnar file. 114 | * 115 | * @param fname File name (can be "stdin" dor stndard input) 116 | * @param dim Number of dimensions (must be 2 or 3) 117 | * @param n Pointer to number of points (output) 118 | * @param points Pointer to array of points [*n] (output) 119 | */ 120 | void points_read(char* fname, int dim, int* n, point** points); 121 | 122 | /** Scales Y coordinate so that the resulting set fits into square: 123 | ** xmax - xmin = ymax - ymin 124 | * 125 | * @param n Number of points 126 | * @param points The points to scale 127 | * @return Y axis compression coefficient 128 | */ 129 | double points_scaletosquare(int n, point* points); 130 | 131 | /** Compresses Y domain by a given multiple. 132 | * 133 | * @param n Number of points 134 | * @param points The points to scale 135 | * @param Y axis compression coefficient as returned by points_scaletosquare() 136 | */ 137 | void points_scale(int n, point* points, double k); 138 | 139 | /** `lpi' -- "Linear Point Interpolator" is a structure for linear 140 | ** interpolation of data on a "point-to-point" basis. 141 | * 142 | * `lpi' interpolates linearly within each triangle resulted from the Delaunay 143 | * triangluation of the input data. `lpi' is much faster than all Natural 144 | * Neighbours interpolators below. 145 | */ 146 | struct lpi; 147 | typedef struct lpi lpi; 148 | 149 | /** Builds linear interpolator. 150 | * 151 | * @param d Delaunay triangulation 152 | * @return Linear interpolator 153 | */ 154 | lpi* lpi_build(delaunay* d); 155 | 156 | /** Destroys linear interpolator. 157 | * 158 | * @param l Structure to be destroyed 159 | */ 160 | void lpi_destroy(lpi* l); 161 | 162 | /** Finds linearly interpolated value in a point. 163 | * 164 | * @param l Linear point interpolator 165 | * @param p Point to be interpolated (p->x, p->y -- input; p->z -- output) 166 | */ 167 | void lpi_interpolate_point(lpi* l, point* p); 168 | 169 | /** Linearly interpolates data in an array of points. 170 | * 171 | * @param nin Number of input points 172 | * @param pin Array of input points [pin] 173 | * @param nout Number of ouput points 174 | * @param pout Array of output points [nout] 175 | */ 176 | void lpi_interpolate_points(int nin, point pin[], int nout, point pout[]); 177 | 178 | /** `nnpi' -- "Natural Neighbours Point Interpolator" is a structure for 179 | ** Natural Neighbours interpolation of data on a "point-to-point" basis. 180 | * 181 | * Because it involves weight calculation for each output point, it is not 182 | * designed to take advantage of consequitive interpolations on the same 183 | * sets of input and output points -- use `nnhpi' or `nnai' in these cases. 184 | */ 185 | struct nnpi; 186 | typedef struct nnpi nnpi; 187 | 188 | /** Creates Natural Neighbours point interpolator. 189 | * 190 | * @param d Delaunay triangulation 191 | * @return Natural Neighbours interpolation 192 | */ 193 | nnpi* nnpi_create(delaunay* d); 194 | 195 | /** Destroys Natural Neighbours point interpolation. 196 | * 197 | * @param nn Structure to be destroyed 198 | */ 199 | void nnpi_destroy(nnpi* nn); 200 | 201 | /** Performs Natural Neighbours interpolation in a point. 202 | * 203 | * @param nn NN point interpolator 204 | * @param p Point to be interpolated (p->x, p->y -- input; p->z -- output) 205 | */ 206 | void nnpi_interpolate_point(nnpi* nn, point* p); 207 | 208 | /** Performs Natural Neighbours interpolation in an array of points. 209 | * 210 | * @param nin Number of input points 211 | * @param pin Array of input points [pin] 212 | * @param wmin Minimal allowed weight 213 | * @param nout Number of output points 214 | * @param pout Array of output points [nout] 215 | */ 216 | void nnpi_interpolate_points(int nin, point pin[], double wmin, int nout, point pout[]); 217 | 218 | /** Sets minimal allowed weight for Natural Neighbours interpolation. 219 | * 220 | * For Sibson interpolation, setting wmin = 0 is equivalent to interpolating 221 | * inside convex hall of the data only (returning NaNs otherwise). 222 | * 223 | * @param nn Natural Neighbours point interpolator 224 | * @param wmin Minimal allowed weight 225 | */ 226 | void nnpi_setwmin(nnpi* nn, double wmin); 227 | 228 | /** `nnhpi' -- "Natural Neighbours Hashing Point Interpolator" -- is a 229 | ** structure for conducting consequitive Natural Neighbours interpolations 230 | ** from the same set of observation points, designed to take advantage of 231 | ** repeated interpolations in the same point. It allows to modify Z 232 | ** coordinate of observed data between interpolations (because this does not 233 | ** affect the interpolant weights). 234 | */ 235 | struct nnhpi; 236 | typedef struct nnhpi nnhpi; 237 | 238 | /** Creates Natural Neighbours hashing point interpolator. 239 | * 240 | * @param d Delaunay triangulation 241 | * @param size Hash table size (should be of order of number of output points) 242 | * @return Natural Neighbours interpolation 243 | */ 244 | nnhpi* nnhpi_create(delaunay* d, int size); 245 | 246 | /** Destroys Natural Neighbours hashing point interpolation. 247 | * 248 | * @param nn Structure to be destroyed 249 | */ 250 | void nnhpi_destroy(nnhpi* nn); 251 | 252 | /** Performs Natural Neighbours interpolation in a point. 253 | * 254 | * @param nnhpi NN hashing point interpolator 255 | * @param p Point to be interpolated (p->x, p->y -- input; p->z -- output) 256 | */ 257 | void nnhpi_interpolate(nnhpi* nn, point* p); 258 | 259 | /** Modifies interpolated data. 260 | * 261 | * Finds point* pd in the underlying Delaunay triangulation such that 262 | * pd->x = p->x and pd->y = p->y, and copies p->z to pd->z. Exits with error 263 | * if the point is not found. 264 | * 265 | * @param nn Natural Neighbours hashing point interpolator 266 | * @param p New data 267 | */ 268 | void nnhpi_modify_data(nnhpi* nn, point* p); 269 | 270 | /** Sets minimal allowed weight for Natural Neighbours interpolation. 271 | * 272 | * For Sibson interpolation, setting wmin = 0 is equivalent to interpolating 273 | * inside convex hall of the data only (returning NaNs otherwise). 274 | * 275 | * @param nn Natural Neighbours point hashing interpolator 276 | * @param wmin Minimal allowed weight 277 | */ 278 | void nnhpi_setwmin(nnhpi* nn, double wmin); 279 | 280 | /** `nnai' -- "Natural Neighbours Array Interpolator" is a structure for 281 | ** conducting consequitive Natural Neighbours interpolations from the same 282 | ** set of observation points in the same set of points. It allows to modify Z 283 | ** coordinate of data between interpolations (because this does not 284 | ** affect the interpolant weights). 285 | * 286 | * `nnai' is the fastest of the three Natural Neighbours interpolators in `nn' 287 | * library. 288 | */ 289 | struct nnai; 290 | typedef struct nnai nnai; 291 | 292 | /** Builds Natural Neighbours array interpolator. 293 | * 294 | * This includes calculation of weights used in nnai_interpolate(). 295 | * 296 | * @param d Delaunay triangulation 297 | * @return Natural Neighbours interpolation 298 | */ 299 | nnai* nnai_build(delaunay* d, int n, double* x, double* y); 300 | 301 | /** Destroys Natural Neighbours array interpolator. 302 | * 303 | * @param nn Structure to be destroyed 304 | */ 305 | void nnai_destroy(nnai* nn); 306 | 307 | /** Conducts NN interpolation in a fixed array of output points using 308 | ** data specified in a fixed array of input points. Uses pre-calculated 309 | ** weights. 310 | * 311 | * @param nn NN array interpolator 312 | * @param zin input data [nn->d->npoints] 313 | * @param zout output data [nn->n]. Must be pre-allocated! 314 | */ 315 | void nnai_interpolate(nnai* nn, double* zin, double* zout); 316 | 317 | /** Sets minimal allowed weight for Natural Neighbours interpolation. 318 | * 319 | * For Sibson interpolation, setting wmin = 0 is equivalent to interpolating 320 | * inside convex hall of the input data only (returning NaNs otherwise). 321 | * 322 | * @param nn Natural Neighbours array interpolator 323 | * @param wmin Minimal allowed weight 324 | */ 325 | void nnai_setwmin(nnai* nn, double wmin); 326 | 327 | /* Sets the verbosity level within nn package. 328 | * 0 (default) - silent 329 | * 1 - verbose 330 | * 2 - very verbose 331 | */ 332 | extern int nn_verbose; 333 | 334 | /* Switches between different formulations for NN weights. 335 | * SIBSON -- classic formulation by Sibson 336 | * NON_SIBSONIAN -- alternative formulation by Belikov & Semenov 337 | * 338 | */ 339 | extern NN_RULE nn_rule; 340 | 341 | /* Contains version string for the nn package. 342 | */ 343 | extern char* nn_version; 344 | 345 | /* Limits verbose information to a particular vertex (used mainly for 346 | * debugging purposes). 347 | */ 348 | extern int nn_test_vertice; 349 | 350 | #endif /* _NN_H */ 351 | -------------------------------------------------------------------------------- /natural_neighbour/nn_internal.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: nn_internal.h 4 | * 5 | * Created: 11/03/2005 6 | * 7 | * Author: Pavel Sakov 8 | * CSIRO Marine Research 9 | * 10 | * Purpose: Header for internal stuff in the nn library 11 | * 12 | * Description: None 13 | * 14 | * Revisions: None 15 | * 16 | *****************************************************************************/ 17 | 18 | #if !defined(_NN_INTERNAL_H) 19 | #define _NN_INTERNAL_H 20 | 21 | /* 22 | * nnpi.c 23 | */ 24 | void nnpi_calculate_weights(nnpi* nn, point* p); 25 | int nnpi_get_nvertices(nnpi* nn); 26 | int* nnpi_get_vertices(nnpi* nn); 27 | double* nnpi_get_weights(nnpi* nn); 28 | 29 | /* 30 | * nncommon.c, nncommon-vulnerable.c 31 | */ 32 | int circle_build1(circle* c, point* p0, point* p1, point* p2); 33 | int circle_build2(circle* c, point* p0, point* p1, point* p2); 34 | int circle_contains(circle* c, point* p); 35 | void nn_quit(char* format, ...); 36 | int str2double(char* token, double* value); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /natural_neighbour/nnai.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: nnai.c 4 | * 5 | * Created: 15/11/2002 6 | * 7 | * Author: Pavel Sakov 8 | * CSIRO Marine Research 9 | * 10 | * Purpose: Code for: 11 | * -- Natural Neighbours Array Interpolator 12 | * 13 | * Description: `nnai' is a tructure for conducting 14 | * consequitive Natural Neighbours interpolations on a given 15 | * spatial data set in a given array of points. It allows to 16 | * modify Z coordinate of data in between interpolations. 17 | * `nnai' is the fastest of the three Natural 18 | * Neighbours interpolators in `nn' library. 19 | * 20 | * Revisions: None 21 | * 22 | *****************************************************************************/ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include "nan.h" 29 | #include "delaunay.h" 30 | #include "nn.h" 31 | #include "nn_internal.h" 32 | 33 | typedef struct { 34 | int nvertices; 35 | int* vertices; /* vertex indices [nvertices] */ 36 | double* weights; /* vertex weights [nvertices] */ 37 | } nn_weights; 38 | 39 | struct nnai { 40 | delaunay* d; 41 | double wmin; 42 | double n; /* number of output points */ 43 | double* x; /* [n] */ 44 | double* y; /* [n] */ 45 | nn_weights* weights; 46 | }; 47 | 48 | /** Builds Natural Neighbours array interpolator. 49 | * 50 | * This includes calculation of weights used in nnai_interpolate(). 51 | * 52 | * @param d Delaunay triangulation 53 | * @return Natural Neighbours interpolation 54 | */ 55 | nnai* nnai_build(delaunay* d, int n, double* x, double* y) 56 | { 57 | nnai* nn = malloc(sizeof(nnai)); 58 | nnpi* nnpi = nnpi_create(d); 59 | int* vertices; 60 | double* weights; 61 | int i; 62 | 63 | if (n <= 0) 64 | nn_quit("nnai_create(): n = %d\n", n); 65 | 66 | nn->d = d; 67 | nn->n = n; 68 | nn->x = malloc(n * sizeof(double)); 69 | memcpy(nn->x, x, n * sizeof(double)); 70 | nn->y = malloc(n * sizeof(double)); 71 | memcpy(nn->y, y, n * sizeof(double)); 72 | nn->weights = malloc(n * sizeof(nn_weights)); 73 | 74 | for (i = 0; i < n; ++i) { 75 | nn_weights* w = &nn->weights[i]; 76 | point p; 77 | 78 | p.x = x[i]; 79 | p.y = y[i]; 80 | 81 | nnpi_calculate_weights(nnpi, &p); 82 | 83 | vertices = nnpi_get_vertices(nnpi); 84 | weights = nnpi_get_weights(nnpi); 85 | 86 | w->nvertices = nnpi_get_nvertices(nnpi); 87 | w->vertices = malloc(w->nvertices * sizeof(int)); 88 | memcpy(w->vertices, vertices, w->nvertices * sizeof(int)); 89 | w->weights = malloc(w->nvertices * sizeof(double)); 90 | memcpy(w->weights, weights, w->nvertices * sizeof(double)); 91 | } 92 | 93 | nnpi_destroy(nnpi); 94 | 95 | return nn; 96 | } 97 | 98 | /* Destroys Natural Neighbours array interpolator. 99 | * 100 | * @param nn Structure to be destroyed 101 | */ 102 | void nnai_destroy(nnai* nn) 103 | { 104 | int i; 105 | 106 | for (i = 0; i < nn->n; ++i) { 107 | nn_weights* w = &nn->weights[i]; 108 | 109 | free(w->vertices); 110 | free(w->weights); 111 | } 112 | 113 | free(nn->x); 114 | free(nn->y); 115 | free(nn->weights); 116 | free(nn); 117 | } 118 | 119 | /* Conducts NN interpolation in a fixed array of output points using 120 | * data specified in a fixed array of input points. Uses pre-calculated 121 | * weights. 122 | * 123 | * @param nn NN array interpolator 124 | * @param zin input data [nn->d->npoints] 125 | * @param zout output data [nn->n]. Must be pre-allocated! 126 | */ 127 | void nnai_interpolate(nnai* nn, double* zin, double* zout) 128 | { 129 | int i; 130 | 131 | for (i = 0; i < nn->n; ++i) { 132 | nn_weights* w = &nn->weights[i]; 133 | double z = 0.0; 134 | int j; 135 | 136 | for (j = 0; j < w->nvertices; ++j) { 137 | double weight = w->weights[j]; 138 | 139 | if (weight < nn->wmin) { 140 | z = NaN; 141 | break; 142 | } 143 | z += weight * zin[w->vertices[j]]; 144 | } 145 | 146 | zout[i] = z; 147 | } 148 | } 149 | 150 | /* Sets minimal allowed weight for Natural Neighbours interpolation. 151 | * 152 | * For Sibson interpolation, setting wmin = 0 is equivalent to interpolating 153 | * inside convex hall of the data only (returning NaNs otherwise). 154 | * 155 | * @param nn Natural Neighbours array interpolator 156 | * @param wmin Minimal allowed weight 157 | */ 158 | void nnai_setwmin(nnai* nn, double wmin) 159 | { 160 | nn->wmin = wmin; 161 | } 162 | 163 | /* The rest of this file contains a number of test programs. 164 | */ 165 | #if defined(NNAI_TEST) 166 | 167 | #include 168 | 169 | #define NPOINTSIN 10000 170 | #define NMIN 10 171 | #define NX 101 172 | #define NXMIN 1 173 | 174 | #define SQ(x) ((x) * (x)) 175 | 176 | static double franke(double x, double y) 177 | { 178 | x *= 9.0; 179 | y *= 9.0; 180 | return 0.75 * exp((-SQ(x - 2.0) - SQ(y - 2.0)) / 4.0) 181 | + 0.75 * exp(-SQ(x - 2.0) / 49.0 - (y - 2.0) / 10.0) 182 | + 0.5 * exp((-SQ(x - 7.0) - SQ(y - 3.0)) / 4.0) 183 | - 0.2 * exp(-SQ(x - 4.0) - SQ(y - 7.0)); 184 | } 185 | 186 | static void usage() 187 | { 188 | printf("Usage: nnai_test [-v|-V] [-n ]\n"); 189 | printf("Options:\n"); 190 | printf(" -a -- use non-Sibsonian interpolation rule\n"); 191 | printf(" -n :\n"); 192 | printf(" -- number of input points (default = 10000)\n"); 193 | printf(" -- number of output points per side (default = 64)\n"); 194 | printf(" -v -- verbose\n"); 195 | printf(" -V -- very verbose\n"); 196 | } 197 | 198 | int main(int argc, char* argv[]) 199 | { 200 | int nin = NPOINTSIN; 201 | int nx = NX; 202 | int nout = 0; 203 | point* pin = NULL; 204 | delaunay* d = NULL; 205 | point* pout = NULL; 206 | nnai* nn = NULL; 207 | double* zin = NULL; 208 | double* xout = NULL; 209 | double* yout = NULL; 210 | double* zout = NULL; 211 | int cpi = -1; /* control point index */ 212 | struct timeval tv0, tv1, tv2; 213 | struct timezone tz; 214 | int i; 215 | 216 | i = 1; 217 | while (i < argc) { 218 | switch (argv[i][1]) { 219 | case 'a': 220 | i++; 221 | nn_rule = NON_SIBSONIAN; 222 | break; 223 | case 'n': 224 | i++; 225 | if (i >= argc) 226 | nn_quit("no number of data points found after -i\n"); 227 | nin = atoi(argv[i]); 228 | i++; 229 | if (i >= argc) 230 | nn_quit("no number of ouput points per side found after -i\n"); 231 | nx = atoi(argv[i]); 232 | i++; 233 | break; 234 | case 'v': 235 | i++; 236 | nn_verbose = 1; 237 | break; 238 | case 'V': 239 | i++; 240 | nn_verbose = 2; 241 | break; 242 | default: 243 | usage(); 244 | break; 245 | } 246 | } 247 | 248 | if (nin < NMIN) 249 | nin = NMIN; 250 | if (nx < NXMIN) 251 | nx = NXMIN; 252 | 253 | printf("\nTest of Natural Neighbours array interpolator:\n\n"); 254 | printf(" %d data points\n", nin); 255 | printf(" %d output points\n", nx * nx); 256 | 257 | /* 258 | * generate data 259 | */ 260 | printf(" generating data:\n"); 261 | fflush(stdout); 262 | pin = malloc(nin * sizeof(point)); 263 | zin = malloc(nin * sizeof(double)); 264 | for (i = 0; i < nin; ++i) { 265 | point* p = &pin[i]; 266 | 267 | p->x = (double) random() / RAND_MAX; 268 | p->y = (double) random() / RAND_MAX; 269 | p->z = franke(p->x, p->y); 270 | zin[i] = p->z; 271 | if (nn_verbose) 272 | printf(" (%f, %f, %f)\n", p->x, p->y, p->z); 273 | } 274 | 275 | /* 276 | * triangulate 277 | */ 278 | printf(" triangulating:\n"); 279 | fflush(stdout); 280 | d = delaunay_build(nin, pin, 0, NULL, 0, NULL); 281 | 282 | /* 283 | * generate output points 284 | */ 285 | points_generate(-0.1, 1.1, -0.1, 1.1, nx, nx, &nout, &pout); 286 | xout = malloc(nout * sizeof(double)); 287 | yout = malloc(nout * sizeof(double)); 288 | zout = malloc(nout * sizeof(double)); 289 | for (i = 0; i < nout; ++i) { 290 | point* p = &pout[i]; 291 | 292 | xout[i] = p->x; 293 | yout[i] = p->y; 294 | zout[i] = NaN; 295 | } 296 | cpi = (nx / 2) * (nx + 1); 297 | 298 | gettimeofday(&tv0, &tz); 299 | 300 | /* 301 | * create interpolator 302 | */ 303 | printf(" creating interpolator:\n"); 304 | fflush(stdout); 305 | nn = nnai_build(d, nout, xout, yout); 306 | 307 | fflush(stdout); 308 | gettimeofday(&tv1, &tz); 309 | { 310 | long dt = 1000000 * (tv1.tv_sec - tv0.tv_sec) + tv1.tv_usec - tv0.tv_usec; 311 | 312 | printf(" interpolator creation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout); 313 | } 314 | 315 | /* 316 | * interpolate 317 | */ 318 | printf(" interpolating:\n"); 319 | fflush(stdout); 320 | nnai_interpolate(nn, zin, zout); 321 | if (nn_verbose) 322 | for (i = 0; i < nout; ++i) 323 | printf(" (%f, %f, %f)\n", xout[i], yout[i], zout[i]); 324 | 325 | fflush(stdout); 326 | gettimeofday(&tv2, &tz); 327 | { 328 | long dt = 1000000.0 * (tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec; 329 | 330 | printf(" interpolation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout); 331 | } 332 | 333 | if (!nn_verbose) 334 | printf(" control point: (%f, %f, %f) (expected z = %f)\n", xout[cpi], yout[cpi], zout[cpi], franke(xout[cpi], yout[cpi])); 335 | 336 | /* 337 | * interpolate one more time 338 | */ 339 | printf(" interpolating one more time:\n"); 340 | fflush(stdout); 341 | nnai_interpolate(nn, zin, zout); 342 | if (nn_verbose) 343 | for (i = 0; i < nout; ++i) 344 | printf(" (%f, %f, %f)\n", xout[i], yout[i], zout[i]); 345 | 346 | fflush(stdout); 347 | gettimeofday(&tv0, &tz); 348 | { 349 | long dt = 1000000.0 * (tv0.tv_sec - tv2.tv_sec) + tv0.tv_usec - tv2.tv_usec; 350 | 351 | printf(" interpolation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout); 352 | } 353 | 354 | if (!nn_verbose) 355 | printf(" control point: (%f, %f, %f) (expected z = %f)\n", xout[cpi], yout[cpi], zout[cpi], franke(xout[cpi], yout[cpi])); 356 | 357 | /* 358 | * change the data 359 | */ 360 | printf(" entering new data:\n"); 361 | fflush(stdout); 362 | for (i = 0; i < nin; ++i) { 363 | point* p = &pin[i]; 364 | 365 | p->z = p->x * p->x - p->y * p->y; 366 | zin[i] = p->z; 367 | if (nn_verbose) 368 | printf(" (%f, %f, %f)\n", p->x, p->y, p->z); 369 | } 370 | 371 | /* 372 | * interpolate 373 | */ 374 | printf(" interpolating:\n"); 375 | fflush(stdout); 376 | nnai_interpolate(nn, zin, zout); 377 | if (nn_verbose) 378 | for (i = 0; i < nout; ++i) 379 | printf(" (%f, %f, %f)\n", xout[i], yout[i], zout[i]); 380 | 381 | if (!nn_verbose) 382 | printf(" control point: (%f, %f, %f) (expected z = %f)\n", xout[cpi], yout[cpi], zout[cpi], xout[cpi] * xout[cpi] - yout[cpi] * yout[cpi]); 383 | 384 | /* 385 | * restore old data 386 | */ 387 | printf(" restoring data:\n"); 388 | fflush(stdout); 389 | for (i = 0; i < nin; ++i) { 390 | point* p = &pin[i]; 391 | 392 | p->z = franke(p->x, p->y); 393 | zin[i] = p->z; 394 | if (nn_verbose) 395 | printf(" (%f, %f, %f)\n", p->x, p->y, p->z); 396 | } 397 | 398 | /* 399 | * interpolate 400 | */ 401 | printf(" interpolating:\n"); 402 | fflush(stdout); 403 | nnai_interpolate(nn, zin, zout); 404 | if (nn_verbose) 405 | for (i = 0; i < nout; ++i) 406 | printf(" (%f, %f, %f)\n", xout[i], yout[i], zout[i]); 407 | 408 | if (!nn_verbose) 409 | printf(" control point: (%f, %f, %f) (expected z = %f)\n", xout[cpi], yout[cpi], zout[cpi], franke(xout[cpi], yout[cpi])); 410 | 411 | printf("\n"); 412 | 413 | nnai_destroy(nn); 414 | free(zin); 415 | free(xout); 416 | free(yout); 417 | free(zout); 418 | free(pout); 419 | delaunay_destroy(d); 420 | free(pin); 421 | 422 | return 0; 423 | } 424 | 425 | #endif 426 | -------------------------------------------------------------------------------- /natural_neighbour/nnbathy.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.nnbathy 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleShortVersionString 14 | 1.0 15 | CFBundleSignature 16 | ???? 17 | CFBundleVersion 18 | 1.0 19 | dSYM_UUID 20 | 21 | i386 22 | 93 f7 47 1a a9 8b 10 aa 1a 79 b5 fa e4 ac f0 6f 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /natural_neighbour/nnbathy.dSYM/Contents/Resources/DWARF/nnbathy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattfoster/matlab-interpolation-toolkit/0d4d7af6aa1bddb6cda0ec18049c6141ea55df6b/natural_neighbour/nnbathy.dSYM/Contents/Resources/DWARF/nnbathy -------------------------------------------------------------------------------- /natural_neighbour/nncommon-vulnerable.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: nncommon-vulnerable.c 4 | * 5 | * Created: 05/08/2004 6 | * 7 | * Author: Pavel Sakov 8 | * CSIRO Marine Research 9 | * 10 | * Purpose: Stuff for NN interpolation library found to be vulnerable 11 | * from -O2 optimisation by gcc. 12 | * 13 | * Description: None 14 | * 15 | * Revisions: 07/04/2005 PS: Changed numerics to force underflow when 16 | * there is a substantial loss of precision. 17 | * 15/04/2005 PS: Further improved numerics. Looks like it 18 | * became pretty good, so I had to split circle_build() into 19 | * circle_build1() -- for general use, and circle_build2() -- 20 | * for use in nnpi_triangle_process() (it signals loss of 21 | * precision in Watson's algorithm). 22 | * 23 | *****************************************************************************/ 24 | 25 | #include 26 | #include "delaunay.h" 27 | #include "nan.h" 28 | #include "nn.h" 29 | #include "nn_internal.h" 30 | 31 | #define MULT 1.0e+7 32 | 33 | int circle_build1(circle* c, point* p1, point* p2, point* p3) 34 | { 35 | double x2 = p2->x - p1->x; 36 | double y2 = p2->y - p1->y; 37 | double x3 = p3->x - p1->x; 38 | double y3 = p3->y - p1->y; 39 | 40 | double denom = x2 * y3 - y2 * x3; 41 | double frac = (x2 * (x2 - x3) + y2 * (y2 - y3)) / denom; 42 | 43 | if (denom == 0.0) { 44 | c->x = NaN; 45 | c->y = NaN; 46 | c->r = NaN; 47 | return 0; 48 | } 49 | 50 | c->x = (x3 + frac * y3) / 2.0; 51 | c->y = (y3 - frac * x3) / 2.0; 52 | c->r = hypot(c->x, c->y); 53 | c->x += p1->x; 54 | c->y += p1->y; 55 | 56 | return 1; 57 | } 58 | 59 | int circle_build2(circle* c, point* p1, point* p2, point* p3) 60 | { 61 | double x2 = p2->x - p1->x; 62 | double y2 = p2->y - p1->y; 63 | double x3 = p3->x - p1->x; 64 | double y3 = p3->y - p1->y; 65 | 66 | double denom = x2 * y3 - y2 * x3; 67 | double frac = (x2 * (x2 - x3) + y2 * (y2 - y3)) / denom; 68 | 69 | if (denom == 0) { 70 | c->x = NaN; 71 | c->y = NaN; 72 | c->r = NaN; 73 | return 0; 74 | } else { 75 | c->x = (x3 + frac * y3) / 2.0; 76 | c->y = (y3 - frac * x3) / 2.0; 77 | c->r = hypot(c->x, c->y); 78 | if (c->r > (fabs(x2) + fabs(x3) + fabs(y2) + fabs(y3)) * MULT) { 79 | c->x = NaN; 80 | c->y = NaN; 81 | } else { 82 | c->x += p1->x; 83 | c->y += p1->y; 84 | } 85 | return 1; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /natural_neighbour/nnconfig.h.in: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattfoster/matlab-interpolation-toolkit/0d4d7af6aa1bddb6cda0ec18049c6141ea55df6b/natural_neighbour/nnconfig.h.in -------------------------------------------------------------------------------- /natural_neighbour/nnpi.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattfoster/matlab-interpolation-toolkit/0d4d7af6aa1bddb6cda0ec18049c6141ea55df6b/natural_neighbour/nnpi.c -------------------------------------------------------------------------------- /natural_neighbour/preader.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: preader.c 4 | * 5 | * Created: 29/05/2006 6 | * 7 | * Author: Pavel Sakov 8 | * CSIRO Marine Research 9 | * 10 | * Purpose: Serial "reader" of the output point locations 11 | * 12 | * Description: Designed to be able to read one output point at a time, 13 | * either from a file or from a virtual nx x ny grid. 14 | * Allows to interpolate on a point-by-point basis and 15 | * therefore avoids allocating the whole output array in 16 | * memory. 17 | * 18 | * Revisions: None 19 | * 20 | *****************************************************************************/ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include "config.h" 30 | #include "nan.h" 31 | #include "delaunay.h" 32 | #include "nn.h" 33 | #include "nn_internal.h" 34 | #include "preader.h" 35 | 36 | #define BUFSIZE 1024 37 | 38 | typedef struct { 39 | char* fname; 40 | FILE* f; 41 | int n; 42 | point p; 43 | } reader; 44 | 45 | typedef struct { 46 | int nx; 47 | int ny; 48 | int nmax; 49 | double stepx; 50 | double stepy; 51 | double x0; 52 | int n; 53 | point p; 54 | } grid; 55 | 56 | struct preader { 57 | grid* g; 58 | reader* r; 59 | }; 60 | 61 | static grid* grid_create(double xmin, double xmax, double ymin, double ymax, int nx, int ny) 62 | { 63 | grid* g = NULL; 64 | 65 | if (nx < 1 || ny < 1) 66 | return NULL; 67 | 68 | g = malloc(sizeof(grid)); 69 | g->nx = nx; 70 | g->ny = ny; 71 | g->nmax = nx * ny; 72 | 73 | g->stepx = (nx > 1) ? (xmax - xmin) / (nx - 1) : 0.0; 74 | g->stepy = (ny > 1) ? (ymax - ymin) / (ny - 1) : 0.0; 75 | g->x0 = (nx > 1) ? xmin : (xmin + xmax) / 2.0; 76 | g->p.y = (ny > 1) ? ymin - g->stepy : (ymin + ymax) / 2.0; 77 | g->n = 0; 78 | 79 | return g; 80 | } 81 | 82 | static point* grid_getpoint(grid * g) 83 | { 84 | if (g->n >= g->nmax) 85 | return NULL; 86 | 87 | if (g->n % g->nx == 0) { 88 | g->p.x = g->x0; 89 | g->p.y += g->stepy; 90 | } else 91 | g->p.x += g->stepx; 92 | 93 | g->n++; 94 | 95 | return &g->p; 96 | } 97 | 98 | static void grid_destroy(grid * g) 99 | { 100 | free(g); 101 | } 102 | 103 | static reader* reader_create(char* fname) 104 | { 105 | reader* r = malloc(sizeof(reader)); 106 | 107 | r->fname = NULL; 108 | r->f = NULL; 109 | r->p.x = NaN; 110 | r->p.y = NaN; 111 | r->p.z = NaN; 112 | r->n = 0; 113 | 114 | if (fname == NULL) { 115 | r->fname = strdup("stdin"); 116 | r->f = stdin; 117 | } else { 118 | if (strcmp(fname, "stdin") == 0 || strcmp(fname, "-") == 0) { 119 | r->fname = strdup("stdin"); 120 | r->f = stdin; 121 | } else { 122 | r->fname = strdup(fname); 123 | r->f = fopen(fname, "r"); 124 | if (r->f == NULL) 125 | nn_quit("%s: %s\n", fname, strerror(errno)); 126 | } 127 | } 128 | 129 | return r; 130 | } 131 | 132 | static point* reader_getpoint(reader * r) 133 | { 134 | char buf[BUFSIZE]; 135 | char seps[] = " ,;\t"; 136 | char* token; 137 | point* p = &r->p; 138 | 139 | if (r->f == NULL) 140 | return NULL; 141 | 142 | while (1) { 143 | if (fgets(buf, BUFSIZE, r->f) == NULL) { 144 | if (r->f != stdin) 145 | if (fclose(r->f) != 0) 146 | nn_quit("%s: %s\n", r->fname, strerror(errno)); 147 | r->f = NULL; 148 | 149 | return NULL; 150 | } 151 | 152 | if (buf[0] == '#') 153 | continue; 154 | if ((token = strtok(buf, seps)) == NULL) 155 | continue; 156 | if (!str2double(token, &p->x)) 157 | continue; 158 | if ((token = strtok(NULL, seps)) == NULL) 159 | continue; 160 | if (!str2double(token, &p->y)) 161 | continue; 162 | r->n++; 163 | 164 | return p; 165 | } 166 | } 167 | 168 | static void reader_destroy(reader * r) 169 | { 170 | if (r->f != stdin && r->f != NULL) 171 | if (fclose(r->f) != 0) 172 | nn_quit("%s: %s\n", r->fname, strerror(errno)); 173 | free(r->fname); 174 | free(r); 175 | } 176 | 177 | preader* preader_create1(double xmin, double xmax, double ymin, double ymax, int nx, int ny) 178 | { 179 | preader* pr = malloc(sizeof(preader)); 180 | 181 | pr->r = NULL; 182 | pr->g = grid_create(xmin, xmax, ymin, ymax, nx, ny); 183 | 184 | return pr; 185 | } 186 | 187 | preader* preader_create2(char* fname) 188 | { 189 | preader* pr = malloc(sizeof(preader)); 190 | 191 | pr->g = NULL; 192 | pr->r = reader_create(fname); 193 | 194 | return pr; 195 | } 196 | 197 | point* preader_getpoint(preader * pr) 198 | { 199 | if (pr->g != NULL) 200 | return grid_getpoint(pr->g); 201 | else 202 | return reader_getpoint(pr->r); 203 | } 204 | 205 | void preader_destroy(preader * pr) 206 | { 207 | if (pr->g != NULL) 208 | grid_destroy(pr->g); 209 | else 210 | reader_destroy(pr->r); 211 | 212 | free(pr); 213 | } 214 | -------------------------------------------------------------------------------- /natural_neighbour/preader.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: preader.h 4 | * 5 | * Created: 29/05/2006 6 | * 7 | * Author: Pavel Sakov 8 | * CSIRO Marine Research 9 | * 10 | * Purpose: A header file with preader.c 11 | * 12 | * Revisions: None 13 | * 14 | *****************************************************************************/ 15 | 16 | #if !defined(_PREADER_H) 17 | #define _PREADER_H 18 | 19 | struct preader; 20 | typedef struct preader preader; 21 | 22 | preader* preader_create1(double xmin, double xmax, double ymin, double ymax, int nx, int ny); 23 | preader* preader_create2(char* fname); 24 | point* preader_getpoint(preader * pr); 25 | void preader_destroy(preader * pr); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /natural_neighbour/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattfoster/matlab-interpolation-toolkit/0d4d7af6aa1bddb6cda0ec18049c6141ea55df6b/natural_neighbour/test -------------------------------------------------------------------------------- /natural_neighbour/version.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * File: version.h 4 | * 5 | * Created: 18/10/2001 6 | * 7 | * Author: Pavel Sakov 8 | * CSIRO Marine Research 9 | * 10 | * Purpose: Contains version string 11 | * 12 | *****************************************************************************/ 13 | 14 | #if !defined(_VERSION_H) 15 | #define _VERSION_H 16 | 17 | char* nn_version = "1.71"; 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /rbf/rbf.m: -------------------------------------------------------------------------------- 1 | function [varargout] = rbf(xi, yi, zi, xx, yy, basis_func, lambda, log_fudge) 2 | % RBF Perform radial basis function interpolation. 3 | % zz = rbf(xi, yi, zi, x, y, basis_function) 4 | % 5 | % Interpolate the scattered values xi, yi, zi at xx, yy (which should be plaid) 6 | % 7 | % Functionality is similar to griddata. 8 | % 9 | % Available basis functions: 10 | % euclidean Default. Also fits first order 11 | % polynomial surface. 12 | % thin_plate_spline / biharmonics These are the same. 13 | % Also fits polynomial surface. 14 | % gaussian Constant not implemented. 15 | % multiquadratic Constant not implemented. 16 | % triharmonic 17 | % 18 | % lambda: 19 | % regularisation parameter see [2]. With regularisation, the interpolation 20 | % condition is relaxed, and the fitted surface will be smoothed. Setting this 21 | % to zero implies interpolation, and high values reduce to a least-squares 22 | % affine model. 23 | % 24 | % log_fudge: 25 | % what to do when when radius = 0. Generally you'll want this to be 0 too. 26 | % 27 | % Example: 28 | % [yy, xx] = meshgrid(1:100, 1:100); 29 | % % Make some correlated data. 30 | % input = corr_data(100, 15); 31 | % aa = rand(100) > 0.95; 32 | % sampled = input.*aa; 33 | % [xi, yi, zi] = find(sampled); 34 | % 35 | % [zz, time] = rbf(xi, yi, zi, xx, yy); 36 | % [zz_tps, time] = rbf(xi, yi, zi, xx, yy, 'thin_plate_spline') 37 | % 38 | % rmse_zz = sqrt(mean((input(:) - zz(:)).^2)) 39 | % rmse_zz_tps = sqrt(mean((input(:) - zz_tps(:)).^2)) 40 | % 41 | % 42 | % Reference: 43 | % [1] Surface interpolation with radial basis functions, 44 | % Carr et al, 45 | % IEEE Trans. on Medical Imaging. 46 | % Vol 16. No. 1, Feb. 1997 47 | % 48 | % [2] Shape matching and object recognition using contexts. 49 | % Belongie et al, 50 | % IEEE Trans. Pattern Analysis and Machine Intelligence 51 | % Vol. 24, No. 24, April 2002 52 | % 53 | % Matt Foster 54 | 55 | % Start timer 56 | tic; 57 | 58 | % Check input and output argument numbers 59 | error(nargchk(5, 8, nargin, 'struct')); 60 | error(nargoutchk(1, 4, nargout, 'struct')) 61 | 62 | % Change this to control the behaviour of log(0). 63 | if nargin < 8 64 | log_fudge = 0; 65 | end 66 | 67 | if nargin >= 6 68 | basis_func = str2func(basis_func); 69 | else 70 | basis_func = @euclidean; 71 | end 72 | 73 | [x1,x2] = meshgrid(yi); 74 | [y1,y2] = meshgrid(xi); 75 | 76 | [basis, poly_order] = basis_func(x1, x2, y1, y2, log_fudge); 77 | 78 | if nargin >= 7 79 | scale = euclidean(x1, x2, y1, y2); 80 | scale = mean(scale(:)); 81 | lambda = scale.^2 .* lambda; 82 | else 83 | lambda = 0; 84 | end 85 | 86 | % Perform regularisation (or not) 87 | basis = basis + lambda * eye(size(basis)); 88 | 89 | % So far I've only seen first order polynomials, or none 90 | if poly_order == 0 91 | mat = basis; 92 | ff = zi; 93 | elseif poly_order == 1 94 | q = [ones(length(xi), 1), xi, yi]; 95 | mat = [basis, q; q', zeros(3)]; 96 | ff = [zi; zeros(3,1)]; 97 | end 98 | 99 | % Things below could get quite memory intensive. 100 | clear basis; 101 | 102 | % Get the coefficients. 103 | lam_c = mat \ ff; 104 | 105 | % Extract the poly coefficients. 106 | lam = lam_c(1:end-poly_order*2-1*poly_order); 107 | c = lam_c(end-poly_order*2:end); 108 | clear lam_c; 109 | clear mat; 110 | 111 | % Now reconstruct everything: 112 | if poly_order > 0 113 | poly = c(1) + xx.*c(2) + c(3).*yy; 114 | else 115 | poly = zeros(size(xx)); 116 | end 117 | 118 | % Create matric for output 119 | zz = zeros(size(xx)); 120 | 121 | %---------------------------------------------------- 122 | % for ii = 1:length(xx(:)) 123 | % % loop over inputs 124 | % for kk = 1:length(zi) 125 | % norm = basis_func(xx(ii), xi(kk), yy(ii), yi(kk), log_fudge); 126 | % zz(ii) = zz(ii) + lam(kk).*norm; 127 | % end 128 | % end 129 | %---------------------------------------------------- 130 | 131 | % This is a vectorised version of the above: 132 | for ii = 1:length(xx(:)) 133 | 134 | nm = basis_func( ... 135 | repmat(xx(ii), [size(xi(:), 1), 1]), ... 136 | xi(:), ... 137 | repmat(yy(ii), [size(yi(:), 1), 1]), ... 138 | yi(:), ... 139 | log_fudge); 140 | 141 | zz(ii) = sum(lam .* nm); 142 | end 143 | 144 | % Add the polynomial 145 | zz = zz + poly; 146 | 147 | % stop timer 148 | time = toc; 149 | 150 | % 4 output args -> x, y, z, time 151 | % 3 output args -> x, y, z 152 | % 2 output args -> z, time 153 | % 1 output arg -> z 154 | if nargout == 4 155 | varargout{1} = xx; 156 | varargout{2} = yy; 157 | varargout{3} = zz; 158 | varargout{4} = time; 159 | elseif nargout == 3 160 | varargout{1} = xx; 161 | varargout{2} = yy; 162 | varargout{3} = zz; 163 | elseif nargout == 2 164 | varargout{1} = zz; 165 | varargout{2} = time; 166 | elseif nargout == 1 167 | varargout{1} = zz; 168 | end 169 | 170 | end % rbf 171 | 172 | % Basis functions 173 | % mlint says these are never used.. it's wrong! 174 | 175 | 176 | % Poly is an integer detailing the order of polynomial to use. 177 | 178 | % Euclidean distance -- Linear basis function 179 | function [bf, poly] = euclidean(x1, x2, y1, y2, varargin) %#ok 180 | bf = sqrt((x1 - x2).^2 + (y1 - y2).^2); 181 | poly = 1; 182 | end 183 | 184 | % Thin plate spline 185 | function [bf, poly] = thin_plate_spline(x1, x2, y1, y2, varargin) %#ok 186 | rr = euclidean(x1, x2, y1, y2); 187 | % Not 100% sure what base to use for the log. 188 | bf = rr.^2 .* log(rr); 189 | bf(rr == 0) = varargin{1}; 190 | poly = 1; 191 | end 192 | 193 | % Gaussian 194 | function [bf, poly] = gaussian(x1, x2, y1, y2, varargin) %#ok 195 | rr = euclidean(x1, x2, y1, y2); 196 | if nargin < 5 197 | aa = 1; 198 | else 199 | aa = varargin{1}; 200 | end 201 | bf = exp(aa.*rr.^2); 202 | % No polynomial here. 203 | poly = 0; 204 | end 205 | 206 | % Multiquadratic 207 | function [bf, poly] = multiquadratic(x1, x2, y1, y2, varargin) %#ok 208 | rr = euclidean(x1, x2, y1, y2); 209 | if nargin < 5 210 | cc = 1; 211 | else 212 | cc = varargin{1}; 213 | end 214 | bf = sqrt(rr.^2 + cc.^2); 215 | poly = 0; 216 | end 217 | 218 | % Triharmonic Spline -- C2 Continuity 219 | function [bf, poly] = triharmonic_spline(x1, x2, y1, y2, varargin) %#ok 220 | rr = euclidean(x1, x2, y1, y2); 221 | bf = rr.^4 .* log(rr); 222 | bf(rr == 0) = varargin{1}; 223 | poly = 0; 224 | end 225 | -------------------------------------------------------------------------------- /simulation/corr_data.m: -------------------------------------------------------------------------------- 1 | function c = corr_data(dim, disk) 2 | % Create a random signal, and impose correlation. 3 | % c = corr_data(dim, disk) 4 | % 5 | % Required arguments: 6 | % 'dim' = size of the field. 7 | % 'disk' = size of the disc used to filter the field. 8 | % 9 | % Literature: 10 | % THE VARIOGRAM AND ITS ESTIMATION, Omre, 1984 11 | % 12 | % See also: 13 | % corr_data_uni 14 | % 15 | % Matt Foster 16 | 17 | if nargin < 2 18 | error('Expected 2 arguments') 19 | end 20 | 21 | if any(size(disk) ~= 1) 22 | error('Disc size should be a single integer') 23 | end 24 | 25 | a = randn(dim); 26 | f = fspecial('disk', disk); 27 | c = filter2(f, a); 28 | 29 | c = c + abs(min(c(:))); 30 | c = c./max(c(:)); 31 | -------------------------------------------------------------------------------- /simulation/corr_data_uni.m: -------------------------------------------------------------------------------- 1 | function c = corr_data_uni(dim, disk, pad, nh) 2 | % Create a random signal, and impose correlation (lognormal version). 3 | % c = corr_data_uni(dim, disk, pad, nh) 4 | % 5 | % Required arguments: 6 | % 'dim' = size of the field. 7 | % 'disk' = size of the disc used to filter the field. 8 | % Optional arguments: 9 | % 'pad' = size of padding (default 50) 10 | % 'nh' = size of neighbourhood (default 2) used for multinormality reduction. 11 | % 12 | % Literature: 13 | % THE VARIOGRAM AND ITS ESTIMATION, Omre, 1984 14 | % 15 | % See also: 16 | % corr_data 17 | % 18 | % Matt Foster 19 | 20 | error(nargchk(2, 4, nargin)); 21 | 22 | if nargin < 3 23 | pad = 50; 24 | end 25 | 26 | if nargin < 4 27 | nh = 2; % 2*nh+1 square neighbourhood 28 | end 29 | 30 | if any(size(disk) ~= 1) 31 | error('Disc size should be a single integer') 32 | end 33 | 34 | 35 | % Get some multinormal data 36 | d = corr_data(dim+(2*pad), disk); 37 | 38 | % remove multinormality 39 | % Look at local neighbourhood 5x5 mask choose randomly from 40 | % top 10 41 | 42 | for ii = pad:size(d,1)-pad-1 43 | for jj = pad:size(d,nh)-pad-1 44 | nhood = d(ii-nh:ii+nh, jj-nh:jj+nh); 45 | sv = sort(nhood(:),'descend'); 46 | pos = ceil(10*rand(1)); 47 | new(ii-pad+1,jj-pad+1) = sv(pos); 48 | end 49 | end 50 | 51 | % Make the histogram look more like omre's 52 | c = abs(reallog(new)); 53 | --------------------------------------------------------------------------------