%This software implements the point matching algorithm described in the paper:
%"Rotation Invariant Nonrigid Point Set Matching in Cluttered Scenes", IEEE transaction on Image Processing, 2012

%Author: Wei Lian (Changzhi University, Changzhi, 046011, Shanxi Province, P.R. China, Email: lianwei3 at gmail.com)

%If you use this software for research purposes, you should cite the aforementioned paper in any resulting publications.


function neig=nearest_neighbour_radius(dist,rad,num)
%%rad: radius of circle within which points are retained.
%%num: minimum number of nearest neighbours
%%neig: matrix contains indices of nearest neighbours for each point, num x N

N=size(dist,1);
neig=cell(N,1);
for ii=1:N
    ind=find(dist(:,ii)<rad);    
    while size(ind,1)<num
        dist(ind,ii)=inf;
        ind2=find(dist(:,ii)==min(dist(:,ii)));
        ind=[ind;ind2(1)];
    end        
    neig{ii}=ind;
end
