function mesh = meshpath(path, Le)
%MESHPATH Create line mesh from a path of lines and Bezier Curves
% MESH = MESHPATH(PATH, L) creates a NiHu mesh MESH from the eps path data
% PATH. The path is meshed by applying an approximate uniform element
% length L.
%
% See also: READ_EPSPATH

%   Copyright 2008-2010 P. Fiala
%   Budapest University of Technology and Economics
%   Dept. of Telecommunications

% Last updated: 2012.12.19.

%
buff_size = 1024;
nodes = zeros(buff_size, 4);
nodes_size = buff_size;

elems = zeros(buff_size, 6);
elems_size = buff_size;

node_cnt = 0;
elem_cnt = 0;

nP = size(path,1);
for iP = 1 : nP
    switch path(iP,1)
        case 1
            r0 = bezier(reshape(path(iP,[2 3 2 3 4 5 4 5]).',2,4).', Le);
        case 2
            r0 = bezier(reshape(path(iP,2:9).',2,4).', Le);
    end
    n = size(r0,1);
    if nodes_size < node_cnt + n
        nodes_size = nodes_size + buff_size;
        nodes(nodes_size, size(nodes, 2)) = 0;
    end
    nodes(node_cnt + (1:n).', :) = [node_cnt + (1:n).', r0, zeros(n,1)];
    
    if elems_size < elem_cnt + (n-1)
        elems_size = elems_size + buff_size;
        elems(elems_size, size(elems, 2)) = 0;
    end
    elems(elem_cnt + (1:n-1).', :) = ...
        [elem_cnt + (1:n-1).', repmat([12,1,1], n-1, 1), node_cnt+(1:n-1).' node_cnt+(2:n).'];
    
    % Increase counters
    node_cnt = node_cnt + n;
    elem_cnt = elem_cnt + (n - 1);
    
    progbar(1, nP, iP);
end

mesh.Nodes = nodes(1 : node_cnt, :);
mesh.Elements = elems(1 : elem_cnt, :);
[mesh.Materials, mesh.Properties] = default_mat_prop();

end % of function meshpath

%
function r = bezier(R, Le)
%BEZIER Coordinates of Bezier curve
%  R = BEZIER(P, L) returns the coordinates of a cubic Bezier curve
%  defined by 4 control points. The control points are given in the rows of
%  the 4x2 or 4x3 matrix P. The curve is discretized so that the
%  approximate element length is L.
%
% Last updated: 21.11.2009
t = linspace(0, 1, 1001).'; % paramteter space
r = [(1-t).^3, 3*(1-t).^2.*t, 3*(1-t).*t.^2, t.^3] * R;  % curve coord
dr = diff(r,1,1);
ds = [0; cumsum(sqrt(dot(dr, dr, 2)))]; % element lengths
[ds, n] = unique(ds);
L = ds(end);        % length of the Bezier curve
N = ceil(L / Le);   % number of segments
l = (0:N)*L/N;      % divide into equal long segments
r = interp1(ds, r(n,:), l, 'linear', 'extrap');
end
