QueryNeighborhood

PURPOSE ^

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 classdef QueryNeighborhood
0002     %QUERYNEIGHBORHOOD Summary of this class goes here
0003     %   Detailed explanation goes here
0004     
0005     properties
0006         data;
0007         searchType;
0008         distanceType;
0009         searchKNN;
0010         searchRadius
0011     end
0012     
0013     methods
0014         function obj = QueryNeighborhood(data, varargin)
0015             % Validator functors
0016             validData = @(x) size(x, 1) > 0 && size(x, 2) == 3;
0017             validScalarGreaterThanZero = @(x) isscalar(x) && x > 0;
0018             validSearchType = @(x) ischar(x) && strcmpi(x, 'radial') || strcmpi(x, 'knn');
0019             validDistanceType = @(x) ischar(x) && strcmpi(x, 'euclidean') || strcmpi(x, 'haversine');
0020             
0021             % Parse the parameters using inputParser
0022             p = inputParser;
0023             addRequired(p, 'data', validData);
0024             addParameter(p, 'Radius', 5, validScalarGreaterThanZero);    
0025             addParameter(p, 'SearchType', 'radial', validSearchType);
0026             addParameter(p, 'K', 25, validScalarGreaterThanZero);
0027             addParameter(p, 'DistanceType', 'euclidean', validDistanceType);
0028             parse(p, data, varargin{:});
0029             
0030             % Retrieve the members from the inputParser
0031             obj.data = p.Results.data;
0032             obj.searchRadius = p.Results.Radius;
0033             obj.searchKNN = p.Results.K;
0034             obj.searchType = p.Results.SearchType;
0035             obj.distanceType = p.Results.DistanceType;
0036         end
0037         
0038         function [ind, distances] = getNeighbors(obj, x, y)
0039             %%Get the neighbors to a query point
0040             if strcmpi(obj.searchType, 'radial')
0041                 if strcmpi(obj.distanceType, 'euclidean')
0042                     [ind, distances] = rangesearch(obj.data(:, 1:2), [x(:) y(:)], obj.searchRadius);
0043                 else
0044                     [ind, distances] = rangesearch(obj.data(:, 1:2), [x(:) y(:)], obj.searchRadius, 'Distance', @haversine);
0045                 end
0046             else
0047                 if strcmpi(obj.distanceType, 'euclidean')
0048                     [ind, distances] = knnsearch(obj.data(:, 1:2), [x(:) y(:)], 'K', obj.searchKNN);
0049                 else
0050                     [ind, distances] = knnsearch(obj.data(:, 1:2), [x(:) y(:)], 'K', obj.searchKNN, 'Distance', @haversine);
0051                 end
0052                 % For consistency with range search
0053                 ind = num2cell(ind, 2);
0054                 distances = num2cell(distances, 2);
0055             end
0056         end
0057     end
0058 end
0059

Generated on Thu 10-Dec-2020 17:34:27 by m2html © 2005