%Attempts to detect whether the input image is a gear using c Fourier
%coefficients and a threshold of t coefficients to compare with the circle.
%c defaults to the size of the Fourier array. t defaults to 10.

%Gears are circular and contain teeth.

function [result, teeth, distfft] = isGear(inImg, c, t)
    %Analyze the centroid distance function.
    cd = centdist(inImg);
    
    if (~exist('c', 'var'))
        c = size(cd, 2);
    end
    
    if (~exist('t', 'var'))
        t = 10;
    end
    
    distfft = abs(fft(cd, c));
    
    %Use half of the coefficients.
    distfft = (distfft(1:floor(c/2)) ./ distfft(1));
    if (size(distfft, 2) < t+1)
        %If we don't even have t coefficients, this is not a gear.
        result = 0;
        teeth = 0;
        return
    end
    
    [maxval, maxindex] = max(distfft(2:t));
    [maxval, maxtoothindex] = max(distfft((t+1):size(distfft, 2)));
    
    meancoef = mean(distfft(2:size(distfft, 2)), 2);
    stdcoef = std(distfft(2:size(distfft, 2)), 0, 2);
    
    result = (maxindex == 1 && abs(maxval - meancoef) > 2*stdcoef);
    
    %We used an offset of t when calculating the index, so add it back.
    teeth = maxtoothindex + t;
end