function [dbin,phibin,thdbin]=getbins(d,phi,thd,dbinsz,phibinsz,thdbinsz)
% Get histogram bin numbers given an edge pair geometry.
%
% Parameters:
%
%   d           Distance between the pairs
%   phi         Direction of target edge
%   thd         Orientation difference
%   dbinsz      Size of a distance bin
%   phibinsz    Size of a direction bin in radians
%   thdbinsz    Size of a orientation difference bin in radians
%
% Return:
%
%   dbin    The distance bin number
%   phibin  The direction bin number
%   thdbin  The orientation difference bin number
%
% Copyright (C) 2001-2009 Center for Perceptual Systems

% We want direction bin number 0 to be centered on the x
% axis so that it includes angles that are +- phibinsz/2
% radians.
phi=phi+phibinsz/2;

% Same goes for orientation
thd=thd+thdbinsz/2;

% Make phi and thd lie in range 0 to 2*pi
phi=mod(phi,2*pi);
thd=mod(thd,2*pi);

% Compute bin index
dbin=floor(d/dbinsz)+1;
phibin=floor(phi/phibinsz)+1;
thdbin=floor(thd/thdbinsz)+1;
