function [XPoints, YPoints, Angles] = filament_follow(SX, SY, TData, delta_t)
% function [XPoints, YPoints, Angles] = filament_follow(SX, SY, TData, delta_t)
%
% Return points (XPoints,YPoints) spaced every delta_t pixels along the
% segmented line determined by verticies (SX, SY)
%
% SE = enable vector

XPoints = [];
YPoints = [];
Angles = [];

% :: Interpolation along the filament

% Find the values of the "parameter" along the line segments

ST = zeros(length(SX));
for n=2:length(SX),
  ST(n) = ST(n-1) + sqrt((SX(n) - SX(n-1))^2 + (SY(n) - SY(n-1))^2);
end

% Now we "drive"!! (c.f. REM)

n = 1;
for t=0:delta_t:max(ST),
  % From the value of the "parameter" (t) we have to interpolate
  % an (x,y) position and also the orientation angle at that
  % point.  We'll just interpolate linearly.
  
  while ((t<ST(n)) | (t>ST(n+1))), n = n+1; end
  
  % ST(n) <= t < ST(n+1)

  if (length(TData)>n) & (~TData(n+1)),
    continue;       % turn off picking until we pass this vertex
  end
  
  % right now we should be between picks N and N+1
  
  alpha = (ST(n+1) - t) / (ST(n+1) - ST(n));
  beta = 1 - alpha;
  
  % interpolate the position
  
  x = round(alpha * SX(n) + beta * SX(n+1));
  y = round(alpha * SY(n) + beta * SY(n+1));
  
  % Compute the angle of this filament segment and add it to the list
  
  Angles = [ Angles rad2deg(atan2(SY(n+1)-SY(n),SX(n+1)-SX(n))) ];
  
  XPoints = [XPoints x];
  YPoints = [YPoints y];
end

