function labels = TGTS3(f)
%
% TGTS3: Tom's Gradient Topographic Sketch: uses directions only
%    labels = TGTS3(f)

[m,n] = size(f);
[fc,fr] = gradient(f);
gdir = eightdir(fc,fr);   % convert gradient vector into 8 neighbor dirs
labels = zeros(m,n);
for r = 3:m-2
  for c = 3:n-2
    label = 0;
    p1 = gdir(r,c+2);    p2 = gdir(r-1,c+2);  p3 = gdir(r-2,c+2);
    p4 = gdir(r-2,c+1);  p5 = gdir(r-2,c);    p6 = gdir(r-2,c-1);
    p7 = gdir(r-2,c-2);  p8 = gdir(r-1,c-2);  p9 = gdir(r,c-2);
    p10 = gdir(r+1,c-2); p11 = gdir(r+2,c-2); p12 = gdir(r+2,c-1);
    p13 = gdir(r+2,c);   p14 = gdir(r+2,c+1); p15 = gdir(r+2,c+2);
    p16 = gdir(r+1,c+2);
    A = [p1,p2,p16]; B = [p4,p5,p6]; C = [p8,p9,p10];   D = [p12,p13,p14];
    E = [p2,p3,p4];  F = [p6,p7,p8]; G = [p10,p11,p12]; H = [p14,p15,p16];

    if dclose(A,C,2,r,c+2,p1,r,c-2,p9)
      label = 1;
    end;
    if dclose(B,D,2,r-2,c,p5,r+2,c,p13) 
      label = 1;
    end;
    if dclose(E,G,2,r-2,c+2,p3,r+2,c-2,p11) 
      label = 1;
    end;
    if dclose(F,H,2,r-2,c-2,p7,r+2,c+2,p15) 
      label = 1;
    end;
    labels(r,c) = label;
  end
end
