% Mika Juntunen / 19.1.2007
%
% This builds the elongationmatrix of struss system.
%
% function A = build_elongationmatrix(joints,bars)
%
% input:
% joints - 2xN matrix containing the location of joints
% bars - 2xN matrix containing the bars i.e. one column is the
%        joints that are connected
%
% output:
% A - elongation matrix connectin movement of the joints and
%     elongation i.e. e=Ax
%

function A = build_elongationmatrix(joints,bars)

% number of joints and bars
Nj = length(joints);
Nb = length(bars);

% initialize sparse elongationmatrix
A = sparse(Nb,2*Nj);

% loop over bars and add values to A
for i = 1:Nb
  % get tanget vector to bar
  tv = joints(:,bars(2,i),:)-joints(:,bars(1,i));
  
  % get angles
  theta(1) = abs(atan2(tv(2),tv(1)));
  theta(2) = abs(atan2(-tv(2),-tv(1)));
  
  % set values to matrix
  for k=1:2
    % find offset in matrix A
    offset = (bars(k,i)-1)*2;

    % set values to matrix
    A(i,offset+1) = -cos(theta(k));
    A(i,offset+2) = -sin(theta(k));
  end
end
