function s_model = model_ice_model( S, img, k1, k2 )
% Model-ice model
% 
% Input data:
% S:    structure data of rectangularized floes, includes: Vertices, 
%       Center, Area, Perimeter
% img:   segmented binary image
% k1:   minimum length/width radio of model ice floe
% k2:   maximum length/width radio of model ice floe
% 
% Output data:
% s_model:   structure data, includes: Vertices, Center, Area, Perimeter,
%            and overlapping floes 
%
%    Copyright:     NTNU, Department of Marine Technology
%    Project:	    Arctic DP, RCN no.199567
%    Author:        Qin Zhang
%    Date created:  2015.08.07  Qin Zhang.
%    Revised:      	2013.08.07


%%
bw = zeros(size(img));
s_model = [];

% find the rectangularized floes who's L/W is between k1 and k2
ss = [];
for i = 1 : length(S)
    v = cat(1, S(i).Vertices);
    
    % length/width radio 
    k = (sqrt((v(1,1,1)-v(2,1,1))^2+(v(1,2,1)-v(2,2,1))^2))/...
        (sqrt((v(3,1,1)-v(2,1,1))^2+(v(3,2,1)-v(2,2,1))^2));  
    if k < k2 && k > k1
        bb = roipoly(double(bw), v(:, 1), v(:, 2));  % region of interested
        bw(bb == 1) = 1;    % model floe
        ss = [ss, S(i)];
    end
    
end

figure, imshow(bw), hold on,
for i = 1 : length(ss)
    v = cat(1, ss(i).Vertices);
    c = cat(1, ss(i).Center);
    line(v(:, 1), v(:, 2));
    plot(c(1), c(2), 'r+');
    
    inter = [];
    for j = 1 : length(ss)
        if j ~= i
            vj = cat(1, ss(j).Vertices);
            [xx, yy] = polybool('intersection', v(:,1), v(:,2), vj(:,1), vj(:,2));
            if xx ~= NaN
                inter = [inter; j];
            end
        end
    end
    
    a = cat(1, ss(i).Area);
    p = cat(1, ss(i).Perimeter);
    
    
    s_model0 = struct('Vertices', v, 'Center', c, 'Area', a, 'Perimeter', p, 'Intersection', inter);
    s_model = [s_model; s_model0];
    
end   

hold off
    


