function basis = ClosCircBasis(xi,N)

%
% basis = ClosCircBasis(xi,N)
%
% This function evaluates the circumferential basis functions
% for a closure basis function at the specified xi point.
%
% xi - the xi location at which the basis functions are to
%      be evaluated
% N - the number of perimeter nodes
%
% Mark Trew - July 2001
%

% auxiliary sector calculations
  i = min(fix(xi*N)+1,N); % sector index
  phi = xi*N-(i-1);       % sector coordinate

% evaluate the 1D sector basis function to use in determining
% the closure circumferential basis function
  PHI = CH(phi); % cubic hermite variation

% initialise apex basis functions
  basis(1,1) = 1;
  basis(1,2) = 0;
  basis(1,3) = 0;

  for p=2:N+1,
  % calculate angle for perimeter node p
    theta = 2*(p-2)*pi/N;

  % evaluate basis function for perimeter node p
    sector_node1_mask = fix(i/(p-1))*fix((p-1)/i);
    sector_node2_mask = fix(i/(p-2+N*max(3-p,0)))*fix((p-2+N*max(3-p,0))/i); 
    basis(p,1) = sector_node1_mask*PHI(1,1) + sector_node2_mask*PHI(2,1);
    basis(p,2) = sector_node1_mask*PHI(1,2) + sector_node2_mask*PHI(2,2);

  % evaluate basis functions for apex node
    basis(1,2) = basis(1,2) + basis(p,1)*cos(theta)-2*pi*basis(p,2)*sin(theta)/N;
    basis(1,3) = basis(1,3) + basis(p,1)*sin(theta)+2*pi*basis(p,2)*cos(theta)/N;
  end;

return;
